From 605f3aa68da22dd2e637ca1fc63bb49085ee9c2b Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 4 Aug 2024 14:08:46 +0200 Subject: [PATCH] Mouse rewrite --- src/text_editor/commands.cpp | 91 ++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 41 deletions(-) diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 4ede984..3e2b95b 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -158,6 +158,44 @@ bool GlobalCommand(Event event) { } } + // Underline word on mouse cursor + if (event.ctrl) { + Vec2I mouse = MouseVec2I(); + For(order) { + Window *window = &Windows[it]; + if (!window->visible) continue; + bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect); + if (mouse_in_document) { + View *view = GetView(window->active_view); + Buffer *buffer = GetBuffer(view->active_buffer); + Int p = ScreenSpaceToBufferPosErrorOutOfBounds(window, view, buffer, mouse); + if (p != -1) { + view->underline_pos[view->underline_count++] = p; + Assert(view->underline_count <= 2); + } + break; + } + } + } + + // Handle wheel scrolling + if (event.wheel.x || event.wheel.y) { + Vec2I mouse = MouseVec2I(); + + For(order) { + Window *window = &Windows[it]; + if (!window->visible) continue; + + bool mouse_in_window = CheckCollisionPointRec(mouse, window->total_rect); + if (mouse_in_window) { + View *view = GetView(window->active_view); + view->scroll.y -= (Int)(event.wheel.y * 48); + view->scroll.x += (Int)(event.wheel.x * 48); + break; + } + } + } + // Handle selected window scrollbar // @note: the order here assumes that we won't run this code on the // same event as the scroll was pressed @@ -175,9 +213,12 @@ bool GlobalCommand(Event event) { double v = p / size_y; v = v + (window->mouse_scroller_offset); view->scroll.y = (Int)(v * (double)s.line_count * (double)FontLineSpacing); + run_window_command = false; } - if (DocumentSelected && MouseUp()) { + if (DocumentSelected != GetActiveWindow()) { + DocumentSelected = NULL; + } else if (DocumentSelected && MouseUp()) { Assert(ScrollbarSelected == NULL); DocumentSelected = NULL; } else if (DocumentSelected) { @@ -207,31 +248,10 @@ bool GlobalCommand(Event event) { } } - // underline - if (event.ctrl) { - Vec2I mouse = MouseVec2I(); - For(order) { - Window *window = &Windows[it]; - if (!window->visible) continue; - bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect); - if (mouse_in_document) { - View *view = GetView(window->active_view); - Buffer *buffer = GetBuffer(view->active_buffer); - Int p = ScreenSpaceToBufferPosErrorOutOfBounds(window, view, buffer, mouse); - if (p != -1) { - view->underline_pos[view->underline_count++] = p; - Assert(view->underline_count <= 2); - } - break; - } - } - } - - if (event.ctrl && Mouse(RIGHT)) { - GoBackToLastCrumb(); - } - // @todo: maybe move some of this stuff to window command ??? + // for now let's leave it because we are relaying on global state + // - maybe just do the check if active window is matching the DocumentSelected window + // - if scrollbar selected then don't invoke window command if (event.ctrl && Mouse(LEFT)) { Vec2I mouse = MouseVec2I(); Window *window = GetActiveWindow(); @@ -290,6 +310,8 @@ bool GlobalCommand(Event event) { } // Figure out scrollbar click + // @todo: it generally works ok but it moves the scrollbar a bit on click + // when mouse is not even moving For(order) { Window *window = &Windows[it]; if (!window->visible) continue; @@ -308,27 +330,14 @@ bool GlobalCommand(Event event) { } else { window->mouse_scroller_offset = (s.rect.min.y - p) / size_y; } + run_window_command = false; break; } } } - // Handle wheel scrolling - if (event.wheel.x || event.wheel.y) { - Vec2I mouse = MouseVec2I(); - - For(order) { - Window *window = &Windows[it]; - if (!window->visible) continue; - - bool mouse_in_window = CheckCollisionPointRec(mouse, window->total_rect); - if (mouse_in_window) { - View *view = GetView(window->active_view); - view->scroll.y -= (Int)(event.wheel.y * 48); - view->scroll.x += (Int)(event.wheel.x * 48); - break; - } - } + if (event.ctrl && Mouse(RIGHT)) { + GoBackToLastCrumb(); } if (Ctrl(SDLK_GRAVE)) {