Draw line numbers

This commit is contained in:
Krzosa Karol
2024-07-22 15:20:54 +02:00
parent 5594b55e5c
commit 535b6d00c4
4 changed files with 30 additions and 3 deletions

View File

@@ -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;

View File

@@ -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);
{

View File

@@ -9,6 +9,7 @@ struct View {
Rect2I rect;
Rect2I scrollbar_rect;
Rect2I infobar_rect;
Rect2I line_numbers_rect;
Vec2I scroll;
int mouse_selecting_scrollbar;

View File

@@ -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;
Scratch scratch;
Caret caret = view.carets[0];
XY xy = PosToXY(*view.buffer, GetFront(caret));