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 : public SharedRom {
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 : 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, int tile_size, float scale = 1.0f,
85 float grid_size = 64.0f, 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), bool drag = false);
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, float scale = 1.0f,
115 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 private:
210 bool draggable_ = false;
211 bool is_hovered_ = false;
212 bool enable_grid_ = true;
218 bool refresh_graphics_ = false;
219
220 float custom_step_ = 0.0f;
221 float global_scale_ = 1.0f;
222
225
229
230 Bitmap *bitmap_ = nullptr;
231
232 ImDrawList *draw_list_ = nullptr;
233
240 ImVec2 selected_tile_pos_ = ImVec2(-1, -1);
241
242 ImVector<ImVec2> points_;
243 ImVector<ImVec2> selected_points_;
244 ImVector<ImVector<std::string>> labels_;
245
246 std::string canvas_id_ = "Canvas";
247 std::string context_id_ = "CanvasContext";
248 std::vector<ImVec2> selected_tiles_;
249};
250
251void BeginCanvas(Canvas &canvas, ImVec2 child_size = ImVec2(0, 0));
252void EndCanvas(Canvas &canvas);
253
254void GraphicsBinCanvasPipeline(int width, int height, int tile_size,
255 int num_sheets_to_load, int canvas_id,
256 bool is_loaded, BitmapTable &graphics_bin);
257
258void BitmapCanvasPipeline(gui::Canvas &canvas, gfx::Bitmap &bitmap, int width,
259 int height, int tile_size, bool is_loaded,
260 bool scrollbar, int canvas_id);
261
262} // namespace gui
263} // namespace yaze
264
265#endif
SharedRom()=default
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:234
auto selected_tile_pos() const
Definition canvas.h:202
ImVector< ImVec2 > points_
Definition canvas.h:242
int highlight_tile_id
Definition canvas.h:224
void DrawBitmap(Bitmap &bitmap, int border_offset, float scale)
Definition canvas.cc:514
void DrawBitmapGroup(std::vector< int > &group, gfx::Tilemap &tilemap, int tile_size, float scale=1.0f)
Definition canvas.cc:597
std::string canvas_id_
Definition canvas.h:246
auto labels(int i)
Definition canvas.h:178
auto height() const
Definition canvas.h:175
auto custom_step() const
Definition canvas.h:173
auto set_current_labels(int i)
Definition canvas.h:196
ImVec2 selected_tile_pos_
Definition canvas.h:240
auto global_scale() const
Definition canvas.h:171
ImVec2 canvas_p1_
Definition canvas.h:237
void DrawOutlineWithColor(int x, int y, int w, int h, ImVec4 color)
Definition canvas.cc:580
uint64_t edit_palette_group_name_index_
Definition canvas.h:227
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:800
auto hover_mouse_pos() const
Definition canvas.h:207
void DrawContextMenu()
Definition canvas.cc:108
auto push_back(ImVec2 pos)
Definition canvas.h:164
ImVec2 mouse_pos_in_canvas_
Definition canvas.h:239
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:273
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:322
bool enable_context_menu_
Definition canvas.h:215
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:823
bool enable_custom_labels_
Definition canvas.h:214
bool DrawTileSelector(int size, int size_y=0)
Definition canvas.cc:397
void UpdateColorPainter(gfx::Bitmap &bitmap, const ImVec4 &color, const std::function< void()> &event, int tile_size, float scale=1.0f)
Definition canvas.cc:51
void DrawGridLines(float grid_step)
Definition canvas.cc:687
uint64_t edit_palette_sub_index_
Definition canvas.h:228
bool custom_canvas_size_
Definition canvas.h:216
void DrawRect(int x, int y, int w, int h, ImVec4 color)
Definition canvas.cc:665
bool DrawTilePainter(const Bitmap &bitmap, int size, float scale=1.0f)
Definition canvas.cc:223
ImDrawList * draw_list_
Definition canvas.h:232
auto custom_labels_enabled()
Definition canvas.h:172
ImVector< ImVec2 > selected_points_
Definition canvas.h:243
void SetCanvasSize(ImVec2 canvas_size)
Definition canvas.h:153
void DrawTileOnBitmap(int tile_size, gfx::Bitmap *bitmap, ImVec4 color)
Definition canvas.cc:376
void DrawCustomHighlight(float grid_step)
Definition canvas.cc:736
bool select_rect_active_
Definition canvas.h:217
auto mutable_selected_tiles()
Definition canvas.h:200
auto mutable_labels(int i)
Definition canvas.h:184
Bitmap * bitmap_
Definition canvas.h:230
void SetCanvasGridSize(CanvasGridSize grid_size)
Definition canvas.h:63
uint16_t edit_palette_index_
Definition canvas.h:226
auto canvas_size() const
Definition canvas.h:169
ImVector< ImVector< std::string > > labels_
Definition canvas.h:244
auto canvas_id() const
Definition canvas.h:177
void DrawText(std::string text, int x, int y)
Definition canvas.cc:678
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:426
auto zero_point() const
Definition canvas.h:166
void DrawBackground(ImVec2 canvas_size=ImVec2(0, 0), bool drag=false)
Definition canvas.cc:73
bool IsMouseHovering() const
Definition canvas.h:158
bool refresh_graphics_
Definition canvas.h:218
void UpdateInfoGrid(ImVec2 bg_size, int tile_size, float scale=1.0f, float grid_size=64.0f, int label_id=0)
Definition canvas.cc:65
float global_scale_
Definition canvas.h:221
void DrawOutline(int x, int y, int w, int h)
Definition canvas.cc:572
auto mutable_points()
Definition canvas.h:163
float custom_step_
Definition canvas.h:220
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:702
bool enable_hex_tile_labels_
Definition canvas.h:213
ImVec2 canvas_p0_
Definition canvas.h:236
auto scrolling() const
Definition canvas.h:167
void DrawBitmapTable(const BitmapTable &gfx_bin)
Definition canvas.cc:559
std::string context_id_
Definition canvas.h:247
ImVec2 canvas_sz_
Definition canvas.h:235
void DrawGrid(float grid_step=64.0f, int tile_id_offset=8)
Definition canvas.cc:749
ImVec2 drawn_tile_pos_
Definition canvas.h:238
auto selected_points() const
Definition canvas.h:205
std::vector< ImVec2 > selected_tiles_
Definition canvas.h:248
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:909
void EndCanvas(Canvas &canvas)
Definition canvas.cc:872
void BeginCanvas(Canvas &canvas, ImVec2 child_size)
Definition canvas.cc:864
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:878
CanvasGridSize
Definition canvas.h:27
Main namespace for the application.
Definition controller.cc:18