Small refactor

This commit is contained in:
Krzosa Karol
2024-07-27 13:01:11 +02:00
parent 477b485a34
commit 9955181b0d
6 changed files with 66 additions and 72 deletions

View File

@@ -37,7 +37,7 @@ void Command_MoveCursorsByPageSize(Window *window, int direction, bool shift = f
View &view = *GetActiveView(window);
Buffer *buffer = GetBuffer(view.active_buffer);
Rect2I visible_cells_rect = GetVisibleCells(*window);
Rect2I visible_cells_rect = GetVisibleCells(window);
Int y = GetSize(visible_cells_rect).y - 2;
if (direction == DIR_UP) y = -y;
@@ -572,7 +572,7 @@ void WindowCommand(Event event, Window *window, View *view) {
MergeCarets(view, &view->selection_anchor);
}
} else if (!(mouse_in_document || window->mouse_selecting) && mouse_in_scrollbar || window->mouse_selecting_scrollbar) {
Scroller s = ComputeScrollerRect(*window);
Scroller s = ComputeScrollerRect(window);
double size_y = (double)GetSize(window->scrollbar_rect).y;
double p = mouse_vec2.y - window->scrollbar_rect.min.y;

View File

@@ -174,10 +174,6 @@ int main()
Array<Int> order = GetWindowZOrder(scratch);
For(order) {
Window *window = &Windows[it];
if (window->invisible_when_inactive) {
if (IsActive(window)) window->visible = true;
else window->visible = false;
}
if (!window->visible) continue;
View *view = GetActiveView(window);
view->main_caret_on_begin_frame = view->carets[0];
@@ -206,9 +202,16 @@ int main()
ReplaceInfobarData();
For(order) {
For(IterateInReverse(&order)) {
Window *window = &Windows[it];
if (!window->visible) continue;
{
if (window->invisible_when_inactive) {
if (IsActive(window)) window->visible = true;
else window->visible = false;
}
if (!window->visible) continue;
}
View *view = GetActiveView(window);
Buffer *buffer = GetBuffer(view->active_buffer);
@@ -218,7 +221,7 @@ int main()
Int front = GetFront(c);
XY xy = PosToXY(*buffer, front);
Rect2I visible = GetVisibleCells(*window);
Rect2I visible = GetVisibleCells(window);
Vec2I visible_cells = GetSize(visible);
Vec2I visible_size = visible_cells * Vec2I{FontCharSpacing, FontLineSpacing};
Vec2I rect_size = GetSize(window->document_rect);
@@ -255,12 +258,7 @@ int main()
// calculating this value incrementally but do we even need X scrollbar or x clipping?
view->scroll.x = ClampBottom(view->scroll.x, (Int)0);
}
}
For(IterateInReverse(&order)) {
Window &window = Windows[it];
if (!window.visible) continue;
// HandleWindowBindings(&window);
DrawWindow(window);
}
EndFrameRender(ColorBackground);

View File

@@ -90,9 +90,9 @@ String WorkingDir;
Arena Perm;
String16 EvalString(Allocator allocator, String16 string16);
Rect2I GetVisibleCells(Window &window);
Rect2I GetVisibleCells(Window *window);
void AfterEdit(View *view, Array<Edit> edits);
Scroller ComputeScrollerRect(Window &window);
Scroller ComputeScrollerRect(Window *window);
void Command_EvalLua(View *view, String16 string);
void MergeCarets(View *view, Range *mouse_selection_anchor = NULL);
inline BufferID AllocBufferID();

View File

@@ -9,7 +9,4 @@
- word completion
- Colored strings
- move off raylib
- Adjust text position a little bit down?
- proper double click that works on laptop
- font cache and on demand unicode loads
- font cache and on demand unicode loads

View File

@@ -1,4 +1,3 @@
Array<Int> GetWindowZOrder(Allocator allocator) {
Array<Int> order = {allocator};
For(Windows) if (it.z == 2) Add(&order, GetIndex(Windows, it));
@@ -86,12 +85,12 @@ void LayoutWindows() {
Rect2I infobar_rect = CutBottom(&screen_rect, (Int)FontLineSpacing);
float line_numbers_size = GetStringSize(&MainFont, L"1234567891").x;
{
int i = 5;
if (Windows[i].visible) {
Rect2I rect = CutBottom(&screen_rect, FontLineSpacing);
Windows[i].total_rect = rect;
Windows[i].document_rect = Windows[i].total_rect;
}
int i = 5;
Rect2I sr = screen_rect;
Rect2I rect = CutBottom(&sr, FontLineSpacing);
Windows[i].total_rect = rect;
Windows[i].document_rect = Windows[i].total_rect;
Windows[i].z = 1;
}
{
int i = 0;

View File

@@ -3,27 +3,27 @@ Vec2I GetCellSize() {
return result;
}
Rect2I GetVisibleCells(Window &window) {
Rect2I GetVisibleCells(Window *window) {
ProfileFunction();
View &view = *GetActiveView(&window);
Vec2I size = GetSize(window.document_rect);
View *view = GetActiveView(window);
Vec2I size = GetSize(window->document_rect);
Int _cx = size.x / FontCharSpacing;
Int _cy = size.y / FontLineSpacing;
Int cx = _cx + 1;
Int cy = _cy + 2;
Vec2I pos = {SafeDivide(view.scroll.x, FontCharSpacing), SafeDivide(view.scroll.y, FontLineSpacing)};
Vec2I pos = {SafeDivide(view->scroll.x, FontCharSpacing), SafeDivide(view->scroll.y, FontLineSpacing)};
Int x = pos.x;
Int y = pos.y;
Rect2I result = {x, y, x + cx, y + cy};
return result;
}
Scroller ComputeScrollerRect(Window &window) {
View &view = *GetActiveView(&window);
Buffer *buffer = GetBuffer(view.active_buffer);
Vec2I size = GetSize(window.scrollbar_rect);
Scroller ComputeScrollerRect(Window *window) {
View *view = GetActiveView(window);
Buffer *buffer = GetBuffer(view->active_buffer);
Vec2I size = GetSize(window->scrollbar_rect);
Rect2I vis = GetVisibleCells(window);
Int line_count = buffer->line_starts.len + GetSize(vis).y - 1;
@@ -31,17 +31,17 @@ Scroller ComputeScrollerRect(Window &window) {
double end = (double)vis.max.y / (double)line_count;
Rect2 rect = {
{(float)window.scrollbar_rect.min.x, (float)window.scrollbar_rect.min.y + (float)((double)size.y * begin)},
{(float)window.scrollbar_rect.max.x, (float)window.scrollbar_rect.min.y + (float)((double)size.y * end)},
{(float)window->scrollbar_rect.min.x, (float)window->scrollbar_rect.min.y + (float)((double)size.y * begin)},
{(float)window->scrollbar_rect.max.x, (float)window->scrollbar_rect.min.y + (float)((double)size.y * end)},
};
Scroller result = {rect, begin, end, line_count};
return result;
}
void DrawVisibleText(Window &window) {
void DrawVisibleText(Window *window) {
ProfileFunction();
Color tint = ColorText;
View *view = GetActiveView(&window);
View *view = GetActiveView(window);
Buffer *buffer = GetBuffer(view->active_buffer);
Rect2I visible = GetVisibleCells(window);
@@ -50,7 +50,7 @@ void DrawVisibleText(Window &window) {
Vec2I pos = {visible.min.x * (Int)FontCharSpacing, line_index * (Int)FontLineSpacing};
pos -= view->scroll;
pos += window.document_rect.min;
pos += window->document_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) {
@@ -70,36 +70,36 @@ void DrawVisibleText(Window &window) {
}
}
Vec2I XYToWorldPos(const View &view, XY xy) {
Vec2I XYToWorldPos(View *view, XY xy) {
Vec2I result = {xy.col * (Int)FontCharSpacing, xy.line * (Int)FontLineSpacing};
return result;
}
Rect2I XYToRect(const View &view, XY xy) {
Rect2I XYToRect(View *view, XY xy) {
Rect2I result = Rect2IFromSize(XYToWorldPos(view, xy), GetCellSize());
return result;
}
void DrawCaret(Window &window, XY xy, float size, Color color) {
View &view = *GetActiveView(&window);
void DrawCaret(Window *window, XY xy, float size, Color color) {
View *view = GetActiveView(window);
Rect2I _rect = XYToRect(view, xy);
Rect2I rect = CutLeft(&_rect, (Int)(size * (float)FontCharSpacing));
rect -= view.scroll;
rect += window.document_rect.min;
rect -= view->scroll;
rect += window->document_rect.min;
DrawRect(ToRect2(rect), color);
}
void DrawWindow(Window &window) {
View &view = *GetActiveView(&window);
Buffer *buffer = GetBuffer(view.active_buffer);
void DrawWindow(Window *window) {
View *view = GetActiveView(window);
Buffer *buffer = GetBuffer(view->active_buffer);
SetScissor(GetScreenRectF());
DrawRect(window.total_rect, ColorBackground);
bool is_active = IsActive(&window) || window.id.id == GetLastActiveWindow().id;
DrawRect(window->total_rect, ColorBackground);
bool is_active = IsActive(window) || window->id.id == GetLastActiveWindow().id;
SetScissor(window.document_rect);
SetScissor(window->document_rect);
BeginProfileScope(draw_caret_selection);
Rect2I visible = GetVisibleCells(window);
For(view.carets) {
For(view->carets) {
XY min = PosToXY(*buffer, it.range.min);
XY max = PosToXY(*buffer, it.range.max);
if (visible.min.y > max.line) continue;
@@ -119,8 +119,8 @@ void DrawWindow(Window &window) {
if (!(a || b || c || d)) continue;
Vec2I pos = {col * FontCharSpacing, line * FontLineSpacing};
pos -= view.scroll;
pos += window.document_rect.min;
pos -= view->scroll;
pos += window->document_rect.min;
Rect2 rect = Rect2FromSize({(float)pos.x, (float)pos.y}, {(float)FontCharSpacing, (float)FontLineSpacing});
DrawRect(rect, ColorSelection);
@@ -139,8 +139,8 @@ void DrawWindow(Window &window) {
Int front = GetFront(it);
XY fxy = PosToXY(*buffer, front);
Vec2I w = XYToWorldPos(view, XYLine(fxy.line));
w -= view.scroll;
w += window.document_rect.min;
w -= view->scroll;
w += window->document_rect.min;
Rect2 rect = {
{ 0.f, (float)w.y},
{WindowSize.x, (float)w.y + (float)FontLineSpacing}
@@ -152,49 +152,49 @@ void DrawWindow(Window &window) {
DrawVisibleText(window);
BeginProfileScope(draw_carets);
For(view.carets) {
For(view->carets) {
Int front = GetFront(it);
XY fxy = PosToXY(*buffer, front);
if (fxy.col >= visible.min.x && fxy.col < visible.max.x && fxy.line >= visible.min.y && fxy.line <= visible.max.y) {
bool main_caret = &it == &view.carets.data[0];
bool main_caret = &it == &view->carets.data[0];
DrawCaret(window, fxy, 0.3f, main_caret ? ColorMainCaret : ColorSubCaret);
}
}
EndProfileScope();
// Draw line numbers
if (window.draw_line_numbers) {
SetScissor(window.line_numbers_rect);
DrawRect(window.line_numbers_rect, ColorBackground);
if (window->draw_line_numbers) {
SetScissor(window->line_numbers_rect);
DrawRect(window->line_numbers_rect, ColorBackground);
Rect2I vlines = GetVisibleCells(window);
for (Int line = vlines.min.y; line <= vlines.max.y; line += 1) {
Scratch scratch;
Vec2I pos = {0, line * FontLineSpacing};
pos.y -= view.scroll.y;
pos += window.line_numbers_rect.min;
pos.y -= view->scroll.y;
pos += window->line_numbers_rect.min;
String s = Format(scratch, "%lld", (long long)line);
String16 string = ToString16(scratch, s);
float x = GetStringSize(&MainFont, string).x;
Vec2 p = ToVec2(pos);
float rectx = (float)GetSize(window.line_numbers_rect).x;
float rectx = (float)GetSize(window->line_numbers_rect).x;
p.x += (rectx - x) / 2.f;
if (x > rectx) p.x = (float)window.line_numbers_rect.min.x;
if (x > rectx) p.x = (float)window->line_numbers_rect.min.x;
DrawString(&MainFont, string, p, ColorTextLineNumbers);
}
}
// Draw scrollbar
if (window.draw_scrollbar) {
SetScissor(window.scrollbar_rect);
if (window->draw_scrollbar) {
SetScissor(window->scrollbar_rect);
// Vec2 mouse = GetMousePosition();
// bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, ToRectangle(window.scrollbar_rect));
// bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, ToRectangle(window->scrollbar_rect));
DrawRect(window.scrollbar_rect, ColorScrollbarBackground);
DrawRect(window->scrollbar_rect, ColorScrollbarBackground);
Scroller scroller = ComputeScrollerRect(window);
Rect2 rect = Shrink(scroller.rect, 2);
Color color = ColorScrollbarScroller;
// if (!window.mouse_selecting && (window.mouse_selecting_scrollbar || mouse_in_scrollbar)) {
// if (!window->mouse_selecting && (window->mouse_selecting_scrollbar || mouse_in_scrollbar)) {
// if (is_active) color = ColorScrollbarScrollerSelected;
// }
DrawRect(rect, color);
@@ -202,6 +202,6 @@ void DrawWindow(Window &window) {
if (!is_active) {
SetScissor(GetScreenRectF());
DrawRect(window.total_rect, {0, 0, 0, 30});
DrawRect(window->total_rect, {0, 0, 0, 30});
}
}