From 8e73057cfb935807803a0dbc009e75861da535f7 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Thu, 8 Aug 2024 06:18:45 +0200 Subject: [PATCH] Move selected windows to ids instead of pointers --- src/text_editor/commands.cpp | 40 ++++++++++++++++----------------- src/text_editor/management.cpp | 21 ++++++++++------- src/text_editor/text_editor.cpp | 2 +- src/text_editor/todo.txt | 1 - src/text_editor/window_draw.cpp | 2 +- 5 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 833c08d..820df25 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -144,11 +144,11 @@ bool GlobalCommand(Event event) { bool mouse_in_total = CheckCollisionPointRec(mouse, window->total_rect); bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, window->scrollbar_rect); - if (!DocumentSelected && (mouse_in_scrollbar || ScrollbarSelected)) { + if (!IsDocumentSelectionValid() && (mouse_in_scrollbar || IsScrollbarSelectionValid())) { SDL_MouseCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT); SDL_SetCursor(SDL_MouseCursor); break; - } else if (mouse_in_total || DocumentSelected) { + } else if (mouse_in_total || IsDocumentSelectionValid()) { SDL_MouseCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_TEXT); SDL_SetCursor(SDL_MouseCursor); break; @@ -182,15 +182,15 @@ bool GlobalCommand(Event event) { // 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 - if (ScrollbarSelected && Mouse(LEFT_UP)) { - Assert(DocumentSelected == NULL); - ScrollbarSelected = NULL; - } else if (ScrollbarSelected) { + if (IsScrollbarSelectionValid() && Mouse(LEFT_UP)) { + Assert(DocumentSelected.id == -1); + ScrollbarSelected.id = -1; + } else if (IsScrollbarSelectionValid()) { // :ScrollbarImprovement // @todo: it generally works ok but it moves the scrollbar a bit on click // when mouse is not even moving - Assert(DocumentSelected == NULL); - Window *window = ScrollbarSelected; + Assert(DocumentSelected.id == -1); + Window *window = GetWindow(ScrollbarSelected); View *view = GetView(window->active_view); Vec2 mouse_vec2 = MouseVec2(); Scroller s = ComputeScrollerRect(window); @@ -202,14 +202,14 @@ bool GlobalCommand(Event event) { run_window_command = false; } - if (DocumentSelected != GetActiveWindow()) { - DocumentSelected = NULL; - } else if (DocumentSelected && MouseUp()) { - Assert(ScrollbarSelected == NULL); - DocumentSelected = NULL; - } else if (DocumentSelected) { - Assert(ScrollbarSelected == NULL); - Window *window = DocumentSelected; + if (DocumentSelected.id != ActiveWindow.id) { + DocumentSelected.id = -1; + } else if (IsDocumentSelectionValid() && MouseUp()) { + Assert(ScrollbarSelected.id == -1); + DocumentSelected.id = -1; + } else if (IsDocumentSelectionValid()) { + Assert(ScrollbarSelected.id == -1); + Window *window = GetWindow(DocumentSelected); View *view = GetView(window->active_view); Buffer *buffer = GetBuffer(view->active_buffer); @@ -293,8 +293,8 @@ bool GlobalCommand(Event event) { } else if (Mouse(LEFT)) { Vec2I mouse = MouseVec2I(); { - Assert(ScrollbarSelected == NULL); - Assert(DocumentSelected == NULL); + Assert(ScrollbarSelected.id == -1); + Assert(DocumentSelected.id == -1); Window *window = GetActiveWindow(); bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect); @@ -302,7 +302,7 @@ bool GlobalCommand(Event event) { if (mouse_in_document || mouse_in_line_numbers) { View *view = GetView(window->active_view); Buffer *buffer = GetBuffer(view->active_buffer); - DocumentSelected = window; + DocumentSelected = window->id; Int p = ScreenSpaceToBufferPos(window, view, buffer, mouse); Caret &caret = view->carets[0]; @@ -362,7 +362,7 @@ bool GlobalCommand(Event event) { if (!window->visible) continue; bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, window->scrollbar_rect); if (mouse_in_scrollbar) { - ScrollbarSelected = window; + ScrollbarSelected = window->id; View *view = GetView(window->active_view); Vec2 mouse_vec2 = MouseVec2(); diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index c5e3d32..adee5ce 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -29,9 +29,19 @@ Array WindowSwitchHistory; // @todo: probably better as a circular bu Array GotoCrumbs; Int CaretChangeID; -Window *ScrollbarSelected = NULL; -Window *DocumentSelected = NULL; -Range DocumentRangeAnchor; +WindowID ScrollbarSelected = {-1}; +WindowID DocumentSelected = {-1}; +Range DocumentRangeAnchor; + +inline bool IsDocumentSelectionValid() { + if (DocumentSelected.id == -1) return false; + return true; +} + +inline bool IsScrollbarSelectionValid() { + if (ScrollbarSelected.id == -1) return false; + return true; +} inline ViewID AllocViewID(View *view) { return {ViewIDs.id++, view}; } inline WindowID AllocWindowID(Window *window) { return {WindowIDs.id++, window}; } @@ -92,11 +102,6 @@ Buffer *CreateTempBuffer(Allocator allocator, Int size = 4096) { } Window *CreateWindow() { - if (Windows.data == NULL) { - Reserve(&Windows, 256); - } - Assert(Windows.len + 1 <= Windows.cap); - Window *w = Alloc(&Windows); w->visible = true; w->draw_scrollbar = StyleDrawScrollbar; diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 3d81042..641ce9a 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -315,7 +315,7 @@ int main() } WaitForEvents = true; - if (DocumentSelected || ScrollbarSelected || ActiveProcesses.len) { + if (IsDocumentSelectionValid() || IsScrollbarSelectionValid()) { WaitForEvents = false; } diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index 4e7d7c2..3579050 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -1,6 +1,5 @@ - delete raylib utils, delete WaitForExit? - remove pointers from ids since they will quickly get invalidated, move fully to array stuff -- try waiting for events despite of running processes, I remember there was a problem with this in 4Coder, your PC would just start going ham - Attach BufferID to process, append to that buffer - kill all processes on exit https://devblogs.microsoft.com/oldnewthing/20131209-00/?p=2433 - AppendToConsole should scroll only if caret is at end and one caret otherwise the carets should not change diff --git a/src/text_editor/window_draw.cpp b/src/text_editor/window_draw.cpp index c7f7cb2..a6c8ecf 100644 --- a/src/text_editor/window_draw.cpp +++ b/src/text_editor/window_draw.cpp @@ -270,7 +270,7 @@ void DrawWindow(Window *window, Event &event) { Scroller scroller = ComputeScrollerRect(window); Rect2 rect = Shrink(scroller.rect, 2); Color color = ColorScrollbarScroller; - if (ScrollbarSelected == window) color = ColorScrollbarScrollerSelected; + if (ScrollbarSelected.id == window->id.id) color = ColorScrollbarScrollerSelected; DrawRect(rect, color); }