diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 0050d6c..3306e40 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -1,18 +1,6 @@ bool AppIsRunning = true; bool WaitForEvents = true; -enum { - MOUSE_NONE, - - MOUSE_LEFT, - MOUSE_RIGHT, - MOUSE_MIDDLE, - - MOUSE_LEFT_UP, - MOUSE_RIGHT_UP, - MOUSE_MIDDLE_UP, -}; - enum EventKind { EVENT_NONE, EVENT_UPDATE, @@ -25,11 +13,14 @@ enum EventKind { EVENT_MOUSE_RIGHT_UP, EVENT_MOUSE_MIDDLE_UP, EVENT_MOUSE_WHEEL, + EVENT_MOUSE_MOVE, EVENT_KEY_PRESS, EVENT_TEXT_INPUT, }; +bool IsMouseEvent(EventKind kind) { return kind >= EVENT_MOUSE_LEFT && kind <= EVENT_MOUSE_MOVE; } + struct Event { EventKind kind; SDL_Keycode key; diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 8df3afe..9523f84 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -591,7 +591,13 @@ void WindowCommand(Event event, Window *window, View *view) { bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, window->scrollbar_rect); bool scrollbar_action = mouse_in_scrollbar || window->mouse_selecting_scrollbar; - bool document_action = mouse_in_document || window->mouse_selecting; + bool document_action = false; + { + bool a = mouse_in_document && IsMouseEvent(event.kind); + bool b = window->mouse_selecting && !mouse_in_document; + document_action = a || b; + } + if (!scrollbar_action && document_action) { Vec2I mworld = mouse - window->document_rect.min + view->scroll; Vec2I pos = mworld / Vec2I{FontCharSpacing, FontLineSpacing}; @@ -606,6 +612,8 @@ void WindowCommand(Event event, Window *window, View *view) { if (InBounds({c->range.min, c->range.max + 1}, p)) { c->range = EncloseWord(*buffer, p); view->selection_anchor = c->range; + } else { + view->selection_anchor = Rng(p); } } else if (mouse_in_document && Mouse(LEFT) && event.ctrl) { Insert(&view->carets, MakeCaret(p, p), 0); diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 4048c88..2668984 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -70,16 +70,19 @@ Event TranslateSDLEvent(SDL_Event *input_event) { case SDL_EVENT_QUIT: { event.kind = EVENT_QUIT; } break; + case SDL_EVENT_KEY_DOWN: { event.kind = EVENT_KEY_PRESS; SDL_KeyboardEvent &key = input_event->key; event.key = key.key; } break; + case SDL_EVENT_TEXT_INPUT: { event.kind = EVENT_TEXT_INPUT; SDL_TextInputEvent &b = input_event->text; event.text = b.text; } break; + case SDL_EVENT_MOUSE_BUTTON_DOWN: { SDL_MouseButtonEvent &b = input_event->button; event.xmouse = (int16_t)b.x; @@ -95,6 +98,7 @@ Event TranslateSDLEvent(SDL_Event *input_event) { event.kind = EVENT_MOUSE_MIDDLE; } } break; + case SDL_EVENT_MOUSE_BUTTON_UP: { SDL_MouseButtonEvent &b = input_event->button; event.xmouse = (int16_t)b.x; @@ -107,6 +111,7 @@ Event TranslateSDLEvent(SDL_Event *input_event) { event.kind = EVENT_MOUSE_MIDDLE_UP; } } break; + case SDL_EVENT_MOUSE_WHEEL: { event.kind = EVENT_MOUSE_WHEEL; SDL_MouseWheelEvent &b = input_event->wheel; @@ -114,6 +119,12 @@ Event TranslateSDLEvent(SDL_Event *input_event) { event.ymouse = (int16_t)b.mouse_y; event.wheel = {b.x, b.y}; } break; + + case SDL_EVENT_MOUSE_MOTION: { + event.kind = EVENT_MOUSE_MOVE; + SDL_MouseMotionEvent &b = input_event->motion; + } break; + default: { }; }