Skip to content

Commit

Permalink
feat: font-scale
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah committed Mar 21, 2023
1 parent a200308 commit 69571a1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/framework/graphics/bitmapfont.cpp
Expand Up @@ -45,7 +45,7 @@ void BitmapFont::load(const OTMLNodePtr& fontNode)
m_glyphSpacing = fontNode->valueAt("spacing", Size(0));

// load font texture
m_texture = g_textures.getTexture(textureFile);
m_texture = g_textures.getTexture(textureFile, false);
if (!m_texture)
return;
m_texture->create();
Expand Down
4 changes: 2 additions & 2 deletions src/framework/graphics/texturemanager.cpp
Expand Up @@ -90,7 +90,7 @@ void TextureManager::liveReload()
}, 1000);
}

TexturePtr TextureManager::getTexture(const std::string& fileName)
TexturePtr TextureManager::getTexture(const std::string& fileName, bool smooth)
{
TexturePtr texture;

Expand Down Expand Up @@ -131,7 +131,7 @@ TexturePtr TextureManager::getTexture(const std::string& fileName)

if (texture) {
texture->setTime(stdext::time());
texture->setSmooth(true);
texture->setSmooth(smooth);
m_textures[filePath] = texture;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/framework/graphics/texturemanager.h
Expand Up @@ -35,8 +35,8 @@ class TextureManager
void clearCache();
void liveReload();

void preload(const std::string& fileName) { getTexture(fileName); }
TexturePtr getTexture(const std::string& fileName);
void preload(const std::string& fileName, bool smooth = true) { getTexture(fileName, smooth); }
TexturePtr getTexture(const std::string& fileName, bool smooth = true);
const TexturePtr& getEmptyTexture() { return m_emptyTexture; }

private:
Expand Down
3 changes: 3 additions & 0 deletions src/framework/ui/uiwidget.h
Expand Up @@ -530,6 +530,8 @@ class UIWidget : public LuaObject

BitmapFontPtr m_font;

float m_fontScale{ 1.f };

public:
void resizeToText();
void clearText() { setText(""); }
Expand All @@ -543,6 +545,7 @@ class UIWidget : public LuaObject
void setTextVerticalAutoResize(bool textAutoResize) { setProp(PropTextVerticalAutoResize, textAutoResize); updateText(); }
void setTextOnlyUpperCase(bool textOnlyUpperCase) { setProp(PropTextOnlyUpperCase, textOnlyUpperCase); setText(m_text); }
void setFont(const std::string_view fontName);
void setFontScale(float scale) { m_fontScale = scale; m_textCachedScreenCoords = {}; }

std::string getText() { return m_text; }
std::string getDrawText() { return m_drawText; }
Expand Down
12 changes: 10 additions & 2 deletions src/framework/ui/uiwidgettext.cpp
Expand Up @@ -89,6 +89,8 @@ void UIWidget::parseTextStyle(const OTMLNodePtr& styleNode)
setTextOnlyUpperCase(node->value<bool>());
else if (node->tag() == "font")
setFont(node->value());
else if (node->tag() == "font-scale")
m_fontScale = node->value<float>();
}
}

Expand All @@ -100,12 +102,18 @@ void UIWidget::drawText(const Rect& screenCoords)
if (screenCoords != m_textCachedScreenCoords) {
m_textCachedScreenCoords = screenCoords;

auto coords = Rect(screenCoords.topLeft(), screenCoords.bottomRight());
coords.translate(m_textOffset);
auto textOffset = m_textOffset;
textOffset.scale(m_fontScale);

auto coords = Rect(screenCoords.topLeft().scale(m_fontScale), screenCoords.bottomRight().scale(m_fontScale));
coords.translate(textOffset);

m_font->fillTextCoords(m_coordsBuffer, m_text, m_textSize, m_textAlign, coords, m_glyphsPositionsCache);
}

g_drawPool.scale(m_fontScale);
g_drawPool.addTexturedCoordsBuffer(m_font->getTexture(), m_coordsBuffer, m_color);
g_drawPool.scale(1.f); // reset scale
}

void UIWidget::onTextChange(const std::string_view text, const std::string_view oldText)
Expand Down
8 changes: 5 additions & 3 deletions src/framework/util/point.h
Expand Up @@ -44,9 +44,11 @@ class TPoint
TPoint<T> translated(T dx, T dy) const { TPoint<T> point = *this; point.x += dx; point.y += dy; return point; }

TPoint<T> scale(float v) {
float factor = (1.f - (1.f / v));
x -= x * factor;
y -= y * factor;
if (v != 1.f) {
float factor = (1.f - (1.f / v));
x -= x * factor;
y -= y * factor;
}
return *this;
}

Expand Down

0 comments on commit 69571a1

Please sign in to comment.