diff --git a/src/text_editor/buffer.cpp b/src/text_editor/buffer.cpp index e6fb356..dbcce7a 100644 --- a/src/text_editor/buffer.cpp +++ b/src/text_editor/buffer.cpp @@ -249,8 +249,11 @@ void ApplyEdits(Buffer *buffer, Array edits) { #endif // We need to sort from lowest to higest based on range.min - Array edits_copy = edits.copy(scratch); - MergeSort(edits.len, edits_copy.data, edits.data); + { + Array edits_copy = edits.copy(scratch); + MergeSort(edits.len, edits_copy.data, edits.data); + edits = edits_copy; + } // Try resizing the buffers int64_t len_offset = size_to_insert - size_to_delete; diff --git a/src/text_editor/main.cpp b/src/text_editor/main.cpp index 709d771..212f252 100644 --- a/src/text_editor/main.cpp +++ b/src/text_editor/main.cpp @@ -107,6 +107,11 @@ int64_t MoveUp(Buffer &buffer, int64_t pos) { return new_pos; } +bool AreEqual(float a, float b, float epsilon = 0.001f) { + bool result = (a - epsilon < b && a + epsilon > b); + return result; +} + void BeforeEdit(Window *window) { // Merge cursors that overlap, this needs to be handled before any edits to // make sure overlapping edits won't happen. @@ -157,8 +162,34 @@ void AfterEdit(Window *window, Array edits) { window->layout = CalculateLayout(&window->layout_arena, window->buffer, window->font, window->font_size, window->font_spacing); } +Arena PermArena; Arena FrameArena; -int main() { +struct DebugLine { + bool persist; + String string; +}; +Array DebugLines = {}; + +void Dbg(const char *str, ...) { + STRING_FORMAT(FrameArena, str, result); + DebugLines.add({false, result}); +} +void DbgPersist(const char *str, ...) { + STRING_FORMAT(PermArena, str, result); + DebugLines.add({true, result}); +} + +void Dbg_Draw() { + int y = 0; + IterRemove(DebugLines) { + IterRemovePrepare(DebugLines); + if (it.persist == false) remove_item = true; + DrawText(it.string.data, 0, y, 20, BLACK); + y += 20; + } +} + +int main() { InitScratch(); RunBufferTests(); @@ -166,6 +197,7 @@ int main() { SetTargetFPS(60); InitArena(&FrameArena); + InitArena(&PermArena); float font_size = 64; float font_spacing = 1; Font font = LoadFontEx("C:/Windows/Fonts/times.ttf", (int)font_size, NULL, 250); @@ -458,6 +490,7 @@ int main() { Array visible_columns = CalculateVisibleColumns(&FrameArena, window); // Mouse selection // @todo: multicursor + // @todo: selecting while not hovering over glyph shapes { SetMouseCursor(MOUSE_CURSOR_DEFAULT); Vec2 mouse = GetMousePosition(); @@ -485,7 +518,7 @@ int main() { window.mouse_selecting = false; } Cursor *cursor = window.cursors.last(); - cursor[0] = ChangeBack(*cursor, rowcol.b->pos); + cursor[0] = ChangeFront(*cursor, rowcol.b->pos); } } } @@ -493,13 +526,38 @@ 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; + + Vec2 rect_size = GetSize(window.rect); + float cut_off_y = visible_size_y - rect_size.y; + + int64_t front = GetFront(window.cursors[0]); + Line line = FindLine(window.buffer, front); + // Tuple 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; + } + + 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; Cursor cursor = window.cursors[0]; - int64_t front = GetBack(cursor); + int64_t front = GetFront(cursor); Line line = FindLine(window.buffer, front); // Scroll Y @@ -528,14 +586,14 @@ int main() { float diff = x_cursor_position_in_window_buffer_world_units - window_buffer_world_left_edge; window.scroll.x += diff; } +#endif } - Range visible_line_range = CalculateVisibleLineRange(window); - // Draw the glyphs Vec2 window_rect_size = GetSize(window.rect); BeginScissorMode((int)window.rect.min.x, (int)window.rect.min.y, (int)window_rect_size.x, (int)window_rect_size.y); + Range visible_line_range = CalculateVisibleLineRange(window); for (int64_t line = visible_line_range.min; line < visible_line_range.max; line += 1) { if (line < 0 || line >= window.layout.rows.len) continue; LayoutRow &row = window.layout.rows[line]; @@ -587,6 +645,7 @@ int main() { EndScissorMode(); } + Dbg_Draw(); EndDrawing(); } } \ No newline at end of file