ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
UIText.h
浏览该文件的文档.
1#pragma once
2#include "../Core/Core.h"
4#include <string>
5#include <SDL3_ttf/SDL_ttf.h>
6
7namespace Shit {
16 class SHIT_API SHIT_REFLECT(BlackList) UIText : public UIRendererComponent {
18 public:
19 enum class TextAnchor { Left, Center, Right };
20
21 UIText() = default;
22 UIText(const std::string& text, const std::string& fontPath, float fontSize);
23 ~UIText() override;
24
25 void onRender(const SDL_FRect& screenRect) override;
26 void onDestroy() override;
27
28 // --- getter & setter(变更后置 dirty) ---
29 const std::string& getText() const { return m_text; }
30 void setText(const std::string& text) { m_text = text; m_isDirty = true; }
31
32 const std::string& getFontPath() const { return m_fontPath; }
33 void setFontPath(const std::string& fontPath) { m_fontPath = fontPath; m_isDirty = true; }
34
35 float getFontSize() const { return m_fontSize; }
36 void setFontSize(float fontSize) { m_fontSize = fontSize; m_isDirty = true; }
37
38 const Color& getColor() const { return m_color; }
39 void setColor(const Color& color) { m_color = color; m_isDirty = true; }
40
41 TextAnchor getAnchor() const { return m_anchor; }
42 void setAnchor(TextAnchor anchor) { m_anchor = anchor; }
43
44 private:
45 void rebuildTexture();
46 TTF_Font* getLoadedFont();
47
48 SHIT_META(({.displayName = "Text", .tooltip = "文字内容"}))
49 std::string m_text;
50 SHIT_META(({.displayName = "Font Path", .tooltip = "字体文件路径"}))
51 std::string m_fontPath;
52 SHIT_META(({.displayName = "Font Size", .tooltip = "字号", .range = {1, 300}}))
53 float m_fontSize = 24.0f;
54 SHIT_META(({.displayName = "Color", .tooltip = "文字颜色"}))
55 Color m_color;
56 SHIT_META(({.displayName = "Anchor", .tooltip = "对齐方式(左/中/右)"}))
57 TextAnchor m_anchor = TextAnchor::Center;
58 SHIT_META(Disable)
59 SDL_Texture* m_cachedTexture = nullptr;
60 SHIT_META(Disable)
61 bool m_isDirty = true;
62 };
63}
#define SHIT_API
定义 Core.h:14
#define SHIT_REFLECT_BODY(Type)
定义 Macros.h:62
#define SHIT_REFLECT(Mode)
定义 Macros.h:59
#define SHIT_META(...)
定义 Macros.h:68
float getFontSize() const
定义 UIText.h:35
const Color & getColor() const
定义 UIText.h:38
UIText(const std::string &text, const std::string &fontPath, float fontSize)
~UIText() override
UIText()=default
const std::string & getText() const
定义 UIText.h:29
void setAnchor(TextAnchor anchor)
定义 UIText.h:42
void setColor(const Color &color)
定义 UIText.h:39
TextAnchor
定义 UIText.h:19
const std::string & getFontPath() const
定义 UIText.h:32
void onRender(const SDL_FRect &screenRect) override
子类实现:在给定屏幕矩形内绘制自身(通过 Renderer 静态 API 绘制)
void setFontPath(const std::string &fontPath)
定义 UIText.h:33
TextAnchor getAnchor() const
定义 UIText.h:41
void setFontSize(float fontSize)
定义 UIText.h:36
void setText(const std::string &text)
定义 UIText.h:30
void onDestroy() override
定义 AudioPlayer.h:10
@ Right
定义 KeyCode.h:97
@ Left
定义 KeyCode.h:98
RGBA 颜色(每通道 8 位)
定义 Core.h:26