Rename cursor to caret because cursor can be confused with mouse pointer
This commit is contained in:
@@ -13,7 +13,7 @@ struct Range {
|
||||
Int max;
|
||||
};
|
||||
|
||||
struct Cursor {
|
||||
struct Caret {
|
||||
union {
|
||||
Range range;
|
||||
Int pos[2];
|
||||
|
||||
@@ -48,19 +48,19 @@ Range GetRange(Buffer &buffer) {
|
||||
return result;
|
||||
}
|
||||
|
||||
Int GetFront(Cursor cursor) {
|
||||
Int result = cursor.pos[cursor.ifront];
|
||||
Int GetFront(Caret caret) {
|
||||
Int result = caret.pos[caret.ifront];
|
||||
return result;
|
||||
}
|
||||
|
||||
Int GetBack(Cursor cursor) {
|
||||
Int index = (cursor.ifront + 1) % 2;
|
||||
Int result = cursor.pos[index];
|
||||
Int GetBack(Caret caret) {
|
||||
Int index = (caret.ifront + 1) % 2;
|
||||
Int result = caret.pos[index];
|
||||
return result;
|
||||
}
|
||||
|
||||
Cursor MakeCursor(Int front, Int back) {
|
||||
Cursor result = {};
|
||||
Caret MakeCaret(Int front, Int back) {
|
||||
Caret result = {};
|
||||
if (front >= back) {
|
||||
result.range.min = back;
|
||||
result.range.max = front;
|
||||
@@ -73,15 +73,15 @@ Cursor MakeCursor(Int front, Int back) {
|
||||
return result;
|
||||
}
|
||||
|
||||
Cursor ChangeBack(Cursor cursor, Int back) {
|
||||
Int front = GetFront(cursor);
|
||||
Cursor result = MakeCursor(front, back);
|
||||
Caret ChangeBack(Caret caret, Int back) {
|
||||
Int front = GetFront(caret);
|
||||
Caret result = MakeCaret(front, back);
|
||||
return result;
|
||||
}
|
||||
|
||||
Cursor ChangeFront(Cursor cursor, Int front) {
|
||||
Int back = GetBack(cursor);
|
||||
Cursor result = MakeCursor(front, back);
|
||||
Caret ChangeFront(Caret caret, Int front) {
|
||||
Int back = GetBack(caret);
|
||||
Caret result = MakeCaret(front, back);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ bool AreEqual(Range a, Range b) {
|
||||
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;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// @todo:
|
||||
// @idea: maybe redo tree?
|
||||
struct HistoryEntry {
|
||||
Array<Edit> edits;
|
||||
Array<Cursor> cursors;
|
||||
Array<Edit> edits;
|
||||
Array<Caret> carets;
|
||||
};
|
||||
|
||||
struct History {
|
||||
|
||||
@@ -1,36 +1,36 @@
|
||||
void MergeCursors(Array<Cursor> *cursors) {
|
||||
void MergeCarets(Array<Caret> *carets) {
|
||||
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.
|
||||
|
||||
// @optimize @refactor: this is retarded, I hit so many array removal bugs here, without allocation please
|
||||
Array<Cursor *> deleted_cursors = {scratch};
|
||||
ForItem(cursor, *cursors) {
|
||||
if (Contains(deleted_cursors, &cursor)) goto end_of_cursor_loop;
|
||||
Array<Caret *> deleted_carets = {scratch};
|
||||
ForItem(caret, *carets) {
|
||||
if (Contains(deleted_carets, &caret)) goto end_of_caret_loop;
|
||||
|
||||
For(*cursors) {
|
||||
if (&it == &cursor) continue;
|
||||
bool a = cursor.range.max >= it.range.min && cursor.range.max <= it.range.max;
|
||||
bool b = cursor.range.min >= it.range.min && cursor.range.min <= it.range.max;
|
||||
if ((a || b) && !Contains(deleted_cursors, &it)) {
|
||||
Add(&deleted_cursors, &it);
|
||||
cursor.range.max = Max(cursor.range.max, it.range.max);
|
||||
cursor.range.min = Min(cursor.range.min, it.range.min);
|
||||
For(*carets) {
|
||||
if (&it == &caret) continue;
|
||||
bool a = caret.range.max >= it.range.min && caret.range.max <= it.range.max;
|
||||
bool b = caret.range.min >= it.range.min && caret.range.min <= it.range.max;
|
||||
if ((a || b) && !Contains(deleted_carets, &it)) {
|
||||
Add(&deleted_carets, &it);
|
||||
caret.range.max = Max(caret.range.max, it.range.max);
|
||||
caret.range.min = Min(caret.range.min, it.range.min);
|
||||
break;
|
||||
}
|
||||
}
|
||||
end_of_cursor_loop:;
|
||||
end_of_caret_loop:;
|
||||
}
|
||||
|
||||
Array<Cursor> new_cursors = {cursors->allocator};
|
||||
For(*cursors) {
|
||||
if (Contains(deleted_cursors, &it) == false) {
|
||||
Add(&new_cursors, it);
|
||||
Array<Caret> new_carets = {carets->allocator};
|
||||
For(*carets) {
|
||||
if (Contains(deleted_carets, &it) == false) {
|
||||
Add(&new_carets, it);
|
||||
}
|
||||
}
|
||||
Assert(new_cursors.len <= cursors->len);
|
||||
Dealloc(cursors);
|
||||
*cursors = new_cursors;
|
||||
Assert(new_carets.len <= carets->len);
|
||||
Dealloc(carets);
|
||||
*carets = new_carets;
|
||||
}
|
||||
|
||||
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});
|
||||
}
|
||||
|
||||
void TestBufferMultiCursor() {
|
||||
void TestBufferMultiCaret() {
|
||||
Scratch scratch;
|
||||
|
||||
{
|
||||
|
||||
@@ -20,7 +20,7 @@ int main(void) {
|
||||
InitScratch();
|
||||
Test();
|
||||
TestBuffer();
|
||||
TestBufferMultiCursor();
|
||||
TestBufferMultiCaret();
|
||||
|
||||
Arena Perm = {};
|
||||
InitArena(&Perm);
|
||||
@@ -47,7 +47,7 @@ int main(void) {
|
||||
view.font_spacing = font_spacing;
|
||||
view.line_spacing = line_spacing;
|
||||
view.char_spacing = GetCharSpacing(font, font_size, font_spacing);
|
||||
Add(&view.cursors, {0, 0});
|
||||
Add(&view.carets, {0, 0});
|
||||
view.buffer = CreateBuffer(Perm);
|
||||
{
|
||||
Scratch scratch;
|
||||
@@ -68,7 +68,7 @@ int main(void) {
|
||||
ClearBackground(RAYWHITE);
|
||||
{
|
||||
DrawVisibleCells(view);
|
||||
For(view.cursors) DrawCursor(view, it);
|
||||
For(view.carets) DrawCaret(view, it);
|
||||
}
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
@@ -5,10 +5,10 @@ struct View {
|
||||
float line_spacing;
|
||||
float char_spacing;
|
||||
|
||||
Vec2 scroll;
|
||||
Buffer *buffer;
|
||||
Array<Cursor> cursors;
|
||||
Rect2 rect;
|
||||
Vec2 scroll;
|
||||
Buffer *buffer;
|
||||
Array<Caret> carets;
|
||||
Rect2 rect;
|
||||
};
|
||||
|
||||
Int MoveOnWhitespaceBoundaryForward(Buffer &buffer, Int pos) {
|
||||
@@ -101,7 +101,7 @@ Int MovePosByXY(Buffer &buffer, Int pos, XY offset) {
|
||||
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);
|
||||
if (ctrl_pressed) {
|
||||
switch (direction) {
|
||||
@@ -125,7 +125,7 @@ Int MoveCursor(Buffer &buffer, Int pos, int direction, bool ctrl_pressed) {
|
||||
void HandleKeybindings(View *_view) {
|
||||
View &view = *_view;
|
||||
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)) {
|
||||
view.scroll.x -= GetMouseWheelMove() * 48;
|
||||
@@ -136,27 +136,28 @@ void HandleKeybindings(View *_view) {
|
||||
int keys[4] = {KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_UP};
|
||||
for (int i = 0; i < 4; i += 1) {
|
||||
if (IsKeyPressed(keys[i]) || IsKeyPressedRepeat(keys[i])) {
|
||||
For(view.cursors) {
|
||||
For(view.carets) {
|
||||
if (IsKeyDown(KEY_LEFT_SHIFT)) {
|
||||
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);
|
||||
} 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
|
||||
// Usually in other text editors apart from view there is also a 'window'
|
||||
// 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])) {
|
||||
if (!AreEqual(main_cursor_on_begin_frame, view.carets[0])) {
|
||||
|
||||
Cursor c = view.cursors[0];
|
||||
Int front = GetFront(c);
|
||||
XY xy = PosToXY(buf, front);
|
||||
Caret c = view.carets[0];
|
||||
Int front = GetFront(c);
|
||||
XY xy = PosToXY(buf, front);
|
||||
|
||||
Rect2I GetVisibleCells(const View &view);
|
||||
Rect2I visible = GetVisibleCells(view);
|
||||
|
||||
@@ -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 = CutLeft(&_rect, size);
|
||||
rect -= view.scroll;
|
||||
DrawRectangleRec(ToRectangle(rect), color);
|
||||
}
|
||||
|
||||
void DrawCursor(const View &view, Cursor it) {
|
||||
void DrawCaret(const View &view, Caret it) {
|
||||
Buffer &buf = *view.buffer;
|
||||
Int front = GetFront(it);
|
||||
XY fxy = PosToXY(buf, front);
|
||||
DrawCursor(view, fxy, 2, RED);
|
||||
DrawCaret(view, fxy, 2, RED);
|
||||
|
||||
Int back = GetBack(it);
|
||||
XY bxy = PosToXY(buf, back);
|
||||
DrawCursor(view, bxy, 1, GREEN);
|
||||
DrawCaret(view, bxy, 1, GREEN);
|
||||
|
||||
// Draw selection
|
||||
if (front != back) {
|
||||
|
||||
Reference in New Issue
Block a user