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
if (event.wheel.x || event.wheel.y) {
Vec2I mouse = MouseVec2I();

View File

@@ -827,20 +827,6 @@ void WindowCommand(Event event, Window *window, View *view) {
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)) {
Vec2I mouse = MouseVec2I();
Int p = ScreenSpaceToBufferPos(window, view, buffer, mouse);

View File

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

View File

@@ -51,9 +51,6 @@ struct View {
Caret main_caret_on_begin_frame;
bool update_scroll;
int underline_count;
Range underline_pos[2];
struct {
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
- draw indentation levels like in sublime (those lines)
- load all files in a directory
- change size of command window because it's wacky
- 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?)

View File

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