diff --git a/UI/ControlMappingScreen.cpp b/UI/ControlMappingScreen.cpp index 913e17f8df..8a0d4583c1 100644 --- a/UI/ControlMappingScreen.cpp +++ b/UI/ControlMappingScreen.cpp @@ -200,7 +200,7 @@ UI::EventReturn ControlMapper::OnAdd(UI::EventParams ¶ms) { UI::EventReturn ControlMapper::OnAddMouse(UI::EventParams ¶ms) { action_ = ADD; g_Config.bMapMouse = true; - scrm_->push(new KeyMappingNewKeyDialog(pspKey_, true, std::bind(&ControlMapper::MappedCallback, this, std::placeholders::_1))); + scrm_->push(new KeyMappingNewMouseKeyDialog(pspKey_, true, std::bind(&ControlMapper::MappedCallback, this, std::placeholders::_1))); return UI::EVENT_DONE; } @@ -318,7 +318,7 @@ bool KeyMappingNewKeyDialog::key(const KeyInput &key) { if (mapped_) return false; if (key.flags & KEY_DOWN) { - if (key.keyCode == NKCODE_EXT_MOUSEBUTTON_1 && !g_Config.bMapMouse) { + if (key.keyCode == NKCODE_EXT_MOUSEBUTTON_1) { return true; } @@ -331,6 +331,34 @@ bool KeyMappingNewKeyDialog::key(const KeyInput &key) { return true; } +void KeyMappingNewMouseKeyDialog::CreatePopupContents(UI::ViewGroup *parent) { + using namespace UI; + + I18NCategory *km = GetI18NCategory("KeyMapping"); + + parent->Add(new TextView(std::string(km->T("You can press ESC to cancel.")), new LinearLayoutParams(Margins(10, 0)))); +} + +bool KeyMappingNewMouseKeyDialog::key(const KeyInput &key) { + if (mapped_) + return false; + if (key.flags & KEY_DOWN) { + if (key.keyCode == NKCODE_ESCAPE) { + TriggerFinish(DR_OK); + g_Config.bMapMouse = false; + return false; + } + + mapped_ = true; + KeyDef kdf(key.deviceId, key.keyCode); + TriggerFinish(DR_OK); + g_Config.bMapMouse = false; + if (callback_) + callback_(kdf); + } + return true; +} + static bool IgnoreAxisForMapping(int axis) { switch (axis) { // Ignore the accelerometer for mapping for now. @@ -376,6 +404,30 @@ bool KeyMappingNewKeyDialog::axis(const AxisInput &axis) { return true; } +bool KeyMappingNewMouseKeyDialog::axis(const AxisInput &axis) { + if (mapped_) + return false; + if (IgnoreAxisForMapping(axis.axisId)) + return false; + + if (axis.value > AXIS_BIND_THRESHOLD) { + mapped_ = true; + KeyDef kdf(axis.deviceId, KeyMap::TranslateKeyCodeFromAxis(axis.axisId, 1)); + TriggerFinish(DR_OK); + if (callback_) + callback_(kdf); + } + + if (axis.value < -AXIS_BIND_THRESHOLD) { + mapped_ = true; + KeyDef kdf(axis.deviceId, KeyMap::TranslateKeyCodeFromAxis(axis.axisId, -1)); + TriggerFinish(DR_OK); + if (callback_) + callback_(kdf); + } + return true; +} + class JoystickHistoryView : public UI::InertView { public: JoystickHistoryView(int xAxis, int xDevice, int xDir, int yAxis, int yDevice, int yDir, UI::LayoutParams *layoutParams = nullptr) diff --git a/UI/ControlMappingScreen.h b/UI/ControlMappingScreen.h index 40244206ae..9456ff925e 100644 --- a/UI/ControlMappingScreen.h +++ b/UI/ControlMappingScreen.h @@ -70,6 +70,29 @@ private: bool mapped_; // Prevent double registrations }; +class KeyMappingNewMouseKeyDialog : public PopupScreen { +public: + explicit KeyMappingNewMouseKeyDialog(int btn, bool replace, std::function callback) + : PopupScreen("Map Mouse", "", ""), callback_(callback), mapped_(false) { + pspBtn_ = btn; + } + + virtual bool key(const KeyInput &key) override; + virtual bool axis(const AxisInput &axis) override; + +protected: + void CreatePopupContents(UI::ViewGroup *parent) override; + + virtual bool FillVertical() const override { return false; } + virtual bool ShowButtons() const override { return true; } + virtual void OnCompleted(DialogResult result) override {} + +private: + int pspBtn_; + std::function callback_; + bool mapped_; // Prevent double registrations +}; + class AnalogTestScreen : public UIDialogScreenWithBackground { public: AnalogTestScreen() {}