app_frame, ui layout, draw the list

This commit is contained in:
Krzosa Karol
2025-01-09 10:38:08 +01:00
parent bd5ce59791
commit 74a60e812d
9 changed files with 234 additions and 217 deletions

View File

@@ -9,7 +9,7 @@
#include "render/backup_font.c"
#include "render/font.c"
#include "render/render_opengl.c"
// #include "gfx2d/gfx2d.c"
// #include "gfx2d/gfx2d.c"dv
#include "ui.c"
@@ -32,8 +32,6 @@ gb globals_t *globals;
fn void app_init(void) {
ui_test_iterators();
ma_arena_t *perm = &tcx._perm;
globals = ma_push_type(perm, globals_t);
// globals->gfx = ma_push_type(perm, gfx_t);
@@ -41,65 +39,53 @@ fn void app_init(void) {
ui_init(perm);
}
fn b32 app_update(app_event_list_t events) {
fn b32 app_update(app_frame_t *frame) {
ui_begin_frame();
defer_block(ui_begin_build(), ui_end_build()) {
defer_block(ui_push_expander("app_event_t"), ui_pop_parent()) {
defer_block(ui_push_expander("mouse_wheel_delta: v3f64_t"), ui_pop_parent()) {
ui_label("x: f64 = value");
ui_label("y: f64 = value");
ui_label("z: f64 = value");
}
ui_label("kind: app_event_kind_t = value");
ui_label("ctrl: b8 = value");
ui_label("shift: b8 = value");
defer_block(ui_push_expander("pos: v2f64_t"), ui_pop_parent()) {
defer_block(ui_push_expander("inner_pos: v2f64_t"), ui_pop_parent()) {
defer_block(ui_push_list_container(), ui_pop_parent()) {
if (ui_push_expander("app_event_t")) {
if (ui_push_expander("mouse_wheel_delta: v3f64_t")) {
ui_label("x: f64 = value");
ui_label("y: f64 = value");
ui_label("z: f64 = value");
ui_pop_expander();
}
ui_label("y: f64 = value");
defer_block(ui_push_expander("inner_pos: v2f64_t"), ui_pop_parent()) {
ui_label("x: f64 = value");
ui_label("kind: app_event_kind_t = value");
ui_label("ctrl: b8 = value");
ui_label("shift: b8 = value");
if (ui_push_expander("pos: v2f64_t")) {
if (ui_push_expander("inner_pos: v2f64_t")) {
ui_label("x: f64 = value");
ui_label("y: f64 = value");
ui_pop_expander();
}
ui_label("y: f64 = value");
if (ui_push_expander("inner_pos: v2f64_t")) {
ui_label("x: f64 = value");
ui_label("y: f64 = value");
ui_pop_expander();
}
ui_pop_expander();
}
ui_label("alt: b8 = value");
ui_label("meta: b8 = value");
ui_pop_expander();
}
ui_label("alt: b8 = value");
ui_label("meta: b8 = value");
}
}
for (app_event_t *ev = events.first; ev; ev = ev->next) {
for (app_event_t *ev = frame->first_event; ev; ev = ev->next) {
if (globals->event.kind == app_event_kind_null) {
globals->event = *ev;
}
#if 0
// ui_struct()
type_t *ti = type(app_event_t);
ui_push_list("▼ %S: struct", ti->name);
for (type_member_t *tm = ti->members; tm < ti->members + ti->count; tm += 1) {
if (tm->kind == type_kind_struct) {
ui_list_struct(tm->type, ti_get_member_offset(tm, p)
} else {
ui_list_item("%-20S: %S = %S", tm->name, ti_serial_type(tcx.temp, tm->type), ti_serial_data(p, type));
}
}
ui_pop_list();
#endif
}
{
app_event_t *ev = events.last;
rn_begin();
rn_draw_string(&rn_state.main_font, v2f32(0,0), black_color_global, s8_lit("hello world!"));
rn_end(v2f64_to_v2f32(ev->window_size), white_color_global);
rn_begin();
rn_draw_stringf(&rn_state.main_font, v2f32(0,0), black_color_global, "delta: %f update: %f", frame->delta, frame->update);
}
ui_draw();
rn_end(frame->window_size, white_color_global);
#if 0
// These steps should be totally optional!!
@@ -138,5 +124,5 @@ fn b32 app_update(app_event_list_t events) {
#endif
ui_end_frame();
return false;
return true;
}

View File

@@ -20,8 +20,8 @@ typedef enum {
typedef struct ui_size_t ui_size_t;
struct ui_size_t {
ui_size_kind_t kind;
f64 value;
f64 strictness;
f32 value;
f32 strictness;
};
typedef struct ui_box_t ui_box_t;
@@ -36,11 +36,13 @@ struct ui_box_t {
ui_box_t *last;
s8_t string;
ui_size_t semantic_size[ui_axis2_count];
b32 grow_axis[ui_axis2_count];
// computed by layout system
f64 computed_rel_pos[ui_axis2_count];
f64 computed_size[ui_axis2_count];
r2f64_t rect;
f32 iter_pos[ui_axis2_count];
f32 computed_rel_pos[ui_axis2_count];
f32 computed_size[ui_axis2_count];
r2f32_t rect;
};
typedef struct ui_t ui_t;
@@ -114,17 +116,50 @@ ui_box_t *ui_pop_parent(void) {
return top;
}
ui_box_t *ui_push_expander(char *fmt, ...) {
S8_FMT(tcx.temp, fmt, string);
ui_id_t id = ui_id_from_string(string);
ui_box_t *ui_spacer(char *spacing_char) {
ui_id_t id = ui_null_id;
ui_box_t *box = ui_alloc_box(id);
ui_push_box(box);
v2f32_t spacing = rn_measure_string(&rn_state.main_font, s8_from_char(spacing_char));
box->semantic_size[0] = (ui_size_t){ui_size_kind_pixels, spacing.x, 0.5};
box->semantic_size[1] = (ui_size_t){ui_size_kind_pixels, 10, 0.5};
return box;
}
ui_box_t *ui_push_list_container(void) {
ui_id_t id = ui_null_id;
ui_box_t *box = ui_alloc_box(id);
ui_push_parent(box);
box->semantic_size[0] = (ui_size_t){ui_size_kind_children_sum, 0, 0.5};
box->semantic_size[1] = (ui_size_t){ui_size_kind_children_sum, 0, 0.5};
box->grow_axis[1] = true;
return box;
}
ui_box_t *ui_push_expander(char *str, ...) {
S8_FMT(tcx.temp, str, string);
ui_id_t id = ui_id_from_string(string);
ui_box_t *box = ui_alloc_box(id);
ui_push_box(box);
box->string = string;
box->semantic_size[0] = (ui_size_t){ui_size_kind_text_content, 0, 0.5};
box->semantic_size[1] = (ui_size_t){ui_size_kind_pixels, 40, 0.5};
box->semantic_size[1] = (ui_size_t){ui_size_kind_text_content, 0, 0.5};
{
ui_box_t *container = ui_push_list_container();
container->grow_axis[0] = 1;
container->grow_axis[1] = 0;
ui_spacer("-");
ui_box_t *c = ui_push_list_container();
}
return box;
}
void ui_pop_expander() {
ui_pop_parent();
ui_pop_parent();
}
ui_box_t *ui_label(char *fmt, ...) {
S8_FMT(tcx.temp, fmt, string);
ui_id_t id = ui_id_from_string(string);
@@ -132,7 +167,7 @@ ui_box_t *ui_label(char *fmt, ...) {
ui_push_box(box);
box->string = string;
box->semantic_size[0] = (ui_size_t){ui_size_kind_text_content, 0, 0.5};
box->semantic_size[1] = (ui_size_t){ui_size_kind_pixels, 40, 0.5};
box->semantic_size[1] = (ui_size_t){ui_size_kind_text_content, 0, 0.5};
return box;
}
@@ -209,66 +244,6 @@ void ui_test_stringify_postorder(sb8_t *sb, ui_box_t *box) {
sb8_printf(sb, "%S", box->string);
}
void ui_test_iterators(void) {
ma_temp_t scratch = ma_begin_scratch();
ui_init(scratch.arena);
defer_block(ui_begin_build(), ui_end_build()) {
defer_block(ui_push_expander("app_event_t"), ui_pop_parent()) {
defer_block(ui_push_expander("mouse_wheel_delta: v3f64_t"), ui_pop_parent()) {
ui_label("x: f64 = value");
ui_label("y: f64 = value");
ui_label("z: f64 = value");
}
ui_label("kind: app_event_kind_t = value");
ui_label("ctrl: b8 = value");
ui_label("shift: b8 = value");
defer_block(ui_push_expander("pos: v2f64_t"), ui_pop_parent()) {
defer_block(ui_push_expander("inner_pos: v2f64_t"), ui_pop_parent()) {
ui_label("x: f64 = value");
ui_label("y: f64 = value");
}
ui_label("y: f64 = value");
defer_block(ui_push_expander("inner_pos: v2f64_t"), ui_pop_parent()) {
ui_label("x: f64 = value");
ui_label("y: f64 = value");
}
}
ui_label("alt: b8 = value");
ui_label("meta: b8 = value");
}
}
{
sb8_t *sb = sb8_serial_begin(scratch.arena);
ui_test_stringify_preorder(sb, ui->root);
s8_t recursive_string = sb8_serial_end(scratch.arena, sb);
sb = sb8_serial_begin(scratch.arena);
for (ui_preorder_iter_t it = ui_iterate_preorder(ui->root); ui_preorder_iter_is_valid(it); ui_iter_advance_preorder(&it)) {
sb8_printf(sb, "%S", it.box->string);
}
s8_t iter_string = sb8_serial_end(scratch.arena, sb);
assert(s8_equal(iter_string, recursive_string));
}
{
sb8_t *sb = sb8_serial_begin(scratch.arena);
ui_test_stringify_postorder(sb, ui->root);
s8_t recursive_string = sb8_serial_end(scratch.arena, sb);
sb = sb8_serial_begin(scratch.arena);
for (ui_postorder_iter_t it = ui_iterate_postorder(ui->root); ui_postorder_iter_is_valid(it); ui_iter_advance_postorder(&it)) {
sb8_printf(sb, "%S", it.box->string);
}
s8_t iter_string = sb8_serial_end(scratch.arena, sb);
assert(s8_equal(iter_string, recursive_string));
}
ui = NULL;
ma_end_scratch(scratch);
}
void ui_end_frame(void) {
for (i32 i = 0; i < ui->allocated_boxes; i += 1) {
ui_box_t *it = ui->box_array + i;
@@ -277,8 +252,9 @@ void ui_end_frame(void) {
}
}
#if 0
void ui_draw(gfx_t *gfx) {
void ui_draw(void) {
rn_font_t *font = &rn_state.main_font;
// compute standalone sizes: (pixels, text_content)
for (ui_preorder_iter_t it = ui_iterate_preorder(ui->root); ui_preorder_iter_is_valid(it); ui_iter_advance_preorder(&it)) {
ui_box_t *box = it.box;
@@ -287,29 +263,75 @@ void ui_draw(gfx_t *gfx) {
if (sem.kind == ui_size_kind_pixels) {
box->computed_size[i] = sem.value;
} else if (sem.kind == ui_size_kind_text_content) {
box->computed_size[i] = measure_text_ex(box->string);
box->computed_size[i] = rn_measure_string(font, box->string).e[i];
}
}
}
// compute: (percent_of_parent)
// for (ui_preorder_iter_t it = ui_iterate_preorder(ui->root); ui_preorder_iter_is_valid(it); ui_iter_advance_preorder(&it)) {
// ui_box_t *box = it.box;
// for (i32 i = 0; i < ui_axis2_count; i += 1) {
// ui_size_t sem = box->semantic_size[i];
// if (sem.kind == ui_size_kind_percent_of_parent) {
// }
// }
// }
for (ui_preorder_iter_t it = ui_iterate_preorder(ui->root); ui_preorder_iter_is_valid(it); ui_iter_advance_preorder(&it)) {
ui_box_t *box = it.box;
ui_box_t *parent = box->parent;
for (i32 i = 0; i < ui_axis2_count; i += 1) {
ui_size_t sem = box->semantic_size[i];
if (sem.kind == ui_size_kind_percent_of_parent) {
assert(sem.value >= 0 && sem.value <= 1.0);
assert(parent->semantic_size[i].kind == ui_size_kind_pixels || parent->semantic_size[i].kind == ui_size_kind_text_content);
box->computed_size[i] = sem.value * parent->computed_size[i];
}
}
}
// compute: (children_sum)
for (ui_postorder_iter_t it = ui_iterate_postorder(ui->root); ui_postorder_iter_is_valid(it); ui_iter_advance_postorder(&it)) {
ui_box_t *box = it.box;
for (i32 i = 0; i < ui_axis2_count; i += 1) {
ui_size_t sem = box->semantic_size[i];
if (sem.kind != ui_size_kind_children_sum) continue;
b32 grow_axis = box->grow_axis[i];
for (ui_box_t *child = box->first; child; child = child->next) {
assert(child->computed_size[i] != 0.f);
if (grow_axis) {
box->computed_size[i] += child->computed_size[i];
} else {
box->computed_size[i] = MAX(box->computed_size[i], child->computed_size[i]);
}
}
}
}
// solve violations
// compute relative positions
v2f32_t pos = {0,0};
for (ui_preorder_iter_t it = ui_iterate_preorder(ui->root); ui_preorder_iter_is_valid(it); ui_iter_advance_preorder(&it)) {
ui_box_t *box = it.box;
ui_box_t *parent = box->parent;
if (box == ui->root) continue;
for (i32 i = 0; i < ui_axis2_count; i += 1) {
f32 *pos = &box->computed_rel_pos[i];
f32 size = box->computed_size[i];
f32 parent_pos = parent->computed_rel_pos[i];
f32 *iter_pos = &parent->iter_pos[i];
*pos = parent_pos + *iter_pos;
if (parent->grow_axis[i]) *iter_pos += size;
}
v2f32_t pos = v2f32(box->computed_rel_pos[0], box->computed_rel_pos[1]);
v2f32_t size = v2f32(box->computed_size[0], box->computed_size[1]);
box->rect = r2f32_mindim(pos, size);
}
for (ui_preorder_iter_t it = ui_iterate_preorder(ui->root); ui_preorder_iter_is_valid(it); ui_iter_advance_preorder(&it)) {
ui_box_t *box = it.box;
rn_draw_rect(box->rect, primary_color_global);
rn_draw_string(font, box->rect.min, black_color_global, box->string);
}
}
#endif