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