Add image support to UI::Choice.

This commit is contained in:
Henrik Rydgård 2021-09-19 18:45:15 +02:00
parent a5e7878f27
commit 74c842c951
3 changed files with 27 additions and 18 deletions

View file

@ -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);
}

View file

@ -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.

View file

@ -275,7 +275,7 @@ void MemStickScreen::CreateViews() {
break;
}
rightColumnItems->Add(new Button(confirmButtonText, confirmButtonImage))->OnClick.Handle<MemStickScreen>(this, &MemStickScreen::OnConfirmClick);
rightColumnItems->Add(new UI::Choice(confirmButtonText, confirmButtonImage))->OnClick.Handle<MemStickScreen>(this, &MemStickScreen::OnConfirmClick);
rightColumnItems->Add(new Spacer(new LinearLayoutParams(FILL_PARENT, 12.0f, 0.0f)));
if (!initialSetup_) {