From b552f73ac90447112eada7bc7ab0a3660558b9bf Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Tue, 6 Aug 2024 06:44:45 +0200 Subject: [PATCH] Fix underline bug and draw selection underline --- src/text_editor/todo.txt | 1 - src/text_editor/window_draw.cpp | 17 +++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index 153d691..636eb4b 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -1,4 +1,3 @@ -BUG: when redo and ctrl pressed a bugged enclosure rect is shown - we probably want to pass the event to render and just do everything there! - we should be able to execute selection using mouse and keyboard (for now only main cursor) - we should be able to execute a buffer (even scratch diff --git a/src/text_editor/window_draw.cpp b/src/text_editor/window_draw.cpp index 40ed7b8..c7f7cb2 100644 --- a/src/text_editor/window_draw.cpp +++ b/src/text_editor/window_draw.cpp @@ -93,6 +93,10 @@ void DrawUnderline(Window *window, View *view, Buffer *buffer, Range range) { XY xy_min = PosToXY(*buffer, range.min); XY xy_max = PosToXY(*buffer, range.max); + // @todo: maybe consider underlining multiple lines but then we would need to consider + // optimizing and so on + if (xy_min.line != xy_max.line) return; + Vec2I min = {xy_min.col * FontCharSpacing, (xy_min.line + 1) * FontLineSpacing - 1}; Vec2I max = {xy_max.col * FontCharSpacing, (xy_max.line + 1) * FontLineSpacing}; Rect2I rect = {min, max}; @@ -183,6 +187,7 @@ void DrawWindow(Window *window, Event &event) { if (event.ctrl || event.alt) { auto enclose_proc = event.ctrl ? EncloseLoadWord : EncloseExecWord; + Caret caret = view->carets[0]; Vec2I mouse = MouseVec2I(); bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect); if (mouse_in_document) { @@ -191,14 +196,18 @@ void DrawWindow(Window *window, Event &event) { Int p = ScreenSpaceToBufferPosErrorOutOfBounds(window, view, buffer, mouse); if (p != -1) { Range range = enclose_proc(buffer, p); + if (InBounds(caret.range, p)) range = caret.range; DrawUnderline(window, view, buffer, range); } } - Caret caret = view->carets[0]; - if (GetSize(caret.range) == 0) { - Range range = enclose_proc(buffer, caret.range.min); - DrawUnderline(window, view, buffer, range); + if (is_active) { + if (GetSize(caret.range) == 0) { + Range range = enclose_proc(buffer, caret.range.min); + DrawUnderline(window, view, buffer, range); + } else { + DrawUnderline(window, view, buffer, caret.range); + } } }