Building out tileset viewer and grid editor.
This commit is contained in:
@@ -24,14 +24,49 @@ public:
|
||||
int grid_pixel_width = 32;
|
||||
int grid_pixel_height = 32;
|
||||
|
||||
int grid[16][16];
|
||||
int grid_rows = 32;
|
||||
int grid_cols = 32;
|
||||
|
||||
int grid[32][32];
|
||||
|
||||
int selected_quad = 0;
|
||||
|
||||
struct Quad {
|
||||
int x;
|
||||
int y;
|
||||
int w;
|
||||
int h;
|
||||
};
|
||||
|
||||
std::vector<Quad> quads;
|
||||
|
||||
void PopulateQuads() {
|
||||
quads.reserve(24);
|
||||
for (int i = 0; i < 24; i++) {
|
||||
Vector2i cell = IndexToCell(i, 16);
|
||||
quads[i] = Quad{cell.x*grid_pixel_width, cell.y*grid_pixel_height, grid_pixel_width, grid_pixel_height};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Vector2i IndexToCell(int index, int width) {
|
||||
int x = index % width;
|
||||
int y = index / width;
|
||||
return {x, y};
|
||||
}
|
||||
|
||||
int CellToIndex(Vector2i cell, int width) {
|
||||
return cell.y*width + cell.x;
|
||||
}
|
||||
|
||||
EditorApp() : OpenGLWindow("Editor App", 1080, 768, GL_VER_MAJOR, GL_VER_MINOR) {
|
||||
|
||||
PopulateQuads();
|
||||
}
|
||||
|
||||
void LoadMisc() {
|
||||
test_tilesheet = new JGL::Texture("../platformer-tileset-template.png");
|
||||
test_tilesheet = new JGL::Texture("../megacommando.png");
|
||||
|
||||
auto texture_size = test_tilesheet->GetDimensions();
|
||||
}
|
||||
|
||||
|
||||
@@ -136,6 +171,31 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
Vector2i GetTilesetCellFromMouse() {
|
||||
auto maus = GetMouseCoordinates();
|
||||
|
||||
Vector2 mouse_v2(maus.x, maus.y);
|
||||
|
||||
|
||||
Vector2 rel = tileset_viewer->Content()->GetAbsolutePosition();
|
||||
|
||||
Vector2 rel_mouse = mouse_v2 - rel;
|
||||
|
||||
return Vector2i(
|
||||
Math::Floor(rel_mouse.x/grid_pixel_width),
|
||||
Math::Floor(rel_mouse.y/grid_pixel_height));
|
||||
}
|
||||
|
||||
Vector2i GetGridCellFromMouse() {
|
||||
auto maus = GetMouseCoordinates();
|
||||
|
||||
Vector2 mouse_v2(maus.x, maus.y);
|
||||
|
||||
return Vector2i(
|
||||
Math::Floor(mouse_v2.x/grid_pixel_width),
|
||||
Math::Floor(mouse_v2.y/grid_pixel_height));
|
||||
}
|
||||
|
||||
void Update(float elapsed) {
|
||||
auto size = GetSize();
|
||||
Vector2i vSize = Vector2i(size.x, size.y);
|
||||
@@ -145,21 +205,12 @@ public:
|
||||
scene->SetViewportSize(Vector2(vSize));
|
||||
scene->Update(elapsed);
|
||||
|
||||
auto maus = GetMouseCoordinates();
|
||||
|
||||
Vector2 mouse_v2(maus.x, maus.y);
|
||||
|
||||
if (tileset_viewer->Content()->IsMouseInside()) {
|
||||
cell_indicator->Visible(true);
|
||||
|
||||
Vector2 rel = tileset_viewer->Content()->GetAbsolutePosition();
|
||||
Vector2 rel_mouse = Vector2(GetTilesetCellFromMouse());
|
||||
|
||||
Vector2 rel_mouse = mouse_v2 - rel;
|
||||
|
||||
rel_mouse = Vector2(
|
||||
Math::Floor(rel_mouse.x/32)*32,
|
||||
Math::Floor(rel_mouse.y/32)*32
|
||||
);
|
||||
rel_mouse *= 32;
|
||||
|
||||
cell_indicator->Position(JUI::UDim2::FromPixels(rel_mouse.x, rel_mouse.y));
|
||||
|
||||
@@ -204,7 +255,21 @@ public:
|
||||
|
||||
|
||||
J2D::Begin();
|
||||
J2D::DrawSprite(test_tilesheet, {32, 32}, 0, Vector2::Zero, Vector2::One, Colors::White);
|
||||
//J2D::DrawSprite(test_tilesheet, {32, 32}, 0, Vector2::Zero, Vector2::One, Colors::White);
|
||||
for (int gx = 0; gx < grid_rows; gx++) {
|
||||
for (int gy = 0; gy < grid_cols; gy++) {
|
||||
|
||||
auto quad_idx = grid[gx][gy];
|
||||
|
||||
if (quad_idx < 0)
|
||||
continue;
|
||||
|
||||
auto quad = quads[quad_idx];
|
||||
Vector2 pos(gx*grid_pixel_width, gy*grid_pixel_height);
|
||||
J2D::DrawPartialSprite(test_tilesheet, pos,
|
||||
Vector2(quad.x, quad.y), Vector2(quad.w, quad.h));
|
||||
}
|
||||
}
|
||||
DrawGrid();
|
||||
J2D::End();
|
||||
|
||||
@@ -230,16 +295,33 @@ public:
|
||||
auto btn = ToJUIEnum(e.Button);
|
||||
|
||||
if (scene->ObserveMouseInput(btn, true)) return;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void OnMouseButtonUp(const MouseButtonUpEvent &e) override {
|
||||
auto btn = ToJUIEnum(e.Button);
|
||||
if (scene->ObserveMouseInput(btn, false)) return;
|
||||
|
||||
if (tileset_viewer->Content()->IsMouseInside()) {
|
||||
|
||||
|
||||
} else {
|
||||
Vector2i cell = GetTilesetCellFromMouse();
|
||||
int index = CellToIndex(cell, 16);
|
||||
|
||||
Vector2i grid_cell = GetGridCellFromMouse();
|
||||
grid[grid_cell.x][grid_cell.y] = 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 {
|
||||
|
BIN
megacommando.png
Normal file
BIN
megacommando.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 78 KiB |
Reference in New Issue
Block a user