Skip to content

How to correctly use IDs ? #719

@victorlevasseur

Description

@victorlevasseur

Hi,

Here is my code :

ImGui::SetNextWindowPosCenter();
    ImGui::SetNextWindowSize(ImVec2(350, 200));
    ImGui::Begin("YAPG - Main menu");
        //Level selection
        static char levelName[512] = "level.xml";
        ImGui::InputText("", levelName, 512);
        ImGui::SameLine();
        if(ImGui::Button("Play !"))
        {
            getStateEngine().pauseAndStartState
                <level::LevelState, std::string, resources::AllResourcesManagers&, settings::SettingsManager&, sfg::SFGUI&, sfg::Desktop&>(
                std::string(levelName), m_resourcesManager, m_settingsManager, m_sfgui, m_desktop
            );
        }

        //Level editor
        if(ImGui::Button("Level editor"))
        {
            getStateEngine().pauseAndStartState
                <editor::LevelEditorState, resources::AllResourcesManagers&, settings::SettingsManager&, sfg::SFGUI&, sfg::Desktop&>(
                m_resourcesManager, m_settingsManager, m_sfgui, m_desktop
            );
        }

        //Settings
        if(ImGui::Button("Settings..."))
        {
            ImGui::OpenPopup("Settings");
        }

        //Settings popup
        ImGui::SetNextWindowSize(ImVec2(350, 400));
        if(ImGui::BeginPopupModal("Settings"))
        {
            if(ImGui::CollapsingHeader("Keyboard settings"))
            {
                ImGui::Indent();
                for(std::size_t i = 0; i < 4; ++i)
                {
                    std::string label = "Player " + std::to_string(i + 1);
                    if(ImGui::CollapsingHeader(label.data()))
                    {
                        ImGui::Text("Left key: ");
                        ImGui::SameLine();
                        if(ImGui::Button(m_playersKeys[i][0]))
                        {
                            m_selectedKeyButton = m_playersKeys[i][0];
                        }

                        ImGui::Text("Right key: ");
                        ImGui::SameLine();
                        if(ImGui::Button(m_playersKeys[i][1]))
                        {
                            m_selectedKeyButton = m_playersKeys[i][1];
                        }

                        ImGui::Text("Jump key: ");
                        ImGui::SameLine();
                        if(ImGui::Button(m_playersKeys[i][2]))
                        {
                            m_selectedKeyButton = m_playersKeys[i][2];
                        }
                    }
                }
                ImGui::Unindent();
            }

            if(ImGui::Button("Close"))
            {
                ImGui::CloseCurrentPopup();
            }
            ImGui::EndPopup();
        }

        //About
        if(ImGui::Button("About..."))
        {

        }

        //Quit button
        if(ImGui::Button("Exit"))
        {
            getStateEngine().stopStateAndUnpause();
        }
    ImGui::End();

As you can see, I have a "for" loop, so I probably need to use PushID and PopID. However, I don't know where to put them. I tried all the possible combinations but it always resulted in buttons randomly acting like others (for exemple : the jump key button of the third player launch the level editor as the "Editor" button only should do !).

So, how do the IDs work ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions