Skip to content

Commit

Permalink
UI: Delay between successive mapping in controls.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownbrackets committed Oct 8, 2021
1 parent 309dcb2 commit 9c92af2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
18 changes: 12 additions & 6 deletions UI/ControlMappingScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "Common/StringUtils.h"
#include "Common/System/Display.h"
#include "Common/System/System.h"
#include "Common/TimeUtil.h"
#include "Core/KeyMap.h"
#include "Core/Host.h"
#include "Core/HLE/sceCtrl.h"
Expand Down Expand Up @@ -326,7 +327,7 @@ void KeyMappingNewKeyDialog::CreatePopupContents(UI::ViewGroup *parent) {
}

bool KeyMappingNewKeyDialog::key(const KeyInput &key) {
if (mapped_)
if (mapped_ || time_now_d() < delayUntil_)
return false;
if (key.flags & KEY_DOWN) {
if (key.keyCode == NKCODE_EXT_MOUSEBUTTON_1) {
Expand All @@ -342,6 +343,10 @@ bool KeyMappingNewKeyDialog::key(const KeyInput &key) {
return true;
}

void KeyMappingNewKeyDialog::SetDelay(float t) {
delayUntil_ = time_now_d() + t;
}

void KeyMappingNewMouseKeyDialog::CreatePopupContents(UI::ViewGroup *parent) {
using namespace UI;

Expand Down Expand Up @@ -392,7 +397,7 @@ static bool IgnoreAxisForMapping(int axis) {


bool KeyMappingNewKeyDialog::axis(const AxisInput &axis) {
if (mapped_)
if (mapped_ || time_now_d() < delayUntil_)
return false;
if (IgnoreAxisForMapping(axis.axisId))
return false;
Expand Down Expand Up @@ -1144,14 +1149,14 @@ void VisualMappingScreen::resized() {

UI::EventReturn VisualMappingScreen::OnMapButton(UI::EventParams &e) {
nextKey_ = e.a;
MapNext();
MapNext(false);
return UI::EVENT_DONE;
}

UI::EventReturn VisualMappingScreen::OnBindAll(UI::EventParams &e) {
bindAll_ = 0;
nextKey_ = bindAllOrder[bindAll_];
MapNext();
MapNext(false);
return UI::EVENT_DONE;
}

Expand Down Expand Up @@ -1184,7 +1189,7 @@ void VisualMappingScreen::HandleKeyMapping(KeyDef key) {

void VisualMappingScreen::dialogFinished(const Screen *dialog, DialogResult result) {
if (result == DR_YES && nextKey_ != 0) {
MapNext();
MapNext(true);
} else {
// This means they canceled.
if (nextKey_ != 0)
Expand All @@ -1195,7 +1200,7 @@ void VisualMappingScreen::dialogFinished(const Screen *dialog, DialogResult resu
}
}

void VisualMappingScreen::MapNext() {
void VisualMappingScreen::MapNext(bool successive) {
auto km = GetI18NCategory("KeyMapping");

if (nextKey_ == VIRTKEY_AXIS_Y_MIN || nextKey_ == VIRTKEY_AXIS_X_MIN || nextKey_ == VIRTKEY_AXIS_X_MAX) {
Expand All @@ -1207,5 +1212,6 @@ void VisualMappingScreen::MapNext() {

Bounds bounds = screenManager()->getUIContext()->GetLayoutBounds();
dialog->SetPopupOffset(psp_->GetPopupOffset() * bounds.h);
dialog->SetDelay(successive ? 0.5f : 0.1f);
screenManager()->push(dialog);
}
9 changes: 6 additions & 3 deletions UI/ControlMappingScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ class ControlMappingScreen : public UIDialogScreenWithBackground {
class KeyMappingNewKeyDialog : public PopupScreen {
public:
explicit KeyMappingNewKeyDialog(int btn, bool replace, std::function<void(KeyDef)> callback, std::shared_ptr<I18NCategory> i18n)
: PopupScreen(i18n->T("Map Key"), "Cancel", ""), callback_(callback), mapped_(false) {
: PopupScreen(i18n->T("Map Key"), "Cancel", ""), callback_(callback) {
pspBtn_ = btn;
}

virtual bool key(const KeyInput &key) override;
virtual bool axis(const AxisInput &axis) override;

void SetDelay(float t);

protected:
void CreatePopupContents(UI::ViewGroup *parent) override;

Expand All @@ -74,7 +76,8 @@ class KeyMappingNewKeyDialog : public PopupScreen {
private:
int pspBtn_;
std::function<void(KeyDef)> callback_;
bool mapped_; // Prevent double registrations
bool mapped_ = false; // Prevent double registrations
double delayUntil_ = 0.0f;
};

class KeyMappingNewMouseKeyDialog : public PopupScreen {
Expand Down Expand Up @@ -178,7 +181,7 @@ class VisualMappingScreen : public UIDialogScreenWithBackground {
UI::EventReturn OnMapButton(UI::EventParams &e);
UI::EventReturn OnBindAll(UI::EventParams &e);
void HandleKeyMapping(KeyDef key);
void MapNext();
void MapNext(bool successive);

MockPSP *psp_ = nullptr;
int nextKey_ = 0;
Expand Down

0 comments on commit 9c92af2

Please sign in to comment.