diff --git a/src/buffer.h b/src/buffer.h index e93e38b..2f143c2 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -3,6 +3,7 @@ #include "ed_array.h" #include "string.h" #include "ui.h" +#include typedef struct { size_t link_index; @@ -74,9 +75,27 @@ uint8_t ed_buffer_get_char_at(ed_buffer *buffer, ed_buffer_cursor cursor) { } ed_buffer ed_buffer_from_file(string path) { + char text[] = +"bool ht_set(ed_ht *ht, string key, void *value) {\n" +" ht_hash_t hash = ht_hash(key, ht->capacity);\n" +"\n" +" for (size_t i=hash; icapacity; ++i) {\n" +" if (ht->key_slots[i].key.data == NULL || string_eq(ht->key_slots[i].key, key)) {\n" +" if (ht->key_slots[i].key.data != NULL) {\n" +" free(ht->key_slots[i].key.data);\n" +" }\n" +" ht->key_slots[i].key = string_copy(key);\n" +"\n" +" memcpy(ht->value_slots+i*ht->value_size, value, ht->value_size);\n" +" return true;\n" +" }\n" +" }\n" +" return false;\n" +"}\n"; + slice(uint8_t) data = { - .data = (uint8_t *)"Hello!\nThis is a test document!\nWith three lines.\nJust kidding, there actually four", - .len = 83, + .data = text, + .len = sizeof(text), }; ed_buffer buffer = { .data = _ed_buffer_init_buffer_data(data), @@ -122,6 +141,54 @@ bool ed_buffer_content_iter(ed_buffer *buffer, ed_buffer_cursor *cursor) { return true; } +void ed_buffer_insert_text(ed_buffer *buffer, string text, ed_buffer_cursor cursor) { + if (text.len == 0) { + return; + } + + size_t content_start = buffer->data.added_content.size; + pushArrayMulti(uint8_t, &buffer->data.added_content, text.data, text.len); + + if (cursor.offset.content_index == 0) { + insertArrayAt(ed_buffer_content_link, + &buffer->data.linked_content, + cursor.offset.link_index, + ((ed_buffer_content_link) { + .type = ED_BUFFER_CONTENT_LINK_TYPE_ADDED, + .start = content_start, + .end = content_start+text.len, + .len = text.len, + }) + ); + } else { + ed_buffer_content_link end_link = buffer->data.linked_content.data[cursor.offset.link_index]; + end_link.start += cursor.offset.content_index; + end_link.len -= cursor.offset.content_index; + + buffer->data.linked_content.data[cursor.offset.link_index].end = end_link.start; + buffer->data.linked_content.data[cursor.offset.link_index].len = buffer->data.linked_content.data[cursor.offset.link_index].end - buffer->data.linked_content.data[cursor.offset.link_index].start; + + // insert new content + insertArrayAt(ed_buffer_content_link, + &buffer->data.linked_content, + cursor.offset.link_index+1, + ((ed_buffer_content_link) { + .type = ED_BUFFER_CONTENT_LINK_TYPE_ADDED, + .start = content_start, + .end = content_start+text.len, + .len = text.len, + }) + ); + + // insert cut slice + insertArrayAt(ed_buffer_content_link, + &buffer->data.linked_content, + cursor.offset.link_index+2, + end_link + ); + } +} + void ed_buffer_render(ui_context *cx, ed_buffer *buffer) { ui_element(cx, _String("Buffer"), UI_AXIS_VERTICAL, ui_make_size(ui_fill, ui_fill), UI_FLAG_DRAW_BACKGROUND); ui_push_parent(cx); @@ -144,7 +211,15 @@ void ed_buffer_render(ui_context *cx, ed_buffer *buffer) { uint8_t *str = malloc(line_buffer_index * sizeof(uint8_t)); memcpy(str, line_buffer, line_buffer_index * sizeof(uint8_t)); - ui_element(cx, (string){ .data = str, .len = line_buffer_index, .owned = true }, UI_AXIS_HORIZONTAL, ui_make_size(ui_fit_text, ui_fit_text), UI_FLAG_DRAW_TEXT); + ui_element(cx, + (string){ .data = str, + .len = line_buffer_index, + .owned = true + }, + UI_AXIS_HORIZONTAL, + ui_make_size(ui_fit_text, ui_fit_text), + UI_FLAG_DRAW_TEXT|UI_FLAG_DRAW_BACKGROUND + ); } while(ed_buffer_content_iter(buffer, &cursor)); } ui_pop_parent(cx); diff --git a/src/ed_array.h b/src/ed_array.h index 150ff98..025223e 100644 --- a/src/ed_array.h +++ b/src/ed_array.h @@ -27,6 +27,24 @@ void T ## _PushArray(array(T) *arr, T value) {\ } else {\ fprintf(stderr, "failed to push to u8 array, size+num > capacity\n");\ }\ +};\ +void T ## _PushArrayMulti(array(T) *arr, T *values, size_t len) {\ + for (size_t i = 0; i < len; ++i) {\ + T ## _PushArray(arr, values[i]);\ + }\ +};\ +void T ## _InsertArrayAt(array(T) *arr, size_t loc, T value) {\ + if (arr->size == arr->capacity) {\ + arr->capacity *= 2;\ + void *new_data = realloc(arr->data, arr->capacity);\ + if (new_data == NULL) {\ + fprintf(stderr, "out of memory when reallocating array\n");\ + }\ + arr->data = new_data;\ + }\ + memcpy(&arr->data[loc+1], &arr->data[loc], arr->size * sizeof(T));\ + arr->data[loc] = value;\ + arr->size += 1;\ }; #define slice(T) struct T ## _Slice @@ -50,6 +68,9 @@ slice(T) T ## _SliceConstruct(void *data, size_t len) {\ #define newArray(T, size) T ## _ArrayConstruct(size) #define newArrayFromSlice(T, s) T ## _FromSlice(s) #define pushArray(T, arr, value) T ## _PushArray(arr, (value)) +#define pushArrayMulti(T, arr, values, len) T ## _PushArrayMulti(arr, values, len) + +#define insertArrayAt(T, arr, loc, value) T ## _InsertArrayAt(arr, loc, (value)) #define newSlice(T, data, len) T ## _SliceConstruct(data, len) diff --git a/src/main.c b/src/main.c index 9d2091b..ed5e69a 100644 --- a/src/main.c +++ b/src/main.c @@ -105,7 +105,7 @@ void ed_init(_gfx_frame_func frame_func) { gfx_push_vertex_buffer(state.gfx_cx, indices, sizeof(indices)); state.gpu_ui_rects = newArray(GpuUiRect, 2000); - state.gpu_glyphs = newArray(GpuGlyph, 1024); + state.gpu_glyphs = newArray(GpuGlyph, 2048); state.glyph_cache = newArray(GpuGlyph, 97); gfx_allocate_vertex_buffer(state.gfx_cx, state.gpu_glyphs.capacity * sizeof(GpuGlyph)); @@ -182,6 +182,8 @@ void ed_init(_gfx_frame_func frame_func) { gfx_push_texture_buffer(state.gfx_cx, font_bitmap_size, font_bitmap_size, font_bitmap, font_bitmap_size*font_bitmap_size * sizeof(uint8_t)); state.test_buffer = ed_buffer_from_file(_String("")); + + ed_buffer_insert_text(&state.test_buffer, _String("_hehe i'm inserting_"), (ed_buffer_cursor) { .offset = (ed_buffer_offset) { .link_index = 0, .content_index = 5 } }); } void render_ui_text(string text, float position[2]) {