orca/src/ui/ui.c

2821 lines
78 KiB
C
Raw Normal View History

2023-05-17 13:48:25 +00:00
/************************************************************//**
*
* @file: ui.c
* @author: Martin Fouilleul
* @date: 08/08/2022
* @revision:
*
*****************************************************************/
2023-07-15 07:15:12 +00:00
#include"platform/platform.h"
This commit restructures the codebase to melt the milepost platform layer into the main orca codebase. Here's a list of commits squashed in this update: - move angle files to ext/ and pull includes/libs from there when needed - remove milepost/ext/angle_headers - Collapsed milepost/ext into ext - Collapse milepost/scripts into scripts/ - collapse milepost/resources into resources/. WARN: this temporarily breaks milepost's native examples - collapse milepost/test into test/ - renamed test/ to tests/ - build milepost directly into build/bin - remove unused GLES and KHR folders from sdk/ - reorganizing milepost directory tree into app, graphics, platfrom, ui, util - collapse milepost/src to src/ - Move all native examples to sketches/ and remove milepost repo - Moving sketches resources into their own shared folder separate from the runtime's resource folder - Moving all binding code to src/wasmbind - Moving all binding code to src/wasmbind - pong: fix typo in error log - fixing log parameter order - add error logs to mg_image_create_* - Fix build scripts on windows - fixed include mistake after rebase - collapsing milepost.{h|c|m} to orca.{h|c|m} and moving from sdk/ to src/ - rename orca_app.h/main.c to runtime.h/c - collapsed sdk/ to src/ - put old sdk files into their respective dirs - renamed canvas_api.json to surface_api.json - moved all stb headers in ext/stb/ - remove unused OpenSansLatinSubset.ttf font - moving cstdlib to src/wasmlibc and removing some duplicates with src/platform - move libc stdarg and string functions from platform/ to wasmlibc/ - rename wasmlibc to libc-shim to reflect non-completeness - Expose abort/assert variadic functions and macros to orca apps, and forward libc-shim abort/assert to these - move Orca API runtime implementations to runtime_xxx - fix missing math constants when including math.h with msvc in graphics_common.c - Change name of runtime to orca_runtime. When bundling on Windows, change name of runtime executable to the name of the app.
2023-08-09 11:06:32 +00:00
#include"platform/platform_debug.h"
2023-07-15 07:15:12 +00:00
#include"platform/platform_clock.h"
#include"util/memory.h"
#include"util/hash.h"
2023-05-17 13:48:25 +00:00
#include"ui.h"
static oc_ui_style OC_UI_STYLE_DEFAULTS =
2023-05-17 13:48:25 +00:00
{
.size.width = {.kind = OC_UI_SIZE_CHILDREN,
2023-05-17 13:48:25 +00:00
.value = 0,
.relax = 0},
.size.height = {.kind = OC_UI_SIZE_CHILDREN,
2023-05-17 13:48:25 +00:00
.value = 0,
.relax = 0},
.layout = {.axis = OC_UI_AXIS_Y,
.align = {OC_UI_ALIGN_START,
OC_UI_ALIGN_START}},
2023-05-17 13:48:25 +00:00
.color = {0, 0, 0, 1},
.fontSize = 16,
};
oc_thread_local oc_ui_context oc_uiThreadContext = {0};
oc_thread_local oc_ui_context* oc_uiCurrentContext = 0;
2023-05-17 13:48:25 +00:00
oc_ui_context* oc_ui_get_context(void)
2023-05-17 13:48:25 +00:00
{
return(oc_uiCurrentContext);
2023-05-17 13:48:25 +00:00
}
void oc_ui_set_context(oc_ui_context* context)
2023-05-17 13:48:25 +00:00
{
oc_uiCurrentContext = context;
2023-05-17 13:48:25 +00:00
}
//-----------------------------------------------------------------------------
// stacks
//-----------------------------------------------------------------------------
oc_ui_stack_elt* oc_ui_stack_push(oc_ui_context* ui, oc_ui_stack_elt** stack)
2023-05-17 13:48:25 +00:00
{
oc_ui_stack_elt* elt = oc_arena_push_type(&ui->frameArena, oc_ui_stack_elt);
memset(elt, 0, sizeof(oc_ui_stack_elt));
2023-05-17 13:48:25 +00:00
elt->parent = *stack;
*stack = elt;
return(elt);
}
void oc_ui_stack_pop(oc_ui_stack_elt** stack)
2023-05-17 13:48:25 +00:00
{
if(*stack)
{
*stack = (*stack)->parent;
}
else
{
oc_log_error("ui stack underflow\n");
2023-05-17 13:48:25 +00:00
}
}
oc_rect oc_ui_intersect_rects(oc_rect lhs, oc_rect rhs)
2023-05-17 13:48:25 +00:00
{
//NOTE(martin): intersect with current clip
f32 x0 = oc_max(lhs.x, rhs.x);
f32 y0 = oc_max(lhs.y, rhs.y);
f32 x1 = oc_min(lhs.x + lhs.w, rhs.x + rhs.w);
f32 y1 = oc_min(lhs.y + lhs.h, rhs.y + rhs.h);
oc_rect r = {x0, y0, oc_max(0, x1-x0), oc_max(0, y1-y0)};
2023-05-17 13:48:25 +00:00
return(r);
}
oc_rect oc_ui_clip_top(void)
2023-05-17 13:48:25 +00:00
{
oc_rect r = {-FLT_MAX/2, -FLT_MAX/2, FLT_MAX, FLT_MAX};
oc_ui_context* ui = oc_ui_get_context();
oc_ui_stack_elt* elt = ui->clipStack;
2023-05-17 13:48:25 +00:00
if(elt)
{
r = elt->clip;
}
return(r);
}
void oc_ui_clip_push(oc_rect clip)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
oc_rect current = oc_ui_clip_top();
oc_ui_stack_elt* elt = oc_ui_stack_push(ui, &ui->clipStack);
elt->clip = oc_ui_intersect_rects(current, clip);
2023-05-17 13:48:25 +00:00
}
void oc_ui_clip_pop(void)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
oc_ui_stack_pop(&ui->clipStack);
2023-05-17 13:48:25 +00:00
}
oc_ui_box* oc_ui_box_top(void)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
oc_ui_stack_elt* elt = ui->boxStack;
oc_ui_box* box = elt ? elt->box : 0;
2023-05-17 13:48:25 +00:00
return(box);
}
void oc_ui_box_push(oc_ui_box* box)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
oc_ui_stack_elt* elt = oc_ui_stack_push(ui, &ui->boxStack);
2023-05-17 13:48:25 +00:00
elt->box = box;
if(box->flags & OC_UI_FLAG_CLIP)
2023-05-17 13:48:25 +00:00
{
oc_ui_clip_push(box->rect);
2023-05-17 13:48:25 +00:00
}
}
void oc_ui_box_pop(void)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
oc_ui_box* box = oc_ui_box_top();
2023-05-17 13:48:25 +00:00
if(box)
{
if(box->flags & OC_UI_FLAG_CLIP)
2023-05-17 13:48:25 +00:00
{
oc_ui_clip_pop();
2023-05-17 13:48:25 +00:00
}
oc_ui_stack_pop(&ui->boxStack);
2023-05-17 13:48:25 +00:00
}
}
//-----------------------------------------------------------------------------
// tagging
//-----------------------------------------------------------------------------
oc_ui_tag oc_ui_tag_make_str8(oc_str8 string)
2023-05-17 13:48:25 +00:00
{
oc_ui_tag tag = {.hash = oc_hash_xx64_string(string)};
2023-05-17 13:48:25 +00:00
return(tag);
}
void oc_ui_tag_box_str8(oc_ui_box* box, oc_str8 string)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
oc_ui_tag_elt* elt = oc_arena_push_type(&ui->frameArena, oc_ui_tag_elt);
elt->tag = oc_ui_tag_make_str8(string);
oc_list_append(&box->tags, &elt->listElt);
2023-05-17 13:48:25 +00:00
}
void oc_ui_tag_next_str8(oc_str8 string)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
oc_ui_tag_elt* elt = oc_arena_push_type(&ui->frameArena, oc_ui_tag_elt);
elt->tag = oc_ui_tag_make_str8(string);
oc_list_append(&ui->nextBoxTags, &elt->listElt);
2023-05-17 13:48:25 +00:00
}
//-----------------------------------------------------------------------------
// key hashing and caching
//-----------------------------------------------------------------------------
oc_ui_key oc_ui_key_make_str8(oc_str8 string)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
u64 seed = 0;
oc_ui_box* parent = oc_ui_box_top();
2023-05-17 13:48:25 +00:00
if(parent)
{
seed = parent->key.hash;
}
oc_ui_key key = {0};
key.hash = oc_hash_xx64_string_seed(string, seed);
2023-05-17 13:48:25 +00:00
return(key);
}
oc_ui_key oc_ui_key_make_path(oc_str8_list path)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
u64 seed = 0;
oc_ui_box* parent = oc_ui_box_top();
2023-05-17 13:48:25 +00:00
if(parent)
{
seed = parent->key.hash;
}
oc_list_for(&path.list, elt, oc_str8_elt, listElt)
2023-05-17 13:48:25 +00:00
{
seed = oc_hash_xx64_string_seed(elt->string, seed);
2023-05-17 13:48:25 +00:00
}
oc_ui_key key = {seed};
2023-05-17 13:48:25 +00:00
return(key);
}
bool oc_ui_key_equal(oc_ui_key a, oc_ui_key b)
2023-05-17 13:48:25 +00:00
{
return(a.hash == b.hash);
}
void oc_ui_box_cache(oc_ui_context* ui, oc_ui_box* box)
2023-05-17 13:48:25 +00:00
{
u64 index = box->key.hash & (OC_UI_BOX_MAP_BUCKET_COUNT-1);
oc_list_append(&(ui->boxMap[index]), &box->bucketElt);
2023-05-17 13:48:25 +00:00
}
oc_ui_box* oc_ui_box_lookup_key(oc_ui_key key)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
u64 index = key.hash & (OC_UI_BOX_MAP_BUCKET_COUNT-1);
2023-05-17 13:48:25 +00:00
oc_list_for(&ui->boxMap[index], box, oc_ui_box, bucketElt)
2023-05-17 13:48:25 +00:00
{
if(oc_ui_key_equal(key, box->key))
2023-05-17 13:48:25 +00:00
{
return(box);
}
}
return(0);
}
oc_ui_box* oc_ui_box_lookup_str8(oc_str8 string)
2023-05-17 13:48:25 +00:00
{
oc_ui_key key = oc_ui_key_make_str8(string);
return(oc_ui_box_lookup_key(key));
2023-05-17 13:48:25 +00:00
}
//-----------------------------------------------------------------------------
// styling
//-----------------------------------------------------------------------------
void oc_ui_pattern_push(oc_arena* arena, oc_ui_pattern* pattern, oc_ui_selector selector)
2023-05-17 13:48:25 +00:00
{
oc_ui_selector* copy = oc_arena_push_type(arena, oc_ui_selector);
2023-05-17 13:48:25 +00:00
*copy = selector;
oc_list_append(&pattern->l, &copy->listElt);
2023-05-17 13:48:25 +00:00
}
oc_ui_pattern oc_ui_pattern_all(void)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
oc_ui_pattern pattern = {0};
oc_ui_pattern_push(&ui->frameArena, &pattern, (oc_ui_selector){.kind = OC_UI_SEL_ANY});
2023-05-17 13:48:25 +00:00
return(pattern);
}
oc_ui_pattern oc_ui_pattern_owner(void)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
oc_ui_pattern pattern = {0};
oc_ui_pattern_push(&ui->frameArena, &pattern, (oc_ui_selector){.kind = OC_UI_SEL_OWNER});
2023-05-17 13:48:25 +00:00
return(pattern);
}
void oc_ui_style_match_before(oc_ui_pattern pattern, oc_ui_style* style, oc_ui_style_mask mask)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
if(ui)
{
oc_ui_style_rule* rule = oc_arena_push_type(&ui->frameArena, oc_ui_style_rule);
2023-05-17 13:48:25 +00:00
rule->pattern = pattern;
rule->mask = mask;
rule->style = oc_arena_push_type(&ui->frameArena, oc_ui_style);
2023-05-17 13:48:25 +00:00
*rule->style = *style;
oc_list_append(&ui->nextBoxBeforeRules, &rule->boxElt);
2023-05-17 13:48:25 +00:00
}
}
void oc_ui_style_match_after(oc_ui_pattern pattern, oc_ui_style* style, oc_ui_style_mask mask)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
if(ui)
{
oc_ui_style_rule* rule = oc_arena_push_type(&ui->frameArena, oc_ui_style_rule);
2023-05-17 13:48:25 +00:00
rule->pattern = pattern;
rule->mask = mask;
rule->style = oc_arena_push_type(&ui->frameArena, oc_ui_style);
2023-05-17 13:48:25 +00:00
*rule->style = *style;
oc_list_append(&ui->nextBoxAfterRules, &rule->boxElt);
2023-05-17 13:48:25 +00:00
}
}
void oc_ui_style_next(oc_ui_style* style, oc_ui_style_mask mask)
2023-05-17 13:48:25 +00:00
{
oc_ui_style_match_before(oc_ui_pattern_owner(), style, mask);
2023-05-17 13:48:25 +00:00
}
void oc_ui_style_box_before(oc_ui_box* box, oc_ui_pattern pattern, oc_ui_style* style, oc_ui_style_mask mask)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
if(ui)
{
oc_ui_style_rule* rule = oc_arena_push_type(&ui->frameArena, oc_ui_style_rule);
2023-05-17 13:48:25 +00:00
rule->pattern = pattern;
rule->mask = mask;
rule->style = oc_arena_push_type(&ui->frameArena, oc_ui_style);
2023-05-17 13:48:25 +00:00
*rule->style = *style;
oc_list_append(&box->beforeRules, &rule->boxElt);
2023-05-17 13:48:25 +00:00
rule->owner = box;
}
}
void oc_ui_style_box_after(oc_ui_box* box, oc_ui_pattern pattern, oc_ui_style* style, oc_ui_style_mask mask)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
if(ui)
{
oc_ui_style_rule* rule = oc_arena_push_type(&ui->frameArena, oc_ui_style_rule);
2023-05-17 13:48:25 +00:00
rule->pattern = pattern;
rule->mask = mask;
rule->style = oc_arena_push_type(&ui->frameArena, oc_ui_style);
2023-05-17 13:48:25 +00:00
*rule->style = *style;
oc_list_append(&box->afterRules, &rule->boxElt);
2023-05-17 13:48:25 +00:00
rule->owner = box;
}
}
//-----------------------------------------------------------------------------
// input
//-----------------------------------------------------------------------------
void oc_ui_process_event(oc_event* event)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
oc_input_process_event(&ui->input, event);
2023-05-17 13:48:25 +00:00
}
oc_vec2 oc_ui_mouse_position(void)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
oc_vec2 mousePos = oc_mouse_position(&ui->input);
2023-05-17 13:48:25 +00:00
return(mousePos);
}
oc_vec2 oc_ui_mouse_delta(void)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
oc_vec2 delta = oc_mouse_delta(&ui->input);
2023-05-17 13:48:25 +00:00
return(delta);
}
oc_vec2 oc_ui_mouse_wheel(void)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
oc_vec2 delta = oc_mouse_wheel(&ui->input);
2023-05-17 13:48:25 +00:00
return(delta);
}
//-----------------------------------------------------------------------------
// ui boxes
//-----------------------------------------------------------------------------
bool oc_ui_rect_hit(oc_rect r, oc_vec2 p)
2023-05-17 13:48:25 +00:00
{
return( (p.x > r.x)
&&(p.x < r.x + r.w)
&&(p.y > r.y)
&&(p.y < r.y + r.h));
}
bool oc_ui_box_hovering(oc_ui_box* box, oc_vec2 p)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
oc_rect clip = oc_ui_clip_top();
oc_rect rect = oc_ui_intersect_rects(clip, box->rect);
bool hit = oc_ui_rect_hit(rect, p);
2023-05-17 13:48:25 +00:00
bool result = hit && (!ui->hovered || box->z >= ui->hovered->z);
return(result);
}
oc_ui_box* oc_ui_box_make_str8(oc_str8 string, oc_ui_flags flags)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
oc_ui_key key = oc_ui_key_make_str8(string);
oc_ui_box* box = oc_ui_box_lookup_key(key);
2023-05-17 13:48:25 +00:00
if(!box)
{
box = oc_pool_alloc_type(&ui->boxPool, oc_ui_box);
memset(box, 0, sizeof(oc_ui_box));
2023-05-17 13:48:25 +00:00
box->key = key;
box->fresh = true;
oc_ui_box_cache(ui, box);
2023-05-17 13:48:25 +00:00
}
else
{
box->fresh = false;
}
//NOTE: setup hierarchy
if(box->frameCounter != ui->frameCounter)
{
oc_list_init(&box->children);
box->parent = oc_ui_box_top();
2023-05-17 13:48:25 +00:00
if(box->parent)
{
oc_list_append(&box->parent->children, &box->listElt);
2023-05-17 13:48:25 +00:00
box->parentClosed = box->parent->closed || box->parent->parentClosed;
}
if(box->flags & OC_UI_FLAG_OVERLAY)
2023-05-17 13:48:25 +00:00
{
oc_list_append(&ui->overlayList, &box->overlayElt);
2023-05-17 13:48:25 +00:00
}
}
else
{
//maybe this should be a warning that we're trying to make the box twice in the same frame?
oc_log_warning("trying to make ui box '%.*s' multiple times in the same frame\n", (int)box->string.len, box->string.ptr);
2023-05-17 13:48:25 +00:00
}
//NOTE: setup per-frame state
box->frameCounter = ui->frameCounter;
box->string = oc_str8_push_copy(&ui->frameArena, string);
2023-05-17 13:48:25 +00:00
box->flags = flags;
//NOTE: create style and setup non-inherited attributes to default values
box->targetStyle = oc_arena_push_type(&ui->frameArena, oc_ui_style);
oc_ui_apply_style_with_mask(box->targetStyle, &OC_UI_STYLE_DEFAULTS, ~0ULL);
2023-05-17 13:48:25 +00:00
//NOTE: set tags, before rules and last box
box->tags = ui->nextBoxTags;
ui->nextBoxTags = (oc_list){0};
2023-05-17 13:48:25 +00:00
box->beforeRules = ui->nextBoxBeforeRules;
oc_list_for(&box->beforeRules, rule, oc_ui_style_rule, boxElt)
2023-05-17 13:48:25 +00:00
{
rule->owner = box;
}
ui->nextBoxBeforeRules = (oc_list){0};
2023-05-17 13:48:25 +00:00
box->afterRules = ui->nextBoxAfterRules;
oc_list_for(&box->afterRules, rule, oc_ui_style_rule, boxElt)
2023-05-17 13:48:25 +00:00
{
rule->owner = box;
}
ui->nextBoxAfterRules = (oc_list){0};
2023-05-17 13:48:25 +00:00
//NOTE: set scroll
if(oc_ui_box_hovering(box, oc_ui_mouse_position()))
2023-05-17 13:48:25 +00:00
{
oc_vec2 wheel = oc_ui_mouse_wheel();
if(box->flags & OC_UI_FLAG_SCROLL_WHEEL_X)
2023-05-17 13:48:25 +00:00
{
box->scroll.x += wheel.x;
}
if(box->flags & OC_UI_FLAG_SCROLL_WHEEL_Y)
2023-05-17 13:48:25 +00:00
{
box->scroll.y += wheel.y;
}
}
return(box);
}
oc_ui_box* oc_ui_box_begin_str8(oc_str8 string, oc_ui_flags flags)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
oc_ui_box* box = oc_ui_box_make_str8(string, flags);
oc_ui_box_push(box);
2023-05-17 13:48:25 +00:00
return(box);
}
oc_ui_box* oc_ui_box_end(void)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
oc_ui_box* box = oc_ui_box_top();
OC_DEBUG_ASSERT(box, "box stack underflow");
2023-05-17 13:48:25 +00:00
oc_ui_box_pop();
2023-05-17 13:48:25 +00:00
return(box);
}
void oc_ui_box_set_draw_proc(oc_ui_box* box, oc_ui_box_draw_proc proc, void* data)
2023-05-17 13:48:25 +00:00
{
box->drawProc = proc;
box->drawData = data;
}
void oc_ui_box_set_closed(oc_ui_box* box, bool closed)
2023-05-17 13:48:25 +00:00
{
box->closed = closed;
}
bool oc_ui_box_closed(oc_ui_box* box)
2023-05-17 13:48:25 +00:00
{
return(box->closed);
}
void oc_ui_box_activate(oc_ui_box* box)
2023-05-17 13:48:25 +00:00
{
box->active = true;
}
void oc_ui_box_deactivate(oc_ui_box* box)
2023-05-17 13:48:25 +00:00
{
box->active = false;
}
bool oc_ui_box_active(oc_ui_box* box)
2023-05-17 13:48:25 +00:00
{
return(box->active);
}
void oc_ui_box_set_hot(oc_ui_box* box, bool hot)
2023-05-17 13:48:25 +00:00
{
box->hot = hot;
}
bool oc_ui_box_hot(oc_ui_box* box)
2023-05-17 13:48:25 +00:00
{
return(box->hot);
}
oc_ui_sig oc_ui_box_sig(oc_ui_box* box)
2023-05-17 13:48:25 +00:00
{
//NOTE: compute input signals
oc_ui_sig sig = {0};
2023-05-17 13:48:25 +00:00
oc_ui_context* ui = oc_ui_get_context();
oc_input_state* input = &ui->input;
2023-05-17 13:48:25 +00:00
sig.box = box;
if(!box->closed && !box->parentClosed)
{
oc_vec2 mousePos = oc_ui_mouse_position();
2023-05-17 13:48:25 +00:00
sig.mouse = (oc_vec2){mousePos.x - box->rect.x, mousePos.y - box->rect.y};
sig.delta = oc_ui_mouse_delta();
sig.wheel = oc_ui_mouse_wheel();
sig.hovering = oc_ui_box_hovering(box, mousePos);
2023-05-17 13:48:25 +00:00
if(box->flags & OC_UI_FLAG_CLICKABLE)
2023-05-17 13:48:25 +00:00
{
if(sig.hovering)
{
sig.pressed = oc_mouse_pressed(input, OC_MOUSE_LEFT);
2023-05-17 13:48:25 +00:00
if(sig.pressed)
{
2023-08-09 22:07:24 +00:00
if(!box->dragging)
{
box->pressedMouse = sig.mouse;
}
2023-05-17 13:48:25 +00:00
box->dragging = true;
}
sig.doubleClicked = oc_mouse_double_clicked(input, OC_MOUSE_LEFT);
sig.rightPressed = oc_mouse_pressed(input, OC_MOUSE_RIGHT);
2023-05-17 13:48:25 +00:00
}
sig.released = oc_mouse_released(input, OC_MOUSE_LEFT);
2023-05-17 13:48:25 +00:00
if(sig.released)
{
if(box->dragging && sig.hovering)
{
sig.clicked = true;
}
}
if(!oc_mouse_down(input, OC_MOUSE_LEFT))
2023-05-17 13:48:25 +00:00
{
box->dragging = false;
}
sig.dragging = box->dragging;
}
}
return(sig);
}
bool oc_ui_box_hidden(oc_ui_box* box)
2023-05-17 13:48:25 +00:00
{
return(box->closed || box->parentClosed);
}
//-----------------------------------------------------------------------------
// Auto-layout
//-----------------------------------------------------------------------------
void oc_ui_animate_f32(oc_ui_context* ui, f32* value, f32 target, f32 animationTime)
2023-05-17 13:48:25 +00:00
{
if( animationTime < 1e-6
|| fabs(*value - target) < 0.001)
{
*value = target;
}
else
{
/*NOTE:
we use the euler approximation for df/dt = alpha(target - f)
the implicit form is f(t) = target*(1-e^(-alpha*t)) for the rising front,
and f(t) = e^(-alpha*t) for the falling front (e.g. classic RC circuit charge/discharge)
Here we bake alpha = 1/tau = -ln(0.05)/tr, with tr the rise time to 95% of target
*/
f32 alpha = 3/animationTime;
f32 dt = ui->lastFrameDuration;
*value += (target - *value)*alpha*dt;
}
}
void oc_ui_animate_color(oc_ui_context* ui, oc_color* color, oc_color target, f32 animationTime)
2023-05-17 13:48:25 +00:00
{
for(int i=0; i<4; i++)
{
oc_ui_animate_f32(ui, &color->c[i], target.c[i], animationTime);
2023-05-17 13:48:25 +00:00
}
}
void oc_ui_animate_oc_ui_size(oc_ui_context* ui, oc_ui_size* size, oc_ui_size target, f32 animationTime)
2023-05-17 13:48:25 +00:00
{
size->kind = target.kind;
oc_ui_animate_f32(ui, &size->value, target.value, animationTime);
oc_ui_animate_f32(ui, &size->relax, target.relax, animationTime);
2023-05-17 13:48:25 +00:00
}
void oc_ui_box_animate_style(oc_ui_context* ui, oc_ui_box* box)
2023-05-17 13:48:25 +00:00
{
oc_ui_style* targetStyle = box->targetStyle;
OC_DEBUG_ASSERT(targetStyle);
2023-05-17 13:48:25 +00:00
f32 animationTime = targetStyle->animationTime;
//NOTE: interpolate based on transition values
oc_ui_style_mask mask = box->targetStyle->animationMask;
2023-05-17 13:48:25 +00:00
if(box->fresh)
{
box->style = *targetStyle;
}
else
{
if(mask & OC_UI_STYLE_SIZE_WIDTH)
2023-05-17 13:48:25 +00:00
{
oc_ui_animate_oc_ui_size(ui, &box->style.size.c[OC_UI_AXIS_X], targetStyle->size.c[OC_UI_AXIS_X], animationTime);
2023-05-17 13:48:25 +00:00
}
else
{
box->style.size.c[OC_UI_AXIS_X] = targetStyle->size.c[OC_UI_AXIS_X];
2023-05-17 13:48:25 +00:00
}
if(mask & OC_UI_STYLE_SIZE_HEIGHT)
2023-05-17 13:48:25 +00:00
{
oc_ui_animate_oc_ui_size(ui, &box->style.size.c[OC_UI_AXIS_Y], targetStyle->size.c[OC_UI_AXIS_Y], animationTime);
2023-05-17 13:48:25 +00:00
}
else
{
box->style.size.c[OC_UI_AXIS_Y] = targetStyle->size.c[OC_UI_AXIS_Y];
2023-05-17 13:48:25 +00:00
}
if(mask & OC_UI_STYLE_COLOR)
2023-05-17 13:48:25 +00:00
{
oc_ui_animate_color(ui, &box->style.color, targetStyle->color, animationTime);
2023-05-17 13:48:25 +00:00
}
else
{
box->style.color = targetStyle->color;
}
if(mask & OC_UI_STYLE_BG_COLOR)
2023-05-17 13:48:25 +00:00
{
oc_ui_animate_color(ui, &box->style.bgColor, targetStyle->bgColor, animationTime);
2023-05-17 13:48:25 +00:00
}
else
{
box->style.bgColor = targetStyle->bgColor;
}
if(mask & OC_UI_STYLE_BORDER_COLOR)
2023-05-17 13:48:25 +00:00
{
oc_ui_animate_color(ui, &box->style.borderColor, targetStyle->borderColor, animationTime);
2023-05-17 13:48:25 +00:00
}
else
{
box->style.borderColor = targetStyle->borderColor;
}
if(mask & OC_UI_STYLE_FONT_SIZE)
2023-05-17 13:48:25 +00:00
{
oc_ui_animate_f32(ui, &box->style.fontSize, targetStyle->fontSize, animationTime);
2023-05-17 13:48:25 +00:00
}
else
{
box->style.fontSize = targetStyle->fontSize;
}
if(mask & OC_UI_STYLE_BORDER_SIZE)
2023-05-17 13:48:25 +00:00
{
oc_ui_animate_f32(ui, &box->style.borderSize, targetStyle->borderSize, animationTime);
2023-05-17 13:48:25 +00:00
}
else
{
box->style.borderSize = targetStyle->borderSize;
}
if(mask & OC_UI_STYLE_ROUNDNESS)
2023-05-17 13:48:25 +00:00
{
oc_ui_animate_f32(ui, &box->style.roundness, targetStyle->roundness, animationTime);
2023-05-17 13:48:25 +00:00
}
else
{
box->style.roundness = targetStyle->roundness;
}
//NOTE: float target is animated in compute rect
box->style.floatTarget = targetStyle->floatTarget;
//TODO: non animatable attributes. use mask
box->style.layout = targetStyle->layout;
box->style.font = targetStyle->font;
}
}
void oc_ui_apply_style_with_mask(oc_ui_style* dst, oc_ui_style* src, oc_ui_style_mask mask)
2023-05-17 13:48:25 +00:00
{
if(mask & OC_UI_STYLE_SIZE_WIDTH)
2023-05-17 13:48:25 +00:00
{
dst->size.c[OC_UI_AXIS_X] = src->size.c[OC_UI_AXIS_X];
2023-05-17 13:48:25 +00:00
}
if(mask & OC_UI_STYLE_SIZE_HEIGHT)
2023-05-17 13:48:25 +00:00
{
dst->size.c[OC_UI_AXIS_Y] = src->size.c[OC_UI_AXIS_Y];
2023-05-17 13:48:25 +00:00
}
if(mask & OC_UI_STYLE_LAYOUT_AXIS)
2023-05-17 13:48:25 +00:00
{
dst->layout.axis = src->layout.axis;
}
if(mask & OC_UI_STYLE_LAYOUT_ALIGN_X)
2023-05-17 13:48:25 +00:00
{
dst->layout.align.x = src->layout.align.x;
}
if(mask & OC_UI_STYLE_LAYOUT_ALIGN_Y)
2023-05-17 13:48:25 +00:00
{
dst->layout.align.y = src->layout.align.y;
}
if(mask & OC_UI_STYLE_LAYOUT_SPACING)
2023-05-17 13:48:25 +00:00
{
dst->layout.spacing = src->layout.spacing;
}
if(mask & OC_UI_STYLE_LAYOUT_MARGIN_X)
2023-05-17 13:48:25 +00:00
{
dst->layout.margin.x = src->layout.margin.x;
}
if(mask & OC_UI_STYLE_LAYOUT_MARGIN_Y)
2023-05-17 13:48:25 +00:00
{
dst->layout.margin.y = src->layout.margin.y;
}
if(mask & OC_UI_STYLE_FLOAT_X)
2023-05-17 13:48:25 +00:00
{
dst->floating.c[OC_UI_AXIS_X] = src->floating.c[OC_UI_AXIS_X];
2023-05-17 13:48:25 +00:00
dst->floatTarget.x = src->floatTarget.x;
}
if(mask & OC_UI_STYLE_FLOAT_Y)
2023-05-17 13:48:25 +00:00
{
dst->floating.c[OC_UI_AXIS_Y] = src->floating.c[OC_UI_AXIS_Y];
2023-05-17 13:48:25 +00:00
dst->floatTarget.y = src->floatTarget.y;
}
if(mask & OC_UI_STYLE_COLOR)
2023-05-17 13:48:25 +00:00
{
dst->color = src->color;
}
if(mask & OC_UI_STYLE_BG_COLOR)
2023-05-17 13:48:25 +00:00
{
dst->bgColor = src->bgColor;
}
if(mask & OC_UI_STYLE_BORDER_COLOR)
2023-05-17 13:48:25 +00:00
{
dst->borderColor = src->borderColor;
}
if(mask & OC_UI_STYLE_BORDER_SIZE)
2023-05-17 13:48:25 +00:00
{
dst->borderSize = src->borderSize;
}
if(mask & OC_UI_STYLE_ROUNDNESS)
2023-05-17 13:48:25 +00:00
{
dst->roundness = src->roundness;
}
if(mask & OC_UI_STYLE_FONT)
2023-05-17 13:48:25 +00:00
{
dst->font = src->font;
}
if(mask & OC_UI_STYLE_FONT_SIZE)
2023-05-17 13:48:25 +00:00
{
dst->fontSize = src->fontSize;
}
if(mask & OC_UI_STYLE_ANIMATION_TIME)
2023-05-17 13:48:25 +00:00
{
dst->animationTime = src->animationTime;
}
if(mask & OC_UI_STYLE_ANIMATION_MASK)
2023-05-17 13:48:25 +00:00
{
dst->animationMask = src->animationMask;
}
}
bool oc_ui_style_selector_match(oc_ui_box* box, oc_ui_style_rule* rule, oc_ui_selector* selector)
2023-05-17 13:48:25 +00:00
{
bool res = false;
switch(selector->kind)
{
case OC_UI_SEL_ANY:
2023-05-17 13:48:25 +00:00
res = true;
break;
case OC_UI_SEL_OWNER:
2023-05-17 13:48:25 +00:00
res = (box == rule->owner);
break;
case OC_UI_SEL_TEXT:
res = !oc_str8_cmp(box->string, selector->text);
2023-05-17 13:48:25 +00:00
break;
case OC_UI_SEL_TAG:
2023-05-17 13:48:25 +00:00
{
oc_list_for(&box->tags, elt, oc_ui_tag_elt, listElt)
2023-05-17 13:48:25 +00:00
{
if(elt->tag.hash == selector->tag.hash)
{
res = true;
break;
}
}
} break;
case OC_UI_SEL_STATUS:
2023-05-17 13:48:25 +00:00
{
res = true;
if(selector->status & OC_UI_HOVER)
2023-05-17 13:48:25 +00:00
{
res = res && oc_ui_box_hovering(box, oc_ui_mouse_position());
2023-05-17 13:48:25 +00:00
}
if(selector->status & OC_UI_ACTIVE)
2023-05-17 13:48:25 +00:00
{
res = res && box->active;
}
if(selector->status & OC_UI_DRAGGING)
2023-05-17 13:48:25 +00:00
{
res = res && box->dragging;
}
} break;
case OC_UI_SEL_KEY:
res = oc_ui_key_equal(box->key, selector->key);
2023-05-17 13:48:25 +00:00
default:
break;
}
return(res);
}
void oc_ui_style_rule_match(oc_ui_context* ui, oc_ui_box* box, oc_ui_style_rule* rule, oc_list* buildList, oc_list* tmpList)
2023-05-17 13:48:25 +00:00
{
oc_ui_selector* selector = oc_list_first_entry(&rule->pattern.l, oc_ui_selector, listElt);
bool match = oc_ui_style_selector_match(box, rule, selector);
2023-05-17 13:48:25 +00:00
selector = oc_list_next_entry(&rule->pattern.l, selector, oc_ui_selector, listElt);
while(match && selector && selector->op == OC_UI_SEL_AND)
2023-05-17 13:48:25 +00:00
{
match = match && oc_ui_style_selector_match(box, rule, selector);
selector = oc_list_next_entry(&rule->pattern.l, selector, oc_ui_selector, listElt);
2023-05-17 13:48:25 +00:00
}
if(match)
{
if(!selector)
{
oc_ui_apply_style_with_mask(box->targetStyle, rule->style, rule->mask);
2023-05-17 13:48:25 +00:00
}
else
{
//NOTE create derived rule if there's more than one selector
oc_ui_style_rule* derived = oc_arena_push_type(&ui->frameArena, oc_ui_style_rule);
2023-05-17 13:48:25 +00:00
derived->mask = rule->mask;
derived->style = rule->style;
derived->pattern.l = (oc_list){&selector->listElt, rule->pattern.l.last};
2023-05-17 13:48:25 +00:00
oc_list_append(buildList, &derived->buildElt);
oc_list_append(tmpList, &derived->tmpElt);
2023-05-17 13:48:25 +00:00
}
}
}
void oc_ui_styling_prepass(oc_ui_context* ui, oc_ui_box* box, oc_list* before, oc_list* after)
2023-05-17 13:48:25 +00:00
{
//NOTE: inherit style from parent
if(box->parent)
{
oc_ui_apply_style_with_mask(box->targetStyle,
2023-05-17 13:48:25 +00:00
box->parent->targetStyle,
OC_UI_STYLE_MASK_INHERITED);
2023-05-17 13:48:25 +00:00
}
//NOTE: append box before rules to before and tmp
oc_list tmpBefore = {0};
oc_list_for(&box->beforeRules, rule, oc_ui_style_rule, boxElt)
2023-05-17 13:48:25 +00:00
{
oc_list_append(before, &rule->buildElt);
oc_list_append(&tmpBefore, &rule->tmpElt);
2023-05-17 13:48:25 +00:00
}
//NOTE: match before rules
oc_list_for(before, rule, oc_ui_style_rule, buildElt)
2023-05-17 13:48:25 +00:00
{
oc_ui_style_rule_match(ui, box, rule, before, &tmpBefore);
2023-05-17 13:48:25 +00:00
}
//NOTE: prepend box after rules to after and append them to tmp
oc_list tmpAfter = {0};
oc_list_for_reverse(&box->afterRules, rule, oc_ui_style_rule, boxElt)
2023-05-17 13:48:25 +00:00
{
oc_list_push(after, &rule->buildElt);
oc_list_append(&tmpAfter, &rule->tmpElt);
2023-05-17 13:48:25 +00:00
}
//NOTE: match after rules
oc_list_for(after, rule, oc_ui_style_rule, buildElt)
2023-05-17 13:48:25 +00:00
{
oc_ui_style_rule_match(ui, box, rule, after, &tmpAfter);
2023-05-17 13:48:25 +00:00
}
//NOTE: compute static sizes
oc_ui_box_animate_style(ui, box);
2023-05-17 13:48:25 +00:00
if(oc_ui_box_hidden(box))
2023-05-17 13:48:25 +00:00
{
return;
}
oc_ui_style* style = &box->style;
2023-05-17 13:48:25 +00:00
oc_rect textBox = {0};
oc_ui_size desiredSize[2] = {box->style.size.c[OC_UI_AXIS_X],
box->style.size.c[OC_UI_AXIS_Y]};
2023-05-17 13:48:25 +00:00
if( desiredSize[OC_UI_AXIS_X].kind == OC_UI_SIZE_TEXT
||desiredSize[OC_UI_AXIS_Y].kind == OC_UI_SIZE_TEXT)
2023-05-17 13:48:25 +00:00
{
textBox = oc_text_bounding_box(style->font, style->fontSize, box->string);
2023-05-17 13:48:25 +00:00
}
for(int i=0; i<OC_UI_AXIS_COUNT; i++)
2023-05-17 13:48:25 +00:00
{
oc_ui_size size = desiredSize[i];
2023-05-17 13:48:25 +00:00
if(size.kind == OC_UI_SIZE_TEXT)
2023-05-17 13:48:25 +00:00
{
f32 margin = style->layout.margin.c[i];
box->rect.c[2+i] = textBox.c[2+i] + margin*2;
}
else if(size.kind == OC_UI_SIZE_PIXELS)
2023-05-17 13:48:25 +00:00
{
box->rect.c[2+i] = size.value;
}
}
//NOTE: descend in children
oc_list_for(&box->children, child, oc_ui_box, listElt)
2023-05-17 13:48:25 +00:00
{
oc_ui_styling_prepass(ui, child, before, after);
2023-05-17 13:48:25 +00:00
}
//NOTE: remove temporary rules
oc_list_for(&tmpBefore, rule, oc_ui_style_rule, tmpElt)
2023-05-17 13:48:25 +00:00
{
oc_list_remove(before, &rule->buildElt);
2023-05-17 13:48:25 +00:00
}
oc_list_for(&tmpAfter, rule, oc_ui_style_rule, tmpElt)
2023-05-17 13:48:25 +00:00
{
oc_list_remove(after, &rule->buildElt);
2023-05-17 13:48:25 +00:00
}
}
bool oc_ui_layout_downward_dependency(oc_ui_box* child, int axis)
2023-05-17 13:48:25 +00:00
{
return( !oc_ui_box_hidden(child)
2023-05-17 13:48:25 +00:00
&& !child->style.floating.c[axis]
&& child->style.size.c[axis].kind != OC_UI_SIZE_PARENT
&& child->style.size.c[axis].kind != OC_UI_SIZE_PARENT_MINUS_PIXELS);
2023-05-17 13:48:25 +00:00
}
void oc_ui_layout_downward_dependent_size(oc_ui_context* ui, oc_ui_box* box, int axis)
2023-05-17 13:48:25 +00:00
{
//NOTE: layout children and compute spacing
f32 count = 0;
oc_list_for(&box->children, child, oc_ui_box, listElt)
2023-05-17 13:48:25 +00:00
{
if(!oc_ui_box_hidden(child))
2023-05-17 13:48:25 +00:00
{
oc_ui_layout_downward_dependent_size(ui, child, axis);
2023-05-17 13:48:25 +00:00
if( box->style.layout.axis == axis
&& !child->style.floating.c[axis])
{
count++;
}
}
}
box->spacing[axis] = oc_max(0, count-1)*box->style.layout.spacing;
2023-05-17 13:48:25 +00:00
oc_ui_size* size = &box->style.size.c[axis];
if(size->kind == OC_UI_SIZE_CHILDREN)
2023-05-17 13:48:25 +00:00
{
//NOTE: if box is dependent on children, compute children's size. If we're in the layout
// axis this is the sum of each child size, otherwise it is the maximum child size
f32 sum = 0;
if(box->style.layout.axis == axis)
{
oc_list_for(&box->children, child, oc_ui_box, listElt)
2023-05-17 13:48:25 +00:00
{
if(oc_ui_layout_downward_dependency(child, axis))
2023-05-17 13:48:25 +00:00
{
sum += child->rect.c[2+axis];
}
}
}
else
{
oc_list_for(&box->children, child, oc_ui_box, listElt)
2023-05-17 13:48:25 +00:00
{
if(oc_ui_layout_downward_dependency(child, axis))
2023-05-17 13:48:25 +00:00
{
sum = oc_max(sum, child->rect.c[2+axis]);
2023-05-17 13:48:25 +00:00
}
}
}
f32 margin = box->style.layout.margin.c[axis];
box->rect.c[2+axis] = sum + box->spacing[axis] + 2*margin;
}
}
void oc_ui_layout_upward_dependent_size(oc_ui_context* ui, oc_ui_box* box, int axis)
2023-05-17 13:48:25 +00:00
{
//NOTE: re-compute/set size of children that depend on box's size
f32 margin = box->style.layout.margin.c[axis];
f32 availableSize = oc_max(0, box->rect.c[2+axis] - box->spacing[axis] - 2*margin);
2023-05-17 13:48:25 +00:00
oc_list_for(&box->children, child, oc_ui_box, listElt)
2023-05-17 13:48:25 +00:00
{
oc_ui_size* size = &child->style.size.c[axis];
if(size->kind == OC_UI_SIZE_PARENT)
2023-05-17 13:48:25 +00:00
{
child->rect.c[2+axis] = availableSize * size->value;
}
else if(size->kind == OC_UI_SIZE_PARENT_MINUS_PIXELS)
2023-05-17 13:48:25 +00:00
{
child->rect.c[2+axis] = oc_max(0, availableSize - size->value);
2023-05-17 13:48:25 +00:00
}
}
//NOTE: solve downard conflicts
int overflowFlag = (OC_UI_FLAG_ALLOW_OVERFLOW_X << axis);
2023-05-17 13:48:25 +00:00
f32 sum = 0;
if(box->style.layout.axis == axis)
{
//NOTE: if we're solving in the layout axis, first compute total sum of children and
// total slack available
f32 slack = 0;
oc_list_for(&box->children, child, oc_ui_box, listElt)
2023-05-17 13:48:25 +00:00
{
if( !oc_ui_box_hidden(child)
2023-05-17 13:48:25 +00:00
&& !child->style.floating.c[axis])
{
sum += child->rect.c[2+axis];
slack += child->rect.c[2+axis] * child->style.size.c[axis].relax;
}
}
if(!(box->flags & overflowFlag))
{
//NOTE: then remove excess proportionally to each box slack, and recompute children sum.
f32 totalContents = sum + box->spacing[axis] + 2*box->style.layout.margin.c[axis];
f32 excess = oc_clamp_low(totalContents - box->rect.c[2+axis], 0);
f32 alpha = oc_clamp(excess / slack, 0, 1);
2023-05-17 13:48:25 +00:00
sum = 0;
oc_list_for(&box->children, child, oc_ui_box, listElt)
2023-05-17 13:48:25 +00:00
{
f32 relax = child->style.size.c[axis].relax;
child->rect.c[2+axis] -= alpha * child->rect.c[2+axis] * relax;
sum += child->rect.c[2+axis];
}
}
}
else
{
//NOTE: if we're solving on the secondary axis, we remove excess to each box individually
// according to its own slack. Children sum is the maximum child size.
oc_list_for(&box->children, child, oc_ui_box, listElt)
2023-05-17 13:48:25 +00:00
{
if(!oc_ui_box_hidden(child) && !child->style.floating.c[axis])
2023-05-17 13:48:25 +00:00
{
if(!(box->flags & overflowFlag))
{
f32 totalContents = child->rect.c[2+axis] + 2*box->style.layout.margin.c[axis];
f32 excess = oc_clamp_low(totalContents - box->rect.c[2+axis], 0);
2023-05-17 13:48:25 +00:00
f32 relax = child->style.size.c[axis].relax;
child->rect.c[2+axis] -= oc_min(excess, child->rect.c[2+axis]*relax);
2023-05-17 13:48:25 +00:00
}
sum = oc_max(sum, child->rect.c[2+axis]);
2023-05-17 13:48:25 +00:00
}
}
}
box->childrenSum[axis] = sum;
//NOTE: recurse in children
oc_list_for(&box->children, child, oc_ui_box, listElt)
2023-05-17 13:48:25 +00:00
{
oc_ui_layout_upward_dependent_size(ui, child, axis);
2023-05-17 13:48:25 +00:00
}
}
void oc_ui_layout_compute_rect(oc_ui_context* ui, oc_ui_box* box, oc_vec2 pos)
2023-05-17 13:48:25 +00:00
{
if(oc_ui_box_hidden(box))
2023-05-17 13:48:25 +00:00
{
return;
}
box->rect.x = pos.x;
box->rect.y = pos.y;
box->z = ui->z;
ui->z++;
oc_ui_axis layoutAxis = box->style.layout.axis;
oc_ui_axis secondAxis = (layoutAxis == OC_UI_AXIS_X) ? OC_UI_AXIS_Y : OC_UI_AXIS_X;
2023-05-17 13:48:25 +00:00
f32 spacing = box->style.layout.spacing;
oc_ui_align* align = box->style.layout.align.c;
2023-05-17 13:48:25 +00:00
oc_vec2 origin = {box->rect.x,
2023-05-17 13:48:25 +00:00
box->rect.y};
oc_vec2 currentPos = origin;
2023-05-17 13:48:25 +00:00
oc_vec2 margin = {box->style.layout.margin.x,
2023-05-17 13:48:25 +00:00
box->style.layout.margin.y};
currentPos.x += margin.x;
currentPos.y += margin.y;
for(int i=0; i<OC_UI_AXIS_COUNT; i++)
2023-05-17 13:48:25 +00:00
{
if(align[i] == OC_UI_ALIGN_END)
2023-05-17 13:48:25 +00:00
{
currentPos.c[i] = origin.c[i] + box->rect.c[2+i] - (box->childrenSum[i] + box->spacing[i] + margin.c[i]);
}
}
if(align[layoutAxis] == OC_UI_ALIGN_CENTER)
2023-05-17 13:48:25 +00:00
{
currentPos.c[layoutAxis] = origin.c[layoutAxis]
+ 0.5*(box->rect.c[2+layoutAxis]
- (box->childrenSum[layoutAxis] + box->spacing[layoutAxis]));
}
currentPos.x -= box->scroll.x;
currentPos.y -= box->scroll.y;
oc_list_for(&box->children, child, oc_ui_box, listElt)
2023-05-17 13:48:25 +00:00
{
if(align[secondAxis] == OC_UI_ALIGN_CENTER)
2023-05-17 13:48:25 +00:00
{
currentPos.c[secondAxis] = origin.c[secondAxis] + 0.5*(box->rect.c[2+secondAxis] - child->rect.c[2+secondAxis]);
}
oc_vec2 childPos = currentPos;
for(int i=0; i<OC_UI_AXIS_COUNT; i++)
2023-05-17 13:48:25 +00:00
{
if(child->style.floating.c[i])
{
oc_ui_style* style = child->targetStyle;
if((child->targetStyle->animationMask & (OC_UI_STYLE_FLOAT_X << i))
2023-05-17 13:48:25 +00:00
&& !child->fresh)
{
oc_ui_animate_f32(ui, &child->floatPos.c[i], child->style.floatTarget.c[i], style->animationTime);
2023-05-17 13:48:25 +00:00
}
else
{
child->floatPos.c[i] = child->style.floatTarget.c[i];
}
childPos.c[i] = origin.c[i] + child->floatPos.c[i];
}
}
oc_ui_layout_compute_rect(ui, child, childPos);
2023-05-17 13:48:25 +00:00
if(!child->style.floating.c[layoutAxis])
{
currentPos.c[layoutAxis] += child->rect.c[2+layoutAxis] + spacing;
}
}
}
void oc_ui_layout_find_next_hovered_recursive(oc_ui_context* ui, oc_ui_box* box, oc_vec2 p)
2023-05-17 13:48:25 +00:00
{
if(oc_ui_box_hidden(box))
2023-05-17 13:48:25 +00:00
{
return;
}
bool hit = oc_ui_rect_hit(box->rect, p);
if(hit && (box->flags & OC_UI_FLAG_BLOCK_MOUSE))
2023-05-17 13:48:25 +00:00
{
ui->hovered = box;
}
if(hit || !(box->flags & OC_UI_FLAG_CLIP))
2023-05-17 13:48:25 +00:00
{
oc_list_for(&box->children, child, oc_ui_box, listElt)
2023-05-17 13:48:25 +00:00
{
oc_ui_layout_find_next_hovered_recursive(ui, child, p);
2023-05-17 13:48:25 +00:00
}
}
}
void oc_ui_layout_find_next_hovered(oc_ui_context* ui, oc_vec2 p)
2023-05-17 13:48:25 +00:00
{
ui->hovered = 0;
oc_ui_layout_find_next_hovered_recursive(ui, ui->root, p);
2023-05-17 13:48:25 +00:00
}
void oc_ui_solve_layout(oc_ui_context* ui)
2023-05-17 13:48:25 +00:00
{
oc_list beforeRules = {0};
oc_list afterRules = {0};
2023-05-17 13:48:25 +00:00
//NOTE: style and compute static sizes
oc_ui_styling_prepass(ui, ui->root, &beforeRules, &afterRules);
2023-05-17 13:48:25 +00:00
//NOTE: reparent overlay boxes
oc_list_for(&ui->overlayList, box, oc_ui_box, overlayElt)
2023-05-17 13:48:25 +00:00
{
if(box->parent)
{
oc_list_remove(&box->parent->children, &box->listElt);
oc_list_append(&ui->overlay->children, &box->listElt);
2023-05-17 13:48:25 +00:00
}
}
//NOTE: compute layout
for(int axis=0; axis<OC_UI_AXIS_COUNT; axis++)
2023-05-17 13:48:25 +00:00
{
oc_ui_layout_downward_dependent_size(ui, ui->root, axis);
oc_ui_layout_upward_dependent_size(ui, ui->root, axis);
2023-05-17 13:48:25 +00:00
}
oc_ui_layout_compute_rect(ui, ui->root, (oc_vec2){0, 0});
2023-05-17 13:48:25 +00:00
oc_vec2 p = oc_ui_mouse_position();
oc_ui_layout_find_next_hovered(ui, p);
2023-05-17 13:48:25 +00:00
}
//-----------------------------------------------------------------------------
// Drawing
//-----------------------------------------------------------------------------
void oc_ui_rectangle_fill(oc_rect rect, f32 roundness)
2023-05-17 13:48:25 +00:00
{
if(roundness)
{
oc_rounded_rectangle_fill(rect.x, rect.y, rect.w, rect.h, roundness);
2023-05-17 13:48:25 +00:00
}
else
{
oc_rectangle_fill(rect.x, rect.y, rect.w, rect.h);
2023-05-17 13:48:25 +00:00
}
}
void oc_ui_rectangle_stroke(oc_rect rect, f32 roundness)
2023-05-17 13:48:25 +00:00
{
if(roundness)
{
oc_rounded_rectangle_stroke(rect.x, rect.y, rect.w, rect.h, roundness);
2023-05-17 13:48:25 +00:00
}
else
{
oc_rectangle_stroke(rect.x, rect.y, rect.w, rect.h);
2023-05-17 13:48:25 +00:00
}
}
void oc_ui_draw_box(oc_ui_box* box)
2023-05-17 13:48:25 +00:00
{
if(oc_ui_box_hidden(box))
2023-05-17 13:48:25 +00:00
{
return;
}
oc_ui_style* style = &box->style;
2023-05-17 13:48:25 +00:00
if(box->flags & OC_UI_FLAG_CLIP)
2023-05-17 13:48:25 +00:00
{
oc_clip_push(box->rect.x, box->rect.y, box->rect.w, box->rect.h);
2023-05-17 13:48:25 +00:00
}
if(box->flags & OC_UI_FLAG_DRAW_BACKGROUND)
2023-05-17 13:48:25 +00:00
{
oc_set_color(style->bgColor);
oc_ui_rectangle_fill(box->rect, style->roundness);
2023-05-17 13:48:25 +00:00
}
if((box->flags & OC_UI_FLAG_DRAW_PROC) && box->drawProc)
2023-05-17 13:48:25 +00:00
{
box->drawProc(box, box->drawData);
}
oc_list_for(&box->children, child, oc_ui_box, listElt)
2023-05-17 13:48:25 +00:00
{
oc_ui_draw_box(child);
2023-05-17 13:48:25 +00:00
}
if(box->flags & OC_UI_FLAG_DRAW_TEXT)
2023-05-17 13:48:25 +00:00
{
oc_rect textBox = oc_text_bounding_box(style->font, style->fontSize, box->string);
2023-05-17 13:48:25 +00:00
f32 x = 0;
f32 y = 0;
switch(style->layout.align.x)
{
case OC_UI_ALIGN_START:
2023-05-17 13:48:25 +00:00
x = box->rect.x + style->layout.margin.x;
break;
case OC_UI_ALIGN_END:
2023-05-17 13:48:25 +00:00
x = box->rect.x + box->rect.w - style->layout.margin.x - textBox.w;
break;
case OC_UI_ALIGN_CENTER:
2023-05-17 13:48:25 +00:00
x = box->rect.x + 0.5*(box->rect.w - textBox.w);
break;
}
switch(style->layout.align.y)
{
case OC_UI_ALIGN_START:
2023-05-17 13:48:25 +00:00
y = box->rect.y + style->layout.margin.y - textBox.y;
break;
case OC_UI_ALIGN_END:
2023-05-17 13:48:25 +00:00
y = box->rect.y + box->rect.h - style->layout.margin.y - textBox.h + textBox.y;
break;
case OC_UI_ALIGN_CENTER:
2023-05-17 13:48:25 +00:00
y = box->rect.y + 0.5*(box->rect.h - textBox.h) - textBox.y;
break;
}
oc_set_font(style->font);
oc_set_font_size(style->fontSize);
oc_set_color(style->color);
2023-05-17 13:48:25 +00:00
oc_move_to(x, y);
oc_text_outlines(box->string);
oc_fill();
2023-05-17 13:48:25 +00:00
}
if(box->flags & OC_UI_FLAG_CLIP)
2023-05-17 13:48:25 +00:00
{
oc_clip_pop();
2023-05-17 13:48:25 +00:00
}
if(box->flags & OC_UI_FLAG_DRAW_BORDER)
2023-05-17 13:48:25 +00:00
{
oc_set_width(style->borderSize);
oc_set_color(style->borderColor);
oc_ui_rectangle_stroke(box->rect, style->roundness);
2023-05-17 13:48:25 +00:00
}
}
void oc_ui_draw()
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
//NOTE: draw
bool oldTextFlip = oc_get_text_flip();
oc_set_text_flip(false);
2023-05-17 13:48:25 +00:00
oc_ui_draw_box(ui->root);
2023-05-17 13:48:25 +00:00
oc_set_text_flip(oldTextFlip);
2023-05-17 13:48:25 +00:00
}
//-----------------------------------------------------------------------------
// frame begin/end
//-----------------------------------------------------------------------------
void oc_ui_begin_frame(oc_vec2 size, oc_ui_style* defaultStyle, oc_ui_style_mask defaultMask)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
oc_arena_clear(&ui->frameArena);
2023-05-17 13:48:25 +00:00
ui->frameCounter++;
f64 time = oc_clock_time(OC_CLOCK_MONOTONIC);
2023-05-17 13:48:25 +00:00
ui->lastFrameDuration = time - ui->frameTime;
ui->frameTime = time;
ui->clipStack = 0;
ui->z = 0;
defaultMask &= OC_UI_STYLE_COLOR
| OC_UI_STYLE_BG_COLOR
| OC_UI_STYLE_BORDER_COLOR
| OC_UI_STYLE_FONT
| OC_UI_STYLE_FONT_SIZE;
2023-05-17 13:48:25 +00:00
oc_ui_style_match_before(oc_ui_pattern_all(), defaultStyle, defaultMask);
oc_ui_style_next(&(oc_ui_style){.size.width = {OC_UI_SIZE_PIXELS, size.x},
.size.height = {OC_UI_SIZE_PIXELS, size.y}},
OC_UI_STYLE_SIZE);
2023-05-17 13:48:25 +00:00
ui->root = oc_ui_box_begin("_root_", 0);
2023-05-17 13:48:25 +00:00
oc_ui_style_mask contentStyleMask = OC_UI_STYLE_SIZE
| OC_UI_STYLE_LAYOUT
| OC_UI_STYLE_FLOAT;
2023-05-17 13:48:25 +00:00
oc_ui_style_next(&(oc_ui_style){.size.width = {OC_UI_SIZE_PARENT, 1},
.size.height = {OC_UI_SIZE_PARENT, 1},
.layout = {OC_UI_AXIS_Y, OC_UI_ALIGN_START, OC_UI_ALIGN_START},
2023-05-17 13:48:25 +00:00
.floating = {true, true},
.floatTarget = {0, 0}},
contentStyleMask);
oc_ui_box* contents = oc_ui_box_make("_contents_", 0);
2023-05-17 13:48:25 +00:00
oc_ui_style_next(&(oc_ui_style){.layout = {OC_UI_AXIS_Y, OC_UI_ALIGN_START, OC_UI_ALIGN_START},
2023-05-17 13:48:25 +00:00
.floating = {true, true},
.floatTarget = {0, 0}},
OC_UI_STYLE_LAYOUT | OC_UI_STYLE_FLOAT_X | OC_UI_STYLE_FLOAT_Y);
2023-05-17 13:48:25 +00:00
ui->overlay = oc_ui_box_make("_overlay_", 0);
ui->overlayList = (oc_list){0};
2023-05-17 13:48:25 +00:00
ui->nextBoxBeforeRules = (oc_list){0};
ui->nextBoxAfterRules = (oc_list){0};
ui->nextBoxTags = (oc_list){0};
2023-05-17 13:48:25 +00:00
oc_ui_box_push(contents);
2023-05-17 13:48:25 +00:00
}
void oc_ui_end_frame(void)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
oc_ui_box_pop();
2023-05-17 13:48:25 +00:00
oc_ui_box* box = oc_ui_box_end();
OC_DEBUG_ASSERT(box == ui->root, "unbalanced box stack");
2023-05-17 13:48:25 +00:00
//TODO: check balancing of style stacks
//NOTE: layout
oc_ui_solve_layout(ui);
2023-05-17 13:48:25 +00:00
//NOTE: prune unused boxes
for(int i=0; i<OC_UI_BOX_MAP_BUCKET_COUNT; i++)
2023-05-17 13:48:25 +00:00
{
oc_list_for_safe(&ui->boxMap[i], box, oc_ui_box, bucketElt)
2023-05-17 13:48:25 +00:00
{
if(box->frameCounter < ui->frameCounter)
{
oc_list_remove(&ui->boxMap[i], &box->bucketElt);
2023-05-17 13:48:25 +00:00
}
}
}
oc_input_next_frame(&ui->input);
2023-05-17 13:48:25 +00:00
}
//-----------------------------------------------------------------------------
// Init / cleanup
//-----------------------------------------------------------------------------
void oc_ui_init(oc_ui_context* ui)
2023-05-17 13:48:25 +00:00
{
oc_uiCurrentContext = &oc_uiThreadContext;
2023-05-17 13:48:25 +00:00
memset(ui, 0, sizeof(oc_ui_context));
oc_arena_init(&ui->frameArena);
oc_pool_init(&ui->boxPool, sizeof(oc_ui_box));
2023-05-17 13:48:25 +00:00
ui->init = true;
oc_ui_set_context(ui);
2023-05-17 13:48:25 +00:00
}
void oc_ui_cleanup(void)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
oc_arena_cleanup(&ui->frameArena);
oc_pool_cleanup(&ui->boxPool);
2023-05-17 13:48:25 +00:00
ui->init = false;
}
//-----------------------------------------------------------------------------
// label
//-----------------------------------------------------------------------------
oc_ui_sig oc_ui_label_str8(oc_str8 label)
2023-05-17 13:48:25 +00:00
{
oc_ui_style_next(&(oc_ui_style){.size.width = {OC_UI_SIZE_TEXT, 0, 0},
.size.height = {OC_UI_SIZE_TEXT, 0, 0}},
OC_UI_STYLE_SIZE_WIDTH | OC_UI_STYLE_SIZE_HEIGHT);
2023-05-17 13:48:25 +00:00
oc_ui_flags flags = OC_UI_FLAG_CLIP
| OC_UI_FLAG_DRAW_TEXT;
oc_ui_box* box = oc_ui_box_make_str8(label, flags);
2023-05-17 13:48:25 +00:00
oc_ui_sig sig = oc_ui_box_sig(box);
2023-05-17 13:48:25 +00:00
return(sig);
}
oc_ui_sig oc_ui_label(const char* label)
2023-05-17 13:48:25 +00:00
{
return(oc_ui_label_str8(OC_STR8((char*)label)));
2023-05-17 13:48:25 +00:00
}
//------------------------------------------------------------------------------
// button
//------------------------------------------------------------------------------
oc_ui_sig oc_ui_button_behavior(oc_ui_box* box)
2023-05-17 13:48:25 +00:00
{
oc_ui_sig sig = oc_ui_box_sig(box);
2023-05-17 13:48:25 +00:00
if(sig.hovering)
{
oc_ui_box_set_hot(box, true);
2023-05-17 13:48:25 +00:00
if(sig.dragging)
{
oc_ui_box_activate(box);
2023-05-17 13:48:25 +00:00
}
}
else
{
oc_ui_box_set_hot(box, false);
2023-05-17 13:48:25 +00:00
}
if(!sig.dragging)
{
oc_ui_box_deactivate(box);
2023-05-17 13:48:25 +00:00
}
return(sig);
}
oc_ui_sig oc_ui_button_str8(oc_str8 label)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
oc_ui_style defaultStyle = {.size.width = {OC_UI_SIZE_TEXT},
.size.height = {OC_UI_SIZE_TEXT},
.layout.align.x = OC_UI_ALIGN_CENTER,
.layout.align.y = OC_UI_ALIGN_CENTER,
2023-05-17 13:48:25 +00:00
.layout.margin.x = 5,
.layout.margin.y = 5,
.bgColor = {0.5, 0.5, 0.5, 1},
.borderColor = {0.2, 0.2, 0.2, 1},
.borderSize = 1,
.roundness = 10};
oc_ui_style_mask defaultMask = OC_UI_STYLE_SIZE_WIDTH
| OC_UI_STYLE_SIZE_HEIGHT
| OC_UI_STYLE_LAYOUT_MARGIN_X
| OC_UI_STYLE_LAYOUT_MARGIN_Y
| OC_UI_STYLE_LAYOUT_ALIGN_X
| OC_UI_STYLE_LAYOUT_ALIGN_Y
| OC_UI_STYLE_BG_COLOR
| OC_UI_STYLE_BORDER_COLOR
| OC_UI_STYLE_BORDER_SIZE
| OC_UI_STYLE_ROUNDNESS;
2023-05-17 13:48:25 +00:00
oc_ui_style_next(&defaultStyle, defaultMask);
2023-05-17 13:48:25 +00:00
oc_ui_style activeStyle = {.bgColor = {0.3, 0.3, 0.3, 1},
2023-05-17 13:48:25 +00:00
.borderColor = {0.2, 0.2, 0.2, 1},
.borderSize = 2};
oc_ui_style_mask activeMask = OC_UI_STYLE_BG_COLOR
| OC_UI_STYLE_BORDER_COLOR
| OC_UI_STYLE_BORDER_SIZE;
oc_ui_pattern activePattern = {0};
oc_ui_pattern_push(&ui->frameArena,
2023-05-17 13:48:25 +00:00
&activePattern,
(oc_ui_selector){.kind = OC_UI_SEL_STATUS,
.status = OC_UI_ACTIVE|OC_UI_HOVER});
oc_ui_style_match_before(activePattern, &activeStyle, activeMask);
oc_ui_flags flags = OC_UI_FLAG_CLICKABLE
| OC_UI_FLAG_CLIP
| OC_UI_FLAG_DRAW_BACKGROUND
| OC_UI_FLAG_DRAW_BORDER
| OC_UI_FLAG_DRAW_TEXT
| OC_UI_FLAG_HOT_ANIMATION
| OC_UI_FLAG_ACTIVE_ANIMATION;
oc_ui_box* box = oc_ui_box_make_str8(label, flags);
oc_ui_tag_box(box, "button");
oc_ui_sig sig = oc_ui_button_behavior(box);
2023-05-17 13:48:25 +00:00
return(sig);
}
oc_ui_sig oc_ui_button(const char* label)
2023-05-17 13:48:25 +00:00
{
return(oc_ui_button_str8(OC_STR8((char*)label)));
2023-05-17 13:48:25 +00:00
}
void oc_ui_checkbox_draw(oc_ui_box* box, void* data)
2023-05-17 13:48:25 +00:00
{
bool checked = *(bool*)data;
if(checked)
{
oc_move_to(box->rect.x + 0.2*box->rect.w, box->rect.y + 0.5*box->rect.h);
oc_line_to(box->rect.x + 0.4*box->rect.w, box->rect.y + 0.75*box->rect.h);
oc_line_to(box->rect.x + 0.8*box->rect.w, box->rect.y + 0.2*box->rect.h);
2023-05-17 13:48:25 +00:00
oc_set_color(box->style.color);
oc_set_width(0.2*box->rect.w);
oc_set_joint(OC_JOINT_MITER);
oc_set_max_joint_excursion(0.2 * box->rect.h);
oc_stroke();
2023-05-17 13:48:25 +00:00
}
}
oc_ui_sig oc_ui_checkbox(const char* name, bool* checked)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
oc_ui_style defaultStyle = {.size.width = {OC_UI_SIZE_PIXELS, 20},
.size.height = {OC_UI_SIZE_PIXELS, 20},
2023-05-17 13:48:25 +00:00
.bgColor = {1, 1, 1, 1},
.color = {0, 0, 0, 1},
.borderColor = {0.2, 0.2, 0.2, 1},
.borderSize = 1,
.roundness = 5};
oc_ui_style_mask defaultMask = OC_UI_STYLE_SIZE_WIDTH
| OC_UI_STYLE_SIZE_HEIGHT
| OC_UI_STYLE_BG_COLOR
| OC_UI_STYLE_COLOR
| OC_UI_STYLE_BORDER_COLOR
| OC_UI_STYLE_BORDER_SIZE
| OC_UI_STYLE_ROUNDNESS;
2023-05-17 13:48:25 +00:00
oc_ui_style_next(&defaultStyle, defaultMask);
2023-05-17 13:48:25 +00:00
oc_ui_style activeStyle = {.bgColor = {0.5, 0.5, 0.5, 1},
2023-05-17 13:48:25 +00:00
.borderColor = {0.2, 0.2, 0.2, 1},
.borderSize = 2};
oc_ui_style_mask activeMask = OC_UI_STYLE_BG_COLOR
| OC_UI_STYLE_BORDER_COLOR
| OC_UI_STYLE_BORDER_SIZE;
oc_ui_pattern activePattern = {0};
oc_ui_pattern_push(&ui->frameArena,
2023-05-17 13:48:25 +00:00
&activePattern,
(oc_ui_selector){.kind = OC_UI_SEL_STATUS,
.status = OC_UI_ACTIVE|OC_UI_HOVER});
oc_ui_style_match_before(activePattern, &activeStyle, activeMask);
oc_ui_flags flags = OC_UI_FLAG_CLICKABLE
| OC_UI_FLAG_CLIP
| OC_UI_FLAG_DRAW_BACKGROUND
| OC_UI_FLAG_DRAW_PROC
| OC_UI_FLAG_DRAW_BORDER
| OC_UI_FLAG_HOT_ANIMATION
| OC_UI_FLAG_ACTIVE_ANIMATION;
oc_ui_box* box = oc_ui_box_make(name, flags);
oc_ui_tag_box(box, "checkbox");
oc_ui_sig sig = oc_ui_button_behavior(box);
2023-05-17 13:48:25 +00:00
if(sig.clicked)
{
*checked = !*checked;
}
oc_ui_box_set_draw_proc(box, oc_ui_checkbox_draw, checked);
2023-05-17 13:48:25 +00:00
return(sig);
}
//------------------------------------------------------------------------------
// slider / scrollbar
//------------------------------------------------------------------------------
oc_ui_box* oc_ui_slider(const char* label, f32 thumbRatio, f32* scrollValue)
2023-05-17 13:48:25 +00:00
{
oc_ui_style_match_before(oc_ui_pattern_all(), &(oc_ui_style){0}, OC_UI_STYLE_LAYOUT);
oc_ui_box* frame = oc_ui_box_begin(label, 0);
2023-05-17 13:48:25 +00:00
{
f32 beforeRatio = (*scrollValue) * (1. - thumbRatio);
f32 afterRatio = (1. - *scrollValue) * (1. - thumbRatio);
oc_ui_axis trackAxis = (frame->rect.w > frame->rect.h) ? OC_UI_AXIS_X : OC_UI_AXIS_Y;
oc_ui_axis secondAxis = (trackAxis == OC_UI_AXIS_Y) ? OC_UI_AXIS_X : OC_UI_AXIS_Y;
2023-05-17 13:48:25 +00:00
f32 roundness = 0.5*frame->rect.c[2+secondAxis];
f32 animationTime = 0.5;
oc_ui_style trackStyle = {.size.width = {OC_UI_SIZE_PARENT, 1},
.size.height = {OC_UI_SIZE_PARENT, 1},
2023-05-17 13:48:25 +00:00
.layout.axis = trackAxis,
.layout.align.x = OC_UI_ALIGN_START,
.layout.align.y = OC_UI_ALIGN_START,
2023-05-17 13:48:25 +00:00
.bgColor = {0.5, 0.5, 0.5, 1},
.roundness = roundness};
oc_ui_style beforeStyle = trackStyle;
beforeStyle.size.c[trackAxis] = (oc_ui_size){OC_UI_SIZE_PARENT, beforeRatio};
2023-05-17 13:48:25 +00:00
oc_ui_style afterStyle = trackStyle;
afterStyle.size.c[trackAxis] = (oc_ui_size){OC_UI_SIZE_PARENT, afterRatio};
2023-05-17 13:48:25 +00:00
oc_ui_style thumbStyle = trackStyle;
thumbStyle.size.c[trackAxis] = (oc_ui_size){OC_UI_SIZE_PARENT, thumbRatio};
thumbStyle.bgColor = (oc_color){0.3, 0.3, 0.3, 1};
2023-05-17 13:48:25 +00:00
oc_ui_style_mask styleMask = OC_UI_STYLE_SIZE_WIDTH
| OC_UI_STYLE_SIZE_HEIGHT
| OC_UI_STYLE_LAYOUT
| OC_UI_STYLE_BG_COLOR
| OC_UI_STYLE_ROUNDNESS;
2023-05-17 13:48:25 +00:00
oc_ui_flags trackFlags = OC_UI_FLAG_CLIP
| OC_UI_FLAG_DRAW_BACKGROUND
| OC_UI_FLAG_HOT_ANIMATION
| OC_UI_FLAG_ACTIVE_ANIMATION;
2023-05-17 13:48:25 +00:00
oc_ui_style_next(&trackStyle, styleMask);
oc_ui_box* track = oc_ui_box_begin("track", trackFlags);
2023-05-17 13:48:25 +00:00
oc_ui_style_next(&beforeStyle, OC_UI_STYLE_SIZE_WIDTH|OC_UI_STYLE_SIZE_HEIGHT);
oc_ui_box* beforeSpacer = oc_ui_box_make("before", 0);
2023-05-17 13:48:25 +00:00
oc_ui_flags thumbFlags = OC_UI_FLAG_CLICKABLE
| OC_UI_FLAG_DRAW_BACKGROUND
| OC_UI_FLAG_HOT_ANIMATION
| OC_UI_FLAG_ACTIVE_ANIMATION;
2023-05-17 13:48:25 +00:00
oc_ui_style_next(&thumbStyle, styleMask);
oc_ui_box* thumb = oc_ui_box_make("thumb", thumbFlags);
2023-05-17 13:48:25 +00:00
oc_ui_style_next(&afterStyle, OC_UI_STYLE_SIZE_WIDTH|OC_UI_STYLE_SIZE_HEIGHT);
oc_ui_box* afterSpacer = oc_ui_box_make("after", 0);
2023-05-17 13:48:25 +00:00
oc_ui_box_end();
2023-05-17 13:48:25 +00:00
//NOTE: interaction
oc_ui_sig thumbSig = oc_ui_box_sig(thumb);
oc_ui_sig trackSig = oc_ui_box_sig(track);
2023-05-17 13:48:25 +00:00
if(thumbSig.dragging)
{
f32 trackExtents = track->rect.c[2+trackAxis] - thumb->rect.c[2+trackAxis];
2023-08-09 22:07:24 +00:00
*scrollValue = (trackSig.mouse.c[trackAxis] - thumb->pressedMouse.c[trackAxis]) / trackExtents;
*scrollValue = oc_clamp(*scrollValue, 0, 1);
2023-05-17 13:48:25 +00:00
}
if(oc_ui_box_active(frame))
2023-05-17 13:48:25 +00:00
{
//NOTE: activated from outside
oc_ui_box_set_hot(track, true);
oc_ui_box_set_hot(thumb, true);
oc_ui_box_activate(track);
oc_ui_box_activate(thumb);
2023-05-17 13:48:25 +00:00
}
if(trackSig.hovering)
{
oc_ui_box_set_hot(track, true);
oc_ui_box_set_hot(thumb, true);
2023-05-17 13:48:25 +00:00
}
else if(thumbSig.wheel.c[trackAxis] == 0)
{
oc_ui_box_set_hot(track, false);
oc_ui_box_set_hot(thumb, false);
2023-05-17 13:48:25 +00:00
}
if(thumbSig.dragging)
{
oc_ui_box_activate(track);
oc_ui_box_activate(thumb);
2023-05-17 13:48:25 +00:00
}
else if(thumbSig.wheel.c[trackAxis] == 0)
{
oc_ui_box_deactivate(track);
oc_ui_box_deactivate(thumb);
oc_ui_box_deactivate(frame);
2023-05-17 13:48:25 +00:00
}
} oc_ui_box_end();
2023-05-17 13:48:25 +00:00
return(frame);
}
//------------------------------------------------------------------------------
// panels
//------------------------------------------------------------------------------
void oc_ui_panel_begin(const char* str, oc_ui_flags flags)
2023-05-17 13:48:25 +00:00
{
flags = flags
| OC_UI_FLAG_CLIP
| OC_UI_FLAG_BLOCK_MOUSE
| OC_UI_FLAG_ALLOW_OVERFLOW_X
| OC_UI_FLAG_ALLOW_OVERFLOW_Y
| OC_UI_FLAG_SCROLL_WHEEL_X
| OC_UI_FLAG_SCROLL_WHEEL_Y;
2023-05-17 13:48:25 +00:00
oc_ui_box_begin(str, flags);
2023-05-17 13:48:25 +00:00
}
void oc_ui_panel_end(void)
2023-05-17 13:48:25 +00:00
{
oc_ui_box* panel = oc_ui_box_top();
oc_ui_sig sig = oc_ui_box_sig(panel);
2023-05-17 13:48:25 +00:00
f32 contentsW = oc_clamp_low(panel->childrenSum[0], panel->rect.w);
f32 contentsH = oc_clamp_low(panel->childrenSum[1], panel->rect.h);
2023-05-17 13:48:25 +00:00
contentsW = oc_clamp_low(contentsW, 1);
contentsH = oc_clamp_low(contentsH, 1);
2023-05-17 13:48:25 +00:00
oc_ui_box* scrollBarX = 0;
oc_ui_box* scrollBarY = 0;
2023-05-17 13:48:25 +00:00
bool needsScrollX = contentsW > panel->rect.w;
bool needsScrollY = contentsH > panel->rect.h;
if(needsScrollX)
{
f32 thumbRatioX = panel->rect.w / contentsW;
f32 sliderX = panel->scroll.x /(contentsW - panel->rect.w);
oc_ui_style_next(&(oc_ui_style){.size.width = {OC_UI_SIZE_PARENT, 1., 0},
.size.height = {OC_UI_SIZE_PIXELS, 10, 0},
2023-05-17 13:48:25 +00:00
.floating.x = true,
.floating.y = true,
.floatTarget = {0, panel->rect.h - 10}},
OC_UI_STYLE_SIZE
|OC_UI_STYLE_FLOAT);
2023-05-17 13:48:25 +00:00
scrollBarX = oc_ui_slider("scrollerX", thumbRatioX, &sliderX);
2023-05-17 13:48:25 +00:00
panel->scroll.x = sliderX * (contentsW - panel->rect.w);
if(sig.hovering)
{
oc_ui_box_activate(scrollBarX);
2023-05-17 13:48:25 +00:00
}
}
if(needsScrollY)
{
f32 thumbRatioY = panel->rect.h / contentsH;
f32 sliderY = panel->scroll.y /(contentsH - panel->rect.h);
f32 spacerSize = needsScrollX ? 10 : 0;
oc_ui_style_next(&(oc_ui_style){.size.width = {OC_UI_SIZE_PIXELS, 10, 0},
.size.height = {OC_UI_SIZE_PARENT_MINUS_PIXELS, spacerSize, 0},
2023-05-17 13:48:25 +00:00
.floating.x = true,
.floating.y = true,
.floatTarget = {panel->rect.w - 10, 0}},
OC_UI_STYLE_SIZE
|OC_UI_STYLE_FLOAT);
2023-05-17 13:48:25 +00:00
scrollBarY = oc_ui_slider("scrollerY", thumbRatioY, &sliderY);
2023-05-17 13:48:25 +00:00
panel->scroll.y = sliderY * (contentsH - panel->rect.h);
if(sig.hovering)
{
oc_ui_box_activate(scrollBarY);
2023-05-17 13:48:25 +00:00
}
}
panel->scroll.x = oc_clamp(panel->scroll.x, 0, contentsW - panel->rect.w);
panel->scroll.y = oc_clamp(panel->scroll.y, 0, contentsH - panel->rect.h);
2023-05-17 13:48:25 +00:00
oc_ui_box_end();
2023-05-17 13:48:25 +00:00
}
//------------------------------------------------------------------------------
// tooltips
//------------------------------------------------------------------------------
oc_ui_sig oc_ui_tooltip_begin(const char* name)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
oc_vec2 p = oc_ui_mouse_position();
2023-05-17 13:48:25 +00:00
oc_ui_style style = {.size.width = {OC_UI_SIZE_CHILDREN},
.size.height = {OC_UI_SIZE_CHILDREN},
2023-05-17 13:48:25 +00:00
.floating.x = true,
.floating.y = true,
.floatTarget = {p.x, p.y}};
oc_ui_style_mask mask = OC_UI_STYLE_SIZE | OC_UI_STYLE_FLOAT;
2023-05-17 13:48:25 +00:00
oc_ui_style_next(&style, mask);
2023-05-17 13:48:25 +00:00
oc_ui_flags flags = OC_UI_FLAG_OVERLAY
| OC_UI_FLAG_DRAW_BACKGROUND
| OC_UI_FLAG_DRAW_BORDER;
2023-05-17 13:48:25 +00:00
oc_ui_box* tooltip = oc_ui_box_make(name, flags);
oc_ui_box_push(tooltip);
2023-05-17 13:48:25 +00:00
return(oc_ui_box_sig(tooltip));
2023-05-17 13:48:25 +00:00
}
void oc_ui_tooltip_end(void)
2023-05-17 13:48:25 +00:00
{
oc_ui_box_pop(); // tooltip
2023-05-17 13:48:25 +00:00
}
//------------------------------------------------------------------------------
// Menus
//------------------------------------------------------------------------------
void oc_ui_menu_bar_begin(const char* name)
2023-05-17 13:48:25 +00:00
{
oc_ui_style style = {.size.width = {OC_UI_SIZE_PARENT, 1, 0},
.size.height = {OC_UI_SIZE_CHILDREN},
.layout.axis = OC_UI_AXIS_X,
2023-05-17 13:48:25 +00:00
.layout.spacing = 20,};
oc_ui_style_mask mask = OC_UI_STYLE_SIZE
| OC_UI_STYLE_LAYOUT_AXIS
| OC_UI_STYLE_LAYOUT_SPACING;
2023-05-17 13:48:25 +00:00
oc_ui_style_next(&style, mask);
oc_ui_box* bar = oc_ui_box_begin(name, OC_UI_FLAG_DRAW_BACKGROUND);
2023-05-17 13:48:25 +00:00
oc_ui_sig sig = oc_ui_box_sig(bar);
oc_ui_context* ui = oc_ui_get_context();
if(!sig.hovering && oc_mouse_released(&ui->input, OC_MOUSE_LEFT))
2023-05-17 13:48:25 +00:00
{
oc_ui_box_deactivate(bar);
2023-05-17 13:48:25 +00:00
}
}
void oc_ui_menu_bar_end(void)
2023-05-17 13:48:25 +00:00
{
oc_ui_box_end(); // menu bar
2023-05-17 13:48:25 +00:00
}
void oc_ui_menu_begin(const char* label)
2023-05-17 13:48:25 +00:00
{
oc_ui_box* container = oc_ui_box_make(label, 0);
oc_ui_box_push(container);
2023-05-17 13:48:25 +00:00
oc_ui_style_next(&(oc_ui_style){.size.width = {OC_UI_SIZE_TEXT},
.size.height = {OC_UI_SIZE_TEXT}},
OC_UI_STYLE_SIZE);
2023-05-17 13:48:25 +00:00
oc_ui_box* button = oc_ui_box_make(label, OC_UI_FLAG_CLICKABLE | OC_UI_FLAG_DRAW_TEXT);
oc_ui_box* bar = container->parent;
2023-05-17 13:48:25 +00:00
oc_ui_sig sig = oc_ui_box_sig(button);
oc_ui_sig barSig = oc_ui_box_sig(bar);
2023-05-17 13:48:25 +00:00
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
oc_ui_style style = {.size.width = {OC_UI_SIZE_CHILDREN},
.size.height = {OC_UI_SIZE_CHILDREN},
2023-05-17 13:48:25 +00:00
.floating.x = true,
.floating.y = true,
.floatTarget = {button->rect.x,
button->rect.y + button->rect.h},
.layout.axis = OC_UI_AXIS_Y,
2023-05-17 13:48:25 +00:00
.layout.spacing = 5,
.layout.margin.x = 0,
.layout.margin.y = 5,
.bgColor = {0.2, 0.2, 0.2, 1}};
oc_ui_style_mask mask = OC_UI_STYLE_SIZE
| OC_UI_STYLE_FLOAT
| OC_UI_STYLE_LAYOUT
| OC_UI_STYLE_BG_COLOR;
2023-05-17 13:48:25 +00:00
oc_ui_flags flags = OC_UI_FLAG_OVERLAY
| OC_UI_FLAG_DRAW_BACKGROUND
| OC_UI_FLAG_DRAW_BORDER;
2023-05-17 13:48:25 +00:00
oc_ui_style_next(&style, mask);
oc_ui_box* menu = oc_ui_box_make("panel", flags);
2023-05-17 13:48:25 +00:00
if(oc_ui_box_active(bar))
2023-05-17 13:48:25 +00:00
{
if(sig.hovering)
{
oc_ui_box_activate(button);
2023-05-17 13:48:25 +00:00
}
else if(barSig.hovering)
{
oc_ui_box_deactivate(button);
2023-05-17 13:48:25 +00:00
}
}
else
{
oc_ui_box_deactivate(button);
2023-05-17 13:48:25 +00:00
if(sig.pressed)
{
oc_ui_box_activate(bar);
oc_ui_box_activate(button);
2023-05-17 13:48:25 +00:00
}
}
oc_ui_box_set_closed(menu, !oc_ui_box_active(button));
oc_ui_box_push(menu);
2023-05-17 13:48:25 +00:00
}
void oc_ui_menu_end(void)
2023-05-17 13:48:25 +00:00
{
oc_ui_box_pop(); // menu
oc_ui_box_pop(); // container
2023-05-17 13:48:25 +00:00
}
oc_ui_sig oc_ui_menu_button(const char* name)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
oc_ui_style_next(&(oc_ui_style){.size.width = {OC_UI_SIZE_TEXT},
.size.height = {OC_UI_SIZE_TEXT},
2023-05-17 13:48:25 +00:00
.layout.margin.x = 5,
.bgColor = {0, 0, 0, 0}},
OC_UI_STYLE_SIZE
|OC_UI_STYLE_LAYOUT_MARGIN_X
|OC_UI_STYLE_BG_COLOR);
2023-05-17 13:48:25 +00:00
oc_ui_pattern pattern = {0};
oc_ui_pattern_push(&ui->frameArena, &pattern, (oc_ui_selector){.kind = OC_UI_SEL_STATUS, .status = OC_UI_HOVER});
2023-05-17 13:48:25 +00:00
oc_ui_style style = {.bgColor = {0, 0, 1, 1}};
oc_ui_style_mask mask = OC_UI_STYLE_BG_COLOR;
oc_ui_style_match_before(pattern, &style, mask);
2023-05-17 13:48:25 +00:00
oc_ui_flags flags = OC_UI_FLAG_CLICKABLE
| OC_UI_FLAG_CLIP
| OC_UI_FLAG_DRAW_TEXT
| OC_UI_FLAG_DRAW_BACKGROUND;
2023-05-17 13:48:25 +00:00
oc_ui_box* box = oc_ui_box_make(name, flags);
oc_ui_sig sig = oc_ui_box_sig(box);
2023-05-17 13:48:25 +00:00
return(sig);
}
void oc_ui_select_popup_draw_arrow(oc_ui_box* box, void* data)
2023-05-17 13:48:25 +00:00
{
f32 r = oc_min(box->parent->style.roundness, box->rect.w);
2023-05-17 13:48:25 +00:00
f32 cr = r*4*(sqrt(2)-1)/3;
oc_move_to(box->rect.x, box->rect.y);
oc_line_to(box->rect.x + box->rect.w - r, box->rect.y);
oc_cubic_to(box->rect.x + box->rect.w - cr, box->rect.y,
2023-05-17 13:48:25 +00:00
box->rect.x + box->rect.w, box->rect.y + cr,
box->rect.x + box->rect.w, box->rect.y + r);
oc_line_to(box->rect.x + box->rect.w, box->rect.y + box->rect.h - r);
oc_cubic_to(box->rect.x + box->rect.w, box->rect.y + box->rect.h - cr,
2023-05-17 13:48:25 +00:00
box->rect.x + box->rect.w - cr, box->rect.y + box->rect.h,
box->rect.x + box->rect.w - r, box->rect.y + box->rect.h);
oc_line_to(box->rect.x, box->rect.y + box->rect.h);
2023-05-17 13:48:25 +00:00
oc_set_color(box->style.bgColor);
oc_fill();
2023-05-17 13:48:25 +00:00
oc_move_to(box->rect.x + 0.25*box->rect.w, box->rect.y + 0.45*box->rect.h);
oc_line_to(box->rect.x + 0.5*box->rect.w, box->rect.y + 0.75*box->rect.h);
oc_line_to(box->rect.x + 0.75*box->rect.w, box->rect.y + 0.45*box->rect.h);
oc_close_path();
2023-05-17 13:48:25 +00:00
oc_set_color(box->style.color);
oc_fill();
2023-05-17 13:48:25 +00:00
}
oc_ui_select_popup_info oc_ui_select_popup(const char* name, oc_ui_select_popup_info* info)
2023-05-17 13:48:25 +00:00
{
oc_ui_select_popup_info result = *info;
2023-05-17 13:48:25 +00:00
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
oc_ui_container(name, 0)
2023-05-17 13:48:25 +00:00
{
oc_ui_box* button = oc_ui_box_make("button",
OC_UI_FLAG_CLICKABLE
|OC_UI_FLAG_DRAW_BACKGROUND
|OC_UI_FLAG_DRAW_BORDER
|OC_UI_FLAG_ALLOW_OVERFLOW_X
|OC_UI_FLAG_CLIP);
2023-05-17 13:48:25 +00:00
f32 maxOptionWidth = 0;
f32 lineHeight = 0;
oc_rect bbox = {0};
2023-05-17 13:48:25 +00:00
for(int i=0; i<info->optionCount; i++)
{
bbox = oc_text_bounding_box(button->style.font, button->style.fontSize, info->options[i]);
maxOptionWidth = oc_max(maxOptionWidth, bbox.w);
2023-05-17 13:48:25 +00:00
}
f32 buttonWidth = maxOptionWidth + 2*button->style.layout.margin.x + button->rect.h;
oc_ui_style_box_before(button,
oc_ui_pattern_owner(),
&(oc_ui_style){.size.width = {OC_UI_SIZE_PIXELS, buttonWidth},
.size.height = {OC_UI_SIZE_CHILDREN},
2023-05-17 13:48:25 +00:00
.layout.margin.x = 5,
.layout.margin.y = 1,
.roundness = 5,
.borderSize = 1,
.borderColor = {0.3, 0.3, 0.3, 1}},
OC_UI_STYLE_SIZE
|OC_UI_STYLE_LAYOUT_MARGIN_X
|OC_UI_STYLE_LAYOUT_MARGIN_Y
|OC_UI_STYLE_ROUNDNESS
|OC_UI_STYLE_BORDER_SIZE
|OC_UI_STYLE_BORDER_COLOR);
oc_ui_box_push(button);
{
oc_ui_label_str8(info->options[info->selectedIndex]);
oc_ui_style_next(&(oc_ui_style){.size.width = {OC_UI_SIZE_PIXELS, button->rect.h},
.size.height = {OC_UI_SIZE_PIXELS, button->rect.h},
2023-05-17 13:48:25 +00:00
.floating.x = true,
.floating.y = true,
.floatTarget = {button->rect.w - button->rect.h, 0},
.color = {0, 0, 0, 1},
.bgColor = {0.7, 0.7, 0.7, 1}},
OC_UI_STYLE_SIZE
|OC_UI_STYLE_FLOAT
|OC_UI_STYLE_COLOR
|OC_UI_STYLE_BG_COLOR);
2023-05-17 13:48:25 +00:00
oc_ui_box* arrow = oc_ui_box_make("arrow", OC_UI_FLAG_DRAW_PROC);
oc_ui_box_set_draw_proc(arrow, oc_ui_select_popup_draw_arrow, 0);
2023-05-17 13:48:25 +00:00
} oc_ui_box_pop();
2023-05-17 13:48:25 +00:00
//panel
oc_ui_box* panel = oc_ui_box_make("panel",
OC_UI_FLAG_DRAW_BACKGROUND
|OC_UI_FLAG_BLOCK_MOUSE
|OC_UI_FLAG_OVERLAY);
2023-05-17 13:48:25 +00:00
//TODO: set width to max(button.w, max child...)
f32 containerWidth = oc_max(maxOptionWidth + 2*panel->style.layout.margin.x,
2023-05-17 13:48:25 +00:00
button->rect.w);
oc_ui_style_box_before(panel,
oc_ui_pattern_owner(),
&(oc_ui_style){.size.width = {OC_UI_SIZE_PIXELS, containerWidth},
.size.height = {OC_UI_SIZE_CHILDREN},
2023-05-17 13:48:25 +00:00
.floating.x = true,
.floating.y = true,
.floatTarget = {button->rect.x,
button->rect.y + button->rect.h},
.layout.axis = OC_UI_AXIS_Y,
2023-05-17 13:48:25 +00:00
.layout.margin.x = 0,
.layout.margin.y = 5,
.bgColor = {0.2, 0.2, 0.2, 1}},
OC_UI_STYLE_SIZE
|OC_UI_STYLE_FLOAT
|OC_UI_STYLE_LAYOUT
|OC_UI_STYLE_BG_COLOR);
2023-05-17 13:48:25 +00:00
oc_ui_box_push(panel);
2023-05-17 13:48:25 +00:00
{
for(int i=0; i<info->optionCount; i++)
{
oc_ui_style_next(&(oc_ui_style){.size.width = {OC_UI_SIZE_PARENT, 1},
.size.height = {OC_UI_SIZE_TEXT},
.layout.axis = OC_UI_AXIS_Y,
.layout.align.x = OC_UI_ALIGN_START,
2023-05-17 13:48:25 +00:00
.layout.margin.x = 5,
.layout.margin.y = 2.5},
OC_UI_STYLE_SIZE
|OC_UI_STYLE_LAYOUT_AXIS
|OC_UI_STYLE_LAYOUT_ALIGN_X
|OC_UI_STYLE_LAYOUT_MARGIN_X
|OC_UI_STYLE_LAYOUT_MARGIN_Y);
oc_ui_pattern pattern = {0};
oc_ui_pattern_push(&ui->frameArena, &pattern, (oc_ui_selector){.kind = OC_UI_SEL_STATUS, .status = OC_UI_HOVER});
oc_ui_style_match_before(pattern, &(oc_ui_style){.bgColor = {0, 0, 1, 1}}, OC_UI_STYLE_BG_COLOR);
oc_ui_box* box = oc_ui_box_make_str8(info->options[i],
OC_UI_FLAG_DRAW_TEXT
|OC_UI_FLAG_CLICKABLE
|OC_UI_FLAG_DRAW_BACKGROUND);
oc_ui_sig sig = oc_ui_box_sig(box);
2023-05-17 13:48:25 +00:00
if(sig.pressed)
{
result.selectedIndex = i;
}
}
}
oc_ui_box_pop();
2023-05-17 13:48:25 +00:00
oc_ui_context* ui = oc_ui_get_context();
if(oc_ui_box_active(panel) && oc_mouse_pressed(&ui->input, OC_MOUSE_LEFT))
2023-05-17 13:48:25 +00:00
{
oc_ui_box_deactivate(panel);
2023-05-17 13:48:25 +00:00
}
else if(oc_ui_box_sig(button).pressed)
2023-05-17 13:48:25 +00:00
{
oc_ui_box_activate(panel);
2023-05-17 13:48:25 +00:00
}
oc_ui_box_set_closed(panel, !oc_ui_box_active(panel));
2023-05-17 13:48:25 +00:00
}
return(result);
}
//------------------------------------------------------------------------------
// text box
//------------------------------------------------------------------------------
oc_str32 oc_ui_edit_replace_selection_with_codepoints(oc_ui_context* ui, oc_str32 codepoints, oc_str32 input)
2023-05-17 13:48:25 +00:00
{
u32 start = oc_min(ui->editCursor, ui->editMark);
u32 end = oc_max(ui->editCursor, ui->editMark);
2023-05-17 13:48:25 +00:00
oc_str32 before = oc_str32_slice(codepoints, 0, start);
oc_str32 after = oc_str32_slice(codepoints, end, codepoints.len);
2023-05-17 13:48:25 +00:00
oc_str32_list list = {0};
oc_str32_list_push(&ui->frameArena, &list, before);
oc_str32_list_push(&ui->frameArena, &list, input);
oc_str32_list_push(&ui->frameArena, &list, after);
2023-05-17 13:48:25 +00:00
codepoints = oc_str32_list_join(&ui->frameArena, list);
2023-05-17 13:48:25 +00:00
ui->editCursor = start + input.len;
ui->editMark = ui->editCursor;
return(codepoints);
}
oc_str32 oc_ui_edit_delete_selection(oc_ui_context* ui, oc_str32 codepoints)
2023-05-17 13:48:25 +00:00
{
return(oc_ui_edit_replace_selection_with_codepoints(ui, codepoints, (oc_str32){0}));
2023-05-17 13:48:25 +00:00
}
void oc_ui_edit_copy_selection_to_clipboard(oc_ui_context* ui, oc_str32 codepoints)
2023-05-17 13:48:25 +00:00
{
#if !OC_PLATFORM_ORCA
2023-05-17 13:48:25 +00:00
if(ui->editCursor == ui->editMark)
{
return;
}
u32 start = oc_min(ui->editCursor, ui->editMark);
u32 end = oc_max(ui->editCursor, ui->editMark);
oc_str32 selection = oc_str32_slice(codepoints, start, end);
oc_str8 string = oc_utf8_push_from_codepoints(&ui->frameArena, selection);
2023-05-17 13:48:25 +00:00
oc_clipboard_clear();
oc_clipboard_set_string(string);
2023-07-14 04:36:31 +00:00
#endif
2023-05-17 13:48:25 +00:00
}
oc_str32 oc_ui_edit_replace_selection_with_clipboard(oc_ui_context* ui, oc_str32 codepoints)
2023-05-17 13:48:25 +00:00
{
#if OC_PLATFORM_ORCA
oc_str32 result = {0};
2023-07-14 04:36:31 +00:00
#else
oc_str8 string = oc_clipboard_get_string(&ui->frameArena);
oc_str32 input = oc_utf8_push_to_codepoints(&ui->frameArena, string);
oc_str32 result = oc_ui_edit_replace_selection_with_codepoints(ui, codepoints, input);
2023-07-14 04:36:31 +00:00
#endif
2023-05-17 13:48:25 +00:00
return(result);
}
typedef enum {
OC_UI_EDIT_MOVE,
OC_UI_EDIT_SELECT,
OC_UI_EDIT_SELECT_EXTEND,
OC_UI_EDIT_DELETE,
OC_UI_EDIT_CUT,
OC_UI_EDIT_COPY,
OC_UI_EDIT_PASTE,
OC_UI_EDIT_SELECT_ALL } oc_ui_edit_op;
2023-05-17 13:48:25 +00:00
typedef enum {
OC_UI_EDIT_MOVE_NONE = 0,
OC_UI_EDIT_MOVE_ONE,
OC_UI_EDIT_MOVE_WORD,
OC_UI_EDIT_MOVE_LINE } oc_ui_edit_move;
2023-05-17 13:48:25 +00:00
typedef struct oc_ui_edit_command
2023-05-17 13:48:25 +00:00
{
oc_key_code key;
oc_keymod_flags mods;
2023-05-17 13:48:25 +00:00
oc_ui_edit_op operation;
oc_ui_edit_move move;
2023-05-17 13:48:25 +00:00
int direction;
} oc_ui_edit_command;
2023-05-17 13:48:25 +00:00
const oc_ui_edit_command OC_UI_EDIT_COMMANDS[] = {
2023-05-17 13:48:25 +00:00
//NOTE(martin): move one left
{
.key = OC_KEY_LEFT,
.operation = OC_UI_EDIT_MOVE,
.move = OC_UI_EDIT_MOVE_ONE,
2023-05-17 13:48:25 +00:00
.direction = -1
},
//NOTE(martin): move one right
{
.key = OC_KEY_RIGHT,
.operation = OC_UI_EDIT_MOVE,
.move = OC_UI_EDIT_MOVE_ONE,
2023-05-17 13:48:25 +00:00
.direction = 1
},
//NOTE(martin): move start
{
.key = OC_KEY_Q,
.mods = OC_KEYMOD_CTRL,
.operation = OC_UI_EDIT_MOVE,
.move = OC_UI_EDIT_MOVE_LINE,
2023-05-17 13:48:25 +00:00
.direction = -1
},
{
.key = OC_KEY_UP,
.operation = OC_UI_EDIT_MOVE,
.move = OC_UI_EDIT_MOVE_LINE,
2023-05-17 13:48:25 +00:00
.direction = -1
},
//NOTE(martin): move end
{
.key = OC_KEY_E,
.mods = OC_KEYMOD_CTRL,
.operation = OC_UI_EDIT_MOVE,
.move = OC_UI_EDIT_MOVE_LINE,
2023-05-17 13:48:25 +00:00
.direction = 1
},
{
.key = OC_KEY_DOWN,
.operation = OC_UI_EDIT_MOVE,
.move = OC_UI_EDIT_MOVE_LINE,
2023-05-17 13:48:25 +00:00
.direction = 1
},
//NOTE(martin): select one left
{
.key = OC_KEY_LEFT,
.mods = OC_KEYMOD_SHIFT,
.operation = OC_UI_EDIT_SELECT,
.move = OC_UI_EDIT_MOVE_ONE,
2023-05-17 13:48:25 +00:00
.direction = -1
},
//NOTE(martin): select one right
{
.key = OC_KEY_RIGHT,
.mods = OC_KEYMOD_SHIFT,
.operation = OC_UI_EDIT_SELECT,
.move = OC_UI_EDIT_MOVE_ONE,
2023-05-17 13:48:25 +00:00
.direction = 1
},
//NOTE(martin): extend select to start
{
.key = OC_KEY_Q,
.mods = OC_KEYMOD_CTRL | OC_KEYMOD_SHIFT,
.operation = OC_UI_EDIT_SELECT_EXTEND,
.move = OC_UI_EDIT_MOVE_LINE,
2023-05-17 13:48:25 +00:00
.direction = -1
},
{
.key = OC_KEY_UP,
.mods = OC_KEYMOD_SHIFT,
.operation = OC_UI_EDIT_SELECT_EXTEND,
.move = OC_UI_EDIT_MOVE_LINE,
2023-05-17 13:48:25 +00:00
.direction = -1
},
//NOTE(martin): extend select to end
{
.key = OC_KEY_E,
.mods = OC_KEYMOD_CTRL | OC_KEYMOD_SHIFT,
.operation = OC_UI_EDIT_SELECT_EXTEND,
.move = OC_UI_EDIT_MOVE_LINE,
2023-05-17 13:48:25 +00:00
.direction = 1
},
{
.key = OC_KEY_DOWN,
.mods = OC_KEYMOD_SHIFT,
.operation = OC_UI_EDIT_SELECT_EXTEND,
.move = OC_UI_EDIT_MOVE_LINE,
2023-05-17 13:48:25 +00:00
.direction = 1
},
//NOTE(martin): select all
{
.key = OC_KEY_Q,
.mods = OC_KEYMOD_MAIN_MODIFIER,
.operation = OC_UI_EDIT_SELECT_ALL,
.move = OC_UI_EDIT_MOVE_NONE
2023-05-17 13:48:25 +00:00
},
//NOTE(martin): delete
{
.key = OC_KEY_DELETE,
.operation = OC_UI_EDIT_DELETE,
.move = OC_UI_EDIT_MOVE_ONE,
2023-05-17 13:48:25 +00:00
.direction = 1
},
//NOTE(martin): backspace
{
.key = OC_KEY_BACKSPACE,
.operation = OC_UI_EDIT_DELETE,
.move = OC_UI_EDIT_MOVE_ONE,
2023-05-17 13:48:25 +00:00
.direction = -1
},
//NOTE(martin): cut
{
.key = OC_KEY_X,
.mods = OC_KEYMOD_MAIN_MODIFIER,
.operation = OC_UI_EDIT_CUT,
.move = OC_UI_EDIT_MOVE_NONE
2023-05-17 13:48:25 +00:00
},
//NOTE(martin): copy
{
.key = OC_KEY_C,
.mods = OC_KEYMOD_MAIN_MODIFIER,
.operation = OC_UI_EDIT_COPY,
.move = OC_UI_EDIT_MOVE_NONE
2023-05-17 13:48:25 +00:00
},
//NOTE(martin): paste
{
.key = OC_KEY_V,
.mods = OC_KEYMOD_MAIN_MODIFIER,
.operation = OC_UI_EDIT_PASTE,
.move = OC_UI_EDIT_MOVE_NONE
2023-05-17 13:48:25 +00:00
}
};
const u32 OC_UI_EDIT_COMMAND_COUNT = sizeof(OC_UI_EDIT_COMMANDS)/sizeof(oc_ui_edit_command);
2023-05-17 13:48:25 +00:00
void oc_ui_edit_perform_move(oc_ui_context* ui, oc_ui_edit_move move, int direction, u32 textLen)
2023-05-17 13:48:25 +00:00
{
switch(move)
{
case OC_UI_EDIT_MOVE_NONE:
2023-05-17 13:48:25 +00:00
break;
case OC_UI_EDIT_MOVE_ONE:
2023-05-17 13:48:25 +00:00
{
if(direction < 0 && ui->editCursor > 0)
{
ui->editCursor--;
}
else if(direction > 0 && ui->editCursor < textLen)
{
ui->editCursor++;
}
} break;
case OC_UI_EDIT_MOVE_LINE:
2023-05-17 13:48:25 +00:00
{
if(direction < 0)
{
ui->editCursor = 0;
}
else if(direction > 0)
{
ui->editCursor = textLen;
}
} break;
case OC_UI_EDIT_MOVE_WORD:
OC_DEBUG_ASSERT(0, "not implemented yet");
2023-05-17 13:48:25 +00:00
break;
}
}
oc_str32 oc_ui_edit_perform_operation(oc_ui_context* ui, oc_ui_edit_op operation, oc_ui_edit_move move, int direction, oc_str32 codepoints)
2023-05-17 13:48:25 +00:00
{
switch(operation)
{
case OC_UI_EDIT_MOVE:
2023-05-17 13:48:25 +00:00
{
//NOTE(martin): we place the cursor on the direction-most side of the selection
// before performing the move
u32 cursor = direction < 0 ?
oc_min(ui->editCursor, ui->editMark) :
oc_max(ui->editCursor, ui->editMark);
2023-05-17 13:48:25 +00:00
ui->editCursor = cursor;
if(ui->editCursor == ui->editMark || move != OC_UI_EDIT_MOVE_ONE)
2023-05-17 13:48:25 +00:00
{
//NOTE: we special case move-one when there is a selection
// (just place the cursor at begining/end of selection)
oc_ui_edit_perform_move(ui, move, direction, codepoints.len);
2023-05-17 13:48:25 +00:00
}
ui->editMark = ui->editCursor;
} break;
case OC_UI_EDIT_SELECT:
2023-05-17 13:48:25 +00:00
{
oc_ui_edit_perform_move(ui, move, direction, codepoints.len);
2023-05-17 13:48:25 +00:00
} break;
case OC_UI_EDIT_SELECT_EXTEND:
2023-05-17 13:48:25 +00:00
{
if((direction > 0) != (ui->editCursor > ui->editMark))
{
u32 tmp = ui->editCursor;
ui->editCursor = ui->editMark;
ui->editMark = tmp;
}
oc_ui_edit_perform_move(ui, move, direction, codepoints.len);
2023-05-17 13:48:25 +00:00
} break;
case OC_UI_EDIT_DELETE:
2023-05-17 13:48:25 +00:00
{
if(ui->editCursor == ui->editMark)
{
oc_ui_edit_perform_move(ui, move, direction, codepoints.len);
2023-05-17 13:48:25 +00:00
}
codepoints = oc_ui_edit_delete_selection(ui, codepoints);
2023-05-17 13:48:25 +00:00
ui->editMark = ui->editCursor;
} break;
case OC_UI_EDIT_CUT:
2023-05-17 13:48:25 +00:00
{
oc_ui_edit_copy_selection_to_clipboard(ui, codepoints);
codepoints = oc_ui_edit_delete_selection(ui, codepoints);
2023-05-17 13:48:25 +00:00
} break;
case OC_UI_EDIT_COPY:
2023-05-17 13:48:25 +00:00
{
oc_ui_edit_copy_selection_to_clipboard(ui, codepoints);
2023-05-17 13:48:25 +00:00
} break;
case OC_UI_EDIT_PASTE:
2023-05-17 13:48:25 +00:00
{
codepoints = oc_ui_edit_replace_selection_with_clipboard(ui, codepoints);
2023-05-17 13:48:25 +00:00
} break;
case OC_UI_EDIT_SELECT_ALL:
2023-05-17 13:48:25 +00:00
{
ui->editCursor = 0;
ui->editMark = codepoints.len;
} break;
}
ui->editCursorBlinkStart = ui->frameTime;
return(codepoints);
}
void oc_ui_text_box_render(oc_ui_box* box, void* data)
2023-05-17 13:48:25 +00:00
{
oc_str32 codepoints = *(oc_str32*)data;
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
u32 firstDisplayedChar = 0;
if(oc_ui_box_active(box))
2023-05-17 13:48:25 +00:00
{
firstDisplayedChar = ui->editFirstDisplayedChar;
}
oc_ui_style* style = &box->style;
oc_font_extents extents = oc_font_get_scaled_extents(style->font, style->fontSize);
2023-05-17 13:48:25 +00:00
f32 lineHeight = extents.ascent + extents.descent;
oc_str32 before = oc_str32_slice(codepoints, 0, firstDisplayedChar);
oc_rect beforeBox = oc_text_bounding_box_utf32(style->font, style->fontSize, before);
2023-05-17 13:48:25 +00:00
f32 textMargin = 5; //TODO: make that configurable
f32 textX = textMargin + box->rect.x - beforeBox.w;
f32 textTop = box->rect.y + 0.5*(box->rect.h - lineHeight);
f32 textY = textTop + extents.ascent ;
if(box->active)
{
u32 selectStart = oc_min(ui->editCursor, ui->editMark);
u32 selectEnd = oc_max(ui->editCursor, ui->editMark);
2023-05-17 13:48:25 +00:00
oc_str32 beforeSelect = oc_str32_slice(codepoints, 0, selectStart);
oc_rect beforeSelectBox = oc_text_bounding_box_utf32(style->font, style->fontSize, beforeSelect);
2023-05-17 13:48:25 +00:00
beforeSelectBox.x += textX;
beforeSelectBox.y += textY;
if(selectStart != selectEnd)
{
oc_str32 select = oc_str32_slice(codepoints, selectStart, selectEnd);
oc_str32 afterSelect = oc_str32_slice(codepoints, selectEnd, codepoints.len);
oc_rect selectBox = oc_text_bounding_box_utf32(style->font, style->fontSize, select);
oc_rect afterSelectBox = oc_text_bounding_box_utf32(style->font, style->fontSize, afterSelect);
2023-05-17 13:48:25 +00:00
selectBox.x += beforeSelectBox.x + beforeSelectBox.w;
selectBox.y += textY;
oc_set_color_rgba(0, 0, 1, 1);
oc_rectangle_fill(selectBox.x, selectBox.y, selectBox.w, lineHeight);
2023-05-17 13:48:25 +00:00
oc_set_font(style->font);
oc_set_font_size(style->fontSize);
oc_set_color(style->color);
2023-05-17 13:48:25 +00:00
oc_move_to(textX, textY);
oc_codepoints_outlines(beforeSelect);
oc_fill();
2023-05-17 13:48:25 +00:00
oc_set_color_rgba(1, 1, 1, 1);
oc_codepoints_outlines(select);
oc_fill();
2023-05-17 13:48:25 +00:00
oc_set_color(style->color);
oc_codepoints_outlines(afterSelect);
oc_fill();
2023-05-17 13:48:25 +00:00
}
else
{
if(!((u64)(2*(ui->frameTime - ui->editCursorBlinkStart)) & 1))
{
f32 caretX = box->rect.x + textMargin - beforeBox.w + beforeSelectBox.w;
f32 caretY = textTop;
oc_set_color(style->color);
oc_rectangle_fill(caretX, caretY, 1, lineHeight);
2023-05-17 13:48:25 +00:00
}
oc_set_font(style->font);
oc_set_font_size(style->fontSize);
oc_set_color(style->color);
2023-05-17 13:48:25 +00:00
oc_move_to(textX, textY);
oc_codepoints_outlines(codepoints);
oc_fill();
2023-05-17 13:48:25 +00:00
}
}
else
{
oc_set_font(style->font);
oc_set_font_size(style->fontSize);
oc_set_color(style->color);
2023-05-17 13:48:25 +00:00
oc_move_to(textX, textY);
oc_codepoints_outlines(codepoints);
oc_fill();
2023-05-17 13:48:25 +00:00
}
}
oc_ui_text_box_result oc_ui_text_box(const char* name, oc_arena* arena, oc_str8 text)
2023-05-17 13:48:25 +00:00
{
oc_ui_context* ui = oc_ui_get_context();
2023-05-17 13:48:25 +00:00
oc_ui_text_box_result result = {.text = text};
2023-05-17 13:48:25 +00:00
oc_ui_flags frameFlags = OC_UI_FLAG_CLICKABLE
| OC_UI_FLAG_DRAW_BACKGROUND
| OC_UI_FLAG_DRAW_BORDER
| OC_UI_FLAG_CLIP
| OC_UI_FLAG_DRAW_PROC;
2023-05-17 13:48:25 +00:00
oc_ui_box* frame = oc_ui_box_make(name, frameFlags);
oc_ui_style* style = &frame->style;
f32 textMargin = 5; //TODO parameterize this margin! must be the same as in oc_ui_text_box_render
2023-05-17 13:48:25 +00:00
oc_font_extents extents = oc_font_get_scaled_extents(style->font, style->fontSize);
2023-05-17 13:48:25 +00:00
oc_ui_sig sig = oc_ui_box_sig(frame);
2023-05-17 13:48:25 +00:00
if(sig.pressed)
2023-05-17 13:48:25 +00:00
{
if(!oc_ui_box_active(frame))
2023-05-17 13:48:25 +00:00
{
oc_ui_box_activate(frame);
2023-05-17 13:48:25 +00:00
//NOTE: focus
ui->focus = frame;
ui->editFirstDisplayedChar = 0;
ui->editCursor = 0;
ui->editMark = 0;
2023-05-17 13:48:25 +00:00
}
ui->editCursorBlinkStart = ui->frameTime;
}
2023-05-17 13:48:25 +00:00
if(sig.pressed || sig.dragging)
{
//NOTE: set cursor/extend selection on mouse press or drag
oc_vec2 pos = oc_ui_mouse_position();
f32 cursorX = pos.x - frame->rect.x - textMargin;
2023-05-17 13:48:25 +00:00
oc_str32 codepoints = oc_utf8_push_to_codepoints(&ui->frameArena, text);
i32 newCursor = codepoints.len;
f32 x = 0;
for(int i = ui->editFirstDisplayedChar; i<codepoints.len; i++)
{
oc_rect bbox = oc_text_bounding_box_utf32(style->font, style->fontSize, oc_str32_slice(codepoints, i, i+1));
if(x + 0.5*bbox.w > cursorX)
2023-05-17 13:48:25 +00:00
{
newCursor = i;
break;
2023-05-17 13:48:25 +00:00
}
x += bbox.w;
}
//NOTE: put cursor the closest to new cursor (this maximizes the resulting selection,
// and seems to be the standard behaviour across a number of text editor)
if(sig.pressed && abs(newCursor - ui->editCursor) > abs(newCursor - ui->editMark))
{
i32 tmp = ui->editCursor;
ui->editCursor = ui->editMark;
ui->editMark = tmp;
2023-05-17 13:48:25 +00:00
}
//NOTE: set the new cursor, and set or leave the mark depending on mode
ui->editCursor = newCursor;
if(sig.pressed && !(oc_key_mods(&ui->input) & OC_KEYMOD_SHIFT))
{
ui->editMark = ui->editCursor;
}
}
if(sig.hovering)
{
oc_ui_box_set_hot(frame, true);
2023-05-17 13:48:25 +00:00
}
else
{
oc_ui_box_set_hot(frame, false);
2023-05-17 13:48:25 +00:00
if(oc_mouse_pressed(&ui->input, OC_MOUSE_LEFT) ||
oc_mouse_pressed(&ui->input, OC_MOUSE_RIGHT) ||
oc_mouse_pressed(&ui->input, OC_MOUSE_MIDDLE))
2023-05-17 13:48:25 +00:00
{
if(oc_ui_box_active(frame))
2023-05-17 13:48:25 +00:00
{
oc_ui_box_deactivate(frame);
2023-05-17 13:48:25 +00:00
//NOTE loose focus
ui->focus = 0;
}
}
}
if(oc_ui_box_active(frame))
2023-05-17 13:48:25 +00:00
{
oc_str32 oldCodepoints = oc_utf8_push_to_codepoints(&ui->frameArena, text);
oc_str32 codepoints = oldCodepoints;
ui->editCursor = oc_clamp(ui->editCursor, 0, codepoints.len);
ui->editMark = oc_clamp(ui->editMark, 0, codepoints.len);
2023-05-17 13:48:25 +00:00
//NOTE replace selection with input codepoints
oc_str32 input = oc_input_text_utf32(&ui->input, &ui->frameArena);
2023-05-17 13:48:25 +00:00
if(input.len)
{
codepoints = oc_ui_edit_replace_selection_with_codepoints(ui, codepoints, input);
2023-05-17 13:48:25 +00:00
ui->editCursorBlinkStart = ui->frameTime;
}
//NOTE handle shortcuts
oc_keymod_flags mods = oc_key_mods(&ui->input);
2023-05-17 13:48:25 +00:00
for(int i=0; i<OC_UI_EDIT_COMMAND_COUNT; i++)
2023-05-17 13:48:25 +00:00
{
const oc_ui_edit_command* command = &(OC_UI_EDIT_COMMANDS[i]);
2023-05-17 13:48:25 +00:00
if( (oc_key_pressed(&ui->input, command->key) || oc_key_repeated(&ui->input, command->key))
2023-05-17 13:48:25 +00:00
&& mods == command->mods)
{
codepoints = oc_ui_edit_perform_operation(ui, command->operation, command->move, command->direction, codepoints);
2023-05-17 13:48:25 +00:00
break;
}
}
//NOTE(martin): check changed/accepted
if(oldCodepoints.ptr != codepoints.ptr)
{
result.changed = true;
result.text = oc_utf8_push_from_codepoints(arena, codepoints);
2023-05-17 13:48:25 +00:00
}
if(oc_key_pressed(&ui->input, OC_KEY_ENTER))
2023-05-17 13:48:25 +00:00
{
//TODO(martin): extract in gui_edit_complete() (and use below)
result.accepted = true;
oc_ui_box_deactivate(frame);
2023-05-17 13:48:25 +00:00
ui->focus = 0;
}
//NOTE slide contents
{
if(ui->editCursor < ui->editFirstDisplayedChar)
{
ui->editFirstDisplayedChar = ui->editCursor;
}
else
{
i32 firstDisplayedChar = ui->editFirstDisplayedChar;
oc_str32 firstToCursor = oc_str32_slice(codepoints, firstDisplayedChar, ui->editCursor);
oc_rect firstToCursorBox = oc_text_bounding_box_utf32(style->font, style->fontSize, firstToCursor);
2023-05-17 13:48:25 +00:00
while(firstToCursorBox.w > (frame->rect.w - 2*textMargin))
{
firstDisplayedChar++;
firstToCursor = oc_str32_slice(codepoints, firstDisplayedChar, ui->editCursor);
firstToCursorBox = oc_text_bounding_box_utf32(style->font, style->fontSize, firstToCursor);
2023-05-17 13:48:25 +00:00
}
ui->editFirstDisplayedChar = firstDisplayedChar;
}
}
//NOTE: set renderer
oc_str32* renderCodepoints = oc_arena_push_type(&ui->frameArena, oc_str32);
*renderCodepoints = oc_str32_push_copy(&ui->frameArena, codepoints);
oc_ui_box_set_draw_proc(frame, oc_ui_text_box_render, renderCodepoints);
2023-05-17 13:48:25 +00:00
}
else
{
//NOTE: set renderer
oc_str32* renderCodepoints = oc_arena_push_type(&ui->frameArena, oc_str32);
*renderCodepoints = oc_utf8_push_to_codepoints(&ui->frameArena, text);
oc_ui_box_set_draw_proc(frame, oc_ui_text_box_render, renderCodepoints);
2023-05-17 13:48:25 +00:00
}
return(result);
}