From 535b6d00c4dcde8e5f571b776220a087c7cf346a Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Mon, 22 Jul 2024 15:20:54 +0200 Subject: [PATCH] Draw line numbers --- src/text_editor/colors.cpp | 1 + src/text_editor/text_editor.cpp | 3 +++ src/text_editor/view.h | 1 + src/text_editor/view_draw.cpp | 28 +++++++++++++++++++++++++--- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/text_editor/colors.cpp b/src/text_editor/colors.cpp index 00f2f6e..af986ff 100644 --- a/src/text_editor/colors.cpp +++ b/src/text_editor/colors.cpp @@ -38,6 +38,7 @@ Color GruvboxFadedOrange = {0xaf, 0x3a, 0x03, 0xff}; Color ColorText = GruvboxDark0Hard; Color ColorBackground = GruvboxLight0Hard; +Color ColorTextLineNumbers = GruvboxDark4; Color ColorScrollbarBackground = GruvboxLight2; Color ColorScrollbarScroller = GruvboxLight1; Color ColorScrollbarScrollerSelected = GruvboxLight0Hard; diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 871eb96..4975551 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -91,6 +91,9 @@ int main(void) { view.scrollbar_rect = CutRight(&view.rect, 10); view.infobar_rect = CutBottom(&view.rect, (int)MenuFontSize); + float width = MeasureTextEx(view.font, "12345", (float)view.font_size, (float)view.font_spacing).x; + view.line_numbers_rect = CutLeft(&view.rect, (Int)width); + BeginDrawing(); ClearBackground(ColorBackground); { diff --git a/src/text_editor/view.h b/src/text_editor/view.h index 337dbd4..a03676e 100644 --- a/src/text_editor/view.h +++ b/src/text_editor/view.h @@ -9,6 +9,7 @@ struct View { Rect2I rect; Rect2I scrollbar_rect; Rect2I infobar_rect; + Rect2I line_numbers_rect; Vec2I scroll; int mouse_selecting_scrollbar; diff --git a/src/text_editor/view_draw.cpp b/src/text_editor/view_draw.cpp index 274b9e6..1fde216 100644 --- a/src/text_editor/view_draw.cpp +++ b/src/text_editor/view_draw.cpp @@ -10,7 +10,7 @@ Rect2I GetVisibleCells(const View &view) { Int _cx = size.x / view.char_spacing; Int _cy = size.y / view.line_spacing; Int cx = _cx + 1; - Int cy = _cy + 1; + Int cy = _cy + 2; Vec2I pos = {SafeDivide(view.scroll.x, view.char_spacing), SafeDivide(view.scroll.y, view.line_spacing)}; Int x = pos.x; @@ -46,6 +46,7 @@ void DrawVisibleText(const View &view) { Vec2I pos = {visible.min.x * (Int)view.char_spacing, line_index * (Int)view.line_spacing}; pos -= view.scroll; + pos += view.rect.min; float text_offset_x = 0; for (Int col_index = visible.min.x; col_index < visible.max.x && col_index >= 0 && col_index < line_string.len; col_index += 1) { @@ -80,12 +81,14 @@ void DrawCaret(const View &view, XY xy, float size, Color color) { Rect2I _rect = XYToRect(view, xy); Rect2I rect = CutLeft(&_rect, (Int)(size * (float)view.char_spacing)); rect -= view.scroll; + rect += view.rect.min; DrawRectangleRec(ToRectangle(rect), color); } void DrawLineHighlight(const View &view, XY fxy, Color color) { Vec2I w = XYToWorldPos(view, XYLine(fxy.line)); w -= view.scroll; + w += view.rect.min; Rect2I rect = { { 0, w.y}, {GetRenderWidth(), w.y + view.line_spacing} @@ -118,6 +121,7 @@ void DrawSelection(const View &view, Caret &it) { if (a || b || c || d) { Vec2I pos = {col * view.char_spacing, line * view.line_spacing}; pos -= view.scroll; + pos += view.rect.min; Rectangle rectangle = {(float)pos.x, (float)pos.y, (float)view.char_spacing, (float)view.line_spacing}; DrawRectangleRec(rectangle, color); @@ -174,12 +178,30 @@ void DrawView(View &view) { DrawRectangleRec(ToRectangle(rect), color); } + // Draw line numbers + { + DrawRectangleRec(ToRectangle(view.line_numbers_rect), ColorBackground); + Rect2I vlines = GetVisibleCells(view); + for (Int line = vlines.min.y; line <= vlines.max.y; line += 1) { + Scratch scratch; + Vec2I pos = {0, line * view.line_spacing}; + pos -= view.scroll; + pos += view.line_numbers_rect.min; + String s = Format(scratch, "%lld", (long long)line); + String16 string = ToString16(scratch, s); + float x = MeasureTextEx(view.font, s.data, (float)view.font_size, (float)view.font_spacing).x; + Vec2 p = ToVec2(pos); + p.x += (GetSize(view.line_numbers_rect).x - x) / 2.f; + DrawString(view.font, string, p, (float)view.font_size, (float)view.font_spacing, ColorTextLineNumbers); + } + } + + // Draw info bar { DrawRectangleRec(ToRectangle(view.infobar_rect), ColorScrollbarBackground); { - Vec2 p = ToVec2(view.infobar_rect.min); - // p.y += 0.f * MenuFontSize; + Vec2 p = ToVec2(view.infobar_rect.min); Scratch scratch; Caret caret = view.carets[0]; XY xy = PosToXY(*view.buffer, GetFront(caret));