Skip to content

Commit

Permalink
Update gamepad opacity once per frame, not once per button
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Dec 29, 2023
1 parent f42e9d9 commit 4609df4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
4 changes: 4 additions & 0 deletions UI/EmuScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,8 @@ bool EmuScreen::UnsyncTouch(const TouchInput &touch) {
}
}

GamepadTouch();

if (root_) {
UIScreen::UnsyncTouch(touch);
}
Expand Down Expand Up @@ -1456,6 +1458,8 @@ ScreenRenderFlags EmuScreen::render(ScreenRenderMode mode) {
return flags; // shouldn't really happen but I've seen a suspicious stack trace..
}

GamepadUpdateOpacity();

bool skipBufferEffects = g_Config.bSkipBufferEffects;

if (mode & ScreenRenderMode::FIRST) {
Expand Down
66 changes: 31 additions & 35 deletions UI/GamepadEmu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,43 @@ const float TOUCH_SCALE_FACTOR = 1.5f;
static uint32_t usedPointerMask = 0;
static uint32_t analogPointerMask = 0;

static u32 GetButtonColor() {
return g_Config.iTouchButtonStyle != 0 ? 0xFFFFFF : 0xc0b080;
}
static float g_gamepadOpacity;
static double g_lastTouch;

void GamepadUpdateOpacity() {
if (coreState != CORE_RUNNING) {
g_gamepadOpacity = 0.0f;
return;
}

GamepadView::GamepadView(const char *key, UI::LayoutParams *layoutParams) : UI::View(layoutParams), key_(key) {
lastFrameTime_ = time_now_d();
float fadeAfterSeconds = g_Config.iTouchButtonHideSeconds;
float fadeTransitionSeconds = std::min(fadeAfterSeconds, 0.5f);
float opacity = g_Config.iTouchButtonOpacity / 100.0f;

float multiplier = 1.0f;
float secondsWithoutTouch = time_now_d() - g_lastTouch;
if (secondsWithoutTouch >= fadeAfterSeconds && fadeAfterSeconds > 0.0f) {
if (secondsWithoutTouch >= fadeAfterSeconds + fadeTransitionSeconds) {
multiplier = 0.0f;
} else {
float secondsIntoFade = secondsWithoutTouch - fadeAfterSeconds;
multiplier = 1.0f - (secondsIntoFade / fadeTransitionSeconds);
}
}

g_gamepadOpacity = opacity * multiplier;
}

bool GamepadView::Touch(const TouchInput &input) {
secondsWithoutTouch_ = 0.0f;
return true;
void GamepadTouch() {
g_lastTouch = time_now_d();
}

void GamepadView::Update() {
const double now = time_now_d();
float delta = now - lastFrameTime_;
if (delta > 0) {
secondsWithoutTouch_ += delta;
}
lastFrameTime_ = now;
static u32 GetButtonColor() {
return g_Config.iTouchButtonStyle != 0 ? 0xFFFFFF : 0xc0b080;
}

GamepadView::GamepadView(const char *key, UI::LayoutParams *layoutParams) : UI::View(layoutParams), key_(key) {}

std::string GamepadView::DescribeText() const {
auto co = GetI18NCategory(I18NCat::CONTROLS);
return co->T(key_);
Expand All @@ -70,26 +85,7 @@ float GamepadView::GetButtonOpacity() {
if (forceVisible_) {
return 1.0f;
}

if (coreState != CORE_RUNNING) {
return 0.0f;
}

float fadeAfterSeconds = g_Config.iTouchButtonHideSeconds;
float fadeTransitionSeconds = std::min(fadeAfterSeconds, 0.5f);
float opacity = g_Config.iTouchButtonOpacity / 100.0f;

float multiplier = 1.0f;
if (secondsWithoutTouch_ >= fadeAfterSeconds && fadeAfterSeconds > 0.0f) {
if (secondsWithoutTouch_ >= fadeAfterSeconds + fadeTransitionSeconds) {
multiplier = 0.0f;
} else {
float secondsIntoFade = secondsWithoutTouch_ - fadeAfterSeconds;
multiplier = 1.0f - (secondsIntoFade / fadeTransitionSeconds);
}
}

return opacity * multiplier;
return g_gamepadOpacity;
}

void MultiTouchButton::GetContentDimensions(const UIContext &dc, float &w, float &h) const {
Expand Down
6 changes: 3 additions & 3 deletions UI/GamepadEmu.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ class GamepadView : public UI::View {
public:
GamepadView(const char *key, UI::LayoutParams *layoutParams);

bool Touch(const TouchInput &input) override;
bool Key(const KeyInput &input) override {
return false;
}
void Update() override;
std::string DescribeText() const override;

void SetForceVisible(bool visible) {
Expand All @@ -45,7 +43,6 @@ class GamepadView : public UI::View {
virtual float GetButtonOpacity();

std::string key_;
double lastFrameTime_;
float secondsWithoutTouch_ = 0.0;
bool forceVisible_ = false;
};
Expand Down Expand Up @@ -379,3 +376,6 @@ namespace GestureKey {
VIRTKEY_AXIS_Y_MAX,
};
}

void GamepadTouch();
void GamepadUpdateOpacity();

0 comments on commit 4609df4

Please sign in to comment.