Skip to content

Commit

Permalink
UI: Scale option text down when there's no space.
Browse files Browse the repository at this point in the history
This way, smaller or portrait screens just get smaller text, rather than
jumbled text going off the side.  Particularly important for some
translations.
  • Loading branch information
unknownbrackets committed Aug 7, 2016
1 parent df5377b commit 277983c
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 20 deletions.
56 changes: 44 additions & 12 deletions ext/native/ui/ui_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,14 @@ void PopupMultiChoice::Draw(UIContext &dc) {
if (!IsEnabled()) {
style = dc.theme->itemDisabledStyle;
}
Choice::Draw(dc);
int paddingX = 12;
dc.SetFontStyle(dc.theme->uiFont);

float ignore;
dc.MeasureText(dc.theme->uiFont, valueText_.c_str(), &textPadding_.right, &ignore, ALIGN_RIGHT | ALIGN_VCENTER);
textPadding_.right += paddingX;

Choice::Draw(dc);
dc.DrawText(valueText_.c_str(), bounds_.x2() - paddingX, bounds_.centerY(), style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER);
}

Expand Down Expand Up @@ -419,15 +424,22 @@ void PopupSliderChoice::Draw(UIContext &dc) {
if (!IsEnabled()) {
style = dc.theme->itemDisabledStyle;
}
Choice::Draw(dc);
int paddingX = 12;
dc.SetFontStyle(dc.theme->uiFont);

char temp[32];
if (zeroLabel_.size() && *value_ == 0) {
strcpy(temp, zeroLabel_.c_str());
} else {
sprintf(temp, fmt_, *value_);
}
dc.SetFontStyle(dc.theme->uiFont);
dc.DrawText(temp, bounds_.x2() - 12, bounds_.centerY(), style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER);

float ignore;
dc.MeasureText(dc.theme->uiFont, temp, &textPadding_.right, &ignore, ALIGN_RIGHT | ALIGN_VCENTER);
textPadding_.right += paddingX;

Choice::Draw(dc);
dc.DrawText(temp, bounds_.x2() - paddingX, bounds_.centerY(), style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER);
}

EventReturn PopupSliderChoiceFloat::HandleClick(EventParams &e) {
Expand All @@ -454,15 +466,22 @@ void PopupSliderChoiceFloat::Draw(UIContext &dc) {
if (!IsEnabled()) {
style = dc.theme->itemDisabledStyle;
}
Choice::Draw(dc);
int paddingX = 12;
dc.SetFontStyle(dc.theme->uiFont);

char temp[32];
if (zeroLabel_.size() && *value_ == 0.0f) {
strcpy(temp, zeroLabel_.c_str());
} else {
sprintf(temp, fmt_, *value_);
}
dc.SetFontStyle(dc.theme->uiFont);
dc.DrawText(temp, bounds_.x2() - 12, bounds_.centerY(), style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER);

float ignore;
dc.MeasureText(dc.theme->uiFont, temp, &textPadding_.right, &ignore, ALIGN_RIGHT | ALIGN_VCENTER);
textPadding_.right += paddingX;

Choice::Draw(dc);
dc.DrawText(temp, bounds_.x2() - paddingX, bounds_.centerY(), style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER);
}

EventReturn SliderPopupScreen::OnDecrease(EventParams &params) {
Expand Down Expand Up @@ -644,8 +663,14 @@ void PopupTextInputChoice::Draw(UIContext &dc) {
if (!IsEnabled()) {
style = dc.theme->itemDisabledStyle;
}
Choice::Draw(dc);
int paddingX = 12;
dc.SetFontStyle(dc.theme->uiFont);

float ignore;
dc.MeasureText(dc.theme->uiFont, value_->c_str(), &textPadding_.right, &ignore, ALIGN_RIGHT | ALIGN_VCENTER);
textPadding_.right += paddingX;

Choice::Draw(dc);
dc.DrawText(value_->c_str(), bounds_.x2() - 12, bounds_.centerY(), style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER);
}

Expand Down Expand Up @@ -683,12 +708,14 @@ void TextEditPopupScreen::OnCompleted(DialogResult result) {

void ChoiceWithValueDisplay::Draw(UIContext &dc) {
Style style = dc.theme->itemStyle;
std::ostringstream valueText;
Choice::Draw(dc);
if (!IsEnabled()) {
style = dc.theme->itemDisabledStyle;
}
int paddingX = 12;
dc.SetFontStyle(dc.theme->uiFont);

I18NCategory *category = GetI18NCategory(category_);

std::ostringstream valueText;
if (sValue_ != nullptr) {
if (category)
valueText << category->T(*sValue_);
Expand All @@ -698,7 +725,12 @@ void ChoiceWithValueDisplay::Draw(UIContext &dc) {
valueText << *iValue_;
}

dc.DrawText(valueText.str().c_str(), bounds_.x2() - 12, bounds_.centerY(), style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER);
float ignore;
dc.MeasureText(dc.theme->uiFont, valueText.str().c_str(), &textPadding_.right, &ignore, ALIGN_RIGHT | ALIGN_VCENTER);
textPadding_.right += paddingX;

Choice::Draw(dc);
dc.DrawText(valueText.str().c_str(), bounds_.x2() - paddingX, bounds_.centerY(), style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER);
}

} // namespace UI
42 changes: 35 additions & 7 deletions ext/native/ui/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ bool focusForced;
static recursive_mutex mutex_;

const float ITEM_HEIGHT = 64.f;
const float MIN_TEXT_SCALE = 0.8f;


struct DispatchQueueItem {
Expand Down Expand Up @@ -454,16 +455,29 @@ void Choice::Draw(UIContext &dc) {
if (atlasImage_ != -1) {
dc.Draw()->DrawImage(atlasImage_, bounds_.centerX(), bounds_.centerY(), 1.0f, style.fgColor, ALIGN_CENTER);
} else {
int paddingX = 12;
dc.SetFontStyle(dc.theme->uiFont);

const int paddingX = 12;
const float availWidth = bounds_.w - paddingX * 2 - textPadding_.horiz();
float scale = 1.0f;

float actualWidth, actualHeight;
dc.MeasureText(dc.theme->uiFont, text_.c_str(), &actualWidth, &actualHeight, ALIGN_VCENTER);
if (actualWidth > availWidth) {
scale = std::max(MIN_TEXT_SCALE, availWidth / actualWidth);
}

dc.SetFontScale(scale, scale);
if (centered_) {
dc.DrawText(text_.c_str(), bounds_.centerX(), bounds_.centerY(), style.fgColor, ALIGN_CENTER);
} else {
if (iconImage_ != -1) {
dc.Draw()->DrawImage(iconImage_, bounds_.x2() - 32 - paddingX, bounds_.centerY(), 0.5f, style.fgColor, ALIGN_CENTER);
}
dc.DrawText(text_.c_str(), bounds_.x + paddingX, bounds_.centerY(), style.fgColor, ALIGN_VCENTER);

dc.DrawText(text_.c_str(), bounds_.x + paddingX + textPadding_.left, bounds_.centerY(), style.fgColor, ALIGN_VCENTER);
}
dc.SetFontScale(1.0f, 1.0f);
}

if (selected_) {
Expand Down Expand Up @@ -537,18 +551,32 @@ EventReturn CheckBox::OnClicked(EventParams &e) {
}

void CheckBox::Draw(UIContext &dc) {
Style style = dc.theme->itemStyle;
if (!IsEnabled())
style = dc.theme->itemDisabledStyle;
dc.SetFontStyle(dc.theme->uiFont);

ClickableItem::Draw(dc);
int paddingX = 12;

int image = *toggle_ ? dc.theme->checkOn : dc.theme->checkOff;
float imageW, imageH;
dc.Draw()->MeasureImage(image, &imageW, &imageH);

Style style = dc.theme->itemStyle;
if (!IsEnabled())
style = dc.theme->itemDisabledStyle;
const int paddingX = 12;
// Padding right of the checkbox image too.
const float availWidth = bounds_.w - paddingX * 2 - imageW - paddingX;
float scale = 1.0f;

dc.SetFontStyle(dc.theme->uiFont);
float actualWidth, actualHeight;
dc.MeasureText(dc.theme->uiFont, text_.c_str(), &actualWidth, &actualHeight, ALIGN_VCENTER);
if (actualWidth > availWidth) {
scale = std::max(MIN_TEXT_SCALE, availWidth / actualWidth);
}

dc.SetFontScale(scale, scale);
dc.DrawText(text_.c_str(), bounds_.x + paddingX, bounds_.centerY(), style.fgColor, ALIGN_VCENTER);
dc.Draw()->DrawImage(image, bounds_.x2() - paddingX, bounds_.centerY(), 1.0f, style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER);
dc.SetFontScale(1.0f, 1.0f);
}

void Button::GetContentDimensions(const UIContext &dc, float &w, float &h) const {
Expand Down
21 changes: 20 additions & 1 deletion ext/native/ui/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,25 @@ struct Margins {
int8_t right;
};

struct Padding {
Padding() : top(0), bottom(0), left(0), right(0) {}
explicit Padding(float all) : top(all), bottom(all), left(all), right(all) {}
Padding(float horiz, float vert) : top(vert), bottom(vert), left(horiz), right(horiz) {}
Padding(float l, float t, float r, float b) : top(t), bottom(b), left(l), right(r) {}

float horiz() {
return left + right;
}
float vert() {
return top + bottom;
}

float top;
float bottom;
float left;
float right;
};

enum LayoutParamsType {
LP_PLAIN = 0,
LP_LINEAR = 1,
Expand Down Expand Up @@ -602,11 +621,11 @@ class Choice : public ClickableItem {
// hackery
virtual bool IsSticky() const { return false; }

int height_;
std::string text_;
std::string smallText_;
ImageID atlasImage_;
ImageID iconImage_; // Only applies for text, non-centered
Padding textPadding_;
bool centered_;
bool highlighted_;

Expand Down

0 comments on commit 277983c

Please sign in to comment.