Mouse rewrite

This commit is contained in:
Krzosa Karol
2024-08-04 14:08:46 +02:00
parent c6a415f642
commit 605f3aa68d

View File

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