From a43fe1789b9fb3837db6906eb1f05e3fe5a2487f Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sat, 27 Jul 2024 09:43:46 +0200 Subject: [PATCH] Ported mouse movement and conditional update --- src/text_editor/commands.cpp | 1 - src/text_editor/commands_window.cpp | 279 +--------------------------- src/text_editor/new_text_editor.cpp | 256 ++++++++++++++----------- 3 files changed, 152 insertions(+), 384 deletions(-) diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 6798bce..2c35c16 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -1,4 +1,3 @@ - Int MoveOnWhitespaceBoundaryForward(Buffer &buffer, Int pos) { pos = Clamp(pos, (Int)0, buffer.len); bool standing_on_whitespace = IsWhitespace(buffer.str[pos]); diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 8cf7f61..f196b0e 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -224,275 +224,6 @@ void MergeCarets(View *view, Range *mouse_selection_anchor) { Swap(&view->carets[first_caret_index], &view->carets[0]); } -void HandleActiveWindowBindings(Window *window, bool *update_scroll) { - View &view = *GetActiveView(window); - Buffer *buffer = GetBuffer(view.active_buffer); - // if (CtrlPress(KEY_F2)) { - // LoadBigLine(buffer); - // } else if (Press(KEY_F2)) { - // LoadBigText(buffer); - // } - - // if (Press(KEY_ESCAPE)) { - // if (window->deactivate_on_escape) { - // SetActiveWindow(GetLastActiveWindow()); - // } else { - // view.carets.len = 1; - // } - // } - - // if (CtrlAltPress(KEY_DOWN)) { - // Command_DuplicateLine(&view, DIR_DOWN); - // } else if (AltShiftPress(KEY_DOWN)) { - // Command_CreateCursorVertical(&view, DIR_DOWN); - // } else if (CtrlShiftPress(KEY_DOWN)) { - // For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_DOWN, true)); - // } else if (CtrlPress(KEY_DOWN)) { - // For(view.carets) it = MakeCaret(MovePos(*buffer, it.range.max, DIR_DOWN, true)); - // } else if (ShiftPress(KEY_DOWN)) { - // For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_DOWN, false)); - // } else if (Press(KEY_DOWN)) { - // For(view.carets) { - // if (GetSize(it.range) == 0) { - // it = MakeCaret(MovePos(*buffer, it.range.max, DIR_DOWN, false)); - // } else { - // it = MakeCaret(it.range.max); - // } - // } - // } - - // if (CtrlAltPress(KEY_UP)) { - // Command_DuplicateLine(&view, DIR_UP); - // } else if (AltShiftPress(KEY_UP)) { - // Command_CreateCursorVertical(&view, DIR_UP); - // } else if (CtrlShiftPress(KEY_UP)) { - // For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_UP, CTRL_PRESSED)); - // } else if (CtrlPress(KEY_UP)) { - // For(view.carets) it = MakeCaret(MovePos(*buffer, it.range.min, DIR_UP, CTRL_PRESSED)); - // } else if (ShiftPress(KEY_UP)) { - // For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_UP)); - // } else if (Press(KEY_UP)) { - // For(view.carets) { - // if (GetSize(it.range) == 0) { - // it = MakeCaret(MovePos(*buffer, it.range.min, DIR_UP)); - // } else { - // it = MakeCaret(it.range.min); - // } - // } - // } - - // if (CtrlShiftPress(KEY_LEFT)) { - // For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_LEFT, true)); - // } else if (CtrlPress(KEY_LEFT)) { - // For(view.carets) { - // if (GetSize(it.range) != 0 && GetFront(it) != it.range.min) { - // it = MakeCaret(it.range.min); - // } else { - // it = MakeCaret(MovePos(*buffer, it.range.min, DIR_LEFT, true)); - // } - // } - // } else if (ShiftPress(KEY_LEFT)) { - // For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_LEFT, false)); - // } else if (Press(KEY_LEFT)) { - // For(view.carets) { - // if (GetSize(it.range) == 0) { - // it = MakeCaret(MovePos(*buffer, it.range.min, DIR_LEFT, false)); - // } else { - // it = MakeCaret(it.range.min); - // } - // } - // } - - // if (CtrlShiftPress(KEY_RIGHT)) { - // For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_RIGHT, true)); - // } else if (CtrlPress(KEY_RIGHT)) { - // For(view.carets) { - // if (GetSize(it.range) != 0 && GetFront(it) != it.range.max) { - // it = MakeCaret(it.range.max); - // } else { - // it = MakeCaret(MovePos(*buffer, it.range.max, DIR_RIGHT, true)); - // } - // } - // } else if (ShiftPress(KEY_RIGHT)) { - // For(view.carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_RIGHT, false)); - // } else if (Press(KEY_RIGHT)) { - // For(view.carets) { - // if (GetSize(it.range) == 0) { - // it = MakeCaret(MovePos(*buffer, it.range.max, DIR_RIGHT, false)); - // } else { - // it = MakeCaret(it.range.max); - // } - // } - // } - - // if (CtrlShiftPress(KEY_Z)) { - // RedoEdit(buffer, &view.carets); - // } else if (CtrlPress(KEY_Z)) { - // UndoEdit(buffer, &view.carets); - // } - - // if (CtrlPress(KEY_C)) { - // Command_Copy(&view); - // } else if (CtrlPress(KEY_V)) { - // Command_Paste(&view); - // } else if (CtrlPress(KEY_X)) { - // Command_Copy(&view); - // Command_Replace(&view, L""); - // } - - // if (CtrlPress(KEY_A)) { - // Command_SelectEntireBuffer(&view); - // *update_scroll = false; - // } - - // if (ShiftPress(KEY_PAGE_UP)) { - // Command_MoveCursorsByPageSize(window, DIR_UP, SHIFT_PRESSED); - // } else if (Press(KEY_PAGE_UP)) { - // Command_MoveCursorsByPageSize(window, DIR_UP); - // } - - // if (ShiftPress(KEY_PAGE_DOWN)) { - // Command_MoveCursorsByPageSize(window, DIR_DOWN, SHIFT_PRESSED); - // } else if (Press(KEY_PAGE_DOWN)) { - // Command_MoveCursorsByPageSize(window, DIR_DOWN); - // } - - // if (ShiftPress(KEY_HOME)) { - // Command_MoveCursorsToSide(window, DIR_LEFT, SHIFT_PRESSED); - // } else if (Press(KEY_HOME)) { - // Command_MoveCursorsToSide(window, DIR_LEFT); - // } - - // if (ShiftPress(KEY_END)) { - // Command_MoveCursorsToSide(window, DIR_RIGHT, SHIFT_PRESSED); - // } else if (Press(KEY_END)) { - // Command_MoveCursorsToSide(window, DIR_RIGHT); - // } - - // bool search = false; - // if (Press(KEY_TAB)) { - // Command_Replace(&view, L" "); - // search = true; - // } - - // if (CtrlPress(KEY_BACKSPACE)) { - // Command_Delete(&view, DIR_LEFT, CTRL_PRESSED); - // search = true; - // } else if (Press(KEY_BACKSPACE)) { - // Command_Delete(&view, DIR_LEFT); - // search = true; - // } - - // if (CtrlPress(KEY_DELETE)) { - // Command_Delete(&view, DIR_RIGHT, CTRL_PRESSED); - // search = true; - // } else if (Press(KEY_DELETE)) { - // Command_Delete(&view, DIR_RIGHT); - // search = true; - // } - - // for (int c = GetCharPressed(); c; c = GetCharPressed()) { - // // we interpret 2 byte sequences as 1 byte when rendering but we still - // // want to read them properly. - // String16 string = L"?"; - // UTF16Result result = UTF32ToUTF16((uint32_t)c); - // if (!result.error) string = {(wchar_t *)result.out_str, result.len}; - // Command_Replace(&view, string); - // search = true; - // } - - // if (CtrlPress(KEY_D)) { - // String16 string = GetString(*buffer, view.carets[0].range); - // Caret caret = FindInBuffer(buffer, string, view.carets[0], true); - // Insert(&view.carets, caret, 0); - // MergeCarets(&view); - // } - - // if (Press(KEY_F3)) { - // Buffer *search_buffer = GetBuffer("*search*"); - // String16 search_string = GetString(*search_buffer); - // Caret caret = FindInBuffer(buffer, search_string, view.carets[0], true); - // if (Ctrl()) { - // Insert(&view.carets, caret, 0); - // MergeCarets(&view); - // } else { - // view.carets.len = 1; - // view.carets[0] = caret; - // } - // } - - // if (window->id.id == SearchWindowID.id) { - // Window *seek_window = GetWindow(GetLastActiveWindow()); - // View *seek_view = GetView(seek_window->active_view); - // Buffer *seek_buffer = GetBuffer(seek_view->active_buffer); - // String16 needle = GetString(*buffer); - // if (search) { - // seek_view->carets[0] = FindInBuffer(seek_buffer, needle, seek_view->carets[0]); - // seek_view->carets.len = 1; - // } - // if (AltPress(KEY_ENTER)) { - // Command_SelectAll(seek_view, needle); - // // SetActiveWindow(seek_window->id); - // } else if (Press(KEY_ENTER)) { - // Caret caret = FindInBuffer(seek_buffer, needle, seek_view->carets[0], true); - // if (Ctrl()) { - // Insert(&seek_view->carets, caret, 0); - // MergeCarets(seek_view); - // } else { - // seek_view->carets.len = 1; - // seek_view->carets[0] = caret; - // } - // } - // } - - // if (window->fuzzy_search && search) { - // Scratch scratch; - // String16 first_line_string = GetLineStringWithoutNL(*buffer, 0); - // Array ratings = FuzzySearchLines(scratch, buffer, 1, buffer->line_starts.len, first_line_string); - - // Buffer *temp_buffer = CreateTempBuffer(scratch, buffer->cap); - // ReplaceText(temp_buffer, GetEndAsRange(*temp_buffer), first_line_string); - // ReplaceText(temp_buffer, GetEndAsRange(*temp_buffer), L"\n"); - // For(ratings) { - // String16 s = GetLineStringWithoutNL(*buffer, it.index); - // if (s.len == 0) continue; - // ReplaceText(temp_buffer, GetEndAsRange(*temp_buffer), s); - // ReplaceText(temp_buffer, GetEndAsRange(*temp_buffer), L"\n"); - // } - - // Caret caret = view.carets[0]; - // Command_SelectEntireBuffer(&view); - // Command_Replace(&view, GetString(*temp_buffer)); - // view.carets[0] = caret; - // } - - // if (CtrlPress(KEY_Q) || IsMouseButtonPressed(MOUSE_BUTTON_MIDDLE)) { - // // @todo: Consider applying this to all cursors - // if (GetSize(view.carets[0].range) == 0) { - // Command_EvalLuaLine(&view); - // } else { - // String16 string = GetString(*buffer, view.carets[0].range); - // Command_EvalLua(&view, string); - // } - // } - - // if (window->execute_line) { - // if (Press(KEY_ENTER)) { - // Command_EvalLuaLine(&view); - // } - // } else if (window->id.id == SearchWindowID.id) { - // } else { - // if (CtrlPress(KEY_ENTER)) { - // Command_MoveCursorsToSide(window, DIR_RIGHT); - // Command_Replace(&view, L"\n"); - // } else if (Press(KEY_ENTER)) { - // Command_Replace(&view, L"\n"); - // } - // } - - MergeCarets(&view); -} - void ReplaceInfobarData() { Window *window = GetWindow(InfoBarWindowID); if (IsActive(window)) return; @@ -514,7 +245,15 @@ void ReplaceInfobarData() { ReplaceText(buffer, GetEndAsRange(*buffer), L"|"); } - String s = Format(scratch, "line: %5lld col: %5lld name: %.*s wid: %d vid: %d bid: %d", (long long)xy.line + 1ll, (long long)xy.col + 1ll, FmtString(name), (int)last_window->id.id, (int)last_view->id.id, (int)last_buffer->id.id); + // { + // Scratch scratch; + // uint64_t ms = SDL_GetTicks(); + // double time = (double)ms / 1000.0; + // String string = Format(scratch, "%d %f %f", (int)FrameID, time, (time / (double)FrameID)); + // DrawString(&MainFont, ToString16(scratch, string), {0, 0}, {255, 0, 0, 255}); + // } + + String s = Format(scratch, "line: %5lld col: %5lld name: %.*s wid: %d vid: %d bid: %d frame%lld", (long long)xy.line + 1ll, (long long)xy.col + 1ll, FmtString(name), (int)last_window->id.id, (int)last_view->id.id, (int)last_buffer->id.id, (long long)FrameID); String16 string = ToString16(scratch, s); ReplaceText(buffer, replace_range, string); Command_SelectRangeOneCursor(view, {}); diff --git a/src/text_editor/new_text_editor.cpp b/src/text_editor/new_text_editor.cpp index f60488c..6192ea9 100644 --- a/src/text_editor/new_text_editor.cpp +++ b/src/text_editor/new_text_editor.cpp @@ -40,6 +40,19 @@ bool AppIsRunning = true; bool WaitForEvents = true; SDL_Cursor *SDL_MouseCursor = NULL; +enum { + MOUSE_NONE, + + MOUSE_MOVE, + MOUSE_LEFT, + MOUSE_RIGHT, + MOUSE_MIDDLE, + + MOUSE_LEFT_UP, + MOUSE_RIGHT_UP, + MOUSE_MIDDLE_UP, +}; + struct Event { SDL_Keycode key; int16_t xmouse; @@ -49,13 +62,9 @@ struct Event { uint8_t ctrl : 1; uint8_t alt : 1; uint8_t super : 1; - - uint8_t mouse_move : 1; - uint8_t mouse_left : 1; - uint8_t mouse_right : 1; - uint8_t mouse_middle : 1; - uint8_t mouse_double_click : 1; }; + uint8_t mouse; + uint8_t mouse_double_click; float wheel; const char *text; }; @@ -67,15 +76,16 @@ struct Event { #define CtrlShift(KEY) (event.key == KEY && event.ctrl && event.shift) #define CtrlAlt(KEY) (event.key == KEY && event.ctrl && event.alt) #define AltShift(KEY) (event.key == KEY && event.shift && event.alt) -#define MousePress() (event.mouse_left == 1 || event.mouse_right == 1 || event.mouse_middle == 1 || event.mouse_double_click == 1) #define MouseVec2() \ Vec2 { (float)event.xmouse, (float)event.ymouse } #define MouseVec2I() \ Vec2I { (Int) event.xmouse, (Int)event.ymouse } +#define Mouse(x) (event.mouse == MOUSE_##x) +#define MousePress() (Mouse(LEFT) || Mouse(RIGHT) || Mouse(MIDDLE)) bool GlobalCommand(Event event) { bool run_window_command = true; - if (event.mouse_move) { + if (Mouse(MOVE)) { Vec2I mouse = MouseVec2I(); Window *window = GetActiveWindow(); bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect); @@ -352,16 +362,6 @@ void WindowCommand(Event event, Window *window, View *view) { search = true; } - // for (int c = GetCharPressed(); c; c = GetCharPressed()) { - // // we interpret 2 byte sequences as 1 byte when rendering but we still - // // want to read them properly. - // String16 string = L"?"; - // UTF16Result result = UTF32ToUTF16((uint32_t)c); - // if (!result.error) string = {(wchar_t *)result.out_str, result.len}; - // Command_Replace(view, string); - // search = true; - // } - if (Ctrl(SDLK_D)) { String16 string = GetString(*buffer, view->carets[0].range); Caret caret = FindInBuffer(buffer, string, view->carets[0], true); @@ -451,93 +451,113 @@ void WindowCommand(Event event, Window *window, View *view) { } } - // { - // ProfileScope(mouse); + if (!Mouse(NONE)) { + ProfileScope(mouse); - // Vec2 _mouse = GetMousePosition(); - // bool mouse_in_view = CheckCollisionPointRec(_mouse, ToRectangle(window->document_rect)); - // bool mouse_in_scrollbar = CheckCollisionPointRec(_mouse, ToRectangle(window->scrollbar_rect)); - // Vec2I mouse = ToVec2I(_mouse); + Vec2 mouse_vec2 = MouseVec2(); + Vec2I mouse = MouseVec2I(); + bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect); + bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, window->scrollbar_rect); - // if (!(mouse_in_scrollbar || window->mouse_selecting_scrollbar) && (mouse_in_view || window->mouse_selecting)) { - // Vec2I mworld = mouse - window->document_rect.min + view.scroll; - // Vec2I pos = mworld / Vec2I{FontCharSpacing, FontLineSpacing}; - // XY xy = {(Int)(pos.x), (Int)(pos.y)}; - // Int p = XYToPosWithoutNL(*buffer, xy); + bool scrollbar_action = mouse_in_scrollbar || window->mouse_selecting_scrollbar; + bool document_action = mouse_in_document || window->mouse_selecting; + if (!scrollbar_action && document_action) { + Vec2I mworld = mouse - window->document_rect.min + view->scroll; + Vec2I pos = mworld / Vec2I{FontCharSpacing, FontLineSpacing}; + XY xy = {(Int)(pos.x), (Int)(pos.y)}; + Int p = XYToPosWithoutNL(*buffer, xy); - // if (mouse_in_view && IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { - // if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - // if (IsDoubleClick()) { - // Caret *c = &view.carets[0]; - // if (InBounds({c->range.min - 1, c->range.max + 1}, p)) { - // c->range = EncloseWord(*buffer, p); - // view.selection_anchor = c->range; - // } - // } else { - // if (!IsKeyDown(KEY_LEFT_CONTROL)) { - // view.carets.len = 0; - // } - // Insert(&view.carets, MakeCaret(p, p), 0); - // view.selection_anchor = view.carets[0].range; - // } - // } else if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { - // window->mouse_selecting = true; - // } - // } + if (mouse_in_document && Mouse(LEFT) && event.mouse_double_click) { + Caret *c = &view->carets[0]; + if (InBounds({c->range.min - 1, c->range.max + 1}, p)) { + c->range = EncloseWord(*buffer, p); + view->selection_anchor = c->range; + } + } else if (mouse_in_document && Mouse(LEFT) && event.ctrl) { + Insert(&view->carets, MakeCaret(p, p), 0); + view->selection_anchor = view->carets[0].range; + } else if (mouse_in_document && Mouse(LEFT)) { + view->carets.len = 0; + Insert(&view->carets, MakeCaret(p, p), 0); + view->selection_anchor = view->carets[0].range; + } - // if (window->mouse_selecting) { - // if (!IsMouseButtonDown(MOUSE_BUTTON_LEFT)) window->mouse_selecting = false; - // MergeCarets(&view, &view.selection_anchor); - // Caret &caret = view.carets[0]; - // if (view.selection_anchor.min > p) { - // caret = MakeCaret(p, view.selection_anchor.max); - // } else if (view.selection_anchor.max < p) { - // caret = MakeCaret(p, view.selection_anchor.min); - // } else { - // caret = MakeCaret(view.selection_anchor.max, view.selection_anchor.min); - // } - // MergeCarets(&view, &view.selection_anchor); - // } - // } else if (!(mouse_in_view || window->mouse_selecting) && mouse_in_scrollbar || window->mouse_selecting_scrollbar) { - // Scroller s = ComputeScrollerRect(*window); - // double size_y = (double)GetSize(window->scrollbar_rect).y; - // double p = _mouse.y - window->scrollbar_rect.min.y; + if (mouse_in_document && Mouse(LEFT)) { + window->mouse_selecting = true; + } - // if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { - // window->mouse_selecting_scrollbar = true; - // } else if (!IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { - // window->mouse_selecting_scrollbar = false; - // } + if (window->mouse_selecting) { + if (Mouse(LEFT_UP)) { + window->mouse_selecting = false; + } - // if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - // if (_mouse.y < s.rect.min.y || _mouse.y > s.rect.max.y) { - // view.scroll.y = (Int)(p / size_y * (double)s.line_count * (double)FontLineSpacing); - // window->mouse_scroller_offset = -(double)GetSize(s.rect).y / 2.0 / size_y; - // } else { - // window->mouse_scroller_offset = (s.rect.min.y - p) / size_y; - // } - // } + MergeCarets(view, &view->selection_anchor); + Caret &caret = view->carets[0]; + if (view->selection_anchor.min > p) { + caret = MakeCaret(p, view->selection_anchor.max); + } else if (view->selection_anchor.max < p) { + caret = MakeCaret(p, view->selection_anchor.min); + } else { + caret = MakeCaret(view->selection_anchor.max, view->selection_anchor.min); + } + MergeCarets(view, &view->selection_anchor); + } + } else if (!(mouse_in_document || window->mouse_selecting) && mouse_in_scrollbar || window->mouse_selecting_scrollbar) { + Scroller s = ComputeScrollerRect(*window); + double size_y = (double)GetSize(window->scrollbar_rect).y; + double p = mouse_vec2.y - window->scrollbar_rect.min.y; - // if (window->mouse_selecting_scrollbar) { - // double v = p / size_y; - // v = v + (window->mouse_scroller_offset); - // view.scroll.y = (Int)(v * (double)s.line_count * (double)FontLineSpacing); - // } - // } - // } + if (Mouse(LEFT)) { + window->mouse_selecting_scrollbar = true; + } else if (Mouse(LEFT_UP)) { + window->mouse_selecting_scrollbar = false; + } + + if (Mouse(LEFT)) { + if (mouse_vec2.y < s.rect.min.y || mouse_vec2.y > s.rect.max.y) { + view->scroll.y = (Int)(p / size_y * (double)s.line_count * (double)FontLineSpacing); + window->mouse_scroller_offset = -(double)GetSize(s.rect).y / 2.0 / size_y; + } else { + window->mouse_scroller_offset = (s.rect.min.y - p) / size_y; + } + } + + if (window->mouse_selecting_scrollbar) { + double v = p / size_y; + v = v + (window->mouse_scroller_offset); + view->scroll.y = (Int)(v * (double)s.line_count * (double)FontLineSpacing); + } + } + + if (window->mouse_selecting || window->mouse_selecting_scrollbar) { + WaitForEvents = false; + } + } +} + +void Command(Event event) { + bool run_window_command = GlobalCommand(event); + if (run_window_command) { + Window *window = GetActiveWindow(); + View *view = GetActiveView(window); + WindowCommand(event, window, view); + MergeCarets(view); + } } void ProcessSDLEvent(SDL_Event *input_event) { - Event event = {}; + Event event = {}; + SDL_Keymod mod = SDL_GetModState(); + event.shift = (mod & SDL_KMOD_SHIFT) != 0; + event.ctrl = (mod & SDL_KMOD_CTRL) != 0; + event.alt = (mod & SDL_KMOD_ALT) != 0; + event.super = (mod & SDL_KMOD_GUI) != 0; + switch (input_event->type) { case SDL_EVENT_QUIT: AppIsRunning = false; return; case SDL_EVENT_KEY_DOWN: { SDL_KeyboardEvent &key = input_event->key; event.key = key.key; - event.shift = (key.mod & SDL_KMOD_SHIFT) != 0; - event.ctrl = (key.mod & SDL_KMOD_CTRL) != 0; - event.alt = (key.mod & SDL_KMOD_ALT) != 0; - event.super = (key.mod & SDL_KMOD_GUI) != 0; } break; case SDL_EVENT_KEY_UP: { return; @@ -547,27 +567,38 @@ void ProcessSDLEvent(SDL_Event *input_event) { event.text = b.text; } break; case SDL_EVENT_MOUSE_MOTION: { - SDL_MouseMotionEvent &b = input_event->motion; - event.xmouse = (int16_t)b.x; - event.ymouse = (int16_t)b.y; - event.mouse_move = 1; + // SDL_MouseMotionEvent &b = input_event->motion; + // event.xmouse = (int16_t)b.x; + // event.ymouse = (int16_t)b.y; + // event.mouse = MOUSE_MOVE; + return; } break; case SDL_EVENT_MOUSE_BUTTON_DOWN: { SDL_MouseButtonEvent &b = input_event->button; event.xmouse = (int16_t)b.x; event.ymouse = (int16_t)b.y; if (b.button == SDL_BUTTON_LEFT) { - event.mouse_left = 1; - if (b.clicks == 2) event.mouse_double_click = 1; + event.mouse = MOUSE_LEFT; + if (b.clicks == 2) { + event.mouse_double_click = 1; + } } else if (b.button == SDL_BUTTON_RIGHT) { - event.mouse_right = 1; + event.mouse = MOUSE_RIGHT; } else if (b.button == SDL_BUTTON_MIDDLE) { - event.mouse_middle = 1; + event.mouse = MOUSE_MIDDLE; } } break; case SDL_EVENT_MOUSE_BUTTON_UP: { SDL_MouseButtonEvent &b = input_event->button; - return; + event.xmouse = (int16_t)b.x; + event.ymouse = (int16_t)b.y; + if (b.button == SDL_BUTTON_LEFT) { + event.mouse = MOUSE_LEFT_UP; + } else if (b.button == SDL_BUTTON_RIGHT) { + event.mouse = MOUSE_RIGHT_UP; + } else if (b.button == SDL_BUTTON_MIDDLE) { + event.mouse = MOUSE_MIDDLE_UP; + } } break; case SDL_EVENT_MOUSE_WHEEL: { SDL_MouseWheelEvent &b = input_event->wheel; @@ -578,13 +609,7 @@ void ProcessSDLEvent(SDL_Event *input_event) { default: return; } - bool run_window_command = GlobalCommand(event); - if (run_window_command) { - Window *window = GetActiveWindow(); - View *view = GetActiveView(window); - WindowCommand(event, window, view); - MergeCarets(view); - } + Command(event); } #if _WIN32 @@ -682,9 +707,21 @@ int main() SDL_WaitEvent(&event); ProcessSDLEvent(&event); } + WaitForEvents = true; while (SDL_PollEvent(&event)) { ProcessSDLEvent(&event); } + + { + Event event = {}; + float xmouse, ymouse; + SDL_GetMouseState(&xmouse, &ymouse); + event.xmouse = (int16_t)xmouse; + event.ymouse = (int16_t)ymouse; + event.mouse = MOUSE_MOVE; + Command(event); + } + ReplaceInfobarData(); For(order) { @@ -744,13 +781,6 @@ int main() // HandleWindowBindings(&window); DrawWindow(window); } - // { - // Scratch scratch; - // uint64_t ms = SDL_GetTicks(); - // double time = (double)ms / 1000.0; - // String string = Format(scratch, "%d %f %f", (int)FrameID, time, (time / (double)FrameID)); - // DrawString(&MainFont, ToString16(scratch, string), {0, 0}, {255, 0, 0, 255}); - // } EndFrameRender(ColorBackground); SDL_GL_SwapWindow(window); }