From 74c842c951473c84f44f290867e5749a0d1229bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 19 Sep 2021 18:45:15 +0200 Subject: [PATCH] Add image support to UI::Choice. --- Common/UI/View.cpp | 25 ++++++++++++++++--------- Common/UI/View.h | 18 ++++++++++-------- UI/MemStickScreen.cpp | 2 +- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/Common/UI/View.cpp b/Common/UI/View.cpp index 2bf77a4053..71096012f2 100644 --- a/Common/UI/View.cpp +++ b/Common/UI/View.cpp @@ -431,8 +431,8 @@ void Choice::Click() { } void Choice::GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert, float &w, float &h) const { - if (atlasImage_.isValid()) { - dc.Draw()->GetAtlas()->measureImage(atlasImage_, &w, &h); + if (image_.isValid()) { + dc.Draw()->GetAtlas()->measureImage(image_, &w, &h); } else { const int paddingX = 12; float availWidth = horiz.size - paddingX * 2 - textPadding_.horiz(); @@ -486,23 +486,30 @@ void Choice::Draw(UIContext &dc) { style = dc.theme->itemDisabledStyle; } - if (atlasImage_.isValid()) { - dc.Draw()->DrawImage(atlasImage_, bounds_.centerX(), bounds_.centerY(), 1.0f, style.fgColor, ALIGN_CENTER); + if (image_.isValid() && text_.empty()) { + dc.Draw()->DrawImage(image_, bounds_.centerX(), bounds_.centerY(), 1.0f, style.fgColor, ALIGN_CENTER); } else { dc.SetFontStyle(dc.theme->uiFont); - const int paddingX = 12; - const float availWidth = bounds_.w - paddingX * 2 - textPadding_.horiz(); + int paddingX = 12; + float availWidth = bounds_.w - paddingX * 2 - textPadding_.horiz(); + + if (image_.isValid()) { + const AtlasImage *image = dc.Draw()->GetAtlas()->getImage(image_); + paddingX += image->w + 12; + availWidth -= image->w + 12; + dc.Draw()->DrawImage(image_, bounds_.x + 6, bounds_.centerY(), 1.0f, 0xFFFFFFFF, ALIGN_LEFT | ALIGN_VCENTER); + } + float scale = CalculateTextScale(dc, availWidth); dc.SetFontScale(scale, scale); if (centered_) { dc.DrawTextRect(text_.c_str(), bounds_, style.fgColor, ALIGN_CENTER | FLAG_WRAP_TEXT); } else { - if (iconImage_.isValid()) { - dc.Draw()->DrawImage(iconImage_, bounds_.x2() - 32 - paddingX, bounds_.centerY(), 0.5f, style.fgColor, ALIGN_CENTER); + if (rightIconImage_.isValid()) { + dc.Draw()->DrawImage(rightIconImage_, bounds_.x2() - 32 - paddingX, bounds_.centerY(), 0.5f, style.fgColor, ALIGN_CENTER); } - Bounds textBounds(bounds_.x + paddingX + textPadding_.left, bounds_.y, availWidth, bounds_.h); dc.DrawTextRect(text_.c_str(), textBounds, style.fgColor, ALIGN_VCENTER | FLAG_WRAP_TEXT); } diff --git a/Common/UI/View.h b/Common/UI/View.h index 136468ebed..adcddd758a 100644 --- a/Common/UI/View.h +++ b/Common/UI/View.h @@ -689,10 +689,12 @@ class Choice : public ClickableItem { public: Choice(const std::string &text, LayoutParams *layoutParams = nullptr) : Choice(text, std::string(), false, layoutParams) {} + Choice(const std::string &text, ImageID image, LayoutParams *layoutParams = nullptr) + : ClickableItem(layoutParams), text_(text), image_(image) {} Choice(const std::string &text, const std::string &smallText, bool selected = false, LayoutParams *layoutParams = nullptr) - : ClickableItem(layoutParams), text_(text), smallText_(smallText), atlasImage_(ImageID::invalid()), iconImage_(ImageID::invalid()), centered_(false), highlighted_(false), selected_(selected) {} + : ClickableItem(layoutParams), text_(text), smallText_(smallText), image_(ImageID::invalid()) {} Choice(ImageID image, LayoutParams *layoutParams = nullptr) - : ClickableItem(layoutParams), atlasImage_(image), iconImage_(ImageID::invalid()), centered_(false), highlighted_(false), selected_(false) {} + : ClickableItem(layoutParams), image_(image), rightIconImage_(ImageID::invalid()) {} void Click() override; virtual void HighlightChanged(bool highlighted); @@ -703,7 +705,7 @@ public: centered_ = c; } virtual void SetIcon(ImageID iconImage) { - iconImage_ = iconImage; + rightIconImage_ = iconImage; } protected: @@ -713,14 +715,14 @@ protected: std::string text_; std::string smallText_; - ImageID atlasImage_; - ImageID iconImage_; // Only applies for text, non-centered + ImageID image_; // Centered if no text, on the left if text. + ImageID rightIconImage_ = ImageID::invalid(); // Shows in the right. Only used for the Gold icon on the main menu. Padding textPadding_; - bool centered_; - bool highlighted_; + bool centered_ = false; + bool highlighted_ = false; private: - bool selected_; + bool selected_ = false; }; // Different key handling. diff --git a/UI/MemStickScreen.cpp b/UI/MemStickScreen.cpp index 81d867957a..ffc38fe540 100644 --- a/UI/MemStickScreen.cpp +++ b/UI/MemStickScreen.cpp @@ -275,7 +275,7 @@ void MemStickScreen::CreateViews() { break; } - rightColumnItems->Add(new Button(confirmButtonText, confirmButtonImage))->OnClick.Handle(this, &MemStickScreen::OnConfirmClick); + rightColumnItems->Add(new UI::Choice(confirmButtonText, confirmButtonImage))->OnClick.Handle(this, &MemStickScreen::OnConfirmClick); rightColumnItems->Add(new Spacer(new LinearLayoutParams(FILL_PARENT, 12.0f, 0.0f))); if (!initialSetup_) {