Prepare for adding windows

This commit is contained in:
Krzosa Karol
2024-07-22 16:46:41 +02:00
parent 53e153a696
commit 37a0c73dc3
4 changed files with 110 additions and 69 deletions

View File

@@ -21,6 +21,12 @@
float MenuFontSize = 19.f;
Font MenuFont;
Font Font;
Int FontSize;
Int FontSpacing;
Int FontLineSpacing;
Int FontCharSpacing;
#include "commands.cpp"
#include "colors.cpp"
@@ -30,10 +36,12 @@ Font MenuFont;
#include "view_draw.cpp"
/*
- Colored strings
- move off raylib
- line endings
- command window
- word completion
- Colored strings
- file dock on left side
- multiple windows
- multiple views per window
@@ -61,44 +69,68 @@ int main(void) {
SetWindowIcon(window_icon_image);
}
MenuFont = LoadFontEx("c:\\Windows\\Fonts\\Segoeui.ttf", (int)MenuFontSize, NULL, 500);
MenuFontSize = 19;
MenuFont = LoadFontEx("c:\\Windows\\Fonts\\Segoeui.ttf", (int)MenuFontSize, NULL, 500);
FontSize = 16;
FontSpacing = 1;
FontLineSpacing = FontSize;
Font = LoadFontEx("c:\\Windows\\Fonts\\consola.ttf", (int)FontSize, NULL, 500);
FontCharSpacing = GetCharSpacing(Font, FontSize, FontSpacing);
Array<Window> windows = {};
View view = {};
View view = {};
{
Int font_size = 16;
Int font_spacing = 1;
Int line_spacing = font_size;
Font font = LoadFontEx("c:\\Windows\\Fonts\\consola.ttf", (int)font_size, NULL, 500);
view.font = font;
view.font_size = font_size;
view.font_spacing = font_spacing;
view.line_spacing = line_spacing;
view.char_spacing = GetCharSpacing(font, font_size, font_spacing);
Add(&view.carets, {0, 0});
view.buffer = CreateBuffer(Perm);
// LoadUnicode(view.buffer);
// LoadBigTextAndBigLine(view.buffer);
// LoadBigLine(view.buffer);
// LoadBigText(view.buffer);
LoadTextA(view.buffer);
// LoadLine(view.buffer);
Window window = {};
{
Add(&view.carets, {0, 0});
view.buffer = CreateBuffer(Perm);
LoadTextA(view.buffer);
Add(&window.views, view);
}
Add(&windows, window);
}
{
Window window = {};
{
View view = {};
Add(&view.carets, {0, 0});
view.buffer = CreateBuffer(Perm);
LoadUnicode(view.buffer);
Add(&window.views, view);
}
Add(&windows, window);
}
while (!WindowShouldClose()) {
ProfileScope(game_loop);
view.rect = GetScreenRect();
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);
Rect2I screen_rect = GetScreenRect();
float line_numbers_size = MeasureTextEx(Font, "12345", (float)FontSize, (float)FontSpacing).x;
{
windows[0].document_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 0.5));
windows[0].scrollbar_rect = CutRight(&windows[0].document_rect, 10);
windows[0].infobar_rect = CutBottom(&windows[0].document_rect, (Int)MenuFontSize);
windows[0].line_numbers_rect = CutLeft(&windows[0].document_rect, (Int)line_numbers_size);
}
{
windows[1].document_rect = screen_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);
}
view.rect = GetScreenRect();
view.scrollbar_rect = CutRight(&view.rect, 10);
view.infobar_rect = CutBottom(&view.rect, (int)MenuFontSize);
view.line_numbers_rect = CutLeft(&view.rect, (Int)line_numbers_size);
BeginDrawing();
ClearBackground(ColorBackground);
{
HandleKeybindings(&view);
DrawView(view);
DrawWindow(view);
}
EndDrawing();
}

View File

@@ -1,26 +1,35 @@
struct View {
// @todo: some of this data probably should go to window
Font font;
Int font_size;
Int font_spacing;
Int line_spacing;
Int char_spacing;
struct View {
Rect2I rect;
Rect2I scrollbar_rect;
Rect2I infobar_rect;
Rect2I line_numbers_rect;
Vec2I scroll;
int mouse_selecting_scrollbar;
bool mouse_selecting;
Range selection_anchor;
double mouse_scroller_offset;
Range selection_anchor;
Vec2I scroll;
Buffer *buffer;
Array<Caret> carets;
};
struct Window {
Int active_view;
Array<View> views;
Rect2I scrollbar_rect;
Rect2I infobar_rect;
Rect2I line_numbers_rect;
Rect2I document_rect;
int mouse_selecting_scrollbar;
bool mouse_selecting;
double mouse_scroller_offset;
Range selection_anchor;
};
struct Scroller {
Rect2 rect;
double begin;

View File

@@ -326,7 +326,7 @@ void HandleKeybindings(View *_view) {
}
Vec2I mworld = mouse - view.rect.min + view.scroll;
Vec2I pos = mworld / Vec2I{view.char_spacing, view.line_spacing};
Vec2I pos = mworld / Vec2I{FontCharSpacing, FontLineSpacing};
XY xy = {(Int)(pos.x), (Int)(pos.y)};
Int p = XYToPosWithoutNL(buf, xy);
@@ -377,7 +377,7 @@ void HandleKeybindings(View *_view) {
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
if (_mouse.y < s.rect.min.y || _mouse.y > s.rect.max.y) {
view.scroll.y = (Int)(p / size_y * (double)s.line_count * (double)view.line_spacing);
view.scroll.y = (Int)(p / size_y * (double)s.line_count * (double)FontLineSpacing);
view.mouse_scroller_offset = -(double)GetSize(s.rect).y / 2.0 / size_y;
} else {
view.mouse_scroller_offset = (s.rect.min.y - p) / size_y;
@@ -387,7 +387,7 @@ void HandleKeybindings(View *_view) {
if (view.mouse_selecting_scrollbar) {
double v = p / size_y;
v = v + (view.mouse_scroller_offset);
view.scroll.y = (Int)(v * (double)s.line_count * (double)view.line_spacing);
view.scroll.y = (Int)(v * (double)s.line_count * (double)FontLineSpacing);
}
}
}
@@ -400,27 +400,27 @@ void HandleKeybindings(View *_view) {
Rect2I visible = GetVisibleCells(view);
Vec2I visible_cells = GetSize(visible);
Vec2I visible_size = visible_cells * Vec2I{view.char_spacing, view.line_spacing};
Vec2I visible_size = visible_cells * Vec2I{FontCharSpacing, FontLineSpacing};
Vec2I rect_size = GetSize(view.rect);
if (xy.line > visible.max.y - 2) {
Int set_view_at_line = xy.line - (visible_cells.y - 1);
Int cut_off_y = Max((Int)0, visible_size.y - rect_size.y);
view.scroll.y = (set_view_at_line * view.line_spacing) + cut_off_y;
view.scroll.y = (set_view_at_line * FontLineSpacing) + cut_off_y;
}
if (xy.line < visible.min.y + 1) {
view.scroll.y = xy.line * view.line_spacing;
view.scroll.y = xy.line * FontLineSpacing;
}
if (xy.col >= visible.max.x - 1) {
Int set_view_at_line = xy.col - (visible_cells.x - 1);
Int cut_off_x = Max((Int)0, visible_size.x - rect_size.x);
view.scroll.x = (set_view_at_line * view.char_spacing) + cut_off_x;
view.scroll.x = (set_view_at_line * FontCharSpacing) + cut_off_x;
}
if (xy.col <= visible.min.x) {
view.scroll.x = xy.col * view.char_spacing;
view.scroll.x = xy.col * FontCharSpacing;
}
}
@@ -428,7 +428,7 @@ void HandleKeybindings(View *_view) {
{
ProfileScope(clip_scroll);
Int last_line = LastLine(view.buffer[0]);
view.scroll.y = Clamp(view.scroll.y, (Int)0, Max((Int)0, (last_line - 1) * view.line_spacing));
view.scroll.y = Clamp(view.scroll.y, (Int)0, Max((Int)0, (last_line - 1) * FontLineSpacing));
// @note:
// GetCharCountOfLongestLine is a bottleneck, there is probably an algorithm for

View File

@@ -1,5 +1,5 @@
Vec2I GetCellSize(const View &view) {
Vec2I result = {(Int)view.char_spacing, (Int)view.line_spacing};
Vec2I result = {(Int)FontCharSpacing, (Int)FontLineSpacing};
return result;
}
@@ -7,12 +7,12 @@ 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 = size.x / FontCharSpacing;
Int _cy = size.y / FontLineSpacing;
Int cx = _cx + 1;
Int cy = _cy + 2;
Vec2I pos = {SafeDivide(view.scroll.x, view.char_spacing), SafeDivide(view.scroll.y, view.line_spacing)};
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};
@@ -44,31 +44,31 @@ void DrawVisibleText(const View &view) {
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};
Vec2I pos = {visible.min.x * (Int)FontCharSpacing, line_index * (Int)FontLineSpacing};
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);
int index = GetGlyphIndex(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);
// DrawCircle((int)pos.x + (int)text_offset_x + (int)FontCharSpacing / 2, (int)pos.y + (int)FontLineSpacing / 2, (float)FontSize / 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);
// DrawCircle((int)pos.x + (int)text_offset_x + (int)FontCharSpacing / 2, (int)pos.y + (int)FontLineSpacing / 2, (float)FontSize / 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);
DrawTextCodepoint(Font, codepoint, {(float)pos.x + text_offset_x, (float)pos.y}, (float)FontSize, 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);
if (Font.glyphs[index].advanceX == 0) text_offset_x += ((float)Font.recs[index].width + FontSpacing);
else text_offset_x += ((float)Font.glyphs[index].advanceX + FontSpacing);
}
}
}
Vec2I XYToWorldPos(const View &view, XY xy) {
Vec2I result = {xy.col * (Int)view.char_spacing, xy.line * (Int)view.line_spacing};
Vec2I result = {xy.col * (Int)FontCharSpacing, xy.line * (Int)FontLineSpacing};
return result;
}
@@ -79,7 +79,7 @@ Rect2I XYToRect(const View &view, XY xy) {
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));
Rect2I rect = CutLeft(&_rect, (Int)(size * (float)FontCharSpacing));
rect -= view.scroll;
rect += view.rect.min;
DrawRectangleRec(ToRectangle(rect), color);
@@ -90,8 +90,8 @@ void DrawLineHighlight(const View &view, XY fxy, Color color) {
w -= view.scroll;
w += view.rect.min;
Rect2I rect = {
{ 0, w.y},
{GetRenderWidth(), w.y + view.line_spacing}
{ 0, w.y},
{GetRenderWidth(), w.y + FontLineSpacing}
};
DrawRectangleRec(ToRectangle(rect), color);
}
@@ -119,19 +119,19 @@ void DrawSelection(const View &view, Caret &it) {
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};
Vec2I pos = {col * FontCharSpacing, line * FontLineSpacing};
pos -= view.scroll;
pos += view.rect.min;
Rectangle rectangle = {(float)pos.x, (float)pos.y, (float)view.char_spacing, (float)view.line_spacing};
Rectangle rectangle = {(float)pos.x, (float)pos.y, (float)FontCharSpacing, (float)FontLineSpacing};
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);
DrawCircle((int)pos.x + (int)FontCharSpacing / 2, (int)pos.y + (int)FontLineSpacing / 2, (float)FontSize / 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);
DrawEllipse((int)pos.x + (int)FontCharSpacing / 2, (int)pos.y + (int)FontLineSpacing / 2, (float)FontSize / 4.f, (float)FontSize / 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);
DrawEllipse((int)pos.x + (int)FontCharSpacing / 2, (int)pos.y + (int)FontLineSpacing / 2, (float)FontSize / 10.f, (float)FontSize / 4.f, ColorWhitespaceDuringSelection);
}
}
}
@@ -139,7 +139,7 @@ void DrawSelection(const View &view, Caret &it) {
}
}
void DrawView(View &view) {
void DrawWindow(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);
@@ -187,17 +187,17 @@ void DrawView(View &view) {
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};
Vec2I pos = {0, line * FontLineSpacing};
pos.y -= view.scroll.y;
pos += r.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;
float x = MeasureTextEx(Font, s.data, (float)FontSize, (float)FontSpacing).x;
Vec2 p = ToVec2(pos);
float rectx = (float)GetSize(r).x;
p.x += (rectx - x) / 2.f;
if (x > rectx) p.x = 0;
DrawString(view.font, string, p, (float)view.font_size, (float)view.font_spacing, ColorTextLineNumbers);
DrawString(Font, string, p, (float)FontSize, (float)FontSpacing, ColorTextLineNumbers);
}
EndScissorMode();