Fix switching windows

This commit is contained in:
Krzosa Karol
2025-05-12 09:25:02 +02:00
parent 872c46c43d
commit fd842a1f70
7 changed files with 70 additions and 105 deletions

View File

@@ -206,7 +206,7 @@ float SafeDivide(float a, float b) {
return a / b;
}
bool CheckCollisionPointRec(Vec2 point, Rect2 rec) {
bool AreOverlapping(Vec2 point, Rect2 rec) {
bool result = (point.x >= rec.min.x) && (point.x < rec.max.x) && (point.y >= rec.min.y) && (point.y < rec.max.y);
return result;
}
}

View File

@@ -150,7 +150,7 @@ Int SafeDivide(Int a, Int b) {
return a / b;
}
bool CheckCollisionPointRec(Vec2I point, Rect2I rec) {
bool AreOverlapping(Vec2I point, Rect2I rec) {
bool result = (point.x >= rec.min.x) && (point.x < rec.max.x) && (point.y >= rec.min.y) && (point.y < rec.max.y);
return result;
}

View File

@@ -83,7 +83,7 @@ void MouseLoadWord(Event event, String meta = "") {
Vec2I mouse = MouseVec2I();
BSet active = GetActiveSet();
bool mouse_in_document = CheckCollisionPointRec(mouse, active.window->document_rect);
bool mouse_in_document = AreOverlapping(mouse, active.window->document_rect);
if (mouse_in_document) {
Int p = ScreenSpaceToBufferPosErrorOutOfBounds(active.window, active.view, active.buffer, mouse);
if (p != -1) {
@@ -1267,4 +1267,41 @@ int Lua_GetBufferList(lua_State *L) {
}
/* We still have table left on top of the Lua stack. */
return 1;
}
}
Window *GetOverlappingWindow(Vec2I p, Window *default_window = NULL) {
for (Window *it = FirstWindow; it; it = it->next) {
if (AreOverlapping(p, it->total_rect)) {
return it;
}
}
return default_window;
}
Vec2I GetSideOfWindow(Window *window, int direction) {
Vec2I p = {};
Rect2I rect = window->total_rect;
float resizer_size = (float)FontCharSpacing*0.5f; // @check_codebase_when_changing
if (direction == DIR_LEFT) {
p.x = rect.min.x - (Int)(resizer_size + FontCharSpacing);
p.y = rect.min.y + (rect.max.y / 2);
} else if (direction == DIR_RIGHT) {
p.x = rect.max.x + (Int)(resizer_size + FontCharSpacing);
p.y = rect.min.y + (rect.max.y / 2);
} else if (direction == DIR_UP) {
p.x = rect.min.x + (rect.max.x / 2);
p.y = rect.min.y - (Int)(resizer_size + FontLineSpacing);
} else {
Assert(direction == DIR_DOWN);
p.x = rect.min.x + (rect.max.x / 2);
p.y = rect.max.y + (Int)(resizer_size + FontLineSpacing);
}
return p;
}
Window *SwitchWindow(int direction) {
Window *window = GetWindow(ActiveWindow);
Vec2I p = GetSideOfWindow(window, direction);
Window *result = GetOverlappingWindow(p, window);
return result;
}

View File

@@ -21,83 +21,6 @@ String16 FetchLoadWord(void) {
return string;
}
Window *GetRightWindow(WindowSplit *split, Window *window, bool *next_is_the_one) {
if (split->kind == WindowSplitKind_Window) {
if (*next_is_the_one) {
return split->window;
}
if (split->window == window) {
*next_is_the_one = true;
}
} else if (split->kind == WindowSplitKind_Vertical || split->kind == WindowSplitKind_Horizontal) {
Window *a = GetRightWindow(split->left, window, next_is_the_one);
if (a != window) return a;
Window *b = GetRightWindow(split->right, window, next_is_the_one);
if (b != window) return b;
} ElseInvalidCodepath();
return window;
}
Window *GetRightWindow(WindowID id) {
Window *window = GetWindow(id);
WindowSplit *split = window->split_ref;
Assert(split->kind == WindowSplitKind_Window);
bool next_is_the_one = false;
Window *result = GetRightWindow(&WindowSplits, window, &next_is_the_one);
return result;
}
Window *GetLeftWindow(WindowSplit *split, Window *window, bool *next_is_the_one) {
if (split->kind == WindowSplitKind_Window) {
if (*next_is_the_one) {
return split->window;
}
if (split->window == window) {
*next_is_the_one = true;
}
} else if (split->kind == WindowSplitKind_Vertical || split->kind == WindowSplitKind_Horizontal) {
Window *b = GetLeftWindow(split->right, window, next_is_the_one);
if (b != window) return b;
Window *a = GetLeftWindow(split->left, window, next_is_the_one);
if (a != window) return a;
} ElseInvalidCodepath();
return window;
}
Window *GetLeftWindow(WindowID id) {
Window *window = GetWindow(id);
WindowSplit *split = window->split_ref;
Assert(split->kind == WindowSplitKind_Window);
bool next_is_the_one = false;
Window *result = GetLeftWindow(&WindowSplits, window, &next_is_the_one);
return result;
}
Window *GetNWindow(WindowSplit *split, Window *null_window, int n, int *counter) {
if (split->kind == WindowSplitKind_Window) {
*counter += 1;
if (n == *counter) {
return split->window;
}
} else if (split->kind == WindowSplitKind_Vertical || split->kind == WindowSplitKind_Horizontal) {
Window *a = GetNWindow(split->left, null_window, n, counter);
if (a) return a;
Window *b = GetNWindow(split->right, null_window, n, counter);
if (b) return b;
} ElseInvalidCodepath();
return NULL;
}
Window *GetNWindow(int n) {
int counter = 0;
Window *result = GetNWindow(&WindowSplits, GetWindow(NullWindowID), n, &counter);
return result;
}
void UpdateScroll(Window *window, bool update_caret_scrolling) {
ProfileFunction();
BSet set = GetBSet(window);
@@ -152,7 +75,7 @@ void ResizerDetectMouse(Event event, WindowSplit *split) {
}
Vec2I mouse = MouseVec2I();
bool mouse_in_rect = CheckCollisionPointRec(mouse, split->resizer_rect);
bool mouse_in_rect = AreOverlapping(mouse, split->resizer_rect);
if (mouse_in_rect) {
ResizerHover = split;
if (Mouse(LEFT)) {
@@ -179,7 +102,7 @@ void OnCommand(Event event) {
For(order) {
if (!it->visible) continue;
bool mouse_in_window = CheckCollisionPointRec(mouse, it->total_rect);
bool mouse_in_window = AreOverlapping(mouse, it->total_rect);
if (mouse_in_window) {
View *view = GetView(it->active_view);
view->scroll.y -= (Int)(event.ywheel * 48);
@@ -263,7 +186,7 @@ void OnCommand(Event event) {
if (!it->visible) {
continue;
}
bool mouse_in_document = CheckCollisionPointRec(mouse, it->document_rect);
bool mouse_in_document = AreOverlapping(mouse, it->document_rect);
if (mouse_in_document) {
ActiveWindow = it->id;
break;
@@ -287,7 +210,7 @@ void OnCommand(Event event) {
} else if (Mouse(RIGHT)) {
Vec2I mouse = MouseVec2I();
BSet active = GetActiveSet();
bool mouse_in_document = CheckCollisionPointRec(mouse, active.window->document_rect);
bool mouse_in_document = AreOverlapping(mouse, active.window->document_rect);
if (mouse_in_document) {
Int p = ScreenSpaceToBufferPos(active.window, active.view, active.buffer, mouse);
Int saved_front = -1;
@@ -324,8 +247,8 @@ void OnCommand(Event event) {
Assert(DocumentSelected.id == -1);
BSet active = GetActiveSet();
bool mouse_in_document = CheckCollisionPointRec(mouse, active.window->document_rect);
bool mouse_in_line_numbers = CheckCollisionPointRec(mouse, active.window->line_numbers_rect);
bool mouse_in_document = AreOverlapping(mouse, active.window->document_rect);
bool mouse_in_line_numbers = AreOverlapping(mouse, active.window->line_numbers_rect);
if (mouse_in_document || mouse_in_line_numbers) {
CheckpointBeforeGoto(active.window);
DocumentSelected = active.window->id;
@@ -363,7 +286,7 @@ void OnCommand(Event event) {
// when mouse is not even moving
For(order) {
if (!it->visible) continue;
bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, it->scrollbar_rect);
bool mouse_in_scrollbar = AreOverlapping(mouse, it->scrollbar_rect);
if (mouse_in_scrollbar) {
ScrollbarSelected = it->id;
@@ -415,16 +338,24 @@ void OnCommand(Event event) {
}
}
if (CtrlPress(SDLK_1)) {
Window *window = GetNWindow(2);
if (window) ActiveWindow = window->id;
ActiveWindow = GetOverlappingWindow({0,0}, GetWindow(ActiveWindow))->id;
}
if (CtrlPress(SDLK_2)) {
Window *window = GetNWindow(3);
if (window) ActiveWindow = window->id;
Window *first = GetOverlappingWindow({0,0}, GetWindow(ActiveWindow));
Vec2I p = GetSideOfWindow(first, DIR_RIGHT);
ActiveWindow = GetOverlappingWindow(p, GetWindow(ActiveWindow))->id;
}
if (CtrlPress(SDLK_3)) {
Window *window = GetNWindow(4);
if (window) ActiveWindow = window->id;
Window *first = GetOverlappingWindow({0,0});
if (first) {
Window *second = GetOverlappingWindow(GetSideOfWindow(first, DIR_RIGHT));
if (second) {
Window *third = GetOverlappingWindow(GetSideOfWindow(second, DIR_RIGHT));
if (third) {
ActiveWindow = third->id;
}
}
}
}
BSet main = GetActiveMainSet();
@@ -483,6 +414,8 @@ void OnCommand(Event event) {
Command_Move(active.view, DIR_LEFT, CTRL_PRESSED);
} else if (ShiftPress(SDLK_LEFT)) {
Command_Move(active.view, DIR_LEFT, false, SHIFT_PRESSED);
} else if (AltPress(SDLK_LEFT)) {
ActiveWindow = SwitchWindow(DIR_LEFT)->id;
} else if (Press(SDLK_LEFT)) {
Command_Move(active.view, DIR_LEFT);
}
@@ -493,6 +426,8 @@ void OnCommand(Event event) {
Command_Move(active.view, DIR_RIGHT, CTRL_PRESSED);
} else if (ShiftPress(SDLK_RIGHT)) {
Command_Move(active.view, DIR_RIGHT, false, SHIFT_PRESSED);
} else if (AltPress(SDLK_RIGHT)) {
ActiveWindow = SwitchWindow(DIR_RIGHT)->id;
} else if (Press(SDLK_RIGHT)) {
Command_Move(active.view, DIR_RIGHT);
}
@@ -685,12 +620,6 @@ void OnCommand(Event event) {
ActiveWindow = GetWindow(active.window->title_bar_window)->id;
}
if (AltPress(SDLK_RIGHT)) {
ActiveWindow = GetRightWindow(ActiveWindow)->id;
}
if (AltPress(SDLK_LEFT)) {
ActiveWindow = GetLeftWindow(ActiveWindow)->id;
}
if (CtrlShiftPress(SDLK_L)) {

View File

@@ -189,8 +189,8 @@ void SetMouseCursor(Event event) {
For(order) {
if (!it->visible) continue;
bool mouse_in_total = CheckCollisionPointRec(mouse, it->total_rect);
bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, it->scrollbar_rect);
bool mouse_in_total = AreOverlapping(mouse, it->total_rect);
bool mouse_in_scrollbar = AreOverlapping(mouse, it->scrollbar_rect);
if (!IsDocumentSelectionValid() && (mouse_in_scrollbar || IsScrollbarSelectionValid())) {
SDL_MouseCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT);

View File

@@ -17,7 +17,6 @@
- Open buffer using id
- Set active window using id
- Fix jump scroll, the query ends up the last line on screen, kind of wacky
- Fix Ctrl+1 Ctrl+2 (choosing window)
- Use project file as working dir instead of scratch buffer path, separate project dir and project file
- Remedybg integration
- GetLine()

View File

@@ -194,7 +194,7 @@ void DrawWindow(Window *window, Event &event) {
if (Ctrl()) {
Caret caret = view->carets[0];
Vec2I mouse = MouseVec2I();
bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect);
bool mouse_in_document = AreOverlapping(mouse, window->document_rect);
if (mouse_in_document) {
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);