Scrolling up and scrolling down

This commit is contained in:
Krzosa Karol
2024-07-20 09:37:02 +02:00
parent 3868afce93
commit ba5930b585
3 changed files with 42 additions and 3 deletions

View File

@@ -23,6 +23,18 @@ Vec2 GetSize(Rect2 r) {
return result;
}
Vec2I GetSize(Rect2I r) {
Vec2I result = {(r.max.x - r.min.x), (r.max.y - r.min.y)};
return result;
}
Vec2 GetSizeF(Rect2I r) {
Vec2 result = {(float)(r.max.x - r.min.x), (float)(r.max.y - r.min.y)};
return result;
}
Vec2 ToVec2(Vec2I v) { return {(float)v.x, (float)v.y}; }
// 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

@@ -123,8 +123,9 @@ Int MoveCursor(Buffer &buffer, Int pos, int direction, bool ctrl_pressed) {
}
void HandleKeybindings(View *_view) {
View &view = *_view;
Buffer &buf = *view.buffer;
View &view = *_view;
Buffer &buf = *view.buffer;
Cursor main_cursor_on_begin_frame = view.cursors[0];
if (IsKeyDown(KEY_F1)) {
view.scroll.x -= GetMouseWheelMove() * 48;
@@ -147,6 +148,33 @@ void HandleKeybindings(View *_view) {
}
}
// @todo: this should happen after we calculate rect for the view
// Usually in other text editors apart from view there is also a 'window'
// abstraction which is exactly the thing which shows the view, will need
// to think about this later
if (!AreEqual(main_cursor_on_begin_frame, view.cursors[0])) {
Cursor c = view.cursors[0];
Int front = GetFront(c);
Int line = PosToLine(buf, front);
Rect2I GetVisibleCells(const View &view);
Rect2I visible = GetVisibleCells(view);
Vec2I visible_cells = GetSize(visible);
Vec2 visible_size = ToVec2(visible_cells) * Vec2{view.char_spacing, view.line_spacing};
Vec2 rect_size = GetSize(view.rect);
if (line > visible.max.y - 3) {
Int set_view_at_line = line - (visible_cells.y - 1);
float cut_off_y = Max(0.f, visible_size.y - rect_size.y);
view.scroll.y = ((float)set_view_at_line * view.line_spacing) + cut_off_y;
}
if (line < visible.min.y + 1) {
view.scroll.y = line * view.line_spacing;
}
}
// Clip scroll
{
Int last_line = LastLine(view.buffer[0]);

View File

@@ -1,4 +1,3 @@
Rect2I GetVisibleCells(const View &view) {
Vec2 size = GetSize(view.rect);
float _cx = size.x / view.char_spacing;