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_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();

View File

@@ -29,9 +29,19 @@ Array<WindowID> WindowSwitchHistory; // @todo: probably better as a circular bu
Array<GotoCrumb> 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;

View File

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

View File

@@ -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

View File

@@ -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);
}