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