From 66b5ff78824efb2be4679993962d77c31abc2589 Mon Sep 17 00:00:00 2001 From: Panos Karabelas Date: Thu, 31 Oct 2024 11:34:34 +0000 Subject: [PATCH] [editor] fixed an imgui error related to the material image slot button --- editor/ImGui/ImGuiExtension.h | 37 ++++++++++++----------------------- editor/Widgets/Console.cpp | 2 +- editor/Widgets/Properties.cpp | 8 ++++---- editor/Widgets/TitleBar.cpp | 8 ++++---- 4 files changed, 22 insertions(+), 33 deletions(-) diff --git a/editor/ImGui/ImGuiExtension.h b/editor/ImGui/ImGuiExtension.h index 3ab988bd0..f1b7e5167 100644 --- a/editor/ImGui/ImGuiExtension.h +++ b/editor/ImGui/ImGuiExtension.h @@ -135,7 +135,7 @@ namespace ImGuiSp return ImGui::Button(label); } - static bool image_button(uint64_t id, Spartan::RHI_Texture* texture, const IconType icon, const Spartan::Math::Vector2& size, bool border, ImVec4 tint = {1,1,1,1}) + static bool image_button(Spartan::RHI_Texture* texture, const IconType icon, const Spartan::Math::Vector2& size, bool border, ImVec4 tint = {1,1,1,1}) { if (!border) { @@ -148,13 +148,10 @@ namespace ImGuiSp texture = IconLoader::GetTextureByType(icon); } - // Compute ID - id += static_cast(icon); - id += texture ? reinterpret_cast(&texture) : 0; - + ImGui::PushID(static_cast(ImGui::GetCursorPosX() + ImGui::GetCursorPosY())); bool result = ImGui::ImageButton ( - std::to_string(id).c_str(), // str_id + "", // str_id reinterpret_cast(texture), // user_texture_id size, // size ImVec2(0, 0), // uv0 @@ -162,6 +159,7 @@ namespace ImGuiSp ImColor(0, 0, 0, 0), // bg_col tint // tint_col ); + ImGui::PopID(); if (!border) { @@ -272,7 +270,7 @@ namespace ImGuiSp return nullptr; } - // Image slot + // image slot static void image_slot(Spartan::RHI_Texture* texture_in, const std::function& setter) { const ImVec2 slot_size = ImVec2(80 * Spartan::Window::GetDpiScale()); @@ -285,35 +283,26 @@ namespace ImGuiSp const ImVec2 pos_image = ImGui::GetCursorPos(); const ImVec2 pos_button = ImVec2(ImGui::GetCursorPosX() + slot_size.x - button_size * 2.0f + 6.0f, ImGui::GetCursorPosY() + 1.0f); - uint32_t id = static_cast(pos_button.x + pos_button.y); - - // Remove button - if (texture != nullptr) - { - ImGui::SetCursorPos(pos_button); - if (image_button(id, nullptr, IconType::Component_Material_RemoveTexture, button_size, true)) - { - texture = nullptr; - setter(nullptr); - } - } - - // Image + // image ImVec4 colro_tint = (texture != nullptr) ? ImVec4(1, 1, 1, 1) : ImVec4(0, 0, 0, 0); ImVec4 color_border = ImVec4(1, 1, 1, 0.5f); ImGui::SetCursorPos(pos_image); image(texture, slot_size, colro_tint, color_border); - // Remove button - Does nothing, drawn again just to be visible + // x (remove) button if (texture != nullptr) { ImGui::SetCursorPos(pos_button); - image_button(id, nullptr, IconType::Component_Material_RemoveTexture, button_size, true); + if (image_button(nullptr, IconType::Component_Material_RemoveTexture, button_size, true)) + { + texture = nullptr; + setter(nullptr); + } } } ImGui::EndGroup(); - // Drop target + // drop target if (auto payload = receive_drag_drop_payload(DragPayloadType::Texture)) { try diff --git a/editor/Widgets/Console.cpp b/editor/Widgets/Console.cpp index 040c6d59b..8d444f1a2 100644 --- a/editor/Widgets/Console.cpp +++ b/editor/Widgets/Console.cpp @@ -70,7 +70,7 @@ void Console::OnTickVisible() { bool& visibility = m_log_type_visibility[index]; ImGui::PushStyleColor(ImGuiCol_Button, visibility ? ImGui::GetStyle().Colors[ImGuiCol_Button] : ImGui::GetStyle().Colors[ImGuiCol_FrameBg]); - if (ImGuiSp::image_button(0, nullptr, IconType::Console, 15.0f * Spartan::Window::GetDpiScale(), false,m_log_type_color[index])) + if (ImGuiSp::image_button(nullptr, IconType::Console, 15.0f * Spartan::Window::GetDpiScale(), false, m_log_type_color[index])) { visibility = !visibility; } diff --git a/editor/Widgets/Properties.cpp b/editor/Widgets/Properties.cpp index fcd2813c4..a894c11b9 100644 --- a/editor/Widgets/Properties.cpp +++ b/editor/Widgets/Properties.cpp @@ -112,10 +112,9 @@ namespace ImGui::SetCursorPosY(original_pen_y + 5.0f); ImGuiSp::image(icon_enum, 15,ImGui::Style::color_accent_1); ImGui::SameLine(ImGui::GetContentRegionAvail().x - icon_width + 1.0f); ImGui::SetCursorPosY(original_pen_y); - uint32_t id = static_cast(ImGui::GetCursorPosX() + ImGui::GetCursorPosY()); ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(1.0f, 1.0f, 1.0f, 0.0f)); - if (ImGuiSp::image_button(id, nullptr, IconType::Component_Options, icon_width, false)) + if (ImGuiSp::image_button(nullptr, IconType::Component_Options, icon_width, false)) { context_menu_id = name; ImGui::OpenPopup(context_menu_id.c_str()); @@ -762,13 +761,14 @@ void Properties::ShowMaterial(Material* material) const max = 2.4f; // diamond } - ImGuiSp::draw_float_wrap("##material_property_slider", &value, 0.004f, min, max); + // this custom slider already has a unique id + ImGuiSp::draw_float_wrap("", &value, 0.004f, min, max); } else { bool is_metallic = value != 0.0f; ImGui::PushID(static_cast(ImGui::GetCursorPosX() + ImGui::GetCursorPosY())); - ImGui::Checkbox("##metalness", &is_metallic); + ImGui::Checkbox("", &is_metallic); ImGui::PopID(); value = is_metallic ? 1.0f : 0.0f; } diff --git a/editor/Widgets/TitleBar.cpp b/editor/Widgets/TitleBar.cpp index 77a11f0aa..970026896 100644 --- a/editor/Widgets/TitleBar.cpp +++ b/editor/Widgets/TitleBar.cpp @@ -92,7 +92,7 @@ namespace ImGui::SetCursorPosY(offset_y); - if (ImGuiSp::image_button(static_cast(icon_type), nullptr, icon_type, button_size * Spartan::Window::GetDpiScale(), false)) + if (ImGuiSp::image_button(nullptr, icon_type, button_size * Spartan::Window::GetDpiScale(), false)) { on_press(); } @@ -185,17 +185,17 @@ namespace ImGui::SetCursorPosX(size_avail_x - offset_right); Spartan::Math::Vector2 icon_size = Spartan::Math::Vector2(24.0f, 24.0f); - if (ImGuiSp::image_button(0, nullptr, IconType::Window_Minimize, icon_size, false)) + if (ImGuiSp::image_button(nullptr, IconType::Window_Minimize, icon_size, false)) { Spartan::Window::Minimize(); } - if (ImGuiSp::image_button(1, nullptr, IconType::Window_Maximize, icon_size, false)) + if (ImGuiSp::image_button(nullptr, IconType::Window_Maximize, icon_size, false)) { Spartan::Window::Maximize(); } - if (ImGuiSp::image_button(2, nullptr, IconType::Window_Close, icon_size, false)) + if (ImGuiSp::image_button(nullptr, IconType::Window_Close, icon_size, false)) { Spartan::Window::Close(); }