diff --git a/src/app/gui/canvas.cc b/src/app/gui/canvas.cc index d1658abc..cfa37736 100644 --- a/src/app/gui/canvas.cc +++ b/src/app/gui/canvas.cc @@ -55,6 +55,14 @@ void Canvas::UpdateEvent(const std::function &event, ImVec2 bg_size, DrawOverlay(); } +void Canvas::UpdateInfoGrid(ImVec2 bg_size, int tile_size, float scale, + float grid_size) { + enable_custom_labels_ = true; + DrawBackground(bg_size); + DrawGrid(grid_size); + DrawOverlay(); +} + void Canvas::DrawBackground(ImVec2 canvas_size) { canvas_p0_ = ImGui::GetCursorScreenPos(); if (!custom_canvas_size_) canvas_sz_ = ImGui::GetContentRegionAvail(); @@ -97,6 +105,17 @@ void Canvas::DrawContextMenu() { scrolling_.x = 0; scrolling_.y = 0; } + if (ImGui::BeginMenu("Canvas Properties")) { + ImGui::Text("Canvas Size: %.0f x %.0f", canvas_sz_.x, canvas_sz_.y); + ImGui::Text("Global Scale: %.1f", global_scale_); + ImGui::Text("Mouse Position: %.0f x %.0f", mouse_pos.x, mouse_pos.y); + ImGui::EndMenu(); + } + if (ImGui::BeginMenu("Bitmap Properties")) { + ImGui::Text("Bitmap Size: %.0f x %.0f", scaled_sz.x, scaled_sz.y); + ImGui::Text("Bitmap Position: %.0f x %.0f", origin.x, origin.y); + ImGui::EndMenu(); + } ImGui::Separator(); if (ImGui::MenuItem("8x8", nullptr, custom_step_ == 8.0f)) { custom_step_ = 8.0f; @@ -110,12 +129,6 @@ void Canvas::DrawContextMenu() { if (ImGui::MenuItem("64x64", nullptr, custom_step_ == 64.0f)) { custom_step_ = 64.0f; } - // Display bitmap metadata such as canvas size and global scale - ImGui::Separator(); - ImGui::Text("Canvas Size: %.0f x %.0f", canvas_sz_.x, canvas_sz_.y); - ImGui::Text("Global Scale: %.1f", global_scale_); - ImGui::Text("Mouse Position: %.0f x %.0f", mouse_pos.x, mouse_pos.y); - ImGui::EndPopup(); } } @@ -143,7 +156,7 @@ bool Canvas::DrawTilePainter(const Bitmap &bitmap, int size, float scale) { points_.push_back(painter_pos); points_.push_back(painter_pos_end); - if (bitmap.IsActive()) { + if (bitmap.is_active()) { draw_list_->AddImage( (void *)bitmap.texture(), ImVec2(origin.x + painter_pos.x, origin.y + painter_pos.y), @@ -466,6 +479,27 @@ void Canvas::DrawGrid(float grid_step) { } } } + + if (enable_custom_labels_) { + // Draw the contents of labels on the grid + for (float x = fmodf(scrolling_.x, grid_step); + x < canvas_sz_.x * global_scale_; x += grid_step) { + for (float y = fmodf(scrolling_.y, grid_step); + y < canvas_sz_.y * global_scale_; y += grid_step) { + int tile_x = (x - scrolling_.x) / grid_step; + int tile_y = (y - scrolling_.y) / grid_step; + int tile_id = tile_x + (tile_y * 8); + + if (tile_id >= labels_[current_labels_].size()) { + break; + } + std::string label = labels_[current_labels_][tile_id]; + draw_list_->AddText(ImVec2(canvas_p0_.x + x + (grid_step / 2) - 8, + canvas_p0_.y + y + (grid_step / 2) - 8), + IM_COL32(255, 255, 255, 255), label.data()); + } + } + } } } diff --git a/src/app/gui/canvas.h b/src/app/gui/canvas.h index 3c89aeb5..2958d2cb 100644 --- a/src/app/gui/canvas.h +++ b/src/app/gui/canvas.h @@ -33,6 +33,9 @@ class Canvas { void UpdateEvent(const std::function& event, ImVec2 bg_size, int tile_size, float scale = 1.0f, float grid_size = 64.0f); + void UpdateInfoGrid(ImVec2 bg_size, int tile_size, float scale = 1.0f, + float grid_size = 64.0f); + // Background for the Canvas represents region without any content drawn to // it, but can be controlled by the user. void DrawBackground(ImVec2 canvas_size = ImVec2(0, 0)); @@ -95,9 +98,25 @@ class Canvas { void set_global_scale(float scale) { global_scale_ = scale; } auto global_scale() const { return global_scale_; } + auto labels(int i) { + if (i >= labels_.size()) { + labels_.push_back(ImVector()); + } + return labels_[i]; + } + auto mutable_labels(int i) { + if (i >= labels_.size()) { + labels_.push_back(ImVector()); + } + return &labels_[i]; + } + + auto set_current_labels(int i) { current_labels_ = i; } + private: bool enable_grid_ = true; bool enable_hex_tile_labels_ = false; + bool enable_custom_labels_ = false; bool enable_context_menu_ = true; bool custom_canvas_size_ = false; bool is_hovered_ = false; @@ -105,8 +124,11 @@ class Canvas { float custom_step_ = 0.0f; float global_scale_ = 1.0f; + int current_labels_ = 0; + ImDrawList* draw_list_; ImVector points_; + ImVector> labels_; ImVec2 scrolling_; ImVec2 canvas_sz_; ImVec2 canvas_p0_;