text insertion!

main
Patrick Cleavelin 2024-03-23 15:04:44 -05:00
parent da0b9ab2c7
commit 0a57e30ead
3 changed files with 102 additions and 4 deletions

View File

@ -3,6 +3,7 @@
#include "ed_array.h" #include "ed_array.h"
#include "string.h" #include "string.h"
#include "ui.h" #include "ui.h"
#include <stdbool.h>
typedef struct { typedef struct {
size_t link_index; 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) { 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; i<ht->capacity; ++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 = { slice(uint8_t) data = {
.data = (uint8_t *)"Hello!\nThis is a test document!\nWith three lines.\nJust kidding, there actually four", .data = text,
.len = 83, .len = sizeof(text),
}; };
ed_buffer buffer = { ed_buffer buffer = {
.data = _ed_buffer_init_buffer_data(data), .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; 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) { 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_element(cx, _String("Buffer"), UI_AXIS_VERTICAL, ui_make_size(ui_fill, ui_fill), UI_FLAG_DRAW_BACKGROUND);
ui_push_parent(cx); 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)); uint8_t *str = malloc(line_buffer_index * sizeof(uint8_t));
memcpy(str, line_buffer, 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)); } while(ed_buffer_content_iter(buffer, &cursor));
} }
ui_pop_parent(cx); ui_pop_parent(cx);

View File

@ -27,6 +27,24 @@ void T ## _PushArray(array(T) *arr, T value) {\
} else {\ } else {\
fprintf(stderr, "failed to push to u8 array, size+num > capacity\n");\ 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 #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 newArray(T, size) T ## _ArrayConstruct(size)
#define newArrayFromSlice(T, s) T ## _FromSlice(s) #define newArrayFromSlice(T, s) T ## _FromSlice(s)
#define pushArray(T, arr, value) T ## _PushArray(arr, (value)) #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) #define newSlice(T, data, len) T ## _SliceConstruct(data, len)

View File

@ -105,7 +105,7 @@ void ed_init(_gfx_frame_func frame_func) {
gfx_push_vertex_buffer(state.gfx_cx, indices, sizeof(indices)); gfx_push_vertex_buffer(state.gfx_cx, indices, sizeof(indices));
state.gpu_ui_rects = newArray(GpuUiRect, 2000); 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); state.glyph_cache = newArray(GpuGlyph, 97);
gfx_allocate_vertex_buffer(state.gfx_cx, state.gpu_glyphs.capacity * sizeof(GpuGlyph)); 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)); 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("")); 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]) { void render_ui_text(string text, float position[2]) {