From e0505dc974d97fb8de90d58301adb160d027d905 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Tue, 2 Jul 2024 07:10:32 +0200 Subject: [PATCH] Implement double click --- src/text_editor/buffer.cpp | 7 ++++ src/text_editor/layout.cpp | 1 + src/text_editor/main.cpp | 39 +++++++++++++++++-- .../multi_cursor_text_editor_behaviors.txt | 10 +++++ 4 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 src/text_editor/multi_cursor_text_editor_behaviors.txt diff --git a/src/text_editor/buffer.cpp b/src/text_editor/buffer.cpp index 686d905..698e2b1 100644 --- a/src/text_editor/buffer.cpp +++ b/src/text_editor/buffer.cpp @@ -614,4 +614,11 @@ int64_t MoveUp(Buffer &buffer, int64_t pos) { LineAndColumn info = FindLineAndColumn(buffer, pos); int64_t new_pos = FindPos(buffer, info.line.number - 1, info.column); return new_pos; +} + +Range EncloseWord(Buffer &buffer, int64_t pos) { + Range result = {}; + result.min = Seek(buffer, pos, ITERATE_BACKWARD); + result.max = Seek(buffer, pos, ITERATE_FORWARD); + return result; } \ No newline at end of file diff --git a/src/text_editor/layout.cpp b/src/text_editor/layout.cpp index f7c6b14..0471cf4 100644 --- a/src/text_editor/layout.cpp +++ b/src/text_editor/layout.cpp @@ -31,6 +31,7 @@ struct Window { float font_spacing; Rect2 rect; + Range selection_anchor_point; bool mouse_selecting; Vec2 scroll; // window_world_to_window_units Cursor main_cursor_begin_frame; diff --git a/src/text_editor/main.cpp b/src/text_editor/main.cpp index 237d380..10ad415 100644 --- a/src/text_editor/main.cpp +++ b/src/text_editor/main.cpp @@ -6,6 +6,9 @@ // @todo: add clipboard history? // @todo: mouse double click // @todo: context menu +// @todo: scrollbars +// @todo: line numbers +// @todo: line wrapping // @todo: toy around with acme ideas #include "rect2.cpp" #include "buffer.cpp" @@ -41,6 +44,23 @@ void Dbg_Draw() { } } +bool IsDoubleClick() { + static double last_click_time; + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + double time = GetTime(); + double diff = time - last_click_time; + last_click_time = time; + // @todo: don't do this manually, use windows + if (diff >= 0.15 && diff <= 0.5) { + last_click_time = 0; + return true; + } + } else if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { + last_click_time = 0; + } + return false; +} + int main() { InitScratch(); InitWindow(800, 600, "Hello"); @@ -389,7 +409,6 @@ int main() { For(focused_window->cursors) AddEdit(&edits, it.range, string); ApplyEdits(focused_window, edits); AfterEdit(focused_window, edits); - // For(focused_window->cursors) it.range.min = it.range.max = MoveRight(focused_window->buffer, it.range.min); } EventRecording_SimulateGetCharPressed(focused_window); } @@ -442,8 +461,16 @@ int main() { if (!IsKeyDown(KEY_LEFT_CONTROL)) { window.cursors.clear(); } - window.cursors.add({col->pos, col->pos}); + window.cursors.add(MakeCursor(col->pos, col->pos)); + window.selection_anchor_point = window.cursors.last()->range; + if (IsDoubleClick()) { + Cursor *c = window.cursors.last(); + c->range = EncloseWord(window.buffer, col->pos); + window.selection_anchor_point = c->range; + } + MergeCursors(&window); + } else if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { window.mouse_selecting = true; } @@ -453,7 +480,13 @@ int main() { window.mouse_selecting = false; } Cursor *cursor = window.cursors.last(); - cursor[0] = ChangeFront(*cursor, col->pos); + if (window.selection_anchor_point.min > col->pos) { + cursor[0] = MakeCursor(col->pos, window.selection_anchor_point.max); + } else if (window.selection_anchor_point.max < col->pos) { + cursor[0] = MakeCursor(col->pos, window.selection_anchor_point.min); + } else { + cursor[0] = MakeCursor(window.selection_anchor_point.max, window.selection_anchor_point.min); + } } } } diff --git a/src/text_editor/multi_cursor_text_editor_behaviors.txt b/src/text_editor/multi_cursor_text_editor_behaviors.txt new file mode 100644 index 0000000..bd91046 --- /dev/null +++ b/src/text_editor/multi_cursor_text_editor_behaviors.txt @@ -0,0 +1,10 @@ +- select word on double click - use selected word as the unselectable anchor point +- ctrl + mouse click to add and control a new cursor +- ctrl + alt + mouse click to do a rectangle select with multiple cursors @todo +- mouse wheel to scroll +- right click to open context menu with cut copy paste @todo +- shift + alt + up arrow to create a new cursor on line above +- ctrl + alt + up arrow to duplicate line, put it above and put cursor on it +- alt + up arrow to move line above @todo +- arrow to move cursor vertically or horizontally +- shift + arrow to move and select