mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-11-29 09:13:39 +01:00
Unify CalculateTextScale
This commit is contained in:
1 parent
9bef58b7f5
commit
4108dcec23
4 files changed
+32
-33
No files matched your search
@@ -290,6 +290,28 @@ void UIContext::DrawTextRect(const char *str, const Bounds &bounds, uint32_t col
|
||||
RebindTexture();
|
||||
}
|
||||
|
||||
static constexpr float MIN_TEXT_SCALE = 0.7f;
|
||||
|
||||
float UIContext::CalculateTextScale(const char *text, float availWidth, float availHeight) const {
|
||||
float actualWidth, actualHeight;
|
||||
Bounds availBounds(0, 0, availWidth, availHeight);
|
||||
MeasureTextRect(theme->uiFont, 1.0f, 1.0f, text, (int)strlen(text), availBounds, &actualWidth, &actualHeight, ALIGN_VCENTER);
|
||||
if (actualWidth > availWidth) {
|
||||
return std::max(MIN_TEXT_SCALE, availWidth / actualWidth);
|
||||
}
|
||||
return 1.0f;
|
||||
}
|
||||
|
||||
void UIContext::DrawTextRectSqueeze(const char *str, const Bounds &bounds, uint32_t color, int align) {
|
||||
float origScaleX = fontScaleX_;
|
||||
float origScaleY = fontScaleY_;
|
||||
float scale = CalculateTextScale(str, bounds.w / origScaleX, bounds.h / origScaleY);
|
||||
SetFontScale(scale * origScaleX, scale * origScaleY);
|
||||
Bounds textBounds(bounds.x, bounds.y, bounds.w, bounds.h);
|
||||
DrawTextRect(str, textBounds, color, align);
|
||||
SetFontScale(origScaleX, origScaleY);
|
||||
}
|
||||
|
||||
void UIContext::DrawTextShadowRect(const char *str, const Bounds &bounds, uint32_t color, int align) {
|
||||
uint32_t alpha = (color >> 1) & 0xFF000000;
|
||||
Bounds shadowBounds(bounds.x+2, bounds.y+2, bounds.w, bounds.h);
|
||||
|
||||
@@ -89,6 +89,11 @@ public:
|
||||
void DrawTextShadow(const char *str, float x, float y, uint32_t color, int align = 0);
|
||||
void DrawTextRect(const char *str, const Bounds &bounds, uint32_t color, int align = 0);
|
||||
void DrawTextShadowRect(const char *str, const Bounds &bounds, uint32_t color, int align = 0);
|
||||
// Will squeeze the text into the bounds if needed.
|
||||
void DrawTextRectSqueeze(const char *str, const Bounds &bounds, uint32_t color, int align = 0);
|
||||
|
||||
float CalculateTextScale(const char *text, float availWidth, float availHeight) const;
|
||||
|
||||
void FillRect(const UI::Drawable &drawable, const Bounds &bounds);
|
||||
void DrawRectDropShadow(const Bounds &bounds, float radius, float alpha, uint32_t color = 0);
|
||||
void DrawImageVGradient(ImageID image, uint32_t color1, uint32_t color2, const Bounds &bounds);
|
||||
|
||||
+5
-30
@@ -460,7 +460,7 @@ void Choice::GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz,
|
||||
}
|
||||
if (horiz.type != EXACTLY && layoutParams_->width > 0.0f && availWidth > layoutParams_->width)
|
||||
availWidth = layoutParams_->width;
|
||||
float scale = CalculateTextScale(dc, availWidth);
|
||||
float scale = dc.CalculateTextScale(text_.c_str(), availWidth, bounds_.h);
|
||||
Bounds availBounds(0, 0, availWidth, vert.size);
|
||||
float textW = 0.0f, textH = 0.0f;
|
||||
dc.MeasureTextRect(dc.theme->uiFont, scale, scale, text_.c_str(), (int)text_.size(), availBounds, &textW, &textH, FLAG_WRAP_TEXT);
|
||||
@@ -473,16 +473,6 @@ void Choice::GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz,
|
||||
h = std::max(h, ITEM_HEIGHT);
|
||||
}
|
||||
|
||||
float Choice::CalculateTextScale(const UIContext &dc, float availWidth) const {
|
||||
float actualWidth, actualHeight;
|
||||
Bounds availBounds(0, 0, availWidth, bounds_.h);
|
||||
dc.MeasureTextRect(dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), (int)text_.size(), availBounds, &actualWidth, &actualHeight, drawTextFlags_);
|
||||
if (actualWidth > availWidth) {
|
||||
return std::max(MIN_TEXT_SCALE, availWidth / actualWidth);
|
||||
}
|
||||
return 1.0f;
|
||||
}
|
||||
|
||||
void Choice::Draw(UIContext &dc) {
|
||||
Style style = dc.theme->itemStyle;
|
||||
if (HasFocus()) style = dc.theme->itemFocusedStyle;
|
||||
@@ -508,18 +498,15 @@ void Choice::Draw(UIContext &dc) {
|
||||
dc.Draw()->DrawImage(image_, bounds_.x + 6, bounds_.centerY(), 1.0f, style.fgColor, 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 | drawTextFlags_);
|
||||
dc.DrawTextRectSqueeze(text_.c_str(), bounds_, style.fgColor, ALIGN_CENTER | FLAG_WRAP_TEXT | drawTextFlags_);
|
||||
} else {
|
||||
if (rightIconImage_.isValid()) {
|
||||
uint32_t col = rightIconKeepColor_ ? 0xffffffff : style.fgColor; // Don't apply theme to gold icon
|
||||
dc.Draw()->DrawImageRotated(rightIconImage_, bounds_.x2() - 32 - paddingX, bounds_.centerY(), rightIconScale_, rightIconRot_, col, rightIconFlipH_);
|
||||
}
|
||||
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 | drawTextFlags_);
|
||||
dc.DrawTextRectSqueeze(text_.c_str(), textBounds, style.fgColor, ALIGN_VCENTER | FLAG_WRAP_TEXT | drawTextFlags_);
|
||||
}
|
||||
dc.SetFontScale(1.0f, 1.0f);
|
||||
}
|
||||
@@ -783,10 +770,8 @@ void CheckBox::Draw(UIContext &dc) {
|
||||
const float availWidth = bounds_.w - paddingX * 2 - imageW - paddingX;
|
||||
|
||||
if (!text_.empty()) {
|
||||
float scale = CalculateTextScale(dc, availWidth);
|
||||
dc.SetFontScale(scale, scale);
|
||||
Bounds textBounds(bounds_.x + paddingX, bounds_.y, availWidth, bounds_.h);
|
||||
dc.DrawTextRect(text_.c_str(), textBounds, style.fgColor, ALIGN_VCENTER | FLAG_WRAP_TEXT);
|
||||
dc.DrawTextRectSqueeze(text_.c_str(), textBounds, style.fgColor, ALIGN_VCENTER | FLAG_WRAP_TEXT);
|
||||
}
|
||||
dc.Draw()->DrawImage(image, bounds_.x2() - paddingX, bounds_.centerY(), 1.0f, style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER);
|
||||
dc.SetFontScale(1.0f, 1.0f);
|
||||
@@ -801,16 +786,6 @@ std::string CheckBox::DescribeText() const {
|
||||
return text;
|
||||
}
|
||||
|
||||
float CheckBox::CalculateTextScale(const UIContext &dc, float availWidth) const {
|
||||
float actualWidth, actualHeight;
|
||||
Bounds availBounds(0, 0, availWidth, bounds_.h);
|
||||
dc.MeasureTextRect(dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), (int)text_.size(), availBounds, &actualWidth, &actualHeight, ALIGN_VCENTER);
|
||||
if (actualWidth > availWidth) {
|
||||
return std::max(MIN_TEXT_SCALE, availWidth / actualWidth);
|
||||
}
|
||||
return 1.0f;
|
||||
}
|
||||
|
||||
void CheckBox::GetContentDimensions(const UIContext &dc, float &w, float &h) const {
|
||||
ImageID image = Toggled() ? dc.theme->checkOn : dc.theme->checkOff;
|
||||
if (imageID_.isValid()) {
|
||||
@@ -838,7 +813,7 @@ void CheckBox::GetContentDimensions(const UIContext &dc, float &w, float &h) con
|
||||
}
|
||||
|
||||
if (!text_.empty()) {
|
||||
float scale = CalculateTextScale(dc, availWidth);
|
||||
float scale = dc.CalculateTextScale(text_.c_str(), availWidth, bounds_.h);
|
||||
|
||||
float actualWidth, actualHeight;
|
||||
Bounds availBounds(0, 0, availWidth, bounds_.h);
|
||||
|
||||
@@ -736,7 +736,6 @@ public:
|
||||
protected:
|
||||
// hackery
|
||||
virtual bool IsSticky() const { return false; }
|
||||
virtual float CalculateTextScale(const UIContext &dc, float availWidth) const;
|
||||
|
||||
std::string text_;
|
||||
std::string smallText_;
|
||||
@@ -859,8 +858,6 @@ public:
|
||||
virtual bool Toggled() const;
|
||||
|
||||
protected:
|
||||
float CalculateTextScale(const UIContext &dc, float availWidth) const;
|
||||
|
||||
bool *toggle_;
|
||||
std::string text_;
|
||||
std::string smallText_;
|
||||
|
||||
Reference in new issue
Block a user