Merge pull request #10385 from unknownbrackets/ui-tween

UI: Add a soft fade when selecting choices / etc.
This commit is contained in:
Henrik Rydgård authored and GitHub committed 2017-12-10 23:11:37 +01:00
commit da3a7fbc5f
9 files changed
+227 -62

No files matched your search

-2
View File
@@ -46,8 +46,6 @@
#include "GPU/D3D11/TextureCacheD3D11.h"
#include "GPU/D3D11/DrawEngineD3D11.h"
#include "UI/OnScreenDisplay.h"
#include "ext/native/thin3d/thin3d.h"
#include <algorithm>
+16 -7
View File
@@ -475,6 +475,15 @@ void SystemInfoScreen::CreateViews() {
#endif
#ifdef MOBILE_DEVICE
buildConfig->Add(new InfoItem("MOBILE_DEVICE", ""));
#endif
#if PPSSPP_ARCH(ARMV7S)
buildConfig->Add(new InfoItem("ARMV7S", ""));
#endif
#if PPSSPP_ARCH(ARM_NEON)
buildConfig->Add(new InfoItem("ARM_NEON", ""));
#endif
#ifdef _M_SSE
buildConfig->Add(new InfoItem("_M_SSE", StringFromFormat("0x%x", _M_SSE)));
#endif
if (System_GetPropertyBool(SYSPROP_APP_GOLD)) {
buildConfig->Add(new InfoItem("GOLD", ""));
@@ -492,7 +501,7 @@ void SystemInfoScreen::CreateViews() {
std::vector<std::string> exts;
SplitString(cpu_info.Summarize(), ',', exts);
for (size_t i = 2; i < exts.size(); i++) {
cpuExtensions->Add(new TextView(exts[i]))->SetFocusable(true);
cpuExtensions->Add(new TextView(exts[i], new LayoutParams(FILL_PARENT, WRAP_CONTENT)))->SetFocusable(true);
}
ViewGroup *gpuExtensionsScroll = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, FILL_PARENT));
@@ -514,8 +523,8 @@ void SystemInfoScreen::CreateViews() {
exts.clear();
SplitString(g_all_gl_extensions, ' ', exts);
std::sort(exts.begin(), exts.end());
for (size_t i = 0; i < exts.size(); i++) {
gpuExtensions->Add(new TextView(exts[i]))->SetFocusable(true);
for (auto &extension : exts) {
gpuExtensions->Add(new TextView(extension, new LayoutParams(FILL_PARENT, WRAP_CONTENT)))->SetFocusable(true);
}
exts.clear();
@@ -534,8 +543,8 @@ void SystemInfoScreen::CreateViews() {
eglExtensions->Add(new ItemHeader(si->T("EGL Extensions")));
for (size_t i = 0; i < exts.size(); i++) {
eglExtensions->Add(new TextView(exts[i]))->SetFocusable(true);
for (auto &extension : exts) {
eglExtensions->Add(new TextView(extension, new LayoutParams(FILL_PARENT, WRAP_CONTENT)))->SetFocusable(true);
}
}
} else if (g_Config.iGPUBackend == GPU_BACKEND_VULKAN) {
@@ -544,12 +553,12 @@ void SystemInfoScreen::CreateViews() {
gpuExtensions->Add(new ItemHeader(si->T("Vulkan Features")));
std::vector<std::string> features = draw->GetFeatureList();
for (auto &feature : features) {
gpuExtensions->Add(new TextView(feature))->SetFocusable(true);
gpuExtensions->Add(new TextView(feature, new LayoutParams(FILL_PARENT, WRAP_CONTENT)))->SetFocusable(true);
}
gpuExtensions->Add(new ItemHeader(si->T("Vulkan Extensions")));
std::vector<std::string> extensions = draw->GetExtensionList();
for (auto &extension : extensions) {
gpuExtensions->Add(new TextView(extension))->SetFocusable(true);
gpuExtensions->Add(new TextView(extension, new LayoutParams(FILL_PARENT, WRAP_CONTENT)))->SetFocusable(true);
}
}
}
+2
View File
@@ -831,7 +831,9 @@ void EmuScreen::CreateViews() {
// We start invisible here, in case of recreated views.
loadingViewColor_ = loadingView_->AddTween(new TextColorTween(0x00FFFFFF, 0x00FFFFFF, 0.2f, &bezierEaseInOut));
loadingViewColor_->Persist();
loadingViewVisible_ = loadingView_->AddTween(new VisibilityTween(UI::V_INVISIBLE, UI::V_INVISIBLE, 0.2f, &bezierEaseInOut));
loadingViewVisible_->Persist();
}
UI::EventReturn EmuScreen::OnDevTools(UI::EventParams &params) {
+56 -20
View File
@@ -152,31 +152,47 @@ void HttpImageFileView::Draw(UIContext &dc) {
// TODO: involve sizemode
if (texture_) {
float tw = texture_->Width();
float th = texture_->Height();
float x = bounds_.x;
float y = bounds_.y;
float w = bounds_.w;
float h = bounds_.h;
if (tw / th < w / h) {
float nw = h * tw / th;
x += (w - nw) / 2.0f;
w = nw;
} else {
float nh = w * th / tw;
y += (h - nh) / 2.0f;
h = nh;
}
dc.Flush();
dc.GetDrawContext()->BindTexture(0, texture_->GetTexture());
dc.Draw()->Rect(bounds_.x, bounds_.y, bounds_.w, bounds_.h, color_);
dc.Draw()->Rect(x, y, w, h, color_);
dc.Flush();
dc.RebindTexture();
} else {
// draw a black rectangle to represent the missing image.
dc.FillRect(UI::Drawable(0xFF000000), GetBounds());
dc.FillRect(UI::Drawable(0x7F000000), GetBounds());
}
}
// This is the entry in a list. Does not have install buttons and so on.
class ProductItemView : public UI::Choice {
class ProductItemView : public UI::StickyChoice {
public:
ProductItemView(const StoreEntry &entry, UI::LayoutParams *layoutParams = 0)
: UI::Choice(entry.name, layoutParams), entry_(entry) {}
: UI::StickyChoice(entry.name, "", layoutParams), entry_(entry) {}
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override {
w = 300;
h = 164;
}
void Update() override;
void Draw(UIContext &dc) override;
StoreEntry GetEntry() const { return entry_; }
@@ -184,15 +200,6 @@ private:
const StoreEntry &entry_;
};
void ProductItemView::Draw(UIContext &dc) {
UI::Choice::Draw(dc);
// dc.DrawText(entry_.name.c_str(), bounds_.centerX(), bounds_.centerY(), 0xFFFFFFFF, ALIGN_CENTER);
}
void ProductItemView::Update() {
View::Update();
}
// This is a "details" view of a game. Lets you install it.
class ProductView : public UI::LinearLayout {
public:
@@ -253,7 +260,7 @@ void ProductView::CreateViews() {
cancelButton_->SetVisibility(V_GONE);
// Add star rating, comments etc?
Add(new TextView(entry_.description));
Add(new TextView(entry_.description, ALIGN_LEFT | FLAG_WRAP_TEXT, false));
float size = entry_.size / (1024.f * 1024.f);
char temp[256];
@@ -326,7 +333,7 @@ UI::EventReturn ProductView::OnLaunchClick(UI::EventParams &e) {
return UI::EVENT_DONE;
}
StoreScreen::StoreScreen() : loading_(true), connectionError_(false) {
StoreScreen::StoreScreen() {
StoreFilter noFilter;
SetFilter(noFilter);
lang_ = g_Config.sLanguageIni;
@@ -438,23 +445,37 @@ void StoreScreen::CreateViews() {
content->Add(new TextView(loading_ ? st->T("Loading...") : st->T("Connection Error")));
content->Add(new Button(di->T("Retry")))->OnClick.Handle(this, &StoreScreen::OnRetry);
content->Add(new Button(di->T("Back")))->OnClick.Handle<UIScreen>(this, &UIScreen::OnBack);
scrollItemView_ = nullptr;
productPanel_ = nullptr;
} else {
content = new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(FILL_PARENT, FILL_PARENT, 1.0f));
ScrollView *leftScroll = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(WRAP_CONTENT, FILL_PARENT, 0.4f));
leftScroll->SetTag("StoreMainList");
content->Add(leftScroll);
LinearLayout *scrollItemView = new LinearLayout(ORIENT_VERTICAL, new LayoutParams(FILL_PARENT, WRAP_CONTENT));
leftScroll->Add(scrollItemView);
scrollItemView_ = new LinearLayout(ORIENT_VERTICAL, new LayoutParams(FILL_PARENT, WRAP_CONTENT));
leftScroll->Add(scrollItemView_);
std::vector<StoreEntry> entries = FilterEntries();
for (size_t i = 0; i < entries.size(); i++) {
scrollItemView->Add(new ProductItemView(entries_[i]))->OnClick.Handle(this, &StoreScreen::OnGameSelected);
scrollItemView_->Add(new ProductItemView(entries_[i]))->OnClick.Handle(this, &StoreScreen::OnGameSelected);
}
// TODO: Similar apps, etc etc
productPanel_ = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(0.5f));
leftScroll->SetTag("StoreMainProduct");
content->Add(productPanel_);
ProductItemView *selectedItem = GetSelectedItem();
if (selectedItem) {
ProductView *productView = new ProductView(selectedItem->GetEntry());
productView->OnClickLaunch.Handle(this, &StoreScreen::OnGameLaunch);
productPanel_->Add(productView);
selectedItem->Press();
} else {
lastSelectedName_ = "";
}
}
root_->Add(content);
}
@@ -469,6 +490,16 @@ std::vector<StoreEntry> StoreScreen::FilterEntries() {
return filtered;
}
ProductItemView *StoreScreen::GetSelectedItem() {
for (int i = 0; i < scrollItemView_->GetNumSubviews(); ++i) {
ProductItemView *item = static_cast<ProductItemView *>(scrollItemView_->GetViewByIndex(i));
if (item->GetEntry().name == lastSelectedName_)
return item;
}
return nullptr;
}
UI::EventReturn StoreScreen::OnGameSelected(UI::EventParams &e) {
ProductItemView *item = static_cast<ProductItemView *>(e.v);
if (!item)
@@ -478,6 +509,11 @@ UI::EventReturn StoreScreen::OnGameSelected(UI::EventParams &e) {
ProductView *productView = new ProductView(item->GetEntry());
productView->OnClickLaunch.Handle(this, &StoreScreen::OnGameLaunch);
productPanel_->Add(productView);
ProductItemView *previousItem = GetSelectedItem();
if (previousItem && previousItem != item)
previousItem->Release();
lastSelectedName_ = item->GetEntry().name;
return UI::EVENT_DONE;
}
+6 -2
View File
@@ -30,6 +30,7 @@
// Uses GameInfoCache heavily to implement the functionality.
struct json_value;
class ProductItemView;
enum EntryType {
ENTRY_PBPZIP,
@@ -74,6 +75,7 @@ protected:
private:
void SetFilter(const StoreFilter &filter);
void ParseListing(std::string json);
ProductItemView *GetSelectedItem();
std::vector<StoreEntry> FilterEntries();
std::string GetStoreJsonURL(std::string storePath) const;
@@ -86,8 +88,8 @@ private:
// local filesystems at the moment.
std::string storePath_;
bool loading_;
bool connectionError_;
bool loading_ = true;
bool connectionError_ = false;
std::map<std::string, StoreCategory> categories_;
@@ -97,7 +99,9 @@ private:
StoreFilter filter_;
std::string lang_;
std::string lastSelectedName_;
UI::ViewGroup *scrollItemView_;
UI::ViewGroup *productPanel_;
UI::TextView *titleText_;
};
+23 -1
View File
@@ -5,11 +5,21 @@
namespace UI {
void Tween::Apply(View *view) {
if (time_now() >= start_ + duration_)
if (!valid_)
return;
if (DurationOffset() >= duration_)
finishApplied_ = true;
float pos = Position();
DoApply(view, pos);
if (finishApplied_) {
UI::EventParams e{};
e.v = view;
e.f = DurationOffset() - duration_;
Finish.Trigger(e);
}
}
template <typename Value>
@@ -17,8 +27,10 @@ void TweenBase<Value>::PersistData(PersistStatus status, std::string anonId, Per
struct TweenData {
float start;
float duration;
float delay;
Value from;
Value to;
bool valid;
};
PersistBuffer &buffer = storage["TweenBase::" + anonId];
@@ -30,8 +42,10 @@ void TweenBase<Value>::PersistData(PersistStatus status, std::string anonId, Per
TweenData &data = *(TweenData *)&buffer[0];
data.start = start_;
data.duration = duration_;
data.delay = delay_;
data.from = from_;
data.to = to_;
data.valid = valid_;
}
break;
case UI::PERSIST_RESTORE:
@@ -39,8 +53,10 @@ void TweenBase<Value>::PersistData(PersistStatus status, std::string anonId, Per
TweenData data = *(TweenData *)&buffer[0];
start_ = data.start;
duration_ = data.duration;
delay_ = data.delay;
from_ = data.from;
to_ = data.to;
valid_ = data.valid;
// We skip finishApplied_ here so that the tween will reapply.
// This does mean it's important to remember to update tweens even after finish.
}
@@ -61,6 +77,12 @@ void TextColorTween::DoApply(View *view, float pos) {
tv->SetTextColor(Current(pos));
}
void CallbackColorTween::DoApply(View *view, float pos) {
if (callback_) {
callback_(view, Current(pos));
}
}
void VisibilityTween::DoApply(View *view, float pos) {
view->SetVisibility(Current(pos));
}
+52 -11
View File
@@ -2,6 +2,7 @@
#include <algorithm>
#include <cstdint>
#include <functional>
#include "base/timeutil.h"
#include "ui/view.h"
@@ -10,7 +11,7 @@ namespace UI {
// This is the class to use in Update().
class Tween {
public:
Tween(float duration, float (*curve)(float)) : duration_(duration), curve_(curve) {
explicit Tween(float duration, float (*curve)(float)) : duration_(duration), curve_(curve) {
start_ = time_now();
}
virtual ~Tween() {
@@ -20,14 +21,27 @@ public:
void Apply(View *view);
bool Finished() {
return finishApplied_ && time_now() >= start_ + duration_;
return finishApplied_ && time_now() >= start_ + delay_ + duration_;
}
void Persist() {
persists_ = true;
}
bool Persists() {
return persists_;
}
void Delay(float s) {
delay_ = s;
}
virtual void PersistData(PersistStatus status, std::string anonId, PersistMap &storage) = 0;
Event Finish;
protected:
float DurationOffset() {
return time_now() - start_;
return time_now() - start_ - delay_;
}
float Position() {
@@ -38,7 +52,10 @@ protected:
float start_;
float duration_;
float delay_ = 0.0f;
bool finishApplied_ = false;
bool persists_ = false;
bool valid_ = false;
float (*curve_)(float);
};
@@ -47,29 +64,37 @@ protected:
template <typename Value>
class TweenBase: public Tween {
public:
TweenBase(float duration, float (*curve)(float) = [](float f) { return f; })
: Tween(duration, curve) {
}
TweenBase(Value from, Value to, float duration, float (*curve)(float) = [](float f) { return f; })
: Tween(duration, curve), from_(from), to_(to) {
valid_ = true;
}
// Use this to change the destination value.
// Useful when a state flips while the tween is half-way through.
void Divert(const Value &newTo, float newDuration = -1.0f) {
const Value newFrom = Current(Position());
const Value newFrom = valid_ ? Current(Position()) : newTo;
// Are we already part way through another transition?
if (time_now() < start_ + duration_) {
if (time_now() < start_ + delay_ + duration_ && valid_) {
if (newTo == to_) {
// Already on course. Don't change.
} else if (newTo == from_) {
return;
} else if (newTo == from_ && duration_ > 0.0f) {
// Reversing, adjust start_ to be smooth from the current value.
float newOffset = duration_ - DurationOffset();
if (newDuration >= 0.0f && duration_ > 0.0f) {
float newOffset = duration_ - std::max(0.0f, DurationOffset());
if (newDuration >= 0.0f) {
newOffset *= newDuration / duration_;
}
start_ = time_now() - newOffset;
} else {
// Otherwise, start over.
start_ = time_now() - newOffset - delay_;
} else if (time_now() <= start_ + delay_) {
// Start the delay over again.
start_ = time_now();
} else {
// Since we've partially animated to the other value, skip delay.
start_ = time_now() - delay_;
}
} else {
// Already finished, so restart.
@@ -79,6 +104,7 @@ public:
from_ = newFrom;
to_ = newTo;
valid_ = true;
if (newDuration >= 0.0f) {
duration_ = newDuration;
}
@@ -93,6 +119,7 @@ public:
void Reset(const Value &newFrom) {
from_ = newFrom;
to_ = newFrom;
valid_ = true;
}
const Value &FromValue() const {
@@ -131,6 +158,20 @@ protected:
void DoApply(View *view, float pos) override;
};
class CallbackColorTween : public ColorTween {
public:
using ColorTween::ColorTween;
void SetCallback(const std::function<void(View *v, uint32_t c)> &cb) {
callback_ = cb;
}
protected:
void DoApply(View *view, float pos) override;
std::function<void(View *v, uint32_t c)> callback_;
};
class VisibilityTween : public TweenBase<Visibility> {
public:
using TweenBase::TweenBase;
+56 -8
View File
@@ -169,9 +169,15 @@ View::~View() {
}
void View::Update() {
for (Tween *tween : tweens_) {
if (!tween->Finished())
for (size_t i = 0; i < tweens_.size(); ++i) {
Tween *tween = tweens_[i];
if (!tween->Finished()) {
tween->Apply(this);
} else if (!tween->Persists()) {
tweens_.erase(tweens_.begin() + i);
i--;
delete tween;
}
}
}
@@ -255,6 +261,28 @@ bool View::SetFocus() {
return false;
}
Clickable::Clickable(LayoutParams *layoutParams)
: View(layoutParams) {
// We set the colors later once we have a UIContext.
bgColor_ = AddTween(new CallbackColorTween(0.1f));
bgColor_->Persist();
}
void Clickable::DrawBG(UIContext &dc, const Style &style) {
if (style.background.type == DRAW_SOLID_COLOR) {
if (time_now() - bgColorLast_ >= 0.25f) {
bgColor_->Reset(style.background.color);
} else {
bgColor_->Divert(style.background.color, down_ ? 0.05f : 0.1f);
}
bgColorLast_ = time_now();
dc.FillRect(Drawable(bgColor_->CurrentValue()), bounds_);
} else {
dc.FillRect(style.background, bounds_);
}
}
void Clickable::Click() {
UI::EventParams e{};
e.v = this;
@@ -445,7 +473,7 @@ ClickableItem::ClickableItem(LayoutParams *layoutParams) : Clickable(layoutParam
}
void ClickableItem::Draw(UIContext &dc) {
Style style = dc.theme->itemStyle;
Style style = dc.theme->itemStyle;
if (HasFocus()) {
style = dc.theme->itemFocusedStyle;
@@ -454,7 +482,7 @@ void ClickableItem::Draw(UIContext &dc) {
style = dc.theme->itemDownStyle;
}
dc.FillRect(style.background, bounds_);
DrawBG(dc, style);
}
void Choice::GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert, float &w, float &h) const {
@@ -496,7 +524,7 @@ void Choice::Draw(UIContext &dc) {
if (!IsSticky()) {
ClickableItem::Draw(dc);
} else {
Style style = dc.theme->itemStyle;
Style style = dc.theme->itemStyle;
if (highlighted_) {
style = dc.theme->itemHighlightedStyle;
}
@@ -506,7 +534,8 @@ void Choice::Draw(UIContext &dc) {
if (HasFocus()) {
style = dc.theme->itemFocusedStyle;
}
dc.FillRect(style.background, bounds_);
DrawBG(dc, style);
}
Style style = dc.theme->itemStyle;
@@ -542,11 +571,30 @@ void Choice::Draw(UIContext &dc) {
}
}
InfoItem::InfoItem(const std::string &text, const std::string &rightText, LayoutParams *layoutParams)
: Item(layoutParams), text_(text), rightText_(rightText) {
// We set the colors later once we have a UIContext.
bgColor_ = AddTween(new CallbackColorTween(0.1f));
bgColor_->Persist();
fgColor_ = AddTween(new CallbackColorTween(0.1f));
fgColor_->Persist();
}
void InfoItem::Draw(UIContext &dc) {
Item::Draw(dc);
UI::Style style = HasFocus() ? dc.theme->itemFocusedStyle : dc.theme->infoStyle;
style.background.color &= 0x7fffffff;
if (style.background.type == DRAW_SOLID_COLOR) {
// For a smoother fade, using the same color with 0 alpha.
if ((style.background.color & 0xFF000000) == 0)
style.background.color = dc.theme->itemFocusedStyle.background.color & 0x00FFFFFF;
bgColor_->Divert(style.background.color & 0x7fffffff);
style.background.color = bgColor_->CurrentValue();
}
fgColor_->Divert(style.fgColor);
style.fgColor = fgColor_->CurrentValue();
dc.FillRect(style.background, bounds_);
int paddingX = 12;
@@ -684,7 +732,7 @@ void Button::Draw(UIContext &dc) {
if (!IsEnabled()) style = dc.theme->buttonDisabledStyle;
// dc.Draw()->DrawImage4Grid(style.image, bounds_.x, bounds_.y, bounds_.x2(), bounds_.y2(), style.bgColor);
dc.FillRect(style.background, bounds_);
DrawBG(dc, style);
float tw, th;
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), &tw, &th);
if (tw > bounds_.w || imageID_ != -1) {
+16 -11
View File
@@ -347,6 +347,7 @@ private:
View *GetFocusedView();
class Tween;
class CallbackColorTween;
class View {
public:
@@ -468,8 +469,7 @@ public:
// All these light up their background when touched, or have focus.
class Clickable : public View {
public:
Clickable(LayoutParams *layoutParams)
: View(layoutParams), downCountDown_(0), dragging_(false), down_(false){}
Clickable(LayoutParams *layoutParams);
bool Key(const KeyInput &input) override;
void Touch(const TouchInput &input) override;
@@ -483,10 +483,13 @@ protected:
// the event.
// Use it for checking/unchecking checkboxes, etc.
virtual void Click();
void DrawBG(UIContext &dc, const Style &style);
int downCountDown_;
bool dragging_;
bool down_;
CallbackColorTween *bgColor_ = nullptr;
float bgColorLast_ = 0.0f;
int downCountDown_ = 0;
bool dragging_ = false;
bool down_ = false;
};
class Button : public Clickable {
@@ -613,11 +616,11 @@ public:
// Use to trigger something or open a submenu screen.
class Choice : public ClickableItem {
public:
Choice(const std::string &text, LayoutParams *layoutParams = 0)
: ClickableItem(layoutParams), text_(text), smallText_(), atlasImage_(-1), iconImage_(-1), centered_(false), highlighted_(false), selected_(false) {}
Choice(const std::string &text, const std::string &smallText, bool selected = false, LayoutParams *layoutParams = 0)
Choice(const std::string &text, LayoutParams *layoutParams = nullptr)
: Choice(text, std::string(), false, layoutParams) {}
Choice(const std::string &text, const std::string &smallText, bool selected = false, LayoutParams *layoutParams = nullptr)
: ClickableItem(layoutParams), text_(text), smallText_(smallText), atlasImage_(-1), iconImage_(-1), centered_(false), highlighted_(false), selected_(selected) {}
Choice(ImageID image, LayoutParams *layoutParams = 0)
Choice(ImageID image, LayoutParams *layoutParams = nullptr)
: ClickableItem(layoutParams), atlasImage_(image), iconImage_(-1), centered_(false), highlighted_(false), selected_(false) {}
virtual void HighlightChanged(bool highlighted);
@@ -670,8 +673,7 @@ protected:
class InfoItem : public Item {
public:
InfoItem(const std::string &text, const std::string &rightText, LayoutParams *layoutParams = 0)
: Item(layoutParams), text_(text), rightText_(rightText) {}
InfoItem(const std::string &text, const std::string &rightText, LayoutParams *layoutParams = nullptr);
void Draw(UIContext &dc) override;
@@ -689,6 +691,9 @@ public:
}
private:
CallbackColorTween *bgColor_ = nullptr;
CallbackColorTween *fgColor_ = nullptr;
std::string text_;
std::string rightText_;
};