Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for some reason when i try to change the color of my text and try to do pop style color it crashed? #7597

Closed
UniqueNotDB opened this issue May 17, 2024 · 3 comments

Comments

@UniqueNotDB
Copy link

Version/Branch of Dear ImGui:

Version 1.XX, Branch: XXX (master/docking/etc.)

Back-ends:

imgui_impl_XXX.cpp + imgui_impl_XXX.cpp

Compiler, OS:

Windows 11, Visual Studio

Full config/build information:

No response

Details:

i tried everything nothings working

Screenshots/Video:

Recording.2024-05-17.113319.mp4

Minimal, Complete and Verifiable Example code:

if (page == 1) { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(127, 0, 255, 255)); }
if (ImGui::Button("\n " ICON_FA_CROSSHAIRS " \n", ImVec2(50, 50))) { page = 1; }
if (page == 1) { ImGui::PopStyleColor(); }
ImGui::SetCursorPosX(50);
if (page == 2) { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(127, 0, 255, 255)); }
if (ImGui::Button("\n " ICON_FA_EYE " \n", ImVec2(50, 50))) { page = 2; }
if (page == 2) { ImGui::PopStyleColor(); }
ImGui::SetCursorPosX(50);
if (page == 3) { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(127, 0, 255, 255)); }
if (ImGui::Button("\n " ICON_FA_CLOUD " \n", ImVec2(50, 50))) { page = 3;  }
if (page == 3) { ImGui::PopStyleColor(); }
ImGui::SetCursorPosX(50);
if (page == 4) { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(127, 0, 255, 255)); }
if (ImGui::Button("\n " ICON_FA_COG " \n", ImVec2(50, 50))) { page = 4; }
if (page == 4) { ImGui::PopStyleColor(); }
@ocornut
Copy link
Owner

ocornut commented May 17, 2024

if (page == 4) { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(127, 0, 255, 255)); }
if (ImGui::Button("\n " ICON_FA_COG " \n", ImVec2(50, 50))) { page = 4; }
if (page == 4) { ImGui::PopStyleColor(); }

Look carefully at the code and you will understand your mistake: when the Button() is pressed you are changing the value of page, creating an assymetry in the Push/Pop logic.

You could do:

int next_page = page;
if (page == 1) { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(127, 0, 255, 255)); }
if (ImGui::Button("\n " ICON_FA_CROSSHAIRS " \n", ImVec2(50, 50))) { next_page = 1; }
if (page == 1) { ImGui::PopStyleColor(); }
ImGui::SetCursorPosX(50);
if (page == 2) { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(127, 0, 255, 255)); }
if (ImGui::Button("\n " ICON_FA_EYE " \n", ImVec2(50, 50))) { next_page = 2; }
if (page == 2) { ImGui::PopStyleColor(); }
ImGui::SetCursorPosX(50);
if (page == 3) { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(127, 0, 255, 255)); }
if (ImGui::Button("\n " ICON_FA_CLOUD " \n", ImVec2(50, 50))) { next_page = 3;  }
if (page == 3) { ImGui::PopStyleColor(); }
ImGui::SetCursorPosX(50);
if (page == 4) { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(127, 0, 255, 255)); }
if (ImGui::Button("\n " ICON_FA_COG " \n", ImVec2(50, 50))) { next_page= 4; }
if (page == 4) { ImGui::PopStyleColor(); }
page = next_page;

Or:

if (page == 1) { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(127, 0, 255, 255)); }
if (ImGui::Button("\n " ICON_FA_CROSSHAIRS " \n", ImVec2(50, 50))) { next_page = 1; }
if (page == 1) { ImGui::PopStyleColor(); }
ImGui::SetCursorPosX(50);
if (page == 2) { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(127, 0, 255, 255)); }
if (ImGui::Button("\n " ICON_FA_EYE " \n", ImVec2(50, 50))) { next_page = 2; }
if (page == 2) { ImGui::PopStyleColor(); }
ImGui::SetCursorPosX(50);
if (page == 3) { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(127, 0, 255, 255)); }
if (ImGui::Button("\n " ICON_FA_CLOUD " \n", ImVec2(50, 50))) { next_page = 3;  }
if (page == 3) { ImGui::PopStyleColor(); }
ImGui::SetCursorPosX(50);
if (page == 4) { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(127, 0, 255, 255)); }
if (ImGui::Button("\n " ICON_FA_COG " \n", ImVec2(50, 50))) { next_page= 4; }
if (page == 4) { ImGui::PopStyleColor(); }

You might also want to create yourself a helper function:

// in your own header+source file
bool ImGui::ButtonColored(const ImVec4& col, const char* label)
{
    PushStyleColor(ImGuiCol_Text, col);
    bool ret = Button(label);
    PopStyleColor();
    return ret;
}

And use it:

ImGui::Indent(50.0f);
ImVec4 base_color = style.Colors[ImGuiCol_Text];
ImVec4 selected_color = ImVec4(127, 0, 255, 255);
ImVec2 button_sz(50,50);
if (ImGui::ButtonColored(page == 1 ? selected_color  : base_color, "\n " ICON_FA_CROSSHAIRS " \n", button_sz)) { page = 1; }
if (ImGui::ButtonColored(page == 2 ? selected_color  : base_color, "\n " ICON_FA_EYE " \n", button_sz)) { page = 2; }
[....]
ImGui::Unindent(50.0f);

@ocornut ocornut closed this as completed May 17, 2024
@UniqueNotDB
Copy link
Author

bro thank u so much lol im new to this

@UniqueNotDB
Copy link
Author

it fixed it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants