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

文本输入控件抽象基类 更多...

#include <UITextInput.h>

类 UITextInput 继承关系图:
UIRendererComponent Component UITextArea UITextBox

Public 成员函数

 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=0
 子类实现:在给定屏幕矩形内绘制自身(通过 Renderer 静态 API 绘制)
virtual bool onKeyDown (KeyCode key, bool shift, bool ctrl)
 子类可覆写:处理键盘控制键(方向键、退格、删除、Home/End、回车)
virtual void insertText (const std::string &utf8)
 子类可覆写:向缓冲插入一段 UTF-8 文本
virtual void insertNewline ()
 子类可覆写:当前选区是否被替换为换行(UITextBox 拒绝)
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 成员函数

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 属性

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

友元

class TextInputGate

详细描述

文本输入控件抽象基类

负责所有输入控件共享的状态与行为:

  • 文本缓冲(UTF-8)+ 光标位置(以 UTF-8 字符计)
  • 选区(Shift + 方向键 / 鼠标拖拽)
  • 占位符文字(无内容且未聚焦时显示)
  • IME 组合串(preedit)展示与插入
  • 聚焦状态,失焦自动停止文本输入模式

单行 vs 多行的差异由子类覆写 onRender / onKeyDown / 插入换行策略实现。 不直接绘制背景:背景由同 GameObject 上的 UIImage 提供(与按钮一致)。

构造及析构函数说明

◆ UITextInput()

UITextInput ( )
default

◆ ~UITextInput()

~UITextInput ( )
override

成员函数说明

◆ acquireFont()

TTF_Font * acquireFont ( )
protected

获取或加载字体(进程级缓存,跨 UITextInput/UITextBox/UITextArea 共享)

◆ byteToChar()

size_t byteToChar ( const std::string & s,
size_t byteOff )
staticprotected

◆ charToByte()

size_t charToByte ( const std::string & s,
size_t charIdx )
staticprotected

◆ clampCursor()

void clampCursor ( )
protected

◆ deleteSelection()

void deleteSelection ( )
protected

◆ getCharacterCount()

size_t getCharacterCount ( ) const
protected

以"字符"计算文本长度

◆ getCursorColor()

const Color & getCursorColor ( ) const
inline

◆ getFontHeight()

float getFontHeight ( ) const
inline

◆ getFontPath()

const std::string & getFontPath ( ) const
inline

◆ getFontSize()

float getFontSize ( ) const
inline

◆ getPlaceholder()

const std::string & getPlaceholder ( ) const
inline

◆ getPlaceholderColor()

const Color & getPlaceholderColor ( ) const
inline

◆ getSelectionColor()

const Color & getSelectionColor ( ) const
inline

◆ getText()

const std::string & getText ( ) const
inline

◆ getTextColor()

const Color & getTextColor ( ) const
inline

◆ insertNewline()

virtual void insertNewline ( )
protectedvirtual

子类可覆写:当前选区是否被替换为换行(UITextBox 拒绝)

UITextArea 重载.

◆ insertText()

virtual void insertText ( const std::string & utf8)
protectedvirtual

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

UITextBox 重载.

◆ isFocused()

bool isFocused ( ) const
inline

◆ isMultiline()

bool isMultiline ( ) const
inline

◆ markDirty()

void markDirty ( )
inlineprotected

标记脏位(下一帧重建纹理/布局)

◆ moveCursor()

void moveCursor ( int deltaChars,
bool selecting )
protected

◆ moveCursorToBoundary()

void moveCursorToBoundary ( bool toStart,
bool selecting )
protected

◆ onKeyDown()

virtual bool onKeyDown ( KeyCode key,
bool shift,
bool ctrl )
protectedvirtual

子类可覆写:处理键盘控制键(方向键、退格、删除、Home/End、回车)

返回
true 表示已处理该按键,不继续传播

UITextArea 重载.

◆ onRender()

void onRender ( const SDL_FRect & screenRect)
overrideprotectedpure virtual

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

实现了 UIRendererComponent.

UITextArea , 以及 UITextBox 内被实现.

◆ onTextEditing()

void onTextEditing ( const char * utf8Text,
int start,
int length )

◆ onTextInput()

void onTextInput ( const char * utf8Text)

◆ setCursorColor()

void setCursorColor ( const Color & color)
inline

◆ setFocused()

void setFocused ( bool focused)

UIRenderSystem 在命中时调用,切换聚焦

◆ setFontPath()

void setFontPath ( const std::string & fontPath)
inline

◆ setFontSize()

void setFontSize ( float fontSize)
inline

◆ setPlaceholder()

void setPlaceholder ( const std::string & placeholder)
inline

◆ setPlaceholderColor()

void setPlaceholderColor ( const Color & color)
inline

◆ setSelectionColor()

void setSelectionColor ( const Color & color)
inline

◆ setText()

virtual void setText ( const std::string & text)
virtual

◆ setTextColor()

void setTextColor ( const Color & color)
inline

◆ TextInputGate

friend class TextInputGate
friend

类成员变量说明

◆ m_cursor

size_t m_cursor = 0
protected

◆ m_cursorColor

Color m_cursorColor { 80, 140, 220, 255 }
protected

◆ m_fontHeight

float m_fontHeight = 0.0f
protected

◆ m_fontPath

std::string m_fontPath
protected

◆ m_fontSize

float m_fontSize = 24.0f
protected

◆ m_isDirty

bool m_isDirty = true
protected

◆ m_isFocused

bool m_isFocused = false
protected

◆ m_isMultiline

bool m_isMultiline = false
protected

◆ m_placeholder

std::string m_placeholder
protected

◆ m_placeholderColor

Color m_placeholderColor { 140, 140, 140, 255 }
protected

◆ m_preedit

std::string m_preedit
protected

◆ m_preeditLength

int m_preeditLength = 0
protected

◆ m_preeditStart

int m_preeditStart = 0
protected

◆ m_selectionAnchor

size_t m_selectionAnchor = 0
protected

◆ m_selectionColor

Color m_selectionColor { 80, 140, 220, 120 }
protected

◆ m_text

std::string m_text
protected

◆ m_textColor

Color m_textColor { 30, 30, 30, 255 }
protected

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