Skip to content

Commit 21d03ed

Browse files
committed
InputText: renamed namespace from stb_texture structure and added an indirection.
1 parent a2366f9 commit 21d03ed

File tree

2 files changed

+66
-51
lines changed

2 files changed

+66
-51
lines changed

imgui_internal.h

+15-12
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ namespace ImStb
216216

217217
} // namespace ImStb
218218

219+
typedef ImStb::STB_TexteditState ImStbTexteditState;
220+
219221
//-----------------------------------------------------------------------------
220222
// [SECTION] Macros
221223
//-----------------------------------------------------------------------------
@@ -1110,11 +1112,13 @@ struct IMGUI_API ImGuiInputTextDeactivatedState
11101112
ImGuiInputTextDeactivatedState() { memset(this, 0, sizeof(*this)); }
11111113
void ClearFreeMemory() { ID = 0; TextA.clear(); }
11121114
};
1115+
11131116
// Internal state of the currently focused/edited text input box
11141117
// For a given item ID, access with ImGui::GetInputTextState()
11151118
struct IMGUI_API ImGuiInputTextState
11161119
{
11171120
ImGuiContext* Ctx; // parent UI context (needs to be set explicitly by parent).
1121+
ImStbTexteditState* Stb; // State for stb_textedit.h
11181122
ImGuiID ID; // widget id owning the text state
11191123
int CurLenW, CurLenA; // we need to maintain our buffer length in both UTF-8 and wchar format. UTF-8 length is valid even if TextA is not.
11201124
ImVector<ImWchar> TextW; // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so we copy into own buffer.
@@ -1123,7 +1127,7 @@ struct IMGUI_API ImGuiInputTextState
11231127
bool TextAIsValid; // temporary UTF8 buffer is not initially valid before we make the widget active (until then we pull the data from user argument)
11241128
int BufCapacityA; // end-user buffer capacity
11251129
ImVec2 Scroll; // horizontal offset (managed manually) + vertical scrolling (pulled from child window's own Scroll.y)
1126-
ImStb::STB_TexteditState Stb; // state for stb_textedit.h
1130+
11271131
float CursorAnim; // timer for cursor blink, reset on every user action so the cursor reappears immediately
11281132
bool CursorFollow; // set when we want scrolling to follow the current cursor position (not always!)
11291133
bool SelectedAllMouseLock; // after a double-click to select all, we ignore further mouse drags to update selection
@@ -1133,30 +1137,29 @@ struct IMGUI_API ImGuiInputTextState
11331137
int ReloadSelectionStart; // POSITIONS ARE IN IMWCHAR units *NOT* UTF-8 this is why this is not exposed yet.
11341138
int ReloadSelectionEnd;
11351139

1136-
ImGuiInputTextState() { memset(this, 0, sizeof(*this)); }
1140+
ImGuiInputTextState();
1141+
~ImGuiInputTextState();
11371142
void ClearText() { CurLenW = CurLenA = 0; TextW[0] = 0; TextA[0] = 0; CursorClamp(); }
11381143
void ClearFreeMemory() { TextW.clear(); TextA.clear(); InitialTextA.clear(); }
1139-
int GetUndoAvailCount() const { return Stb.undostate.undo_point; }
1140-
int GetRedoAvailCount() const { return IMSTB_TEXTEDIT_UNDOSTATECOUNT - Stb.undostate.redo_point; }
11411144
void OnKeyPressed(int key); // Cannot be inline because we call in code in stb_textedit.h implementation
11421145

11431146
// Cursor & Selection
11441147
void CursorAnimReset() { CursorAnim = -0.30f; } // After a user-input the cursor stays on for a while without blinking
1145-
void CursorClamp() { Stb.cursor = ImMin(Stb.cursor, CurLenW); Stb.select_start = ImMin(Stb.select_start, CurLenW); Stb.select_end = ImMin(Stb.select_end, CurLenW); }
1146-
bool HasSelection() const { return Stb.select_start != Stb.select_end; }
1147-
void ClearSelection() { Stb.select_start = Stb.select_end = Stb.cursor; }
1148-
int GetCursorPos() const { return Stb.cursor; }
1149-
int GetSelectionStart() const { return Stb.select_start; }
1150-
int GetSelectionEnd() const { return Stb.select_end; }
1151-
void SelectAll() { Stb.select_start = 0; Stb.cursor = Stb.select_end = CurLenW; Stb.has_preferred_x = 0; }
1148+
void CursorClamp() { Stb->cursor = ImMin(Stb->cursor, CurLenW); Stb->select_start = ImMin(Stb->select_start, CurLenW); Stb->select_end = ImMin(Stb->select_end, CurLenW); }
1149+
bool HasSelection() const { return Stb->select_start != Stb->select_end; }
1150+
void ClearSelection() { Stb->select_start = Stb->select_end = Stb->cursor; }
1151+
int GetCursorPos() const { return Stb->cursor; }
1152+
int GetSelectionStart() const { return Stb->select_start; }
1153+
int GetSelectionEnd() const { return Stb->select_end; }
1154+
void SelectAll() { Stb->select_start = 0; Stb->cursor = Stb->select_end = CurLenW; Stb->has_preferred_x = 0; }
11521155

11531156
// Reload user buf (WIP #2890)
11541157
// If you modify underlying user-passed const char* while active you need to call this (InputText V2 may lift this)
11551158
// strcpy(my_buf, "hello");
11561159
// if (ImGuiInputTextState* state = ImGui::GetInputTextState(id)) // id may be ImGui::GetItemID() is last item
11571160
// state->ReloadUserBufAndSelectAll();
11581161
void ReloadUserBufAndSelectAll() { ReloadUserBuf = true; ReloadSelectionStart = 0; ReloadSelectionEnd = INT_MAX; }
1159-
void ReloadUserBufAndKeepSelection() { ReloadUserBuf = true; ReloadSelectionStart = Stb.select_start; ReloadSelectionEnd = Stb.select_end; }
1162+
void ReloadUserBufAndKeepSelection() { ReloadUserBuf = true; ReloadSelectionStart = Stb->select_start; ReloadSelectionEnd = Stb->select_end; }
11601163
void ReloadUserBufAndMoveToEnd() { ReloadUserBuf = true; ReloadSelectionStart = ReloadSelectionEnd = INT_MAX; }
11611164

11621165
};

0 commit comments

Comments
 (0)