12#include "imgui/imgui.h"
13#include "imgui_memory_editor.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;
46 return ImVec2(std::floor((
double)pos.x / scale) * scale,
47 std::floor((
double)pos.y / scale) * scale);
52 const std::function<
void()> &event,
53 int tile_size,
float scale) {
66 float grid_size,
int label_id) {
85 ImGui::InvisibleButton(
91 const ImGuiIO &io = GetIO();
92 const bool is_active = IsItemActive();
95 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
98 if (
const float mouse_threshold_for_pan =
101 IsMouseDragging(ImGuiMouseButton_Right, mouse_threshold_for_pan)) {
109 const ImGuiIO &io = GetIO();
114 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
116 static bool show_bitmap_data =
false;
117 if (show_bitmap_data &&
bitmap_ !=
nullptr) {
118 MemoryEditor mem_edit;
119 mem_edit.DrawWindow(
"Bitmap Data", (
void *)
bitmap_->data(),
bitmap_->size(),
124 if (ImVec2 drag_delta = GetMouseDragDelta(ImGuiMouseButton_Right);
126 OpenPopupOnItemClick(
context_id_.c_str(), ImGuiPopupFlags_MouseButtonRight);
130 if (
MenuItem(
"Reset Position",
nullptr,
false)) {
136 if (BeginMenu(
"Canvas Properties")) {
139 Text(
"Mouse Position: %.0f x %.0f", mouse_pos.x, mouse_pos.y);
143 if (BeginMenu(
"Bitmap Properties")) {
144 Text(
"Size: %.0f x %.0f", scaled_sz.x, scaled_sz.y);
146 Text(
"BitsPerPixel: %d",
bitmap_->surface()->format->BitsPerPixel);
147 Text(
"BytesPerPixel: %d",
bitmap_->surface()->format->BytesPerPixel);
150 if (BeginMenu(
"Bitmap Format")) {
166 if (BeginMenu(
"View Palette")) {
171 if (BeginMenu(
"Bitmap Palette")) {
172 if (
rom()->is_loaded()) {
174 ImGui::SetNextItemWidth(100.f);
178 ImGui::SetNextItemWidth(100.f);
181 auto palette_group =
rom()->mutable_palette_group()->get_group(
185 if (ImGui::BeginChild(
"Palette", ImVec2(0, 300),
true)) {
190 bitmap_->SetPaletteWithTransparent(*palette,
200 MenuItem(
"Bitmap Data",
nullptr, &show_bitmap_data);
203 if (BeginMenu(
"Grid Tile Size")) {
224 const ImGuiIO &io = GetIO();
225 const bool is_hovered = IsItemHovered();
229 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
230 const auto scaled_size = size * scale;
244 ImVec2 paint_pos = AlignPosToGrid(mouse_pos, scaled_size);
247 ImVec2(paint_pos.x + scaled_size, paint_pos.y + scaled_size);
249 points_.push_back(paint_pos_end);
253 ImVec2(origin.x + paint_pos.x, origin.y + paint_pos.y),
254 ImVec2(origin.x + paint_pos.x + scaled_size,
255 origin.y + paint_pos.y + scaled_size));
258 if (IsMouseClicked(ImGuiMouseButton_Left)) {
263 }
else if (ImGui::IsMouseDragging(ImGuiMouseButton_Left)) {
274 const ImGuiIO &io = GetIO();
275 const bool is_hovered = IsItemHovered();
278 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
290 ImVec2 paint_pos = AlignPosToGrid(mouse_pos, scaled_size);
295 ImVec2(paint_pos.x + scaled_size, paint_pos.y + scaled_size));
306 (ImTextureID)(intptr_t)tilemap.
tile_bitmaps[current_tile].texture(),
307 ImVec2(origin.x + paint_pos.x, origin.y + paint_pos.y),
308 ImVec2(origin.x + paint_pos.x + scaled_size,
309 origin.y + paint_pos.y + scaled_size));
311 if (IsMouseClicked(ImGuiMouseButton_Left)) {
314 }
else if (ImGui::IsMouseDragging(ImGuiMouseButton_Left)) {
323 const ImGuiIO &io = GetIO();
324 const bool is_hovered = IsItemHovered();
328 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
330 static bool is_dragging =
false;
331 static ImVec2 start_drag_pos;
345 ImVec2 paint_pos = AlignPosToGrid(mouse_pos, scaled_tile_size);
354 ImVec2(paint_pos.x + scaled_tile_size, paint_pos.y + scaled_tile_size));
357 ImVec2(origin.x + paint_pos.x + 1, origin.y + paint_pos.y + 1),
358 ImVec2(origin.x + paint_pos.x + scaled_tile_size,
359 origin.y + paint_pos.y + scaled_tile_size),
360 IM_COL32(color.x * 255, color.y * 255, color.z * 255, 255));
362 if (IsMouseClicked(ImGuiMouseButton_Left)) {
364 start_drag_pos = paint_pos;
367 if (is_dragging && ImGui::IsMouseReleased(ImGuiMouseButton_Left)) {
379 int tile_index_x =
static_cast<int>(position.x /
global_scale_) / tile_size;
380 int tile_index_y =
static_cast<int>(position.y /
global_scale_) / tile_size;
382 ImVec2 start_position(tile_index_x * tile_size, tile_index_y * tile_size);
385 for (
int y = 0; y < tile_size; ++y) {
386 for (
int x = 0; x < tile_size; ++x) {
389 (start_position.y + y) * bitmap->
width() + (start_position.x + x);
398 const ImGuiIO &io = GetIO();
399 const bool is_hovered = IsItemHovered();
401 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
406 if (is_hovered && IsMouseClicked(ImGuiMouseButton_Left)) {
411 painter_pos.x = std::floor((
double)mouse_pos.x / size) * size;
412 painter_pos.y = std::floor((
double)mouse_pos.y / size) * size;
414 points_.push_back(painter_pos);
415 points_.push_back(ImVec2(painter_pos.x + size, painter_pos.y + size_y));
419 if (is_hovered && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
427 const ImGuiIO &io = GetIO();
429 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
430 static ImVec2 drag_start_pos;
431 const float scaled_size = tile_size * scale;
432 static bool dragging =
false;
433 constexpr int small_map_size = 0x200;
434 int superY = current_map / 8;
435 int superX = current_map % 8;
438 if (IsMouseClicked(ImGuiMouseButton_Right)) {
439 ImVec2 painter_pos = AlignPosToGrid(mouse_pos, scaled_size);
440 int painter_x = painter_pos.x;
441 int painter_y = painter_pos.y;
443 auto tile16_x = (painter_x % small_map_size) / (small_map_size / 0x20);
444 auto tile16_y = (painter_y % small_map_size) / (small_map_size / 0x20);
446 int index_x = superX * 0x20 + tile16_x;
447 int index_y = superY * 0x20 + tile16_y;
453 drag_start_pos = AlignPosToGrid(mouse_pos, scaled_size);
457 ImVec2 drag_end_pos = AlignPosToGrid(mouse_pos, scaled_size);
458 if (ImGui::IsMouseDragging(ImGuiMouseButton_Right)) {
459 auto start = ImVec2(
canvas_p0_.x + drag_start_pos.x,
461 auto end = ImVec2(
canvas_p0_.x + drag_end_pos.x + tile_size,
469 if (!ImGui::IsMouseDown(ImGuiMouseButton_Right)) {
473 constexpr int tile16_size = 16;
474 int start_x = std::floor(drag_start_pos.x / scaled_size) * tile16_size;
475 int start_y = std::floor(drag_start_pos.y / scaled_size) * tile16_size;
476 int end_x = std::floor(drag_end_pos.x / scaled_size) * tile16_size;
477 int end_y = std::floor(drag_end_pos.y / scaled_size) * tile16_size;
480 if (start_x > end_x) std::swap(start_x, end_x);
481 if (start_y > end_y) std::swap(start_y, end_y);
485 constexpr int tiles_per_local_map = small_map_size / 16;
488 for (
int y = start_y; y <= end_y; y += tile16_size) {
489 for (
int x = start_x; x <= end_x; x += tile16_size) {
491 int local_map_x = x / small_map_size;
492 int local_map_y = y / small_map_size;
495 int tile16_x = (x % small_map_size) / tile16_size;
496 int tile16_y = (y % small_map_size) / tile16_size;
499 int index_x = local_map_x * tiles_per_local_map + tile16_x;
500 int index_y = local_map_y * tiles_per_local_map + tile16_y;
533 (ImTextureID)(intptr_t)bitmap.
texture(),
539 ImVec2(0, 0), ImVec2(1, 1), IM_COL32(255, 255, 255, alpha));
543 ImVec2 src_pos, ImVec2 src_size) {
549 (ImTextureID)(intptr_t)bitmap.
texture(),
551 ImVec2(
canvas_p0_.x + dest_pos.x + dest_size.x,
553 ImVec2(src_pos.x / bitmap.
width(), src_pos.y / bitmap.
height()),
554 ImVec2((src_pos.x + src_size.x) / bitmap.
width(),
555 (src_pos.y + src_size.y) / bitmap.
height()));
560 for (
const auto &[key, value] : gfx_bin) {
561 int offset = 0x40 * (key + 1);
566 draw_list_->AddImage((ImTextureID)(intptr_t)value.texture(),
586 IM_COL32(color.x, color.y, color.z, color.w));
598 int tile_size,
float scale) {
614 static_cast<int>(std::floor(rect_top_left.x / (tile_size * scale)));
616 static_cast<int>(std::floor(rect_top_left.y / (tile_size * scale)));
618 static_cast<int>(std::floor(rect_bottom_right.x / (tile_size * scale)));
620 static_cast<int>(std::floor(rect_bottom_right.y / (tile_size * scale)));
622 if (start_tile_x > end_tile_x) std::swap(start_tile_x, end_tile_x);
623 if (start_tile_y > end_tile_y) std::swap(start_tile_y, end_tile_y);
626 int rect_width = (end_tile_x - start_tile_x) * tile_size;
627 int rect_height = (end_tile_y - start_tile_y) * tile_size;
629 int tiles_per_row = rect_width / tile_size;
630 int tiles_per_col = rect_height / tile_size;
633 for (
int y = 0; y < tiles_per_col + 1; ++y) {
634 for (
int x = 0; x < tiles_per_row + 1; ++x) {
635 int tile_id = group[i];
640 if (tile_id >= 0 && tile_id < tilemap_size) {
642 int tile_pos_x = (x + start_tile_x) * tile_size * scale;
643 int tile_pos_y = (y + start_tile_y) * tile_size * scale;
654 const ImGuiIO &io = GetIO();
656 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
657 auto new_start_pos = AlignPosToGrid(mouse_pos, tile_size * scale);
661 ImVec2(new_start_pos.x + rect_width, new_start_pos.y + rect_height));
671 IM_COL32(color.x, color.y, color.z, color.w));
673 ImVec2 outline_origin(origin.x - 1, origin.y - 1);
674 ImVec2 outline_size(size.x + 1, size.y + 1);
688 const uint32_t grid_color = IM_COL32(200, 200, 200, 50);
689 const float grid_thickness = 0.5f;
690 for (
float x = fmodf(
scrolling_.x, grid_step);
695 for (
float y = fmodf(
scrolling_.y, grid_step);
714 for (
float x = fmodf(
scrolling_.x, grid_step);
716 for (
float y = fmodf(
scrolling_.y, grid_step);
720 int tile_id = tile_x + (tile_y * tile_id_offset);
722 if (tile_id >=
labels_[label_id].size()) {
725 std::string label =
labels_[label_id][tile_id];
727 ImVec2(
canvas_p0_.x + x + (grid_step / 2) - tile_id_offset,
728 canvas_p0_.y + y + (grid_step / 2) - tile_id_offset),
742 ImVec2 tile_pos_end(tile_pos.x + grid_step, tile_pos.y + grid_step);
744 draw_list_->AddRectFilled(tile_pos, tile_pos_end,
745 IM_COL32(255, 0, 255, 255));
761 for (
float x = fmodf(
scrolling_.x, grid_step);
763 for (
float y = fmodf(
scrolling_.y, grid_step);
767 int tile_id = tile_x + (tile_y * 16);
768 std::string hex_id = absl::StrFormat(
"%02X", tile_id);
778 for (
float x = fmodf(
scrolling_.x, grid_step);
780 for (
float y = fmodf(
scrolling_.y, grid_step);
784 int tile_id = tile_x + (tile_y * tile_id_offset);
791 ImVec2(
canvas_p0_.x + x + (grid_step / 2) - tile_id_offset,
792 canvas_p0_.y + y + (grid_step / 2) - tile_id_offset),
803 for (
int n = 0; n <
points_.Size; n += 2) {
825 ImDrawList *
draw_list = ImGui::GetWindowDrawList();
827 Text(
"Blue shape is drawn first: appears in back");
828 Text(
"Red shape is drawn after: appears in front");
829 ImVec2 p0 = ImGui::GetCursorScreenPos();
830 draw_list->AddRectFilled(ImVec2(p0.x, p0.y), ImVec2(p0.x + 50, p0.y + 50),
831 IM_COL32(0, 0, 255, 255));
832 draw_list->AddRectFilled(ImVec2(p0.x + 25, p0.y + 25),
833 ImVec2(p0.x + 75, p0.y + 75),
834 IM_COL32(255, 0, 0, 255));
835 ImGui::Dummy(ImVec2(75, 75));
839 Text(
"Blue shape is drawn first, into channel 1: appears in front");
840 Text(
"Red shape is drawn after, into channel 0: appears in back");
841 ImVec2 p1 = ImGui::GetCursorScreenPos();
848 draw_list->AddRectFilled(ImVec2(p1.x, p1.y), ImVec2(p1.x + 50, p1.y + 50),
849 IM_COL32(0, 0, 255, 255));
851 draw_list->AddRectFilled(ImVec2(p1.x + 25, p1.y + 25),
852 ImVec2(p1.x + 75, p1.y + 75),
853 IM_COL32(255, 0, 0, 255));
859 ImGui::Dummy(ImVec2(75, 75));
860 Text(
"After reordering, contents of channel 0 appears below channel 1.");
866 ImGui::BeginChild(canvas.
canvas_id().c_str(), child_size,
true);
879 int num_sheets_to_load,
int canvas_id,
882 if (ImGuiID child_id =
883 ImGui::GetID((ImTextureID)(intptr_t)(intptr_t)canvas_id);
884 ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(),
true,
885 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
886 canvas.
DrawBackground(ImVec2(width + 1, num_sheets_to_load * height + 1));
889 for (
const auto &[key, value] : graphics_bin) {
890 int offset = height * (key + 1);
893 top_left_y = canvas.
zero_point().y + height * key;
896 (ImTextureID)(intptr_t)value.texture(),
897 ImVec2(canvas.
zero_point().x + 2, top_left_y),
910 int height,
int tile_size,
bool is_loaded,
911 bool scrollbar,
int canvas_id) {
913 int height,
int tile_size,
bool is_loaded) {
923 if (ImGuiID child_id =
924 ImGui::GetID((ImTextureID)(intptr_t)(intptr_t)canvas_id);
925 ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(),
true,
926 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
927 draw_canvas(canvas, bitmap, width, height, tile_size, is_loaded);
931 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)
void RenderBitmap(gfx::Bitmap *bitmap)
Represents a bitmap image.
const SnesPalette & palette() const
SDL_Texture * texture() const
void WriteColor(int position, const ImVec4 &color)
Write a color to a pixel at the given position.
Represents a canvas for drawing and manipulating graphics.
ImVector< ImVec2 > points_
void DrawBitmap(Bitmap &bitmap, int border_offset, float scale)
void DrawBitmapGroup(std::vector< int > &group, gfx::Tilemap &tilemap, int tile_size, float scale=1.0f)
ImVec2 selected_tile_pos_
void DrawOutlineWithColor(int x, int y, int w, int h, ImVec4 color)
uint64_t edit_palette_group_name_index_
ImVec2 mouse_pos_in_canvas_
bool DrawTilemapPainter(gfx::Tilemap &tilemap, int current_tile)
bool DrawSolidTilePainter(const ImVec4 &color, int size)
bool enable_context_menu_
void DrawLayeredElements()
bool enable_custom_labels_
bool DrawTileSelector(int size, int size_y=0)
void UpdateColorPainter(gfx::Bitmap &bitmap, const ImVec4 &color, const std::function< void()> &event, int tile_size, float scale=1.0f)
void DrawGridLines(float grid_step)
uint64_t edit_palette_sub_index_
void DrawRect(int x, int y, int w, int h, ImVec4 color)
bool DrawTilePainter(const Bitmap &bitmap, int size, float scale=1.0f)
ImVector< ImVec2 > selected_points_
void DrawTileOnBitmap(int tile_size, gfx::Bitmap *bitmap, ImVec4 color)
void DrawCustomHighlight(float grid_step)
uint16_t edit_palette_index_
ImVector< ImVector< std::string > > labels_
void DrawText(std::string text, int x, int y)
void DrawSelectRect(int current_map, int tile_size=0x10, float scale=1.0f)
void DrawBackground(ImVec2 canvas_size=ImVec2(0, 0), bool drag=false)
void UpdateInfoGrid(ImVec2 bg_size, int tile_size, float scale=1.0f, float grid_size=64.0f, int label_id=0)
void DrawOutline(int x, int y, int w, int h)
void DrawInfoGrid(float grid_step=64.0f, int tile_id_offset=8, int label_id=0)
bool enable_hex_tile_labels_
void DrawBitmapTable(const BitmapTable &gfx_bin)
void DrawGrid(float grid_step=64.0f, int tile_id_offset=8)
std::vector< ImVec2 > selected_tiles_
constexpr const char * kPaletteGroupAddressesKeys[]
std::vector< uint8_t > GetTilemapData(Tilemap &tilemap, int tile_id)
std::unordered_map< int, gfx::Bitmap > BitmapTable
void RenderTile(Tilemap &tilemap, int tile_id)
ImVec2 AlignPosToGrid(ImVec2 pos, float scale)
Graphical User Interface (GUI) components for the application.
constexpr uint32_t kWhiteColor
constexpr uint32_t kRectangleColor
void BitmapCanvasPipeline(gui::Canvas &canvas, gfx::Bitmap &bitmap, int width, int height, int tile_size, bool is_loaded, bool scrollbar, int canvas_id)
void EndCanvas(Canvas &canvas)
bool InputHexWord(const char *label, uint16_t *data, float input_width, bool no_step)
void BeginCanvas(Canvas &canvas, ImVec2 child_size)
void GraphicsBinCanvasPipeline(int width, int height, int tile_size, int num_sheets_to_load, int canvas_id, bool is_loaded, gfx::BitmapTable &graphics_bin)
constexpr uint32_t kOutlineRect
void SelectablePalettePipeline(uint64_t &palette_id, bool &refresh_graphics, gfx::SnesPalette &palette)
constexpr uint32_t kBlackColor
constexpr ImGuiButtonFlags kMouseFlags
void TextWithSeparators(const absl::string_view &text)
absl::Status DisplayEditablePalette(gfx::SnesPalette &palette, const std::string &title, bool show_color_picker, int colors_per_row, ImGuiColorEditFlags flags)
Main namespace for the application.
absl::flat_hash_map< int, Bitmap > tile_bitmaps