mirror of
https://github.com/Maschell/libgui-sdl.git
synced 2025-02-22 18:57:14 +01:00
Improve GuiText handling
This commit is contained in:
parent
7be3824fea
commit
eb5528e0cf
@ -38,4 +38,4 @@ add_library(${PROJECT_NAME}
|
|||||||
source/gui/input/ControllerManager.cpp
|
source/gui/input/ControllerManager.cpp
|
||||||
source/gui/system/SDLSystem.cpp
|
source/gui/system/SDLSystem.cpp
|
||||||
source/gui/video/SDL_FontCache.cpp
|
source/gui/video/SDL_FontCache.cpp
|
||||||
)
|
include/gui-sdl/gui/GuiFont.h source/gui/GuiFont.cpp)
|
||||||
|
22
include/gui-sdl/gui/GuiFont.h
Normal file
22
include/gui-sdl/gui/GuiFont.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <SDL2/SDL_rwops.h>
|
||||||
|
#include <map>
|
||||||
|
#include <gui/video/SDL_FontCache.h>
|
||||||
|
#include <gui/video/Renderer.h>
|
||||||
|
|
||||||
|
class GuiFont {
|
||||||
|
public:
|
||||||
|
GuiFont(const uint8_t *buffer, size_t buffer_size, Renderer *renderer);
|
||||||
|
~GuiFont();
|
||||||
|
|
||||||
|
FC_Font *getFont(int32_t size);
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::map<int32_t, FC_Font *> font_cache;
|
||||||
|
std::map<int32_t, TTF_Font *> ttf_font_cache;
|
||||||
|
SDL_RWops *rw;
|
||||||
|
Renderer *renderer;
|
||||||
|
};
|
@ -20,17 +20,31 @@
|
|||||||
#include <gui/GuiTextureData.h>
|
#include <gui/GuiTextureData.h>
|
||||||
#include <gui/video/SDL_FontCache.h>
|
#include <gui/video/SDL_FontCache.h>
|
||||||
#include <gui/GuiImage.h>
|
#include <gui/GuiImage.h>
|
||||||
|
#include <gui/GuiFont.h>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <SDL2/SDL_ttf.h>
|
#include <SDL2/SDL_ttf.h>
|
||||||
|
|
||||||
//!Display, manage, and manipulate text in the GUI
|
//!Display, manage, and manipulate text in the GUI
|
||||||
class GuiText : public GuiElement {
|
class GuiText : public GuiElement {
|
||||||
public:
|
public:
|
||||||
|
//!\param t Text
|
||||||
|
explicit GuiText(const std::string &t);
|
||||||
|
|
||||||
|
//!\param t Text
|
||||||
|
//!\param s Font size
|
||||||
|
GuiText(const std::string &t, int32_t s);
|
||||||
|
|
||||||
//!\param t Text
|
//!\param t Text
|
||||||
//!\param s Font size
|
//!\param s Font size
|
||||||
//!\param c Font color
|
//!\param c Font color
|
||||||
GuiText(const std::string &t, SDL_Color c, FC_Font *font);
|
GuiText(const std::string &t, int32_t s, SDL_Color c);
|
||||||
|
|
||||||
|
//!\param t Text
|
||||||
|
//!\param s Font size
|
||||||
|
//!\param c Font color
|
||||||
|
//!\param font Font
|
||||||
|
GuiText(const std::string &t, int32_t s, SDL_Color c, GuiFont *font);
|
||||||
|
|
||||||
~GuiText() override;
|
~GuiText() override;
|
||||||
|
|
||||||
void draw(Renderer *pVideo) override;
|
void draw(Renderer *pVideo) override;
|
||||||
@ -39,9 +53,15 @@ public:
|
|||||||
|
|
||||||
void setMaxWidth(float width);
|
void setMaxWidth(float width);
|
||||||
|
|
||||||
|
static void setPresetFont(GuiFont *f);
|
||||||
|
|
||||||
|
static void setPresets(int32_t sz, const SDL_Color & c);
|
||||||
|
|
||||||
|
void setFontSize(int32_t size);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
GuiImage texture;
|
GuiImage texture;
|
||||||
GuiTextureData* textureData = nullptr;
|
GuiTextureData *textureData = nullptr;
|
||||||
|
|
||||||
std::string text;
|
std::string text;
|
||||||
SDL_Color color;
|
SDL_Color color;
|
||||||
@ -53,4 +73,10 @@ protected:
|
|||||||
void updateSize();
|
void updateSize();
|
||||||
|
|
||||||
void updateTexture(Renderer *renderer);
|
void updateTexture(Renderer *renderer);
|
||||||
|
|
||||||
|
GuiFont *gFont = nullptr;
|
||||||
|
|
||||||
|
static GuiFont *presetFont;
|
||||||
|
static SDL_Color presetColor;
|
||||||
|
static int32_t presetSize;
|
||||||
};
|
};
|
||||||
|
46
source/gui/GuiFont.cpp
Normal file
46
source/gui/GuiFont.cpp
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#include <gui/GuiFont.h>
|
||||||
|
|
||||||
|
GuiFont::GuiFont(const uint8_t *buffer, size_t buffer_size, Renderer *renderer) {
|
||||||
|
this->rw = SDL_RWFromMem((void *) buffer, buffer_size);
|
||||||
|
this->renderer = renderer;
|
||||||
|
}
|
||||||
|
|
||||||
|
GuiFont::~GuiFont() {
|
||||||
|
for (auto const&[key, val] : font_cache) {
|
||||||
|
FC_FreeFont(val);
|
||||||
|
}
|
||||||
|
for (auto const&[key, val] : ttf_font_cache) {
|
||||||
|
TTF_CloseFont(val);
|
||||||
|
}
|
||||||
|
SDL_RWclose(rw);
|
||||||
|
}
|
||||||
|
|
||||||
|
FC_Font *GuiFont::getFont(int32_t size) {
|
||||||
|
if (font_cache.count(size) > 0) {
|
||||||
|
return font_cache[size];
|
||||||
|
}
|
||||||
|
|
||||||
|
TTF_Font *font = TTF_OpenFontRW(rw, 0, size);
|
||||||
|
if (!font) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
FC_Font *fc_font = FC_CreateFont();
|
||||||
|
if (!fc_font) {
|
||||||
|
TTF_CloseFont(font);
|
||||||
|
font = nullptr;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto res = FC_LoadFontFromTTF(fc_font, renderer->getRenderer(), font, {255, 255, 255, 255});
|
||||||
|
if (!res) {
|
||||||
|
TTF_CloseFont(font);
|
||||||
|
font = nullptr;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
font_cache[size] = fc_font;
|
||||||
|
ttf_font_cache[size] = font;
|
||||||
|
|
||||||
|
return fc_font;
|
||||||
|
}
|
@ -17,36 +17,41 @@
|
|||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <SDL2/SDL_surface.h>
|
#include <SDL2/SDL_surface.h>
|
||||||
#include <gui/GuiText.h>
|
#include <gui/GuiText.h>
|
||||||
#include <gui/system/libgui_log.h>
|
|
||||||
|
|
||||||
/**
|
GuiFont *GuiText::presetFont = nullptr;
|
||||||
* Constructor for the GuiText class.
|
int32_t GuiText::presetSize = 28;
|
||||||
*/
|
SDL_Color GuiText::presetColor = (SDL_Color) {
|
||||||
|
255, 255, 255, 255
|
||||||
|
};
|
||||||
|
|
||||||
GuiText::GuiText(const std::string& text, SDL_Color c, FC_Font* gFont) {
|
GuiText::GuiText(const std::string &t)
|
||||||
this->text = text;
|
: GuiText(t, presetSize) {
|
||||||
this->color = c;
|
}
|
||||||
this->fc_font = gFont;
|
|
||||||
this->doUpdateTexture = true;
|
GuiText::GuiText(const std::string &t, int32_t s)
|
||||||
this->texture.setParent(this);
|
: GuiText(t, s, presetColor) {
|
||||||
|
}
|
||||||
|
|
||||||
|
GuiText::GuiText(const std::string &t, int32_t s, SDL_Color c)
|
||||||
|
: GuiText(t, s, c, presetFont) {
|
||||||
|
}
|
||||||
|
|
||||||
|
GuiText::GuiText(const std::string &text, int32_t size, SDL_Color c, GuiFont *gFont) :
|
||||||
|
text(text),
|
||||||
|
color(c) {
|
||||||
|
this->gFont = gFont;
|
||||||
|
this->fc_font = gFont->getFont(size);
|
||||||
|
this->doUpdateTexture = true;
|
||||||
|
this->texture.setParent(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
GuiText::~GuiText() {
|
GuiText::~GuiText() {
|
||||||
delete textureData;
|
delete textureData;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GuiText::draw(Renderer *renderer) {
|
void GuiText::setFontSize(int32_t size) {
|
||||||
if (!this->isVisible()) {
|
this->fc_font = this->gFont->getFont(size);
|
||||||
return;
|
this->doUpdateTexture = true;
|
||||||
}
|
|
||||||
|
|
||||||
updateTexture(renderer);
|
|
||||||
|
|
||||||
texture.draw(renderer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GuiText::process() {
|
|
||||||
GuiElement::process();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GuiText::setMaxWidth(float width) {
|
void GuiText::setMaxWidth(float width) {
|
||||||
@ -85,7 +90,7 @@ void GuiText::updateTexture(Renderer *renderer) {
|
|||||||
SDL_RenderClear(renderer->getRenderer());
|
SDL_RenderClear(renderer->getRenderer());
|
||||||
|
|
||||||
// Draw text to texture
|
// Draw text to texture
|
||||||
FC_DrawColumn(fc_font, renderer->getRenderer(), 0, 0, maxWidth, text.c_str());
|
FC_DrawColumnColor(fc_font, renderer->getRenderer(), 0, 0, maxWidth, color, text.c_str());
|
||||||
|
|
||||||
// Restore render target
|
// Restore render target
|
||||||
SDL_SetRenderTarget(renderer->getRenderer(), nullptr);
|
SDL_SetRenderTarget(renderer->getRenderer(), nullptr);
|
||||||
@ -95,3 +100,26 @@ void GuiText::updateTexture(Renderer *renderer) {
|
|||||||
doUpdateTexture = false;
|
doUpdateTexture = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GuiText::draw(Renderer *renderer) {
|
||||||
|
if (!this->isVisible()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
updateTexture(renderer);
|
||||||
|
|
||||||
|
texture.draw(renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GuiText::process() {
|
||||||
|
GuiElement::process();
|
||||||
|
}
|
||||||
|
|
||||||
|
void GuiText::setPresets(int32_t sz, const SDL_Color &c) {
|
||||||
|
GuiText::presetSize = sz;
|
||||||
|
GuiText::presetColor = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GuiText::setPresetFont(GuiFont *f) {
|
||||||
|
GuiText::presetFont = f;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user