diff --git a/Core/Config.cpp b/Core/Config.cpp index 3b5607fd65..9d05f834a9 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -567,6 +567,7 @@ static ConfigSetting controlSettings[] = { ConfigSetting("GamepadOnlyFocused", &g_Config.bGamepadOnlyFocused, false, true, true), ConfigSetting("TouchButtonStyle", &g_Config.iTouchButtonStyle, 1, true, true), ConfigSetting("TouchButtonOpacity", &g_Config.iTouchButtonOpacity, 65, true, true), + ConfigSetting("TouchButtonHideSeconds", &g_Config.iTouchButtonHideSeconds, 0, true, true), ConfigSetting("AutoCenterTouchAnalog", &g_Config.bAutoCenterTouchAnalog, false, true, true), // -1.0f means uninitialized, set in GamepadEmu::CreatePadLayout(). diff --git a/Core/Config.h b/Core/Config.h index 74fe1a43f7..af03245310 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -242,8 +242,8 @@ public: bool bGamepadOnlyFocused; // Control Style int iTouchButtonStyle; - // Control Positions int iTouchButtonOpacity; + int iTouchButtonHideSeconds; // Floating analog stick (recenters on thumb on press). bool bAutoCenterTouchAnalog; diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index e3783bfb3f..fb3d5793df 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -427,8 +427,10 @@ void GameSettingsScreen::CreateViews() { CheckBox *disableDiags = controlsSettings->Add(new CheckBox(&g_Config.bDisableDpadDiagonals, co->T("Disable D-Pad diagonals (4-way touch)"))); disableDiags->SetEnabledPtr(&g_Config.bShowTouchControls); - View *opacity = controlsSettings->Add(new PopupSliderChoice(&g_Config.iTouchButtonOpacity, 0, 100, co->T("Button Opacity"), screenManager(),"%")); + View *opacity = controlsSettings->Add(new PopupSliderChoice(&g_Config.iTouchButtonOpacity, 0, 100, co->T("Button Opacity"), screenManager(), "%")); opacity->SetEnabledPtr(&g_Config.bShowTouchControls); + PopupSliderChoice *autoHide = controlsSettings->Add(new PopupSliderChoice(&g_Config.iTouchButtonHideSeconds, 0, 300, co->T("Auto-hide buttons after (0 = off)"), screenManager(), "seconds")); + autoHide->SetEnabledPtr(&g_Config.bShowTouchControls); static const char *touchControlStyles[] = {"Classic", "Thin borders"}; View *style = controlsSettings->Add(new PopupMultiChoice(&g_Config.iTouchButtonStyle, co->T("Button style"), touchControlStyles, 0, ARRAY_SIZE(touchControlStyles), co->GetName(), screenManager())); style->SetEnabledPtr(&g_Config.bShowTouchControls); diff --git a/UI/GamepadEmu.cpp b/UI/GamepadEmu.cpp index 0df97759ac..1cd63abf58 100644 --- a/UI/GamepadEmu.cpp +++ b/UI/GamepadEmu.cpp @@ -33,6 +33,34 @@ static u32 GetButtonColor() { return g_Config.iTouchButtonStyle == 1 ? 0xFFFFFF : 0xc0b080; } +int GamepadView::framesWithoutTouch_; + +void GamepadView::Touch(const TouchInput &input) { + framesWithoutTouch_ = 0; +} + +void GamepadView::Update(const InputState &input) { + ++framesWithoutTouch_; +} + +float GamepadView::GetButtonOpacity() { + int fadeAfterFrames = g_Config.iTouchButtonHideSeconds * 60; + int fadeTransitionFrames = std::min(fadeAfterFrames, 5 * 60); + float opacity = g_Config.iTouchButtonOpacity / 100.0f; + + float multiplier = 1.0f; + if (framesWithoutTouch_ > fadeAfterFrames && fadeAfterFrames != 0) { + if (framesWithoutTouch_ > fadeAfterFrames + fadeTransitionFrames) { + multiplier = 0.0f; + } else { + int framesIntoFade = framesWithoutTouch_ - fadeAfterFrames; + multiplier = 1.0f - (framesIntoFade / (float)fadeTransitionFrames); + } + } + + return opacity * multiplier; +} + void MultiTouchButton::GetContentDimensions(const UIContext &dc, float &w, float &h) const { const AtlasImage &image = dc.Draw()->GetAtlas()->images[bgImg_]; w = image.w * scale_; @@ -40,6 +68,7 @@ void MultiTouchButton::GetContentDimensions(const UIContext &dc, float &w, float } void MultiTouchButton::Touch(const TouchInput &input) { + GamepadView::Touch(input); if ((input.flags & TOUCH_DOWN) && bounds_.Contains(input.x, input.y)) { pointerDownMask_ |= 1 << input.id; } @@ -58,7 +87,7 @@ void MultiTouchButton::Touch(const TouchInput &input) { } void MultiTouchButton::Draw(UIContext &dc) { - float opacity = g_Config.iTouchButtonOpacity / 100.0f; + float opacity = GetButtonOpacity(); float scale = scale_; if (IsDown()) { @@ -132,7 +161,7 @@ bool PSPButton::IsDown() { } PSPDpad::PSPDpad(int arrowIndex, int overlayIndex, float scale, float spacing, UI::LayoutParams *layoutParams) - : UI::View(layoutParams), arrowIndex_(arrowIndex), overlayIndex_(overlayIndex), + : GamepadView(layoutParams), arrowIndex_(arrowIndex), overlayIndex_(overlayIndex), scale_(scale), spacing_(spacing), dragPointerId_(-1), down_(0) { } @@ -143,6 +172,7 @@ void PSPDpad::GetContentDimensions(const UIContext &dc, float &w, float &h) cons void PSPDpad::Touch(const TouchInput &input) { int lastDown = down_; + GamepadView::Touch(input); if (input.flags & TOUCH_DOWN) { if (dragPointerId_ == -1 && bounds_.Contains(input.x, input.y)) { @@ -222,7 +252,7 @@ void PSPDpad::ProcessTouch(float x, float y, bool down) { } void PSPDpad::Draw(UIContext &dc) { - float opacity = g_Config.iTouchButtonOpacity / 100.0f; + float opacity = GetButtonOpacity(); uint32_t colorBg = colorAlpha(GetButtonColor(), opacity); uint32_t color = colorAlpha(0xFFFFFF, opacity); @@ -246,7 +276,7 @@ void PSPDpad::Draw(UIContext &dc) { } PSPStick::PSPStick(int bgImg, int stickImg, int stick, float scale, UI::LayoutParams *layoutParams) - : UI::View(layoutParams), dragPointerId_(-1), bgImg_(bgImg), stickImageIndex_(stickImg), stick_(stick), scale_(scale), centerX_(-1), centerY_(-1) { + : GamepadView(layoutParams), dragPointerId_(-1), bgImg_(bgImg), stickImageIndex_(stickImg), stick_(stick), scale_(scale), centerX_(-1), centerY_(-1) { stick_size_ = 50; } @@ -257,7 +287,7 @@ void PSPStick::GetContentDimensions(const UIContext &dc, float &w, float &h) con } void PSPStick::Draw(UIContext &dc) { - float opacity = g_Config.iTouchButtonOpacity / 100.0f; + float opacity = GetButtonOpacity(); uint32_t colorBg = colorAlpha(GetButtonColor(), opacity); uint32_t color = colorAlpha(0x808080, opacity); @@ -278,6 +308,7 @@ void PSPStick::Draw(UIContext &dc) { } void PSPStick::Touch(const TouchInput &input) { + GamepadView::Touch(input); if (input.flags & TOUCH_RELEASE_ALL) { dragPointerId_ = -1; centerX_ = bounds_.centerX(); diff --git a/UI/GamepadEmu.h b/UI/GamepadEmu.h index 799cc02a5f..c959632b5c 100644 --- a/UI/GamepadEmu.h +++ b/UI/GamepadEmu.h @@ -23,17 +23,32 @@ #include "ui/view.h" #include "ui/viewgroup.h" -class MultiTouchButton : public UI::View { +class GamepadView : public UI::View { public: - MultiTouchButton(int bgImg, int img, float scale, UI::LayoutParams *layoutParams) - : UI::View(layoutParams), pointerDownMask_(0), scale_(scale), bgImg_(bgImg), img_(img), angle_(0.0f), flipImageH_(false) { + GamepadView(UI::LayoutParams *layoutParams) : UI::View(layoutParams) { } - virtual bool Key(const KeyInput &input) override { return false; } - virtual void Update(const InputState &input) override {} - virtual void Touch(const TouchInput &input) override; - virtual void Draw(UIContext &dc) override; - virtual void GetContentDimensions(const UIContext &dc, float &w, float &h) const override; + void Touch(const TouchInput &input) override; + bool Key(const KeyInput &input) override { + return false; + } + void Update(const InputState &input) override; + +protected: + float GetButtonOpacity(); + + static int framesWithoutTouch_; +}; + +class MultiTouchButton : public GamepadView { +public: + MultiTouchButton(int bgImg, int img, float scale, UI::LayoutParams *layoutParams) + : GamepadView(layoutParams), pointerDownMask_(0), scale_(scale), bgImg_(bgImg), img_(img), angle_(0.0f), flipImageH_(false) { + } + + void Touch(const TouchInput &input) override; + void Draw(UIContext &dc) override; + void GetContentDimensions(const UIContext &dc, float &w, float &h) const override; virtual bool IsDown() { return pointerDownMask_ != 0; } // chainable MultiTouchButton *FlipImageH(bool flip) { flipImageH_ = flip; return this; } @@ -56,8 +71,8 @@ public: : MultiTouchButton(bgImg, img, scale, layoutParams), value_(value) { } - virtual void Touch(const TouchInput &input) override; - virtual bool IsDown() override { return *value_; } + void Touch(const TouchInput &input) override; + bool IsDown() override { return *value_; } private: bool *value_; @@ -69,18 +84,16 @@ public: : MultiTouchButton(bgImg, img, scale, layoutParams), pspButtonBit_(pspButtonBit) { } void Touch(const TouchInput &input) override; - virtual bool IsDown() override; + bool IsDown() override; private: int pspButtonBit_; }; -class PSPDpad : public UI::View { +class PSPDpad : public GamepadView { public: PSPDpad(int arrowIndex, int overlayIndex, float scale, float spacing, UI::LayoutParams *layoutParams); - bool Key(const KeyInput &input) override { return false; } - void Update(const InputState &input) override {} void Touch(const TouchInput &input) override; void Draw(UIContext &dc) override; void GetContentDimensions(const UIContext &dc, float &w, float &h) const override; @@ -97,12 +110,10 @@ private: int down_; }; -class PSPStick : public UI::View { +class PSPStick : public GamepadView { public: PSPStick(int bgImg, int stickImg, int stick, float scale, UI::LayoutParams *layoutParams); - bool Key(const KeyInput &input) override { return false; } - void Update(const InputState &input) override {} void Touch(const TouchInput &input) override; void Draw(UIContext &dc) override; void GetContentDimensions(const UIContext &dc, float &w, float &h) const override; @@ -136,7 +147,7 @@ public: ComboKey(int pspButtonBit, int bgImg, int img, float scale, UI::LayoutParams *layoutParams) : MultiTouchButton(bgImg, img, scale, layoutParams), pspButtonBit_(pspButtonBit) { } - virtual void Touch(const TouchInput &input); + void Touch(const TouchInput &input) override; private: int pspButtonBit_; };