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 <cstdint>
5#include <string>
6
7#include "app/gfx/bitmap.h"
8#include "app/rom.h"
9#include "imgui/imgui.h"
10
11namespace yaze {
12
17namespace gui {
18
19using 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::array<gfx::Bitmap, 4096>& 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 float x = mouse_pos_in_canvas_.x;
135 float 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 tile_id = tile_id / global_scale_;
140 if (tile_id >= num_columns * num_rows) {
141 tile_id = -1; // Invalid tile ID
142 }
143 return tile_id;
144 }
147 custom_canvas_size_ = true;
148 }
149 void DrawCustomHighlight(float grid_step);
150 bool IsMouseHovering() const { return is_hovered_; }
151 void ZoomIn() { global_scale_ += 0.25f; }
152 void ZoomOut() { global_scale_ -= 0.25f; }
153
154 auto points() const { return points_; }
155 auto mutable_points() { return &points_; }
156 auto push_back(ImVec2 pos) { points_.push_back(pos); }
157 auto draw_list() const { return draw_list_; }
158 auto zero_point() const { return canvas_p0_; }
159 auto scrolling() const { return scrolling_; }
160 auto drawn_tile_position() const { return drawn_tile_pos_; }
161 auto canvas_size() const { return canvas_sz_; }
162 void set_global_scale(float scale) { global_scale_ = scale; }
163 auto global_scale() const { return global_scale_; }
165 auto custom_step() const { return custom_step_; }
166 auto width() const { return canvas_sz_.x; }
167 auto height() const { return canvas_sz_.y; }
168 auto set_draggable(bool value) { draggable_ = value; }
169
170 auto labels(int i) {
171 if (i >= labels_.size()) {
172 labels_.push_back(ImVector<std::string>());
173 }
174 return labels_[i];
175 }
176 auto mutable_labels(int i) {
177 if (i >= labels_.size()) {
178 int x = i;
179 while (x >= labels_.size()) {
180 labels_.push_back(ImVector<std::string>());
181 x--;
182 }
183 labels_.push_back(ImVector<std::string>());
184 }
185 return &labels_[i];
186 }
187
190
191 auto selected_tiles() const { return selected_tiles_; }
193
194 auto selected_tile_pos() const { return selected_tile_pos_; }
195 auto set_selected_tile_pos(ImVec2 pos) { selected_tile_pos_ = pos; }
197 auto selected_points() const { return selected_points_; }
198
199 auto hover_mouse_pos() const { return mouse_pos_in_canvas_; }
200
201private:
202 bool draggable_ = false;
203 bool is_hovered_ = false;
204 bool enable_grid_ = true;
210 bool refresh_graphics_ = false;
211
212 float custom_step_ = 0.0f;
213 float global_scale_ = 1.0f;
214
217
221
222 ImDrawList* draw_list_ = nullptr;
223
230 ImVec2 selected_tile_pos_ = ImVec2(-1, -1);
231
232 ImVector<ImVec2> points_;
233 ImVector<ImVec2> selected_points_;
234 ImVector<ImVector<std::string>> labels_;
235
236 std::string canvas_id_ = "Canvas";
237 std::string context_id_ = "CanvasContext";
238 std::vector<ImVec2> selected_tiles_;
239};
240
241void GraphicsBinCanvasPipeline(int width, int height, int tile_size,
242 int num_sheets_to_load, int canvas_id,
243 bool is_loaded, BitmapTable &graphics_bin);
244
245void BitmapCanvasPipeline(gui::Canvas &canvas, const gfx::Bitmap &bitmap,
246 int width, int height, int tile_size, bool is_loaded,
247 bool scrollbar, int canvas_id);
248
249} // namespace gui
250} // namespace yaze
251
252#endif
SharedRom()=default
Represents a bitmap image.
Definition bitmap.h:66
Represents a canvas for drawing and manipulating graphics.
Definition canvas.h:34
ImVec2 scrolling_
Definition canvas.h:224
auto selected_tile_pos() const
Definition canvas.h:194
ImVector< ImVec2 > points_
Definition canvas.h:232
int highlight_tile_id
Definition canvas.h:216
std::string canvas_id_
Definition canvas.h:236
auto labels(int i)
Definition canvas.h:170
auto height() const
Definition canvas.h:167
auto custom_step() const
Definition canvas.h:165
auto set_current_labels(int i)
Definition canvas.h:188
ImVec2 selected_tile_pos_
Definition canvas.h:230
auto global_scale() const
Definition canvas.h:163
ImVec2 canvas_p1_
Definition canvas.h:227
void DrawOutlineWithColor(int x, int y, int w, int h, ImVec4 color)
Definition canvas.cc:518
uint64_t edit_palette_group_name_index_
Definition canvas.h:219
auto selected_tiles() const
Definition canvas.h:191
auto set_draggable(bool value)
Definition canvas.h:168
Canvas(const std::string &id, ImVec2 canvas_size)
Definition canvas.h:40
void DrawOverlay()
Definition canvas.cc:736
auto hover_mouse_pos() const
Definition canvas.h:199
auto push_back(ImVec2 pos)
Definition canvas.h:156
ImVec2 mouse_pos_in_canvas_
Definition canvas.h:229
int GetTileIdFromMousePos()
Definition canvas.h:133
auto points() const
Definition canvas.h:154
auto drawn_tile_position() const
Definition canvas.h:160
Canvas(const std::string &id, ImVec2 canvas_size, CanvasGridSize grid_size, float global_scale)
Definition canvas.h:50
bool DrawSolidTilePainter(const ImVec4 &color, int size)
Definition canvas.cc:259
bool enable_context_menu_
Definition canvas.h:207
auto draw_list() const
Definition canvas.h:157
auto set_selected_tile_pos(ImVec2 pos)
Definition canvas.h:195
auto width() const
Definition canvas.h:166
void DrawLayeredElements()
Definition canvas.cc:759
void DrawBitmap(const Bitmap &bitmap, int border_offset=0, bool ready=true)
Definition canvas.cc:460
bool enable_custom_labels_
Definition canvas.h:206
void UpdateColorPainter(gfx::Bitmap &bitmap, const ImVec4 &color, const std::function< void()> &event, int tile_size, float scale=1.0f)
Definition canvas.cc:43
void DrawGridLines(float grid_step)
Definition canvas.cc:623
uint64_t edit_palette_sub_index_
Definition canvas.h:220
bool custom_canvas_size_
Definition canvas.h:208
void DrawRect(int x, int y, int w, int h, ImVec4 color)
Definition canvas.cc:601
bool DrawTilePainter(const Bitmap &bitmap, int size, float scale=1.0f)
Definition canvas.cc:205
ImDrawList * draw_list_
Definition canvas.h:222
auto custom_labels_enabled()
Definition canvas.h:164
ImVector< ImVec2 > selected_points_
Definition canvas.h:233
void SetCanvasSize(ImVec2 canvas_size)
Definition canvas.h:145
void DrawTileOnBitmap(int tile_size, gfx::Bitmap *bitmap, ImVec4 color)
Definition canvas.cc:318
void DrawCustomHighlight(float grid_step)
Definition canvas.cc:672
bool select_rect_active_
Definition canvas.h:209
auto mutable_selected_tiles()
Definition canvas.h:192
auto mutable_labels(int i)
Definition canvas.h:176
void SetCanvasGridSize(CanvasGridSize grid_size)
Definition canvas.h:56
uint16_t edit_palette_index_
Definition canvas.h:218
auto canvas_size() const
Definition canvas.h:161
ImVector< ImVector< std::string > > labels_
Definition canvas.h:234
void DrawText(std::string text, int x, int y)
Definition canvas.cc:614
void set_global_scale(float scale)
Definition canvas.h:162
bool select_rect_active() const
Definition canvas.h:196
Canvas(const std::string &id)
Definition canvas.h:37
void DrawSelectRect(int current_map, int tile_size=0x10, float scale=1.0f)
Definition canvas.cc:372
auto zero_point() const
Definition canvas.h:158
void DrawBackground(ImVec2 canvas_size=ImVec2(0, 0), bool drag=false)
Definition canvas.cc:65
bool IsMouseHovering() const
Definition canvas.h:150
bool refresh_graphics_
Definition canvas.h:210
void UpdateInfoGrid(ImVec2 bg_size, int tile_size, float scale=1.0f, float grid_size=64.0f, int label_id=0)
Definition canvas.cc:57
float global_scale_
Definition canvas.h:213
void DrawOutline(int x, int y, int w, int h)
Definition canvas.cc:510
auto mutable_points()
Definition canvas.h:155
float custom_step_
Definition canvas.h:212
Canvas(const std::string &id, ImVec2 canvas_size, CanvasGridSize grid_size)
Definition canvas.h:44
void DrawInfoGrid(float grid_step=64.0f, int tile_id_offset=8, int label_id=0)
Definition canvas.cc:638
bool enable_hex_tile_labels_
Definition canvas.h:205
ImVec2 canvas_p0_
Definition canvas.h:226
void DrawBitmapGroup(std::vector< int > &group, std::array< gfx::Bitmap, 4096 > &tile16_individual_, int tile_size, float scale=1.0f)
Definition canvas.cc:535
void DrawContextMenu(gfx::Bitmap *bitmap=nullptr)
Definition canvas.cc:100
auto scrolling() const
Definition canvas.h:159
void DrawBitmapTable(const BitmapTable &gfx_bin)
Definition canvas.cc:497
std::string context_id_
Definition canvas.h:237
ImVec2 canvas_sz_
Definition canvas.h:225
bool DrawTileSelector(int size)
Definition canvas.cc:339
void DrawGrid(float grid_step=64.0f, int tile_id_offset=8)
Definition canvas.cc:685
ImVec2 drawn_tile_pos_
Definition canvas.h:228
auto selected_points() const
Definition canvas.h:197
std::vector< ImVec2 > selected_tiles_
Definition canvas.h:238
auto set_highlight_tile_id(int i)
Definition canvas.h:189
std::unordered_map< int, gfx::Bitmap > BitmapTable
Definition bitmap.h:193
Graphical User Interface (GUI) components for the application.
Definition canvas.cc:15
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:831
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:800
CanvasGridSize
Definition canvas.h:24
Main namespace for the application.
Definition controller.cc:18