From 9d02d5ab788f0cd3525707690a9f606e2356f249 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 26 Jul 2024 21:47:23 +0200 Subject: [PATCH] Fixing scissoring --- src/basic/math.cpp | 9 + src/platform/render_opengl.cpp | 82 ++-- src/text_editor/commands.cpp | 145 ++----- src/text_editor/commands_window.cpp | 646 ++++++++++++++-------------- src/text_editor/new_text_editor.cpp | 133 +----- src/text_editor/todo.txt | 1 + src/text_editor/window.cpp | 127 ++++++ src/text_editor/window_draw.cpp | 45 +- 8 files changed, 572 insertions(+), 616 deletions(-) diff --git a/src/basic/math.cpp b/src/basic/math.cpp index dd1540c..cd0ce41 100644 --- a/src/basic/math.cpp +++ b/src/basic/math.cpp @@ -80,6 +80,15 @@ Rect2 operator+=(Rect2 &r, float value) { r = r + value; return r; } Rect2 operator*=(Rect2 &r, float value) { r = r * value; return r; } Rect2 operator/=(Rect2 &r, float value) { r = r / value; return r; } +bool operator==(Rect2 a, Rect2 b) { + bool result = a.min.x == b.min.x && a.min.y == b.min.y && a.max.x == b.max.x && a.max.y == b.max.y; + return result; +} +bool operator!=(Rect2 a, Rect2 b) { + bool result = !(a == b); + return result; +} + Vec2 operator-(Vec2 a, Vec2 b) { return {a.x - b.x, a.y - b.y}; } Vec2 operator+(Vec2 a, Vec2 b) { return {a.x + b.x, a.y + b.y}; } Vec2 operator*(Vec2 a, Vec2 b) { return {a.x * b.x, a.y * b.y}; } diff --git a/src/platform/render_opengl.cpp b/src/platform/render_opengl.cpp index aec2ad1..4109c72 100644 --- a/src/platform/render_opengl.cpp +++ b/src/platform/render_opengl.cpp @@ -14,7 +14,8 @@ struct Vertex2D { struct VertexNode2D { VertexNode2D *next; int count; - Vertex2D vertices[1024 * 64]; + Vertex2D vertices[1024 * 16]; + Rect2 scissor; }; struct VertexList2D { @@ -29,17 +30,27 @@ unsigned VBO, VAO; Shader Shader2D; Arena RenderArena; Vec2 WindowSize; +Rect2 CurrentScissor; Font MainFont; Int FontLineSpacing; Int FontCharSpacing; -void BeginFrameRender(Vec2 window_size) { +Rect2 GetScreenRectF() { + Rect2 result = {0, 0, WindowSize.x, WindowSize.y}; + return result; +} + +Rect2I GetScreenRectI() { + Rect2I result = {0, 0, (Int)WindowSize.x, (Int)WindowSize.y}; + return result; +} +void BeginFrameRender() { Clear(&RenderArena); TotalVertexCount = 0; Vertices.first = NULL; Vertices.last = NULL; - WindowSize = window_size; + CurrentScissor = GetScreenRectF(); } void EndFrameRender(Color color) { @@ -50,8 +61,9 @@ void EndFrameRender(Color color) { glDisable(GL_CULL_FACE); glViewport(0, 0, (GLsizei)WindowSize.x, (GLsizei)WindowSize.y); + glScissor(0, 0, (GLsizei)WindowSize.x, (GLsizei)WindowSize.y); glClearColor(color.r, color.g, color.b, color.a); - glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT); // Default draw using the font texture glBindProgramPipeline(Shader2D.pipeline); @@ -59,6 +71,12 @@ void EndFrameRender(Color color) { float yinverse = 1.f / (WindowSize.y / 2.f); glProgramUniform2f(Shader2D.vshader, 0, xinverse, yinverse); for (VertexNode2D *it = Vertices.first; it; it = it->next) { + Rect2 rect = it->scissor; + GLint x = (GLint)rect.min.x; + GLint y = (GLint)rect.min.y; + GLsizei w = (GLsizei)(rect.max.x - rect.min.x); + GLsizei h = (GLsizei)(rect.max.y - rect.min.y); + glScissor(x, (GLint)WindowSize.y - h, w, h); glNamedBufferSubData(VBO, 0, it->count * sizeof(Vertex2D), it->vertices); glBindVertexArray(VAO); GLint s_texture = 0; // texture unit that sampler2D will use in GLSL code @@ -183,11 +201,39 @@ Shader CreateShader(char *glsl_vshader, char *glsl_fshader) { } while (0) #define SLL_QUEUE_ADD(f, l, n) SLL_QUEUE_ADD_MOD(f, l, n, next) +VertexNode2D *AllocVertexNode2D(Allocator allocator, VertexList2D *list) { + VertexNode2D *node = AllocType(allocator, VertexNode2D); + SLL_QUEUE_ADD(list->first, list->last, node); + return node; +} + +void SetScissor(Rect2 rect) { + CurrentScissor = rect; + + VertexNode2D *node = Vertices.last; + if (!node) { + node = AllocVertexNode2D(RenderArena, &Vertices); + node->scissor = rect; + return; + } + + if (node->scissor != rect && node->count == 0) { + node->scissor = rect; + return; + } + + if (node->scissor != rect) { + node = AllocVertexNode2D(RenderArena, &Vertices); + node->scissor = rect; + return; + } +} + Vertex2D *AllocVertex2D(Allocator allocator, VertexList2D *list, int count) { VertexNode2D *node = list->last; if (node == 0 || node->count + count > Lengthof(node->vertices)) { - node = AllocType(allocator, VertexNode2D); - SLL_QUEUE_ADD(list->first, list->last, node); + node = AllocVertexNode2D(allocator, list); + node->scissor = CurrentScissor; } TotalVertexCount += count; @@ -249,7 +295,7 @@ void DrawRect(Rect2 rect, Color color) { } Vec2 DrawString(Font *font, String16 string, Vec2 pos, Color color, bool draw = true) { - pos.y += font->ascent; + pos.y += font->ascent - font->descent; Vec2 original_pos = pos; For(string) { Glyph *g = GetGlyph(font, it); @@ -277,25 +323,3 @@ Int GetLineSpacing(Font *font) { Int result = (Int)(font->ascent - font->descent + font->line_gap); return result; } - -void BeginScissor(Rect2 rect) { - glScissor((GLint)rect.min.x, (GLint)rect.min.y, (GLsizei)(rect.max.x - rect.min.x), (GLsizei)(rect.max.y - rect.min.y)); -} - -void BeginScissor(Rect2I rect) { - glScissor((GLint)rect.min.x, (GLint)rect.min.y, (GLsizei)(rect.max.x - rect.min.x), (GLsizei)(rect.max.y - rect.min.y)); -} - -void EndScissor() { - glScissor(0, 0, (GLsizei)WindowSize.x, (GLsizei)WindowSize.y); -} - -Rect2 GetScreenRectF() { - Rect2 result = {0, 0, WindowSize.x, WindowSize.y}; - return result; -} - -Rect2I GetScreenRectI() { - Rect2I result = {0, 0, (Int)WindowSize.x, (Int)WindowSize.y}; - return result; -} \ No newline at end of file diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 91d827a..19ea902 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -1,69 +1,3 @@ -inline bool Press(int key) { - bool result = IsKeyPressed(key) || IsKeyPressedRepeat(key); - return result; -} - -inline bool Shift() { - bool result = IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT); - return result; -} - -inline bool Ctrl() { - bool result = IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL); - return result; -} - -inline bool Alt() { - bool result = IsKeyDown(KEY_LEFT_ALT) || IsKeyDown(KEY_RIGHT_ALT); - return result; -} - -inline bool AltPress(int key) { - bool result = Press(key) && Alt(); - return result; -} - -inline bool CtrlPress(int key) { - bool result = Press(key) && Ctrl(); - return result; -} - -inline bool ShiftPress(int key) { - bool result = Press(key) && Shift(); - return result; -} - -inline bool CtrlShiftPress(int key) { - bool result = Press(key) && Shift() && Ctrl(); - return result; -} - -inline bool CtrlAltPress(int key) { - bool result = Press(key) && Ctrl() && Alt(); - return result; -} - -inline bool AltShiftPress(int key) { - bool result = Press(key) && Shift() && Alt(); - return result; -} - -bool IsDoubleClick() { - static double last_click_time; - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - double time = GetTime(); - double diff = time - last_click_time; - last_click_time = time; - // @todo: don't do this manually, use windows - if (diff >= 0.05 && diff <= 0.3) { - last_click_time = 0; - return true; - } - } else if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { - last_click_time = 0; - } - return false; -} Int MoveOnWhitespaceBoundaryForward(Buffer &buffer, Int pos) { pos = Clamp(pos, (Int)0, buffer.len); @@ -209,19 +143,6 @@ 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); -} - Caret FindInBuffer(Buffer *buffer, String16 needle, Caret caret, bool find_next = false) { Int pos = caret.range.min; String16 medium = GetString(*buffer, {pos, INT64_MAX}); @@ -261,39 +182,39 @@ Array FindAllInBuffer(Allocator allocator, Buffer *buffer, String16 needl return result; } -void HandleGlobalCommands() { - if (CtrlPress(KEY_P)) { - Window *command_window = GetWindow(CommandWindowID); - if (command_window->visible) { - SetActiveWindow(GetLastActiveWindow()); - } else { - SetActiveWindow(command_window->id); - } - } +// void HandleGlobalCommands() { +// if (CtrlPress(KEY_P)) { +// Window *command_window = GetWindow(CommandWindowID); +// if (command_window->visible) { +// SetActiveWindow(GetLastActiveWindow()); +// } else { +// SetActiveWindow(command_window->id); +// } +// } - if (CtrlPress(KEY_F)) { - Window *search_window = GetWindow(SearchWindowID); - if (search_window->visible) { - SetActiveWindow(GetLastActiveWindow()); - } else { - SetActiveWindow(search_window->id); - } - } +// if (CtrlPress(KEY_F)) { +// Window *search_window = GetWindow(SearchWindowID); +// if (search_window->visible) { +// SetActiveWindow(GetLastActiveWindow()); +// } else { +// SetActiveWindow(search_window->id); +// } +// } - 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_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)) { - SetActiveWindow({1}); - } else if (CtrlPress(KEY_THREE)) { - SetActiveWindow({2}); - } -} +// if (CtrlPress(KEY_ONE)) { +// SetActiveWindow({0}); +// } else if (CtrlPress(KEY_TWO)) { +// SetActiveWindow({1}); +// } else if (CtrlPress(KEY_THREE)) { +// SetActiveWindow({2}); +// } +// } diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 6ae250a..fcd7c18 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -231,342 +231,342 @@ void MergeCarets(View *view, Range *mouse_selection_anchor) { void HandleActiveWindowBindings(Window *window, bool *update_scroll) { View &view = *GetActiveView(window); Buffer *buffer = GetBuffer(view.active_buffer); - if (CtrlPress(KEY_F2)) { - LoadBigLine(buffer); - } else if (Press(KEY_F2)) { - LoadBigText(buffer); - } + // if (CtrlPress(KEY_F2)) { + // LoadBigLine(buffer); + // } else if (Press(KEY_F2)) { + // LoadBigText(buffer); + // } - if (Press(KEY_ESCAPE)) { - if (window->deactivate_on_escape) { - SetActiveWindow(GetLastActiveWindow()); - } else { - view.carets.len = 1; - } - } + // if (Press(KEY_ESCAPE)) { + // if (window->deactivate_on_escape) { + // SetActiveWindow(GetLastActiveWindow()); + // } else { + // view.carets.len = 1; + // } + // } - if (CtrlAltPress(KEY_DOWN)) { - Command_DuplicateLine(&view, DIR_DOWN); - } else if (AltShiftPress(KEY_DOWN)) { - Command_CreateCursorVertical(&view, DIR_DOWN); - } else if (CtrlShiftPress(KEY_DOWN)) { - For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_DOWN, true)); - } else if (CtrlPress(KEY_DOWN)) { - For(view.carets) it = MakeCaret(MovePos(*buffer, it.range.max, DIR_DOWN, true)); - } else if (ShiftPress(KEY_DOWN)) { - For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_DOWN, false)); - } else if (Press(KEY_DOWN)) { - For(view.carets) { - if (GetSize(it.range) == 0) { - it = MakeCaret(MovePos(*buffer, it.range.max, DIR_DOWN, false)); - } else { - it = MakeCaret(it.range.max); - } - } - } + // if (CtrlAltPress(KEY_DOWN)) { + // Command_DuplicateLine(&view, DIR_DOWN); + // } else if (AltShiftPress(KEY_DOWN)) { + // Command_CreateCursorVertical(&view, DIR_DOWN); + // } else if (CtrlShiftPress(KEY_DOWN)) { + // For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_DOWN, true)); + // } else if (CtrlPress(KEY_DOWN)) { + // For(view.carets) it = MakeCaret(MovePos(*buffer, it.range.max, DIR_DOWN, true)); + // } else if (ShiftPress(KEY_DOWN)) { + // For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_DOWN, false)); + // } else if (Press(KEY_DOWN)) { + // For(view.carets) { + // if (GetSize(it.range) == 0) { + // it = MakeCaret(MovePos(*buffer, it.range.max, DIR_DOWN, false)); + // } else { + // it = MakeCaret(it.range.max); + // } + // } + // } - if (CtrlAltPress(KEY_UP)) { - Command_DuplicateLine(&view, DIR_UP); - } else if (AltShiftPress(KEY_UP)) { - Command_CreateCursorVertical(&view, DIR_UP); - } else if (CtrlShiftPress(KEY_UP)) { - For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_UP, CTRL_PRESSED)); - } else if (CtrlPress(KEY_UP)) { - For(view.carets) it = MakeCaret(MovePos(*buffer, it.range.min, DIR_UP, CTRL_PRESSED)); - } else if (ShiftPress(KEY_UP)) { - For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_UP)); - } else if (Press(KEY_UP)) { - For(view.carets) { - if (GetSize(it.range) == 0) { - it = MakeCaret(MovePos(*buffer, it.range.min, DIR_UP)); - } else { - it = MakeCaret(it.range.min); - } - } - } + // if (CtrlAltPress(KEY_UP)) { + // Command_DuplicateLine(&view, DIR_UP); + // } else if (AltShiftPress(KEY_UP)) { + // Command_CreateCursorVertical(&view, DIR_UP); + // } else if (CtrlShiftPress(KEY_UP)) { + // For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_UP, CTRL_PRESSED)); + // } else if (CtrlPress(KEY_UP)) { + // For(view.carets) it = MakeCaret(MovePos(*buffer, it.range.min, DIR_UP, CTRL_PRESSED)); + // } else if (ShiftPress(KEY_UP)) { + // For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_UP)); + // } else if (Press(KEY_UP)) { + // For(view.carets) { + // if (GetSize(it.range) == 0) { + // it = MakeCaret(MovePos(*buffer, it.range.min, DIR_UP)); + // } else { + // it = MakeCaret(it.range.min); + // } + // } + // } - if (CtrlShiftPress(KEY_LEFT)) { - For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_LEFT, true)); - } else if (CtrlPress(KEY_LEFT)) { - For(view.carets) { - if (GetSize(it.range) != 0 && GetFront(it) != it.range.min) { - it = MakeCaret(it.range.min); - } else { - it = MakeCaret(MovePos(*buffer, it.range.min, DIR_LEFT, true)); - } - } - } else if (ShiftPress(KEY_LEFT)) { - For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_LEFT, false)); - } else if (Press(KEY_LEFT)) { - For(view.carets) { - if (GetSize(it.range) == 0) { - it = MakeCaret(MovePos(*buffer, it.range.min, DIR_LEFT, false)); - } else { - it = MakeCaret(it.range.min); - } - } - } + // if (CtrlShiftPress(KEY_LEFT)) { + // For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_LEFT, true)); + // } else if (CtrlPress(KEY_LEFT)) { + // For(view.carets) { + // if (GetSize(it.range) != 0 && GetFront(it) != it.range.min) { + // it = MakeCaret(it.range.min); + // } else { + // it = MakeCaret(MovePos(*buffer, it.range.min, DIR_LEFT, true)); + // } + // } + // } else if (ShiftPress(KEY_LEFT)) { + // For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_LEFT, false)); + // } else if (Press(KEY_LEFT)) { + // For(view.carets) { + // if (GetSize(it.range) == 0) { + // it = MakeCaret(MovePos(*buffer, it.range.min, DIR_LEFT, false)); + // } else { + // it = MakeCaret(it.range.min); + // } + // } + // } - if (CtrlShiftPress(KEY_RIGHT)) { - For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_RIGHT, true)); - } else if (CtrlPress(KEY_RIGHT)) { - For(view.carets) { - if (GetSize(it.range) != 0 && GetFront(it) != it.range.max) { - it = MakeCaret(it.range.max); - } else { - it = MakeCaret(MovePos(*buffer, it.range.max, DIR_RIGHT, true)); - } - } - } else if (ShiftPress(KEY_RIGHT)) { - For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_RIGHT, false)); - } else if (Press(KEY_RIGHT)) { - For(view.carets) { - if (GetSize(it.range) == 0) { - it = MakeCaret(MovePos(*buffer, it.range.max, DIR_RIGHT, false)); - } else { - it = MakeCaret(it.range.max); - } - } - } + // if (CtrlShiftPress(KEY_RIGHT)) { + // For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_RIGHT, true)); + // } else if (CtrlPress(KEY_RIGHT)) { + // For(view.carets) { + // if (GetSize(it.range) != 0 && GetFront(it) != it.range.max) { + // it = MakeCaret(it.range.max); + // } else { + // it = MakeCaret(MovePos(*buffer, it.range.max, DIR_RIGHT, true)); + // } + // } + // } else if (ShiftPress(KEY_RIGHT)) { + // For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_RIGHT, false)); + // } else if (Press(KEY_RIGHT)) { + // For(view.carets) { + // if (GetSize(it.range) == 0) { + // it = MakeCaret(MovePos(*buffer, it.range.max, DIR_RIGHT, false)); + // } else { + // it = MakeCaret(it.range.max); + // } + // } + // } - if (CtrlShiftPress(KEY_Z)) { - RedoEdit(buffer, &view.carets); - } else if (CtrlPress(KEY_Z)) { - UndoEdit(buffer, &view.carets); - } + // if (CtrlShiftPress(KEY_Z)) { + // RedoEdit(buffer, &view.carets); + // } else if (CtrlPress(KEY_Z)) { + // UndoEdit(buffer, &view.carets); + // } - if (CtrlPress(KEY_C)) { - Command_Copy(&view); - } else if (CtrlPress(KEY_V)) { - Command_Paste(&view); - } else if (CtrlPress(KEY_X)) { - Command_Copy(&view); - Command_Replace(&view, L""); - } + // if (CtrlPress(KEY_C)) { + // Command_Copy(&view); + // } else if (CtrlPress(KEY_V)) { + // Command_Paste(&view); + // } else if (CtrlPress(KEY_X)) { + // Command_Copy(&view); + // Command_Replace(&view, L""); + // } - if (CtrlPress(KEY_A)) { - Command_SelectEntireBuffer(&view); - *update_scroll = false; - } + // if (CtrlPress(KEY_A)) { + // Command_SelectEntireBuffer(&view); + // *update_scroll = false; + // } - if (ShiftPress(KEY_PAGE_UP)) { - Command_MoveCursorsByPageSize(window, DIR_UP, SHIFT_PRESSED); - } else if (Press(KEY_PAGE_UP)) { - Command_MoveCursorsByPageSize(window, DIR_UP); - } + // if (ShiftPress(KEY_PAGE_UP)) { + // Command_MoveCursorsByPageSize(window, DIR_UP, SHIFT_PRESSED); + // } else if (Press(KEY_PAGE_UP)) { + // Command_MoveCursorsByPageSize(window, DIR_UP); + // } - if (ShiftPress(KEY_PAGE_DOWN)) { - Command_MoveCursorsByPageSize(window, DIR_DOWN, SHIFT_PRESSED); - } else if (Press(KEY_PAGE_DOWN)) { - Command_MoveCursorsByPageSize(window, DIR_DOWN); - } + // if (ShiftPress(KEY_PAGE_DOWN)) { + // Command_MoveCursorsByPageSize(window, DIR_DOWN, SHIFT_PRESSED); + // } else if (Press(KEY_PAGE_DOWN)) { + // Command_MoveCursorsByPageSize(window, DIR_DOWN); + // } - if (ShiftPress(KEY_HOME)) { - Command_MoveCursorsToSide(window, DIR_LEFT, SHIFT_PRESSED); - } else if (Press(KEY_HOME)) { - Command_MoveCursorsToSide(window, DIR_LEFT); - } + // if (ShiftPress(KEY_HOME)) { + // Command_MoveCursorsToSide(window, DIR_LEFT, SHIFT_PRESSED); + // } else if (Press(KEY_HOME)) { + // Command_MoveCursorsToSide(window, DIR_LEFT); + // } - if (ShiftPress(KEY_END)) { - Command_MoveCursorsToSide(window, DIR_RIGHT, SHIFT_PRESSED); - } else if (Press(KEY_END)) { - Command_MoveCursorsToSide(window, DIR_RIGHT); - } + // if (ShiftPress(KEY_END)) { + // Command_MoveCursorsToSide(window, DIR_RIGHT, SHIFT_PRESSED); + // } else if (Press(KEY_END)) { + // Command_MoveCursorsToSide(window, DIR_RIGHT); + // } - bool search = false; - if (Press(KEY_TAB)) { - Command_Replace(&view, L" "); - search = true; - } + // bool search = false; + // if (Press(KEY_TAB)) { + // Command_Replace(&view, L" "); + // search = true; + // } - if (CtrlPress(KEY_BACKSPACE)) { - Command_Delete(&view, DIR_LEFT, CTRL_PRESSED); - search = true; - } else if (Press(KEY_BACKSPACE)) { - Command_Delete(&view, DIR_LEFT); - search = true; - } + // if (CtrlPress(KEY_BACKSPACE)) { + // Command_Delete(&view, DIR_LEFT, CTRL_PRESSED); + // search = true; + // } else if (Press(KEY_BACKSPACE)) { + // Command_Delete(&view, DIR_LEFT); + // search = true; + // } - if (CtrlPress(KEY_DELETE)) { - Command_Delete(&view, DIR_RIGHT, CTRL_PRESSED); - search = true; - } else if (Press(KEY_DELETE)) { - Command_Delete(&view, DIR_RIGHT); - search = true; - } + // if (CtrlPress(KEY_DELETE)) { + // Command_Delete(&view, DIR_RIGHT, CTRL_PRESSED); + // search = true; + // } else if (Press(KEY_DELETE)) { + // Command_Delete(&view, DIR_RIGHT); + // search = true; + // } - for (int c = GetCharPressed(); c; c = GetCharPressed()) { - // we interpret 2 byte sequences as 1 byte when rendering but we still - // want to read them properly. - String16 string = L"?"; - UTF16Result result = UTF32ToUTF16((uint32_t)c); - if (!result.error) string = {(wchar_t *)result.out_str, result.len}; - Command_Replace(&view, string); - search = true; - } + // for (int c = GetCharPressed(); c; c = GetCharPressed()) { + // // we interpret 2 byte sequences as 1 byte when rendering but we still + // // want to read them properly. + // String16 string = L"?"; + // UTF16Result result = UTF32ToUTF16((uint32_t)c); + // if (!result.error) string = {(wchar_t *)result.out_str, result.len}; + // Command_Replace(&view, string); + // search = true; + // } - if (CtrlPress(KEY_D)) { - String16 string = GetString(*buffer, view.carets[0].range); - Caret caret = FindInBuffer(buffer, string, view.carets[0], true); - Insert(&view.carets, caret, 0); - MergeCarets(&view); - } + // if (CtrlPress(KEY_D)) { + // String16 string = GetString(*buffer, view.carets[0].range); + // Caret caret = FindInBuffer(buffer, string, view.carets[0], true); + // Insert(&view.carets, caret, 0); + // MergeCarets(&view); + // } - if (Press(KEY_F3)) { - Buffer *search_buffer = GetBuffer("*search*"); - String16 search_string = GetString(*search_buffer); - Caret caret = FindInBuffer(buffer, search_string, view.carets[0], true); - if (Ctrl()) { - Insert(&view.carets, caret, 0); - MergeCarets(&view); - } else { - view.carets.len = 1; - view.carets[0] = caret; - } - } + // if (Press(KEY_F3)) { + // Buffer *search_buffer = GetBuffer("*search*"); + // String16 search_string = GetString(*search_buffer); + // Caret caret = FindInBuffer(buffer, search_string, view.carets[0], true); + // if (Ctrl()) { + // Insert(&view.carets, caret, 0); + // MergeCarets(&view); + // } else { + // view.carets.len = 1; + // view.carets[0] = caret; + // } + // } - if (window->id.id == SearchWindowID.id) { - Window *seek_window = GetWindow(GetLastActiveWindow()); - View *seek_view = GetView(seek_window->active_view); - Buffer *seek_buffer = GetBuffer(seek_view->active_buffer); - String16 needle = GetString(*buffer); - if (search) { - seek_view->carets[0] = FindInBuffer(seek_buffer, needle, seek_view->carets[0]); - seek_view->carets.len = 1; - } - if (AltPress(KEY_ENTER)) { - Command_SelectAll(seek_view, needle); - // SetActiveWindow(seek_window->id); - } else if (Press(KEY_ENTER)) { - Caret caret = FindInBuffer(seek_buffer, needle, seek_view->carets[0], true); - if (Ctrl()) { - Insert(&seek_view->carets, caret, 0); - MergeCarets(seek_view); - } else { - seek_view->carets.len = 1; - seek_view->carets[0] = caret; - } - } - } + // if (window->id.id == SearchWindowID.id) { + // Window *seek_window = GetWindow(GetLastActiveWindow()); + // View *seek_view = GetView(seek_window->active_view); + // Buffer *seek_buffer = GetBuffer(seek_view->active_buffer); + // String16 needle = GetString(*buffer); + // if (search) { + // seek_view->carets[0] = FindInBuffer(seek_buffer, needle, seek_view->carets[0]); + // seek_view->carets.len = 1; + // } + // if (AltPress(KEY_ENTER)) { + // Command_SelectAll(seek_view, needle); + // // SetActiveWindow(seek_window->id); + // } else if (Press(KEY_ENTER)) { + // Caret caret = FindInBuffer(seek_buffer, needle, seek_view->carets[0], true); + // if (Ctrl()) { + // Insert(&seek_view->carets, caret, 0); + // MergeCarets(seek_view); + // } else { + // seek_view->carets.len = 1; + // seek_view->carets[0] = caret; + // } + // } + // } - if (window->fuzzy_search && search) { - Scratch scratch; - String16 first_line_string = GetLineStringWithoutNL(*buffer, 0); - Array ratings = FuzzySearchLines(scratch, buffer, 1, buffer->line_starts.len, first_line_string); + // if (window->fuzzy_search && search) { + // Scratch scratch; + // String16 first_line_string = GetLineStringWithoutNL(*buffer, 0); + // Array ratings = FuzzySearchLines(scratch, buffer, 1, buffer->line_starts.len, first_line_string); - Buffer *temp_buffer = CreateTempBuffer(scratch, buffer->cap); - ReplaceText(temp_buffer, GetEndAsRange(*temp_buffer), first_line_string); - ReplaceText(temp_buffer, GetEndAsRange(*temp_buffer), L"\n"); - For(ratings) { - String16 s = GetLineStringWithoutNL(*buffer, it.index); - if (s.len == 0) continue; - ReplaceText(temp_buffer, GetEndAsRange(*temp_buffer), s); - ReplaceText(temp_buffer, GetEndAsRange(*temp_buffer), L"\n"); - } + // Buffer *temp_buffer = CreateTempBuffer(scratch, buffer->cap); + // ReplaceText(temp_buffer, GetEndAsRange(*temp_buffer), first_line_string); + // ReplaceText(temp_buffer, GetEndAsRange(*temp_buffer), L"\n"); + // For(ratings) { + // String16 s = GetLineStringWithoutNL(*buffer, it.index); + // if (s.len == 0) continue; + // ReplaceText(temp_buffer, GetEndAsRange(*temp_buffer), s); + // ReplaceText(temp_buffer, GetEndAsRange(*temp_buffer), L"\n"); + // } - Caret caret = view.carets[0]; - Command_SelectEntireBuffer(&view); - Command_Replace(&view, GetString(*temp_buffer)); - view.carets[0] = caret; - } + // Caret caret = view.carets[0]; + // Command_SelectEntireBuffer(&view); + // Command_Replace(&view, GetString(*temp_buffer)); + // view.carets[0] = caret; + // } - if (CtrlPress(KEY_Q) || IsMouseButtonPressed(MOUSE_BUTTON_MIDDLE)) { - // @todo: Consider applying this to all cursors - if (GetSize(view.carets[0].range) == 0) { - Command_EvalLuaLine(&view); - } else { - String16 string = GetString(*buffer, view.carets[0].range); - Command_EvalLua(&view, string); - } - } + // if (CtrlPress(KEY_Q) || IsMouseButtonPressed(MOUSE_BUTTON_MIDDLE)) { + // // @todo: Consider applying this to all cursors + // if (GetSize(view.carets[0].range) == 0) { + // Command_EvalLuaLine(&view); + // } else { + // String16 string = GetString(*buffer, view.carets[0].range); + // Command_EvalLua(&view, string); + // } + // } - if (window->execute_line) { - if (Press(KEY_ENTER)) { - Command_EvalLuaLine(&view); - } - } else if (window->id.id == SearchWindowID.id) { - } else { - if (CtrlPress(KEY_ENTER)) { - Command_MoveCursorsToSide(window, DIR_RIGHT); - Command_Replace(&view, L"\n"); - } else if (Press(KEY_ENTER)) { - Command_Replace(&view, L"\n"); - } - } - { - ProfileScope(mouse); + // if (window->execute_line) { + // if (Press(KEY_ENTER)) { + // Command_EvalLuaLine(&view); + // } + // } else if (window->id.id == SearchWindowID.id) { + // } else { + // if (CtrlPress(KEY_ENTER)) { + // Command_MoveCursorsToSide(window, DIR_RIGHT); + // Command_Replace(&view, L"\n"); + // } else if (Press(KEY_ENTER)) { + // Command_Replace(&view, L"\n"); + // } + // } + // { + // ProfileScope(mouse); - Vec2 _mouse = GetMousePosition(); - bool mouse_in_view = CheckCollisionPointRec(_mouse, ToRectangle(window->document_rect)); - bool mouse_in_scrollbar = CheckCollisionPointRec(_mouse, ToRectangle(window->scrollbar_rect)); - Vec2I mouse = ToVec2I(_mouse); + // Vec2 _mouse = GetMousePosition(); + // bool mouse_in_view = CheckCollisionPointRec(_mouse, ToRectangle(window->document_rect)); + // bool mouse_in_scrollbar = CheckCollisionPointRec(_mouse, ToRectangle(window->scrollbar_rect)); + // Vec2I mouse = ToVec2I(_mouse); - if (!(mouse_in_scrollbar || window->mouse_selecting_scrollbar) && (mouse_in_view || window->mouse_selecting)) { - Vec2I mworld = mouse - window->document_rect.min + view.scroll; - Vec2I pos = mworld / Vec2I{FontCharSpacing, FontLineSpacing}; - XY xy = {(Int)(pos.x), (Int)(pos.y)}; - Int p = XYToPosWithoutNL(*buffer, xy); + // if (!(mouse_in_scrollbar || window->mouse_selecting_scrollbar) && (mouse_in_view || window->mouse_selecting)) { + // Vec2I mworld = mouse - window->document_rect.min + view.scroll; + // Vec2I pos = mworld / Vec2I{FontCharSpacing, FontLineSpacing}; + // XY xy = {(Int)(pos.x), (Int)(pos.y)}; + // Int p = XYToPosWithoutNL(*buffer, xy); - if (mouse_in_view && IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - if (IsDoubleClick()) { - Caret *c = &view.carets[0]; - if (InBounds({c->range.min - 1, c->range.max + 1}, p)) { - c->range = EncloseWord(*buffer, p); - view.selection_anchor = c->range; - } - } else { - if (!IsKeyDown(KEY_LEFT_CONTROL)) { - view.carets.len = 0; - } - Insert(&view.carets, MakeCaret(p, p), 0); - view.selection_anchor = view.carets[0].range; - } - } else if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { - window->mouse_selecting = true; - } - } + // if (mouse_in_view && IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { + // if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + // if (IsDoubleClick()) { + // Caret *c = &view.carets[0]; + // if (InBounds({c->range.min - 1, c->range.max + 1}, p)) { + // c->range = EncloseWord(*buffer, p); + // view.selection_anchor = c->range; + // } + // } else { + // if (!IsKeyDown(KEY_LEFT_CONTROL)) { + // view.carets.len = 0; + // } + // Insert(&view.carets, MakeCaret(p, p), 0); + // view.selection_anchor = view.carets[0].range; + // } + // } else if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { + // window->mouse_selecting = true; + // } + // } - if (window->mouse_selecting) { - if (!IsMouseButtonDown(MOUSE_BUTTON_LEFT)) window->mouse_selecting = false; - MergeCarets(&view, &view.selection_anchor); - Caret &caret = view.carets[0]; - if (view.selection_anchor.min > p) { - caret = MakeCaret(p, view.selection_anchor.max); - } else if (view.selection_anchor.max < p) { - caret = MakeCaret(p, view.selection_anchor.min); - } else { - caret = MakeCaret(view.selection_anchor.max, view.selection_anchor.min); - } - MergeCarets(&view, &view.selection_anchor); - } - } else if (!(mouse_in_view || window->mouse_selecting) && mouse_in_scrollbar || window->mouse_selecting_scrollbar) { - Scroller s = ComputeScrollerRect(*window); - double size_y = (double)GetSize(window->scrollbar_rect).y; - double p = _mouse.y - window->scrollbar_rect.min.y; + // if (window->mouse_selecting) { + // if (!IsMouseButtonDown(MOUSE_BUTTON_LEFT)) window->mouse_selecting = false; + // MergeCarets(&view, &view.selection_anchor); + // Caret &caret = view.carets[0]; + // if (view.selection_anchor.min > p) { + // caret = MakeCaret(p, view.selection_anchor.max); + // } else if (view.selection_anchor.max < p) { + // caret = MakeCaret(p, view.selection_anchor.min); + // } else { + // caret = MakeCaret(view.selection_anchor.max, view.selection_anchor.min); + // } + // MergeCarets(&view, &view.selection_anchor); + // } + // } else if (!(mouse_in_view || window->mouse_selecting) && mouse_in_scrollbar || window->mouse_selecting_scrollbar) { + // Scroller s = ComputeScrollerRect(*window); + // double size_y = (double)GetSize(window->scrollbar_rect).y; + // double p = _mouse.y - window->scrollbar_rect.min.y; - if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { - window->mouse_selecting_scrollbar = true; - } else if (!IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { - window->mouse_selecting_scrollbar = false; - } + // if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { + // window->mouse_selecting_scrollbar = true; + // } else if (!IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { + // window->mouse_selecting_scrollbar = false; + // } - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - if (_mouse.y < s.rect.min.y || _mouse.y > s.rect.max.y) { - view.scroll.y = (Int)(p / size_y * (double)s.line_count * (double)FontLineSpacing); - window->mouse_scroller_offset = -(double)GetSize(s.rect).y / 2.0 / size_y; - } else { - window->mouse_scroller_offset = (s.rect.min.y - p) / size_y; - } - } + // if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + // if (_mouse.y < s.rect.min.y || _mouse.y > s.rect.max.y) { + // view.scroll.y = (Int)(p / size_y * (double)s.line_count * (double)FontLineSpacing); + // window->mouse_scroller_offset = -(double)GetSize(s.rect).y / 2.0 / size_y; + // } else { + // window->mouse_scroller_offset = (s.rect.min.y - p) / size_y; + // } + // } - if (window->mouse_selecting_scrollbar) { - double v = p / size_y; - v = v + (window->mouse_scroller_offset); - view.scroll.y = (Int)(v * (double)s.line_count * (double)FontLineSpacing); - } - } - } + // if (window->mouse_selecting_scrollbar) { + // double v = p / size_y; + // v = v + (window->mouse_scroller_offset); + // view.scroll.y = (Int)(v * (double)s.line_count * (double)FontLineSpacing); + // } + // } + // } MergeCarets(&view); } @@ -638,24 +638,24 @@ void ChangeActiveWindowAndScroll(Array &order) { View *view = GetActiveView(window); view->main_caret_on_begin_frame = view->carets[0]; - Vec2 mouse = GetMousePosition(); - bool mouse_in_window = CheckCollisionPointRec(mouse, ToRectangle(window->total_rect)); - if (mouse_in_window) { - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - Window *active_window = GetWindow(ActiveWindow); - if (active_window->z <= window->z) { - SetActiveWindow(window->id); - } - } - if (LastFrameIDWhenScrolled != FrameID) { - if (IsKeyDown(KEY_F1)) { - view->scroll.x -= (Int)(GetMouseWheelMove() * 48); - } else { - view->scroll.y -= (Int)(GetMouseWheelMove() * 48); - } - LastFrameIDWhenScrolled = FrameID; - } - } + // Vec2 mouse = GetMousePosition(); + // bool mouse_in_window = CheckCollisionPointRec(mouse, ToRectangle(window->total_rect)); + // if (mouse_in_window) { + // if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + // Window *active_window = GetWindow(ActiveWindow); + // if (active_window->z <= window->z) { + // SetActiveWindow(window->id); + // } + // } + // if (LastFrameIDWhenScrolled != FrameID) { + // if (IsKeyDown(KEY_F1)) { + // view->scroll.x -= (Int)(GetMouseWheelMove() * 48); + // } else { + // view->scroll.y -= (Int)(GetMouseWheelMove() * 48); + // } + // LastFrameIDWhenScrolled = FrameID; + // } + // } } } diff --git a/src/text_editor/new_text_editor.cpp b/src/text_editor/new_text_editor.cpp index 110fc17..6fc846b 100644 --- a/src/text_editor/new_text_editor.cpp +++ b/src/text_editor/new_text_editor.cpp @@ -26,6 +26,8 @@ #include "management.cpp" #include "window.cpp" +#include "commands.cpp" +#include "commands_window.cpp" #include "colors.cpp" #include "window_draw.cpp" @@ -139,135 +141,14 @@ int main() FontLineSpacing = GetLineSpacing(&MainFont); } - Allocator sys_allocator = GetSystemAllocator(); - { - Buffer *buffer = CreateBuffer(sys_allocator, "*scratch*"); - } - - { - Window *w = CreateWindow(); - Buffer *b = CreateBuffer(sys_allocator, "*load_text_a*"); - View *v = CreateView(b->id); - LoadTextA(b); - AddView(w, v->id); - } - SetActiveWindow({1}); - - { - Window *w = CreateWindow(); - Buffer *b = CreateBuffer(sys_allocator, "*load_unicode*"); - View *v = CreateView(b->id); - LoadUnicode(b); - AddView(w, v->id); - } - { - Window *w = CreateWindow(); - Buffer *b = GetBuffer({0}); - View *v = CreateView(b->id); - AddView(w, v->id); - } - { - Window *w = CreateWindow(); - w->draw_scrollbar = false; - w->draw_line_numbers = false; - w->dont_save_in_active_window_history = true; - w->deactivate_on_escape = true; - Buffer *b = CreateBuffer(sys_allocator, "*infobar*"); - b->no_history = true; - View *v = CreateView(b->id); - AddView(w, v->id); - InfoBarWindowID = w->id; - } - - { - Window *w = CreateWindow(); - w->draw_scrollbar = false; - w->draw_line_numbers = false; - w->visible = false; - w->fuzzy_search = true; - w->execute_line = true; - w->invisible_when_inactive = true; - w->dont_save_in_active_window_history = true; - w->deactivate_on_escape = true; - Buffer *b = CreateBuffer(sys_allocator, "*commands*"); - View *v = CreateView(b->id); - AddView(w, v->id); - CommandWindowID = w->id; - - // Command_EvalLua(v, L"open \"./\""); - } - - { - Window *w = CreateWindow(); - w->draw_scrollbar = false; - w->draw_line_numbers = false; - w->visible = false; - w->dont_save_in_active_window_history = true; - w->invisible_when_inactive = true; - w->deactivate_on_escape = true; - Buffer *b = CreateBuffer(sys_allocator, "*search*"); - View *v = CreateView(b->id); - AddView(w, v->id); - SearchWindowID = w->id; - } - + InitWindows(); while (AppIsRunning) { FrameID += 1; int window_x, window_y; SDL_GetWindowSize(window, &window_x, &window_y); - Vec2 window_size = {(float)window_x, (float)window_y}; - BeginFrameRender(window_size); - - Rect2I screen_rect = GetScreenRectI(); - Rect2I infobar_rect = CutBottom(&screen_rect, (Int)FontLineSpacing); - float line_numbers_size = GetStringSize(&MainFont, L"1234567891").x; - { - int i = 5; - if (Windows[i].visible) { - Rect2I rect = CutBottom(&screen_rect, FontLineSpacing); - Windows[i].total_rect = rect; - Windows[i].document_rect = Windows[i].total_rect; - } - } - { - int i = 0; - Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 0.33)); - Windows[i].document_rect = Windows[i].total_rect; - if (Windows[i].draw_scrollbar) Windows[i].scrollbar_rect = CutRight(&Windows[i].document_rect, 10); - if (Windows[i].draw_line_numbers) Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size); - } - { - int i = 1; - Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 0.5)); - Windows[i].document_rect = Windows[i].total_rect; - if (Windows[i].draw_scrollbar) Windows[i].scrollbar_rect = CutRight(&Windows[i].document_rect, 10); - if (Windows[i].draw_line_numbers) Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size); - } - { - int i = 2; - Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 1.0)); - Windows[i].document_rect = Windows[i].total_rect; - if (Windows[i].draw_scrollbar) Windows[i].scrollbar_rect = CutRight(&Windows[i].document_rect, 10); - if (Windows[i].draw_line_numbers) Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size); - } - { - int i = 3; - Windows[i].total_rect = infobar_rect; - Windows[i].document_rect = Windows[i].total_rect; - } - { - int i = 4; - Rect2 screen_rect = GetScreenRectF(); - Vec2 size = GetSize(screen_rect); - CutTop(&screen_rect, size.y * 0.05f); - CutLeft(&screen_rect, size.x * 0.2f); - CutRight(&screen_rect, size.x * 0.2f); - Rect2 r = CutTop(&screen_rect, FontLineSpacing * 30.f); - - Windows[i].z = 1; - Windows[i].total_rect = ToRect2I(r); - Windows[i].document_rect = Windows[i].total_rect; - } + WindowSize = {(float)window_x, (float)window_y}; + BeginFrameRender(); + LayoutWindows(); SDL_Event event; if (WaitForEvents) { @@ -280,6 +161,7 @@ int main() Scratch scratch; Array order = GetWindowZOrder(scratch); + For(IterateInReverse(&order)) { Window &window = Windows[it]; if (window.visible) { @@ -287,7 +169,6 @@ int main() DrawWindow(window); } } - // { // Scratch scratch; // uint64_t ms = SDL_GetTicks(); diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index 510ef2c..67ed431 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -4,6 +4,7 @@ - We can actually combine this with command window and lua, it's just going to be a buffer of - open "asd/asd/asd/asd" - Ctrl + F +- Rework window views to contain a history of past views - word completion - Colored strings diff --git a/src/text_editor/window.cpp b/src/text_editor/window.cpp index c9425fe..e06cb09 100644 --- a/src/text_editor/window.cpp +++ b/src/text_editor/window.cpp @@ -22,3 +22,130 @@ Array GetWindowZOrder(Allocator allocator) { For(Windows) if (it.z == 0) Add(&order, GetIndex(Windows, it)); return order; } + +void InitWindows() { + Allocator sys_allocator = GetSystemAllocator(); + { + Buffer *buffer = CreateBuffer(sys_allocator, "*scratch*"); + } + + { + Window *w = CreateWindow(); + Buffer *b = CreateBuffer(sys_allocator, "*load_text_a*"); + View *v = CreateView(b->id); + LoadTextA(b); + AddView(w, v->id); + } + + { + Window *w = CreateWindow(); + Buffer *b = CreateBuffer(sys_allocator, "*load_unicode*"); + View *v = CreateView(b->id); + LoadUnicode(b); + AddView(w, v->id); + } + { + Window *w = CreateWindow(); + Buffer *b = GetBuffer({0}); + View *v = CreateView(b->id); + AddView(w, v->id); + } + { + Window *w = CreateWindow(); + w->draw_scrollbar = false; + w->draw_line_numbers = false; + w->dont_save_in_active_window_history = true; + w->deactivate_on_escape = true; + Buffer *b = CreateBuffer(sys_allocator, "*infobar*"); + b->no_history = true; + View *v = CreateView(b->id); + AddView(w, v->id); + InfoBarWindowID = w->id; + } + + { + Window *w = CreateWindow(); + w->draw_scrollbar = false; + w->draw_line_numbers = false; + w->visible = false; + w->fuzzy_search = true; + w->execute_line = true; + w->invisible_when_inactive = true; + w->dont_save_in_active_window_history = true; + w->deactivate_on_escape = true; + Buffer *b = CreateBuffer(sys_allocator, "*commands*"); + View *v = CreateView(b->id); + AddView(w, v->id); + CommandWindowID = w->id; + + // Command_EvalLua(v, L"open \"./\""); + } + + { + Window *w = CreateWindow(); + w->draw_scrollbar = false; + w->draw_line_numbers = false; + w->visible = false; + w->dont_save_in_active_window_history = true; + w->invisible_when_inactive = true; + w->deactivate_on_escape = true; + Buffer *b = CreateBuffer(sys_allocator, "*search*"); + View *v = CreateView(b->id); + AddView(w, v->id); + SearchWindowID = w->id; + } + SetActiveWindow({1}); +} + +void LayoutWindows() { + Rect2I screen_rect = GetScreenRectI(); + Rect2I infobar_rect = CutBottom(&screen_rect, (Int)FontLineSpacing); + float line_numbers_size = GetStringSize(&MainFont, L"1234567891").x; + { + int i = 5; + if (Windows[i].visible) { + Rect2I rect = CutBottom(&screen_rect, FontLineSpacing); + Windows[i].total_rect = rect; + Windows[i].document_rect = Windows[i].total_rect; + } + } + { + int i = 0; + Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 0.33)); + Windows[i].document_rect = Windows[i].total_rect; + if (Windows[i].draw_scrollbar) Windows[i].scrollbar_rect = CutRight(&Windows[i].document_rect, 10); + if (Windows[i].draw_line_numbers) Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size); + } + { + int i = 1; + Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 0.5)); + Windows[i].document_rect = Windows[i].total_rect; + if (Windows[i].draw_scrollbar) Windows[i].scrollbar_rect = CutRight(&Windows[i].document_rect, 10); + if (Windows[i].draw_line_numbers) Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size); + } + { + int i = 2; + Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 1.0)); + Windows[i].document_rect = Windows[i].total_rect; + if (Windows[i].draw_scrollbar) Windows[i].scrollbar_rect = CutRight(&Windows[i].document_rect, 10); + if (Windows[i].draw_line_numbers) Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size); + } + { + int i = 3; + Windows[i].total_rect = infobar_rect; + Windows[i].document_rect = Windows[i].total_rect; + } + { + int i = 4; + Rect2 screen_rect = GetScreenRectF(); + Vec2 size = GetSize(screen_rect); + CutTop(&screen_rect, size.y * 0.05f); + CutLeft(&screen_rect, size.x * 0.2f); + CutRight(&screen_rect, size.x * 0.2f); + Rect2 r = CutTop(&screen_rect, FontLineSpacing * 30.f); + + Windows[i].z = 1; + Windows[i].total_rect = ToRect2I(r); + Windows[i].document_rect = Windows[i].total_rect; + } +} \ No newline at end of file diff --git a/src/text_editor/window_draw.cpp b/src/text_editor/window_draw.cpp index 8d606e4..ac7dd72 100644 --- a/src/text_editor/window_draw.cpp +++ b/src/text_editor/window_draw.cpp @@ -57,17 +57,12 @@ void DrawVisibleText(Window &window) { int codepoint = line_string[col_index]; Glyph *g = GetGlyph(&MainFont, codepoint); Vec2 p = ToVec2(pos); - p.y += MainFont.ascent; + p.y += MainFont.ascent - MainFont.descent; p.x += text_offset_x; Rect2 rect = Rect2FromSize(p + g->offset, g->size); - if (codepoint == '\n' || codepoint == '\r') { - // DrawCircle((int)pos.x + (int)text_offset_x + (int)FontCharSpacing / 2, (int)pos.y + (int)FontLineSpacing / 2, (float)FontSize / 10.f, tint); - } else if (codepoint == ' ') { - // DrawCircle((int)pos.x + (int)text_offset_x + (int)FontCharSpacing / 2, (int)pos.y + (int)FontLineSpacing / 2, (float)FontSize / 10.f, space_color); - } else if (codepoint != '\t') { + if (codepoint != '\n' && codepoint != '\r' && codepoint != ' ') { PushQuad2D(RenderArena, &Vertices, rect, g->atlas_bounding_box, tint); - // DrawTextCodepoint(MainFont, codepoint, {(float)pos.x + text_offset_x, (float)pos.y}, (float)FontSize, tint); } if (g->xadvance == 0) text_offset_x += g->size.x; @@ -101,7 +96,7 @@ void DrawWindow(Window &window) { DrawRect(ToRect2(window.total_rect), ColorBackground); bool is_active = IsActive(&window) || window.id.id == GetLastActiveWindow().id; - BeginScissor(window.document_rect); + SetScissor(ToRect2(window.document_rect)); BeginProfileScope(draw_caret_selection); Rect2I visible = GetVisibleCells(window); For(view.carets) { @@ -167,28 +162,12 @@ void DrawWindow(Window &window) { } } EndProfileScope(); - EndScissor(); - - // Draw scrollbar - if (window.draw_scrollbar) { - // Vec2 mouse = GetMousePosition(); - // bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, ToRectangle(window.scrollbar_rect)); - - DrawRect(ToRect2(window.scrollbar_rect), ColorScrollbarBackground); - Scroller scroller = ComputeScrollerRect(window); - Rect2 rect = Shrink(scroller.rect, 2); - Color color = ColorScrollbarScroller; - // if (!window.mouse_selecting && (window.mouse_selecting_scrollbar || mouse_in_scrollbar)) { - // if (is_active) color = ColorScrollbarScrollerSelected; - // } - DrawRect(rect, color); - } // Draw line numbers if (window.draw_line_numbers) { Rect2I r = window.line_numbers_rect; DrawRect(ToRect2(r), ColorBackground); - BeginScissor(r); + SetScissor(ToRect2(r)); Rect2I vlines = GetVisibleCells(window); for (Int line = vlines.min.y; line <= vlines.max.y; line += 1) { @@ -205,8 +184,22 @@ void DrawWindow(Window &window) { if (x > rectx) p.x = (float)r.min.x; DrawString(&MainFont, string, p, ColorTextLineNumbers); } + } - EndScissor(); + SetScissor(GetScreenRectF()); + // Draw scrollbar + if (window.draw_scrollbar) { + // Vec2 mouse = GetMousePosition(); + // bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, ToRectangle(window.scrollbar_rect)); + + DrawRect(ToRect2(window.scrollbar_rect), ColorScrollbarBackground); + Scroller scroller = ComputeScrollerRect(window); + Rect2 rect = Shrink(scroller.rect, 2); + Color color = ColorScrollbarScroller; + // if (!window.mouse_selecting && (window.mouse_selecting_scrollbar || mouse_in_scrollbar)) { + // if (is_active) color = ColorScrollbarScrollerSelected; + // } + DrawRect(rect, color); } if (!is_active) {