mirror of
https://github.com/hrydgard/ppsspp.git
synced 2025-04-02 11:01:50 -04:00
UI: Allow measuring text with a scale.
This commit is contained in:
parent
2076d66663
commit
d6820f3cc3
7 changed files with 32 additions and 32 deletions
|
@ -284,7 +284,7 @@ void GameButton::Draw(UIContext &dc) {
|
||||||
title_ = ReplaceAll(title_, "\n", " ");
|
title_ = ReplaceAll(title_, "\n", " ");
|
||||||
}
|
}
|
||||||
|
|
||||||
dc.MeasureText(dc.GetFontStyle(), title_.c_str(), &tw, &th, 0);
|
dc.MeasureText(dc.GetFontStyle(), 1.0f, 1.0f, title_.c_str(), &tw, &th, 0);
|
||||||
|
|
||||||
int availableWidth = bounds_.w - 150;
|
int availableWidth = bounds_.w - 150;
|
||||||
float sineWidth = std::max(0.0f, (tw - availableWidth)) / 2.0f;
|
float sineWidth = std::max(0.0f, (tw - availableWidth)) / 2.0f;
|
||||||
|
@ -367,7 +367,7 @@ void DirButton::Draw(UIContext &dc) {
|
||||||
}
|
}
|
||||||
|
|
||||||
float tw, th;
|
float tw, th;
|
||||||
dc.MeasureText(dc.GetFontStyle(), text.c_str(), &tw, &th, 0);
|
dc.MeasureText(dc.GetFontStyle(), 1.0f, 1.0f, text.c_str(), &tw, &th, 0);
|
||||||
|
|
||||||
bool compact = bounds_.w < 180;
|
bool compact = bounds_.w < 180;
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ void OnScreenMessagesView::Draw(UIContext &dc) {
|
||||||
|
|
||||||
// Get height
|
// Get height
|
||||||
float w, h;
|
float w, h;
|
||||||
dc.MeasureText(dc.theme->uiFont, "Wg", &w, &h);
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, "Wg", &w, &h);
|
||||||
|
|
||||||
float y = 10.0f;
|
float y = 10.0f;
|
||||||
// Then draw them all.
|
// Then draw them all.
|
||||||
|
@ -27,7 +27,7 @@ void OnScreenMessagesView::Draw(UIContext &dc) {
|
||||||
if (alpha < 0.0) alpha = 0.0f;
|
if (alpha < 0.0) alpha = 0.0f;
|
||||||
// Messages that are wider than the screen are left-aligned instead of centered.
|
// Messages that are wider than the screen are left-aligned instead of centered.
|
||||||
float tw, th;
|
float tw, th;
|
||||||
dc.MeasureText(dc.theme->uiFont, iter->text.c_str(), &tw, &th);
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, iter->text.c_str(), &tw, &th);
|
||||||
float x = bounds_.centerX();
|
float x = bounds_.centerX();
|
||||||
int align = ALIGN_TOP | ALIGN_HCENTER;
|
int align = ALIGN_TOP | ALIGN_HCENTER;
|
||||||
if (tw > bounds_.w) {
|
if (tw > bounds_.w) {
|
||||||
|
|
|
@ -251,7 +251,7 @@ void SavedataButton::Draw(UIContext &dc) {
|
||||||
subtitle_ = CleanSaveString(savedata_title) + StringFromFormat(" (%d kB)", ginfo->gameSize / 1024);
|
subtitle_ = CleanSaveString(savedata_title) + StringFromFormat(" (%d kB)", ginfo->gameSize / 1024);
|
||||||
}
|
}
|
||||||
|
|
||||||
dc.MeasureText(dc.GetFontStyle(), title_.c_str(), &tw, &th, 0);
|
dc.MeasureText(dc.GetFontStyle(), 1.0f, 1.0f, title_.c_str(), &tw, &th, 0);
|
||||||
|
|
||||||
int availableWidth = bounds_.w - 150;
|
int availableWidth = bounds_.w - 150;
|
||||||
float sineWidth = std::max(0.0f, (tw - availableWidth)) / 2.0f;
|
float sineWidth = std::max(0.0f, (tw - availableWidth)) / 2.0f;
|
||||||
|
|
|
@ -127,36 +127,36 @@ void UIContext::SetFontStyle(const UI::FontStyle &fontStyle) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UIContext::MeasureText(const UI::FontStyle &style, const char *str, float *x, float *y, int align) const {
|
void UIContext::MeasureText(const UI::FontStyle &style, float scaleX, float scaleY, const char *str, float *x, float *y, int align) const {
|
||||||
MeasureTextCount(style, str, (int)strlen(str), x, y, align);
|
MeasureTextCount(style, scaleX, scaleY, str, (int)strlen(str), x, y, align);
|
||||||
}
|
}
|
||||||
|
|
||||||
void UIContext::MeasureTextCount(const UI::FontStyle &style, const char *str, int count, float *x, float *y, int align) const {
|
void UIContext::MeasureTextCount(const UI::FontStyle &style, float scaleX, float scaleY, const char *str, int count, float *x, float *y, int align) const {
|
||||||
if (!textDrawer_ || (align & FLAG_DYNAMIC_ASCII)) {
|
if (!textDrawer_ || (align & FLAG_DYNAMIC_ASCII)) {
|
||||||
float sizeFactor = (float)style.sizePts / 24.0f;
|
float sizeFactor = (float)style.sizePts / 24.0f;
|
||||||
Draw()->SetFontScale(fontScaleX_ * sizeFactor, fontScaleY_ * sizeFactor);
|
Draw()->SetFontScale(scaleX * sizeFactor, scaleY * sizeFactor);
|
||||||
Draw()->MeasureTextCount(style.atlasFont, str, count, x, y);
|
Draw()->MeasureTextCount(style.atlasFont, str, count, x, y);
|
||||||
} else {
|
} else {
|
||||||
textDrawer_->SetFont(style.fontName.c_str(), style.sizePts, style.flags);
|
textDrawer_->SetFont(style.fontName.c_str(), style.sizePts, style.flags);
|
||||||
textDrawer_->SetFontScale(fontScaleX_, fontScaleY_);
|
textDrawer_->SetFontScale(scaleX, scaleY);
|
||||||
textDrawer_->MeasureString(str, count, x, y);
|
textDrawer_->MeasureString(str, count, x, y);
|
||||||
textDrawer_->SetFont(fontStyle_->fontName.c_str(), fontStyle_->sizePts, fontStyle_->flags);
|
textDrawer_->SetFont(fontStyle_->fontName.c_str(), fontStyle_->sizePts, fontStyle_->flags);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UIContext::MeasureTextRect(const UI::FontStyle &style, const char *str, int count, const Bounds &bounds, float *x, float *y, int align) const {
|
void UIContext::MeasureTextRect(const UI::FontStyle &style, float scaleX, float scaleY, const char *str, int count, const Bounds &bounds, float *x, float *y, int align) const {
|
||||||
if ((align & FLAG_WRAP_TEXT) == 0) {
|
if ((align & FLAG_WRAP_TEXT) == 0) {
|
||||||
MeasureTextCount(style, str, count, x, y, align);
|
MeasureTextCount(style, scaleX, scaleY, str, count, x, y, align);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!textDrawer_ || (align & FLAG_DYNAMIC_ASCII)) {
|
if (!textDrawer_ || (align & FLAG_DYNAMIC_ASCII)) {
|
||||||
float sizeFactor = (float)style.sizePts / 24.0f;
|
float sizeFactor = (float)style.sizePts / 24.0f;
|
||||||
Draw()->SetFontScale(fontScaleX_ * sizeFactor, fontScaleY_ * sizeFactor);
|
Draw()->SetFontScale(scaleX * sizeFactor, scaleY * sizeFactor);
|
||||||
Draw()->MeasureTextRect(style.atlasFont, str, count, bounds, x, y, align);
|
Draw()->MeasureTextRect(style.atlasFont, str, count, bounds, x, y, align);
|
||||||
} else {
|
} else {
|
||||||
textDrawer_->SetFont(style.fontName.c_str(), style.sizePts, style.flags);
|
textDrawer_->SetFont(style.fontName.c_str(), style.sizePts, style.flags);
|
||||||
textDrawer_->SetFontScale(fontScaleX_, fontScaleY_);
|
textDrawer_->SetFontScale(scaleX, scaleY);
|
||||||
textDrawer_->MeasureStringRect(str, count, bounds, x, y, align);
|
textDrawer_->MeasureStringRect(str, count, bounds, x, y, align);
|
||||||
textDrawer_->SetFont(fontStyle_->fontName.c_str(), fontStyle_->sizePts, fontStyle_->flags);
|
textDrawer_->SetFont(fontStyle_->fontName.c_str(), fontStyle_->sizePts, fontStyle_->flags);
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,9 +59,9 @@ public:
|
||||||
void SetFontStyle(const UI::FontStyle &style);
|
void SetFontStyle(const UI::FontStyle &style);
|
||||||
const UI::FontStyle &GetFontStyle() { return *fontStyle_; }
|
const UI::FontStyle &GetFontStyle() { return *fontStyle_; }
|
||||||
void SetFontScale(float scaleX, float scaleY);
|
void SetFontScale(float scaleX, float scaleY);
|
||||||
void MeasureTextCount(const UI::FontStyle &style, const char *str, int count, float *x, float *y, int align = 0) const;
|
void MeasureTextCount(const UI::FontStyle &style, float scaleX, float scaleY, const char *str, int count, float *x, float *y, int align = 0) const;
|
||||||
void MeasureText(const UI::FontStyle &style, const char *str, float *x, float *y, int align = 0) const;
|
void MeasureText(const UI::FontStyle &style, float scaleX, float scaleY, const char *str, float *x, float *y, int align = 0) const;
|
||||||
void MeasureTextRect(const UI::FontStyle &style, const char *str, int count, const Bounds &bounds, float *x, float *y, int align = 0) const;
|
void MeasureTextRect(const UI::FontStyle &style, float scaleX, float scaleY, const char *str, int count, const Bounds &bounds, float *x, float *y, int align = 0) const;
|
||||||
void DrawText(const char *str, float x, float y, uint32_t color, int align = 0);
|
void DrawText(const char *str, float x, float y, uint32_t color, int align = 0);
|
||||||
void DrawTextShadow(const char *str, float x, float y, uint32_t color, int align = 0);
|
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 DrawTextRect(const char *str, const Bounds &bounds, uint32_t color, int align = 0);
|
||||||
|
|
|
@ -369,7 +369,7 @@ void PopupMultiChoice::Draw(UIContext &dc) {
|
||||||
dc.SetFontStyle(dc.theme->uiFont);
|
dc.SetFontStyle(dc.theme->uiFont);
|
||||||
|
|
||||||
float ignore;
|
float ignore;
|
||||||
dc.MeasureText(dc.theme->uiFont, valueText_.c_str(), &textPadding_.right, &ignore, ALIGN_RIGHT | ALIGN_VCENTER);
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, valueText_.c_str(), &textPadding_.right, &ignore, ALIGN_RIGHT | ALIGN_VCENTER);
|
||||||
textPadding_.right += paddingX;
|
textPadding_.right += paddingX;
|
||||||
|
|
||||||
Choice::Draw(dc);
|
Choice::Draw(dc);
|
||||||
|
@ -435,7 +435,7 @@ void PopupSliderChoice::Draw(UIContext &dc) {
|
||||||
}
|
}
|
||||||
|
|
||||||
float ignore;
|
float ignore;
|
||||||
dc.MeasureText(dc.theme->uiFont, temp, &textPadding_.right, &ignore, ALIGN_RIGHT | ALIGN_VCENTER);
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, temp, &textPadding_.right, &ignore, ALIGN_RIGHT | ALIGN_VCENTER);
|
||||||
textPadding_.right += paddingX;
|
textPadding_.right += paddingX;
|
||||||
|
|
||||||
Choice::Draw(dc);
|
Choice::Draw(dc);
|
||||||
|
@ -477,7 +477,7 @@ void PopupSliderChoiceFloat::Draw(UIContext &dc) {
|
||||||
}
|
}
|
||||||
|
|
||||||
float ignore;
|
float ignore;
|
||||||
dc.MeasureText(dc.theme->uiFont, temp, &textPadding_.right, &ignore, ALIGN_RIGHT | ALIGN_VCENTER);
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, temp, &textPadding_.right, &ignore, ALIGN_RIGHT | ALIGN_VCENTER);
|
||||||
textPadding_.right += paddingX;
|
textPadding_.right += paddingX;
|
||||||
|
|
||||||
Choice::Draw(dc);
|
Choice::Draw(dc);
|
||||||
|
@ -667,7 +667,7 @@ void PopupTextInputChoice::Draw(UIContext &dc) {
|
||||||
dc.SetFontStyle(dc.theme->uiFont);
|
dc.SetFontStyle(dc.theme->uiFont);
|
||||||
|
|
||||||
float ignore;
|
float ignore;
|
||||||
dc.MeasureText(dc.theme->uiFont, value_->c_str(), &textPadding_.right, &ignore, ALIGN_RIGHT | ALIGN_VCENTER);
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, value_->c_str(), &textPadding_.right, &ignore, ALIGN_RIGHT | ALIGN_VCENTER);
|
||||||
textPadding_.right += paddingX;
|
textPadding_.right += paddingX;
|
||||||
|
|
||||||
Choice::Draw(dc);
|
Choice::Draw(dc);
|
||||||
|
@ -726,7 +726,7 @@ void ChoiceWithValueDisplay::Draw(UIContext &dc) {
|
||||||
}
|
}
|
||||||
|
|
||||||
float ignore;
|
float ignore;
|
||||||
dc.MeasureText(dc.theme->uiFont, valueText.str().c_str(), &textPadding_.right, &ignore, ALIGN_RIGHT | ALIGN_VCENTER);
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, valueText.str().c_str(), &textPadding_.right, &ignore, ALIGN_RIGHT | ALIGN_VCENTER);
|
||||||
textPadding_.right += paddingX;
|
textPadding_.right += paddingX;
|
||||||
|
|
||||||
Choice::Draw(dc);
|
Choice::Draw(dc);
|
||||||
|
|
|
@ -443,7 +443,7 @@ void Choice::GetContentDimensions(const UIContext &dc, float &w, float &h) const
|
||||||
w = img.w;
|
w = img.w;
|
||||||
h = img.h;
|
h = img.h;
|
||||||
} else {
|
} else {
|
||||||
dc.MeasureText(dc.theme->uiFont, text_.c_str(), &w, &h);
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), &w, &h);
|
||||||
}
|
}
|
||||||
w += 24;
|
w += 24;
|
||||||
h += 16;
|
h += 16;
|
||||||
|
@ -485,7 +485,7 @@ void Choice::Draw(UIContext &dc) {
|
||||||
float scale = 1.0f;
|
float scale = 1.0f;
|
||||||
|
|
||||||
float actualWidth, actualHeight;
|
float actualWidth, actualHeight;
|
||||||
dc.MeasureText(dc.theme->uiFont, text_.c_str(), &actualWidth, &actualHeight, ALIGN_VCENTER);
|
dc.MeasureText(dc.theme->uiFont, scale, scale, text_.c_str(), &actualWidth, &actualHeight, ALIGN_VCENTER);
|
||||||
if (actualWidth > availWidth) {
|
if (actualWidth > availWidth) {
|
||||||
scale = std::max(MIN_TEXT_SCALE, availWidth / actualWidth);
|
scale = std::max(MIN_TEXT_SCALE, availWidth / actualWidth);
|
||||||
}
|
}
|
||||||
|
@ -541,7 +541,7 @@ void PopupHeader::Draw(UIContext &dc) {
|
||||||
|
|
||||||
float tw, th;
|
float tw, th;
|
||||||
dc.SetFontStyle(dc.theme->uiFont);
|
dc.SetFontStyle(dc.theme->uiFont);
|
||||||
dc.MeasureText(dc.GetFontStyle(), text_.c_str(), &tw, &th, 0);
|
dc.MeasureText(dc.GetFontStyle(), 1.0f, 1.0f, text_.c_str(), &tw, &th, 0);
|
||||||
|
|
||||||
float sineWidth = std::max(0.0f, (tw - availableWidth)) / 2.0f;
|
float sineWidth = std::max(0.0f, (tw - availableWidth)) / 2.0f;
|
||||||
|
|
||||||
|
@ -591,7 +591,7 @@ void CheckBox::Draw(UIContext &dc) {
|
||||||
float scale = 1.0f;
|
float scale = 1.0f;
|
||||||
|
|
||||||
float actualWidth, actualHeight;
|
float actualWidth, actualHeight;
|
||||||
dc.MeasureText(dc.theme->uiFont, text_.c_str(), &actualWidth, &actualHeight, ALIGN_VCENTER);
|
dc.MeasureText(dc.theme->uiFont, scale, scale, text_.c_str(), &actualWidth, &actualHeight, ALIGN_VCENTER);
|
||||||
if (actualWidth > availWidth) {
|
if (actualWidth > availWidth) {
|
||||||
scale = std::max(MIN_TEXT_SCALE, availWidth / actualWidth);
|
scale = std::max(MIN_TEXT_SCALE, availWidth / actualWidth);
|
||||||
}
|
}
|
||||||
|
@ -608,7 +608,7 @@ void Button::GetContentDimensions(const UIContext &dc, float &w, float &h) const
|
||||||
w = img->w;
|
w = img->w;
|
||||||
h = img->h;
|
h = img->h;
|
||||||
} else {
|
} else {
|
||||||
dc.MeasureText(dc.theme->uiFont, text_.c_str(), &w, &h);
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), &w, &h);
|
||||||
}
|
}
|
||||||
// Add some internal padding to not look totally ugly
|
// Add some internal padding to not look totally ugly
|
||||||
w += 16;
|
w += 16;
|
||||||
|
@ -625,7 +625,7 @@ void Button::Draw(UIContext &dc) {
|
||||||
// dc.Draw()->DrawImage4Grid(style.image, bounds_.x, bounds_.y, bounds_.x2(), bounds_.y2(), style.bgColor);
|
// dc.Draw()->DrawImage4Grid(style.image, bounds_.x, bounds_.y, bounds_.x2(), bounds_.y2(), style.bgColor);
|
||||||
dc.FillRect(style.background, bounds_);
|
dc.FillRect(style.background, bounds_);
|
||||||
float tw, th;
|
float tw, th;
|
||||||
dc.MeasureText(dc.theme->uiFont, text_.c_str(), &tw, &th);
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), &tw, &th);
|
||||||
if (tw > bounds_.w || imageID_ != -1) {
|
if (tw > bounds_.w || imageID_ != -1) {
|
||||||
dc.PushScissor(bounds_);
|
dc.PushScissor(bounds_);
|
||||||
}
|
}
|
||||||
|
@ -690,7 +690,7 @@ void TextView::GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz
|
||||||
bounds.h = vert.size == 0 ? 1000.f : vert.size;
|
bounds.h = vert.size == 0 ? 1000.f : vert.size;
|
||||||
}
|
}
|
||||||
ApplyBoundsBySpec(bounds, horiz, vert);
|
ApplyBoundsBySpec(bounds, horiz, vert);
|
||||||
dc.MeasureTextRect(small_ ? dc.theme->uiFontSmall : dc.theme->uiFont, text_.c_str(), (int)text_.length(), bounds, &w, &h, textAlign_);
|
dc.MeasureTextRect(small_ ? dc.theme->uiFontSmall : dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), (int)text_.length(), bounds, &w, &h, textAlign_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextView::Draw(UIContext &dc) {
|
void TextView::Draw(UIContext &dc) {
|
||||||
|
@ -749,7 +749,7 @@ void TextEdit::Draw(UIContext &dc) {
|
||||||
|
|
||||||
if (HasFocus()) {
|
if (HasFocus()) {
|
||||||
// Hack to find the caret position. Might want to find a better way...
|
// Hack to find the caret position. Might want to find a better way...
|
||||||
dc.MeasureTextCount(dc.theme->uiFont, text_.c_str(), caret_, &w, &h, ALIGN_VCENTER | ALIGN_LEFT);
|
dc.MeasureTextCount(dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), caret_, &w, &h, ALIGN_VCENTER | ALIGN_LEFT);
|
||||||
float caretX = w;
|
float caretX = w;
|
||||||
caretX += textX;
|
caretX += textX;
|
||||||
|
|
||||||
|
@ -763,7 +763,7 @@ void TextEdit::Draw(UIContext &dc) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEdit::GetContentDimensions(const UIContext &dc, float &w, float &h) const {
|
void TextEdit::GetContentDimensions(const UIContext &dc, float &w, float &h) const {
|
||||||
dc.MeasureText(dc.theme->uiFont, text_.size() ? text_.c_str() : "Wj", &w, &h);
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_.size() ? text_.c_str() : "Wj", &w, &h);
|
||||||
w += 2;
|
w += 2;
|
||||||
h += 2;
|
h += 2;
|
||||||
}
|
}
|
||||||
|
@ -932,7 +932,7 @@ void TextEdit::InsertAtCaret(const char *text) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProgressBar::GetContentDimensions(const UIContext &dc, float &w, float &h) const {
|
void ProgressBar::GetContentDimensions(const UIContext &dc, float &w, float &h) const {
|
||||||
dc.MeasureText(dc.theme->uiFont, " 100% ", &w, &h);
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, " 100% ", &w, &h);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProgressBar::Draw(UIContext &dc) {
|
void ProgressBar::Draw(UIContext &dc) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue