mirror of
https://github.com/Maschell/libgui-sdl.git
synced 2025-02-21 10:32:03 +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/system/SDLSystem.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/video/SDL_FontCache.h>
|
||||
#include <gui/GuiImage.h>
|
||||
#include <gui/GuiFont.h>
|
||||
#include <mutex>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
|
||||
//!Display, manage, and manipulate text in the GUI
|
||||
class GuiText : public GuiElement {
|
||||
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 s Font size
|
||||
//!\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;
|
||||
|
||||
void draw(Renderer *pVideo) override;
|
||||
@ -39,9 +53,15 @@ public:
|
||||
|
||||
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:
|
||||
GuiImage texture;
|
||||
GuiTextureData* textureData = nullptr;
|
||||
GuiTextureData *textureData = nullptr;
|
||||
|
||||
std::string text;
|
||||
SDL_Color color;
|
||||
@ -53,4 +73,10 @@ protected:
|
||||
void updateSize();
|
||||
|
||||
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 <SDL2/SDL_surface.h>
|
||||
#include <gui/GuiText.h>
|
||||
#include <gui/system/libgui_log.h>
|
||||
|
||||
/**
|
||||
* Constructor for the GuiText class.
|
||||
*/
|
||||
GuiFont *GuiText::presetFont = nullptr;
|
||||
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) {
|
||||
this->text = text;
|
||||
this->color = c;
|
||||
this->fc_font = gFont;
|
||||
this->doUpdateTexture = true;
|
||||
this->texture.setParent(this);
|
||||
GuiText::GuiText(const std::string &t)
|
||||
: GuiText(t, presetSize) {
|
||||
}
|
||||
|
||||
GuiText::GuiText(const std::string &t, int32_t s)
|
||||
: 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() {
|
||||
delete textureData;
|
||||
}
|
||||
|
||||
void GuiText::draw(Renderer *renderer) {
|
||||
if (!this->isVisible()) {
|
||||
return;
|
||||
}
|
||||
|
||||
updateTexture(renderer);
|
||||
|
||||
texture.draw(renderer);
|
||||
}
|
||||
|
||||
void GuiText::process() {
|
||||
GuiElement::process();
|
||||
void GuiText::setFontSize(int32_t size) {
|
||||
this->fc_font = this->gFont->getFont(size);
|
||||
this->doUpdateTexture = true;
|
||||
}
|
||||
|
||||
void GuiText::setMaxWidth(float width) {
|
||||
@ -85,7 +90,7 @@ void GuiText::updateTexture(Renderer *renderer) {
|
||||
SDL_RenderClear(renderer->getRenderer());
|
||||
|
||||
// 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
|
||||
SDL_SetRenderTarget(renderer->getRenderer(), nullptr);
|
||||
@ -95,3 +100,26 @@ void GuiText::updateTexture(Renderer *renderer) {
|
||||
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