ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
UITextInput.h
浏览该文件的文档.
1#pragma once
2#include "../Core/Core.h"
3#include "../Input/KeyCode.h"
5#include <SDL3/SDL_rect.h>
6#include <string>
7#include <cstdint>
8
9struct TTF_Font;
10
11namespace Shit {
27 public:
28 UITextInput() = default;
29 ~UITextInput() override;
30
31 // --- 文本内容 ---
32 const std::string& getText() const { return m_text; }
33 virtual void setText(const std::string& text);
34
35 // --- 占位符 ---
36 const std::string& getPlaceholder() const { return m_placeholder; }
37 void setPlaceholder(const std::string& placeholder) { m_placeholder = placeholder; }
38
39 // --- 字体(与 UIText 接口一致,按 path+size 缓存)---
40 const std::string& getFontPath() const { return m_fontPath; }
41 void setFontPath(const std::string& fontPath) { m_fontPath = fontPath; m_isDirty = true; }
42
43 float getFontSize() const { return m_fontSize; }
44 void setFontSize(float fontSize) { m_fontSize = fontSize; m_isDirty = true; }
45
46 float getFontHeight() const { return m_fontHeight; }
47
48 const Color& getTextColor() const { return m_textColor; }
49 void setTextColor(const Color& color) { m_textColor = color; m_isDirty = true; }
50
51 const Color& getPlaceholderColor() const { return m_placeholderColor; }
52 void setPlaceholderColor(const Color& color) { m_placeholderColor = color; }
53
54 const Color& getCursorColor() const { return m_cursorColor; }
55 void setCursorColor(const Color& color) { m_cursorColor = color; }
56
57 const Color& getSelectionColor() const { return m_selectionColor; }
58 void setSelectionColor(const Color& color) { m_selectionColor = color; }
59
60 // --- 状态查询 ---
61 bool isFocused() const { return m_isFocused; }
62 bool isMultiline() const { return m_isMultiline; }
63
64 // --- 由 TextInputGate 转发的 SDL 文本事件 ---
65 void onTextInput(const char* utf8Text);
66 void onTextEditing(const char* utf8Text, int start, int length);
67
69 void setFocused(bool focused);
70
71 protected:
72 // 子类覆写:把当前"已提交文本 + 光标布局"画到给定矩形内
73 void onRender(const SDL_FRect& screenRect) override = 0;
74
77 virtual bool onKeyDown(KeyCode key, bool shift, bool ctrl);
78
80 virtual void insertText(const std::string& utf8);
81
83 virtual void insertNewline();
84
86 size_t getCharacterCount() const;
87
89 void markDirty() { m_isDirty = true; }
90
92 TTF_Font* acquireFont();
93
94 SHIT_META(Disable)
95 bool m_isFocused = false;
96 SHIT_META(Disable)
97 bool m_isMultiline = false;
98 SHIT_META(({.displayName = "Text", .tooltip = "文本内容"}))
99 std::string m_text;
100 SHIT_META(({.displayName = "Placeholder", .tooltip = "占位符文字"}))
101 std::string m_placeholder;
102 SHIT_META(({.displayName = "Font Path", .tooltip = "字体文件路径"}))
103 std::string m_fontPath;
104 SHIT_META(({.displayName = "Font Size", .tooltip = "字号", .range = {1, 300}}))
105 float m_fontSize = 24.0f;
106 SHIT_META(Disable)
107 float m_fontHeight = 0.0f;
108 SHIT_META(({.displayName = "Text Color", .tooltip = "文字颜色"}))
109 Color m_textColor { 30, 30, 30, 255 };
110 SHIT_META(({.displayName = "Placeholder Color", .tooltip = "占位符颜色"}))
111 Color m_placeholderColor { 140, 140, 140, 255 };
112 SHIT_META(({.displayName = "Cursor Color", .tooltip = "光标颜色"}))
113 Color m_cursorColor { 80, 140, 220, 255 };
114 SHIT_META(({.displayName = "Selection Color", .tooltip = "选区颜色"}))
115 Color m_selectionColor { 80, 140, 220, 120 };
116 SHIT_META(Disable)
117 bool m_isDirty = true;
118
119 // 光标 / 选区(单位:UTF-8 字节偏移)
120 SHIT_META(Disable)
121 size_t m_cursor = 0;
122 SHIT_META(Disable)
124
125 // IME 组合(preedit)
126 SHIT_META(Disable)
127 std::string m_preedit;
128 SHIT_META(Disable)
130 SHIT_META(Disable)
132
133 // SDL 文本事件编辑辅助
135 void moveCursor(int deltaChars, bool selecting);
136 void moveCursorToBoundary(bool toStart, bool selecting);
138
139 // 内部 UTF-8 工具(供子类渲染使用)
140 static size_t charToByte(const std::string& s, size_t charIdx);
141 static size_t byteToChar(const std::string& s, size_t byteOff);
142
143 private:
144 friend class TextInputGate;
145 void onDetach() override;
146 void onDestroy() override;
147 };
148}
#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
定义 UITextInput.h:43
float m_fontHeight
定义 UITextInput.h:107
const Color & getCursorColor() const
定义 UITextInput.h:54
Color m_textColor
定义 UITextInput.h:109
bool isMultiline() const
定义 UITextInput.h:62
bool m_isMultiline
定义 UITextInput.h:97
const Color & getTextColor() const
定义 UITextInput.h:48
Color m_cursorColor
定义 UITextInput.h:113
const std::string & getText() const
定义 UITextInput.h:32
void setTextColor(const Color &color)
定义 UITextInput.h:49
virtual bool onKeyDown(KeyCode key, bool shift, bool ctrl)
子类可覆写:处理键盘控制键(方向键、退格、删除、Home/End、回车)
void onTextEditing(const char *utf8Text, int start, int length)
void markDirty()
标记脏位(下一帧重建纹理/布局)
定义 UITextInput.h:89
UITextInput()=default
bool isFocused() const
定义 UITextInput.h:61
void setPlaceholderColor(const Color &color)
定义 UITextInput.h:52
size_t m_selectionAnchor
定义 UITextInput.h:123
static size_t charToByte(const std::string &s, size_t charIdx)
const std::string & getFontPath() const
定义 UITextInput.h:40
virtual void insertText(const std::string &utf8)
子类可覆写:向缓冲插入一段 UTF-8 文本
int m_preeditLength
定义 UITextInput.h:131
const Color & getSelectionColor() const
定义 UITextInput.h:57
void onRender(const SDL_FRect &screenRect) override=0
子类实现:在给定屏幕矩形内绘制自身(通过 Renderer 静态 API 绘制)
void onTextInput(const char *utf8Text)
virtual void insertNewline()
子类可覆写:当前选区是否被替换为换行(UITextBox 拒绝)
static size_t byteToChar(const std::string &s, size_t byteOff)
void setFontPath(const std::string &fontPath)
定义 UITextInput.h:41
void setSelectionColor(const Color &color)
定义 UITextInput.h:58
Color m_selectionColor
定义 UITextInput.h:115
std::string m_preedit
定义 UITextInput.h:127
float m_fontSize
定义 UITextInput.h:105
void moveCursor(int deltaChars, bool selecting)
std::string m_text
定义 UITextInput.h:99
size_t m_cursor
定义 UITextInput.h:121
void setFocused(bool focused)
由 UIRenderSystem 在命中时调用,切换聚焦
void setCursorColor(const Color &color)
定义 UITextInput.h:55
const Color & getPlaceholderColor() const
定义 UITextInput.h:51
void setFontSize(float fontSize)
定义 UITextInput.h:44
float getFontHeight() const
定义 UITextInput.h:46
TTF_Font * acquireFont()
获取或加载字体(进程级缓存,跨 UITextInput/UITextBox/UITextArea 共享)
const std::string & getPlaceholder() const
定义 UITextInput.h:36
void setPlaceholder(const std::string &placeholder)
定义 UITextInput.h:37
virtual void setText(const std::string &text)
int m_preeditStart
定义 UITextInput.h:129
bool m_isFocused
定义 UITextInput.h:95
friend class TextInputGate
定义 UITextInput.h:144
void moveCursorToBoundary(bool toStart, bool selecting)
~UITextInput() override
std::string m_placeholder
定义 UITextInput.h:101
Color m_placeholderColor
定义 UITextInput.h:111
std::string m_fontPath
定义 UITextInput.h:103
size_t getCharacterCount() const
以"字符"计算文本长度
bool m_isDirty
定义 UITextInput.h:117
定义 AudioPlayer.h:10
KeyCode
定义 KeyCode.h:8
RGBA 颜色(每通道 8 位)
定义 Core.h:26