yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
canvas.h
Go to the documentation of this file.
1#ifndef YAZE_GUI_CANVAS_H
2#define YAZE_GUI_CANVAS_H
3
4#define IMGUI_DEFINE_MATH_OPERATORS
5
6#include <cstdint>
7#include <string>
8
9#include "app/gfx/bitmap.h"
10#include "app/rom.h"
11#include "imgui/imgui.h"
12
13namespace yaze {
14
19namespace gui {
20
21using gfx::Bitmap;
23
24enum class CanvasType { kTile, kBlock, kMap };
25enum class CanvasMode { kPaint, kSelect };
27
36class Canvas : public SharedRom {
37 public:
38 Canvas() = default;
39 explicit Canvas(const std::string &id) : canvas_id_(id) {
40 context_id_ = id + "Context";
41 }
42 explicit Canvas(const std::string &id, ImVec2 canvas_size)
44 context_id_ = id + "Context";
45 }
46 explicit Canvas(const std::string &id, ImVec2 canvas_size,
47 CanvasGridSize grid_size)
49 context_id_ = id + "Context";
50 SetCanvasGridSize(grid_size);
51 }
52 explicit Canvas(const std::string &id, ImVec2 canvas_size,
53 CanvasGridSize grid_size, float global_scale)
54 : canvas_id_(id),
58 context_id_ = id + "Context";
59 SetCanvasGridSize(grid_size);
60 }
61
63 switch (grid_size) {
65 custom_step_ = 8.0f;
66 break;
68 custom_step_ = 16.0f;
69 break;
71 custom_step_ = 32.0f;
72 break;
74 custom_step_ = 64.0f;
75 break;
76 }
77 }
78
79 void UpdateColorPainter(gfx::Bitmap &bitmap, const ImVec4 &color,
80 const std::function<void()> &event, int tile_size,
81 float scale = 1.0f);
82
83 void UpdateInfoGrid(ImVec2 bg_size, int tile_size, float scale = 1.0f,
84 float grid_size = 64.0f, int label_id = 0);
85
86 // Background for the Canvas represents region without any content drawn to
87 // it, but can be controlled by the user.
88 void DrawBackground(ImVec2 canvas_size = ImVec2(0, 0), bool drag = false);
89
90 // Context Menu refers to what happens when the right mouse button is pressed
91 // This routine also handles the scrolling for the canvas.
92 void DrawContextMenu();
93
94 // Tile painter shows a preview of the currently selected tile
95 // and allows the user to left click to paint the tile or right
96 // click to select a new tile to paint with.
97 bool DrawTilePainter(const Bitmap &bitmap, int size, float scale = 1.0f);
98 bool DrawSolidTilePainter(const ImVec4 &color, int size);
99
100 // Draws a tile on the canvas at the specified position
101 void DrawTileOnBitmap(int tile_size, gfx::Bitmap *bitmap, ImVec4 color);
102
103 // Dictates which tile is currently selected based on what the user clicks
104 // in the canvas window. Represented and split apart into a grid of tiles.
105 bool DrawTileSelector(int size);
106
107 // Draws the selection rectangle when the user is selecting multiple tiles
108 void DrawSelectRect(int current_map, int tile_size = 0x10,
109 float scale = 1.0f);
110
111 // Draws the contents of the Bitmap image to the Canvas
112 void DrawBitmap(Bitmap &bitmap, int border_offset = 0, bool ready = true);
113 void DrawBitmap(Bitmap &bitmap, int border_offset, float scale);
114 void DrawBitmap(Bitmap &bitmap, int x_offset = 0, int y_offset = 0,
115 float scale = 1.0f, int alpha = 255);
116 void DrawBitmapTable(const BitmapTable &gfx_bin);
117
118 void DrawBitmapGroup(std::vector<int> &group,
119 std::array<gfx::Bitmap, 4096> &tile16_individual_,
120 int tile_size, float scale = 1.0f);
121
122 void DrawOutline(int x, int y, int w, int h);
123 void DrawOutlineWithColor(int x, int y, int w, int h, ImVec4 color);
124 void DrawOutlineWithColor(int x, int y, int w, int h, uint32_t color);
125
126 void DrawRect(int x, int y, int w, int h, ImVec4 color);
127
128 void DrawText(std::string text, int x, int y);
129 void DrawGridLines(float grid_step);
130 void DrawGrid(float grid_step = 64.0f, int tile_id_offset = 8);
131 void DrawOverlay(); // last
132
133 void DrawInfoGrid(float grid_step = 64.0f, int tile_id_offset = 8,
134 int label_id = 0);
135
136 void DrawLayeredElements();
137
139 float x = mouse_pos_in_canvas_.x;
140 float y = mouse_pos_in_canvas_.y;
141 int num_columns = (canvas_sz_.x / global_scale_) / custom_step_;
142 int num_rows = (canvas_sz_.y / global_scale_) / custom_step_;
143 int tile_id = (x / custom_step_) + (y / custom_step_) * num_columns;
144 tile_id = tile_id / global_scale_;
145 if (tile_id >= num_columns * num_rows) {
146 tile_id = -1; // Invalid tile ID
147 }
148 return tile_id;
149 }
152 custom_canvas_size_ = true;
153 }
154 void DrawCustomHighlight(float grid_step);
155 bool IsMouseHovering() const { return is_hovered_; }
156 void ZoomIn() { global_scale_ += 0.25f; }
157 void ZoomOut() { global_scale_ -= 0.25f; }
158
159 auto points() const { return points_; }
160 auto mutable_points() { return &points_; }
161 auto push_back(ImVec2 pos) { points_.push_back(pos); }
162 auto draw_list() const { return draw_list_; }
163 auto zero_point() const { return canvas_p0_; }
164 auto scrolling() const { return scrolling_; }
165 auto drawn_tile_position() const { return drawn_tile_pos_; }
166 auto canvas_size() const { return canvas_sz_; }
167 void set_global_scale(float scale) { global_scale_ = scale; }
168 auto global_scale() const { return global_scale_; }
170 auto custom_step() const { return custom_step_; }
171 auto width() const { return canvas_sz_.x; }
172 auto height() const { return canvas_sz_.y; }
173 auto set_draggable(bool value) { draggable_ = value; }
174
175 auto labels(int i) {
176 if (i >= labels_.size()) {
177 labels_.push_back(ImVector<std::string>());
178 }
179 return labels_[i];
180 }
181 auto mutable_labels(int i) {
182 if (i >= labels_.size()) {
183 int x = i;
184 while (x >= labels_.size()) {
185 labels_.push_back(ImVector<std::string>());
186 x--;
187 }
188 labels_.push_back(ImVector<std::string>());
189 }
190 return &labels_[i];
191 }
192
195
196 auto selected_tiles() const { return selected_tiles_; }
198
199 auto selected_tile_pos() const { return selected_tile_pos_; }
200 auto set_selected_tile_pos(ImVec2 pos) { selected_tile_pos_ = pos; }
202 auto selected_points() const { return selected_points_; }
203
204 auto hover_mouse_pos() const { return mouse_pos_in_canvas_; }
205
206 private:
207 bool draggable_ = false;
208 bool is_hovered_ = false;
209 bool enable_grid_ = true;
215 bool refresh_graphics_ = false;
216
217 float custom_step_ = 0.0f;
218 float global_scale_ = 1.0f;
219
222
226
227 Bitmap *bitmap_ = nullptr;
228
229 ImDrawList *draw_list_ = nullptr;
230
237 ImVec2 selected_tile_pos_ = ImVec2(-1, -1);
238
239 ImVector<ImVec2> points_;
240 ImVector<ImVec2> selected_points_;
241 ImVector<ImVector<std::string>> labels_;
242
243 std::string canvas_id_ = "Canvas";
244 std::string context_id_ = "CanvasContext";
245 std::vector<ImVec2> selected_tiles_;
246};
247
248void GraphicsBinCanvasPipeline(int width, int height, int tile_size,
249 int num_sheets_to_load, int canvas_id,
250 bool is_loaded, BitmapTable &graphics_bin);
251
252void BitmapCanvasPipeline(gui::Canvas &canvas, gfx::Bitmap &bitmap, int width,
253 int height, int tile_size, bool is_loaded,
254 bool scrollbar, int canvas_id);
255
256} // namespace gui
257} // namespace yaze
258
259#endif
SharedRom()=default
Represents a bitmap image.
Definition bitmap.h:66
Represents a canvas for drawing and manipulating graphics.
Definition canvas.h:36
ImVec2 scrolling_
Definition canvas.h:231
auto selected_tile_pos() const
Definition canvas.h:199
ImVector< ImVec2 > points_
Definition canvas.h:239
int highlight_tile_id
Definition canvas.h:221
std::string canvas_id_
Definition canvas.h:243
auto labels(int i)
Definition canvas.h:175
auto height() const
Definition canvas.h:172
auto custom_step() const
Definition canvas.h:170
void DrawBitmap(Bitmap &bitmap, int border_offset=0, bool ready=true)
Definition canvas.cc:461
auto set_current_labels(int i)
Definition canvas.h:193
ImVec2 selected_tile_pos_
Definition canvas.h:237
auto global_scale() const
Definition canvas.h:168
ImVec2 canvas_p1_
Definition canvas.h:234
void DrawOutlineWithColor(int x, int y, int w, int h, ImVec4 color)
Definition canvas.cc:522
uint64_t edit_palette_group_name_index_
Definition canvas.h:224
auto selected_tiles() const
Definition canvas.h:196
auto set_draggable(bool value)
Definition canvas.h:173
Canvas(const std::string &id, ImVec2 canvas_size)
Definition canvas.h:42
void DrawOverlay()
Definition canvas.cc:740
auto hover_mouse_pos() const
Definition canvas.h:204
void DrawContextMenu()
Definition canvas.cc:109
auto push_back(ImVec2 pos)
Definition canvas.h:161
ImVec2 mouse_pos_in_canvas_
Definition canvas.h:236
int GetTileIdFromMousePos()
Definition canvas.h:138
auto points() const
Definition canvas.h:159
auto drawn_tile_position() const
Definition canvas.h:165
Canvas(const std::string &id, ImVec2 canvas_size, CanvasGridSize grid_size, float global_scale)
Definition canvas.h:52
bool DrawSolidTilePainter(const ImVec4 &color, int size)
Definition canvas.cc:272
bool enable_context_menu_
Definition canvas.h:212
auto draw_list() const
Definition canvas.h:162
auto set_selected_tile_pos(ImVec2 pos)
Definition canvas.h:200
auto width() const
Definition canvas.h:171
void DrawLayeredElements()
Definition canvas.cc:763
bool enable_custom_labels_
Definition canvas.h:211
void UpdateColorPainter(gfx::Bitmap &bitmap, const ImVec4 &color, const std::function< void()> &event, int tile_size, float scale=1.0f)
Definition canvas.cc:52
void DrawGridLines(float grid_step)
Definition canvas.cc:627
uint64_t edit_palette_sub_index_
Definition canvas.h:225
bool custom_canvas_size_
Definition canvas.h:213
void DrawRect(int x, int y, int w, int h, ImVec4 color)
Definition canvas.cc:605
bool DrawTilePainter(const Bitmap &bitmap, int size, float scale=1.0f)
Definition canvas.cc:222
ImDrawList * draw_list_
Definition canvas.h:229
auto custom_labels_enabled()
Definition canvas.h:169
ImVector< ImVec2 > selected_points_
Definition canvas.h:240
void SetCanvasSize(ImVec2 canvas_size)
Definition canvas.h:150
void DrawTileOnBitmap(int tile_size, gfx::Bitmap *bitmap, ImVec4 color)
Definition canvas.cc:326
void DrawCustomHighlight(float grid_step)
Definition canvas.cc:676
bool select_rect_active_
Definition canvas.h:214
auto mutable_selected_tiles()
Definition canvas.h:197
auto mutable_labels(int i)
Definition canvas.h:181
Bitmap * bitmap_
Definition canvas.h:227
void SetCanvasGridSize(CanvasGridSize grid_size)
Definition canvas.h:62
uint16_t edit_palette_index_
Definition canvas.h:223
auto canvas_size() const
Definition canvas.h:166
ImVector< ImVector< std::string > > labels_
Definition canvas.h:241
void DrawText(std::string text, int x, int y)
Definition canvas.cc:618
void set_global_scale(float scale)
Definition canvas.h:167
bool select_rect_active() const
Definition canvas.h:201
Canvas(const std::string &id)
Definition canvas.h:39
void DrawSelectRect(int current_map, int tile_size=0x10, float scale=1.0f)
Definition canvas.cc:373
auto zero_point() const
Definition canvas.h:163
void DrawBackground(ImVec2 canvas_size=ImVec2(0, 0), bool drag=false)
Definition canvas.cc:74
bool IsMouseHovering() const
Definition canvas.h:155
bool refresh_graphics_
Definition canvas.h:215
void UpdateInfoGrid(ImVec2 bg_size, int tile_size, float scale=1.0f, float grid_size=64.0f, int label_id=0)
Definition canvas.cc:66
float global_scale_
Definition canvas.h:218
void DrawOutline(int x, int y, int w, int h)
Definition canvas.cc:514
auto mutable_points()
Definition canvas.h:160
float custom_step_
Definition canvas.h:217
Canvas(const std::string &id, ImVec2 canvas_size, CanvasGridSize grid_size)
Definition canvas.h:46
void DrawInfoGrid(float grid_step=64.0f, int tile_id_offset=8, int label_id=0)
Definition canvas.cc:642
bool enable_hex_tile_labels_
Definition canvas.h:210
ImVec2 canvas_p0_
Definition canvas.h:233
void DrawBitmapGroup(std::vector< int > &group, std::array< gfx::Bitmap, 4096 > &tile16_individual_, int tile_size, float scale=1.0f)
Definition canvas.cc:539
auto scrolling() const
Definition canvas.h:164
void DrawBitmapTable(const BitmapTable &gfx_bin)
Definition canvas.cc:501
std::string context_id_
Definition canvas.h:244
ImVec2 canvas_sz_
Definition canvas.h:232
bool DrawTileSelector(int size)
Definition canvas.cc:347
void DrawGrid(float grid_step=64.0f, int tile_id_offset=8)
Definition canvas.cc:689
ImVec2 drawn_tile_pos_
Definition canvas.h:235
auto selected_points() const
Definition canvas.h:202
std::vector< ImVec2 > selected_tiles_
Definition canvas.h:245
auto set_highlight_tile_id(int i)
Definition canvas.h:194
std::unordered_map< int, gfx::Bitmap > BitmapTable
Definition bitmap.h:254
void BitmapCanvasPipeline(gui::Canvas &canvas, gfx::Bitmap &bitmap, int width, int height, int tile_size, bool is_loaded, bool scrollbar, int canvas_id)
Definition canvas.cc:835
void GraphicsBinCanvasPipeline(int width, int height, int tile_size, int num_sheets_to_load, int canvas_id, bool is_loaded, gfx::BitmapTable &graphics_bin)
Definition canvas.cc:804
CanvasGridSize
Definition canvas.h:26
Main namespace for the application.
Definition controller.cc:18