ui
This commit is contained in:
@@ -110,13 +110,13 @@ typedef double f64;
|
|||||||
#define debug_break() (debug__break(), 0)
|
#define debug_break() (debug__break(), 0)
|
||||||
|
|
||||||
#if PLATFORM_WASM
|
#if PLATFORM_WASM
|
||||||
#define THREAD_LOCAL
|
#define gb_thread
|
||||||
#elif PLATFORM_GCC | PLATFORM_CLANG
|
#elif PLATFORM_GCC | PLATFORM_CLANG
|
||||||
#define THREAD_LOCAL __thread
|
#define gb_thread __thread
|
||||||
#elif PLATFORM_CL
|
#elif PLATFORM_CL
|
||||||
#define THREAD_LOCAL __declspec(thread)
|
#define gb_thread __declspec(thread)
|
||||||
#else
|
#else
|
||||||
#define THREAD_LOCAL _Thread_local
|
#define gb_thread _Thread_local
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if PLATFORM_CL
|
#if PLATFORM_CL
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ struct thread_ctx_t {
|
|||||||
logger_t log;
|
logger_t log;
|
||||||
};
|
};
|
||||||
|
|
||||||
THREAD_LOCAL thread_ctx_t tcx = {
|
gb_thread thread_ctx_t tcx = {
|
||||||
.log = {
|
.log = {
|
||||||
.break_on_fatal = true,
|
.break_on_fatal = true,
|
||||||
.log_proc = default_log_proc,
|
.log_proc = default_log_proc,
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
** [ ] Choosing a keying strategy from user code
|
** [ ] Choosing a keying strategy from user code
|
||||||
** [ ] Using parents
|
** [ ] Using parents
|
||||||
** [ ] Using file and line
|
** [ ] Using file and line
|
||||||
**
|
** [ ] Keyboard friendliness
|
||||||
**
|
** [ ] ui_em size
|
||||||
**
|
**
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -117,10 +117,10 @@ gb ui_t *ui = NULL;
|
|||||||
gb_read_only ui_id_t ui_id_null;
|
gb_read_only ui_id_t ui_id_null;
|
||||||
gb_read_only ui_box_t ui_box_null;
|
gb_read_only ui_box_t ui_box_null;
|
||||||
|
|
||||||
fn b32 ui_null_id(ui_id_t id) { return id.value == 0; }
|
fn b32 ui_is_null_id(ui_id_t id) { return id.value == 0; }
|
||||||
fn b32 ui_null(ui_box_t *box) { return box->id.value == 0; }
|
fn b32 ui_is_null_box(ui_box_t *box) { return box->id.value == 0; }
|
||||||
fn b32 ui_hot(ui_box_t *box) { return !ui_null(box) && box->id.value == ui->hot.value; }
|
fn b32 ui_is_hot_box(ui_box_t *box) { return !ui_is_null_box(box) && box->id.value == ui->hot.value; }
|
||||||
fn b32 ui_active(ui_box_t *box) { return !ui_null(box) && box->id.value == ui->active.value; }
|
fn b32 ui_is_active_box(ui_box_t *box) { return !ui_is_null_box(box) && box->id.value == ui->active.value; }
|
||||||
|
|
||||||
#define ev_left(ev) ((ev)->mouse_button == app_mouse_button_left)
|
#define ev_left(ev) ((ev)->mouse_button == app_mouse_button_left)
|
||||||
#define ev_left_up(ev) ((ev)->kind == app_event_kind_mouse_up && ev_left(ev))
|
#define ev_left_up(ev) ((ev)->kind == app_event_kind_mouse_up && ev_left(ev))
|
||||||
@@ -149,20 +149,23 @@ s8_t ui_get_hash_string(s8_t string) {
|
|||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// @todo: we are not building a hierarchy, it's flat ! so this doesn't work!!
|
|
||||||
fn ui_id_t ui_id_from_string(s8_t string) {
|
fn ui_id_t ui_id_from_string(s8_t string) {
|
||||||
u64 value = ht_hash_data(ui_get_hash_string(string));
|
u64 value = ht_hash_data(ui_get_hash_string(string));
|
||||||
ui_id_t id = {value};
|
ui_id_t id = {value};
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ui_id_t ui_id_from_id_stack(void) {
|
fn ui_id_t ui_find_valid_id(ui_box_t *box) {
|
||||||
u64 value = 134;
|
for (ui_box_t *it = box; it; it = it->parent) {
|
||||||
for (i32 i = 0; i < ui->id_stack.len; i += 1) {
|
if (!ui_is_null_box(it)) return it->id;
|
||||||
value = ui_hash_mix(value, ui->id_stack.data[i].value);
|
|
||||||
}
|
}
|
||||||
ui_id_t id = {value};
|
return ui_id_null;
|
||||||
return id;
|
}
|
||||||
|
|
||||||
|
fn ui_id_t ui_id_from_parent(void) {
|
||||||
|
ui_id_t parent_id = ui_find_valid_id(ui->top);
|
||||||
|
if (ui_is_null_id(parent_id)) parent_id.value = 1423;
|
||||||
|
return parent_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ui_box_t *ui_find_box(ui_id_t id) {
|
fn ui_box_t *ui_find_box(ui_id_t id) {
|
||||||
@@ -191,13 +194,19 @@ fn void ui_push_box(ui_box_t *box) {
|
|||||||
DLLQ_APPEND(ui->top->first, ui->top->last, box);
|
DLLQ_APPEND(ui->top->first, ui->top->last, box);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn s8_t ui_tprint_loc(ui_code_loc_t loc) {
|
||||||
|
return s8_printf(tcx.temp, "%s(%d)", loc.file, loc.line);
|
||||||
|
}
|
||||||
|
|
||||||
fn ui_box_t *ui_build_box_from_id(ui_code_loc_t loc, ui_id_t id) {
|
fn ui_box_t *ui_build_box_from_id(ui_code_loc_t loc, ui_id_t id) {
|
||||||
ui_box_t *box = ui_find_box(id);
|
ui_box_t *box = ui_find_box(id);
|
||||||
if (box) {
|
if (box) {
|
||||||
|
expect (box->last_touched_event_id != ui->event->id) {
|
||||||
|
fatalf("likely ui id collision between: %S and %S", ui_tprint_loc(loc), ui_tprint_loc(box->loc));
|
||||||
|
}
|
||||||
memory_zero(box, offsetof(ui_box_t, id));
|
memory_zero(box, offsetof(ui_box_t, id));
|
||||||
} else {
|
} else {
|
||||||
box = ui_alloc_box();
|
box = ui_alloc_box();
|
||||||
// @todo: we can't really make sure ids don't repeat can't we?
|
|
||||||
DLLQ_APPEND_EX(ui->hash_first, ui->hash_last, box, hash_next, hash_prev);
|
DLLQ_APPEND_EX(ui->hash_first, ui->hash_last, box, hash_next, hash_prev);
|
||||||
}
|
}
|
||||||
box->loc = loc;
|
box->loc = loc;
|
||||||
@@ -217,7 +226,7 @@ fn ui_box_t *ui_build_box_from_string(ui_code_loc_t loc, s8_t string) {
|
|||||||
id.value = ui_hash_mix(file_hash, line_hash);
|
id.value = ui_hash_mix(file_hash, line_hash);
|
||||||
id.value = ui_hash_mix(id.value, cont_hash);
|
id.value = ui_hash_mix(id.value, cont_hash);
|
||||||
} else if (ui->id_strategy == ui_id_strategy_hierarchy) {
|
} else if (ui->id_strategy == ui_id_strategy_hierarchy) {
|
||||||
ui_id_t id_from_stack = ui_id_from_id_stack();
|
ui_id_t id_from_stack = ui_id_from_parent();
|
||||||
id.value = ui_hash_mix(string_id.value, id_from_stack.value);
|
id.value = ui_hash_mix(string_id.value, id_from_stack.value);
|
||||||
} else_is_invalid;
|
} else_is_invalid;
|
||||||
|
|
||||||
@@ -233,21 +242,21 @@ fn ui_signal_t ui_signal_from_box(ui_box_t *box) {
|
|||||||
b32 move = ev->kind == app_event_kind_mouse_move;
|
b32 move = ev->kind == app_event_kind_mouse_move;
|
||||||
b32 inside = r2f32_contains(box->rect, ev->mouse_pos);
|
b32 inside = r2f32_contains(box->rect, ev->mouse_pos);
|
||||||
|
|
||||||
if (ui_active(box)) {
|
if (ui_is_active_box(box)) {
|
||||||
if (ev_left_up(ev)) {
|
if (ev_left_up(ev)) {
|
||||||
if (ui_hot(box)) {
|
if (ui_is_hot_box(box)) {
|
||||||
result.clicked = true;
|
result.clicked = true;
|
||||||
} else {
|
} else {
|
||||||
ui->active.value = 0;
|
ui->active.value = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (ui_hot(box) && ev_left_down(ev)) {
|
} else if (ui_is_hot_box(box) && ev_left_down(ev)) {
|
||||||
ui->active = box->id;
|
ui->active = box->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inside) {
|
if (inside) {
|
||||||
ui->hot.value = box->id.value;
|
ui->hot.value = box->id.value;
|
||||||
} else if (!inside && ui_hot(box)) {
|
} else if (!inside && ui_is_hot_box(box)) {
|
||||||
ui->hot = ui_id_null;
|
ui->hot = ui_id_null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -352,14 +361,14 @@ fn ui_signal_t ui_push_exp(ui_code_loc_t loc, char *str, ...) {
|
|||||||
|
|
||||||
|
|
||||||
if (signal.clicked) {
|
if (signal.clicked) {
|
||||||
|
ui->indent_stack += 1;
|
||||||
STACK_PUSH(ui->id_stack, box->id);
|
STACK_PUSH(ui->id_stack, box->id);
|
||||||
ui_push_indent();
|
|
||||||
}
|
}
|
||||||
return signal;
|
return signal;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn void ui_pop_exp(void) {
|
fn void ui_pop_exp(void) {
|
||||||
ui_pop_indent();
|
ui->indent_stack -= 1;
|
||||||
STACK_POP(ui->id_stack);
|
STACK_POP(ui->id_stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -549,10 +558,10 @@ fn void ui_draw(void) {
|
|||||||
ui_box_t *box = it.box;
|
ui_box_t *box = it.box;
|
||||||
|
|
||||||
v4f32_t rect_color = primary_color_global;
|
v4f32_t rect_color = primary_color_global;
|
||||||
if (ui_hot(box)) {
|
if (ui_is_hot_box(box)) {
|
||||||
rect_color = secondary_color_global;
|
rect_color = secondary_color_global;
|
||||||
}
|
}
|
||||||
if (ui_active(box)) {
|
if (ui_is_active_box(box)) {
|
||||||
rect_color = accent1_color_global;
|
rect_color = accent1_color_global;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user