close popup with escape

This commit is contained in:
2024-03-09 18:09:31 +01:00
parent 510e850db2
commit 0708431730
2 changed files with 19 additions and 13 deletions

View File

@@ -17,7 +17,8 @@ class OptionsMenu : public GuiWidget {
private:
bool m_ShowFPS;
bool m_VSync;
bool m_IsOpen;
bool m_IsKeyPopupOpen;
bool m_KeyPopupShouldClose;
utils::Timer m_Timer{100};
KeyAction m_CurrentAction;

View File

@@ -32,7 +32,7 @@ static std::string GetImGuiKeyName(int key) {
void OptionsMenu::HotkeyBindingButton() {
for (int i = 0; i < m_Client->GetConfig()->GetKeys().size(); i++) {
if (ImGui::Button(utils::Format("%s##%i", GetKeyActionCodeName(KeyAction(i)).c_str(), i).c_str())) {
m_IsOpen = true;
m_IsKeyPopupOpen = true;
m_CurrentAction = KeyAction(i);
ImGui::OpenPopup("Changer de touche");
}
@@ -59,17 +59,18 @@ void OptionsMenu::HotkeyBindingPopUp() {
ImGui::SameLine();
ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "%s", GetKeyActionCodeName(m_CurrentAction).c_str());
//%s est : %s", GetActionName(m_CurrentAction).c_str(), GetKeyActionCodeName(m_CurrentAction).c_str());
if (!m_IsOpen && m_Timer.Update(ImGui::GetIO().DeltaTime * 1000))
if (m_KeyPopupShouldClose && m_Timer.Update(ImGui::GetIO().DeltaTime * 1000)) {
m_KeyPopupShouldClose = false;
m_IsKeyPopupOpen = false;
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
}
OptionsMenu::OptionsMenu(GuiWidget* parent, Client* client) : GuiWidget(parent, client) {
OptionsMenu::OptionsMenu(GuiWidget* parent, Client* client) :
GuiWidget(parent, client), m_KeyPopupShouldClose(false), m_IsKeyPopupOpen(false) {
AddWidget(std::make_unique<FPSMenu>(this, client));
InputManager::BindKeyDownCallback(std::bind(&OptionsMenu::OnKeyEvent, this, std::placeholders::_1));
@@ -81,14 +82,18 @@ OptionsMenu::OptionsMenu(GuiWidget* parent, Client* client) : GuiWidget(parent,
}
void OptionsMenu::OnKeyEvent(int key) {
if (!m_IsKeyPopupOpen)
return;
if (key == ImGuiKey_Escape) {
m_IsOpen = false;
m_KeyPopupShouldClose = true;
ImGui::GetIO().ClearInputKeys(); // releases the Escape key
return;
}
if (m_IsOpen) {
m_Client->GetConfig()->GetKeys()[static_cast<std::size_t>(m_CurrentAction)] = key;
m_IsOpen = false;
}
m_Client->GetConfig()->GetKeys()[static_cast<std::size_t>(m_CurrentAction)] = key;
m_KeyPopupShouldClose = true;
utils::LOG(std::to_string(key));
}
@@ -121,7 +126,7 @@ void OptionsMenu::Render() {
ImGui::NewLine();
if (ImGui::Button("Retour") || ImGui::IsKeyPressed(ImGuiKey_Escape)) {
if (ImGui::Button("Retour") || (ImGui::IsKeyPressed(ImGuiKey_Escape, false) && !m_IsKeyPopupOpen)) {
Disable();
if (m_Client->IsConnected()) {
InputManager::GrabMouse(true);