Files
text_editor/src/text_editor/view_draw.cpp
2024-07-22 15:20:54 +02:00

214 lines
9.0 KiB
C++

Vec2I GetCellSize(const View &view) {
Vec2I result = {(Int)view.char_spacing, (Int)view.line_spacing};
return result;
}
Rect2I GetVisibleCells(const View &view) {
ProfileFunction();
Vec2I size = GetSize(view.rect);
Int _cx = size.x / view.char_spacing;
Int _cy = size.y / view.line_spacing;
Int cx = _cx + 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;
Int y = pos.y;
Rect2I result = {x, y, x + cx, y + cy};
return result;
}
Scroller ComputeScrollerRect(const View &view) {
Vec2I size = GetSize(view.scrollbar_rect);
Rect2I vis = GetVisibleCells(view);
Int line_count = view.buffer->line_starts.len + GetSize(vis).y - 1;
double begin = (double)vis.min.y / (double)line_count;
double end = (double)vis.max.y / (double)line_count;
Rect2 rect = {
{(float)view.scrollbar_rect.min.x, (float)view.scrollbar_rect.min.y + (float)((double)size.y * begin)},
{(float)view.scrollbar_rect.max.x, (float)view.scrollbar_rect.min.y + (float)((double)size.y * end)},
};
Scroller result = {rect, begin, end, line_count};
return result;
}
void DrawVisibleText(const View &view) {
ProfileFunction();
Color tint = ColorText;
Rect2I visible = GetVisibleCells(view);
for (Int line_index = visible.min.y; line_index < visible.max.y && line_index >= 0 && line_index < view.buffer->line_starts.len; line_index += 1) {
Range line_range = GetLineRange(*view.buffer, line_index);
String16 line_string = GetString(*view.buffer, line_range);
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) {
int codepoint = line_string[col_index];
int index = GetGlyphIndex(view.font, codepoint);
if (codepoint == '\n' || codepoint == '\r') {
// DrawCircle((int)pos.x + (int)text_offset_x + (int)view.char_spacing / 2, (int)pos.y + (int)view.line_spacing / 2, (float)view.font_size / 10.f, tint);
} else if (codepoint == ' ') {
// DrawCircle((int)pos.x + (int)text_offset_x + (int)view.char_spacing / 2, (int)pos.y + (int)view.line_spacing / 2, (float)view.font_size / 10.f, space_color);
} else if (codepoint != '\t') {
DrawTextCodepoint(view.font, codepoint, {(float)pos.x + text_offset_x, (float)pos.y}, (float)view.font_size, tint);
}
if (view.font.glyphs[index].advanceX == 0) text_offset_x += ((float)view.font.recs[index].width + view.font_spacing);
else text_offset_x += ((float)view.font.glyphs[index].advanceX + view.font_spacing);
}
}
}
Vec2I XYToWorldPos(const View &view, XY xy) {
Vec2I result = {xy.col * (Int)view.char_spacing, xy.line * (Int)view.line_spacing};
return result;
}
Rect2I XYToRect(const View &view, XY xy) {
Rect2I result = Rect2IFromSize(XYToWorldPos(view, xy), GetCellSize(view));
return result;
}
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}
};
DrawRectangleRec(ToRectangle(rect), color);
}
void DrawSelection(const View &view, Caret &it) {
ProfileFunction();
Buffer &buf = *view.buffer;
Int front = GetFront(it);
Int back = GetBack(it);
if (front != back) {
XY bxy = PosToXY(buf, back);
XY min = PosToXY(buf, it.range.min);
XY max = PosToXY(buf, it.range.max);
Color color = ColorSelection;
Rect2I vlines = GetVisibleCells(view);
for (Int line = vlines.min.y; line <= vlines.max.y && line >= 0 && line < view.buffer->line_starts.len; line += 1) {
Range range = GetLineRange(buf, line);
String16 line_string = GetString(buf, range);
for (Int col = vlines.min.x; col < vlines.max.x && col >= 0 && col < line_string.len; col += 1) {
bool a = line > min.line && line < max.line;
bool b = min.line != max.line && (line == min.line && col >= min.col);
bool c = min.line != max.line && (line == max.line && col < max.col);
bool d = min.line == max.line && line == max.line && col >= min.col && col < max.col;
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);
if (line_string[col] == ' ' || line_string[col] == '\t') {
DrawCircle((int)pos.x + (int)view.char_spacing / 2, (int)pos.y + (int)view.line_spacing / 2, (float)view.font_size / 10.f, ColorWhitespaceDuringSelection);
} else if (line_string[col] == '\n') {
DrawEllipse((int)pos.x + (int)view.char_spacing / 2, (int)pos.y + (int)view.line_spacing / 2, (float)view.font_size / 4.f, (float)view.font_size / 15.f, ColorWhitespaceDuringSelection);
} else if (line_string[col] == '\r') {
DrawEllipse((int)pos.x + (int)view.char_spacing / 2, (int)pos.y + (int)view.line_spacing / 2, (float)view.font_size / 10.f, (float)view.font_size / 4.f, ColorWhitespaceDuringSelection);
}
}
}
}
}
}
void DrawView(View &view) {
Buffer &buf = *view.buffer;
BeginScissorMode((int)view.rect.min.x, (int)view.rect.min.y, (int)view.rect.max.x - (int)view.rect.min.x, (int)view.rect.max.y - (int)view.rect.min.y);
For(view.carets) {
Int front = GetFront(it);
XY fxy = PosToXY(buf, front);
if (GetSize(it.range)) {
DrawSelection(view, it);
} else {
DrawLineHighlight(view, fxy, ColorLineHighlight);
}
}
DrawVisibleText(view);
For(view.carets) {
Int front = GetFront(it);
XY fxy = PosToXY(buf, front);
bool main_caret = &it == &view.carets.data[0];
DrawCaret(view, fxy, 0.3f, main_caret ? ColorMainCaret : ColorSubCaret);
}
EndScissorMode();
// Draw scrollbar
{
Vec2 mouse = GetMousePosition();
bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, ToRectangle(view.scrollbar_rect));
DrawRectangleRec(ToRectangle(view.scrollbar_rect), ColorScrollbarBackground);
Scroller scroller = ComputeScrollerRect(view);
Rect2 rect = Shrink(scroller.rect, 2);
Color color = ColorScrollbarScroller;
if (!view.mouse_selecting && (view.mouse_selecting_scrollbar || mouse_in_scrollbar)) {
color = ColorScrollbarScrollerSelected;
}
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);
Scratch scratch;
Caret caret = view.carets[0];
XY xy = PosToXY(*view.buffer, GetFront(caret));
String s = Format(scratch, "-- line: %lld col: %lld", (long long)xy.line + 1ll, (long long)xy.col + 1ll);
String16 string = ToString16(scratch, s);
DrawString(MenuFont, string, p, MenuFontSize, 1, ColorText);
}
}
}