12#include "imgui/imgui.h"
20using ImGui::BeginMenu;
22using ImGui::GetContentRegionAvail;
23using ImGui::GetCursorScreenPos;
25using ImGui::GetMouseDragDelta;
26using ImGui::GetWindowDrawList;
27using ImGui::IsItemActive;
28using ImGui::IsItemHovered;
29using ImGui::IsMouseClicked;
30using ImGui::IsMouseDragging;
32using ImGui::OpenPopupOnItemClick;
33using ImGui::Selectable;
42 ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight;
45 const std::function<
void()> &event,
46 int tile_size,
float scale) {
59 float grid_size,
int label_id) {
78 ImGui::InvisibleButton(
84 const ImGuiIO &io = GetIO();
85 const bool is_active = IsItemActive();
88 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
91 if (
const float mouse_threshold_for_pan =
94 IsMouseDragging(ImGuiMouseButton_Right, mouse_threshold_for_pan)) {
102 const ImGuiIO &io = GetIO();
107 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
110 if (ImVec2 drag_delta = GetMouseDragDelta(ImGuiMouseButton_Right);
112 OpenPopupOnItemClick(
context_id_.c_str(), ImGuiPopupFlags_MouseButtonRight);
116 if (MenuItem(
"Reset Position",
nullptr,
false)) {
122 if (BeginMenu(
"Canvas Properties")) {
125 Text(
"Mouse Position: %.0f x %.0f", mouse_pos.x, mouse_pos.y);
128 if (bitmap !=
nullptr) {
129 if (BeginMenu(
"Bitmap Properties")) {
130 Text(
"Size: %.0f x %.0f", scaled_sz.x, scaled_sz.y);
131 Text(
"Pitch: %d", bitmap->
surface()->pitch);
132 Text(
"BitsPerPixel: %d", bitmap->
surface()->format->BitsPerPixel);
133 Text(
"BytesPerPixel: %d", bitmap->
surface()->format->BytesPerPixel);
136 if (BeginMenu(
"Bitmap Format")) {
137 if (MenuItem(
"Indexed")) {
141 if (MenuItem(
"2BPP")) {
145 if (MenuItem(
"4BPP")) {
149 if (MenuItem(
"8BPP")) {
155 if (BeginMenu(
"Bitmap Palette")) {
156 if (
rom()->is_loaded()) {
158 ImGui::SetNextItemWidth(100.f);
162 ImGui::SetNextItemWidth(100.f);
165 auto palette_group =
rom()->mutable_palette_group()->get_group(
169 if (ImGui::BeginChild(
"Palette", ImVec2(0, 300),
true)) {
186 if (BeginMenu(
"Grid Tile Size")) {
190 if (MenuItem(
"16x16",
nullptr,
custom_step_ == 16.0f)) {
193 if (MenuItem(
"32x32",
nullptr,
custom_step_ == 32.0f)) {
196 if (MenuItem(
"64x64",
nullptr,
custom_step_ == 64.0f)) {
207 const ImGuiIO &io = GetIO();
208 const bool is_hovered = IsItemHovered();
212 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
223 std::floor((
double)mouse_pos.x / (size * scale)) * (size * scale);
225 std::floor((
double)mouse_pos.y / (size * scale)) * (size * scale);
229 auto painter_pos_end =
230 ImVec2(painter_pos.x + (size * scale), painter_pos.y + (size * scale));
231 points_.push_back(painter_pos);
232 points_.push_back(painter_pos_end);
236 (ImTextureID)(intptr_t)bitmap.
texture(),
237 ImVec2(origin.x + painter_pos.x, origin.y + painter_pos.y),
238 ImVec2(origin.x + painter_pos.x + (size)*scale,
239 origin.y + painter_pos.y + size * scale));
242 if (IsMouseClicked(ImGuiMouseButton_Left)) {
247 }
else if (ImGui::IsMouseDragging(ImGuiMouseButton_Left)) {
261 const ImGuiIO &io = GetIO();
262 const bool is_hovered = IsItemHovered();
266 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
269 static bool is_dragging =
false;
270 static ImVec2 start_drag_pos;
281 std::floor((
double)mouse_pos.x / scaled_tile_size) * scaled_tile_size;
283 std::floor((
double)mouse_pos.y / scaled_tile_size) * scaled_tile_size;
291 points_.push_back(painter_pos);
292 points_.push_back(ImVec2(painter_pos.x + scaled_tile_size,
293 painter_pos.y + scaled_tile_size));
296 ImVec2(origin.x + painter_pos.x + 1, origin.y + painter_pos.y + 1),
297 ImVec2(origin.x + painter_pos.x + scaled_tile_size,
298 origin.y + painter_pos.y + scaled_tile_size),
299 IM_COL32(color.x * 255, color.y * 255, color.z * 255, 255));
301 if (IsMouseClicked(ImGuiMouseButton_Left)) {
303 start_drag_pos = painter_pos;
306 if (is_dragging && ImGui::IsMouseReleased(ImGuiMouseButton_Left)) {
322 int tile_index_x =
static_cast<int>(position.x /
global_scale_) / tile_size;
323 int tile_index_y =
static_cast<int>(position.y /
global_scale_) / tile_size;
325 ImVec2 start_position(tile_index_x * tile_size, tile_index_y * tile_size);
328 for (
int y = 0; y < tile_size; ++y) {
329 for (
int x = 0; x < tile_size; ++x) {
332 (start_position.y + y) * bitmap->
width() + (start_position.x + x);
341 const ImGuiIO &io = GetIO();
342 const bool is_hovered = IsItemHovered();
344 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
346 if (is_hovered && IsMouseClicked(ImGuiMouseButton_Left)) {
351 painter_pos.x = std::floor((
double)mouse_pos.x / size) * size;
352 painter_pos.y = std::floor((
double)mouse_pos.y / size) * size;
354 points_.push_back(painter_pos);
355 points_.push_back(ImVec2(painter_pos.x + size, painter_pos.y + size));
359 if (is_hovered && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
368 return ImVec2(std::floor((
double)pos.x / scale) * scale,
369 std::floor((
double)pos.y / scale) * scale);
374 const ImGuiIO &io = GetIO();
376 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
377 static ImVec2 drag_start_pos;
378 const float scaled_size = tile_size * scale;
379 static bool dragging =
false;
380 constexpr int small_map_size = 0x200;
381 int superY = current_map / 8;
382 int superX = current_map % 8;
385 if (IsMouseClicked(ImGuiMouseButton_Right)) {
386 ImVec2 painter_pos = AlignPosToGrid(mouse_pos, scaled_size);
387 int painter_x = painter_pos.x;
388 int painter_y = painter_pos.y;
390 auto tile16_x = (painter_x % small_map_size) / (small_map_size / 0x20);
391 auto tile16_y = (painter_y % small_map_size) / (small_map_size / 0x20);
393 int index_x = superX * 0x20 + tile16_x;
394 int index_y = superY * 0x20 + tile16_y;
400 drag_start_pos = AlignPosToGrid(mouse_pos, scaled_size);
404 ImVec2 drag_end_pos = AlignPosToGrid(mouse_pos, scaled_size);
405 if (ImGui::IsMouseDragging(ImGuiMouseButton_Right)) {
406 auto start = ImVec2(
canvas_p0_.x + drag_start_pos.x,
408 auto end = ImVec2(
canvas_p0_.x + drag_end_pos.x + tile_size,
416 if (!ImGui::IsMouseDown(ImGuiMouseButton_Right)) {
420 constexpr int tile16_size = 16;
421 int start_x = std::floor(drag_start_pos.x / scaled_size) * tile16_size;
422 int start_y = std::floor(drag_start_pos.y / scaled_size) * tile16_size;
423 int end_x = std::floor(drag_end_pos.x / scaled_size) * tile16_size;
424 int end_y = std::floor(drag_end_pos.y / scaled_size) * tile16_size;
427 if (start_x > end_x) std::swap(start_x, end_x);
428 if (start_y > end_y) std::swap(start_y, end_y);
432 constexpr int tiles_per_local_map = small_map_size / 16;
435 for (
int y = start_y; y <= end_y; y += tile16_size) {
436 for (
int x = start_x; x <= end_x; x += tile16_size) {
438 int local_map_x = x / small_map_size;
439 int local_map_y = y / small_map_size;
442 int tile16_x = (x % small_map_size) / tile16_size;
443 int tile16_y = (y % small_map_size) / tile16_size;
446 int index_x = local_map_x * tiles_per_local_map + tile16_x;
447 int index_y = local_map_y * tiles_per_local_map + tile16_y;
464 (ImTextureID)(intptr_t)bitmap.
texture(),
483 float scale,
int alpha) {
488 (ImTextureID)(intptr_t)bitmap.
texture(),
494 ImVec2(0, 0), ImVec2(1, 1), IM_COL32(255, 255, 255, alpha));
499 for (
const auto &[key, value] : gfx_bin) {
500 int offset = 0x40 * (key + 1);
505 draw_list_->AddImage((ImTextureID)(intptr_t)value.texture(),
525 IM_COL32(color.x, color.y, color.z, color.w));
537 std::vector<gfx::Bitmap> &tile16_individual_,
538 int tile_size,
float scale) {
554 static_cast<int>(std::floor(rect_top_left.x / (tile_size * scale)));
556 static_cast<int>(std::floor(rect_top_left.y / (tile_size * scale)));
558 static_cast<int>(std::floor(rect_bottom_right.x / (tile_size * scale)));
560 static_cast<int>(std::floor(rect_bottom_right.y / (tile_size * scale)));
562 if (start_tile_x > end_tile_x) std::swap(start_tile_x, end_tile_x);
563 if (start_tile_y > end_tile_y) std::swap(start_tile_y, end_tile_y);
566 int rect_width = (end_tile_x - start_tile_x) * tile_size;
567 int rect_height = (end_tile_y - start_tile_y) * tile_size;
569 int tiles_per_row = rect_width / tile_size;
570 int tiles_per_col = rect_height / tile_size;
573 for (
int y = 0; y < tiles_per_col + 1; ++y) {
574 for (
int x = 0; x < tiles_per_row + 1; ++x) {
575 int tile_id = group[i];
578 if (tile_id >= 0 && tile_id < tile16_individual_.size()) {
580 int tile_pos_x = (x + start_tile_x) * tile_size * scale;
581 int tile_pos_y = (y + start_tile_y) * tile_size * scale;
584 DrawBitmap(tile16_individual_[tile_id], tile_pos_x, tile_pos_y, scale,
591 const ImGuiIO &io = GetIO();
593 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
594 auto new_start_pos = AlignPosToGrid(mouse_pos, tile_size * scale);
598 ImVec2(new_start_pos.x + rect_width, new_start_pos.y + rect_height));
608 IM_COL32(color.x, color.y, color.z, color.w));
610 ImVec2 outline_origin(origin.x - 1, origin.y - 1);
611 ImVec2 outline_size(size.x + 1, size.y + 1);
625 const uint32_t grid_color = IM_COL32(200, 200, 200, 50);
626 const float grid_thickness = 0.5f;
627 for (
float x = fmodf(
scrolling_.x, grid_step);
632 for (
float y = fmodf(
scrolling_.y, grid_step);
651 for (
float x = fmodf(
scrolling_.x, grid_step);
653 for (
float y = fmodf(
scrolling_.y, grid_step);
657 int tile_id = tile_x + (tile_y * tile_id_offset);
659 if (tile_id >=
labels_[label_id].size()) {
662 std::string label =
labels_[label_id][tile_id];
664 ImVec2(
canvas_p0_.x + x + (grid_step / 2) - tile_id_offset,
665 canvas_p0_.y + y + (grid_step / 2) - tile_id_offset),
679 ImVec2 tile_pos_end(tile_pos.x + grid_step, tile_pos.y + grid_step);
681 draw_list_->AddRectFilled(tile_pos, tile_pos_end,
682 IM_COL32(255, 0, 255, 255));
698 for (
float x = fmodf(
scrolling_.x, grid_step);
700 for (
float y = fmodf(
scrolling_.y, grid_step);
704 int tile_id = tile_x + (tile_y * 16);
705 std::string hex_id = absl::StrFormat(
"%02X", tile_id);
715 for (
float x = fmodf(
scrolling_.x, grid_step);
717 for (
float y = fmodf(
scrolling_.y, grid_step);
721 int tile_id = tile_x + (tile_y * tile_id_offset);
728 ImVec2(
canvas_p0_.x + x + (grid_step / 2) - tile_id_offset,
729 canvas_p0_.y + y + (grid_step / 2) - tile_id_offset),
740 for (
int n = 0; n <
points_.Size; n += 2) {
762 ImDrawList *
draw_list = ImGui::GetWindowDrawList();
764 Text(
"Blue shape is drawn first: appears in back");
765 Text(
"Red shape is drawn after: appears in front");
766 ImVec2 p0 = ImGui::GetCursorScreenPos();
767 draw_list->AddRectFilled(ImVec2(p0.x, p0.y), ImVec2(p0.x + 50, p0.y + 50),
768 IM_COL32(0, 0, 255, 255));
769 draw_list->AddRectFilled(ImVec2(p0.x + 25, p0.y + 25),
770 ImVec2(p0.x + 75, p0.y + 75),
771 IM_COL32(255, 0, 0, 255));
772 ImGui::Dummy(ImVec2(75, 75));
776 Text(
"Blue shape is drawn first, into channel 1: appears in front");
777 Text(
"Red shape is drawn after, into channel 0: appears in back");
778 ImVec2 p1 = ImGui::GetCursorScreenPos();
785 draw_list->AddRectFilled(ImVec2(p1.x, p1.y), ImVec2(p1.x + 50, p1.y + 50),
786 IM_COL32(0, 0, 255, 255));
788 draw_list->AddRectFilled(ImVec2(p1.x + 25, p1.y + 25),
789 ImVec2(p1.x + 75, p1.y + 75),
790 IM_COL32(255, 0, 0, 255));
796 ImGui::Dummy(ImVec2(75, 75));
797 Text(
"After reordering, contents of channel 0 appears below channel 1.");
802 int num_sheets_to_load,
int canvas_id,
805 if (ImGuiID child_id =
806 ImGui::GetID((ImTextureID)(intptr_t)(intptr_t)canvas_id);
807 ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(),
true,
808 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
809 canvas.
DrawBackground(ImVec2(width + 1, num_sheets_to_load * height + 1));
812 for (
const auto &[key, value] : graphics_bin) {
813 int offset = height * (key + 1);
816 top_left_y = canvas.
zero_point().y + height * key;
819 (ImTextureID)(intptr_t)value.texture(),
820 ImVec2(canvas.
zero_point().x + 2, top_left_y),
833 int width,
int height,
int tile_size,
bool is_loaded,
834 bool scrollbar,
int canvas_id) {
836 int width,
int height,
int tile_size,
bool is_loaded) {
846 if (ImGuiID child_id =
847 ImGui::GetID((ImTextureID)(intptr_t)(intptr_t)canvas_id);
848 ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(),
true,
849 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
850 draw_canvas(canvas, bitmap, width, height, tile_size, is_loaded);
854 draw_canvas(canvas, bitmap, width, height, tile_size, is_loaded);
The Renderer class represents the renderer for the Yaze application.
void UpdateBitmap(gfx::Bitmap *bitmap)
Used to update a bitmap on the screen.
static Renderer & GetInstance()
Represents a bitmap image.
void Reformat(int format)
absl::Status ApplyPaletteWithTransparent(const SnesPalette &palette, size_t index, int length=7)
void WriteColor(int position, const ImVec4 &color)
Represents a canvas for drawing and manipulating graphics.
bool DrawTileSelector(int size)
void DrawOutline(int x, int y, int w, int h)
void DrawBackground(ImVec2 canvas_size=ImVec2(0, 0), bool drag=false)
void DrawCustomHighlight(float grid_step)
void DrawRect(int x, int y, int w, int h, ImVec4 color)
void UpdateInfoGrid(ImVec2 bg_size, int tile_size, float scale=1.0f, float grid_size=64.0f, int label_id=0)
ImVec2 selected_tile_pos_
void DrawInfoGrid(float grid_step=64.0f, int tile_id_offset=8, int label_id=0)
void DrawGrid(float grid_step=64.0f, int tile_id_offset=8)
void DrawBitmapGroup(std::vector< int > &group, std::vector< gfx::Bitmap > &tile16_individual_, int tile_size, float scale=1.0f)
void DrawText(std::string text, int x, int y)
bool DrawSolidTilePainter(const ImVec4 &color, int size)
void DrawTileOnBitmap(int tile_size, gfx::Bitmap *bitmap, ImVec4 color)
void DrawBitmap(const Bitmap &bitmap, int border_offset=0, bool ready=true)
uint64_t edit_palette_sub_index_
bool enable_context_menu_
void DrawSelectRect(int current_map, int tile_size=0x10, float scale=1.0f)
void DrawOutlineWithColor(int x, int y, int w, int h, ImVec4 color)
uint16_t edit_palette_index_
std::vector< ImVec2 > selected_tiles_
bool enable_custom_labels_
void DrawBitmapTable(const BitmapTable &gfx_bin)
ImVector< ImVec2 > points_
bool DrawTilePainter(const Bitmap &bitmap, int size, float scale=1.0f)
void UpdateColorPainter(gfx::Bitmap &bitmap, const ImVec4 &color, const std::function< void()> &event, int tile_size, float scale=1.0f)
void DrawLayeredElements()
uint64_t edit_palette_group_name_index_
ImVec2 mouse_pos_in_canvas_
bool enable_hex_tile_labels_
void DrawContextMenu(gfx::Bitmap *bitmap=nullptr)
ImVector< ImVec2 > selected_points_
ImVector< ImVector< std::string > > labels_
void DrawGridLines(float grid_step)
std::unordered_map< int, gfx::Bitmap > BitmapTable
constexpr const char * kPaletteGroupAddressesKeys[]
ImVec2 AlignPosToGrid(ImVec2 pos, float scale)
void SelectablePalettePipeline(uint64_t &palette_id, bool &refresh_graphics, gfx::SnesPalette &palette)
constexpr uint32_t kRectangleColor
constexpr uint32_t kWhiteColor
constexpr ImGuiButtonFlags kMouseFlags
void TextWithSeparators(const absl::string_view &text)
void BitmapCanvasPipeline(gui::Canvas &canvas, const gfx::Bitmap &bitmap, int width, int height, int tile_size, bool is_loaded, bool scrollbar, int canvas_id)
bool InputHexWord(const char *label, uint16_t *data, float input_width, bool no_step)
constexpr uint32_t kOutlineRect
constexpr uint32_t kBlackColor
void GraphicsBinCanvasPipeline(int width, int height, int tile_size, int num_sheets_to_load, int canvas_id, bool is_loaded, gfx::BitmapTable &graphics_bin)