Skip to content

Commit b17ff0e

Browse files
authored
Merge pull request #14818 from iota97/super-wp
Focus based moving background
2 parents 4477001 + c94e9ad commit b17ff0e

File tree

8 files changed

+107
-16
lines changed

8 files changed

+107
-16
lines changed

Common/UI/Root.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ static std::mutex eventMutex_;
2424
static std::function<void(UISound)> soundCallback;
2525
static bool soundEnabled = true;
2626

27-
2827
struct DispatchQueueItem {
2928
Event *e;
3029
EventParams params;

Common/UI/Screen.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Common/UI/Screen.h"
55
#include "Common/UI/UI.h"
66
#include "Common/UI/View.h"
7+
#include "Common/UI/ViewGroup.h"
78

89
#include "Common/Log.h"
910
#include "Common/TimeUtil.h"
@@ -191,6 +192,15 @@ void ScreenManager::render() {
191192
processFinishDialog();
192193
}
193194

195+
void ScreenManager::getFocusPosition(float &x, float &y, float &z) {
196+
UI::ScrollView::GetLastScrollPosition(x, y);
197+
198+
UI::View *v = UI::GetFocusedView();
199+
x += v ? v->GetBounds().x : 0;
200+
y += v ? v->GetBounds().y : 0;
201+
z = stack_.size();
202+
}
203+
194204
void ScreenManager::sendMessage(const char *msg, const char *value) {
195205
if (!strcmp(msg, "recreateviews"))
196206
RecreateAllViews();

Common/UI/Screen.h

+2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ class ScreenManager {
141141

142142
Screen *topScreen() const;
143143

144+
void getFocusPosition(float &x, float &y, float &z);
145+
144146
std::recursive_mutex inputLock_;
145147

146148
private:

Common/UI/ViewGroup.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -1091,11 +1091,25 @@ bool ScrollView::CanScroll() const {
10911091
}
10921092
}
10931093

1094+
float ScrollView::lastScrollPosX = 0;
1095+
float ScrollView::lastScrollPosY = 0;
1096+
1097+
ScrollView::~ScrollView() {
1098+
lastScrollPosX = 0;
1099+
lastScrollPosY = 0;
1100+
}
1101+
1102+
void ScrollView::GetLastScrollPosition(float &x, float &y) {
1103+
x = lastScrollPosX;
1104+
y = lastScrollPosY;
1105+
}
1106+
10941107
void ScrollView::Update() {
10951108
if (visibility_ != V_VISIBLE) {
10961109
inertia_ = 0.0f;
10971110
}
10981111
ViewGroup::Update();
1112+
float oldPos = scrollPos_;
10991113

11001114
Gesture gesture = orientation_ == ORIENT_VERTICAL ? GESTURE_DRAG_VERTICAL : GESTURE_DRAG_HORIZONTAL;
11011115
gesture_.UpdateFrame();
@@ -1124,6 +1138,9 @@ void ScrollView::Update() {
11241138
pull_ = 0.0f;
11251139
}
11261140
}
1141+
1142+
if (oldPos != scrollPos_)
1143+
orientation_ == ORIENT_HORIZONTAL ? lastScrollPosX = scrollPos_ : lastScrollPosY = scrollPos_;
11271144
}
11281145

11291146
void AnchorLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) {

Common/UI/ViewGroup.h

+7
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ class ScrollView : public ViewGroup {
264264
public:
265265
ScrollView(Orientation orientation, LayoutParams *layoutParams = 0, bool rememberPosition = false)
266266
: ViewGroup(layoutParams), orientation_(orientation), rememberPosition_(rememberPosition) {}
267+
~ScrollView();
267268

268269
void Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) override;
269270
void Layout() override;
@@ -280,6 +281,9 @@ class ScrollView : public ViewGroup {
280281
bool CanScroll() const;
281282
void Update() override;
282283

284+
// Get the last moved scroll view position
285+
static void GetLastScrollPosition(float &x, float &y);
286+
283287
// Override so that we can scroll to the active one after moving the focus.
284288
bool SubviewFocused(View *view) override;
285289
void PersistData(PersistStatus status, std::string anonId, PersistMap &storage) override;
@@ -306,6 +310,9 @@ class ScrollView : public ViewGroup {
306310
float lastViewSize_ = 0.0f;
307311
bool scrollToTopOnSizeChange_ = false;
308312
bool rememberPosition_;
313+
314+
static float lastScrollPosX;
315+
static float lastScrollPosY;
309316
};
310317

311318
class ViewPager : public ScrollView {

Core/ConfigValues.h

+1
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,5 @@ enum class BackgroundAnimation {
129129
FLOATING_SYMBOLS = 1,
130130
RECENT_GAMES = 2,
131131
WAVE = 3,
132+
MOVING_BACKGROUND = 4,
132133
};

UI/GameSettingsScreen.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ void GameSettingsScreen::CreateViews() {
880880
if (backgroundChoice_ != nullptr) {
881881
backgroundChoice_->OnClick.Handle(this, &GameSettingsScreen::OnChangeBackground);
882882
}
883-
static const char *backgroundAnimations[] = { "No animation", "Floating symbols", "Recent games", "Waves" };
883+
static const char *backgroundAnimations[] = { "No animation", "Floating symbols", "Recent games", "Waves", "Moving background" };
884884
systemSettings->Add(new PopupMultiChoice(&g_Config.iBackgroundAnimation, sy->T("UI background animation"), backgroundAnimations, 0, ARRAY_SIZE(backgroundAnimations), sy->GetName(), screenManager()));
885885

886886
systemSettings->Add(new ItemHeader(sy->T("Help the PPSSPP team")));

UI/MiscScreens.cpp

+69-14
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,54 @@ static std::unique_ptr<ManagedTexture> bgTexture;
7676
class Animation {
7777
public:
7878
virtual ~Animation() {}
79-
virtual void Draw(UIContext &dc, double t, float alpha) = 0;
79+
virtual void Draw(UIContext &dc, double t, float alpha, float x, float y, float z) = 0;
80+
};
81+
82+
class MovingBackground : public Animation {
83+
public:
84+
void Draw(UIContext &dc, double t, float alpha, float x, float y, float z) override {
85+
if (!bgTexture)
86+
return;
87+
88+
dc.Flush();
89+
dc.GetDrawContext()->BindTexture(0, bgTexture->GetTexture());
90+
Bounds bounds = dc.GetBounds();
91+
92+
x = std::min(std::max(x/bounds.w, 0.0f), 1.0f) * XFAC;
93+
y = std::min(std::max(y/bounds.h, 0.0f), 1.0f) * YFAC;
94+
z = 1.0f + std::max(XFAC, YFAC) + (z-1.0f) * ZFAC;
95+
96+
lastX_ = abs(x-lastX_) > 0.001f ? x*XSPEED+lastX_*(1.0f-XSPEED) : x;
97+
lastY_ = abs(y-lastY_) > 0.001f ? y*YSPEED+lastY_*(1.0f-YSPEED) : y;
98+
lastZ_ = abs(z-lastZ_) > 0.001f ? z*ZSPEED+lastZ_*(1.0f-ZSPEED) : z;
99+
100+
float u1 = lastX_/lastZ_;
101+
float v1 = lastY_/lastZ_;
102+
float u2 = (1.0f+lastX_)/lastZ_;
103+
float v2 = (1.0f+lastY_)/lastZ_;
104+
105+
dc.Draw()->DrawTexRect(bounds, u1, v1, u2, v2, whiteAlpha(alpha));
106+
107+
dc.Flush();
108+
dc.RebindTexture();
109+
}
110+
111+
private:
112+
static constexpr float XFAC = 0.3f;
113+
static constexpr float YFAC = 0.3f;
114+
static constexpr float ZFAC = 0.12f;
115+
static constexpr float XSPEED = 0.05f;
116+
static constexpr float YSPEED = 0.05f;
117+
static constexpr float ZSPEED = 0.1f;
118+
119+
float lastX_ = 0.0f;
120+
float lastY_ = 0.0f;
121+
float lastZ_ = 1.0f + std::max(XFAC, YFAC);
80122
};
81123

82124
class WaveAnimation : public Animation {
83125
public:
84-
void Draw(UIContext &dc, double t, float alpha) override {
126+
void Draw(UIContext &dc, double t, float alpha, float x, float y, float z) override {
85127
const uint32_t color = colorAlpha(0xFFFFFFFF, alpha * 0.2f);
86128
const float speed = 1.0;
87129

@@ -114,7 +156,7 @@ class WaveAnimation : public Animation {
114156
class FloatingSymbolsAnimation : public Animation {
115157
public:
116158
~FloatingSymbolsAnimation() override {}
117-
void Draw(UIContext &dc, double t, float alpha) override {
159+
void Draw(UIContext &dc, double t, float alpha, float x, float y, float z) override {
118160
float xres = dc.GetBounds().w;
119161
float yres = dc.GetBounds().h;
120162
if (last_xres != xres || last_yres != yres) {
@@ -153,7 +195,7 @@ class FloatingSymbolsAnimation : public Animation {
153195
class RecentGamesAnimation : public Animation {
154196
public:
155197
~RecentGamesAnimation() override {}
156-
void Draw(UIContext &dc, double t, float alpha) override {
198+
void Draw(UIContext &dc, double t, float alpha, float x, float y, float z) override {
157199
if (lastIndex_ == nextIndex_) {
158200
CheckNext(dc, t);
159201
} else if (t > nextT_) {
@@ -253,7 +295,7 @@ void UIBackgroundShutdown() {
253295
bgTextureInited = false;
254296
}
255297

256-
void DrawBackground(UIContext &dc, float alpha) {
298+
void DrawBackground(UIContext &dc, float alpha, float x, float y, float z) {
257299
if (!bgTextureInited) {
258300
UIBackgroundInit(dc);
259301
bgTextureInited = true;
@@ -271,6 +313,9 @@ void DrawBackground(UIContext &dc, float alpha) {
271313
case BackgroundAnimation::WAVE:
272314
g_Animation.reset(new WaveAnimation());
273315
break;
316+
case BackgroundAnimation::MOVING_BACKGROUND:
317+
g_Animation.reset(new MovingBackground());
318+
break;
274319
default:
275320
g_Animation.reset(nullptr);
276321
}
@@ -301,11 +346,11 @@ void DrawBackground(UIContext &dc, float alpha) {
301346
#endif
302347

303348
if (g_Animation) {
304-
g_Animation->Draw(dc, t, alpha);
349+
g_Animation->Draw(dc, t, alpha, x, y, z);
305350
}
306351
}
307352

308-
void DrawGameBackground(UIContext &dc, const Path &gamePath) {
353+
void DrawGameBackground(UIContext &dc, const Path &gamePath, float x, float y, float z) {
309354
std::shared_ptr<GameInfo> ginfo;
310355
if (!gamePath.empty())
311356
ginfo = g_gameInfoCache->GetInfo(dc.GetDrawContext(), gamePath, GAMEINFO_WANTBG);
@@ -321,7 +366,7 @@ void DrawGameBackground(UIContext &dc, const Path &gamePath) {
321366
dc.Flush();
322367
dc.RebindTexture();
323368
} else {
324-
::DrawBackground(dc, 1.0f);
369+
::DrawBackground(dc, 1.0f, x, y, z);
325370
dc.RebindTexture();
326371
dc.Flush();
327372
}
@@ -367,15 +412,19 @@ void HandleCommonMessages(const char *message, const char *value, ScreenManager
367412
}
368413

369414
void UIScreenWithBackground::DrawBackground(UIContext &dc) {
370-
::DrawBackground(dc, 1.0f);
415+
float x, y, z;
416+
screenManager()->getFocusPosition(x, y, z);
417+
::DrawBackground(dc, 1.0f, x, y, z);
371418
dc.Flush();
372419
}
373420

374421
void UIScreenWithGameBackground::DrawBackground(UIContext &dc) {
422+
float x, y, z;
423+
screenManager()->getFocusPosition(x, y, z);
375424
if (!gamePath_.empty()) {
376-
DrawGameBackground(dc, gamePath_);
425+
DrawGameBackground(dc, gamePath_, x, y, z);
377426
} else {
378-
::DrawBackground(dc, 1.0f);
427+
::DrawBackground(dc, 1.0f, x, y, z);
379428
dc.Flush();
380429
}
381430
}
@@ -389,7 +438,9 @@ void UIScreenWithGameBackground::sendMessage(const char *message, const char *va
389438
}
390439

391440
void UIDialogScreenWithGameBackground::DrawBackground(UIContext &dc) {
392-
DrawGameBackground(dc, gamePath_);
441+
float x, y, z;
442+
screenManager()->getFocusPosition(x, y, z);
443+
DrawGameBackground(dc, gamePath_, x, y, z);
393444
}
394445

395446
void UIDialogScreenWithGameBackground::sendMessage(const char *message, const char *value) {
@@ -405,7 +456,9 @@ void UIScreenWithBackground::sendMessage(const char *message, const char *value)
405456
}
406457

407458
void UIDialogScreenWithBackground::DrawBackground(UIContext &dc) {
408-
::DrawBackground(dc, 1.0f);
459+
float x, y, z;
460+
screenManager()->getFocusPosition(x, y, z);
461+
::DrawBackground(dc, 1.0f, x, y, z);
409462
dc.Flush();
410463
}
411464

@@ -689,7 +742,9 @@ void LogoScreen::render() {
689742
alphaText = 3.0f - t;
690743
uint32_t textColor = colorAlpha(dc.theme->infoStyle.fgColor, alphaText);
691744

692-
::DrawBackground(dc, alpha);
745+
float x, y, z;
746+
screenManager()->getFocusPosition(x, y, z);
747+
::DrawBackground(dc, alpha, x, y, z);
693748

694749
auto cr = GetI18NCategory("PSPCredits");
695750
auto gr = GetI18NCategory("Graphics");

0 commit comments

Comments
 (0)