Z ordering, prep for command window

This commit is contained in:
Krzosa Karol
2024-07-23 13:34:12 +02:00
parent 0f34416726
commit 24ad9464b5
7 changed files with 329 additions and 276 deletions

View File

@@ -20,6 +20,7 @@ Vec2 GetSizeF(Rect2I r) {
Vec2 ToVec2(Vec2I v) { return {(float)v.x, (float)v.y}; } Vec2 ToVec2(Vec2I v) { return {(float)v.x, (float)v.y}; }
Vec2I ToVec2I(Vec2 v) { return {(Int)v.x, (Int)v.y}; } Vec2I ToVec2I(Vec2 v) { return {(Int)v.x, (Int)v.y}; }
Rect2I ToRect2I(Rect2 r) { return {ToVec2I(r.min), ToVec2I(r.max)}; }
// clang-format off // clang-format off
Rect2 operator-(Rect2 r, Rect2 value) { return { r.min.x - value.min.x, r.min.y - value.min.y, r.max.x - value.max.x, r.max.y - value.max.y }; } Rect2 operator-(Rect2 r, Rect2 value) { return { r.min.x - value.min.x, r.min.y - value.min.y, r.max.x - value.max.x, r.max.y - value.max.y }; }

View File

@@ -13,6 +13,11 @@ Rect2I GetScreenRect() {
return result; return result;
} }
Rect2 GetScreenRectF() {
Rect2 result = {0, 0, (float)GetRenderWidth(), (float)GetRenderHeight()};
return result;
}
Int GetCharSpacing(Font font, Int font_size, Int spacing) { Int GetCharSpacing(Font font, Int font_size, Int spacing) {
int index = GetGlyphIndex(font, '_'); int index = GetGlyphIndex(font, '_');
Int textOffsetX = 0; Int textOffsetX = 0;

View File

@@ -96,9 +96,22 @@ int main(void) {
LoadLine(b); LoadLine(b);
AddView(w->id, v->id); AddView(w->id, v->id);
} }
{
Window *w = CreateWindow();
w->draw_scrollbar = false;
w->draw_line_numbers = false;
w->draw_infobar = false;
Buffer *b = CreateBuffer(sys_allocator);
View *v = CreateView(b->id);
LoadLine(b);
AddView(w->id, v->id);
}
Int LastFrameIDWhenSwitchedActiveWindow = 0;
while (!WindowShouldClose()) { while (!WindowShouldClose()) {
ProfileScope(game_loop); ProfileScope(game_loop);
FrameID += 1;
Rect2I screen_rect = GetScreenRect(); Rect2I screen_rect = GetScreenRect();
float line_numbers_size = MeasureTextEx(MainFont, "12345", (float)FontSize, (float)FontSpacing).x; float line_numbers_size = MeasureTextEx(MainFont, "12345", (float)FontSize, (float)FontSpacing).x;
@@ -126,13 +139,56 @@ int main(void) {
if (Windows[i].draw_infobar) Windows[i].infobar_rect = CutBottom(&Windows[i].document_rect, (Int)MenuFontSize); if (Windows[i].draw_infobar) Windows[i].infobar_rect = CutBottom(&Windows[i].document_rect, (Int)MenuFontSize);
if (Windows[i].draw_line_numbers) Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size); if (Windows[i].draw_line_numbers) Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size);
} }
{
int i = 4;
Rect2 screen_rect = GetScreenRectF();
Vec2 size = GetSize(screen_rect);
CutTop(&screen_rect, size.y * 0.05f);
CutBottom(&screen_rect, size.y * 0.8f);
CutLeft(&screen_rect, size.x * 0.2f);
CutRight(&screen_rect, size.x * 0.2f);
Windows[i].z = 1;
Windows[i].total_rect = ToRect2I(screen_rect);
Windows[i].document_rect = Windows[i].total_rect;
}
BeginDrawing(); BeginDrawing();
ClearBackground(ColorBackground); ClearBackground(ColorBackground);
For(Windows) {
if (!it.visible) continue; Scratch scratch;
HandleKeybindings(&it); Array<Int> order = GetWindowZOrder(scratch);
DrawWindow(it);
For(order) {
Window *window = &Windows[it];
if (window->visible == false) continue;
View *view = GetActiveView(window);
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 (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
if (LastFrameIDWhenSwitchedActiveWindow != FrameID) {
ActiveWindow = window->id;
LastFrameIDWhenSwitchedActiveWindow = FrameID;
}
}
}
}
For(IterateInReverse(&order)) {
Window &window = Windows[it];
if (window.visible == false) continue;
HandleKeybindings(&window);
DrawWindow(window);
} }
SetMouseCursor(); SetMouseCursor();
EndDrawing(); EndDrawing();

View File

@@ -55,6 +55,7 @@ struct Window {
Rect2I document_rect; Rect2I document_rect;
double mouse_scroller_offset; double mouse_scroller_offset;
int z;
struct { struct {
bool mouse_selecting_scrollbar : 1; bool mouse_selecting_scrollbar : 1;
@@ -99,6 +100,8 @@ Int FontSpacing;
Int FontLineSpacing; Int FontLineSpacing;
Int FontCharSpacing; Int FontCharSpacing;
Int FrameID;
inline Window *GetWindow(WindowID id) { inline Window *GetWindow(WindowID id) {
For(Windows) if (it.id.id == id.id) return &it; For(Windows) if (it.id.id == id.id) return &it;
return &Windows[0]; return &Windows[0];

View File

@@ -118,29 +118,7 @@ void HandleKeybindings(Window *window) {
Buffer *buffer = GetBuffer(view.buffer_id); Buffer *buffer = GetBuffer(view.buffer_id);
Caret main_caret_on_begin_frame = view.carets[0]; Caret main_caret_on_begin_frame = view.carets[0];
{ if (IsActive(window)) {
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 = window->id;
}
}
}
}
if (!IsActive(window)) {
return;
}
if (CtrlPress(KEY_F2)) { if (CtrlPress(KEY_F2)) {
LoadBigLine(buffer); LoadBigLine(buffer);
@@ -433,6 +411,7 @@ void HandleKeybindings(Window *window) {
view.scroll.x = xy.col * FontCharSpacing; view.scroll.x = xy.col * FontCharSpacing;
} }
} }
}
// Clip scroll // Clip scroll
{ {

View File

@@ -150,6 +150,7 @@ void DrawSelection(Window &window, Caret &it) {
void DrawWindow(Window &window) { void DrawWindow(Window &window) {
View &view = *GetActiveView(&window); View &view = *GetActiveView(&window);
Buffer *buffer = GetBuffer(view.buffer_id); Buffer *buffer = GetBuffer(view.buffer_id);
DrawRectangleRec(ToRectangle(window.total_rect), ColorBackground);
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); 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) { For(view.carets) {

View File

@@ -39,3 +39,11 @@ void SetMouseCursor() {
SetMouseCursor(MOUSE_CURSOR_POINTING_HAND); SetMouseCursor(MOUSE_CURSOR_POINTING_HAND);
} }
} }
Array<Int> GetWindowZOrder(Allocator allocator) {
Array<Int> order = {allocator};
For(Windows) if (it.z == 2) Add(&order, GetIndex(Windows, it));
For(Windows) if (it.z == 1) Add(&order, GetIndex(Windows, it));
For(Windows) if (it.z == 0) Add(&order, GetIndex(Windows, it));
return order;
}