From 8dace27f569c31a2fe0ee7db06e5234c163dcb1c Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Wed, 24 Jul 2024 08:33:59 +0200 Subject: [PATCH] Font size adjustment, toying with command windows and lua --- src/text_editor/commands.cpp | 22 +++++++++++ src/text_editor/commands_window.cpp | 61 +++++++++++++++++++++++------ src/text_editor/lua_api.cpp | 57 ++++++--------------------- src/text_editor/text_editor.cpp | 8 +--- src/text_editor/text_editor.h | 2 +- 5 files changed, 86 insertions(+), 64 deletions(-) diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 344dced..4d9dc29 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -204,6 +204,19 @@ Range EncloseWord(Buffer &buffer, Int pos) { return result; } +void ReloadFont(Int font_size) { + if (MainFont.glyphs) { + UnloadFont(MainFont); + MainFont = {}; + } + + FontSize = font_size; + FontSpacing = 1; + FontLineSpacing = FontSize; + MainFont = LoadFontEx("c:\\Windows\\Fonts\\consola.ttf", (int)FontSize, NULL, 500); + FontCharSpacing = GetCharSpacing(MainFont, FontSize, FontSpacing); +} + void HandleGlobalCommands() { Window *command_window = GetWindow(CommandWindowID); if (!IsActive(command_window)) { @@ -218,6 +231,15 @@ void HandleGlobalCommands() { command_window->visible = !command_window->visible; } + if (CtrlPress(KEY_MINUS)) { + Int font_size = Clamp(FontSize - 1, (Int)4, (Int)100); + ReloadFont(font_size); + } + if (CtrlPress(KEY_EQUAL)) { + Int font_size = Clamp(FontSize + 1, (Int)4, (Int)100); + ReloadFont(font_size); + } + if (CtrlPress(KEY_ONE)) { SetActiveWindow({0}); } else if (CtrlPress(KEY_TWO)) { diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 11c0320..524ff67 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -112,6 +112,18 @@ void Command_CreateCursorVertical(View *_view, int direction) { MergeCarets(*buffer, &view.carets); } +void Command_SelectRange(View *view, Range range) { + Buffer *buffer = GetBuffer(view->buffer_id); + view->carets.len = 1; + view->carets[0] = MakeCaret(range.min, range.max); +} + +void Command_SelectEntireBuffer(View *view) { + Buffer *buffer = GetBuffer(view->buffer_id); + view->carets.len = 1; + view->carets[0] = MakeCaret(0, buffer->len); +} + int64_t FuzzyCloserWordBegin = 5; int64_t FuzzyConsecutiveMultiplier = 3; int64_t FuzzyRate(String16 string, String16 with) { @@ -266,8 +278,7 @@ void HandleActiveWindowBindings(Window *window) { bool dont_update_scroll = false; if (CtrlPress(KEY_A)) { - view.carets.len = 1; - view.carets[0] = MakeCaret(0, buffer->len); + Command_SelectEntireBuffer(&view); dont_update_scroll = true; } @@ -358,19 +369,47 @@ void HandleActiveWindowBindings(Window *window) { String16 s = GetString(*buffer, l); ReplaceText(temp_buffer, GetEndAsRange(*temp_buffer), s); } - ReplaceText(buffer, {0, buffer->len}, GetString(*temp_buffer)); + + Caret caret = view.carets[0]; + Command_SelectEntireBuffer(&view); + Command_Replace(&view, GetString(*temp_buffer)); + view.carets[0] = caret; } if (window->execute_line) { if (Press(KEY_ENTER)) { - Int line = PosToLine(*buffer, GetFront(view.carets[0])); - Range range = GetLineRange(*buffer, line); - String16 string = GetString(*buffer, range); - EvalString(string); - if (window->id.id == CommandWindowID.id) { - Window *command_window = GetWindow(CommandWindowID); - command_window->visible = !command_window->visible; - ActiveWindow = LastActiveWindow; + Int line = PosToLine(*buffer, GetFront(view.carets[0])); + Range range = GetLineRange(*buffer, line); + String16 string = GetString(*buffer, range); + String16 eval_result = EvalString(string); + + if (Ctrl()) { + Command_SelectEntireBuffer(&view); + Command_Replace(&view, eval_result); + Command_SelectRange(&view, {}); + Command_Replace(&view, L"\n"); + Command_SelectRange(&view, {}); + } else { + + if (window->id.id == CommandWindowID.id) { + Window *command_window = GetWindow(CommandWindowID); + command_window->visible = !command_window->visible; + ActiveWindow = LastActiveWindow; + } + + { + Window *window = GetWindow(LastActiveWindow); + View *view = GetView(window->active_view); + SetActiveWindow(window->id); + Command_Replace(view, eval_result); + } + + Int end_of_buffer = 0; + Range range = GetLineRange(*buffer, 0, &end_of_buffer); + range.max -= 1; + range.max += end_of_buffer; + Command_SelectRange(&view, range); + Command_Replace(&view, {}); } } } else { diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 1935f2f..28c8f95 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -1,4 +1,5 @@ -lua_State *LuaState = NULL; +lua_State *LuaState = NULL; +String16 LuaCommandResult = {}; int LuaOpenFile(lua_State *L) { const char *text = luaL_checkstring(L, 1); @@ -8,50 +9,18 @@ int LuaOpenFile(lua_State *L) { return 0; // number of results } -// struct ResolvedSet { -// Window *window; -// View *view; -// Buffer *buffer; -// }; - -// ResolvedSet GetActiveWindow() { -// if (ActiveWindow.id == CommandWindowID.id) { -// Window *window = GetWindow(LastActiveWindow); -// View *view = GetView(window->active_view); -// SetActiveWindow(window->id); -// Buffer *buffer = GetBuffer(view->buffer_id); -// return {window, view, buffer}; -// } - -// Window *window = GetWindow(ActiveWindow); -// View *view = GetView(window->active_view); -// Buffer *buffer = GetBuffer(view->buffer_id); -// return {window, view, buffer}; -// } - int LuaListOpenBuffers(lua_State *L) { - Window *window = GetWindow(LastActiveWindow); - View *view = GetView(window->active_view); - SetActiveWindow(window->id); - Buffer *buffer = GetBuffer(view->buffer_id); - Scratch scratch; Array strings = {scratch}; For(Buffers) { - String string = Format(scratch, "%.*s\n", FmtString(it.name)); + String string = Format(scratch, "open \"%.*s\"", FmtString(it.name)); Add(&strings, ToString16(scratch, string)); } - String16 string = Merge(scratch, strings, L"\n"); - Command_Replace(view, string); + LuaCommandResult = Merge(scratch, strings, L"\n"); return 0; } int LuaListViews(lua_State *L) { - Window *window = GetWindow(LastActiveWindow); - View *view = GetView(window->active_view); - SetActiveWindow(window->id); - Buffer *buffer = GetBuffer(view->buffer_id); - Scratch scratch; Array strings = {scratch}; For(Views) { @@ -59,17 +28,11 @@ int LuaListViews(lua_State *L) { String string = Format(scratch, "view = %lld buffer = %lld name = %.*s", (long long)it.id.id, (long long)buffer->id.id, FmtString(buffer->name)); Add(&strings, ToString16(scratch, string)); } - String16 string = Merge(scratch, strings, L"\n"); - Command_Replace(view, string); + LuaCommandResult = Merge(scratch, strings, L"\n"); return 0; } int LuaListWindows(lua_State *L) { - Window *window = GetWindow(LastActiveWindow); - View *view = GetView(window->active_view); - SetActiveWindow(window->id); - Buffer *buffer = GetBuffer(view->buffer_id); - Scratch scratch; Array strings = {scratch}; For(Windows) { @@ -84,8 +47,7 @@ int LuaListWindows(lua_State *L) { Add(&strings, ToString16(scratch, child_string)); } } - String16 string = Merge(scratch, strings, L"\n"); - Command_Replace(view, string); + LuaCommandResult = Merge(scratch, strings, L"\n"); return 0; } @@ -113,11 +75,14 @@ void SetInfoBarErrorMessage(String string) { if (string.len) InfoBarErrorMessage = Copy(sys, string); } -void EvalString(String16 string16) { +// @todo: get result from lua? +String16 EvalString(String16 string16) { Scratch scratch; - String string = ToString(scratch, string16); + LuaCommandResult = {}; + String string = ToString(scratch, string16); if (luaL_dostring(LuaState, string.data) != LUA_OK) { const char *text = lua_tostring(LuaState, -1); SetInfoBarErrorMessage(text); } + return LuaCommandResult; } \ No newline at end of file diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 1d06909..6b0091f 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -74,12 +74,8 @@ int main(void) { MenuFontSize = 19; MenuFont = LoadFontEx("c:\\Windows\\Fonts\\Segoeui.ttf", (int)MenuFontSize, NULL, 500); + ReloadFont(16); - FontSize = 16; - FontSpacing = 1; - FontLineSpacing = FontSize; - MainFont = LoadFontEx("c:\\Windows\\Fonts\\consola.ttf", (int)FontSize, NULL, 500); - FontCharSpacing = GetCharSpacing(MainFont, FontSize, FontSpacing); InitLua(); Allocator sys_allocator = GetSystemAllocator(); @@ -148,7 +144,7 @@ int main(void) { FrameID += 1; Rect2I screen_rect = GetScreenRect(); - Rect2I infobar_rect = CutBottom(&screen_rect, (Int)MenuFontSize); + Rect2I infobar_rect = CutBottom(&screen_rect, (Int)FontLineSpacing); float line_numbers_size = MeasureTextEx(MainFont, "12345", (float)FontSize, (float)FontSpacing).x; { int i = 0; diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index f9733b6..a1b4ea6 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -109,7 +109,7 @@ WindowID LastActiveWindow; Int LastFrameIDWhenSwitchedActiveWindow; String InfoBarErrorMessage; -void EvalString(String16 string16); +String16 EvalString(String16 string16); Rect2I GetVisibleCells(Window &window); void AfterEdit(View *view, Array edits); Scroller ComputeScrollerRect(Window &window);