12#include "imgui/imgui.h"
13#include "imgui/imgui_internal.h"
14#include "imgui_memory_editor.h"
21using ImGui::BeginMenu;
23using ImGui::GetContentRegionAvail;
24using ImGui::GetCursorScreenPos;
26using ImGui::GetMouseDragDelta;
27using ImGui::GetWindowDrawList;
28using ImGui::IsItemActive;
29using ImGui::IsItemHovered;
30using ImGui::IsMouseClicked;
31using ImGui::IsMouseDragging;
33using ImGui::OpenPopupOnItemClick;
34using ImGui::Selectable;
43 ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight;
47 return ImVec2(std::floor((
double)pos.x / scale) * scale,
48 std::floor((
double)pos.y / scale) * scale);
53 const std::function<
void()> &event,
54 int tile_size,
float scale) {
67 float grid_size,
int label_id) {
86 ImGui::InvisibleButton(
92 const ImGuiIO &io = GetIO();
93 const bool is_active = IsItemActive();
96 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
99 if (
const float mouse_threshold_for_pan =
102 IsMouseDragging(ImGuiMouseButton_Right, mouse_threshold_for_pan)) {
110 const ImGuiIO &io = GetIO();
115 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
117 static bool show_bitmap_data =
false;
118 if (show_bitmap_data &&
bitmap_ !=
nullptr) {
119 MemoryEditor mem_edit;
120 mem_edit.DrawWindow(
"Bitmap Data", (
void *)
bitmap_->data(),
bitmap_->size(),
125 if (ImVec2 drag_delta = GetMouseDragDelta(ImGuiMouseButton_Right);
127 OpenPopupOnItemClick(
context_id_.c_str(), ImGuiPopupFlags_MouseButtonRight);
131 if (
MenuItem(
"Reset Position",
nullptr,
false)) {
137 if (BeginMenu(
"Canvas Properties")) {
140 Text(
"Mouse Position: %.0f x %.0f", mouse_pos.x, mouse_pos.y);
144 if (BeginMenu(
"Bitmap Properties")) {
145 Text(
"Size: %.0f x %.0f", scaled_sz.x, scaled_sz.y);
147 Text(
"BitsPerPixel: %d",
bitmap_->surface()->format->BitsPerPixel);
148 Text(
"BytesPerPixel: %d",
bitmap_->surface()->format->BytesPerPixel);
151 if (BeginMenu(
"Bitmap Format")) {
170 if (BeginMenu(
"Bitmap Palette")) {
171 if (
rom()->is_loaded()) {
173 ImGui::SetNextItemWidth(100.f);
177 ImGui::SetNextItemWidth(100.f);
180 auto palette_group =
rom()->mutable_palette_group()->get_group(
184 if (ImGui::BeginChild(
"Palette", ImVec2(0, 300),
true)) {
189 bitmap_->SetPaletteWithTransparent(*palette,
199 MenuItem(
"Bitmap Data",
nullptr, &show_bitmap_data);
202 if (BeginMenu(
"Grid Tile Size")) {
223 const ImGuiIO &io = GetIO();
224 const bool is_hovered = IsItemHovered();
228 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
229 const auto scaled_size = size * scale;
243 ImVec2 paint_pos = AlignPosToGrid(mouse_pos, scaled_size);
246 ImVec2(paint_pos.x + scaled_size, paint_pos.y + scaled_size);
248 points_.push_back(paint_pos_end);
252 ImVec2(origin.x + paint_pos.x, origin.y + paint_pos.y),
253 ImVec2(origin.x + paint_pos.x + scaled_size,
254 origin.y + paint_pos.y + scaled_size));
257 if (IsMouseClicked(ImGuiMouseButton_Left)) {
262 }
else if (ImGui::IsMouseDragging(ImGuiMouseButton_Left)) {
273 const ImGuiIO &io = GetIO();
274 const bool is_hovered = IsItemHovered();
278 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
280 static bool is_dragging =
false;
281 static ImVec2 start_drag_pos;
295 ImVec2 paint_pos = AlignPosToGrid(mouse_pos, scaled_tile_size);
304 ImVec2(paint_pos.x + scaled_tile_size, paint_pos.y + scaled_tile_size));
307 ImVec2(origin.x + paint_pos.x + 1, origin.y + paint_pos.y + 1),
308 ImVec2(origin.x + paint_pos.x + scaled_tile_size,
309 origin.y + paint_pos.y + scaled_tile_size),
310 IM_COL32(color.x * 255, color.y * 255, color.z * 255, 255));
312 if (IsMouseClicked(ImGuiMouseButton_Left)) {
314 start_drag_pos = paint_pos;
317 if (is_dragging && ImGui::IsMouseReleased(ImGuiMouseButton_Left)) {
329 int tile_index_x =
static_cast<int>(position.x /
global_scale_) / tile_size;
330 int tile_index_y =
static_cast<int>(position.y /
global_scale_) / tile_size;
332 ImVec2 start_position(tile_index_x * tile_size, tile_index_y * tile_size);
335 for (
int y = 0; y < tile_size; ++y) {
336 for (
int x = 0; x < tile_size; ++x) {
339 (start_position.y + y) * bitmap->
width() + (start_position.x + x);
348 const ImGuiIO &io = GetIO();
349 const bool is_hovered = IsItemHovered();
351 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
353 if (is_hovered && IsMouseClicked(ImGuiMouseButton_Left)) {
358 painter_pos.x = std::floor((
double)mouse_pos.x / size) * size;
359 painter_pos.y = std::floor((
double)mouse_pos.y / size) * size;
361 points_.push_back(painter_pos);
362 points_.push_back(ImVec2(painter_pos.x + size, painter_pos.y + size));
366 if (is_hovered && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
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;
465 (ImTextureID)(intptr_t)bitmap.
texture(),
491 (ImTextureID)(intptr_t)bitmap.
texture(),
497 ImVec2(0, 0), ImVec2(1, 1), IM_COL32(255, 255, 255, alpha));
502 for (
const auto &[key, value] : gfx_bin) {
503 int offset = 0x40 * (key + 1);
508 draw_list_->AddImage((ImTextureID)(intptr_t)value.texture(),
528 IM_COL32(color.x, color.y, color.z, color.w));
540 std::array<gfx::Bitmap, 4096> &tile16_individual_,
541 int tile_size,
float scale) {
557 static_cast<int>(std::floor(rect_top_left.x / (tile_size * scale)));
559 static_cast<int>(std::floor(rect_top_left.y / (tile_size * scale)));
561 static_cast<int>(std::floor(rect_bottom_right.x / (tile_size * scale)));
563 static_cast<int>(std::floor(rect_bottom_right.y / (tile_size * scale)));
565 if (start_tile_x > end_tile_x) std::swap(start_tile_x, end_tile_x);
566 if (start_tile_y > end_tile_y) std::swap(start_tile_y, end_tile_y);
569 int rect_width = (end_tile_x - start_tile_x) * tile_size;
570 int rect_height = (end_tile_y - start_tile_y) * tile_size;
572 int tiles_per_row = rect_width / tile_size;
573 int tiles_per_col = rect_height / tile_size;
576 for (
int y = 0; y < tiles_per_col + 1; ++y) {
577 for (
int x = 0; x < tiles_per_row + 1; ++x) {
578 int tile_id = group[i];
581 if (tile_id >= 0 && tile_id < tile16_individual_.size()) {
583 int tile_pos_x = (x + start_tile_x) * tile_size * scale;
584 int tile_pos_y = (y + start_tile_y) * tile_size * scale;
587 DrawBitmap(tile16_individual_[tile_id], tile_pos_x, tile_pos_y, scale,
594 const ImGuiIO &io = GetIO();
596 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
597 auto new_start_pos = AlignPosToGrid(mouse_pos, tile_size * scale);
601 ImVec2(new_start_pos.x + rect_width, new_start_pos.y + rect_height));
611 IM_COL32(color.x, color.y, color.z, color.w));
613 ImVec2 outline_origin(origin.x - 1, origin.y - 1);
614 ImVec2 outline_size(size.x + 1, size.y + 1);
628 const uint32_t grid_color = IM_COL32(200, 200, 200, 50);
629 const float grid_thickness = 0.5f;
630 for (
float x = fmodf(
scrolling_.x, grid_step);
635 for (
float y = fmodf(
scrolling_.y, grid_step);
654 for (
float x = fmodf(
scrolling_.x, grid_step);
656 for (
float y = fmodf(
scrolling_.y, grid_step);
660 int tile_id = tile_x + (tile_y * tile_id_offset);
662 if (tile_id >=
labels_[label_id].size()) {
665 std::string label =
labels_[label_id][tile_id];
667 ImVec2(
canvas_p0_.x + x + (grid_step / 2) - tile_id_offset,
668 canvas_p0_.y + y + (grid_step / 2) - tile_id_offset),
682 ImVec2 tile_pos_end(tile_pos.x + grid_step, tile_pos.y + grid_step);
684 draw_list_->AddRectFilled(tile_pos, tile_pos_end,
685 IM_COL32(255, 0, 255, 255));
701 for (
float x = fmodf(
scrolling_.x, grid_step);
703 for (
float y = fmodf(
scrolling_.y, grid_step);
707 int tile_id = tile_x + (tile_y * 16);
708 std::string hex_id = absl::StrFormat(
"%02X", tile_id);
718 for (
float x = fmodf(
scrolling_.x, grid_step);
720 for (
float y = fmodf(
scrolling_.y, grid_step);
724 int tile_id = tile_x + (tile_y * tile_id_offset);
731 ImVec2(
canvas_p0_.x + x + (grid_step / 2) - tile_id_offset,
732 canvas_p0_.y + y + (grid_step / 2) - tile_id_offset),
743 for (
int n = 0; n <
points_.Size; n += 2) {
765 ImDrawList *
draw_list = ImGui::GetWindowDrawList();
767 Text(
"Blue shape is drawn first: appears in back");
768 Text(
"Red shape is drawn after: appears in front");
769 ImVec2 p0 = ImGui::GetCursorScreenPos();
770 draw_list->AddRectFilled(ImVec2(p0.x, p0.y), ImVec2(p0.x + 50, p0.y + 50),
771 IM_COL32(0, 0, 255, 255));
772 draw_list->AddRectFilled(ImVec2(p0.x + 25, p0.y + 25),
773 ImVec2(p0.x + 75, p0.y + 75),
774 IM_COL32(255, 0, 0, 255));
775 ImGui::Dummy(ImVec2(75, 75));
779 Text(
"Blue shape is drawn first, into channel 1: appears in front");
780 Text(
"Red shape is drawn after, into channel 0: appears in back");
781 ImVec2 p1 = ImGui::GetCursorScreenPos();
788 draw_list->AddRectFilled(ImVec2(p1.x, p1.y), ImVec2(p1.x + 50, p1.y + 50),
789 IM_COL32(0, 0, 255, 255));
791 draw_list->AddRectFilled(ImVec2(p1.x + 25, p1.y + 25),
792 ImVec2(p1.x + 75, p1.y + 75),
793 IM_COL32(255, 0, 0, 255));
799 ImGui::Dummy(ImVec2(75, 75));
800 Text(
"After reordering, contents of channel 0 appears below channel 1.");
805 int num_sheets_to_load,
int canvas_id,
808 if (ImGuiID child_id =
809 ImGui::GetID((ImTextureID)(intptr_t)(intptr_t)canvas_id);
810 ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(),
true,
811 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
812 canvas.
DrawBackground(ImVec2(width + 1, num_sheets_to_load * height + 1));
815 for (
const auto &[key, value] : graphics_bin) {
816 int offset = height * (key + 1);
819 top_left_y = canvas.
zero_point().y + height * key;
822 (ImTextureID)(intptr_t)value.texture(),
823 ImVec2(canvas.
zero_point().x + 2, top_left_y),
836 int height,
int tile_size,
bool is_loaded,
837 bool scrollbar,
int canvas_id) {
839 int height,
int tile_size,
bool is_loaded) {
849 if (ImGuiID child_id =
850 ImGui::GetID((ImTextureID)(intptr_t)(intptr_t)canvas_id);
851 ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(),
true,
852 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
853 draw_canvas(canvas, bitmap, width, height, tile_size, is_loaded);
857 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.
Represents a bitmap image.
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=0, bool ready=true)
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 DrawSolidTilePainter(const ImVec4 &color, int size)
bool enable_context_menu_
void DrawLayeredElements()
bool enable_custom_labels_
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 DrawBitmapGroup(std::vector< int > &group, std::array< gfx::Bitmap, 4096 > &tile16_individual_, int tile_size, float scale=1.0f)
void DrawBitmapTable(const BitmapTable &gfx_bin)
bool DrawTileSelector(int size)
void DrawGrid(float grid_step=64.0f, int tile_id_offset=8)
std::vector< ImVec2 > selected_tiles_
static Renderer & GetInstance()
constexpr const char * kPaletteGroupAddressesKeys[]
std::unordered_map< int, gfx::Bitmap > BitmapTable
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)
bool InputHexWord(const char *label, uint16_t *data, float input_width, bool no_step)
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)
Main namespace for the application.