ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
UITextBox类 参考

单行文本输入框 更多...

#include <UITextBox.h>

类 UITextBox 继承关系图:
UITextInput UIRendererComponent Component

Public 成员函数

 UITextBox ()
 ~UITextBox () override=default
void setCharacterLimit (size_t limit)
size_t getCharacterLimit () const
Public 成员函数 继承自 UITextInput
 UITextInput ()=default
 ~UITextInput () override
const std::string & getText () const
virtual void setText (const std::string &text)
const std::string & getPlaceholder () const
void setPlaceholder (const std::string &placeholder)
const std::string & getFontPath () const
void setFontPath (const std::string &fontPath)
float getFontSize () const
void setFontSize (float fontSize)
float getFontHeight () const
const ColorgetTextColor () const
void setTextColor (const Color &color)
const ColorgetPlaceholderColor () const
void setPlaceholderColor (const Color &color)
const ColorgetCursorColor () const
void setCursorColor (const Color &color)
const ColorgetSelectionColor () const
void setSelectionColor (const Color &color)
bool isFocused () const
bool isMultiline () const
void onTextInput (const char *utf8Text)
void onTextEditing (const char *utf8Text, int start, int length)
void setFocused (bool focused)
 UIRenderSystem 在命中时调用,切换聚焦
Public 成员函数 继承自 UIRendererComponent
 UIRendererComponent ()
 ~UIRendererComponent () override=default
void onAttach () override
virtual bool containsPoint (const SDL_FRect &screenRect, const Vector2 &point) const
 点击命中检测:默认矩形内即命中,子类可覆写为更精确的形状
int getZIndex () const
void setZIndex (int zIndex)
bool isVisible () const
void setVisible (bool visible)
Public 成员函数 继承自 Component
 Component ()
virtual ~Component ()=default
virtual void onCreate ()
virtual void onAfterDeserialize ()
 反序列化完成后回调(Prefab::apply 逐组件调用一次,字段已写入)。 用于重建"直写字段绕过了 setter、无法自动触发的引擎状态"(如精灵纹理加载)。
 Component (const Component &)=delete
Componentoperator= (const Component &)=delete
 Component (Component &&)=delete
Componentoperator= (Component &&)=delete
GameObjectgetOwner () const
bool isRegistered () const

Protected 成员函数

void onRender (const SDL_FRect &screenRect) override
 子类实现:在给定屏幕矩形内绘制自身(通过 Renderer 静态 API 绘制)
void insertText (const std::string &utf8) override
 子类可覆写:向缓冲插入一段 UTF-8 文本
Protected 成员函数 继承自 UITextInput
virtual bool onKeyDown (KeyCode key, bool shift, bool ctrl)
 子类可覆写:处理键盘控制键(方向键、退格、删除、Home/End、回车)
size_t getCharacterCount () const
 以"字符"计算文本长度
void markDirty ()
 标记脏位(下一帧重建纹理/布局)
TTF_Font * acquireFont ()
 获取或加载字体(进程级缓存,跨 UITextInput/UITextBox/UITextArea 共享)
void deleteSelection ()
void moveCursor (int deltaChars, bool selecting)
void moveCursorToBoundary (bool toStart, bool selecting)
void clampCursor ()
Protected 成员函数 继承自 UIRendererComponent
void requestSort ()
 components变更后通知系统重排(实例:zIndex 变化)
Protected 成员函数 继承自 Component
void setRegistered (bool registered)

额外继承的成员函数

静态 Protected 成员函数 继承自 UITextInput
static size_t charToByte (const std::string &s, size_t charIdx)
static size_t byteToChar (const std::string &s, size_t byteOff)
静态 Protected 成员函数 继承自 UIRendererComponent
static SDL_Color toSDLColor (const Color &color)
 Color → SDL_Color 边界转换(供 UIText / UITextInput 系列传给 SDL_ttf 用)
Protected 属性 继承自 UITextInput
bool m_isFocused = false
bool m_isMultiline = false
std::string m_text
std::string m_placeholder
std::string m_fontPath
float m_fontSize = 24.0f
float m_fontHeight = 0.0f
Color m_textColor { 30, 30, 30, 255 }
Color m_placeholderColor { 140, 140, 140, 255 }
Color m_cursorColor { 80, 140, 220, 255 }
Color m_selectionColor { 80, 140, 220, 120 }
bool m_isDirty = true
size_t m_cursor = 0
size_t m_selectionAnchor = 0
std::string m_preedit
int m_preeditStart = 0
int m_preeditLength = 0
Protected 属性 继承自 UIRendererComponent
int m_zIndex = 0
 渲染层级(值越大越靠上)
bool m_isVisible = true
 是否参与渲染与命中
Protected 属性 继承自 Component
GameObjectm_owner = nullptr
bool m_isRegistered = false

详细描述

单行文本输入框

水平单行布局,不接收换行。聚焦时按 Home/End 跳到行首/行尾,左右方向键逐字移动, Ctrl+Left/Right 跳词(粗略按空格分词)。文字超出可见宽度时自动水平滚动跟随光标。

用法: auto* go = scene->createGameObject("Input"); go->setParent(canvas); go->addComponent<Shit::UITransform>(...); go->addComponent<Shit::UIImage>("input_bg.png"); // 背景可选 auto* input = go->addComponent<Shit::UITextBox>(); input->setFontPath("fonts/main.ttf"); input->setPlaceholder("请输入...");

聚焦管理(由 UIRenderSystem 驱动):点击输入框即聚焦;点击空白区域自动失焦; 点击其他 UI 控件(按钮/图片)不夺走输入框焦点,可同时保持输入状态。

构造及析构函数说明

◆ UITextBox()

UITextBox ( )

◆ ~UITextBox()

~UITextBox ( )
overridedefault

成员函数说明

◆ getCharacterLimit()

size_t getCharacterLimit ( ) const
inline

◆ insertText()

void insertText ( const std::string & utf8)
overrideprotectedvirtual

子类可覆写:向缓冲插入一段 UTF-8 文本

重载 UITextInput .

◆ onRender()

void onRender ( const SDL_FRect & screenRect)
overrideprotectedvirtual

子类实现:在给定屏幕矩形内绘制自身(通过 Renderer 静态 API 绘制)

实现了 UITextInput.

◆ setCharacterLimit()

void setCharacterLimit ( size_t limit)
inline

该类的文档由以下文件生成: