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 = options.viewport_size.value_or(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 ImVec2(0, 0), ImVec2(1, 1), ImGui::GetColorU32(ImVec4(1, 1, 1, 1)));
121 }
122 }
123 canvas.DrawTileSelector(tile_size);
124 canvas.DrawGrid(tile_size);
125 canvas.DrawOverlay();
126 canvas.RenderPersistentPopups();
127 }
128 ImGui::EndChild();
129}
130
131void BitmapCanvasPipeline(Canvas& canvas, gfx::Bitmap& bitmap, int width,
132 int height, int tile_size, bool is_loaded,
133 bool scrollbar, int canvas_id) {
134 auto draw_canvas = [&](Canvas& c, gfx::Bitmap& bmp, int w, int h, int ts,
135 bool loaded) {
136 c.DrawBackground(ImVec2(w + 1, h + 1));
137 c.DrawContextMenu();
138 c.DrawBitmap(bmp, 2, loaded);
139 c.DrawTileSelector(ts);
140 c.DrawGrid(ts);
141 c.DrawOverlay();
143 };
144
145 if (scrollbar) {
146 if (ImGuiID child_id =
147 ImGui::GetID((ImTextureID)(intptr_t)(intptr_t)canvas_id);
148 ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(), true,
149 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
150 draw_canvas(canvas, bitmap, width, height, tile_size, is_loaded);
151 }
152 ImGui::EndChild();
153 } else {
154 draw_canvas(canvas, bitmap, width, height, tile_size, is_loaded);
155 }
156}
157
159 const std::string& label, bool auto_resize) {
160 canvas.SetAutoResize(auto_resize);
161
162 if (auto_resize && bitmap.is_active()) {
163 ImVec2 content_size = ImVec2(bitmap.width(), bitmap.height());
164 ImVec2 preferred_size = CanvasUtils::CalculatePreferredCanvasSize(
165 content_size, canvas.GetGlobalScale());
166 canvas.SetCanvasSize(preferred_size);
167 }
168
169 if (canvas.BeginTableCanvas(label)) {
170 canvas.DrawBackground();
171 canvas.DrawContextMenu();
172
173 if (bitmap.is_active()) {
174 canvas.DrawBitmap(bitmap, 2, 2, canvas.GetGlobalScale());
175 }
176
177 canvas.DrawGrid();
178 canvas.DrawOverlay();
179 canvas.RenderPersistentPopups();
180 }
181 canvas.EndTableCanvas();
182}
183
184} // namespace yaze::gui
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:70
bool is_active() const
Definition bitmap.h:414
int height() const
Definition bitmap.h:399
int width() const
Definition bitmap.h:398
Modern, robust canvas for drawing and manipulating graphics.
Definition canvas.h:64
void DrawBitmap(Bitmap &bitmap, int border_offset, float scale)
Definition canvas.cc:1293
bool BeginTableCanvas(const std::string &label="")
Definition canvas.cc:500
void DrawContextMenu()
Definition canvas.cc:823
auto draw_list() const
Definition canvas.h:363
bool DrawTileSelector(int size, int size_y=0)
Definition canvas.cc:1229
ImVec2 GetCurrentSize() const
Definition canvas.h:290
void SetCanvasSize(ImVec2 canvas_size)
Definition canvas.h:388
ImVec2 GetPreferredSize() const
Definition canvas.cc:490
float GetGridStep() const
Definition canvas.h:396
void Begin(ImVec2 canvas_size=ImVec2(0, 0))
Begin canvas rendering (ImGui-style)
Definition canvas.cc:607
auto canvas_size() const
Definition canvas.h:373
auto canvas_id() const
Definition canvas.h:420
void RenderPersistentPopups()
Definition canvas.cc:1009
bool IsAutoResize() const
Definition canvas.h:292
void End()
End canvas rendering (ImGui-style)
Definition canvas.cc:613
float GetGlobalScale() const
Definition canvas.h:392
auto zero_point() const
Definition canvas.h:364
bool IsMouseHovering() const
Definition canvas.h:354
void DrawBackground(ImVec2 canvas_size=ImVec2(0, 0), std::optional< ImVec2 > viewport_size=std::nullopt)
Definition canvas.cc:702
auto scrolling() const
Definition canvas.h:366
void EndTableCanvas()
Definition canvas.cc:523
void SetAutoResize(bool auto_resize)
Definition canvas.h:291
void DrawGrid(float grid_step=64.0f, int tile_id_offset=8)
Definition canvas.cc:1620
std::unordered_map< int, std::unique_ptr< gfx::Bitmap > > BitmapTable
Definition bitmap.h:549
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:296
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:300
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< ImVec2 > viewport_size
std::optional< float > grid_step