yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
canvas_pipelines.cc
Go to the documentation of this file.
2
3#include <algorithm>
4
8#include "imgui/imgui.h"
9
10namespace yaze::gui {
11
12void BeginCanvas(Canvas& canvas, ImVec2 child_size) {
13 BeginPadding(1);
14
15 ImVec2 effective_size = child_size;
16 if (child_size.x == 0 && child_size.y == 0) {
17 if (canvas.IsAutoResize()) {
18 effective_size = canvas.GetPreferredSize();
19 } else {
20 effective_size = canvas.GetCurrentSize();
21 }
22 }
23
24 ImGui::BeginChild(canvas.canvas_id().c_str(), effective_size, true,
25 ImGuiWindowFlags_NoScrollbar);
26 canvas.DrawBackground();
27 EndPadding();
28 canvas.DrawContextMenu();
29}
30
31void EndCanvas(Canvas& canvas) {
32 canvas.DrawGrid();
33 canvas.DrawOverlay();
34 ImGui::EndChild();
35}
36
38 canvas.Begin(options);
39
40 CanvasRuntime runtime;
41 runtime.draw_list = canvas.draw_list();
42 runtime.canvas_p0 = canvas.zero_point();
43 runtime.canvas_sz = canvas.canvas_size();
44 runtime.scrolling = canvas.scrolling();
45 runtime.hovered = canvas.IsMouseHovering();
46 runtime.grid_step = options.grid_step.value_or(canvas.GetGridStep());
47 runtime.scale = canvas.GetGlobalScale();
48 runtime.content_size = canvas.GetCurrentSize();
49
50 return runtime;
51}
52
53void EndCanvas(Canvas& canvas, CanvasRuntime& /*runtime*/,
54 const CanvasFrameOptions& options) {
55 canvas.End(options);
56}
57
58ZoomToFitResult ComputeZoomToFit(ImVec2 content_px, ImVec2 canvas_px,
59 float padding_px) {
60 ZoomToFitResult result;
61 result.scale = 1.0f;
62 result.scroll = ImVec2(0, 0);
63
64 if (content_px.x <= 0 || content_px.y <= 0) {
65 return result;
66 }
67
68 float available_x = canvas_px.x - padding_px * 2;
69 float available_y = canvas_px.y - padding_px * 2;
70
71 if (available_x <= 0 || available_y <= 0) {
72 return result;
73 }
74
75 float scale_x = available_x / content_px.x;
76 float scale_y = available_y / content_px.y;
77 result.scale = std::min(scale_x, scale_y);
78
79 float scaled_w = content_px.x * result.scale;
80 float scaled_h = content_px.y * result.scale;
81 result.scroll.x = (canvas_px.x - scaled_w) / 2.0f;
82 result.scroll.y = (canvas_px.y - scaled_h) / 2.0f;
83
84 return result;
85}
86
87ImVec2 ClampScroll(ImVec2 scroll, ImVec2 content_px, ImVec2 canvas_px) {
88 float max_scroll_x = std::max(0.0f, content_px.x - canvas_px.x);
89 float max_scroll_y = std::max(0.0f, content_px.y - canvas_px.y);
90
91 return ImVec2(std::clamp(scroll.x, -max_scroll_x, 0.0f),
92 std::clamp(scroll.y, -max_scroll_y, 0.0f));
93}
94
95void GraphicsBinCanvasPipeline(int width, int height, int tile_size,
96 int num_sheets_to_load, int canvas_id,
97 bool is_loaded, gfx::BitmapTable& graphics_bin) {
98 Canvas canvas;
99 if (ImGuiID child_id =
100 ImGui::GetID((ImTextureID)(intptr_t)(intptr_t)canvas_id);
101 ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(), true,
102 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
103 canvas.DrawBackground(ImVec2(width + 1, num_sheets_to_load * height + 1));
104 canvas.DrawContextMenu();
105 if (is_loaded) {
106 for (const auto& [key, value] : graphics_bin) {
107 if (!value || !value->texture()) {
108 continue;
109 }
110 int offset = height * (key + 1);
111 int top_left_y = canvas.zero_point().y + 2;
112 if (key >= 1) {
113 top_left_y = canvas.zero_point().y + height * key;
114 }
115 canvas.draw_list()->AddImage(
116 (ImTextureID)(intptr_t)value->texture(),
117 ImVec2(canvas.zero_point().x + 2, top_left_y),
118 ImVec2(canvas.zero_point().x + 0x100,
119 canvas.zero_point().y + offset));
120 }
121 }
122 canvas.DrawTileSelector(tile_size);
123 canvas.DrawGrid(tile_size);
124 canvas.DrawOverlay();
125 canvas.RenderPersistentPopups();
126 }
127 ImGui::EndChild();
128}
129
130void BitmapCanvasPipeline(Canvas& canvas, gfx::Bitmap& bitmap, int width,
131 int height, int tile_size, bool is_loaded,
132 bool scrollbar, int canvas_id) {
133 auto draw_canvas = [&](Canvas& c, gfx::Bitmap& bmp, int w, int h, int ts,
134 bool loaded) {
135 c.DrawBackground(ImVec2(w + 1, h + 1));
136 c.DrawContextMenu();
137 c.DrawBitmap(bmp, 2, loaded);
138 c.DrawTileSelector(ts);
139 c.DrawGrid(ts);
140 c.DrawOverlay();
142 };
143
144 if (scrollbar) {
145 if (ImGuiID child_id =
146 ImGui::GetID((ImTextureID)(intptr_t)(intptr_t)canvas_id);
147 ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(), true,
148 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
149 draw_canvas(canvas, bitmap, width, height, tile_size, is_loaded);
150 }
151 ImGui::EndChild();
152 } else {
153 draw_canvas(canvas, bitmap, width, height, tile_size, is_loaded);
154 }
155}
156
158 const std::string& label, bool auto_resize) {
159 canvas.SetAutoResize(auto_resize);
160
161 if (auto_resize && bitmap.is_active()) {
162 ImVec2 content_size = ImVec2(bitmap.width(), bitmap.height());
163 ImVec2 preferred_size = CanvasUtils::CalculatePreferredCanvasSize(
164 content_size, canvas.GetGlobalScale());
165 canvas.SetCanvasSize(preferred_size);
166 }
167
168 if (canvas.BeginTableCanvas(label)) {
169 canvas.DrawBackground();
170 canvas.DrawContextMenu();
171
172 if (bitmap.is_active()) {
173 canvas.DrawBitmap(bitmap, 2, 2, canvas.GetGlobalScale());
174 }
175
176 canvas.DrawGrid();
177 canvas.DrawOverlay();
178 canvas.RenderPersistentPopups();
179 }
180 canvas.EndTableCanvas();
181}
182
183} // namespace yaze::gui
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:67
bool is_active() const
Definition bitmap.h:405
int height() const
Definition bitmap.h:395
int width() const
Definition bitmap.h:394
Modern, robust canvas for drawing and manipulating graphics.
Definition canvas.h:64
void DrawBitmap(Bitmap &bitmap, int border_offset, float scale)
Definition canvas.cc:1162
bool BeginTableCanvas(const std::string &label="")
Definition canvas.cc:400
void DrawContextMenu()
Definition canvas.cc:692
auto draw_list() const
Definition canvas.h:347
bool DrawTileSelector(int size, int size_y=0)
Definition canvas.cc:1098
ImVec2 GetCurrentSize() const
Definition canvas.h:274
void SetCanvasSize(ImVec2 canvas_size)
Definition canvas.h:372
ImVec2 GetPreferredSize() const
Definition canvas.cc:390
float GetGridStep() const
Definition canvas.h:380
void Begin(ImVec2 canvas_size=ImVec2(0, 0))
Begin canvas rendering (ImGui-style)
Definition canvas.cc:507
auto canvas_size() const
Definition canvas.h:357
auto canvas_id() const
Definition canvas.h:404
void RenderPersistentPopups()
Definition canvas.cc:878
bool IsAutoResize() const
Definition canvas.h:276
void End()
End canvas rendering (ImGui-style)
Definition canvas.cc:513
float GetGlobalScale() const
Definition canvas.h:376
auto zero_point() const
Definition canvas.h:348
bool IsMouseHovering() const
Definition canvas.h:338
auto scrolling() const
Definition canvas.h:350
void DrawBackground(ImVec2 canvas_size=ImVec2(0, 0))
Definition canvas.cc:602
void EndTableCanvas()
Definition canvas.cc:423
void SetAutoResize(bool auto_resize)
Definition canvas.h:275
void DrawGrid(float grid_step=64.0f, int tile_id_offset=8)
Definition canvas.cc:1484
std::unordered_map< int, std::unique_ptr< gfx::Bitmap > > BitmapTable
Definition bitmap.h:518
ImVec2 CalculatePreferredCanvasSize(ImVec2 content_size, float global_scale, float min_scale)
Graphical User Interface (GUI) components for the application.
void EndCanvas(Canvas &canvas)
void BeginPadding(int i)
Definition style.cc:277
void BeginCanvas(Canvas &canvas, ImVec2 child_size)
void GraphicsBinCanvasPipeline(int width, int height, int tile_size, int num_sheets_to_load, int canvas_id, bool is_loaded, gfx::BitmapTable &graphics_bin)
ImVec2 ClampScroll(ImVec2 scroll, ImVec2 content_px, ImVec2 canvas_px)
void TableCanvasPipeline(Canvas &canvas, gfx::Bitmap &bitmap, const std::string &label, bool auto_resize)
void EndPadding()
Definition style.cc:281
ZoomToFitResult ComputeZoomToFit(ImVec2 content_px, ImVec2 canvas_px, float padding_px)
void BitmapCanvasPipeline(Canvas &canvas, gfx::Bitmap &bitmap, int width, int height, int tile_size, bool is_loaded, bool scrollbar, int canvas_id)
std::optional< float > grid_step