Move up down and multi cursor

This commit is contained in:
Krzosa Karol
2024-06-22 20:58:29 +02:00
parent 5ba9975c10
commit 48eca55a4c
2 changed files with 140 additions and 69 deletions

View File

@@ -69,15 +69,29 @@ void DrawString(Font font, String text, Vector2 position, float fontSize, float
}
int64_t MoveRight(Buffer &buffer, int64_t pos) {
int64_t result = pos + 1;
result = AdjustUTF8Pos(buffer, result);
return result;
pos = pos + 1;
pos = AdjustUTF8Pos(buffer, pos);
Assert(pos >= 0 && pos <= buffer.len);
return pos;
}
int64_t MoveLeft(Buffer &buffer, int64_t pos) {
int64_t result = pos - 1;
result = AdjustUTF8Pos(buffer, result, -1);
return result;
pos = pos - 1;
pos = AdjustUTF8Pos(buffer, pos, -1);
Assert(pos >= 0 && pos <= buffer.len);
return pos;
}
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);
return new_pos;
}
int64_t MoveUp(Buffer &buffer, int64_t pos) {
LineAndColumn info = FindLineAndColumn(buffer, pos);
int64_t new_pos = FindPos(buffer, info.line.number - 1, info.column);
return new_pos;
}
int main() {
@@ -99,8 +113,8 @@ int main() {
Window *window = &windows[0];
Buffer *buffer = &window->buffer;
InitBuffer(buffer);
if (0) {
for (int i = 0; i < 100; i += 1) {
if (1) {
for (int i = 0; i < 5; i += 1) {
Array<Edit> edits = {FrameArena};
AddEdit(&edits, GetEnd(*buffer), Format(FrameArena, "line number: %d\n", i));
ApplyEdits(buffer, edits);
@@ -108,6 +122,7 @@ int main() {
}
window->cursors.add({});
window->cursors.add(GetLine(*buffer, 1).range);
}
Vec2 camera_offset_world_to_render_units = {};
@@ -123,20 +138,6 @@ int main() {
focused_window->window_world_to_window_units.y -= mouse_wheel;
focused_window->window_world_to_window_units.y = ClampBottom(focused_window->window_world_to_window_units.y, 0.f);
for (int c = GetCharPressed(); c; c = GetCharPressed()) {
String string = "?";
UTF8Result utf8 = UTF32ToUTF8((uint32_t)c);
if (utf8.error == 0) {
string = {(char *)utf8.out_str, (int64_t)utf8.len};
}
Array<Edit> edits = {FrameArena};
For(focused_window->cursors) AddEdit(&edits, it, string);
ApplyEdits(&focused_window->buffer, edits);
For(focused_window->cursors)
it.max = it.min = MoveRight(focused_window->buffer, it.min);
}
if (IsKeyPressed(KEY_LEFT) || IsKeyPressedRepeat(KEY_LEFT)) {
For(focused_window->cursors) {
it.max = it.min = MoveLeft(focused_window->buffer, it.min);
@@ -147,6 +148,53 @@ int main() {
it.max = it.min = MoveRight(focused_window->buffer, it.min);
}
}
if (IsKeyPressed(KEY_DOWN) || IsKeyPressedRepeat(KEY_DOWN)) {
For(focused_window->cursors) {
it.max = it.min = MoveDown(focused_window->buffer, it.min);
}
}
if (IsKeyPressed(KEY_UP) || IsKeyPressedRepeat(KEY_UP)) {
For(focused_window->cursors) {
it.max = it.min = MoveUp(focused_window->buffer, it.min);
}
}
// Merge cursors that overlap
IterRemove(focused_window->cursors) {
IterRemovePrepare(focused_window->cursors);
ForItem(cursor, focused_window->cursors) {
if (&cursor == &it) continue;
if (cursor.min == it.min) {
remove_item = true;
break;
}
}
}
// Handle user input chars
for (int c = GetCharPressed(); c; c = GetCharPressed()) {
String string = "?";
UTF8Result utf8 = UTF32ToUTF8((uint32_t)c);
if (utf8.error == 0) {
string = {(char *)utf8.out_str, (int64_t)utf8.len};
}
Array<Edit> edits = {FrameArena};
For(focused_window->cursors) {
AddEdit(&edits, it, string);
}
ApplyEdits(&focused_window->buffer, edits);
For(focused_window->cursors) {
// Need to update current cursor and all cursors after it.
ForItem(cursor, focused_window->cursors) {
if (cursor.min >= it.min) {
cursor.min = cursor.max = MoveRight(focused_window->buffer, cursor.min);
}
}
}
}
}
BeginDrawing();
@@ -215,16 +263,16 @@ int main() {
if (font.texture.id == 0) font = GetFontDefault();
float textOffsetX = 0.0f;
float scaleFactor = font_size / font.baseSize; // Character quad scaling factor
for (int64_t i = 0;;) {
bool end_of_buffer = i == window.buffer.len;
bool new_line = i == line_range.max && i != window.buffer.len;
bool in_range = i < text.len;
for (BufferIter iter = Iterate(window.buffer, line_range);; Advance(&iter)) {
bool end_of_buffer = iter.pos == window.buffer.len;
bool new_line = iter.pos == line_range.max;
bool in_range = IsValid(iter);
bool continue_looping = end_of_buffer || new_line || in_range;
if (!continue_looping) break;
int codepointByteCount = 1;
int codepoint = '\n';
if (in_range) codepoint = GetCodepointNext(&text.data[i], &codepointByteCount);
int codepoint = '\n';
if (in_range) codepoint = iter.item;
int index = GetGlyphIndex(font, codepoint);
GlyphInfo *glyph = font.glyphs + index;
@@ -239,12 +287,12 @@ int main() {
// Clip everything that is outside the window and screen
if (CheckCollisionRecs(cell_rectangle, ToRectangle(window_rect_in_render_units_clamped_to_screen))) {
row.cells.add({cell_rect, codepoint, line_range.min + i});
row.cells.add({cell_rect, codepoint, iter.pos});
row.rect.max = cell_rect.max;
}
textOffsetX += x_to_offset_by;
i += codepointByteCount; // Move text bytes counter to next codepoint
if (end_of_buffer || new_line) break;
}
if (row.cells.len) rows.add(row);