Initial window implementation

This commit is contained in:
Krzosa Karol
2024-07-22 17:16:48 +02:00
parent 37a0c73dc3
commit bc4fbfd28a
5 changed files with 146 additions and 109 deletions

View File

@@ -31,6 +31,7 @@ Int FontCharSpacing;
#include "colors.cpp"
#include "view.h"
#include "windows.cpp"
#include "view_commands_clipboard.cpp"
#include "view_commands.cpp"
#include "view_draw.cpp"
@@ -43,7 +44,7 @@ Int FontCharSpacing;
- word completion
- Colored strings
- file dock on left side
- multiple windows
- multiple Windows
- multiple views per window
- Font cache
*/
@@ -78,8 +79,7 @@ int main(void) {
Font = LoadFontEx("c:\\Windows\\Fonts\\consola.ttf", (int)FontSize, NULL, 500);
FontCharSpacing = GetCharSpacing(Font, FontSize, FontSpacing);
Array<Window> windows = {};
View view = {};
View view = {};
{
Window window = {};
@@ -89,7 +89,7 @@ int main(void) {
LoadTextA(view.buffer);
Add(&window.views, view);
}
Add(&windows, window);
Add(&Windows, window);
}
{
Window window = {};
@@ -100,7 +100,7 @@ int main(void) {
LoadUnicode(view.buffer);
Add(&window.views, view);
}
Add(&windows, window);
Add(&Windows, window);
}
while (!WindowShouldClose()) {
@@ -109,28 +109,25 @@ int main(void) {
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[0].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 0.5));
Windows[0].document_rect = Windows[0].total_rect;
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);
Windows[1].total_rect = screen_rect;
Windows[1].document_rect = Windows[1].total_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);
DrawWindow(view);
For(Windows) {
HandleKeybindings(&it);
DrawWindow(it);
}
EndDrawing();
}

View File

@@ -1,24 +1,15 @@
struct View {
Rect2I rect;
Rect2I scrollbar_rect;
Rect2I infobar_rect;
Rect2I line_numbers_rect;
int mouse_selecting_scrollbar;
bool mouse_selecting;
double mouse_scroller_offset;
Range selection_anchor;
Vec2I scroll;
Buffer *buffer;
Array<Caret> carets;
Range selection_anchor;
};
struct Window {
Int active_view;
Array<View> views;
Rect2I total_rect;
Rect2I scrollbar_rect;
Rect2I infobar_rect;
Rect2I line_numbers_rect;
@@ -27,7 +18,6 @@ struct Window {
int mouse_selecting_scrollbar;
bool mouse_selecting;
double mouse_scroller_offset;
Range selection_anchor;
};
struct Scroller {
@@ -37,6 +27,6 @@ struct Scroller {
Int line_count;
};
Rect2I GetVisibleCells(const View &view);
Rect2I GetVisibleCells(Window &window);
void AfterEdit(View *view, Array<Edit> edits);
Scroller ComputeScrollerRect(const View &view);
Scroller ComputeScrollerRect(Window &window);

View File

@@ -30,12 +30,12 @@ void Command_DuplicateLine(View *view, int direction) {
}
bool SHIFT_PRESSED = true;
void Command_MoveCursorsByPageSize(View *_view, int direction, bool shift = false) {
void Command_MoveCursorsByPageSize(Window *window, int direction, bool shift = false) {
Assert(direction == DIR_UP || direction == DIR_DOWN);
View &view = *_view;
View &view = *GetActiveView(*window);
Buffer &buf = *view.buffer;
Rect2I visible_cells_rect = GetVisibleCells(view);
Rect2I visible_cells_rect = GetVisibleCells(*window);
Int y = GetSize(visible_cells_rect).y - 2;
if (direction == DIR_UP) y = -y;
@@ -51,9 +51,9 @@ void Command_MoveCursorsByPageSize(View *_view, int direction, bool shift = fals
}
}
void Command_MoveCursorsToSide(View *_view, int direction, bool shift = false) {
void Command_MoveCursorsToSide(Window *window, int direction, bool shift = false) {
Assert(direction == DIR_LEFT || direction == DIR_RIGHT);
View &view = *_view;
View &view = *GetActiveView(*window);
Buffer &buf = *view.buffer;
For(view.carets) {
@@ -110,16 +110,34 @@ void Command_CreateCursorVertical(View *_view, int direction) {
MergeCarets(&view.carets);
}
void HandleKeybindings(View *_view) {
void HandleKeybindings(Window *window) {
ProfileFunction();
View &view = *_view;
View &view = *GetActiveView(*window);
Buffer &buf = *view.buffer;
Caret main_caret_on_begin_frame = view.carets[0];
if (IsKeyDown(KEY_F1)) {
view.scroll.x -= (Int)(GetMouseWheelMove() * 48);
} else {
view.scroll.y -= (Int)(GetMouseWheelMove() * 48);
{
Vec2 _mouse = GetMousePosition();
bool mouse_in_view = CheckCollisionPointRec(_mouse, ToRectangle(window->document_rect));
bool mouse_in_scrollbar = CheckCollisionPointRec(_mouse, ToRectangle(window->scrollbar_rect));
if (mouse_in_view || mouse_in_scrollbar) {
if (IsKeyDown(KEY_F1)) {
view.scroll.x -= (Int)(GetMouseWheelMove() * 48);
} else {
view.scroll.y -= (Int)(GetMouseWheelMove() * 48);
}
if (!IsActive(window)) {
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
ActiveWindow = GetIndex(Windows, *window);
}
}
}
}
if (!IsActive(window)) {
return;
}
if (CtrlPress(KEY_F2)) {
@@ -254,27 +272,27 @@ void HandleKeybindings(View *_view) {
}
if (ShiftPress(KEY_PAGE_UP)) {
Command_MoveCursorsByPageSize(&view, DIR_UP, SHIFT_PRESSED);
Command_MoveCursorsByPageSize(window, DIR_UP, SHIFT_PRESSED);
} else if (Press(KEY_PAGE_UP)) {
Command_MoveCursorsByPageSize(&view, DIR_UP);
Command_MoveCursorsByPageSize(window, DIR_UP);
}
if (ShiftPress(KEY_PAGE_DOWN)) {
Command_MoveCursorsByPageSize(&view, DIR_DOWN, SHIFT_PRESSED);
Command_MoveCursorsByPageSize(window, DIR_DOWN, SHIFT_PRESSED);
} else if (Press(KEY_PAGE_DOWN)) {
Command_MoveCursorsByPageSize(&view, DIR_DOWN);
Command_MoveCursorsByPageSize(window, DIR_DOWN);
}
if (ShiftPress(KEY_HOME)) {
Command_MoveCursorsToSide(&view, DIR_LEFT, SHIFT_PRESSED);
Command_MoveCursorsToSide(window, DIR_LEFT, SHIFT_PRESSED);
} else if (Press(KEY_HOME)) {
Command_MoveCursorsToSide(&view, DIR_LEFT);
Command_MoveCursorsToSide(window, DIR_LEFT);
}
if (ShiftPress(KEY_END)) {
Command_MoveCursorsToSide(&view, DIR_RIGHT, SHIFT_PRESSED);
Command_MoveCursorsToSide(window, DIR_RIGHT, SHIFT_PRESSED);
} else if (Press(KEY_END)) {
Command_MoveCursorsToSide(&view, DIR_RIGHT);
Command_MoveCursorsToSide(window, DIR_RIGHT);
}
if (Press(KEY_ENTER)) {
@@ -310,14 +328,14 @@ void HandleKeybindings(View *_view) {
ProfileScope(mouse);
Vec2 _mouse = GetMousePosition();
bool mouse_in_view = CheckCollisionPointRec(_mouse, ToRectangle(view.rect));
bool mouse_in_scrollbar = CheckCollisionPointRec(_mouse, ToRectangle(view.scrollbar_rect));
bool mouse_in_view = CheckCollisionPointRec(_mouse, ToRectangle(window->document_rect));
bool mouse_in_scrollbar = CheckCollisionPointRec(_mouse, ToRectangle(window->scrollbar_rect));
Vec2I mouse = ToVec2I(_mouse);
if (!mouse_in_view) SetMouseCursor(MOUSE_CURSOR_DEFAULT);
if (!(mouse_in_scrollbar || view.mouse_selecting_scrollbar) && (mouse_in_view || view.mouse_selecting)) {
if (!view.mouse_selecting) {
if (!(mouse_in_scrollbar || window->mouse_selecting_scrollbar) && (mouse_in_view || window->mouse_selecting)) {
if (!window->mouse_selecting) {
if (mouse_in_view) {
SetMouseCursor(MOUSE_CURSOR_IBEAM);
} else {
@@ -325,7 +343,7 @@ void HandleKeybindings(View *_view) {
}
}
Vec2I mworld = mouse - view.rect.min + view.scroll;
Vec2I mworld = mouse - window->document_rect.min + view.scroll;
Vec2I pos = mworld / Vec2I{FontCharSpacing, FontLineSpacing};
XY xy = {(Int)(pos.x), (Int)(pos.y)};
Int p = XYToPosWithoutNL(buf, xy);
@@ -348,12 +366,12 @@ void HandleKeybindings(View *_view) {
MergeCarets(&view.carets);
} else if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
view.mouse_selecting = true;
window->mouse_selecting = true;
}
}
if (view.mouse_selecting) {
if (!IsMouseButtonDown(MOUSE_BUTTON_LEFT)) view.mouse_selecting = false;
if (window->mouse_selecting) {
if (!IsMouseButtonDown(MOUSE_BUTTON_LEFT)) window->mouse_selecting = false;
Caret &caret = *GetLast(view.carets);
if (view.selection_anchor.min > p) {
caret = MakeCaret(p, view.selection_anchor.max);
@@ -364,29 +382,29 @@ void HandleKeybindings(View *_view) {
}
MergeCarets(&view.carets, &view.selection_anchor);
}
} else if (!(mouse_in_view || view.mouse_selecting) && mouse_in_scrollbar || view.mouse_selecting_scrollbar) {
Scroller s = ComputeScrollerRect(view);
double size_y = (double)GetSize(view.scrollbar_rect).y;
double p = _mouse.y - view.scrollbar_rect.min.y;
} else if (!(mouse_in_view || window->mouse_selecting) && mouse_in_scrollbar || window->mouse_selecting_scrollbar) {
Scroller s = ComputeScrollerRect(*window);
double size_y = (double)GetSize(window->scrollbar_rect).y;
double p = _mouse.y - window->scrollbar_rect.min.y;
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
view.mouse_selecting_scrollbar = true;
window->mouse_selecting_scrollbar = true;
} else if (!IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
view.mouse_selecting_scrollbar = false;
window->mouse_selecting_scrollbar = false;
}
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)FontLineSpacing);
view.mouse_scroller_offset = -(double)GetSize(s.rect).y / 2.0 / size_y;
view.scroll.y = (Int)(p / size_y * (double)s.line_count * (double)FontLineSpacing);
window->mouse_scroller_offset = -(double)GetSize(s.rect).y / 2.0 / size_y;
} else {
view.mouse_scroller_offset = (s.rect.min.y - p) / size_y;
window->mouse_scroller_offset = (s.rect.min.y - p) / size_y;
}
}
if (view.mouse_selecting_scrollbar) {
if (window->mouse_selecting_scrollbar) {
double v = p / size_y;
v = v + (view.mouse_scroller_offset);
v = v + (window->mouse_scroller_offset);
view.scroll.y = (Int)(v * (double)s.line_count * (double)FontLineSpacing);
}
}
@@ -398,10 +416,10 @@ void HandleKeybindings(View *_view) {
Int front = GetFront(c);
XY xy = PosToXY(buf, front);
Rect2I visible = GetVisibleCells(view);
Rect2I visible = GetVisibleCells(*window);
Vec2I visible_cells = GetSize(visible);
Vec2I visible_size = visible_cells * Vec2I{FontCharSpacing, FontLineSpacing};
Vec2I rect_size = GetSize(view.rect);
Vec2I rect_size = GetSize(window->document_rect);
if (xy.line > visible.max.y - 2) {
Int set_view_at_line = xy.line - (visible_cells.y - 1);

View File

@@ -1,11 +1,12 @@
Vec2I GetCellSize(const View &view) {
Vec2I GetCellSize() {
Vec2I result = {(Int)FontCharSpacing, (Int)FontLineSpacing};
return result;
}
Rect2I GetVisibleCells(const View &view) {
Rect2I GetVisibleCells(Window &window) {
ProfileFunction();
Vec2I size = GetSize(view.rect);
View &view = *GetActiveView(window);
Vec2I size = GetSize(window.document_rect);
Int _cx = size.x / FontCharSpacing;
Int _cy = size.y / FontLineSpacing;
@@ -19,34 +20,36 @@ Rect2I GetVisibleCells(const View &view) {
return result;
}
Scroller ComputeScrollerRect(const View &view) {
Vec2I size = GetSize(view.scrollbar_rect);
Rect2I vis = GetVisibleCells(view);
Scroller ComputeScrollerRect(Window &window) {
View &view = *GetActiveView(window);
Vec2I size = GetSize(window.scrollbar_rect);
Rect2I vis = GetVisibleCells(window);
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)},
{(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(const View &view) {
void DrawVisibleText(Window &window) {
ProfileFunction();
Color tint = ColorText;
View &view = *GetActiveView(window);
Rect2I visible = GetVisibleCells(view);
Rect2I visible = GetVisibleCells(window);
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)FontCharSpacing, line_index * (Int)FontLineSpacing};
pos -= view.scroll;
pos += view.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) {
@@ -73,22 +76,24 @@ Vec2I XYToWorldPos(const View &view, XY xy) {
}
Rect2I XYToRect(const View &view, XY xy) {
Rect2I result = Rect2IFromSize(XYToWorldPos(view, xy), GetCellSize(view));
Rect2I result = Rect2IFromSize(XYToWorldPos(view, xy), GetCellSize());
return result;
}
void DrawCaret(const View &view, XY xy, float size, Color color) {
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 += view.rect.min;
rect += window.document_rect.min;
DrawRectangleRec(ToRectangle(rect), color);
}
void DrawLineHighlight(const View &view, XY fxy, Color color) {
Vec2I w = XYToWorldPos(view, XYLine(fxy.line));
void DrawLineHighlight(Window &window, XY fxy, Color color) {
View &view = *GetActiveView(window);
Vec2I w = XYToWorldPos(view, XYLine(fxy.line));
w -= view.scroll;
w += view.rect.min;
w += window.document_rect.min;
Rect2I rect = {
{ 0, w.y},
{GetRenderWidth(), w.y + FontLineSpacing}
@@ -96,8 +101,9 @@ void DrawLineHighlight(const View &view, XY fxy, Color color) {
DrawRectangleRec(ToRectangle(rect), color);
}
void DrawSelection(const View &view, Caret &it) {
void DrawSelection(Window &window, Caret &it) {
ProfileFunction();
View &view = *GetActiveView(window);
Buffer &buf = *view.buffer;
Int front = GetFront(it);
Int back = GetBack(it);
@@ -107,7 +113,7 @@ void DrawSelection(const View &view, Caret &it) {
XY max = PosToXY(buf, it.range.max);
Color color = ColorSelection;
Rect2I vlines = GetVisibleCells(view);
Rect2I vlines = GetVisibleCells(window);
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);
@@ -121,7 +127,7 @@ void DrawSelection(const View &view, Caret &it) {
if (a || b || c || d) {
Vec2I pos = {col * FontCharSpacing, line * FontLineSpacing};
pos -= view.scroll;
pos += view.rect.min;
pos += window.document_rect.min;
Rectangle rectangle = {(float)pos.x, (float)pos.y, (float)FontCharSpacing, (float)FontLineSpacing};
DrawRectangleRec(rectangle, color);
@@ -139,40 +145,41 @@ void DrawSelection(const View &view, Caret &it) {
}
}
void DrawWindow(View &view) {
Buffer &buf = *view.buffer;
void DrawWindow(Window &window) {
View &view = *GetActiveView(window);
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);
BeginScissorMode((int)window.document_rect.min.x, (int)window.document_rect.min.y, (int)window.document_rect.max.x - (int)window.document_rect.min.x, (int)window.document_rect.max.y - (int)window.document_rect.min.y);
For(view.carets) {
Int front = GetFront(it);
XY fxy = PosToXY(buf, front);
if (GetSize(it.range)) {
DrawSelection(view, it);
DrawSelection(window, it);
} else {
DrawLineHighlight(view, fxy, ColorLineHighlight);
DrawLineHighlight(window, fxy, ColorLineHighlight);
}
}
DrawVisibleText(view);
DrawVisibleText(window);
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);
DrawCaret(window, fxy, 0.3f, main_caret ? ColorMainCaret : ColorSubCaret);
}
EndScissorMode();
// Draw scrollbar
{
Vec2 mouse = GetMousePosition();
bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, ToRectangle(view.scrollbar_rect));
bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, ToRectangle(window.scrollbar_rect));
DrawRectangleRec(ToRectangle(view.scrollbar_rect), ColorScrollbarBackground);
Scroller scroller = ComputeScrollerRect(view);
DrawRectangleRec(ToRectangle(window.scrollbar_rect), ColorScrollbarBackground);
Scroller scroller = ComputeScrollerRect(window);
Rect2 rect = Shrink(scroller.rect, 2);
Color color = ColorScrollbarScroller;
if (!view.mouse_selecting && (view.mouse_selecting_scrollbar || mouse_in_scrollbar)) {
if (!window.mouse_selecting && (window.mouse_selecting_scrollbar || mouse_in_scrollbar)) {
color = ColorScrollbarScrollerSelected;
}
DrawRectangleRec(ToRectangle(rect), color);
@@ -180,11 +187,11 @@ void DrawWindow(View &view) {
// Draw line numbers
{
Rect2I r = view.line_numbers_rect;
Rect2I r = window.line_numbers_rect;
DrawRectangleRec(ToRectangle(r), ColorBackground);
BeginScissorMode((int)r.min.x, (int)r.min.y, (int)r.max.x - (int)r.min.x, (int)r.max.y - (int)r.min.y);
Rect2I vlines = GetVisibleCells(view);
Rect2I vlines = GetVisibleCells(window);
for (Int line = vlines.min.y; line <= vlines.max.y; line += 1) {
Scratch scratch;
Vec2I pos = {0, line * FontLineSpacing};
@@ -205,7 +212,7 @@ void DrawWindow(View &view) {
// Draw info bar
{
Rect2I r = view.infobar_rect;
Rect2I r = window.infobar_rect;
DrawRectangleRec(ToRectangle(r), ColorScrollbarBackground);
{
Vec2 p = ToVec2(r.min);
@@ -217,4 +224,8 @@ void DrawWindow(View &view) {
DrawString(MenuFont, string, p, MenuFontSize, 1, ColorText);
}
}
if (!IsActive(&window)) {
DrawRectangleRec(ToRectangle(window.total_rect), {0, 0, 0, 30});
}
}

View File

@@ -0,0 +1,21 @@
Array<Window> Windows = {};
Int ActiveWindow = 0;
Window *GetActiveWindow() {
return &Windows[ActiveWindow];
}
View *GetActiveView(Window &window) {
return &window.views[window.active_view];
}
bool IsActive(Window *window) {
Window *active_window = GetActiveWindow();
bool result = window == active_window;
return result;
}
bool IsActive(Window *window, View *view) {
bool result = view == GetActiveView(*window);
return result;
}