188 lines
5.9 KiB
C++
188 lines
5.9 KiB
C++
#pragma once
|
|
|
|
#include <Editor/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");
|
|
|
|
SetResizable(false);
|
|
SetDraggable(true);
|
|
|
|
// TODO: Allow changing the background color of the tileset viewer.
|
|
Content()->BGColor(Colors::Black);
|
|
}
|
|
|
|
TilesetView(Tileset* tileset, JGL::Texture* texture) : TilesetView() {
|
|
|
|
stored_tileset = tileset;
|
|
stored_texture = texture;
|
|
|
|
Title(std::format("Tileset View : {}", tileset->name));
|
|
Size(JUI::UDim2::FromPixels(texture->GetDimensions().x, texture->GetDimensions().y+20));
|
|
|
|
tilesheet = new JUI::Image(Content());
|
|
tilesheet->Content(texture);
|
|
tilesheet->FitImageToParent(false);
|
|
|
|
auto grid_overlay_target = new JGL::RenderTarget(texture->GetDimensions());
|
|
J2D::Begin(grid_overlay_target);
|
|
AABB2D aabb({0,0}, Vector2(texture->GetDimensions()));
|
|
Vector2 cell_size(tileset->tile_width, tileset->tile_height);
|
|
DrawGrid2(aabb, cell_size, grid_overlay_color);
|
|
J2D::End();
|
|
grid_overlay = new JGL::Texture(*grid_overlay_target->GetTexture());
|
|
|
|
auto* overlay = new JUI::Image(this->Content());
|
|
overlay->Content(grid_overlay);
|
|
overlay->ZIndex(2);
|
|
overlay->FitImageToParent(false);
|
|
|
|
cell_indicator = new JUI::Rect(Content());
|
|
cell_indicator->BorderMode(JUI::BorderMode::Outline);
|
|
cell_indicator->BorderColor(Colors::Blues::CornflowerBlue);
|
|
cell_indicator->BorderWidth(2);
|
|
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);
|
|
}
|
|
TilesetView(Tileset* tileset, JGL::Texture* texture, Widget* parent) : TilesetView(tileset, texture) {
|
|
Parent(parent);
|
|
}
|
|
|
|
Vector2i GetTilesetCell(const Vector2& pos) {
|
|
|
|
}
|
|
|
|
Vector2i GetTilesetCellFromMouse(const Vector2& mouse) {
|
|
Vector2 rel = Content()->GetAbsolutePosition();
|
|
|
|
Vector2 rel_mouse = mouse - rel;
|
|
|
|
return Vector2i(
|
|
Math::Floor(rel_mouse.x/stored_tileset->tile_width),
|
|
Math::Floor(rel_mouse.y/stored_tileset->tile_height));
|
|
}
|
|
|
|
|
|
void SetCellSizeIndicatorToTilesetSize() {
|
|
cell_indicator->Size(JUI::UDim2::FromPixels(stored_tileset->tile_width, stored_tileset->tile_height));
|
|
}
|
|
|
|
void UpdateCellIndicator(float elapsed) {
|
|
if (IsMouseInside() && Content()->IsMouseInside()) {
|
|
is_focusing = true;
|
|
cell_indicator->Visible(true);
|
|
|
|
auto ipair = InputService::GetMousePosition();
|
|
Vector2 mpos(ipair.x, ipair.y);
|
|
|
|
Vector2 rel = Vector2(GetTilesetCellFromMouse(mpos));
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
bool ObserveMouseInput(JUI::MouseButton btn, bool pressed) override {
|
|
if (Window::ObserveMouseInput(btn, pressed)) return true;
|
|
|
|
if (IsMouseInside()) {
|
|
if (Content()->IsMouseInside() && pressed == false && btn == JUI::MouseButton::Left) {
|
|
Vector2i cell = GetTilesetCellFromMouse(last_known_mouse_pos);
|
|
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;
|
|
}
|
|
|
|
bool IsFocusing() {
|
|
|
|
}
|
|
|
|
|
|
void Update(float delta) override {
|
|
Window::Update(delta);
|
|
UpdateCellIndicator(delta);
|
|
}
|
|
|
|
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:
|
|
|
|
};
|