From 93de74144e813ed9f4e76ba013a7963a86336aca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 17 Jun 2023 22:22:59 +0200 Subject: [PATCH] Add basic achievement rendering --- Common/Data/Collections/TinySet.h | 2 +- UI/RetroAchievementScreens.cpp | 51 +++++++++++++++++++++++++++++-- UI/RetroAchievementScreens.h | 22 +++++++++++++ 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/Common/Data/Collections/TinySet.h b/Common/Data/Collections/TinySet.h index 702a98dc0f..6521beab48 100644 --- a/Common/Data/Collections/TinySet.h +++ b/Common/Data/Collections/TinySet.h @@ -187,7 +187,7 @@ struct FixedTinyVec { bool operator == (const FixedTinyVec &other) const { if (count_ != other.count_) return false; - for (size_t i = 0; i < count_; i++) { + for (int i = 0; i < count_; i++) { if (!(data_[i] == other.data_[i])) { return false; } diff --git a/UI/RetroAchievementScreens.cpp b/UI/RetroAchievementScreens.cpp index 887c8a4a70..96444d3a80 100644 --- a/UI/RetroAchievementScreens.cpp +++ b/UI/RetroAchievementScreens.cpp @@ -2,6 +2,7 @@ #include "UI/RetroAchievements.h" #include "Common/UI/View.h" #include "Common/UI/ViewGroup.h" +#include "Common/UI/Context.h" #include "Common/Data/Text/I18n.h" void RetroAchievementsListScreen::CreateViews() { @@ -12,6 +13,7 @@ void RetroAchievementsListScreen::CreateViews() { root_ = new ScrollView(UI::ORIENT_VERTICAL); LinearLayout *listLayout = root_->Add(new LinearLayout(UI::ORIENT_VERTICAL)); + listLayout->SetSpacing(0.0f); std::vector achievements; @@ -21,8 +23,7 @@ void RetroAchievementsListScreen::CreateViews() { }); for (auto achievement : achievements) { - listLayout->Add(new TextView(achievement.title)); - listLayout->Add(new TextView(achievement.description)); + listLayout->Add(new AchievementView(achievement)); } } @@ -62,3 +63,49 @@ void RetroAchievementsSettingsScreen::CreateAccountTab(UI::ViewGroup *viewGroup) }); } } + +void MeasureAchievement(const Achievements::Achievement &achievement, float *w, float *h) { + *w = 0.0f; + *h = 60.0f; +} + + +// Render style references: + +// https://www.trueachievements.com/achievement-meme-maker + + +// Graphical +void RenderAchievement(UIContext &dc, const Achievements::Achievement &achievement, AchievementRenderStyle style, const Bounds &bounds, float opacity) { + using namespace UI; + UI::Drawable background = achievement.locked ? dc.theme->popupStyle.background : dc.theme->itemStyle.background; + + background.color = colorAlpha(background.color, opacity); + + dc.Begin(); + dc.FillRect(background, bounds); + + dc.SetFontScale(0.7f, 0.7f); + dc.DrawTextRect(achievement.title.c_str(), bounds.Expand(-5.0f, -5.0f), dc.theme->itemStyle.fgColor, ALIGN_TOPLEFT); + + dc.SetFontScale(0.5f, 0.5f); + dc.DrawTextRect(achievement.description.c_str(), bounds.Expand(-5.0f, -5.0f).Offset(0.0f, 30.0f), dc.theme->itemStyle.fgColor, ALIGN_TOPLEFT); + + char temp[64]; + snprintf(temp, sizeof(temp), "%d", achievement.points); + + dc.SetFontScale(1.5f, 1.5f); + dc.DrawTextRect(temp, bounds.Expand(-5.0f, -5.0f), dc.theme->itemStyle.fgColor, ALIGN_RIGHT | ALIGN_VCENTER); + + dc.SetFontScale(1.0f, 1.0f); + + dc.Flush(); +} + +void AchievementView::Draw(UIContext &dc) { + RenderAchievement(dc, achievement_, AchievementRenderStyle::LISTED, bounds_, 0.0f); +} + +void AchievementView::GetContentDimensions(const UIContext &dc, float &w, float &h) const { + MeasureAchievement(achievement_, &w, &h); +} diff --git a/UI/RetroAchievementScreens.h b/UI/RetroAchievementScreens.h index 5213bb8fc9..8a990d4ecf 100644 --- a/UI/RetroAchievementScreens.h +++ b/UI/RetroAchievementScreens.h @@ -1,10 +1,12 @@ #pragma once +#include "Common/UI/View.h" #include "Common/UI/UIScreen.h" #include "Common/UI/ViewGroup.h" #include "UI/MiscScreens.h" #include "UI/TabbedDialogScreen.h" #include "Common/File/Path.h" +#include "UI/RetroAchievements.h" class RetroAchievementsListScreen : public UIDialogScreenWithGameBackground { public: @@ -25,3 +27,23 @@ public: private: void CreateAccountTab(UI::ViewGroup *viewGroup); }; + +class UIContext; + +enum class AchievementRenderStyle { + LISTED, + UNLOCKED, +}; + +void MeasureAchievement(const Achievements::Achievement &achievement, float *w, float *h); +void RenderAchievement(UIContext &context, const Achievements::Achievement &achievement, AchievementRenderStyle style, const Bounds &bounds, float time_s); + +class AchievementView : public UI::Item { +public: + AchievementView(const Achievements::Achievement &achievement, UI::LayoutParams *layoutParams = nullptr) : UI::Item(layoutParams), achievement_(achievement) {} + + void Draw(UIContext &dc) override; + void GetContentDimensions(const UIContext &dc, float &w, float &h) const override; +private: + Achievements::Achievement achievement_; +};