diff --git a/src/text_editor/buffer.cpp b/src/text_editor/buffer.cpp index 8d8e0c8..8ec6e66 100644 --- a/src/text_editor/buffer.cpp +++ b/src/text_editor/buffer.cpp @@ -11,11 +11,10 @@ void InitBuffer(Allocator allocator, Buffer *buffer) { Add(&buffer->line_starts, (Int)0); } -BufferID CreateBuffer(Allocator allocator) { - Buffer result = {}; - InitBuffer(allocator, &result); - Add(&Buffers, result); - return result.id; +Buffer *CreateBuffer(Allocator allocator) { + Buffer *result = Alloc(&Buffers); + InitBuffer(allocator, result); + return result; } void Grow(Buffer *buffer, Int change_size) { diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 0f6d64a..0e38280 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -22,19 +22,17 @@ #include "view_draw.cpp" /* -- Window IDS and View IDS and Buffer IDS -- Null Window, Null View, Null Buffer -- Open file (utf8->utf16), process determine line endings, tabs to spaces?, Save file (utf16->utf8) +j- Open file (utf8->utf16), process determine line endings, tabs to spaces?, Save file (utf16->utf8) - - -- move off raylib - line endings - command window - word completion - Colored strings - file dock on left side -- Font cache + +- move off raylib + - proper double click that works on laptop + - font cache and on demand unicode loads */ int main(void) { @@ -67,62 +65,36 @@ int main(void) { MainFont = LoadFontEx("c:\\Windows\\Fonts\\consola.ttf", (int)FontSize, NULL, 500); FontCharSpacing = GetCharSpacing(MainFont, FontSize, FontSpacing); + Allocator sys_allocator = GetSystemAllocator(); // Create null { - CreateBuffer(GetSystemAllocator()); - - View view = {AllocViewID()}; - Add(&view.carets, {0, 0}); - Add(&Views, view); - - Window window = {AllocWindowID()}; - Add(&window.views, {0}); - Add(&Windows, window); + Buffer *buffer = CreateBuffer(sys_allocator); + View *view = CreateView(buffer->id); + Window *window = CreateWindow(); + window->visible = false; + AddView(window->id, view->id); } { - Window window = {AllocWindowID()}; - { - View view = {}; - view.id = AllocViewID(); - Add(&view.carets, {0, 0}); - view.buffer_id = CreateBuffer(Perm); - Buffer *buffer = GetBuffer(view.buffer_id); - LoadTextA(buffer); - Add(&Views, view); - Add(&window.views, view.id); - window.active_view = view.id; - } - Add(&Windows, window); + Window *w = CreateWindow(); + Buffer *b = CreateBuffer(sys_allocator); + View *v = CreateView(b->id); + LoadTextA(b); + AddView(w->id, v->id); } { - Window window = {AllocWindowID()}; - { - View view = {AllocViewID()}; - Add(&view.carets, {0, 0}); - view.buffer_id = CreateBuffer(Perm); - Buffer *buffer = GetBuffer(view.buffer_id); - LoadUnicode(buffer); - Add(&Views, view); - Add(&window.views, view.id); - window.active_view = view.id; - } - Add(&Windows, window); + Window *w = CreateWindow(); + Buffer *b = CreateBuffer(sys_allocator); + View *v = CreateView(b->id); + LoadUnicode(b); + AddView(w->id, v->id); } { - Window window = {AllocWindowID()}; - { - View view = {}; - view.id = AllocViewID(); - Add(&view.carets, {0, 0}); - view.buffer_id = CreateBuffer(Perm); - Buffer *buffer = GetBuffer(view.buffer_id); - LoadLine(buffer); - Add(&Views, view); - Add(&window.views, view.id); - window.active_view = view.id; - } - Add(&Windows, window); + Window *w = CreateWindow(); + Buffer *b = CreateBuffer(sys_allocator); + View *v = CreateView(b->id); + LoadLine(b); + AddView(w->id, v->id); } while (!WindowShouldClose()) { @@ -131,35 +103,38 @@ int main(void) { Rect2I screen_rect = GetScreenRect(); float line_numbers_size = MeasureTextEx(MainFont, "12345", (float)FontSize, (float)FontSpacing).x; { - Windows[1].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 0.33)); - Windows[1].document_rect = Windows[1].total_rect; - Windows[1].scrollbar_rect = CutRight(&Windows[1].document_rect, 10); - Windows[1].infobar_rect = CutBottom(&Windows[1].document_rect, (Int)MenuFontSize); - Windows[1].line_numbers_rect = CutLeft(&Windows[1].document_rect, (Int)line_numbers_size); + int i = 1; + Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 0.33)); + Windows[i].document_rect = Windows[i].total_rect; + if (Windows[i].draw_scrollbar) Windows[i].scrollbar_rect = CutRight(&Windows[i].document_rect, 10); + if (Windows[i].draw_infobar) Windows[i].infobar_rect = CutBottom(&Windows[i].document_rect, (Int)MenuFontSize); + if (Windows[i].draw_line_numbers) Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size); } { - int i = 2; - Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 0.5)); - Windows[i].document_rect = Windows[i].total_rect; - Windows[i].scrollbar_rect = CutRight(&Windows[i].document_rect, 10); - Windows[i].infobar_rect = CutBottom(&Windows[i].document_rect, (Int)MenuFontSize); - Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size); + int i = 2; + Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 0.5)); + Windows[i].document_rect = Windows[i].total_rect; + if (Windows[i].draw_scrollbar) Windows[i].scrollbar_rect = CutRight(&Windows[i].document_rect, 10); + if (Windows[i].draw_infobar) Windows[i].infobar_rect = CutBottom(&Windows[i].document_rect, (Int)MenuFontSize); + if (Windows[i].draw_line_numbers) Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size); } { - int i = 3; - Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 1.0)); - Windows[i].document_rect = Windows[i].total_rect; - Windows[i].scrollbar_rect = CutRight(&Windows[i].document_rect, 10); - Windows[i].infobar_rect = CutBottom(&Windows[i].document_rect, (Int)MenuFontSize); - Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size); + int i = 3; + Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 1.0)); + Windows[i].document_rect = Windows[i].total_rect; + if (Windows[i].draw_scrollbar) Windows[i].scrollbar_rect = CutRight(&Windows[i].document_rect, 10); + if (Windows[i].draw_infobar) Windows[i].infobar_rect = CutBottom(&Windows[i].document_rect, (Int)MenuFontSize); + if (Windows[i].draw_line_numbers) Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size); } BeginDrawing(); ClearBackground(ColorBackground); For(Windows) { + if (!it.visible) continue; HandleKeybindings(&it); DrawWindow(it); } + SetMouseCursor(); EndDrawing(); } CloseWindow(); diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index b9d32d6..7327372 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -54,9 +54,16 @@ struct Window { Rect2I line_numbers_rect; Rect2I document_rect; - int mouse_selecting_scrollbar; - bool mouse_selecting; double mouse_scroller_offset; + + struct { + bool mouse_selecting_scrollbar : 1; + bool mouse_selecting : 1; + bool draw_scrollbar : 1; + bool draw_line_numbers : 1; + bool draw_infobar : 1; + bool visible : 1; + }; }; struct Scroller { @@ -83,7 +90,7 @@ Array Views = {}; Array Windows = {}; WindowID ActiveWindow = {}; -float MenuFontSize = 19.f; +float MenuFontSize; Font MenuFont; Font MainFont; diff --git a/src/text_editor/view_commands.cpp b/src/text_editor/view_commands.cpp index eed3510..a52e41d 100644 --- a/src/text_editor/view_commands.cpp +++ b/src/text_editor/view_commands.cpp @@ -334,17 +334,7 @@ void HandleKeybindings(Window *window) { bool mouse_in_scrollbar = CheckCollisionPointRec(_mouse, ToRectangle(window->scrollbar_rect)); Vec2I mouse = ToVec2I(_mouse); - if (!mouse_in_view) SetMouseCursor(MOUSE_CURSOR_DEFAULT); - if (!(mouse_in_scrollbar || window->mouse_selecting_scrollbar) && (mouse_in_view || window->mouse_selecting)) { - if (!window->mouse_selecting) { - if (mouse_in_view) { - SetMouseCursor(MOUSE_CURSOR_IBEAM); - } else { - SetMouseCursor(MOUSE_CURSOR_DEFAULT); - } - } - Vec2I mworld = mouse - window->document_rect.min + view.scroll; Vec2I pos = mworld / Vec2I{FontCharSpacing, FontLineSpacing}; XY xy = {(Int)(pos.x), (Int)(pos.y)}; diff --git a/src/text_editor/view_draw.cpp b/src/text_editor/view_draw.cpp index 4686007..f5bb59d 100644 --- a/src/text_editor/view_draw.cpp +++ b/src/text_editor/view_draw.cpp @@ -173,7 +173,7 @@ void DrawWindow(Window &window) { EndScissorMode(); // Draw scrollbar - { + if (window.draw_scrollbar) { Vec2 mouse = GetMousePosition(); bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, ToRectangle(window.scrollbar_rect)); @@ -188,7 +188,7 @@ void DrawWindow(Window &window) { } // Draw line numbers - { + if (window.draw_line_numbers) { Rect2I r = window.line_numbers_rect; DrawRectangleRec(ToRectangle(r), ColorBackground); BeginScissorMode((int)r.min.x, (int)r.min.y, (int)r.max.x - (int)r.min.x, (int)r.max.y - (int)r.min.y); @@ -213,7 +213,7 @@ void DrawWindow(Window &window) { } // Draw info bar - { + if (window.draw_infobar) { Rect2I r = window.infobar_rect; DrawRectangleRec(ToRectangle(r), ColorScrollbarBackground); { diff --git a/src/text_editor/windows.cpp b/src/text_editor/windows.cpp index e69de29..5b5a6b3 100644 --- a/src/text_editor/windows.cpp +++ b/src/text_editor/windows.cpp @@ -0,0 +1,41 @@ +Window *CreateWindow() { + Window *w = Alloc(&Windows); + w->visible = true; + w->draw_scrollbar = true; + w->draw_line_numbers = true; + w->draw_infobar = true; + w->id = AllocWindowID(); + return w; +} + +void AddView(WindowID window, ViewID view) { + Window *w = GetWindow(window); + if (w->active_view.id == 0) w->active_view = view; + For(w->views) if (it.id == view.id) return; + Add(&w->views, view); +} + +View *CreateView(BufferID buffer_id) { + View *w = Alloc(&Views); + w->id = AllocViewID(); + w->buffer_id = buffer_id; + Add(&w->carets, {0, 0}); + return w; +} + +void SetMouseCursor() { + Window *w = GetActiveWindow(); + Vec2 mouse = GetMousePosition(); + + bool mouse_in_document = CheckCollisionPointRec(mouse, ToRectangle(w->document_rect)); + bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, ToRectangle(w->scrollbar_rect)); + bool mouse_in_total = CheckCollisionPointRec(mouse, ToRectangle(w->total_rect)); + + if (w->mouse_selecting || mouse_in_document) { + SetMouseCursor(MOUSE_CURSOR_IBEAM); + } else if (mouse_in_scrollbar || w->mouse_selecting_scrollbar) { + SetMouseCursor(MOUSE_CURSOR_DEFAULT); + } else { + SetMouseCursor(MOUSE_CURSOR_POINTING_HAND); + } +} \ No newline at end of file