add canvas id and context id to Canvas

This commit is contained in:
scawful
2024-07-19 15:50:36 -04:00
parent ca7204215b
commit f10eaf0877
7 changed files with 35 additions and 25 deletions

View File

@@ -24,6 +24,8 @@ using ImGui::IsItemHovered;
using ImGui::IsMouseClicked;
using ImGui::IsMouseDragging;
using ImGui::MenuItem;
using ImGui::Selectable;
using ImGui::Separator;
using ImGui::Text;
constexpr uint32_t kRectangleColor = IM_COL32(32, 32, 32, 255);
@@ -66,7 +68,7 @@ void Canvas::DrawBackground(ImVec2 canvas_size, bool can_drag) {
draw_list_->AddRect(canvas_p0_, canvas_p1_, kRectangleBorder);
ImGui::InvisibleButton(
"canvas",
canvas_id_.c_str(),
ImVec2(canvas_sz_.x * global_scale_, canvas_sz_.y * global_scale_),
kMouseFlags);
@@ -99,21 +101,22 @@ void Canvas::DrawContextMenu(gfx::Bitmap *bitmap) {
// Context menu (under default mouse threshold)
if (ImVec2 drag_delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Right);
enable_context_menu_ && drag_delta.x == 0.0f && drag_delta.y == 0.0f)
ImGui::OpenPopupOnItemClick("context", ImGuiPopupFlags_MouseButtonRight);
ImGui::OpenPopupOnItemClick(context_id_.c_str(),
ImGuiPopupFlags_MouseButtonRight);
// Contents of the Context Menu
if (ImGui::BeginPopup("context")) {
if (ImGui::BeginPopup(context_id_.c_str())) {
if (MenuItem("Reset Position", nullptr, false)) {
scrolling_.x = 0;
scrolling_.y = 0;
}
MenuItem("Show Grid", nullptr, &enable_grid_);
ImGui::Selectable("Show Position Labels", &enable_hex_tile_labels_);
Selectable("Show Position Labels", &enable_hex_tile_labels_);
if (BeginMenu("Canvas Properties")) {
Text("Canvas Size: %.0f x %.0f", canvas_sz_.x, canvas_sz_.y);
Text("Global Scale: %.1f", global_scale_);
Text("Mouse Position: %.0f x %.0f", mouse_pos.x, mouse_pos.y);
ImGui::EndMenu();
EndMenu();
}
if (bitmap != nullptr) {
if (BeginMenu("Bitmap Properties")) {
@@ -122,7 +125,7 @@ void Canvas::DrawContextMenu(gfx::Bitmap *bitmap) {
absl::StrFormat("%d", bitmap->surface()->pitch).c_str());
Text("BitsPerPixel: %d", bitmap->surface()->format->BitsPerPixel);
Text("BytesPerPixel: %d", bitmap->surface()->format->BytesPerPixel);
ImGui::EndMenu();
EndMenu();
}
}
ImGui::Separator();
@@ -139,7 +142,7 @@ void Canvas::DrawContextMenu(gfx::Bitmap *bitmap) {
if (MenuItem("64x64", nullptr, custom_step_ == 64.0f)) {
custom_step_ = 64.0f;
}
ImGui::EndMenu();
EndMenu();
}
// TODO: Add a menu item for selecting the palette

View File

@@ -36,10 +36,14 @@ enum class CanvasGridSize { k8x8, k16x16, k32x32, k64x64 };
class Canvas {
public:
Canvas() = default;
explicit Canvas(ImVec2 canvas_size)
: custom_canvas_size_(true), canvas_sz_(canvas_size) {}
explicit Canvas(ImVec2 canvas_size, CanvasGridSize grid_size)
: custom_canvas_size_(true), canvas_sz_(canvas_size) {
explicit Canvas(const std::string& id, ImVec2 canvas_size)
: canvas_id_(id), custom_canvas_size_(true), canvas_sz_(canvas_size) {
context_id_ = id + "Context";
}
explicit Canvas(const std::string& id, ImVec2 canvas_size,
CanvasGridSize grid_size)
: canvas_id_(id), custom_canvas_size_(true), canvas_sz_(canvas_size) {
context_id_ = id + "Context";
switch (grid_size) {
case CanvasGridSize::k8x8:
custom_step_ = 8.0f;
@@ -84,7 +88,7 @@ class Canvas {
// in the canvas window. Represented and split apart into a grid of tiles.
bool DrawTileSelector(int size);
// Draws the selection rectangle when the user is selecting multiple tiles
void DrawSelectRect(int current_map, int tile_size = 0x10,
float scale = 1.0f);
@@ -104,9 +108,6 @@ class Canvas {
void DrawOutlineWithColor(int x, int y, int w, int h, ImVec4 color);
void DrawOutlineWithColor(int x, int y, int w, int h, uint32_t color);
void DrawSelectRect(int current_map, int tile_size = 0x10,
float scale = 1.0f);
void DrawRect(int x, int y, int w, int h, ImVec4 color);
void DrawText(std::string text, int x, int y);
@@ -197,6 +198,9 @@ class Canvas {
int current_labels_ = 0;
int highlight_tile_id = -1;
std::string canvas_id_ = "Canvas";
std::string context_id_ = "CanvasContext";
ImDrawList* draw_list_;
ImVector<ImVec2> points_;
ImVector<ImVector<std::string>> labels_;