Skip to content

Commit

Permalink
block mouse mapping when mouse outside popup
Browse files Browse the repository at this point in the history
  • Loading branch information
lightmanLP committed Dec 24, 2024
1 parent a272e1a commit 3c94495
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 16 deletions.
14 changes: 14 additions & 0 deletions src/controller/controldeck/ControlDeck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#endif
#include <imgui.h>
#include "controller/deviceindex/ShipDeviceIndexMappingManager.h"
#include "window/gui/BaseInputEditor.h"

namespace Ship {

Expand Down Expand Up @@ -94,6 +95,19 @@ bool ControlDeck::MouseGameInputBlocked() {
(window->ID != Context::GetInstance()->GetWindow()->GetGui()->GetMainGameWindowID());
}

bool ControlDeck::MouseMappingInputBlocked() {
static std::shared_ptr<BaseInputEditor> inputEditor = nullptr;
if (inputEditor == nullptr) {
inputEditor = std::dynamic_pointer_cast<BaseInputEditor>(
Ship::Context::GetInstance()->GetWindow()->GetGui()->GetGuiWindow("Custom Input Editor"));
}
if (inputEditor == nullptr) {
inputEditor = std::dynamic_pointer_cast<BaseInputEditor>(
Ship::Context::GetInstance()->GetWindow()->GetGui()->GetGuiWindow("Input Editor"));
}
return ImGui::IsAnyItemHovered() || inputEditor->IsMouseMappingBlocked();
}

std::shared_ptr<Controller> ControlDeck::GetControllerByPort(uint8_t port) {
return mPorts[port]->GetConnectedController();
}
Expand Down
1 change: 1 addition & 0 deletions src/controller/controldeck/ControlDeck.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ControlDeck {
bool GamepadGameInputBlocked();
bool KeyboardGameInputBlocked();
bool MouseGameInputBlocked();
bool MouseMappingInputBlocked();
void SetSinglePlayerMappingMode(bool singlePlayer);
bool IsSinglePlayerMappingMode();
bool ProcessKeyboardEvent(KbEventType eventType, KbScancode scancode);
Expand Down
6 changes: 3 additions & 3 deletions src/controller/controldevice/controller/ControllerButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ bool ControllerButton::AddOrEditButtonMappingFromRawPress(CONTROLLERBUTTONS_T bi
mUseInputToCreateNewMapping = true;
if (mKeyboardScancodeForNewMapping != LUS_KB_UNKNOWN) {
mapping = std::make_shared<KeyboardKeyToButtonMapping>(mPortIndex, bitmask, mKeyboardScancodeForNewMapping);
} else if (!Context::GetInstance()->GetWindow()->GetGui()->IsMouseOverAnyGuiItem() &&
mMouseButtonForNewMapping != LUS_MOUSE_BTN_UNKNOWN) {
mapping = std::make_shared<MouseButtonToButtonMapping>(mPortIndex, bitmask, mMouseButtonForNewMapping);
} else if (!Context::GetInstance()->GetControlDeck()->MouseMappingInputBlocked() &&
mMouseButtonForNewMapping != MouseBtn::MOUSE_BTN_UNKNOWN) {
mapping = std::make_shared<MouseKeyToButtonMapping>(mPortIndex, bitmask, mMouseButtonForNewMapping);
} else {
mapping = ButtonMappingFactory::CreateButtonMappingFromSDLInput(mPortIndex, bitmask);
}
Expand Down
9 changes: 4 additions & 5 deletions src/controller/controldevice/controller/ControllerStick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,10 @@ bool ControllerStick::AddOrEditAxisDirectionMappingFromRawPress(Direction direct
mUseInputToCreateNewMapping = true;
if (mKeyboardScancodeForNewMapping != LUS_KB_UNKNOWN) {
mapping = std::make_shared<KeyboardKeyToAxisDirectionMapping>(mPortIndex, mStickIndex, direction,
mKeyboardScancodeForNewMapping);
} else if (!Context::GetInstance()->GetWindow()->GetGui()->IsMouseOverAnyGuiItem() &&
mMouseButtonForNewMapping != LUS_MOUSE_BTN_UNKNOWN) {
mapping = std::make_shared<MouseButtonToAxisDirectionMapping>(mPortIndex, mStickIndex, direction,
mMouseButtonForNewMapping);
} else if (!Context::GetInstance()->GetControlDeck()->MouseMappingInputBlocked() &&
mMouseButtonForNewMapping != MouseBtn::MOUSE_BTN_UNKNOWN) {
mapping =
std::make_shared<MouseKeyToAxisDirectionMapping>(mPortIndex, mStick, direction, mMouseButtonForNewMapping);
} else {
mapping =
AxisDirectionMappingFactory::CreateAxisDirectionMappingFromSDLInput(mPortIndex, mStickIndex, direction);
Expand Down
8 changes: 8 additions & 0 deletions src/window/gui/BaseInputEditor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

namespace Ship {
class BaseInputEditor {
public:
virtual bool IsMouseMappingBlocked() = 0;
};
} // namespace Ship
13 changes: 7 additions & 6 deletions src/window/gui/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,12 +770,17 @@ void Gui::SaveConsoleVariablesNextFrame() {
}

void Gui::AddGuiWindow(std::shared_ptr<GuiWindow> guiWindow) {
return Gui::AddGuiWindow(guiWindow, guiWindow->GetName());
}

void Gui::AddGuiWindow(std::shared_ptr<GuiWindow> guiWindow, std::string name) {
if (mGuiWindows.contains(guiWindow->GetName())) {
SPDLOG_ERROR("ImGui::AddGuiWindow: Attempting to add duplicate window name {}", guiWindow->GetName());
SPDLOG_ERROR("ImGui::AddGuiWindow: Attempting to add duplicate window name {}", name);
if (mGuiWindows.contains(name)) {
return;
}

mGuiWindows[guiWindow->GetName()] = guiWindow;
mGuiWindows[name] = guiWindow;
guiWindow->Init();
}

Expand Down Expand Up @@ -990,10 +995,6 @@ bool Gui::GetMenuOrMenubarVisible() {
return (GetMenuBar() && GetMenuBar()->IsVisible()) || (GetMenu() && GetMenu()->IsVisible());
}

bool Gui::IsMouseOverAnyGuiItem() {
return ImGui::IsAnyItemHovered();
}

std::shared_ptr<GuiWindow> Gui::GetMenu() {
return mMenu;
}
Expand Down
2 changes: 1 addition & 1 deletion src/window/gui/Gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Gui {
ImGuiID GetMainGameWindowID();

void AddGuiWindow(std::shared_ptr<GuiWindow> guiWindow);
void AddGuiWindow(std::shared_ptr<GuiWindow> guiWindow, std::string name);
std::shared_ptr<GuiWindow> GetGuiWindow(const std::string& name);
void RemoveGuiWindow(std::shared_ptr<GuiWindow> guiWindow);
void RemoveGuiWindow(const std::string& name);
Expand All @@ -99,7 +100,6 @@ class Gui {
void SetMenu(std::shared_ptr<GuiWindow> menu);
std::shared_ptr<GuiWindow> GetMenu();
bool GetMenuOrMenubarVisible();
bool IsMouseOverAnyGuiItem();

bool GamepadNavigationEnabled();
void BlockGamepadNavigation();
Expand Down
24 changes: 24 additions & 0 deletions src/window/gui/InputEditorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void InputEditorWindow::InitElement() {
mRumbleTimer = INT32_MAX;
mRumbleMappingToTest = nullptr;
mInputEditorPopupOpen = false;
mCurrentMappingPopupId = 0;

mButtonsBitmasks = { BTN_A, BTN_B, BTN_START, BTN_L, BTN_R, BTN_Z, BTN_CUP, BTN_CDOWN, BTN_CLEFT, BTN_CRIGHT };
mDpadBitmasks = { BTN_DUP, BTN_DDOWN, BTN_DLEFT, BTN_DRIGHT };
Expand Down Expand Up @@ -217,6 +218,7 @@ void InputEditorWindow::DrawButtonLineAddMappingButton(uint8_t port, CONTROLLERB
if (ImGui::Button(StringHelper::Sprintf("%s###addButtonMappingButton%d-%d", ICON_FA_PLUS, port, bitmask).c_str(),
ImVec2(SCALE_IMGUI_SIZE(20.0f), 0.0f))) {
ImGui::OpenPopup(popupId.c_str());
mCurrentMappingPopupId = ImGui::GetID(popupId.c_str());
};
ImGui::PopStyleVar();

Expand Down Expand Up @@ -278,6 +280,7 @@ void InputEditorWindow::DrawButtonLineEditMappingButton(uint8_t port, CONTROLLER
.c_str(),
ImVec2(ImGui::CalcTextSize(physicalInputDisplayName.c_str()).x + SCALE_IMGUI_SIZE(12.0f), 0.0f))) {
ImGui::OpenPopup(popupId.c_str());
mCurrentMappingPopupId = ImGui::GetID(popupId.c_str());
}
if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayNormal | ImGuiHoveredFlags_NoSharedDelay)) {
ImGui::SetTooltip("%s", mapping->GetPhysicalDeviceName().c_str());
Expand Down Expand Up @@ -325,6 +328,7 @@ void InputEditorWindow::DrawButtonLineEditMappingButton(uint8_t port, CONTROLLER
if (ImGui::Button(StringHelper::Sprintf("%s###editAxisThresholdButton%s", ICON_FA_COG, id.c_str()).c_str(),
ImVec2(ImGui::CalcTextSize(ICON_FA_COG).x + SCALE_IMGUI_SIZE(10.0f), 0.0f))) {
ImGui::OpenPopup(popupId.c_str());
mCurrentMappingPopupId = ImGui::GetID(popupId.c_str());
}
if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayNormal | ImGuiHoveredFlags_NoSharedDelay)) {
ImGui::SetTooltip("Edit axis threshold");
Expand Down Expand Up @@ -460,6 +464,7 @@ void InputEditorWindow::DrawStickDirectionLineAddMappingButton(uint8_t port, uin
.c_str(),
ImVec2(SCALE_IMGUI_SIZE(20.0f), 0.0f))) {
ImGui::OpenPopup(popupId.c_str());
mCurrentMappingPopupId = ImGui::GetID(popupId.c_str());
};
ImGui::PopStyleVar();

Expand Down Expand Up @@ -546,6 +551,7 @@ void InputEditorWindow::DrawStickDirectionLineEditMappingButton(uint8_t port, ui
.c_str(),
ImVec2(ImGui::CalcTextSize(physicalInputDisplayName.c_str()).x + SCALE_IMGUI_SIZE(12.0f), 0.0f))) {
ImGui::OpenPopup(popupId.c_str());
mCurrentMappingPopupId = ImGui::GetID(popupId.c_str());
}
if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayNormal | ImGuiHoveredFlags_NoSharedDelay)) {
ImGui::SetTooltip("%s", mapping->GetPhysicalDeviceName().c_str());
Expand Down Expand Up @@ -822,6 +828,7 @@ void InputEditorWindow::DrawAddRumbleMappingButton(uint8_t port) {
if (ImGui::Button(StringHelper::Sprintf("%s###addRumbleMapping%d", ICON_FA_PLUS, port).c_str(),
ImVec2(SCALE_IMGUI_SIZE(20.0f), SCALE_IMGUI_SIZE(20.0f)))) {
ImGui::OpenPopup(popupId.c_str());
mCurrentMappingPopupId = ImGui::GetID(popupId.c_str());
}
ImGui::PopStyleVar();

Expand Down Expand Up @@ -985,6 +992,21 @@ void InputEditorWindow::DrawRumbleSection(uint8_t port) {
DrawAddRumbleMappingButton(port);
}

bool InputEditorWindow::IsMouseMappingBlocked() {
if (mCurrentMappingPopupId == 0) {
return true;
}
ImGuiContext* ctx = ImGui::GetCurrentContext();
if (ctx->HoveredWindow == NULL) {
return true;
}
ImGuiPopupData& popup = ctx->OpenPopupStack.back();
if (popup.Window == NULL) {
return true;
}
return !(ctx->HoveredWindow->ID == popup.Window->ID && popup.PopupId == mCurrentMappingPopupId);
}

void InputEditorWindow::DrawRemoveLEDMappingButton(uint8_t port, std::string id) {
ImGui::SameLine();
ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, ImVec2(1.0f, 0.5f));
Expand All @@ -1002,6 +1024,7 @@ void InputEditorWindow::DrawAddLEDMappingButton(uint8_t port) {
if (ImGui::Button(StringHelper::Sprintf("%s###addLEDMapping%d", ICON_FA_PLUS, port).c_str(),
ImVec2(SCALE_IMGUI_SIZE(20.0f), SCALE_IMGUI_SIZE(20.0f)))) {
ImGui::OpenPopup(popupId.c_str());
mCurrentMappingPopupId = ImGui::GetID(popupId.c_str());
}
ImGui::PopStyleVar();

Expand Down Expand Up @@ -1081,6 +1104,7 @@ void InputEditorWindow::DrawAddGyroMappingButton(uint8_t port) {
if (ImGui::Button(StringHelper::Sprintf("%s###addGyroMapping%d", ICON_FA_PLUS, port).c_str(),
ImVec2(SCALE_IMGUI_SIZE(20.0f), SCALE_IMGUI_SIZE(20.0f)))) {
ImGui::OpenPopup(popupId.c_str());
mCurrentMappingPopupId = ImGui::GetID(popupId.c_str());
}
ImGui::PopStyleVar();

Expand Down
5 changes: 4 additions & 1 deletion src/window/gui/InputEditorWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "stdint.h"
#include "window/gui/GuiWindow.h"
#include "window/gui/BaseInputEditor.h"
#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS
#endif
Expand All @@ -14,14 +15,15 @@

namespace Ship {

class InputEditorWindow : public GuiWindow {
class InputEditorWindow : public GuiWindow, public BaseInputEditor {
public:
using GuiWindow::GuiWindow;
~InputEditorWindow();

void DrawInputChip(const char* buttonName, ImVec4 color);
void DrawAnalogPreview(const char* label, ImVec2 stick, float deadzone = 0, bool gyro = false);
bool TestingRumble();
bool IsMouseMappingBlocked() override;

protected:
void InitElement() override;
Expand Down Expand Up @@ -76,6 +78,7 @@ class InputEditorWindow : public GuiWindow {
void DrawGyroDeviceIcons(uint8_t portIndex);
void DrawLEDDeviceIcons(uint8_t portIndex);
bool mInputEditorPopupOpen;
ImGuiID mCurrentMappingPopupId;
void DrawSetDefaultsButton(uint8_t portIndex);
void DrawClearAllButton(uint8_t portIndex);

Expand Down

0 comments on commit 3c94495

Please sign in to comment.