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