Adding tooltip and TileMetaDialog to TilesetView

This commit is contained in:
2025-06-26 21:49:57 -05:00
parent 27349b2ee1
commit ba3d88004f
10 changed files with 227 additions and 41 deletions

View File

@@ -135,8 +135,6 @@ public:
//std::vector<Quad> quads;
/// 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.
Vector2i IndexToCell(int index, int width);
@@ -164,6 +162,8 @@ public:
/// @note Placeholder until FileDialogs are added.
void SaveCurrentLevel() const;
void SaveTileset() const;
void SaveCurrentLevelAs(const std::string &filename) const;
void LoadLevel(const std::filesystem::path& level_meta_path);

View File

@@ -0,0 +1,38 @@
#pragma once
#include <Data/Tileset.hpp>
#include <JUI/Base/Widget.hpp>
#include <JUI/Widgets/Window.hpp>
#include <JUI/Widgets/Checkbox.hpp>
#include <JUI/Widgets/TextInputForm.hpp>
class TileMetaDialog : public JUI::Window, public JUI::Hoverable {
public:
Event<bool> CollidesChanged;
Event<std::string> NameChanged;
TileMetaDialog();
explicit TileMetaDialog(JUI::Widget* parent);
void SetTileMetadata(const Tile& tile);
[[nodiscard]] int CurrentTileID() const;
[[nodiscard]] std::string CurrentTileName() const;
void OnExit(const Vector2 &MousePos) override;
JUI::TextRect* name_label;
JUI::TextInputForm* name_form;
JUI::TextRect* collides_label;
JUI::Checkbox* collides_form;
int cur_tile_id;
std::string cur_tile_name;
protected:
private:
};

View File

@@ -2,11 +2,17 @@
#include <App/Grid.hpp>
#include "TileMetaDialog.hpp"
class TilesetView : public JUI::Window {
public:
Tileset* stored_tileset;
Texture* stored_texture;
Event<int> TileSelected;
Event<int, std::string> TileNameChanged;
Event<int, bool> TileCollidesChanged;
bool is_focusing;
TilesetView() : JUI::Window() {
Title("Tileset View");
@@ -50,6 +56,19 @@ public:
cell_indicator->BGColor(Colors::Transparent);
SetCellSizeIndicatorToTilesetSize();
tile_info_tooltip = new JUI::Tooltip(this->Content());
tile_meta_dialog = new TileMetaDialog(this->Content());
tile_meta_dialog->CollidesChanged += [&] (bool collides) {
TileCollidesChanged(tile_meta_dialog->CurrentTileID(), collides);
};
tile_meta_dialog->NameChanged += [&] (const std::string& new_name) {
TileNameChanged(tile_meta_dialog->CurrentTileID(), new_name);
};
}
TilesetView(Widget* parent) : TilesetView() {
Parent(parent);
@@ -90,8 +109,17 @@ public:
rel.x *= stored_tileset->tile_width;
rel.y *= stored_tileset->tile_height;
Vector2i cell = GetTilesetCellFromMouse(last_known_mouse_pos);
int index = CellToIndex(cell, stored_tileset->rows);
auto tile = stored_tileset->tiles[index];
cell_indicator->Position(JUI::UDim2::FromPixels(rel.x, rel.y));
tile_info_tooltip->Content(std::format("#{}: {}", tile.id, tile.name));
tile_info_tooltip->AnchorPoint({0, 0.5f});
tile_info_tooltip->Position(cell_indicator->Position());
} else {
cell_indicator->Visible(false);
is_focusing = false;
@@ -107,6 +135,29 @@ public:
int index = CellToIndex(cell, stored_tileset->rows);
TileSelected.Invoke(index);
}
if (Content()->IsMouseInside() && pressed == false && btn == JUI::MouseButton::Right) {
auto ipair = InputService::GetMousePosition();
Vector2 mpos(ipair.x, ipair.y);
Vector2i cell = GetTilesetCellFromMouse(last_known_mouse_pos);
int index = CellToIndex(cell, stored_tileset->rows);
auto tile = stored_tileset->tiles[index];
Vector2 rel = Vector2(GetTilesetCellFromMouse(mpos));
rel.x *= stored_tileset->tile_width;
rel.y *= stored_tileset->tile_height;
tile_meta_dialog->SetTileMetadata(tile);
tile_meta_dialog->Position(JUI::UDim2::FromPixels(rel.x, rel.y));
tile_meta_dialog->Open();
return true;
}
return true;
}
return false;
@@ -126,9 +177,11 @@ protected:
JUI::Image* tilesheet = nullptr;
JGL::Texture* grid_overlay = nullptr;
JUI::Rect* cell_indicator = nullptr;
JUI::Tooltip* tile_info_tooltip = nullptr;
TileMetaDialog* tile_meta_dialog = nullptr;
protected:
Color4 grid_overlay_color = {255, 255, 255, 64};
Color4 cell_pointer_outline_color = Colors::Blues::LightSteelBlue;
private:
};
};

View File

@@ -26,6 +26,8 @@ struct Tile
Quad quad;
json::value metadata;
bool collides;
bool persistent = false;
};
/// TODO: Only generate tile entry for tiles that are used in the level, or have been assigned custom metadata.
@@ -88,11 +90,11 @@ public:
tileset.author = "J. O'Leary";
tileset.name = "Sample Tileset";
tileset.file_path = "tileset.json";
tileset.cols = 80;
tileset.cols = 50;
tileset.rows = 64;
tileset.texture_path = "assets/megacommando.png";
tileset.tex_width = 1024;
tileset.tex_height = 1280;
tileset.tex_height = 800;
return tileset;
}
@@ -136,6 +138,7 @@ protected:
void FillTiles() {
// Generate some tiles
tiles.reserve(rows*cols);
tile_count = rows*cols;
for (int i = 0 ; i < this->rows*this->cols; i++)
{
Tile tile;