Adding scrollbars and titlebars, laying it out

This commit is contained in:
Krzosa Karol
2024-06-23 07:22:03 +02:00
parent 48eca55a4c
commit 6347b56751

View File

@@ -11,18 +11,30 @@ Arena FrameArena;
// Render units - positions ready to draw, y // Render units - positions ready to draw, y
// World units - positions offset by screen movement // World units - positions offset by screen movement
// Window units - positions inside the window (starts in left top of window) // Window units - positions inside the window (starts in left top of window)
// WindowWorld units - positions offset by a position inside the buffer // WindowBuffer units
// WindowBufferWorld units
const uint64_t WindowFlag_DrawTitleBar = 1 << 1;
const uint64_t WindowFlag_DrawLineNumbers = 1 << 2;
const uint64_t WindowFlag_DrawRightScrollBar = 1 << 3;
const uint64_t WindowFlag_DrawBottomScrollBar = 1 << 4;
struct Window { struct Window {
uint64_t flags;
Rect2 rect_in_world_units; Rect2 rect_in_world_units;
Vec2 window_world_to_window_units; Vec2 scroll; // window_world_to_window_units
Array<Range> cursors; Array<Range> cursors;
Buffer buffer; Buffer buffer;
}; };
Vec2 WindowWorldToWindowUnits(Vec2 value, const Window &window) { Vec2 WindowBufferWorldToWindowBufferUnits(Vec2 value, const Window &window) {
Vec2 result = Vector2Subtract(value, window.window_world_to_window_units); Vec2 result = Vector2Subtract(value, window.scroll);
return result;
}
Vec2 WindowBufferToWindowUnits(Vec2 value, Vec2 window_buffer_to_window_units) {
Vec2 result = Vector2Add(value, window_buffer_to_window_units);
return result; return result;
} }
@@ -108,21 +120,24 @@ int main() {
if (0) font = LoadFontEx("C:/Windows/Fonts/consola.ttf", (int)font_size, NULL, 250); if (0) font = LoadFontEx("C:/Windows/Fonts/consola.ttf", (int)font_size, NULL, 250);
Array<Window> windows = {}; Array<Window> windows = {};
windows.add({GetScreenRectRenderUnits()});
{ {
Window *window = &windows[0]; Window window = {};
Buffer *buffer = &window->buffer; window.flags = WindowFlag_DrawTitleBar | WindowFlag_DrawLineNumbers;
InitBuffer(buffer); window.rect_in_world_units = GetScreenRectRenderUnits();
window.rect_in_world_units.max.x *= 2;
window.rect_in_world_units.max.y *= 2;
InitBuffer(&window.buffer);
if (1) { if (1) {
for (int i = 0; i < 5; i += 1) { for (int i = 0; i < 5; i += 1) {
Array<Edit> edits = {FrameArena}; Array<Edit> edits = {FrameArena};
AddEdit(&edits, GetEnd(*buffer), Format(FrameArena, "line number: %d\n", i)); AddEdit(&edits, GetEnd(window.buffer), Format(FrameArena, "line number: %d\n", i));
ApplyEdits(buffer, edits); ApplyEdits(&window.buffer, edits);
} }
} }
window->cursors.add({}); window.cursors.add({});
window->cursors.add(GetLine(*buffer, 1).range); window.cursors.add(GetLine(window.buffer, 1).range);
windows.add(window);
} }
Vec2 camera_offset_world_to_render_units = {}; Vec2 camera_offset_world_to_render_units = {};
@@ -135,8 +150,8 @@ int main() {
} }
float mouse_wheel = GetMouseWheelMove() * 48; float mouse_wheel = GetMouseWheelMove() * 48;
focused_window->window_world_to_window_units.y -= mouse_wheel; focused_window->scroll.y -= mouse_wheel;
focused_window->window_world_to_window_units.y = ClampBottom(focused_window->window_world_to_window_units.y, 0.f); focused_window->scroll.y = ClampBottom(focused_window->scroll.y, 0.f);
if (IsKeyPressed(KEY_LEFT) || IsKeyPressedRepeat(KEY_LEFT)) { if (IsKeyPressed(KEY_LEFT) || IsKeyPressedRepeat(KEY_LEFT)) {
For(focused_window->cursors) { For(focused_window->cursors) {
@@ -208,24 +223,33 @@ int main() {
Rectangle rectangle_in_render_units = ToRectangle(window_rect_in_render_units); Rectangle rectangle_in_render_units = ToRectangle(window_rect_in_render_units);
DrawRectangleRec(rectangle_in_render_units, WHITE); DrawRectangleRec(rectangle_in_render_units, WHITE);
Rect2 screen_rect_in_render_units = GetScreenRectRenderUnits(); Rect2 window_text_rect_in_render_units = window_rect_in_render_units;
Rect2 window_rect_in_render_units_clamped_to_screen = window_rect_in_render_units;
window_rect_in_render_units_clamped_to_screen.min.x = Clamp(window_rect_in_render_units_clamped_to_screen.min.x, screen_rect_in_render_units.min.x, screen_rect_in_render_units.max.x);
window_rect_in_render_units_clamped_to_screen.max.x = Clamp(window_rect_in_render_units_clamped_to_screen.max.x, screen_rect_in_render_units.min.x, screen_rect_in_render_units.max.x);
window_rect_in_render_units_clamped_to_screen.min.y = Clamp(window_rect_in_render_units_clamped_to_screen.min.y, screen_rect_in_render_units.min.y, screen_rect_in_render_units.max.y);
window_rect_in_render_units_clamped_to_screen.max.y = Clamp(window_rect_in_render_units_clamped_to_screen.max.y, screen_rect_in_render_units.min.y, screen_rect_in_render_units.max.y);
if (0) {
window_rect_in_render_units_clamped_to_screen = Shrink(window_rect_in_render_units_clamped_to_screen, 10);
DrawRectangleRec(ToRectangle(window_rect_in_render_units_clamped_to_screen), {255, 0, 0, 50});
}
// Figure out which lines to draw Rect2 window_text_rect_in_render_units_clamped_to_screen = window_text_rect_in_render_units;
Vec2 s = GetSize(window_rect_in_render_units); Rect2 screen_rect_in_render_units = GetScreenRectRenderUnits();
float line_offset = font_size; window_text_rect_in_render_units_clamped_to_screen.min.x = Clamp(window_text_rect_in_render_units_clamped_to_screen.min.x, screen_rect_in_render_units.min.x, screen_rect_in_render_units.max.x);
float _line_min_y = (window.window_world_to_window_units.y) / line_offset; window_text_rect_in_render_units_clamped_to_screen.max.x = Clamp(window_text_rect_in_render_units_clamped_to_screen.max.x, screen_rect_in_render_units.min.x, screen_rect_in_render_units.max.x);
float _line_max_y = (s.y + window.window_world_to_window_units.y) / line_offset; window_text_rect_in_render_units_clamped_to_screen.min.y = Clamp(window_text_rect_in_render_units_clamped_to_screen.min.y, screen_rect_in_render_units.min.y, screen_rect_in_render_units.max.y);
int64_t line_min_y = (int64_t)floorf(_line_min_y); window_text_rect_in_render_units_clamped_to_screen.max.y = Clamp(window_text_rect_in_render_units_clamped_to_screen.max.y, screen_rect_in_render_units.min.y, screen_rect_in_render_units.max.y);
int64_t line_max_y = (int64_t)ceilf(_line_max_y);
float title_bar_size = 20;
float line_number_bar_size = 40;
float scroll_bar_size = 30;
Vec2 window_buffer_to_window_units = {line_number_bar_size, title_bar_size};
Rect2 title_bar_in_render_units = CutTop(&window_text_rect_in_render_units_clamped_to_screen, title_bar_size);
Rect2 line_number_bar_in_render_units = CutLeft(&window_text_rect_in_render_units_clamped_to_screen, line_number_bar_size);
Rect2 bottom_scroll_bar_in_render_units = CutBottom(&window_text_rect_in_render_units_clamped_to_screen, scroll_bar_size);
Rect2 right_scroll_bar_in_render_units = CutRight(&window_text_rect_in_render_units_clamped_to_screen, scroll_bar_size);
DrawRectangleRec(ToRectangle(title_bar_in_render_units), GRAY);
DrawRectangleRec(ToRectangle(line_number_bar_in_render_units), GRAY);
DrawRectangleRec(ToRectangle(bottom_scroll_bar_in_render_units), GRAY);
DrawRectangleRec(ToRectangle(right_scroll_bar_in_render_units), GRAY);
if (0) {
window_text_rect_in_render_units_clamped_to_screen = Shrink(window_text_rect_in_render_units_clamped_to_screen, 10);
DrawRectangleRec(ToRectangle(window_text_rect_in_render_units_clamped_to_screen), {255, 0, 0, 50});
}
struct Cell { struct Cell {
Rect2 rect; Rect2 rect;
@@ -241,13 +265,23 @@ int main() {
// Compute visible rows and cells // Compute visible rows and cells
Array<CellRow> rows = {FrameArena}; Array<CellRow> rows = {FrameArena};
{
// Figure out which lines to draw
Vec2 s = GetSize(window_rect_in_render_units);
float line_offset = font_size;
float _line_min_y = (window.scroll.y) / line_offset;
float _line_max_y = (s.y + window.scroll.y) / line_offset;
int64_t line_min_y = (int64_t)floorf(_line_min_y);
int64_t line_max_y = (int64_t)ceilf(_line_max_y);
for (int64_t line = line_min_y; line < line_max_y; line += 1) { for (int64_t line = line_min_y; line < line_max_y; line += 1) {
if (line < 0) break; if (line < 0) break;
if (line >= window.buffer.lines.len) break; if (line >= window.buffer.lines.len) break;
Range line_range = window.buffer.lines[line]; Range line_range = window.buffer.lines[line];
Vec2 text_position_in_world_window_units = {0, line_offset * (float)line}; Vec2 text_position_in_window_buffer_world_units = {0, line_offset * (float)line};
Vec2 text_position_in_window_units = WindowWorldToWindowUnits(text_position_in_world_window_units, window); Vec2 text_position_in_window_buffer_units = WindowBufferWorldToWindowBufferUnits(text_position_in_window_buffer_world_units, window);
Vec2 text_position_in_window_units = WindowBufferToWindowUnits(text_position_in_window_buffer_units, window_buffer_to_window_units);
Vec2 text_position_in_world_units = WindowToWorldUnits(text_position_in_window_units, window); Vec2 text_position_in_world_units = WindowToWorldUnits(text_position_in_window_units, window);
Vec2 text_position_in_render_units = WorldToRenderUnits(text_position_in_world_units, camera_offset_world_to_render_units); Vec2 text_position_in_render_units = WorldToRenderUnits(text_position_in_world_units, camera_offset_world_to_render_units);
@@ -286,7 +320,7 @@ int main() {
Rectangle cell_rectangle = ToRectangle(cell_rect); Rectangle cell_rectangle = ToRectangle(cell_rect);
// Clip everything that is outside the window and screen // Clip everything that is outside the window and screen
if (CheckCollisionRecs(cell_rectangle, ToRectangle(window_rect_in_render_units_clamped_to_screen))) { if (CheckCollisionRecs(cell_rectangle, ToRectangle(window_text_rect_in_render_units_clamped_to_screen))) {
row.cells.add({cell_rect, codepoint, iter.pos}); row.cells.add({cell_rect, codepoint, iter.pos});
row.rect.max = cell_rect.max; row.rect.max = cell_rect.max;
} }
@@ -297,6 +331,7 @@ int main() {
if (row.cells.len) rows.add(row); if (row.cells.len) rows.add(row);
} }
}
// Draw debug markers // Draw debug markers
if (0) { if (0) {
@@ -309,8 +344,8 @@ int main() {
} }
// Draw the glyphs // Draw the glyphs
Vec2 window_rect_in_render_units_size = GetSize(window_rect_in_render_units); Vec2 window_text_rect_in_render_units_clamped_to_screen_size = GetSize(window_text_rect_in_render_units_clamped_to_screen);
BeginScissorMode((int)window_rect_in_render_units.min.x, (int)window_rect_in_render_units.min.y, (int)window_rect_in_render_units_size.x, (int)window_rect_in_render_units_size.y); BeginScissorMode((int)window_text_rect_in_render_units_clamped_to_screen.min.x, (int)window_text_rect_in_render_units_clamped_to_screen.min.y, (int)window_text_rect_in_render_units_clamped_to_screen_size.x, (int)window_text_rect_in_render_units_clamped_to_screen_size.y);
ForItem(row, rows) { ForItem(row, rows) {
For(row.cells) { For(row.cells) {
if (it.codepoint == '\n') { if (it.codepoint == '\n') {