Working on tileset-metadata editing.

This commit is contained in:
2025-06-16 22:58:42 -05:00
parent b8bed66928
commit 214f27fa21
5 changed files with 161 additions and 76 deletions

View File

@@ -221,8 +221,6 @@ void EditorApp::LoadLevel(const std::filesystem::path& level_meta_path)
for (auto layer : loaded_level->layers)
layer->Load();
layer_view->UpdateComponents(loaded_level);
data_ready = true;
}
@@ -443,6 +441,11 @@ bool EditorApp::Open()
layer_view->UpdateComponents(loaded_level);
for (auto tile : loaded_tileset->tiles) {
std::cout << tile.id << ", " << tile.name << std::endl;
}
return true;
}
@@ -763,6 +766,18 @@ void EditorApp::DrawLevel(const Level* level) const
}
}
bool EditorApp::IsMouseGridCellInBounds() {
Vector2i cell = GetGridCellFromMouse();
if (cell.x < 0) return false;
if (cell.y < 0) return false;
if (cell.x >= loaded_level->rows) return false;
if (cell.y >= loaded_level->cols) return false;
return true;
}
void EditorApp::Draw()
{
glClearColor(bg_color.RN(), bg_color.GN(), bg_color.BN(), 1);
@@ -788,6 +803,28 @@ void EditorApp::Draw()
DrawCellPointerOutline();
Tile selected_tile = loaded_tileset->tiles[selected_quad];
J2D::DrawString(Colors::White, std::format("Selected Tile: {}", selected_tile.name), 16, 48, 16);
Vector2i cell = GetGridCellFromMouse();
int hovered_tile_id = GetTile(cell);
if (hovered_tile_id > 0) {
Tile hovered_tile = loaded_tileset->tiles[hovered_tile_id];
J2D::DrawString(Colors::White, std::format("Hovered Tile: {}", hovered_tile.name), 16, 64, 16);
}
glPopMatrix();
auto maus = GetMouseCoordinates();
@@ -799,8 +836,6 @@ void EditorApp::Draw()
J2D::DrawPoint(Colors::White, bruhbruh, 4);
J2D::End();
scene->Draw();
}

View File

@@ -6,6 +6,9 @@ Tileset::Tileset():
tile_count(0), tiles_per_row(0)
{ }
Tileset::Tileset(const std::string& name, const std::filesystem::path& texture_path, int tex_width, int tex_height, int tile_width, int tile_height,
int tile_spacing)
{
@@ -29,27 +32,18 @@ Tileset::Tileset(const std::string& name, const std::filesystem::path& texture_p
ComputeQuads();
// Generate some tiles
for (int i = 0 ; i < this->rows*this->cols; i++)
{
Tile tile;
tile.id = i;
tile.quad = quads[i];
tile.name = std::format("tile_{}", i);
tile.metadata = json::value();
tiles.push_back(tile);
}
FillTiles();
}
Tileset::Tileset(const json::value& json)
{
Deserialize(json);
}
Tileset::Tileset(const std::filesystem::path& filePath)
{
auto [json, err] = json::parse(read_file_contents(filePath));
Deserialize(json);
}
@@ -66,20 +60,27 @@ void Tileset::Deserialize(const json::value& json)
this->tex_width = (int)json["texture-width"].number.value();
this->tex_height = (int)json["texture-height"].number.value();
this->rows = J3ML::Math::Floor(this->tex_width / this->tile_width);
this->cols = J3ML::Math::Floor(this->tex_height / this->tile_height);
ComputeQuads();
FillTiles();
if (json.as_object().contains("tiles"))
{
auto tilemeta = json["tiles"].as_array();
for (json::value& v : tilemeta)
{
// this->tiles.push_back(v);
int id = v["id"].number.value();
tiles[id].name = v["name"].as_string();
if (v.contains("collides"))
tiles[id].collides = v["collides"].boolean.value_or(true);
// this->tiles.push_back(v);
}
}
this->rows = J3ML::Math::Floor(this->tex_width / this->tile_width);
this->cols = J3ML::Math::Floor(this->tex_height / this->tile_height);
ComputeQuads();
}
json::value Tileset::Serialize() const
@@ -121,6 +122,7 @@ json::value Tileset::Serialize() const
tile_json["name"] = tile.name;
tile_json["id"] = (float)tile.id;
tile_json["metadata"] = tile.metadata;
tile_json["collides"] = json::boolean(tile.collides);
json::array quad;
quad.push_back((float)tile.quad.x);
quad.push_back((float)tile.quad.y);