Refactor cursor underlining

This commit is contained in:
Krzosa Karol
2024-08-06 06:38:21 +02:00
parent f7100ae8ee
commit f0df0b69f0
6 changed files with 45 additions and 82 deletions

View File

@@ -161,43 +161,6 @@ 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++] = EncloseLoadWord(buffer, p);
Assert(view->underline_count <= 2);
}
break;
}
}
} else if (event.alt) {
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++] = EncloseExecWord(buffer, p);
Assert(view->underline_count <= 2);
}
break;
}
}
}
// Handle wheel scrolling // Handle wheel scrolling
if (event.wheel.x || event.wheel.y) { if (event.wheel.x || event.wheel.y) {
Vec2I mouse = MouseVec2I(); Vec2I mouse = MouseVec2I();

View File

@@ -827,20 +827,6 @@ void WindowCommand(Event event, Window *window, View *view) {
ToggleConsole(); ToggleConsole();
} }
if (event.ctrl) {
Caret caret = view->carets[0];
if (GetSize(caret.range) == 0) {
view->underline_pos[view->underline_count++] = EncloseLoadWord(buffer, caret.range.min);
Assert(view->underline_count <= 2);
}
} else if (event.alt) {
Caret caret = view->carets[0];
if (GetSize(caret.range) == 0) {
view->underline_pos[view->underline_count++] = EncloseExecWord(buffer, caret.range.min);
Assert(view->underline_count <= 2);
}
}
if (Mouse(MIDDLE)) { if (Mouse(MIDDLE)) {
Vec2I mouse = MouseVec2I(); Vec2I mouse = MouseVec2I();
Int p = ScreenSpaceToBufferPos(window, view, buffer, mouse); Int p = ScreenSpaceToBufferPos(window, view, buffer, mouse);

View File

@@ -160,7 +160,6 @@ void Update(Event event) {
View *view = GetActiveView(window); View *view = GetActiveView(window);
view->main_caret_on_begin_frame = view->carets[0]; view->main_caret_on_begin_frame = view->carets[0];
view->update_scroll = true; view->update_scroll = true;
view->underline_count = 0;
} }
HandleEvent(event); HandleEvent(event);
@@ -210,6 +209,7 @@ Array<Event> GetEventsForFrame(Allocator allocator) {
Add(&result, event); Add(&result, event);
} }
Assert(result.len);
return result; return result;
} }
@@ -296,24 +296,21 @@ int main()
while (AppIsRunning) { while (AppIsRunning) {
FrameID += 1; FrameID += 1;
{ Scratch scratch;
Scratch scratch; Array<Event> frame_events = GetEventsForFrame(scratch);
Array<Event> frame_events = GetEventsForFrame(scratch); For(frame_events) {
For(frame_events) { if (it.kind == EVENT_QUIT) goto end_of_editor_loop;
if (it.kind == EVENT_QUIT) goto end_of_editor_loop; Update(it);
Update(it); }
}
WaitForEvents = true; WaitForEvents = true;
if (DocumentSelected || ScrollbarSelected) { if (DocumentSelected || ScrollbarSelected) {
WaitForEvents = false; WaitForEvents = false;
}
} }
// This shouldn't matter to the state of the program, only appearence for // This shouldn't matter to the state of the program, only appearence for
// the user // the user
{ {
Scratch scratch;
Window *window = GetActiveWindow(); Window *window = GetActiveWindow();
View *view = GetView(window->active_view); View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
@@ -322,15 +319,13 @@ int main()
SDL_SetWindowTitle(SDLWindow, string.data); SDL_SetWindowTitle(SDLWindow, string.data);
} }
// This is here to render changes in title bar size without a frame of delay LayoutWindows(); // This is here to render changes in title bar size without a frame of delay
LayoutWindows();
BeginFrameRender(); BeginFrameRender();
Scratch scratch;
Array<Int> order = GetWindowZOrder(scratch); Array<Int> order = GetWindowZOrder(scratch);
For(IterateInReverse(&order)) { For(IterateInReverse(&order)) {
Window *window = &Windows[it]; Window *window = &Windows[it];
if (!window->visible) continue; if (!window->visible) continue;
DrawWindow(window); DrawWindow(window, *GetLast(frame_events));
} }
EndFrameRender(ColorBackground); EndFrameRender(ColorBackground);
SDL_GL_SwapWindow(SDLWindow); SDL_GL_SwapWindow(SDLWindow);

View File

@@ -51,9 +51,6 @@ struct View {
Caret main_caret_on_begin_frame; Caret main_caret_on_begin_frame;
bool update_scroll; bool update_scroll;
int underline_count;
Range underline_pos[2];
struct { struct {
bool fuzzy_search : 1; bool fuzzy_search : 1;
}; };

View File

@@ -10,6 +10,7 @@ BUG: when redo and ctrl pressed a bugged enclosure rect is shown - we probably w
- search backwards - search backwards
- draw indentation levels like in sublime (those lines) - draw indentation levels like in sublime (those lines)
- load all files in a directory - load all files in a directory
- change size of command window because it's wacky
- word complete - word complete
- Search all buffers in 10X style, incrementally searched results popping up on every key press (maybe we need coroutine library in C so this is easier?) - Search all buffers in 10X style, incrementally searched results popping up on every key press (maybe we need coroutine library in C so this is easier?)

View File

@@ -89,7 +89,19 @@ void DrawCaret(Window *window, XY xy, float size, Color color) {
DrawRect(ToRect2(rect), color); DrawRect(ToRect2(rect), color);
} }
void DrawWindow(Window *window) { void DrawUnderline(Window *window, View *view, Buffer *buffer, Range range) {
XY xy_min = PosToXY(*buffer, range.min);
XY xy_max = PosToXY(*buffer, range.max);
Vec2I min = {xy_min.col * FontCharSpacing, (xy_min.line + 1) * FontLineSpacing - 1};
Vec2I max = {xy_max.col * FontCharSpacing, (xy_max.line + 1) * FontLineSpacing};
Rect2I rect = {min, max};
rect -= view->scroll;
rect += window->document_rect.min;
DrawRectOutline(rect, ColorText);
}
void DrawWindow(Window *window, Event &event) {
View *view = GetActiveView(window); View *view = GetActiveView(window);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
SetScissor(GetScreenRectF()); SetScissor(GetScreenRectF());
@@ -167,18 +179,27 @@ void DrawWindow(Window *window) {
} }
} }
for (int i = 0; i < view->underline_count; i += 1) { // Underline word under mouse cursor
Range range = view->underline_pos[i]; if (event.ctrl || event.alt) {
auto enclose_proc = event.ctrl ? EncloseLoadWord : EncloseExecWord;
XY xy_min = PosToXY(*buffer, range.min); Vec2I mouse = MouseVec2I();
XY xy_max = PosToXY(*buffer, range.max); 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) {
Range range = enclose_proc(buffer, p);
DrawUnderline(window, view, buffer, range);
}
}
Vec2I min = {xy_min.col * FontCharSpacing, (xy_min.line + 1) * FontLineSpacing - 1}; Caret caret = view->carets[0];
Vec2I max = {xy_max.col * FontCharSpacing, (xy_max.line + 1) * FontLineSpacing}; if (GetSize(caret.range) == 0) {
Rect2I rect = {min, max}; Range range = enclose_proc(buffer, caret.range.min);
rect -= view->scroll; DrawUnderline(window, view, buffer, range);
rect += window->document_rect.min; }
DrawRectOutline(rect, ColorText);
} }
if (view->fuzzy_search) { if (view->fuzzy_search) {