Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: Scale option text down when there's no space #8898

Merged
merged 3 commits into from
Aug 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/native/gfx_es2/draw_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ void TextDrawer::WrapString(std::string &out, const char *str, float maxW) {

void TextDrawer::SetFontScale(float xscale, float yscale) {
fontScaleX_ = xscale;
fontScaleY_ = xscale;
fontScaleY_ = yscale;
}

void TextDrawer::DrawStringRect(DrawBuffer &target, const char *str, const Bounds &bounds, uint32_t color, int align) {
Expand Down
4 changes: 4 additions & 0 deletions ext/native/ui/ui_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ void UIContext::MeasureTextCount(const UI::FontStyle &style, const char *str, in
Draw()->SetFontScale(fontScaleX_ * sizeFactor, fontScaleY_ * sizeFactor);
Draw()->MeasureTextCount(style.atlasFont, str, count, x, y);
} else {
textDrawer_->SetFont(style.fontName.c_str(), style.sizePts, style.flags);
textDrawer_->SetFontScale(fontScaleX_, fontScaleY_);
textDrawer_->MeasureString(str, count, x, y);
textDrawer_->SetFont(fontStyle_->fontName.c_str(), fontStyle_->sizePts, fontStyle_->flags);
}
}

Expand All @@ -153,8 +155,10 @@ void UIContext::MeasureTextRect(const UI::FontStyle &style, const char *str, int
Draw()->SetFontScale(fontScaleX_ * sizeFactor, fontScaleY_ * sizeFactor);
Draw()->MeasureTextRect(style.atlasFont, str, count, bounds, x, y, align);
} else {
textDrawer_->SetFont(style.fontName.c_str(), style.sizePts, style.flags);
textDrawer_->SetFontScale(fontScaleX_, fontScaleY_);
textDrawer_->MeasureStringRect(str, count, bounds, x, y, align);
textDrawer_->SetFont(fontStyle_->fontName.c_str(), fontStyle_->sizePts, fontStyle_->flags);
}
}

Expand Down
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
28 changes: 27 additions & 1 deletion ext/native/ui/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,38 @@ struct Margins {
Margins(int8_t horiz, int8_t vert) : top(vert), bottom(vert), left(horiz), right(horiz) {}
Margins(int8_t l, int8_t t, int8_t r, int8_t b) : top(t), bottom(b), left(l), right(r) {}

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

int8_t top;
int8_t bottom;
int8_t left;
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 @@ -595,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
32 changes: 15 additions & 17 deletions ext/native/ui/viewgroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,20 +424,20 @@ void LinearLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec v
MeasureSpec v = vert;
if (v.type == UNSPECIFIED && measuredHeight_ != 0.0)
v = MeasureSpec(AT_MOST, measuredHeight_);
views_[i]->Measure(dc, MeasureSpec(UNSPECIFIED, measuredWidth_), v - (float)(margins.top + margins.bottom));
views_[i]->Measure(dc, MeasureSpec(UNSPECIFIED, measuredWidth_), v - (float)margins.vert());
} else if (orientation_ == ORIENT_VERTICAL) {
MeasureSpec h = horiz;
if (h.type == UNSPECIFIED && measuredWidth_ != 0) h = MeasureSpec(AT_MOST, measuredWidth_);
views_[i]->Measure(dc, h - (float)(margins.left + margins.right), MeasureSpec(UNSPECIFIED, measuredHeight_));
views_[i]->Measure(dc, h - (float)margins.horiz(), MeasureSpec(UNSPECIFIED, measuredHeight_));
}

float amount;
if (orientation_ == ORIENT_HORIZONTAL) {
amount = views_[i]->GetMeasuredWidth() + margins.left + margins.right;
maxOther = std::max(maxOther, views_[i]->GetMeasuredHeight() + margins.top + margins.bottom);
amount = views_[i]->GetMeasuredWidth() + margins.horiz();
maxOther = std::max(maxOther, views_[i]->GetMeasuredHeight() + margins.vert());
} else {
amount = views_[i]->GetMeasuredHeight() + margins.top + margins.bottom;
maxOther = std::max(maxOther, views_[i]->GetMeasuredWidth() + margins.left + margins.right);
amount = views_[i]->GetMeasuredHeight() + margins.vert();
maxOther = std::max(maxOther, views_[i]->GetMeasuredWidth() + margins.horiz());
}

sum += amount;
Expand Down Expand Up @@ -478,15 +478,14 @@ void LinearLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec v
Margins margins = defaultMargins_;
if (linLayoutParams->HasMargins())
margins = linLayoutParams->margins;
int marginSum = margins.left + margins.right;
MeasureSpec v = vert;
if (v.type == UNSPECIFIED && measuredHeight_ != 0.0)
v = MeasureSpec(AT_MOST, measuredHeight_);
MeasureSpec h(AT_MOST, unit * linLayoutParams->weight - marginSum);
MeasureSpec h(AT_MOST, unit * linLayoutParams->weight - margins.horiz());
if (horiz.type == EXACTLY) {
h.type = EXACTLY;
}
views_[i]->Measure(dc, h, v - (float)(margins.top + margins.bottom));
views_[i]->Measure(dc, h, v - (float)margins.vert());
usedWidth += views_[i]->GetMeasuredWidth();
}
}
Expand Down Expand Up @@ -517,15 +516,14 @@ void LinearLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec v
Margins margins = defaultMargins_;
if (linLayoutParams->HasMargins())
margins = linLayoutParams->margins;
int marginSum = margins.top + margins.bottom;
MeasureSpec h = horiz;
if (h.type == UNSPECIFIED && measuredWidth_ != 0.0)
h = MeasureSpec(AT_MOST, measuredWidth_);
MeasureSpec v(AT_MOST, unit * linLayoutParams->weight - marginSum);
MeasureSpec v(AT_MOST, unit * linLayoutParams->weight - margins.vert());
if (vert.type == EXACTLY) {
v.type = EXACTLY;
}
views_[i]->Measure(dc, h - (float)(margins.left + margins.right), v);
views_[i]->Measure(dc, h - (float)margins.horiz(), v);
usedHeight += views_[i]->GetMeasuredHeight();
}
}
Expand Down Expand Up @@ -569,10 +567,10 @@ void LinearLayout::Layout() {

if (orientation_ == ORIENT_HORIZONTAL) {
itemBounds.x = pos;
itemBounds.w = views_[i]->GetMeasuredWidth() + margins.left + margins.right;
itemBounds.w = views_[i]->GetMeasuredWidth() + margins.horiz();
} else {
itemBounds.y = pos;
itemBounds.h = views_[i]->GetMeasuredHeight() + margins.top + margins.bottom;
itemBounds.h = views_[i]->GetMeasuredHeight() + margins.vert();
}

Bounds innerBounds;
Expand Down Expand Up @@ -637,7 +635,7 @@ void ScrollView::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec ver
views_[0]->Measure(dc, MeasureSpec(UNSPECIFIED), MeasureSpec(UNSPECIFIED));
MeasureBySpec(layoutParams_->height, views_[0]->GetMeasuredHeight(), vert, &measuredHeight_);
} else {
views_[0]->Measure(dc, MeasureSpec(AT_MOST, measuredWidth_ - (margins.left + margins.right)), MeasureSpec(UNSPECIFIED));
views_[0]->Measure(dc, MeasureSpec(AT_MOST, measuredWidth_ - margins.horiz()), MeasureSpec(UNSPECIFIED));
MeasureBySpec(layoutParams_->width, views_[0]->GetMeasuredWidth(), horiz, &measuredWidth_);
}
if (orientation_ == ORIENT_VERTICAL && vert.type != EXACTLY) {
Expand Down Expand Up @@ -666,8 +664,8 @@ void ScrollView::Layout() {
margins = linLayoutParams->margins;
}

scrolled.w = views_[0]->GetMeasuredWidth() - (margins.left + margins.right);
scrolled.h = views_[0]->GetMeasuredHeight() - (margins.top + margins.bottom);
scrolled.w = views_[0]->GetMeasuredWidth() - margins.horiz();
scrolled.h = views_[0]->GetMeasuredHeight() - margins.vert();

float layoutScrollPos = ClampedScrollPos(scrollPos_);

Expand Down