13#include "imgui/imgui.h"
21using ImGui::BeginMenu;
22using ImGui::BeginPopup;
24using ImGui::GetContentRegionAvail;
25using ImGui::GetCursorScreenPos;
27using ImGui::GetMouseDragDelta;
28using ImGui::GetWindowDrawList;
29using ImGui::IsItemActive;
30using ImGui::IsItemHovered;
31using ImGui::IsMouseClicked;
32using ImGui::IsMouseDragging;
34using ImGui::OpenPopupOnItemClick;
35using ImGui::Selectable;
36using ImGui::Separator;
45 ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight;
48 const std::function<
void()> &event,
49 int tile_size,
float scale) {
62 float grid_size,
int label_id) {
81 ImGui::InvisibleButton(
87 const ImGuiIO &io = GetIO();
88 const bool is_active = IsItemActive();
91 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
94 if (
const float mouse_threshold_for_pan =
97 IsMouseDragging(ImGuiMouseButton_Right, mouse_threshold_for_pan)) {
105 const ImGuiIO &io = GetIO();
110 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
113 if (ImVec2 drag_delta = GetMouseDragDelta(ImGuiMouseButton_Right);
115 OpenPopupOnItemClick(
context_id_.c_str(), ImGuiPopupFlags_MouseButtonRight);
119 if (MenuItem(
"Reset Position",
nullptr,
false)) {
125 if (BeginMenu(
"Canvas Properties")) {
128 Text(
"Mouse Position: %.0f x %.0f", mouse_pos.x, mouse_pos.y);
131 if (bitmap !=
nullptr) {
132 if (BeginMenu(
"Bitmap Properties")) {
133 Text(
"Size: %.0f x %.0f", scaled_sz.x, scaled_sz.y);
134 Text(
"Pitch: %d", bitmap->
surface()->pitch);
135 Text(
"BitsPerPixel: %d", bitmap->
surface()->format->BitsPerPixel);
136 Text(
"BytesPerPixel: %d", bitmap->
surface()->format->BytesPerPixel);
139 if (BeginMenu(
"Bitmap Format")) {
140 if (MenuItem(
"Indexed")) {
144 if (MenuItem(
"2BPP")) {
148 if (MenuItem(
"4BPP")) {
152 if (MenuItem(
"8BPP")) {
158 if (BeginMenu(
"Bitmap Palette")) {
159 if (
rom()->is_loaded()) {
161 ImGui::SetNextItemWidth(100.f);
165 ImGui::SetNextItemWidth(100.f);
168 auto palette_group =
rom()->mutable_palette_group()->get_group(
172 if (ImGui::BeginChild(
"Palette", ImVec2(0, 300),
true)) {
189 if (BeginMenu(
"Grid Tile Size")) {
193 if (MenuItem(
"16x16",
nullptr,
custom_step_ == 16.0f)) {
196 if (MenuItem(
"32x32",
nullptr,
custom_step_ == 32.0f)) {
199 if (MenuItem(
"64x64",
nullptr,
custom_step_ == 64.0f)) {
210 const ImGuiIO &io = GetIO();
211 const bool is_hovered = IsItemHovered();
215 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
226 std::floor((
double)mouse_pos.x / (size * scale)) * (size * scale);
228 std::floor((
double)mouse_pos.y / (size * scale)) * (size * scale);
232 auto painter_pos_end =
233 ImVec2(painter_pos.x + (size * scale), painter_pos.y + (size * scale));
234 points_.push_back(painter_pos);
235 points_.push_back(painter_pos_end);
240 ImVec2(origin.x + painter_pos.x, origin.y + painter_pos.y),
241 ImVec2(origin.x + painter_pos.x + (size)*scale,
242 origin.y + painter_pos.y + size * scale));
245 if (IsMouseClicked(ImGuiMouseButton_Left)) {
250 }
else if (ImGui::IsMouseDragging(ImGuiMouseButton_Left)) {
264 const ImGuiIO &io = GetIO();
265 const bool is_hovered = IsItemHovered();
269 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
272 static bool is_dragging =
false;
273 static ImVec2 start_drag_pos;
284 std::floor((
double)mouse_pos.x / scaled_tile_size) * scaled_tile_size;
286 std::floor((
double)mouse_pos.y / scaled_tile_size) * scaled_tile_size;
294 points_.push_back(painter_pos);
295 points_.push_back(ImVec2(painter_pos.x + scaled_tile_size,
296 painter_pos.y + scaled_tile_size));
299 ImVec2(origin.x + painter_pos.x + 1, origin.y + painter_pos.y + 1),
300 ImVec2(origin.x + painter_pos.x + scaled_tile_size,
301 origin.y + painter_pos.y + scaled_tile_size),
302 IM_COL32(color.x * 255, color.y * 255, color.z * 255, 255));
304 if (IsMouseClicked(ImGuiMouseButton_Left)) {
306 start_drag_pos = painter_pos;
309 if (is_dragging && ImGui::IsMouseReleased(ImGuiMouseButton_Left)) {
325 int tile_index_x =
static_cast<int>(position.x /
global_scale_) / tile_size;
326 int tile_index_y =
static_cast<int>(position.y /
global_scale_) / tile_size;
328 ImVec2 start_position(tile_index_x * tile_size, tile_index_y * tile_size);
331 for (
int y = 0; y < tile_size; ++y) {
332 for (
int x = 0; x < tile_size; ++x) {
335 (start_position.y + y) * bitmap->
width() + (start_position.x + x);
344 const ImGuiIO &io = GetIO();
345 const bool is_hovered = IsItemHovered();
347 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
349 if (is_hovered && IsMouseClicked(ImGuiMouseButton_Left)) {
354 painter_pos.x = std::floor((
double)mouse_pos.x / size) * size;
355 painter_pos.y = std::floor((
double)mouse_pos.y / size) * size;
357 points_.push_back(painter_pos);
358 points_.push_back(ImVec2(painter_pos.x + size, painter_pos.y + size));
362 if (is_hovered && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
371 return ImVec2(std::floor((
double)pos.x / scale) * scale,
372 std::floor((
double)pos.y / scale) * scale);
377 const ImGuiIO &io = GetIO();
379 const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
380 static ImVec2 drag_start_pos;
381 const float scaled_size = tile_size * scale;
382 static bool dragging =
false;
383 constexpr int small_map_size = 0x200;
384 int superY = current_map / 8;
385 int superX = current_map % 8;
388 if (IsMouseClicked(ImGuiMouseButton_Right)) {
389 ImVec2 painter_pos = AlignPosToGrid(mouse_pos, scaled_size);
390 int painter_x = painter_pos.x;
391 int painter_y = painter_pos.y;
393 auto tile16_x = (painter_x % small_map_size) / (small_map_size / 0x20);
394 auto tile16_y = (painter_y % small_map_size) / (small_map_size / 0x20);
396 int index_x = superX * 0x20 + tile16_x;
397 int index_y = superY * 0x20 + tile16_y;
403 drag_start_pos = AlignPosToGrid(mouse_pos, scaled_size);
407 ImVec2 drag_end_pos = AlignPosToGrid(mouse_pos, scaled_size);
408 if (ImGui::IsMouseDragging(ImGuiMouseButton_Right)) {
409 auto start = ImVec2(
canvas_p0_.x + drag_start_pos.x,
411 auto end = ImVec2(
canvas_p0_.x + drag_end_pos.x + tile_size,
419 if (!ImGui::IsMouseDown(ImGuiMouseButton_Right)) {
423 constexpr int tile16_size = 16;
424 int start_x = std::floor(drag_start_pos.x / scaled_size) * tile16_size;
425 int start_y = std::floor(drag_start_pos.y / scaled_size) * tile16_size;
426 int end_x = std::floor(drag_end_pos.x / scaled_size) * tile16_size;
427 int end_y = std::floor(drag_end_pos.y / scaled_size) * tile16_size;
430 if (start_x > end_x) std::swap(start_x, end_x);
431 if (start_y > end_y) std::swap(start_y, end_y);
435 constexpr int tiles_per_local_map = small_map_size / 16;
438 for (
int y = start_y; y <= end_y; y += tile16_size) {
439 for (
int x = start_x; x <= end_x; x += tile16_size) {
441 int local_map_x = x / small_map_size;
442 int local_map_y = y / small_map_size;
445 int tile16_x = (x % small_map_size) / tile16_size;
446 int tile16_y = (y % small_map_size) / tile16_size;
449 int index_x = local_map_x * tiles_per_local_map + tile16_x;
450 int index_y = local_map_y * tiles_per_local_map + tile16_y;
486 float scale,
int alpha) {
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);
528 IM_COL32(color.x, color.y, color.z, color.w));
540 std::vector<gfx::Bitmap> &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 = ImGui::GetID((
void *)(intptr_t)canvas_id);
809 ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(),
true,
810 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
811 canvas.
DrawBackground(ImVec2(width + 1, num_sheets_to_load * height + 1));
814 for (
const auto &[key, value] : graphics_bin) {
815 int offset = height * (key + 1);
818 top_left_y = canvas.
zero_point().y + height * key;
821 (
void *)value.texture(),
822 ImVec2(canvas.
zero_point().x + 2, top_left_y),
835 int width,
int height,
int tile_size,
bool is_loaded,
836 bool scrollbar,
int canvas_id) {
838 int width,
int height,
int tile_size,
bool is_loaded) {
848 if (ImGuiID child_id = ImGui::GetID((
void *)(intptr_t)canvas_id);
849 ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(),
true,
850 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
851 draw_canvas(canvas, bitmap, width, height, tile_size, is_loaded);
855 draw_canvas(canvas, bitmap, width, height, tile_size, is_loaded);
The Renderer class represents the renderer for the Yaze application.
static Renderer & GetInstance()
void UpdateBitmap(gfx::Bitmap *bitmap, bool use_sdl_update=false)
Used to update a bitmap on the screen.
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)