Cursor scrolling working

This commit is contained in:
Krzosa Karol
2024-06-28 13:43:58 +02:00
parent 4dfa9b432e
commit 63adca1fac

View File

@@ -95,6 +95,7 @@ int64_t MoveLeft(Buffer &buffer, int64_t pos) {
return pos;
}
// @todo: when at the end of line, it resets to first position on next line
int64_t MoveDown(Buffer &buffer, int64_t pos) {
LineAndColumn info = FindLineAndColumn(buffer, pos);
int64_t new_pos = FindPos(buffer, info.line.number + 1, info.column);
@@ -219,7 +220,7 @@ int main() {
if (1) {
for (int i = 0; i < 50; i += 1) {
Array<Edit> edits = {FrameArena};
AddEdit(&edits, GetEnd(window.buffer), Format(FrameArena, "line number: %d\n", i));
AddEdit(&edits, GetEnd(window.buffer), Format(FrameArena, "line number line number line number line number: %d\n", i));
ApplyEdits(&window.buffer, edits);
}
}
@@ -524,9 +525,7 @@ int main() {
}
// Update the scroll based on first cursor
// @todo: needs a rewrite, make sure we also handle scrolling for mouse
if (!AreEqual(window.main_cursor_begin_frame, window.cursors[0])) {
#if 1
Range visible_line_range = CalculateVisibleLineRange(window);
int64_t visible_lines = GetRangeSize(visible_line_range);
float visible_size_y = font_size * (float)visible_lines;
@@ -536,57 +535,34 @@ int main() {
int64_t front = GetFront(window.cursors[0]);
Line line = FindLine(window.buffer, front);
// Tuple<LayoutRow, LayoutColumn> rowcol = GetRowCol(window, front);
// DbgPersist("line num = %d visible_line_range.max = %d", (int)line.number, (int)visible_line_range.max);
// Bottom
if (line.number > visible_line_range.max - 2) {
int64_t set_view_at_line = line.number - (visible_lines - 1);
window.scroll.y = (set_view_at_line * font_size) + cut_off_y;
}
// Top
if (line.number < visible_line_range.min + 1) {
int64_t set_view_at_line = line.number;
window.scroll.y = (set_view_at_line * font_size);
}
#else
Range visible_line_range = CalculateVisibleLineRange(window);
Vec2 rect_size = GetSize(window.rect);
float visible_cells_in_render_units = font_size * (float)GetRangeSize(visible_line_range);
float cut_off_in_render_units = visible_cells_in_render_units - rect_size.y;
// Right
Tuple<LayoutRow *, LayoutColumn *> rowcol = GetRowCol(window, front);
Cursor cursor = window.cursors[0];
int64_t front = GetFront(cursor);
Line line = FindLine(window.buffer, front);
// Scroll Y
if (line.number < (visible_line_range.min)) {
window.scroll.y = line.number * font_size;
} else if (line.number >= (visible_line_range.max)) {
int64_t diff = line.number - visible_line_range.max;
window.scroll.y = (visible_line_range.min + diff) * font_size + cut_off_in_render_units;
float x = rowcol.b->rect.max.x;
float right_edge = rect_size.x + window.scroll.x;
if (x >= right_edge) {
window.scroll.x = x - rect_size.x;
}
// Scroll X
Range x_distance = {line.range.min, front};
String x_distance_string = GetString(window.buffer, x_distance);
Vec2 size = MeasureString(font, x_distance_string, font_size, font_spacing);
if (x_distance_string.len <= 0) size.x = 0;
float x_cursor_position_in_window_buffer_world_units = size.x;
GlyphInfo info = GetGlyphInfo(font, ' ');
float right_scroll_zone = (float)(info.image.width + info.advanceX) * 3;
float window_buffer_world_right_edge = (rect_size.x + window.scroll.x) - right_scroll_zone;
float window_buffer_world_left_edge = window.scroll.x;
if (x_cursor_position_in_window_buffer_world_units >= window_buffer_world_right_edge) {
float diff = x_cursor_position_in_window_buffer_world_units - window_buffer_world_right_edge;
window.scroll.x += diff;
} else if (x_cursor_position_in_window_buffer_world_units <= window_buffer_world_left_edge) {
float diff = x_cursor_position_in_window_buffer_world_units - window_buffer_world_left_edge;
window.scroll.x += diff;
// Left
x = rowcol.b->rect.min.x;
float left_edge = 0 + window.scroll.x;
if (x <= left_edge) {
window.scroll.x = x;
}
#endif
}
// Draw the glyphs