Skip to content

Commit

Permalink
[editor] fixed an imgui error related to the material image slot button
Browse files Browse the repository at this point in the history
  • Loading branch information
PanosK92 committed Oct 31, 2024
1 parent 84442b0 commit 66b5ff7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 33 deletions.
37 changes: 13 additions & 24 deletions editor/ImGui/ImGuiExtension.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -148,20 +148,18 @@ namespace ImGuiSp
texture = IconLoader::GetTextureByType(icon);
}

// Compute ID
id += static_cast<uint64_t>(icon);
id += texture ? reinterpret_cast<uint64_t>(&texture) : 0;

ImGui::PushID(static_cast<int>(ImGui::GetCursorPosX() + ImGui::GetCursorPosY()));
bool result = ImGui::ImageButton
(
std::to_string(id).c_str(), // str_id
"", // str_id
reinterpret_cast<ImTextureID>(texture), // user_texture_id
size, // size
ImVec2(0, 0), // uv0
ImVec2(1, 1), // uv1
ImColor(0, 0, 0, 0), // bg_col
tint // tint_col
);
ImGui::PopID();

if (!border)
{
Expand Down Expand Up @@ -272,7 +270,7 @@ namespace ImGuiSp
return nullptr;
}

// Image slot
// image slot
static void image_slot(Spartan::RHI_Texture* texture_in, const std::function<void(Spartan::RHI_Texture*)>& setter)
{
const ImVec2 slot_size = ImVec2(80 * Spartan::Window::GetDpiScale());
Expand All @@ -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<uint32_t>(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
Expand Down
2 changes: 1 addition & 1 deletion editor/Widgets/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions editor/Widgets/Properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(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());
Expand Down Expand Up @@ -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<int>(ImGui::GetCursorPosX() + ImGui::GetCursorPosY()));
ImGui::Checkbox("##metalness", &is_metallic);
ImGui::Checkbox("", &is_metallic);
ImGui::PopID();
value = is_metallic ? 1.0f : 0.0f;
}
Expand Down
8 changes: 4 additions & 4 deletions editor/Widgets/TitleBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace

ImGui::SetCursorPosY(offset_y);

if (ImGuiSp::image_button(static_cast<uint64_t>(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();
}
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 66b5ff7

Please sign in to comment.