Move selected windows to ids instead of pointers

This commit is contained in:
Krzosa Karol
2024-08-08 06:18:45 +02:00
parent 74ab76344b
commit 8e73057cfb
5 changed files with 35 additions and 31 deletions

View File

@@ -144,11 +144,11 @@ bool GlobalCommand(Event event) {
bool mouse_in_total = CheckCollisionPointRec(mouse, window->total_rect); bool mouse_in_total = CheckCollisionPointRec(mouse, window->total_rect);
bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, window->scrollbar_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_MouseCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT);
SDL_SetCursor(SDL_MouseCursor); SDL_SetCursor(SDL_MouseCursor);
break; break;
} else if (mouse_in_total || DocumentSelected) { } else if (mouse_in_total || IsDocumentSelectionValid()) {
SDL_MouseCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_TEXT); SDL_MouseCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_TEXT);
SDL_SetCursor(SDL_MouseCursor); SDL_SetCursor(SDL_MouseCursor);
break; break;
@@ -182,15 +182,15 @@ bool GlobalCommand(Event event) {
// Handle selected window scrollbar // Handle selected window scrollbar
// @note: the order here assumes that we won't run this code on the // @note: the order here assumes that we won't run this code on the
// same event as the scroll was pressed // same event as the scroll was pressed
if (ScrollbarSelected && Mouse(LEFT_UP)) { if (IsScrollbarSelectionValid() && Mouse(LEFT_UP)) {
Assert(DocumentSelected == NULL); Assert(DocumentSelected.id == -1);
ScrollbarSelected = NULL; ScrollbarSelected.id = -1;
} else if (ScrollbarSelected) { } else if (IsScrollbarSelectionValid()) {
// :ScrollbarImprovement // :ScrollbarImprovement
// @todo: it generally works ok but it moves the scrollbar a bit on click // @todo: it generally works ok but it moves the scrollbar a bit on click
// when mouse is not even moving // when mouse is not even moving
Assert(DocumentSelected == NULL); Assert(DocumentSelected.id == -1);
Window *window = ScrollbarSelected; Window *window = GetWindow(ScrollbarSelected);
View *view = GetView(window->active_view); View *view = GetView(window->active_view);
Vec2 mouse_vec2 = MouseVec2(); Vec2 mouse_vec2 = MouseVec2();
Scroller s = ComputeScrollerRect(window); Scroller s = ComputeScrollerRect(window);
@@ -202,14 +202,14 @@ bool GlobalCommand(Event event) {
run_window_command = false; run_window_command = false;
} }
if (DocumentSelected != GetActiveWindow()) { if (DocumentSelected.id != ActiveWindow.id) {
DocumentSelected = NULL; DocumentSelected.id = -1;
} else if (DocumentSelected && MouseUp()) { } else if (IsDocumentSelectionValid() && MouseUp()) {
Assert(ScrollbarSelected == NULL); Assert(ScrollbarSelected.id == -1);
DocumentSelected = NULL; DocumentSelected.id = -1;
} else if (DocumentSelected) { } else if (IsDocumentSelectionValid()) {
Assert(ScrollbarSelected == NULL); Assert(ScrollbarSelected.id == -1);
Window *window = DocumentSelected; Window *window = GetWindow(DocumentSelected);
View *view = GetView(window->active_view); View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
@@ -293,8 +293,8 @@ bool GlobalCommand(Event event) {
} else if (Mouse(LEFT)) { } else if (Mouse(LEFT)) {
Vec2I mouse = MouseVec2I(); Vec2I mouse = MouseVec2I();
{ {
Assert(ScrollbarSelected == NULL); Assert(ScrollbarSelected.id == -1);
Assert(DocumentSelected == NULL); Assert(DocumentSelected.id == -1);
Window *window = GetActiveWindow(); Window *window = GetActiveWindow();
bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect); 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) { if (mouse_in_document || mouse_in_line_numbers) {
View *view = GetView(window->active_view); View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
DocumentSelected = window; DocumentSelected = window->id;
Int p = ScreenSpaceToBufferPos(window, view, buffer, mouse); Int p = ScreenSpaceToBufferPos(window, view, buffer, mouse);
Caret &caret = view->carets[0]; Caret &caret = view->carets[0];
@@ -362,7 +362,7 @@ bool GlobalCommand(Event event) {
if (!window->visible) continue; if (!window->visible) continue;
bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, window->scrollbar_rect); bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, window->scrollbar_rect);
if (mouse_in_scrollbar) { if (mouse_in_scrollbar) {
ScrollbarSelected = window; ScrollbarSelected = window->id;
View *view = GetView(window->active_view); View *view = GetView(window->active_view);
Vec2 mouse_vec2 = MouseVec2(); Vec2 mouse_vec2 = MouseVec2();

View File

@@ -29,9 +29,19 @@ Array<WindowID> WindowSwitchHistory; // @todo: probably better as a circular bu
Array<GotoCrumb> GotoCrumbs; Array<GotoCrumb> GotoCrumbs;
Int CaretChangeID; Int CaretChangeID;
Window *ScrollbarSelected = NULL; WindowID ScrollbarSelected = {-1};
Window *DocumentSelected = NULL; WindowID DocumentSelected = {-1};
Range DocumentRangeAnchor; 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 ViewID AllocViewID(View *view) { return {ViewIDs.id++, view}; }
inline WindowID AllocWindowID(Window *window) { return {WindowIDs.id++, window}; } inline WindowID AllocWindowID(Window *window) { return {WindowIDs.id++, window}; }
@@ -92,11 +102,6 @@ Buffer *CreateTempBuffer(Allocator allocator, Int size = 4096) {
} }
Window *CreateWindow() { Window *CreateWindow() {
if (Windows.data == NULL) {
Reserve(&Windows, 256);
}
Assert(Windows.len + 1 <= Windows.cap);
Window *w = Alloc(&Windows); Window *w = Alloc(&Windows);
w->visible = true; w->visible = true;
w->draw_scrollbar = StyleDrawScrollbar; w->draw_scrollbar = StyleDrawScrollbar;

View File

@@ -315,7 +315,7 @@ int main()
} }
WaitForEvents = true; WaitForEvents = true;
if (DocumentSelected || ScrollbarSelected || ActiveProcesses.len) { if (IsDocumentSelectionValid() || IsScrollbarSelectionValid()) {
WaitForEvents = false; WaitForEvents = false;
} }

View File

@@ -1,6 +1,5 @@
- delete raylib utils, delete WaitForExit? - delete raylib utils, delete WaitForExit?
- remove pointers from ids since they will quickly get invalidated, move fully to array stuff - 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 - Attach BufferID to process, append to that buffer
- kill all processes on exit https://devblogs.microsoft.com/oldnewthing/20131209-00/?p=2433 - 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 - AppendToConsole should scroll only if caret is at end and one caret otherwise the carets should not change

View File

@@ -270,7 +270,7 @@ void DrawWindow(Window *window, Event &event) {
Scroller scroller = ComputeScrollerRect(window); Scroller scroller = ComputeScrollerRect(window);
Rect2 rect = Shrink(scroller.rect, 2); Rect2 rect = Shrink(scroller.rect, 2);
Color color = ColorScrollbarScroller; Color color = ColorScrollbarScroller;
if (ScrollbarSelected == window) color = ColorScrollbarScrollerSelected; if (ScrollbarSelected.id == window->id.id) color = ColorScrollbarScrollerSelected;
DrawRect(rect, color); DrawRect(rect, color);
} }