跳到主要内容

单行编辑器 InlineEditor

HXSingleLineEditor 是一个与 LayoutStack 解耦的单行文本编辑组件。它被设计用于需要在任意坐标空间中进行文本编辑的场景,例如 Blueprint 节点内的内联编辑、Viewport 中的标注、以及缩放/平移画布中的文字输入。

TextInput 不同的是,单行编辑器不依赖全局布局系统,而是通过显式的 Bounds 矩形和 Painter 进行绘制和输入处理。

核心类型

HXSingleLineEditorState

持久化的编辑状态,必须跨帧保留(static 或动态分配):

struct HXSingleLineEditorState {
HXString Text;
int CursorPos = 0;
int SelectStart = 0;
int SelectEnd = 0;
int AnchorPos = 0;
bool Dragging = false;
bool InIMEComposition = false;
HXString IMECompositionString;
clock_t LastIMEEndTime = 0;
clock_t BlinkClock = 0;
bool ShowCursor = true;

void Reset();
bool HasSelection() const;
bool DeleteSelection();
void Insert(const HXString& str);
void InsertChar(HXChar ch);
};

HXSingleLineEditorStyle

struct HXSingleLineEditorStyle {
int FontSize = 18;
HXColor TextColor = {220, 220, 220};
HXColor SelectionBg = {0, 120, 215};
HXColor SelectionTextColor = {255, 255, 255};
HXColor CursorColor = {255, 255, 255};
HXColor BackgroundColor = {45, 45, 48};
HXColor BorderColor = {100, 100, 100};
HXColor BorderFocusColor = {0, 150, 255};
int LeftPadding = 5;
};

HXSingleLineEditor

struct HXSingleLineEditor {
HXSingleLineEditorState* State = nullptr;
HXRect Bounds = {0, 0, 0, 0};
bool Active = false;
bool WasActive = false;
bool MouseStyle = false;
HXSingleLineEditorStyle Style;
HXPoint IMEBasePos = {0, 0};
};

API 函数

// 处理当前帧的所有消息(输入、光标、选择、IME)
void SingleLineEditorProcessMessages(HXSingleLineEditor& editor, HXBufferPainter* painter);

// 渲染编辑器(应在 ProcessMessages 之后调用)
void SingleLineEditorRender(HXSingleLineEditor& editor, HXBufferPainter* painter);

// 将局部 X 坐标转换为文本光标位置
int SingleLineEditorHitTest(HXSingleLineEditor& editor, HXBufferPainter* painter, HXGInt localX);

// 更新 IME 候选窗口位置
void SingleLineEditorUpdateIME(HXSingleLineEditor& editor, HXBufferPainter* painter);

使用示例

static HX::HXSingleLineEditorState editState;
static HX::HXSingleLineEditor editor;
editor.State = &editState;
editor.Bounds = {100, 100, 200, 30};

// 每帧调用
HX::SingleLineEditorProcessMessages(editor, HX::CurrentPainter());
HX::SingleLineEditorRender(editor, HX::CurrentPainter());
if (editor.Active) {
HX::SingleLineEditorUpdateIME(editor, HX::CurrentPainter());
}
关键注意事项
  1. State 指针必须始终有效,且状态对象必须跨帧持久化。
  2. Bounds 使用的是当前 Painter 的局部坐标,而非屏幕坐标。
  3. ProcessMessagesRender 的调用顺序不能颠倒。
  4. IME 更新仅在编辑器处于激活状态且获得焦点时才需要调用。
提示

如果你只是在普通 Window 内需要一个单行输入框,直接使用 TextInput 即可,无需关心单行编辑器的复杂坐标管理。单行编辑器主要服务于需要在缩放/平移画布中进行文本编辑的高级场景(如 Blueprint 节点重命名)。