Skip to content

Commit

Permalink
lua: reduce code duplication in color text editor handling
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-lysiuk committed Sep 22, 2024
1 parent bc1fc7e commit 2e0def2
Showing 1 changed file with 18 additions and 32 deletions.
50 changes: 18 additions & 32 deletions Quake/ls_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,16 @@ void LS_ClearImGuiStack()

constexpr LS_UserDataType<TextEditor*> ls_imguicolortextedit_type("ImGuiColorTextEdit");

static TextEditor* LS_GetColorTextEdit(lua_State* state)
{
LS_EnsureFrameScope(state);

TextEditor* texteditor = ls_imguicolortextedit_type.GetValue(state, 1);
assert(texteditor);

return texteditor;
}

static int LS_value_ImGuiColorTextEdit_gc(lua_State* state)
{
TextEditor* const texteditor = ls_imguicolortextedit_type.GetValue(state, 1);
Expand All @@ -465,10 +475,7 @@ static int LS_value_ImGuiColorTextEdit_index(lua_State* state)

static int LS_value_ImGuiColorTextEdit_GetCursorPosition(lua_State* state)
{
LS_EnsureFrameScope(state);

TextEditor* texteditor = ls_imguicolortextedit_type.GetValue(state, 1);
assert(texteditor);
TextEditor* texteditor = LS_GetColorTextEdit(state);

int line, column;
texteditor->GetCursorPosition(line, column);
Expand All @@ -481,10 +488,7 @@ static int LS_value_ImGuiColorTextEdit_GetCursorPosition(lua_State* state)

static int LS_value_ImGuiColorTextEdit_Render(lua_State* state)
{
LS_EnsureFrameScope(state);

TextEditor* texteditor = ls_imguicolortextedit_type.GetValue(state, 1);
assert(texteditor);
TextEditor* texteditor = LS_GetColorTextEdit(state);

const char* const title = luaL_checkstring(state, 2);
const bool parentIsFocused = luaL_opt(state, lua_toboolean, 3, false);
Expand All @@ -498,10 +502,7 @@ static int LS_value_ImGuiColorTextEdit_Render(lua_State* state)

static int LS_value_ImGuiColorTextEdit_SelectLine(lua_State* state)
{
LS_EnsureFrameScope(state);

TextEditor* texteditor = ls_imguicolortextedit_type.GetValue(state, 1);
assert(texteditor);
TextEditor* texteditor = LS_GetColorTextEdit(state);

const int line = luaL_checkinteger(state, 2);
texteditor->SelectLine(line - 1); // first line index is zero
Expand All @@ -510,10 +511,7 @@ static int LS_value_ImGuiColorTextEdit_SelectLine(lua_State* state)

static int LS_value_ImGuiColorTextEdit_SelectRegion(lua_State* state)
{
LS_EnsureFrameScope(state);

TextEditor* texteditor = ls_imguicolortextedit_type.GetValue(state, 1);
assert(texteditor);
TextEditor* texteditor = LS_GetColorTextEdit(state);

const int startline = luaL_checkinteger(state, 2);
const int startchar = luaL_checkinteger(state, 3);
Expand All @@ -527,10 +525,7 @@ static int LS_value_ImGuiColorTextEdit_SelectRegion(lua_State* state)

static int LS_value_ImGuiColorTextEdit_SetCursorPosition(lua_State* state)
{
LS_EnsureFrameScope(state);

TextEditor* texteditor = ls_imguicolortextedit_type.GetValue(state, 1);
assert(texteditor);
TextEditor* texteditor = LS_GetColorTextEdit(state);

const int line = luaL_checkinteger(state, 2);
const int character = luaL_optinteger(state, 3, 1);
Expand All @@ -542,10 +537,7 @@ static int LS_value_ImGuiColorTextEdit_SetCursorPosition(lua_State* state)

static int LS_value_ImGuiColorTextEdit_SetLanguageDefinition(lua_State* state)
{
LS_EnsureFrameScope(state);

TextEditor* texteditor = ls_imguicolortextedit_type.GetValue(state, 1);
assert(texteditor);
TextEditor* texteditor = LS_GetColorTextEdit(state);

static const char* const languages[] = { "none", "cpp", "lua", "entities" };
const int languageId = luaL_checkoption(state, 2, nullptr, languages);
Expand Down Expand Up @@ -580,10 +572,7 @@ static int LS_value_ImGuiColorTextEdit_SetLanguageDefinition(lua_State* state)

static int LS_value_ImGuiColorTextEdit_SetReadOnly(lua_State* state)
{
LS_EnsureFrameScope(state);

TextEditor* texteditor = ls_imguicolortextedit_type.GetValue(state, 1);
assert(texteditor);
TextEditor* texteditor = LS_GetColorTextEdit(state);

const bool readonly = lua_toboolean(state, 2);
texteditor->SetReadOnlyEnabled(readonly);
Expand All @@ -592,10 +581,7 @@ static int LS_value_ImGuiColorTextEdit_SetReadOnly(lua_State* state)

static int LS_value_ImGuiColorTextEdit_SetText(lua_State* state)
{
LS_EnsureFrameScope(state);

TextEditor* texteditor = ls_imguicolortextedit_type.GetValue(state, 1);
assert(texteditor);
TextEditor* texteditor = LS_GetColorTextEdit(state);

size_t length;
const char* const text = luaL_checklstring(state, 2, &length);
Expand Down

0 comments on commit 2e0def2

Please sign in to comment.