1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-10-31 00:07:39 +02:00

Moved fonts handling from Graphics to RenderHandler class

This commit is contained in:
Ivan Savenko
2024-09-23 15:43:11 +00:00
parent 92c5fd4156
commit 248cff49a4
21 changed files with 132 additions and 101 deletions

View File

@@ -112,9 +112,10 @@ std::vector<std::string> BattleConsole::splitText(const std::string &text)
boost::split(lines, text, boost::is_any_of("\n")); boost::split(lines, text, boost::is_any_of("\n"));
const auto & font = GH.renderHandler().loadFont(FONT_SMALL);
for(const auto & line : lines) for(const auto & line : lines)
{ {
if (graphics->fonts[FONT_SMALL]->getStringWidth(text) < pos.w) if (font->getStringWidth(text) < pos.w)
{ {
output.push_back(line); output.push_back(line);
} }
@@ -1098,7 +1099,8 @@ void StackQueue::StackBox::setUnit(const battle::Unit * unit, size_t turn, std::
if(currentTurn && !owner->embedded) if(currentTurn && !owner->embedded)
{ {
std::string tmp = std::to_string(*currentTurn); std::string tmp = std::to_string(*currentTurn);
int len = graphics->fonts[FONT_SMALL]->getStringWidth(tmp); const auto & font = GH.renderHandler().loadFont(FONT_SMALL);
int len = font->getStringWidth(tmp);
roundRect->pos.w = len + 6; roundRect->pos.w = len + 6;
round->setText(tmp); round->setText(tmp);
} }

View File

@@ -17,7 +17,9 @@
#include "../render/Colors.h" #include "../render/Colors.h"
#include "../render/EFont.h" #include "../render/EFont.h"
#include "../render/IFont.h" #include "../render/IFont.h"
#include "../render/IRenderHandler.h"
#include "../gui/TextAlignment.h" #include "../gui/TextAlignment.h"
#include "../gui/CGuiHandler.h"
#include "../render/Graphics.h" #include "../render/Graphics.h"
BattleOverlayLogVisualizer::BattleOverlayLogVisualizer( BattleOverlayLogVisualizer::BattleOverlayLogVisualizer(
@@ -30,7 +32,8 @@ BattleOverlayLogVisualizer::BattleOverlayLogVisualizer(
void BattleOverlayLogVisualizer::drawText(BattleHex hex, int lineNumber, const std::string & text) void BattleOverlayLogVisualizer::drawText(BattleHex hex, int lineNumber, const std::string & text)
{ {
Point offset = owner.fieldController->hexPositionLocal(hex).topLeft() + Point(20, 20); Point offset = owner.fieldController->hexPositionLocal(hex).topLeft() + Point(20, 20);
int h = graphics->fonts[EFonts::FONT_TINY]->getLineHeight(); const auto & font = GH.renderHandler().loadFont(FONT_TINY);
int h = font->getLineHeight();
offset.y += h * lineNumber; offset.y += h * lineNumber;

View File

@@ -318,7 +318,8 @@ void BattleStacksController::showStackAmountBox(Canvas & canvas, const CStack *
boxPosition = owner.fieldController->hexPositionLocal(frontPos).center() + Point(-8, -14); boxPosition = owner.fieldController->hexPositionLocal(frontPos).center() + Point(-8, -14);
} }
Point textPosition = Point(amountBG->dimensions().x/2 + boxPosition.x, boxPosition.y + graphics->fonts[EFonts::FONT_TINY]->getLineHeight() - 6); const auto & font = GH.renderHandler().loadFont(FONT_TINY);
Point textPosition = Point(amountBG->dimensions().x/2 + boxPosition.x, boxPosition.y + font->getLineHeight() - 6);
canvas.draw(amountBG, boxPosition); canvas.draw(amountBG, boxPosition);
canvas.drawText(textPosition, EFonts::FONT_TINY, Colors::WHITE, ETextAlignment::TOPCENTER, TextOperations::formatMetric(stack->getCount(), 4)); canvas.drawText(textPosition, EFonts::FONT_TINY, Colors::WHITE, ETextAlignment::TOPCENTER, TextOperations::formatMetric(stack->getCount(), 4));

View File

@@ -192,7 +192,8 @@ void CGuiHandler::drawFPSCounter()
std::string fps = std::to_string(framerate().getFramerate())+" FPS"; std::string fps = std::to_string(framerate().getFramerate())+" FPS";
graphics->fonts[FONT_SMALL]->renderTextLeft(screen, fps, Colors::WHITE, Point(8 * scaling, screen->h-22 * scaling)); const auto & font = GH.renderHandler().loadFont(FONT_SMALL);
font->renderTextLeft(screen, fps, Colors::WHITE, Point(8 * scaling, screen->h-22 * scaling));
} }
bool CGuiHandler::amIGuiThread() bool CGuiHandler::amIGuiThread()

View File

@@ -19,6 +19,7 @@
#include "../gui/Shortcut.h" #include "../gui/Shortcut.h"
#include "../render/Graphics.h" #include "../render/Graphics.h"
#include "../render/IFont.h" #include "../render/IFont.h"
#include "../render/IRenderHandler.h"
#include "../widgets/CComponent.h" #include "../widgets/CComponent.h"
#include "../widgets/ComboBox.h" #include "../widgets/ComboBox.h"
#include "../widgets/Buttons.h" #include "../widgets/Buttons.h"
@@ -357,8 +358,9 @@ std::shared_ptr<CMultiLineLabel> InterfaceObjectConfigurable::buildMultiLineLabe
auto color = readColor(config["color"]); auto color = readColor(config["color"]);
auto text = readText(config["text"]); auto text = readText(config["text"]);
Rect rect = readRect(config["rect"]); Rect rect = readRect(config["rect"]);
const auto & fontPtr = GH.renderHandler().loadFont(font);
if(!config["adoptHeight"].isNull() && config["adoptHeight"].Bool()) if(!config["adoptHeight"].isNull() && config["adoptHeight"].Bool())
rect.h = graphics->fonts[font]->getLineHeight() * 2; rect.h = fontPtr->getLineHeight() * 2;
return std::make_shared<CMultiLineLabel>(rect, font, alignment, color, text); return std::make_shared<CMultiLineLabel>(rect, font, alignment, color, text);
} }

View File

@@ -256,15 +256,17 @@ void InfoCard::changeSelection()
if(!showChat) if(!showChat)
labelGroupPlayers->disable(); labelGroupPlayers->disable();
const auto & font = GH.renderHandler().loadFont(FONT_SMALL);
for(const auto & p : CSH->playerNames) for(const auto & p : CSH->playerNames)
{ {
int slotsUsed = labelGroupPlayers->currentSize(); int slotsUsed = labelGroupPlayers->currentSize();
Point labelPosition; Point labelPosition;
if(slotsUsed < 4) if(slotsUsed < 4)
labelPosition = Point(24, 285 + slotsUsed * graphics->fonts[FONT_SMALL]->getLineHeight()); // left column labelPosition = Point(24, 285 + slotsUsed * font->getLineHeight()); // left column
else else
labelPosition = Point(193, 285 + (slotsUsed - 4) * graphics->fonts[FONT_SMALL]->getLineHeight()); // right column labelPosition = Point(193, 285 + (slotsUsed - 4) * font->getLineHeight()); // right column
labelGroupPlayers->add(labelPosition.x, labelPosition.y, p.second.name); labelGroupPlayers->add(labelPosition.x, labelPosition.y, p.second.name);
} }
@@ -358,7 +360,8 @@ CChatBox::CChatBox(const Rect & rect)
pos += rect.topLeft(); pos += rect.topLeft();
setRedrawParent(true); setRedrawParent(true);
const int height = static_cast<int>(graphics->fonts[FONT_SMALL]->getLineHeight()); const auto & font = GH.renderHandler().loadFont(FONT_SMALL);
const int height = font->getLineHeight();
Rect textInputArea(1, rect.h - height, rect.w - 1, height); Rect textInputArea(1, rect.h - height, rect.w - 1, height);
Rect chatHistoryArea(3, 1, rect.w - 3, rect.h - height - 1); Rect chatHistoryArea(3, 1, rect.w - 3, rect.h - height - 1);
inputBackground = std::make_shared<TransparentFilledRectangle>(textInputArea, ColorRGBA(0,0,0,192)); inputBackground = std::make_shared<TransparentFilledRectangle>(textInputArea, ColorRGBA(0,0,0,192));

View File

@@ -19,6 +19,7 @@
#include "../gui/WindowHandler.h" #include "../gui/WindowHandler.h"
#include "../render/Graphics.h" #include "../render/Graphics.h"
#include "../render/IFont.h" #include "../render/IFont.h"
#include "../render/IRenderHandler.h"
#include "../media/ISoundPlayer.h" #include "../media/ISoundPlayer.h"
#include "../widgets/CComponent.h" #include "../widgets/CComponent.h"
#include "../widgets/ComboBox.h" #include "../widgets/ComboBox.h"
@@ -1034,12 +1035,14 @@ OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry(const PlayerSettings & S, con
labelPlayerNameEdit = std::make_shared<CTextInput>(Rect(6, 3, 95, 15), EFonts::FONT_SMALL, ETextAlignment::CENTER, false); labelPlayerNameEdit = std::make_shared<CTextInput>(Rect(6, 3, 95, 15), EFonts::FONT_SMALL, ETextAlignment::CENTER, false);
labelPlayerNameEdit->setText(name); labelPlayerNameEdit->setText(name);
} }
labelWhoCanPlay = std::make_shared<CMultiLineLabel>(Rect(6, 23, 45, (int)graphics->fonts[EFonts::FONT_TINY]->getLineHeight()*2), EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->arraytxt[206 + whoCanPlay]); const auto & font = GH.renderHandler().loadFont(FONT_SMALL);
labelWhoCanPlay = std::make_shared<CMultiLineLabel>(Rect(6, 23, 45, font->getLineHeight()*2), EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->arraytxt[206 + whoCanPlay]);
auto hasHandicap = [this](){ return s->handicap.startBonus.empty() && s->handicap.percentIncome == 100 && s->handicap.percentGrowth == 100; }; auto hasHandicap = [this](){ return s->handicap.startBonus.empty() && s->handicap.percentIncome == 100 && s->handicap.percentGrowth == 100; };
std::string labelHandicapText = hasHandicap() ? CGI->generaltexth->arraytxt[210] : MetaString::createFromTextID("vcmi.lobby.handicap").toString(); std::string labelHandicapText = hasHandicap() ? CGI->generaltexth->arraytxt[210] : MetaString::createFromTextID("vcmi.lobby.handicap").toString();
labelHandicap = std::make_shared<CMultiLineLabel>(Rect(57, 24, 47, (int)graphics->fonts[EFonts::FONT_TINY]->getLineHeight()*2), EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, labelHandicapText); labelHandicap = std::make_shared<CMultiLineLabel>(Rect(57, 24, 47, font->getLineHeight()*2), EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, labelHandicapText);
handicap = std::make_shared<LRClickableArea>(Rect(56, 24, 49, (int)graphics->fonts[EFonts::FONT_TINY]->getLineHeight()*2), [](){ handicap = std::make_shared<LRClickableArea>(Rect(56, 24, 49, font->getLineHeight()*2), [](){
if(!CSH->isHost()) if(!CSH->isHost())
return; return;

View File

@@ -17,8 +17,10 @@
#include "../render/Colors.h" #include "../render/Colors.h"
#include "../render/EFont.h" #include "../render/EFont.h"
#include "../render/IFont.h" #include "../render/IFont.h"
#include "../render/IRenderHandler.h"
#include "../render/Graphics.h" #include "../render/Graphics.h"
#include "../gui/TextAlignment.h" #include "../gui/TextAlignment.h"
#include "../gui/CGuiHandler.h"
MapOverlayLogVisualizer::MapOverlayLogVisualizer(Canvas & target, std::shared_ptr<MapViewModel> model) MapOverlayLogVisualizer::MapOverlayLogVisualizer(Canvas & target, std::shared_ptr<MapViewModel> model)
@@ -78,8 +80,10 @@ void MapOverlayLogVisualizer::drawText(
if(viewPort.isInside(pStart)) if(viewPort.isInside(pStart))
{ {
int w = graphics->fonts[EFonts::FONT_TINY]->getStringWidth(text); const auto & font = GH.renderHandler().loadFont(FONT_TINY);
int h = graphics->fonts[EFonts::FONT_TINY]->getLineHeight();
int w = font->getStringWidth(text);
int h = font->getLineHeight();
pStart.y += h * lineNumber; pStart.y += h * lineNumber;

View File

@@ -188,6 +188,8 @@ void MapViewCache::render(const std::shared_ptr<IMapRendererContext> & context,
if(context->showTextOverlay()) if(context->showTextOverlay())
{ {
const auto & font = GH.renderHandler().loadFont(FONT_TINY);
for(int y = dimensions.top(); y < dimensions.bottom(); ++y) for(int y = dimensions.top(); y < dimensions.bottom(); ++y)
{ {
for(int x = dimensions.left(); x < dimensions.right(); ++x) for(int x = dimensions.left(); x < dimensions.right(); ++x)
@@ -204,8 +206,6 @@ void MapViewCache::render(const std::shared_ptr<IMapRendererContext> & context,
else else
position.y -= targetRect.h / 4; position.y -= targetRect.h / 4;
const auto font = graphics->fonts[EFonts::FONT_TINY];
Point dimensions(font->getStringWidth(overlay), font->getLineHeight()); Point dimensions(font->getStringWidth(overlay), font->getLineHeight());
Rect textRect = Rect(position - dimensions / 2, dimensions).resize(2); Rect textRect = Rect(position - dimensions / 2, dimensions).resize(2);

View File

@@ -11,6 +11,7 @@
#include "Canvas.h" #include "Canvas.h"
#include "../gui/CGuiHandler.h" #include "../gui/CGuiHandler.h"
#include "../render/IRenderHandler.h"
#include "../render/IScreenHandler.h" #include "../render/IScreenHandler.h"
#include "../renderSDL/SDL_Extensions.h" #include "../renderSDL/SDL_Extensions.h"
#include "Colors.h" #include "Colors.h"
@@ -169,23 +170,27 @@ void Canvas::drawBorderDashed(const Rect & target, const ColorRGBA & color)
void Canvas::drawText(const Point & position, const EFonts & font, const ColorRGBA & colorDest, ETextAlignment alignment, const std::string & text ) void Canvas::drawText(const Point & position, const EFonts & font, const ColorRGBA & colorDest, ETextAlignment alignment, const std::string & text )
{ {
const auto & fontPtr = GH.renderHandler().loadFont(font);
switch (alignment) switch (alignment)
{ {
case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLeft (surface, text, colorDest, transformPos(position)); case ETextAlignment::TOPLEFT: return fontPtr->renderTextLeft (surface, text, colorDest, transformPos(position));
case ETextAlignment::TOPCENTER: return graphics->fonts[font]->renderTextCenter(surface, text, colorDest, transformPos(position)); case ETextAlignment::TOPCENTER: return fontPtr->renderTextCenter(surface, text, colorDest, transformPos(position));
case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextCenter(surface, text, colorDest, transformPos(position)); case ETextAlignment::CENTER: return fontPtr->renderTextCenter(surface, text, colorDest, transformPos(position));
case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextRight (surface, text, colorDest, transformPos(position)); case ETextAlignment::BOTTOMRIGHT: return fontPtr->renderTextRight (surface, text, colorDest, transformPos(position));
} }
} }
void Canvas::drawText(const Point & position, const EFonts & font, const ColorRGBA & colorDest, ETextAlignment alignment, const std::vector<std::string> & text ) void Canvas::drawText(const Point & position, const EFonts & font, const ColorRGBA & colorDest, ETextAlignment alignment, const std::vector<std::string> & text )
{ {
const auto & fontPtr = GH.renderHandler().loadFont(font);
switch (alignment) switch (alignment)
{ {
case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLinesLeft (surface, text, colorDest, transformPos(position)); case ETextAlignment::TOPLEFT: return fontPtr->renderTextLinesLeft (surface, text, colorDest, transformPos(position));
case ETextAlignment::TOPCENTER: return graphics->fonts[font]->renderTextLinesCenter(surface, text, colorDest, transformPos(position)); case ETextAlignment::TOPCENTER: return fontPtr->renderTextLinesCenter(surface, text, colorDest, transformPos(position));
case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextLinesCenter(surface, text, colorDest, transformPos(position)); case ETextAlignment::CENTER: return fontPtr->renderTextLinesCenter(surface, text, colorDest, transformPos(position));
case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextLinesRight (surface, text, colorDest, transformPos(position)); case ETextAlignment::BOTTOMRIGHT: return fontPtr->renderTextLinesRight (surface, text, colorDest, transformPos(position));
} }
} }

View File

@@ -15,7 +15,7 @@
struct SDL_Surface; struct SDL_Surface;
class IImage; class IImage;
enum EFonts : int; enum EFonts : int8_t;
enum class CanvasScalingPolicy enum class CanvasScalingPolicy
{ {

View File

@@ -9,7 +9,15 @@
*/ */
#pragma once #pragma once
enum EFonts : int enum EFonts : int8_t
{ {
FONT_BIG, FONT_CALLI, FONT_CREDITS, FONT_HIGH_SCORE, FONT_MEDIUM, FONT_SMALL, FONT_TIMES, FONT_TINY, FONT_VERD FONT_BIG,
FONT_CALLI,
FONT_CREDITS,
FONT_HIGH_SCORE,
FONT_MEDIUM,
FONT_SMALL,
FONT_TIMES,
FONT_TINY,
FONT_VERD
}; };

View File

@@ -24,20 +24,13 @@
#include "../renderSDL/CTrueTypeFont.h" #include "../renderSDL/CTrueTypeFont.h"
#include "../render/CAnimation.h" #include "../render/CAnimation.h"
#include "../render/IImage.h" #include "../render/IImage.h"
#include "../render/IRenderHandler.h"
#include "../gui/CGuiHandler.h"
#include "../lib/filesystem/Filesystem.h" #include "../lib/filesystem/Filesystem.h"
#include "../lib/filesystem/CBinaryReader.h" #include "../lib/filesystem/CBinaryReader.h"
#include "../../lib/json/JsonNode.h" #include "../../lib/json/JsonNode.h"
#include "../lib/modding/CModHandler.h" #include "../lib/modding/CModHandler.h"
#include "../lib/modding/ModScope.h" #include "../lib/modding/ModScope.h"
#include "CGameInfo.h"
#include "../lib/VCMI_Lib.h" #include "../lib/VCMI_Lib.h"
#include "../CCallback.h"
#include "../lib/texts/CGeneralTextHandler.h"
#include "../lib/vcmi_endian.h"
#include "../lib/CStopWatch.h"
#include "../lib/CHeroHandler.h" #include "../lib/CHeroHandler.h"
#include <SDL_surface.h> #include <SDL_surface.h>
@@ -127,7 +120,6 @@ void Graphics::initializeBattleGraphics()
} }
Graphics::Graphics() Graphics::Graphics()
{ {
loadFonts();
loadPaletteAndColors(); loadPaletteAndColors();
initializeBattleGraphics(); initializeBattleGraphics();
loadErmuToPicture(); loadErmuToPicture();
@@ -166,29 +158,6 @@ void Graphics::setPlayerFlagColor(SDL_Palette * targetPalette, PlayerColor playe
} }
} }
void Graphics::loadFonts()
{
const JsonNode config(JsonPath::builtin("config/fonts.json"));
const JsonVector & bmpConf = config["bitmap"].Vector();
const JsonNode & ttfConf = config["trueType"];
const JsonNode & hanConf = config["bitmapHan"];
assert(bmpConf.size() == FONTS_NUMBER);
for (size_t i=0; i<FONTS_NUMBER; i++)
{
std::string filename = bmpConf[i].String();
if (!hanConf[filename].isNull())
fonts[i] = std::make_shared<CBitmapHanFont>(hanConf[filename]);
else if (!ttfConf[filename].isNull()) // no ttf override
fonts[i] = std::make_shared<CTrueTypeFont>(ttfConf[filename]);
else
fonts[i] = std::make_shared<CBitmapFont>(filename);
}
}
void Graphics::loadErmuToPicture() void Graphics::loadErmuToPicture()
{ {
//loading ERMU to picture //loading ERMU to picture

View File

@@ -9,26 +9,11 @@
*/ */
#pragma once #pragma once
#include "../lib/GameConstants.h" #include "../lib/constants/NumericConstants.h"
#include "../lib/constants/EntityIdentifiers.h"
#include "../lib/Color.h" #include "../lib/Color.h"
#include "../lib/filesystem/ResourcePath.h"
VCMI_LIB_NAMESPACE_BEGIN
class CGHeroInstance;
class CGTownInstance;
class CHeroClass;
struct InfoAboutHero;
struct InfoAboutTown;
class CGObjectInstance;
class ObjectTemplate;
class EntityService;
class JsonNode;
VCMI_LIB_NAMESPACE_END
struct SDL_Palette; struct SDL_Palette;
class IFont;
/// Handles fonts, hero images, town images, various graphics /// Handles fonts, hero images, town images, various graphics
class Graphics class Graphics
@@ -36,13 +21,8 @@ class Graphics
void initializeBattleGraphics(); void initializeBattleGraphics();
void loadPaletteAndColors(); void loadPaletteAndColors();
void loadErmuToPicture(); void loadErmuToPicture();
void loadFonts();
public: public:
//Fonts
static const int FONTS_NUMBER = 9;
std::array< std::shared_ptr<IFont>, FONTS_NUMBER> fonts;
using PlayerPalette = std::array<ColorRGBA, 32>; using PlayerPalette = std::array<ColorRGBA, 32>;
//various graphics //various graphics

View File

@@ -17,9 +17,11 @@ VCMI_LIB_NAMESPACE_END
struct SDL_Surface; struct SDL_Surface;
class IFont;
class IImage; class IImage;
class CAnimation; class CAnimation;
enum class EImageBlitMode : uint8_t; enum class EImageBlitMode : uint8_t;
enum EFonts : int8_t;
class IRenderHandler : public boost::noncopyable class IRenderHandler : public boost::noncopyable
{ {
@@ -40,4 +42,7 @@ public:
/// Loads animation using given path /// Loads animation using given path
virtual std::shared_ptr<CAnimation> loadAnimation(const AnimationPath & path, EImageBlitMode mode) = 0; virtual std::shared_ptr<CAnimation> loadAnimation(const AnimationPath & path, EImageBlitMode mode) = 0;
/// Returns font with specified identifer
virtual std::shared_ptr<const IFont> loadFont(EFonts font) = 0;
}; };

View File

@@ -12,6 +12,8 @@
#include "SDLImage.h" #include "SDLImage.h"
#include "ImageScaled.h" #include "ImageScaled.h"
#include "CBitmapFont.h"
#include "CTrueTypeFont.h"
#include "../gui/CGuiHandler.h" #include "../gui/CGuiHandler.h"
@@ -335,3 +337,26 @@ void RenderHandler::onLibraryLoadingFinished(const Services * services)
addImageListEntries(services->spells()); addImageListEntries(services->spells());
addImageListEntries(services->skills()); addImageListEntries(services->skills());
} }
std::shared_ptr<const IFont> RenderHandler::loadFont(EFonts font)
{
if (fonts.count(font))
return fonts.at(font);
const int8_t index = static_cast<int8_t>(font);
const JsonNode config(JsonPath::builtin("config/fonts.json"));
const JsonVector & bmpConf = config["bitmap"].Vector();
const JsonNode & ttfConf = config["trueType"];
std::string filename = bmpConf[index].String();
std::shared_ptr<const IFont> loadedFont;
if (ttfConf[filename].isNull())
loadedFont = std::make_shared<CBitmapFont>(filename);
else
loadedFont = std::make_shared<CTrueTypeFont>(ttfConf[filename]);
fonts[font] = loadedFont;
return loadedFont;
}

View File

@@ -26,6 +26,7 @@ class RenderHandler : public IRenderHandler
std::map<AnimationPath, std::shared_ptr<CDefFile>> animationFiles; std::map<AnimationPath, std::shared_ptr<CDefFile>> animationFiles;
std::map<AnimationPath, AnimationLayoutMap> animationLayouts; std::map<AnimationPath, AnimationLayoutMap> animationLayouts;
std::map<ImageLocator, std::shared_ptr<ISharedImage>> imageFiles; std::map<ImageLocator, std::shared_ptr<ISharedImage>> imageFiles;
std::map<EFonts, std::shared_ptr<const IFont>> fonts;
std::shared_ptr<CDefFile> getAnimationFile(const AnimationPath & path); std::shared_ptr<CDefFile> getAnimationFile(const AnimationPath & path);
AnimationLayoutMap & getAnimationLayout(const AnimationPath & path); AnimationLayoutMap & getAnimationLayout(const AnimationPath & path);
@@ -59,4 +60,7 @@ public:
std::shared_ptr<CAnimation> loadAnimation(const AnimationPath & path, EImageBlitMode mode) override; std::shared_ptr<CAnimation> loadAnimation(const AnimationPath & path, EImageBlitMode mode) override;
std::shared_ptr<IImage> createImage(SDL_Surface * source) override; std::shared_ptr<IImage> createImage(SDL_Surface * source) override;
/// Returns font with specified identifer
std::shared_ptr<const IFont> loadFont(EFonts font) override;
}; };

View File

@@ -21,6 +21,7 @@
#include "../gui/Shortcut.h" #include "../gui/Shortcut.h"
#include "../render/Canvas.h" #include "../render/Canvas.h"
#include "../render/IFont.h" #include "../render/IFont.h"
#include "../render/IRenderHandler.h"
#include "../render/Graphics.h" #include "../render/Graphics.h"
#include "../windows/CMessage.h" #include "../windows/CMessage.h"
#include "../windows/InfoWindows.h" #include "../windows/InfoWindows.h"
@@ -95,9 +96,11 @@ void CComponent::init(ComponentType Type, ComponentSubType Subtype, std::optiona
max = 80; max = 80;
std::vector<std::string> textLines = CMessage::breakText(getSubtitle(), std::max<int>(max, pos.w), font); std::vector<std::string> textLines = CMessage::breakText(getSubtitle(), std::max<int>(max, pos.w), font);
const auto & fontPtr = GH.renderHandler().loadFont(font);
const int height = static_cast<int>(fontPtr->getLineHeight());
for(auto & line : textLines) for(auto & line : textLines)
{ {
int height = static_cast<int>(graphics->fonts[font]->getLineHeight());
auto label = std::make_shared<CLabel>(pos.w/2, pos.h + height/2, font, ETextAlignment::CENTER, Colors::WHITE, line); auto label = std::make_shared<CLabel>(pos.w/2, pos.h + height/2, font, ETextAlignment::CENTER, Colors::WHITE, line);
pos.h += height; pos.h += height;

View File

@@ -17,6 +17,7 @@
#include "../gui/Shortcut.h" #include "../gui/Shortcut.h"
#include "../render/Graphics.h" #include "../render/Graphics.h"
#include "../render/IFont.h" #include "../render/IFont.h"
#include "../render/IRenderHandler.h"
#include "../../lib/texts/TextOperations.h" #include "../../lib/texts/TextOperations.h"
@@ -190,8 +191,9 @@ void CTextInput::updateLabel()
std::string visibleText = getVisibleText(); std::string visibleText = getVisibleText();
label->alignment = originalAlignment; label->alignment = originalAlignment;
const auto & font = GH.renderHandler().loadFont(label->font);
while (graphics->fonts[label->font]->getStringWidth(visibleText) > pos.w) while (font->getStringWidth(visibleText) > pos.w)
{ {
label->alignment = ETextAlignment::CENTERRIGHT; label->alignment = ETextAlignment::CENTERRIGHT;
visibleText = visibleText.substr(TextOperations::getUnicodeCharacterSize(visibleText[0])); visibleText = visibleText.substr(TextOperations::getUnicodeCharacterSize(visibleText[0]));

View File

@@ -22,6 +22,7 @@
#include "../render/Canvas.h" #include "../render/Canvas.h"
#include "../render/Graphics.h" #include "../render/Graphics.h"
#include "../render/IFont.h" #include "../render/IFont.h"
#include "../render/IRenderHandler.h"
#include "../../lib/texts/TextOperations.h" #include "../../lib/texts/TextOperations.h"
@@ -56,8 +57,9 @@ CLabel::CLabel(int x, int y, EFonts Font, ETextAlignment Align, const ColorRGBA
if(alignment == ETextAlignment::TOPLEFT) // causes issues for MIDDLE if(alignment == ETextAlignment::TOPLEFT) // causes issues for MIDDLE
{ {
pos.w = (int)graphics->fonts[font]->getStringWidth(visibleText().c_str()); const auto & fontPtr = GH.renderHandler().loadFont(font);
pos.h = (int)graphics->fonts[font]->getLineHeight(); pos.w = fontPtr->getStringWidth(visibleText().c_str());
pos.h = fontPtr->getLineHeight();
} }
} }
@@ -114,8 +116,12 @@ void CLabel::setMaxWidth(int width)
void CLabel::trimText() void CLabel::trimText()
{ {
if(maxWidth > 0) if(maxWidth > 0)
while ((int)graphics->fonts[font]->getStringWidth(visibleText().c_str()) > maxWidth) {
const auto & fontPtr = GH.renderHandler().loadFont(font);
while (fontPtr->getStringWidth(visibleText().c_str()) > maxWidth)
TextOperations::trimRightUnicode(text); TextOperations::trimRightUnicode(text);
}
} }
void CLabel::setColor(const ColorRGBA & Color) void CLabel::setColor(const ColorRGBA & Color)
@@ -132,7 +138,8 @@ void CLabel::setColor(const ColorRGBA & Color)
size_t CLabel::getWidth() size_t CLabel::getWidth()
{ {
return graphics->fonts[font]->getStringWidth(visibleText()); const auto & fontPtr = GH.renderHandler().loadFont(font);
return fontPtr->getStringWidth(visibleText());
} }
CMultiLineLabel::CMultiLineLabel(Rect position, EFonts Font, ETextAlignment Align, const ColorRGBA & Color, const std::string & Text) : CMultiLineLabel::CMultiLineLabel(Rect position, EFonts Font, ETextAlignment Align, const ColorRGBA & Color, const std::string & Text) :
@@ -171,7 +178,7 @@ void CMultiLineLabel::setText(const std::string & Txt)
void CTextContainer::blitLine(Canvas & to, Rect destRect, std::string what) void CTextContainer::blitLine(Canvas & to, Rect destRect, std::string what)
{ {
const auto f = graphics->fonts[font]; const auto f = GH.renderHandler().loadFont(font);
Point where = destRect.topLeft(); Point where = destRect.topLeft();
const std::string delimiters = "{}"; const std::string delimiters = "{}";
auto delimitersCount = std::count_if(what.cbegin(), what.cend(), [&delimiters](char c) auto delimitersCount = std::count_if(what.cbegin(), what.cend(), [&delimiters](char c)
@@ -264,7 +271,7 @@ void CMultiLineLabel::showAll(Canvas & to)
{ {
CIntObject::showAll(to); CIntObject::showAll(to);
const auto f = graphics->fonts[font]; const auto & fontPtr = GH.renderHandler().loadFont(font);
// calculate which lines should be visible // calculate which lines should be visible
int totalLines = static_cast<int>(lines.size()); int totalLines = static_cast<int>(lines.size());
@@ -274,17 +281,17 @@ void CMultiLineLabel::showAll(Canvas & to)
if(beginLine < 0) if(beginLine < 0)
beginLine = 0; beginLine = 0;
else else
beginLine /= (int)f->getLineHeight(); beginLine /= fontPtr->getLineHeight();
if(endLine < 0) if(endLine < 0)
endLine = 0; endLine = 0;
else else
endLine /= (int)f->getLineHeight(); endLine /= fontPtr->getLineHeight();
endLine++; endLine++;
// and where they should be displayed // and where they should be displayed
Point lineStart = getTextLocation().topLeft() - visibleSize + Point(0, beginLine * (int)f->getLineHeight()); Point lineStart = getTextLocation().topLeft() - visibleSize + Point(0, beginLine * fontPtr->getLineHeight());
Point lineSize = Point(getTextLocation().w, (int)f->getLineHeight()); Point lineSize = Point(getTextLocation().w, fontPtr->getLineHeight());
CSDL_Ext::CClipRectGuard guard(to.getInternalSurface(), getTextLocation()); // to properly trim text that is too big to fit CSDL_Ext::CClipRectGuard guard(to.getInternalSurface(), getTextLocation()); // to properly trim text that is too big to fit
@@ -293,7 +300,7 @@ void CMultiLineLabel::showAll(Canvas & to)
if(!lines[i].empty()) //non-empty line if(!lines[i].empty()) //non-empty line
blitLine(to, Rect(lineStart, lineSize), lines[i]); blitLine(to, Rect(lineStart, lineSize), lines[i]);
lineStart.y += (int)f->getLineHeight(); lineStart.y += fontPtr->getLineHeight();
} }
} }
@@ -301,15 +308,15 @@ void CMultiLineLabel::splitText(const std::string & Txt, bool redrawAfter)
{ {
lines.clear(); lines.clear();
const auto f = graphics->fonts[font]; const auto & fontPtr = GH.renderHandler().loadFont(font);
int lineHeight = static_cast<int>(f->getLineHeight()); int lineHeight = static_cast<int>(fontPtr->getLineHeight());
lines = CMessage::breakText(Txt, pos.w, font); lines = CMessage::breakText(Txt, pos.w, font);
textSize.y = lineHeight * (int)lines.size(); textSize.y = lineHeight * (int)lines.size();
textSize.x = 0; textSize.x = 0;
for(const std::string & line : lines) for(const std::string & line : lines)
vstd::amax(textSize.x, f->getStringWidth(line.c_str())); vstd::amax(textSize.x, fontPtr->getStringWidth(line.c_str()));
if(redrawAfter) if(redrawAfter)
redraw(); redraw();
} }
@@ -322,7 +329,8 @@ Rect CMultiLineLabel::getTextLocation()
if(pos.h <= textSize.y) if(pos.h <= textSize.y)
return pos; return pos;
Point textSize(pos.w, (int)graphics->fonts[font]->getLineHeight() * (int)lines.size()); const auto & fontPtr = GH.renderHandler().loadFont(font);
Point textSize(pos.w, fontPtr->getLineHeight() * (int)lines.size());
Point textOffset(pos.w - textSize.x, pos.h - textSize.y); Point textOffset(pos.w - textSize.x, pos.h - textSize.y);
switch(alignment) switch(alignment)
@@ -421,11 +429,12 @@ void CTextBox::setText(const std::string & text)
label->pos.w = pos.w - 16; label->pos.w = pos.w - 16;
assert(label->pos.w > 0); assert(label->pos.w > 0);
label->setText(text); label->setText(text);
const auto & fontPtr = GH.renderHandler().loadFont(label->font);
OBJECT_CONSTRUCTION; OBJECT_CONSTRUCTION;
slider = std::make_shared<CSlider>(Point(pos.w - 16, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1), slider = std::make_shared<CSlider>(Point(pos.w - 16, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1),
label->pos.h, label->textSize.y, 0, Orientation::VERTICAL, CSlider::EStyle(sliderStyle)); label->pos.h, label->textSize.y, 0, Orientation::VERTICAL, CSlider::EStyle(sliderStyle));
slider->setScrollStep((int)graphics->fonts[label->font]->getLineHeight()); slider->setScrollStep(fontPtr->getLineHeight());
slider->setPanningStep(1); slider->setPanningStep(1);
slider->setScrollBounds(pos - slider->pos.topLeft()); slider->setScrollBounds(pos - slider->pos.topLeft());
} }

View File

@@ -70,6 +70,8 @@ std::vector<std::string> CMessage::breakText(std::string text, size_t maxLineWid
boost::algorithm::trim_right_if(text, boost::algorithm::is_any_of(std::string(" "))); boost::algorithm::trim_right_if(text, boost::algorithm::is_any_of(std::string(" ")));
const auto & fontPtr = GH.renderHandler().loadFont(font);
// each iteration generates one output line // each iteration generates one output line
while(text.length()) while(text.length())
{ {
@@ -83,7 +85,7 @@ std::vector<std::string> CMessage::breakText(std::string text, size_t maxLineWid
std::string printableString; std::string printableString;
// loops till line is full or end of text reached // loops till line is full or end of text reached
while(currPos < text.length() && text[currPos] != 0x0a && graphics->fonts[font]->getStringWidth(printableString) <= maxLineWidth) while(currPos < text.length() && text[currPos] != 0x0a && fontPtr->getStringWidth(printableString) <= maxLineWidth)
{ {
symbolSize = TextOperations::getUnicodeCharacterSize(text[currPos]); symbolSize = TextOperations::getUnicodeCharacterSize(text[currPos]);
@@ -186,9 +188,9 @@ std::string CMessage::guessHeader(const std::string & msg)
int CMessage::guessHeight(const std::string & txt, int width, EFonts font) int CMessage::guessHeight(const std::string & txt, int width, EFonts font)
{ {
const auto f = graphics->fonts[font]; const auto & fontPtr = GH.renderHandler().loadFont(font);
const auto lines = CMessage::breakText(txt, width, font); const auto lines = CMessage::breakText(txt, width, font);
size_t lineHeight = f->getLineHeight(); size_t lineHeight = fontPtr->getLineHeight();
return lineHeight * lines.size(); return lineHeight * lines.size();
} }