Refactoring lots of stuff.

This commit is contained in:
2025-05-29 17:45:48 -05:00
parent e6022898e5
commit 474b62f267
7 changed files with 282 additions and 165 deletions

View File

@@ -0,0 +1,11 @@
#pragma once
#include <JUI/Widgets/Window.hpp>
class AboutDialog : public JUI::Window {
public:
protected:
private:
};

View File

@@ -68,8 +68,6 @@ class EditHistoryLogWindow : public JUI::Window {
public:
};
class EditorApp : public OpenGLWindow {
public:
#pragma region GUI Elements
@@ -81,6 +79,9 @@ public:
LayerView* layer_view = nullptr;
JUI::Window* bg_color_tool_window = nullptr;
JUI::ColorPicker* bg_color_tool = nullptr;
JUI::UtilityBar* topbar = nullptr;
JUI::Window* app_info_dialog = nullptr;
#pragma endregion
#pragma region Map Data
@@ -91,6 +92,9 @@ public:
EditorCamera camera;
JUI::Window * CreateAppInfoDialogWindow(JUI::Widget *parent);
Preferences preferences;
@@ -126,8 +130,7 @@ public:
//std::vector<Quad> quads;
/// Fill the tile-id lookup table with their respective partial-sprite bounding boxes.
void PopulateQuads();
/// This provides a mapping from a 1D flat array to a 2D grid.
/// @return The {x,y} coordinate of the grid-cell that maps to the given index.
@@ -142,7 +145,6 @@ public:
EditorApp();
~EditorApp() override
{
//DeleteGrid();
}
void LoadPreferences();
@@ -158,10 +160,7 @@ public:
void SaveTestFile();
void LoadLevel(const std::filesystem::path& level_meta_path);
/// Loads test-data, including a tilesheet, and a binary level-data file.
/// @note Placeholder until FileDialogs are added.
/// @note Placeholder until Tileset json is added.
void LoadMisc();
void SizeCellIndicatorToTilesetSize();
/// Creates a JUI widget that displays the Tileset, and lets you select a tile from it.
@@ -191,6 +190,7 @@ public:
void PlaceTileBrush(const Vector2i& cell);
void EraseTile(const Vector2i& cell);
bool IsMouseFocusedAnyActiveWidget();
void Update(float elapsed);
@@ -207,105 +207,28 @@ public:
void DrawTiles();
void DrawLayer(const Layer* layer) const
{
for (int gx = 0; gx < layer->rows; gx++) {
for (int gy = 0; gy < layer->cols; gy++) {
void DrawLayer(const Layer* layer) const;
auto quad_idx = layer->cells[gx][gy];
Vector2 pos(gx*layer->cell_width, gy*layer->cell_height);
//if ((gx+gy) % 2 == 0)
//J2D::FillRect(Colors::Purples::Fuchsia, pos, {16, 16});
if (quad_idx < 0)
continue;
auto quad = loaded_tileset->quads[quad_idx];
J2D::DrawPartialSprite(loaded_tilesheet, pos,
Vector2(quad.x, quad.y), Vector2(quad.w, quad.h));
}
}
}
void DrawLevel(const Level* level)
{
// TODO: Draw Per-Level Background Texture.
// TODO: Draw Per-Layer Background Texture.
for (const auto* layer : level->layers)
{
DrawLayer(layer);
}
// TODO: Support a handful of presets to represent entity shape types.
for (auto entity : level->entities)
{
J2D::DrawPoint(entity->overlay_color, entity->x, entity->y, 1);
J2D::DrawString(Colors::White, std::format("{} - {}", entity->type, entity->name), entity->x, entity->y, 1.f, 12);
}
}
void DrawLevel(const Level* level) const;
void Draw();
// TODO: Closing the app nominally doesn't work on Linux.
void OnClosing() override;
void OnRefresh(float elapsed) override {
Update(elapsed);
Draw();
SwapBuffers();
}
/// Performs a single step of the entire program logic. Update, then Draw, then Swapbuffers.
void OnRefresh(float elapsed) override;
enum JUI::MouseButton ToJUIEnum(const MouseButton& btn) {
if (btn == MouseButtons::Left) return JUI::MouseButton::Left;
if (btn == MouseButtons::Middle) return JUI::MouseButton::Middle;
if (btn == MouseButtons::Right) return JUI::MouseButton::Right;
// Default condition.
return JUI::MouseButton::Left;
}
enum JUI::MouseButton ToJUIEnum(const MouseButton& btn);
void OnMouseButtonDown(const MouseButtonDownEvent &e) override
{
auto btn = ToJUIEnum(e.Button);
void OnMouseButtonDown(const MouseButtonDownEvent &e) override;
if (scene->ObserveMouseInput(btn, true)) return;
void OnMouseButtonUp(const MouseButtonUpEvent &e) override;
// Tile-pick - Middle Click
if (btn == JUI::MouseButton::Middle)
{
Vector2i cell = GetGridCellFromMouse();
selected_quad = loaded_level->layers[0]->cells[cell.x][cell.y];
}
}
void OnMouseMove(const MouseMoveEvent &e) override;
void OnMouseButtonUp(const MouseButtonUpEvent &e) override {
auto btn = ToJUIEnum(e.Button);
if (scene->ObserveMouseInput(btn, false)) return;
void OnKeyDown(const KeyDownEvent &e) override;
if (tileset_viewer->Content()->IsMouseInside()) {
Vector2i cell = GetTilesetCellFromMouse();
int index = CellToIndex(cell, loaded_tileset->rows);
selected_quad = index;
}
// TODO: This always runs even if we release the mouse inside the window. ObserveMouseInput should handle the case.
}
void OnMouseMove(const MouseMoveEvent &e) override {
Vector2 mposv2(e.Position.x, e.Position.y);
if (scene->ObserveMouseMovement(mposv2)) return;
}
void OnKeyDown(const KeyDownEvent &e) override {
if (scene->ObserveKeyInput(e.key, true)) return;
}
void OnKeyUp(const KeyUpEvent &e) override {
if (scene->ObserveKeyInput(e.key, false)) return;
}
void OnKeyUp(const KeyUpEvent &e) override;
};

View File

@@ -3,6 +3,7 @@
#include <string>
#include <JJX/json.hpp>
#include <Color4.hpp>
#include <Utils.hpp>
using namespace JJX;
@@ -43,9 +44,19 @@ class Entity
json::value Serialize() const
{
json::object data;
return data;
data["name"] = this->name;
data["type"] = this->type;
data["x"] = this->x;
data["y"] = this->y;
data["width"] = this->width;
data["height"] = this->height;
data["rotation"] = this->rotation;
data["flip-h"] = this->flip_h;
data["flip-v"] = this->flip_v;
data["z-index"] = (float)this->z_index;
data["color"] = JsonConversions::deparse_color_to_hex(overlay_color);
data["metadata"] = this->metadata;
return data;
}
void Deserialize(const json::value& json)
@@ -56,6 +67,13 @@ class Entity
y = json["y"].number.value();
width = json["width"].number.value_or(0);
height = json["height"].number.value_or(0);
rotation = json["rotation"].number.value_or(0);
//flip_h = json["flip-h"].boolean.value();
//flip_v = json["flip-v"].boolean.value();
z_index = json["z-index"].number.value();
// TODO: sscanf is bad!!!
//overlay_color = JsonConversions::parse_color(json["color"]);
metadata = json["metadata"];
}

View File

@@ -129,5 +129,6 @@ protected:
inline int CellToIndex(int cx, int cy);
/// Fill the tile-id lookup table with their respective partial-sprite bounding boxes.
void ComputeQuads();
};