Rename cursor to caret because cursor can be confused with mouse pointer

This commit is contained in:
Krzosa Karol
2024-07-20 09:48:40 +02:00
parent 2ba9d991bf
commit 941652b4bc
7 changed files with 60 additions and 59 deletions

View File

@@ -13,7 +13,7 @@ struct Range {
Int max; Int max;
}; };
struct Cursor { struct Caret {
union { union {
Range range; Range range;
Int pos[2]; Int pos[2];

View File

@@ -48,19 +48,19 @@ Range GetRange(Buffer &buffer) {
return result; return result;
} }
Int GetFront(Cursor cursor) { Int GetFront(Caret caret) {
Int result = cursor.pos[cursor.ifront]; Int result = caret.pos[caret.ifront];
return result; return result;
} }
Int GetBack(Cursor cursor) { Int GetBack(Caret caret) {
Int index = (cursor.ifront + 1) % 2; Int index = (caret.ifront + 1) % 2;
Int result = cursor.pos[index]; Int result = caret.pos[index];
return result; return result;
} }
Cursor MakeCursor(Int front, Int back) { Caret MakeCaret(Int front, Int back) {
Cursor result = {}; Caret result = {};
if (front >= back) { if (front >= back) {
result.range.min = back; result.range.min = back;
result.range.max = front; result.range.max = front;
@@ -73,15 +73,15 @@ Cursor MakeCursor(Int front, Int back) {
return result; return result;
} }
Cursor ChangeBack(Cursor cursor, Int back) { Caret ChangeBack(Caret caret, Int back) {
Int front = GetFront(cursor); Int front = GetFront(caret);
Cursor result = MakeCursor(front, back); Caret result = MakeCaret(front, back);
return result; return result;
} }
Cursor ChangeFront(Cursor cursor, Int front) { Caret ChangeFront(Caret caret, Int front) {
Int back = GetBack(cursor); Int back = GetBack(caret);
Cursor result = MakeCursor(front, back); Caret result = MakeCaret(front, back);
return result; return result;
} }
@@ -95,7 +95,7 @@ bool AreEqual(Range a, Range b) {
return result; return result;
} }
bool AreEqual(Cursor a, Cursor b) { bool AreEqual(Caret a, Caret b) {
bool result = AreEqual(a.range, b.range) && a.ifront == b.ifront; bool result = AreEqual(a.range, b.range) && a.ifront == b.ifront;
return result; return result;
} }

View File

@@ -1,8 +1,8 @@
// @todo: // @todo:
// @idea: maybe redo tree? // @idea: maybe redo tree?
struct HistoryEntry { struct HistoryEntry {
Array<Edit> edits; Array<Edit> edits;
Array<Cursor> cursors; Array<Caret> carets;
}; };
struct History { struct History {

View File

@@ -1,36 +1,36 @@
void MergeCursors(Array<Cursor> *cursors) { void MergeCarets(Array<Caret> *carets) {
Scratch scratch; Scratch scratch;
// Merge cursors that overlap, this needs to be handled before any edits to // Merge carets that overlap, this needs to be handled before any edits to
// make sure overlapping edits won't happen. // make sure overlapping edits won't happen.
// @optimize @refactor: this is retarded, I hit so many array removal bugs here, without allocation please // @optimize @refactor: this is retarded, I hit so many array removal bugs here, without allocation please
Array<Cursor *> deleted_cursors = {scratch}; Array<Caret *> deleted_carets = {scratch};
ForItem(cursor, *cursors) { ForItem(caret, *carets) {
if (Contains(deleted_cursors, &cursor)) goto end_of_cursor_loop; if (Contains(deleted_carets, &caret)) goto end_of_caret_loop;
For(*cursors) { For(*carets) {
if (&it == &cursor) continue; if (&it == &caret) continue;
bool a = cursor.range.max >= it.range.min && cursor.range.max <= it.range.max; bool a = caret.range.max >= it.range.min && caret.range.max <= it.range.max;
bool b = cursor.range.min >= it.range.min && cursor.range.min <= it.range.max; bool b = caret.range.min >= it.range.min && caret.range.min <= it.range.max;
if ((a || b) && !Contains(deleted_cursors, &it)) { if ((a || b) && !Contains(deleted_carets, &it)) {
Add(&deleted_cursors, &it); Add(&deleted_carets, &it);
cursor.range.max = Max(cursor.range.max, it.range.max); caret.range.max = Max(caret.range.max, it.range.max);
cursor.range.min = Min(cursor.range.min, it.range.min); caret.range.min = Min(caret.range.min, it.range.min);
break; break;
} }
} }
end_of_cursor_loop:; end_of_caret_loop:;
} }
Array<Cursor> new_cursors = {cursors->allocator}; Array<Caret> new_carets = {carets->allocator};
For(*cursors) { For(*carets) {
if (Contains(deleted_cursors, &it) == false) { if (Contains(deleted_carets, &it) == false) {
Add(&new_cursors, it); Add(&new_carets, it);
} }
} }
Assert(new_cursors.len <= cursors->len); Assert(new_carets.len <= carets->len);
Dealloc(cursors); Dealloc(carets);
*cursors = new_cursors; *carets = new_carets;
} }
void MergeSort(int64_t Count, Edit *First, Edit *Temp) { void MergeSort(int64_t Count, Edit *First, Edit *Temp) {
@@ -134,7 +134,7 @@ void AddEdit(Array<Edit> *e, Range range, String16 string) {
Add(e, {range, string}); Add(e, {range, string});
} }
void TestBufferMultiCursor() { void TestBufferMultiCaret() {
Scratch scratch; Scratch scratch;
{ {

View File

@@ -20,7 +20,7 @@ int main(void) {
InitScratch(); InitScratch();
Test(); Test();
TestBuffer(); TestBuffer();
TestBufferMultiCursor(); TestBufferMultiCaret();
Arena Perm = {}; Arena Perm = {};
InitArena(&Perm); InitArena(&Perm);
@@ -47,7 +47,7 @@ int main(void) {
view.font_spacing = font_spacing; view.font_spacing = font_spacing;
view.line_spacing = line_spacing; view.line_spacing = line_spacing;
view.char_spacing = GetCharSpacing(font, font_size, font_spacing); view.char_spacing = GetCharSpacing(font, font_size, font_spacing);
Add(&view.cursors, {0, 0}); Add(&view.carets, {0, 0});
view.buffer = CreateBuffer(Perm); view.buffer = CreateBuffer(Perm);
{ {
Scratch scratch; Scratch scratch;
@@ -68,7 +68,7 @@ int main(void) {
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
{ {
DrawVisibleCells(view); DrawVisibleCells(view);
For(view.cursors) DrawCursor(view, it); For(view.carets) DrawCaret(view, it);
} }
EndDrawing(); EndDrawing();
} }

View File

@@ -5,10 +5,10 @@ struct View {
float line_spacing; float line_spacing;
float char_spacing; float char_spacing;
Vec2 scroll; Vec2 scroll;
Buffer *buffer; Buffer *buffer;
Array<Cursor> cursors; Array<Caret> carets;
Rect2 rect; Rect2 rect;
}; };
Int MoveOnWhitespaceBoundaryForward(Buffer &buffer, Int pos) { Int MoveOnWhitespaceBoundaryForward(Buffer &buffer, Int pos) {
@@ -101,7 +101,7 @@ Int MovePosByXY(Buffer &buffer, Int pos, XY offset) {
return result; return result;
} }
Int MoveCursor(Buffer &buffer, Int pos, int direction, bool ctrl_pressed) { Int MoveCaret(Buffer &buffer, Int pos, int direction, bool ctrl_pressed) {
Assert(direction >= 0 && direction <= 3); Assert(direction >= 0 && direction <= 3);
if (ctrl_pressed) { if (ctrl_pressed) {
switch (direction) { switch (direction) {
@@ -125,7 +125,7 @@ Int MoveCursor(Buffer &buffer, Int pos, int direction, bool ctrl_pressed) {
void HandleKeybindings(View *_view) { void HandleKeybindings(View *_view) {
View &view = *_view; View &view = *_view;
Buffer &buf = *view.buffer; Buffer &buf = *view.buffer;
Cursor main_cursor_on_begin_frame = view.cursors[0]; Caret main_cursor_on_begin_frame = view.carets[0];
if (IsKeyDown(KEY_F1)) { if (IsKeyDown(KEY_F1)) {
view.scroll.x -= GetMouseWheelMove() * 48; view.scroll.x -= GetMouseWheelMove() * 48;
@@ -136,27 +136,28 @@ void HandleKeybindings(View *_view) {
int keys[4] = {KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_UP}; int keys[4] = {KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_UP};
for (int i = 0; i < 4; i += 1) { for (int i = 0; i < 4; i += 1) {
if (IsKeyPressed(keys[i]) || IsKeyPressedRepeat(keys[i])) { if (IsKeyPressed(keys[i]) || IsKeyPressedRepeat(keys[i])) {
For(view.cursors) { For(view.carets) {
if (IsKeyDown(KEY_LEFT_SHIFT)) { if (IsKeyDown(KEY_LEFT_SHIFT)) {
Int front = GetFront(it); Int front = GetFront(it);
Int new_front = MoveCursor(buf, front, i, IsKeyDown(KEY_LEFT_CONTROL)); Int new_front = MoveCaret(buf, front, i, IsKeyDown(KEY_LEFT_CONTROL));
it = ChangeFront(it, new_front); it = ChangeFront(it, new_front);
} else { } else {
it.range.max = it.range.min = MoveCursor(buf, it.range.max, i, IsKeyDown(KEY_LEFT_CONTROL)); it.range.max = it.range.min = MoveCaret(buf, it.range.max, i, IsKeyDown(KEY_LEFT_CONTROL));
} }
} }
} }
} }
// Scrolling with caret
// @refactor: this should happen after we calculate rect for the view // @refactor: this should happen after we calculate rect for the view
// Usually in other text editors apart from view there is also a 'window' // Usually in other text editors apart from view there is also a 'window'
// which is exactly the thing which shows the view, will need // which is exactly the thing which shows the view, will need
// to think about this later // to think about this later
if (!AreEqual(main_cursor_on_begin_frame, view.cursors[0])) { if (!AreEqual(main_cursor_on_begin_frame, view.carets[0])) {
Cursor c = view.cursors[0]; Caret c = view.carets[0];
Int front = GetFront(c); Int front = GetFront(c);
XY xy = PosToXY(buf, front); XY xy = PosToXY(buf, front);
Rect2I GetVisibleCells(const View &view); Rect2I GetVisibleCells(const View &view);
Rect2I visible = GetVisibleCells(view); Rect2I visible = GetVisibleCells(view);

View File

@@ -26,22 +26,22 @@ void DrawVisibleCells(const View &view) {
} }
} }
void DrawCursor(const View &view, XY xy, float size, Color color) { void DrawCaret(const View &view, XY xy, float size, Color color) {
Rect2 _rect = Rect2FromSize({(float)xy.col * view.char_spacing, (float)xy.line * view.line_spacing}, {view.char_spacing, view.line_spacing}); Rect2 _rect = Rect2FromSize({(float)xy.col * view.char_spacing, (float)xy.line * view.line_spacing}, {view.char_spacing, view.line_spacing});
Rect2 rect = CutLeft(&_rect, size); Rect2 rect = CutLeft(&_rect, size);
rect -= view.scroll; rect -= view.scroll;
DrawRectangleRec(ToRectangle(rect), color); DrawRectangleRec(ToRectangle(rect), color);
} }
void DrawCursor(const View &view, Cursor it) { void DrawCaret(const View &view, Caret it) {
Buffer &buf = *view.buffer; Buffer &buf = *view.buffer;
Int front = GetFront(it); Int front = GetFront(it);
XY fxy = PosToXY(buf, front); XY fxy = PosToXY(buf, front);
DrawCursor(view, fxy, 2, RED); DrawCaret(view, fxy, 2, RED);
Int back = GetBack(it); Int back = GetBack(it);
XY bxy = PosToXY(buf, back); XY bxy = PosToXY(buf, back);
DrawCursor(view, bxy, 1, GREEN); DrawCaret(view, bxy, 1, GREEN);
// Draw selection // Draw selection
if (front != back) { if (front != back) {