2022-08-14 16:19:40 +00:00
|
|
|
/************************************************************//**
|
|
|
|
*
|
|
|
|
* @file: ui.c
|
|
|
|
* @author: Martin Fouilleul
|
|
|
|
* @date: 08/08/2022
|
|
|
|
* @revision:
|
|
|
|
*
|
|
|
|
*****************************************************************/
|
2023-03-09 16:38:41 +00:00
|
|
|
#include"platform.h"
|
2022-08-14 16:19:40 +00:00
|
|
|
#include"memory.h"
|
|
|
|
#include"hash.h"
|
|
|
|
#include"platform_clock.h"
|
|
|
|
#include"ui.h"
|
|
|
|
|
|
|
|
#define LOG_SUBSYSTEM "UI"
|
|
|
|
|
2023-03-07 18:06:04 +00:00
|
|
|
|
|
|
|
static ui_style UI_STYLE_DEFAULTS =
|
|
|
|
{
|
|
|
|
.size.width = {.kind = UI_SIZE_CHILDREN,
|
|
|
|
.value = 0,
|
2023-03-09 11:36:38 +00:00
|
|
|
.relax = 0},
|
2023-03-07 18:06:04 +00:00
|
|
|
.size.height = {.kind = UI_SIZE_CHILDREN,
|
|
|
|
.value = 0,
|
2023-03-09 11:36:38 +00:00
|
|
|
.relax = 0},
|
2023-03-07 18:06:04 +00:00
|
|
|
|
|
|
|
.layout = {.axis = UI_AXIS_Y,
|
|
|
|
.align = {UI_ALIGN_START,
|
|
|
|
UI_ALIGN_START}},
|
|
|
|
.color = {0, 0, 0, 1},
|
|
|
|
.fontSize = 16,
|
|
|
|
};
|
2022-08-14 16:19:40 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// context
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2023-03-09 16:38:41 +00:00
|
|
|
enum { UI_MAX_INPUT_CHAR_PER_FRAME = 64 };
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
typedef struct ui_input_text
|
|
|
|
{
|
|
|
|
u8 count;
|
|
|
|
utf32 codePoints[UI_MAX_INPUT_CHAR_PER_FRAME];
|
|
|
|
|
|
|
|
} ui_input_text;
|
|
|
|
|
|
|
|
typedef struct ui_stack_elt ui_stack_elt;
|
|
|
|
struct ui_stack_elt
|
|
|
|
{
|
|
|
|
ui_stack_elt* parent;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
ui_box* box;
|
|
|
|
ui_size size;
|
|
|
|
mp_rect clip;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-03-06 18:33:01 +00:00
|
|
|
typedef struct ui_tag_elt
|
|
|
|
{
|
|
|
|
list_elt listElt;
|
|
|
|
ui_tag tag;
|
|
|
|
} ui_tag_elt;
|
|
|
|
|
2023-03-09 16:38:41 +00:00
|
|
|
enum { UI_BOX_MAP_BUCKET_COUNT = 1024 };
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
typedef struct ui_context
|
|
|
|
{
|
|
|
|
bool init;
|
|
|
|
|
|
|
|
u64 frameCounter;
|
|
|
|
f64 frameTime;
|
|
|
|
f64 lastFrameDuration;
|
|
|
|
|
|
|
|
mem_arena frameArena;
|
|
|
|
mem_pool boxPool;
|
|
|
|
list_info boxMap[UI_BOX_MAP_BUCKET_COUNT];
|
|
|
|
|
|
|
|
ui_box* root;
|
|
|
|
ui_box* overlay;
|
2023-03-13 09:26:39 +00:00
|
|
|
list_info overlayList;
|
2022-08-14 16:19:40 +00:00
|
|
|
ui_stack_elt* boxStack;
|
|
|
|
ui_stack_elt* clipStack;
|
|
|
|
|
2023-03-08 10:09:28 +00:00
|
|
|
list_info nextBoxBeforeRules;
|
|
|
|
list_info nextBoxAfterRules;
|
2023-03-06 18:33:01 +00:00
|
|
|
list_info nextBoxTags;
|
2022-08-15 08:20:29 +00:00
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
u32 z;
|
|
|
|
ui_box* hovered;
|
|
|
|
|
2022-08-15 08:20:29 +00:00
|
|
|
ui_box* focus;
|
2023-03-05 14:59:57 +00:00
|
|
|
i32 editCursor;
|
|
|
|
i32 editMark;
|
|
|
|
i32 editFirstDisplayedChar;
|
2022-08-15 08:20:29 +00:00
|
|
|
f64 editCursorBlinkStart;
|
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
} ui_context;
|
|
|
|
|
2023-03-09 16:38:41 +00:00
|
|
|
mp_thread_local ui_context __uiThreadContext = {0};
|
|
|
|
mp_thread_local ui_context* __uiCurrentContext = 0;
|
2022-08-14 16:19:40 +00:00
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
ui_context* ui_get_context(void)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2022-08-15 21:11:00 +00:00
|
|
|
return(__uiCurrentContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_set_context(ui_context* context)
|
|
|
|
{
|
|
|
|
__uiCurrentContext = context;
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// stacks
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
ui_stack_elt* ui_stack_push(ui_context* ui, ui_stack_elt** stack)
|
|
|
|
{
|
|
|
|
ui_stack_elt* elt = mem_arena_alloc_type(&ui->frameArena, ui_stack_elt);
|
|
|
|
memset(elt, 0, sizeof(ui_stack_elt));
|
|
|
|
elt->parent = *stack;
|
|
|
|
*stack = elt;
|
|
|
|
return(elt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_stack_pop(ui_stack_elt** stack)
|
|
|
|
{
|
|
|
|
if(*stack)
|
|
|
|
{
|
|
|
|
*stack = (*stack)->parent;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_ERROR("ui stack underflow\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_rect ui_intersect_rects(mp_rect lhs, mp_rect rhs)
|
|
|
|
{
|
|
|
|
//NOTE(martin): intersect with current clip
|
|
|
|
f32 x0 = maximum(lhs.x, rhs.x);
|
|
|
|
f32 y0 = maximum(lhs.y, rhs.y);
|
|
|
|
f32 x1 = minimum(lhs.x + lhs.w, rhs.x + rhs.w);
|
|
|
|
f32 y1 = minimum(lhs.y + lhs.h, rhs.y + rhs.h);
|
|
|
|
mp_rect r = {x0, y0, maximum(0, x1-x0), maximum(0, y1-y0)};
|
|
|
|
return(r);
|
|
|
|
}
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
mp_rect ui_clip_top(void)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
mp_rect r = {-FLT_MAX/2, -FLT_MAX/2, FLT_MAX, FLT_MAX};
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
ui_stack_elt* elt = ui->clipStack;
|
|
|
|
if(elt)
|
|
|
|
{
|
|
|
|
r = elt->clip;
|
|
|
|
}
|
|
|
|
return(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_clip_push(mp_rect clip)
|
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
mp_rect current = ui_clip_top();
|
|
|
|
ui_stack_elt* elt = ui_stack_push(ui, &ui->clipStack);
|
|
|
|
elt->clip = ui_intersect_rects(current, clip);
|
|
|
|
}
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
void ui_clip_pop(void)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
ui_stack_pop(&ui->clipStack);
|
|
|
|
}
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
ui_box* ui_box_top(void)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
ui_stack_elt* elt = ui->boxStack;
|
|
|
|
ui_box* box = elt ? elt->box : 0;
|
|
|
|
return(box);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_box_push(ui_box* box)
|
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
ui_stack_elt* elt = ui_stack_push(ui, &ui->boxStack);
|
|
|
|
elt->box = box;
|
|
|
|
if(box->flags & UI_FLAG_CLIP)
|
|
|
|
{
|
|
|
|
ui_clip_push(box->rect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
void ui_box_pop(void)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
ui_box* box = ui_box_top();
|
2023-03-03 16:13:03 +00:00
|
|
|
if(box)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-03 16:13:03 +00:00
|
|
|
if(box->flags & UI_FLAG_CLIP)
|
|
|
|
{
|
|
|
|
ui_clip_pop();
|
|
|
|
}
|
|
|
|
ui_stack_pop(&ui->boxStack);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 18:33:01 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// tagging
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-08-15 08:20:29 +00:00
|
|
|
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_tag ui_tag_make_str8(str8 string)
|
2022-08-15 08:20:29 +00:00
|
|
|
{
|
2023-03-06 18:33:01 +00:00
|
|
|
ui_tag tag = {.hash = mp_hash_aes_string(string)};
|
|
|
|
return(tag);
|
2022-08-15 08:20:29 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 18:06:04 +00:00
|
|
|
void ui_tag_box_str8(ui_box* box, str8 string)
|
2022-08-15 08:20:29 +00:00
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
2023-03-06 18:33:01 +00:00
|
|
|
ui_tag_elt* elt = mem_arena_alloc_type(&ui->frameArena, ui_tag_elt);
|
2023-03-07 18:06:04 +00:00
|
|
|
elt->tag = ui_tag_make_str8(string);
|
2023-03-08 11:00:10 +00:00
|
|
|
list_append(&box->tags, &elt->listElt);
|
2022-08-15 08:20:29 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 18:06:04 +00:00
|
|
|
void ui_tag_next_str8(str8 string)
|
2022-08-15 08:20:29 +00:00
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
2023-03-06 18:33:01 +00:00
|
|
|
ui_tag_elt* elt = mem_arena_alloc_type(&ui->frameArena, ui_tag_elt);
|
2023-03-07 18:06:04 +00:00
|
|
|
elt->tag = ui_tag_make_str8(string);
|
2023-03-08 11:00:10 +00:00
|
|
|
list_append(&ui->nextBoxTags, &elt->listElt);
|
2022-08-15 08:20:29 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// key hashing and caching
|
|
|
|
//-----------------------------------------------------------------------------
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_key ui_key_make_str8(str8 string)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
u64 seed = 0;
|
|
|
|
ui_box* parent = ui_box_top();
|
|
|
|
if(parent)
|
|
|
|
{
|
|
|
|
seed = parent->key.hash;
|
|
|
|
}
|
|
|
|
|
2023-03-09 16:38:41 +00:00
|
|
|
ui_key key = {0};
|
2022-08-14 16:19:40 +00:00
|
|
|
key.hash = mp_hash_aes_string_seed(string, seed);
|
|
|
|
return(key);
|
|
|
|
}
|
|
|
|
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_key ui_key_make_path(str8_list path)
|
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
u64 seed = 0;
|
|
|
|
ui_box* parent = ui_box_top();
|
|
|
|
if(parent)
|
|
|
|
{
|
|
|
|
seed = parent->key.hash;
|
|
|
|
}
|
2023-03-08 11:03:33 +00:00
|
|
|
for_list(&path.list, elt, str8_elt, listElt)
|
2023-03-07 18:06:04 +00:00
|
|
|
{
|
|
|
|
seed = mp_hash_aes_string_seed(elt->string, seed);
|
|
|
|
}
|
|
|
|
ui_key key = {seed};
|
|
|
|
return(key);
|
|
|
|
}
|
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
bool ui_key_equal(ui_key a, ui_key b)
|
|
|
|
{
|
|
|
|
return(a.hash == b.hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_box_cache(ui_context* ui, ui_box* box)
|
|
|
|
{
|
|
|
|
u64 index = box->key.hash & (UI_BOX_MAP_BUCKET_COUNT-1);
|
2023-03-08 11:00:10 +00:00
|
|
|
list_append(&(ui->boxMap[index]), &box->bucketElt);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_box* ui_box_lookup_key(ui_key key)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_context* ui = ui_get_context();
|
2022-08-14 16:19:40 +00:00
|
|
|
u64 index = key.hash & (UI_BOX_MAP_BUCKET_COUNT-1);
|
|
|
|
|
2023-03-08 11:03:33 +00:00
|
|
|
for_list(&ui->boxMap[index], box, ui_box, bucketElt)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
if(ui_key_equal(key, box->key))
|
|
|
|
{
|
|
|
|
return(box);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
ui_box* ui_box_lookup_str8(str8 string)
|
|
|
|
{
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_key key = ui_key_make_str8(string);
|
|
|
|
return(ui_box_lookup_key(key));
|
2022-08-19 17:46:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-06 18:33:01 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// styling
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void ui_pattern_push(mem_arena* arena, ui_pattern* pattern, ui_selector selector)
|
|
|
|
{
|
|
|
|
ui_selector* copy = mem_arena_alloc_type(arena, ui_selector);
|
|
|
|
*copy = selector;
|
2023-03-08 11:00:10 +00:00
|
|
|
list_append(&pattern->l, ©->listElt);
|
2023-03-06 18:33:01 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 10:09:28 +00:00
|
|
|
ui_pattern ui_pattern_all(void)
|
2023-03-06 18:33:01 +00:00
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
2023-03-08 10:09:28 +00:00
|
|
|
ui_pattern pattern = {0};
|
|
|
|
ui_pattern_push(&ui->frameArena, &pattern, (ui_selector){.kind = UI_SEL_ANY});
|
|
|
|
return(pattern);
|
|
|
|
}
|
2023-03-06 18:33:01 +00:00
|
|
|
|
2023-03-08 10:09:28 +00:00
|
|
|
ui_pattern ui_pattern_owner(void)
|
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
ui_pattern pattern = {0};
|
|
|
|
ui_pattern_push(&ui->frameArena, &pattern, (ui_selector){.kind = UI_SEL_OWNER});
|
|
|
|
return(pattern);
|
2023-03-06 18:33:01 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 10:13:32 +00:00
|
|
|
void ui_style_match_before(ui_pattern pattern, ui_style* style, ui_style_mask mask)
|
2023-03-06 18:33:01 +00:00
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
2023-03-08 10:09:28 +00:00
|
|
|
if(ui)
|
2023-03-06 18:33:01 +00:00
|
|
|
{
|
|
|
|
ui_style_rule* rule = mem_arena_alloc_type(&ui->frameArena, ui_style_rule);
|
|
|
|
rule->pattern = pattern;
|
|
|
|
rule->mask = mask;
|
|
|
|
rule->style = mem_arena_alloc_type(&ui->frameArena, ui_style);
|
|
|
|
*rule->style = *style;
|
|
|
|
|
2023-03-08 11:00:10 +00:00
|
|
|
list_append(&ui->nextBoxBeforeRules, &rule->boxElt);
|
2023-03-06 18:33:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 10:13:32 +00:00
|
|
|
void ui_style_match_after(ui_pattern pattern, ui_style* style, ui_style_mask mask)
|
2023-03-07 18:06:04 +00:00
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
if(ui)
|
|
|
|
{
|
|
|
|
ui_style_rule* rule = mem_arena_alloc_type(&ui->frameArena, ui_style_rule);
|
|
|
|
rule->pattern = pattern;
|
|
|
|
rule->mask = mask;
|
|
|
|
rule->style = mem_arena_alloc_type(&ui->frameArena, ui_style);
|
|
|
|
*rule->style = *style;
|
|
|
|
|
2023-03-08 11:00:10 +00:00
|
|
|
list_append(&ui->nextBoxAfterRules, &rule->boxElt);
|
2023-03-07 18:06:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 10:09:28 +00:00
|
|
|
void ui_style_next(ui_style* style, ui_style_mask mask)
|
2023-03-07 18:06:04 +00:00
|
|
|
{
|
2023-03-08 10:13:32 +00:00
|
|
|
ui_style_match_before(ui_pattern_owner(), style, mask);
|
2023-03-07 18:06:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-12 18:06:09 +00:00
|
|
|
void ui_style_box_before(ui_box* box, ui_pattern pattern, ui_style* style, ui_style_mask mask)
|
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
if(ui)
|
|
|
|
{
|
|
|
|
ui_style_rule* rule = mem_arena_alloc_type(&ui->frameArena, ui_style_rule);
|
|
|
|
rule->pattern = pattern;
|
|
|
|
rule->mask = mask;
|
|
|
|
rule->style = mem_arena_alloc_type(&ui->frameArena, ui_style);
|
|
|
|
*rule->style = *style;
|
|
|
|
|
|
|
|
list_append(&box->beforeRules, &rule->boxElt);
|
|
|
|
rule->owner = box;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_style_box_after(ui_box* box, ui_pattern pattern, ui_style* style, ui_style_mask mask)
|
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
if(ui)
|
|
|
|
{
|
|
|
|
ui_style_rule* rule = mem_arena_alloc_type(&ui->frameArena, ui_style_rule);
|
|
|
|
rule->pattern = pattern;
|
|
|
|
rule->mask = mask;
|
|
|
|
rule->style = mem_arena_alloc_type(&ui->frameArena, ui_style);
|
|
|
|
*rule->style = *style;
|
|
|
|
|
|
|
|
list_append(&box->afterRules, &rule->boxElt);
|
|
|
|
rule->owner = box;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// ui boxes
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool ui_rect_hit(mp_rect r, vec2 p)
|
|
|
|
{
|
|
|
|
return( (p.x > r.x)
|
|
|
|
&&(p.x < r.x + r.w)
|
|
|
|
&&(p.y > r.y)
|
|
|
|
&&(p.y < r.y + r.h));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ui_box_hovering(ui_box* box, vec2 p)
|
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
|
|
|
|
mp_rect clip = ui_clip_top();
|
|
|
|
mp_rect rect = ui_intersect_rects(clip, box->rect);
|
|
|
|
bool hit = ui_rect_hit(rect, p);
|
|
|
|
bool result = hit && (!ui->hovered || box->z >= ui->hovered->z);
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
vec2 ui_mouse_position(void)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-04 18:24:30 +00:00
|
|
|
vec2 mousePos = mp_mouse_position();
|
2022-08-14 16:19:40 +00:00
|
|
|
return(mousePos);
|
|
|
|
}
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
vec2 ui_mouse_delta(void)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
2023-03-04 18:24:30 +00:00
|
|
|
vec2 delta = mp_mouse_delta();
|
2022-08-14 16:19:40 +00:00
|
|
|
return(delta);
|
|
|
|
}
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
vec2 ui_mouse_wheel(void)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
2023-03-04 18:24:30 +00:00
|
|
|
vec2 delta = mp_mouse_wheel();
|
2022-08-14 16:19:40 +00:00
|
|
|
return(delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
ui_box* ui_box_make_str8(str8 string, ui_flags flags)
|
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_key key = ui_key_make_str8(string);
|
|
|
|
ui_box* box = ui_box_lookup_key(key);
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
if(!box)
|
|
|
|
{
|
|
|
|
box = mem_pool_alloc_type(&ui->boxPool, ui_box);
|
|
|
|
memset(box, 0, sizeof(ui_box));
|
|
|
|
|
|
|
|
box->key = key;
|
2022-08-16 07:58:16 +00:00
|
|
|
box->fresh = true;
|
2022-08-14 16:19:40 +00:00
|
|
|
ui_box_cache(ui, box);
|
|
|
|
}
|
2022-08-16 07:58:16 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
box->fresh = false;
|
|
|
|
}
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
//NOTE: setup hierarchy
|
2022-08-21 15:07:54 +00:00
|
|
|
if(box->frameCounter != ui->frameCounter)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-08 11:00:10 +00:00
|
|
|
list_init(&box->children);
|
2022-08-21 15:07:54 +00:00
|
|
|
box->parent = ui_box_top();
|
|
|
|
if(box->parent)
|
|
|
|
{
|
2023-03-08 11:00:10 +00:00
|
|
|
list_append(&box->parent->children, &box->listElt);
|
2022-08-21 15:07:54 +00:00
|
|
|
box->parentClosed = box->parent->closed || box->parent->parentClosed;
|
|
|
|
}
|
2023-03-13 09:26:39 +00:00
|
|
|
|
|
|
|
if(box->flags & UI_FLAG_OVERLAY)
|
|
|
|
{
|
|
|
|
list_append(&ui->overlayList, &box->overlayElt);
|
|
|
|
}
|
2022-08-21 15:07:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//maybe this should be a warning that we're trying to make the box twice in the same frame?
|
|
|
|
LOG_WARNING("trying to make ui box '%.*s' multiple times in the same frame\n", (int)box->string.len, box->string.ptr);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//NOTE: setup per-frame state
|
|
|
|
box->frameCounter = ui->frameCounter;
|
|
|
|
box->string = str8_push_copy(&ui->frameArena, string);
|
|
|
|
box->flags = flags;
|
|
|
|
|
2023-03-07 18:06:04 +00:00
|
|
|
//NOTE: create style and setup non-inherited attributes to default values
|
2023-03-06 18:33:01 +00:00
|
|
|
box->targetStyle = mem_arena_alloc_type(&ui->frameArena, ui_style);
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_apply_style_with_mask(box->targetStyle, &UI_STYLE_DEFAULTS, ~0ULL);
|
2023-03-06 18:33:01 +00:00
|
|
|
|
|
|
|
//NOTE: set tags, before rules and last box
|
|
|
|
box->tags = ui->nextBoxTags;
|
|
|
|
ui->nextBoxTags = (list_info){0};
|
|
|
|
|
2023-03-08 10:09:28 +00:00
|
|
|
box->beforeRules = ui->nextBoxBeforeRules;
|
2023-03-08 11:03:33 +00:00
|
|
|
for_list(&box->beforeRules, rule, ui_style_rule, boxElt)
|
2023-03-07 18:06:04 +00:00
|
|
|
{
|
|
|
|
rule->owner = box;
|
|
|
|
}
|
2023-03-08 10:09:28 +00:00
|
|
|
ui->nextBoxBeforeRules = (list_info){0};
|
2023-03-07 18:06:04 +00:00
|
|
|
|
2023-03-08 10:09:28 +00:00
|
|
|
box->afterRules = ui->nextBoxAfterRules;
|
2023-03-08 11:03:33 +00:00
|
|
|
for_list(&box->afterRules, rule, ui_style_rule, boxElt)
|
2023-03-08 10:09:28 +00:00
|
|
|
{
|
|
|
|
rule->owner = box;
|
|
|
|
}
|
|
|
|
ui->nextBoxAfterRules = (list_info){0};
|
2022-08-15 08:20:29 +00:00
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
return(box);
|
|
|
|
}
|
|
|
|
|
|
|
|
ui_box* ui_box_begin_str8(str8 string, ui_flags flags)
|
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
ui_box* box = ui_box_make_str8(string, flags);
|
|
|
|
ui_box_push(box);
|
|
|
|
return(box);
|
|
|
|
}
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
ui_box* ui_box_end(void)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
ui_box* box = ui_box_top();
|
|
|
|
DEBUG_ASSERT(box, "box stack underflow");
|
|
|
|
|
|
|
|
ui_box_pop();
|
2023-03-06 18:33:01 +00:00
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
return(box);
|
|
|
|
}
|
|
|
|
|
2023-03-11 18:05:25 +00:00
|
|
|
void ui_box_set_draw_proc(ui_box* box, ui_box_draw_proc proc, void* data)
|
2022-08-15 15:20:44 +00:00
|
|
|
{
|
2023-03-11 18:05:25 +00:00
|
|
|
box->drawProc = proc;
|
|
|
|
box->drawData = data;
|
2022-08-15 15:20:44 +00:00
|
|
|
}
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
void ui_box_set_closed(ui_box* box, bool closed)
|
|
|
|
{
|
|
|
|
box->closed = closed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ui_box_closed(ui_box* box)
|
|
|
|
{
|
|
|
|
return(box->closed);
|
|
|
|
}
|
|
|
|
|
2022-08-15 08:20:29 +00:00
|
|
|
void ui_box_activate(ui_box* box)
|
|
|
|
{
|
|
|
|
box->active = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_box_deactivate(ui_box* box)
|
|
|
|
{
|
|
|
|
box->active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ui_box_active(ui_box* box)
|
|
|
|
{
|
|
|
|
return(box->active);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_box_set_hot(ui_box* box, bool hot)
|
|
|
|
{
|
|
|
|
box->hot = hot;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ui_box_hot(ui_box* box)
|
|
|
|
{
|
|
|
|
return(box->hot);
|
|
|
|
}
|
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
ui_sig ui_box_sig(ui_box* box)
|
|
|
|
{
|
2022-08-19 17:46:31 +00:00
|
|
|
//NOTE: compute input signals
|
|
|
|
ui_sig sig = {0};
|
|
|
|
|
|
|
|
sig.box = box;
|
|
|
|
|
|
|
|
if(!box->closed && !box->parentClosed)
|
|
|
|
{
|
|
|
|
vec2 mousePos = ui_mouse_position();
|
|
|
|
|
|
|
|
sig.hovering = ui_box_hovering(box, mousePos);
|
|
|
|
|
|
|
|
if(box->flags & UI_FLAG_CLICKABLE)
|
|
|
|
{
|
|
|
|
if(sig.hovering)
|
|
|
|
{
|
2023-03-04 18:24:30 +00:00
|
|
|
sig.pressed = mp_mouse_pressed(MP_MOUSE_LEFT);
|
2022-08-19 17:46:31 +00:00
|
|
|
if(sig.pressed)
|
|
|
|
{
|
|
|
|
box->dragging = true;
|
|
|
|
}
|
2023-03-04 18:24:30 +00:00
|
|
|
sig.doubleClicked = mp_mouse_double_clicked(MP_MOUSE_LEFT);
|
|
|
|
sig.rightPressed = mp_mouse_pressed(MP_MOUSE_RIGHT);
|
2022-08-19 17:46:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-04 18:24:30 +00:00
|
|
|
sig.released = mp_mouse_released(MP_MOUSE_LEFT);
|
2022-08-19 17:46:31 +00:00
|
|
|
if(sig.released)
|
|
|
|
{
|
|
|
|
if(box->dragging && sig.hovering)
|
|
|
|
{
|
2023-03-04 18:24:30 +00:00
|
|
|
sig.clicked = true;
|
2022-08-19 17:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-04 18:24:30 +00:00
|
|
|
if(!mp_mouse_down(MP_MOUSE_LEFT))
|
2022-08-19 17:46:31 +00:00
|
|
|
{
|
|
|
|
box->dragging = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sig.dragging = box->dragging;
|
|
|
|
}
|
|
|
|
|
|
|
|
sig.mouse = (vec2){mousePos.x - box->rect.x, mousePos.y - box->rect.y};
|
|
|
|
sig.delta = ui_mouse_delta();
|
|
|
|
sig.wheel = ui_mouse_wheel();
|
|
|
|
}
|
|
|
|
return(sig);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ui_box_hidden(ui_box* box)
|
|
|
|
{
|
|
|
|
return(box->closed || box->parentClosed);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Auto-layout
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void ui_animate_f32(ui_context* ui, f32* value, f32 target, f32 animationTime)
|
|
|
|
{
|
|
|
|
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 ui_animate_color(ui_context* ui, mg_color* color, mg_color target, f32 animationTime)
|
|
|
|
{
|
|
|
|
for(int i=0; i<4; i++)
|
|
|
|
{
|
|
|
|
ui_animate_f32(ui, &color->c[i], target.c[i], animationTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
void ui_animate_ui_size(ui_context* ui, ui_size* size, ui_size target, f32 animationTime)
|
|
|
|
{
|
|
|
|
size->kind = target.kind;
|
|
|
|
ui_animate_f32(ui, &size->value, target.value, animationTime);
|
2023-03-09 11:36:38 +00:00
|
|
|
ui_animate_f32(ui, &size->relax, target.relax, animationTime);
|
2022-08-19 17:46:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
void ui_box_animate_style(ui_context* ui, ui_box* box)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2022-08-15 08:20:29 +00:00
|
|
|
ui_style* targetStyle = box->targetStyle;
|
2022-08-14 16:19:40 +00:00
|
|
|
DEBUG_ASSERT(targetStyle);
|
|
|
|
|
|
|
|
f32 animationTime = targetStyle->animationTime;
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
//NOTE: interpolate based on transition values
|
2023-03-11 18:05:25 +00:00
|
|
|
ui_style_mask mask = box->targetStyle->animationMask;
|
2022-08-15 22:01:07 +00:00
|
|
|
|
2022-08-16 07:58:16 +00:00
|
|
|
if(box->fresh)
|
2022-08-15 22:01:07 +00:00
|
|
|
{
|
2023-03-07 09:33:22 +00:00
|
|
|
box->style = *targetStyle;
|
2022-08-15 22:01:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-11 18:05:25 +00:00
|
|
|
if(mask & UI_STYLE_SIZE_WIDTH)
|
2022-08-19 17:46:31 +00:00
|
|
|
{
|
2023-03-08 18:53:28 +00:00
|
|
|
ui_animate_ui_size(ui, &box->style.size.c[UI_AXIS_X], targetStyle->size.c[UI_AXIS_X], animationTime);
|
2022-08-19 17:46:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-08 18:53:28 +00:00
|
|
|
box->style.size.c[UI_AXIS_X] = targetStyle->size.c[UI_AXIS_X];
|
2022-08-19 17:46:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 18:05:25 +00:00
|
|
|
if(mask & UI_STYLE_SIZE_HEIGHT)
|
2022-08-19 17:46:31 +00:00
|
|
|
{
|
2023-03-08 18:53:28 +00:00
|
|
|
ui_animate_ui_size(ui, &box->style.size.c[UI_AXIS_Y], targetStyle->size.c[UI_AXIS_Y], animationTime);
|
2022-08-19 17:46:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-08 18:53:28 +00:00
|
|
|
box->style.size.c[UI_AXIS_Y] = targetStyle->size.c[UI_AXIS_Y];
|
2022-08-19 17:46:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 18:05:25 +00:00
|
|
|
if(mask & UI_STYLE_COLOR)
|
2022-08-16 07:58:16 +00:00
|
|
|
{
|
2023-03-07 09:33:22 +00:00
|
|
|
ui_animate_color(ui, &box->style.color, targetStyle->color, animationTime);
|
2022-08-16 07:58:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-07 09:33:22 +00:00
|
|
|
box->style.color = targetStyle->color;
|
2022-08-16 07:58:16 +00:00
|
|
|
}
|
2022-08-15 22:01:07 +00:00
|
|
|
|
2023-03-06 18:33:01 +00:00
|
|
|
|
2023-03-11 18:05:25 +00:00
|
|
|
if(mask & UI_STYLE_BG_COLOR)
|
2022-08-16 07:58:16 +00:00
|
|
|
{
|
2023-03-07 09:33:22 +00:00
|
|
|
ui_animate_color(ui, &box->style.bgColor, targetStyle->bgColor, animationTime);
|
2022-08-16 07:58:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-07 09:33:22 +00:00
|
|
|
box->style.bgColor = targetStyle->bgColor;
|
2022-08-16 07:58:16 +00:00
|
|
|
}
|
2022-08-15 22:01:07 +00:00
|
|
|
|
2023-03-11 18:05:25 +00:00
|
|
|
if(mask & UI_STYLE_BORDER_COLOR)
|
2022-08-16 07:58:16 +00:00
|
|
|
{
|
2023-03-07 09:33:22 +00:00
|
|
|
ui_animate_color(ui, &box->style.borderColor, targetStyle->borderColor, animationTime);
|
2022-08-16 07:58:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-07 09:33:22 +00:00
|
|
|
box->style.borderColor = targetStyle->borderColor;
|
2022-08-16 07:58:16 +00:00
|
|
|
}
|
2022-08-15 22:01:07 +00:00
|
|
|
|
2023-03-11 18:05:25 +00:00
|
|
|
if(mask & UI_STYLE_FONT_SIZE)
|
2022-08-16 07:58:16 +00:00
|
|
|
{
|
2023-03-07 09:33:22 +00:00
|
|
|
ui_animate_f32(ui, &box->style.fontSize, targetStyle->fontSize, animationTime);
|
2022-08-16 07:58:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-07 09:33:22 +00:00
|
|
|
box->style.fontSize = targetStyle->fontSize;
|
2022-08-16 07:58:16 +00:00
|
|
|
}
|
2022-08-15 22:01:07 +00:00
|
|
|
|
2023-03-11 18:05:25 +00:00
|
|
|
if(mask & UI_STYLE_BORDER_SIZE)
|
2022-08-16 07:58:16 +00:00
|
|
|
{
|
2023-03-07 09:33:22 +00:00
|
|
|
ui_animate_f32(ui, &box->style.borderSize, targetStyle->borderSize, animationTime);
|
2022-08-16 07:58:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-07 09:33:22 +00:00
|
|
|
box->style.borderSize = targetStyle->borderSize;
|
2022-08-16 07:58:16 +00:00
|
|
|
}
|
2022-08-14 16:19:40 +00:00
|
|
|
|
2023-03-11 18:05:25 +00:00
|
|
|
if(mask & UI_STYLE_ROUNDNESS)
|
2022-08-16 07:58:16 +00:00
|
|
|
{
|
2023-03-07 09:33:22 +00:00
|
|
|
ui_animate_f32(ui, &box->style.roundness, targetStyle->roundness, animationTime);
|
2022-08-16 07:58:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-07 09:33:22 +00:00
|
|
|
box->style.roundness = targetStyle->roundness;
|
2022-08-16 07:58:16 +00:00
|
|
|
}
|
2023-03-07 18:06:04 +00:00
|
|
|
|
2023-03-08 18:53:28 +00:00
|
|
|
//NOTE: float target is animated in compute rect
|
|
|
|
box->style.floatTarget = targetStyle->floatTarget;
|
|
|
|
|
|
|
|
//TODO: non animatable attributes. use mask
|
2023-03-07 18:06:04 +00:00
|
|
|
box->style.layout = targetStyle->layout;
|
2023-03-07 09:33:22 +00:00
|
|
|
box->style.font = targetStyle->font;
|
2022-08-15 22:01:07 +00:00
|
|
|
}
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
2023-03-06 18:33:01 +00:00
|
|
|
void ui_apply_style_with_mask(ui_style* dst, ui_style* src, ui_style_mask mask)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-08 10:09:28 +00:00
|
|
|
if(mask & UI_STYLE_SIZE_WIDTH)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-08 18:53:28 +00:00
|
|
|
dst->size.c[UI_AXIS_X] = src->size.c[UI_AXIS_X];
|
2023-03-06 18:33:01 +00:00
|
|
|
}
|
2023-03-08 10:09:28 +00:00
|
|
|
if(mask & UI_STYLE_SIZE_HEIGHT)
|
2023-03-06 18:33:01 +00:00
|
|
|
{
|
2023-03-08 18:53:28 +00:00
|
|
|
dst->size.c[UI_AXIS_Y] = src->size.c[UI_AXIS_Y];
|
2023-03-07 18:06:04 +00:00
|
|
|
}
|
|
|
|
if(mask & UI_STYLE_LAYOUT_AXIS)
|
|
|
|
{
|
|
|
|
dst->layout.axis = src->layout.axis;
|
|
|
|
}
|
|
|
|
if(mask & UI_STYLE_LAYOUT_ALIGN_X)
|
|
|
|
{
|
|
|
|
dst->layout.align.x = src->layout.align.x;
|
|
|
|
}
|
|
|
|
if(mask & UI_STYLE_LAYOUT_ALIGN_Y)
|
|
|
|
{
|
|
|
|
dst->layout.align.y = src->layout.align.y;
|
|
|
|
}
|
|
|
|
if(mask & UI_STYLE_LAYOUT_SPACING)
|
|
|
|
{
|
|
|
|
dst->layout.spacing = src->layout.spacing;
|
|
|
|
}
|
|
|
|
if(mask & UI_STYLE_LAYOUT_MARGIN_X)
|
|
|
|
{
|
2023-03-09 15:02:46 +00:00
|
|
|
dst->layout.margin.x = src->layout.margin.x;
|
2023-03-07 18:06:04 +00:00
|
|
|
}
|
|
|
|
if(mask & UI_STYLE_LAYOUT_MARGIN_Y)
|
|
|
|
{
|
|
|
|
dst->layout.margin.y = src->layout.margin.y;
|
|
|
|
}
|
|
|
|
if(mask & UI_STYLE_FLOAT_X)
|
|
|
|
{
|
2023-03-08 18:53:28 +00:00
|
|
|
dst->floating.c[UI_AXIS_X] = src->floating.c[UI_AXIS_X];
|
2023-03-07 18:06:04 +00:00
|
|
|
dst->floatTarget.x = src->floatTarget.x;
|
|
|
|
}
|
|
|
|
if(mask & UI_STYLE_FLOAT_Y)
|
|
|
|
{
|
2023-03-08 18:53:28 +00:00
|
|
|
dst->floating.c[UI_AXIS_Y] = src->floating.c[UI_AXIS_Y];
|
2023-03-07 18:06:04 +00:00
|
|
|
dst->floatTarget.y = src->floatTarget.y;
|
2023-03-06 18:33:01 +00:00
|
|
|
}
|
|
|
|
if(mask & UI_STYLE_COLOR)
|
|
|
|
{
|
|
|
|
dst->color = src->color;
|
|
|
|
}
|
|
|
|
if(mask & UI_STYLE_BG_COLOR)
|
|
|
|
{
|
|
|
|
dst->bgColor = src->bgColor;
|
|
|
|
}
|
|
|
|
if(mask & UI_STYLE_BORDER_COLOR)
|
|
|
|
{
|
|
|
|
dst->borderColor = src->borderColor;
|
|
|
|
}
|
|
|
|
if(mask & UI_STYLE_BORDER_SIZE)
|
|
|
|
{
|
|
|
|
dst->borderSize = src->borderSize;
|
|
|
|
}
|
|
|
|
if(mask & UI_STYLE_ROUNDNESS)
|
|
|
|
{
|
|
|
|
dst->roundness = src->roundness;
|
|
|
|
}
|
|
|
|
if(mask & UI_STYLE_FONT)
|
|
|
|
{
|
|
|
|
dst->font = src->font;
|
|
|
|
}
|
|
|
|
if(mask & UI_STYLE_FONT_SIZE)
|
|
|
|
{
|
|
|
|
dst->fontSize = src->fontSize;
|
|
|
|
}
|
|
|
|
if(mask & UI_STYLE_ANIMATION_TIME)
|
|
|
|
{
|
|
|
|
dst->animationTime = src->animationTime;
|
|
|
|
}
|
2023-03-11 18:05:25 +00:00
|
|
|
if(mask & UI_STYLE_ANIMATION_MASK)
|
2023-03-06 18:33:01 +00:00
|
|
|
{
|
2023-03-11 18:05:25 +00:00
|
|
|
dst->animationMask = src->animationMask;
|
2023-03-06 18:33:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-07 18:06:04 +00:00
|
|
|
bool ui_style_selector_match(ui_box* box, ui_style_rule* rule, ui_selector* selector)
|
2023-03-06 18:33:01 +00:00
|
|
|
{
|
|
|
|
bool res = false;
|
|
|
|
switch(selector->kind)
|
|
|
|
{
|
2023-03-07 18:06:04 +00:00
|
|
|
case UI_SEL_ANY:
|
|
|
|
res = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UI_SEL_OWNER:
|
|
|
|
res = (box == rule->owner);
|
|
|
|
break;
|
|
|
|
|
2023-03-06 18:33:01 +00:00
|
|
|
case UI_SEL_TEXT:
|
|
|
|
res = !str8_cmp(box->string, selector->text);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UI_SEL_TAG:
|
|
|
|
{
|
2023-03-08 11:03:33 +00:00
|
|
|
for_list(&box->tags, elt, ui_tag_elt, listElt)
|
2023-03-06 18:33:01 +00:00
|
|
|
{
|
|
|
|
if(elt->tag.hash == selector->tag.hash)
|
|
|
|
{
|
|
|
|
res = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
2023-03-07 18:06:04 +00:00
|
|
|
case UI_SEL_STATUS:
|
|
|
|
{
|
|
|
|
res = true;
|
|
|
|
if(selector->status & UI_HOVER)
|
|
|
|
{
|
2023-03-09 15:02:46 +00:00
|
|
|
res = res && ui_box_hovering(box, ui_mouse_position());
|
2023-03-07 18:06:04 +00:00
|
|
|
}
|
|
|
|
if(selector->status & UI_ACTIVE)
|
|
|
|
{
|
|
|
|
res = res && box->active;
|
|
|
|
}
|
|
|
|
if(selector->status & UI_DRAGGING)
|
|
|
|
{
|
|
|
|
res = res && box->dragging;
|
|
|
|
}
|
|
|
|
} break;
|
2023-03-06 18:33:01 +00:00
|
|
|
|
|
|
|
case UI_SEL_KEY:
|
|
|
|
res = ui_key_equal(box->key, selector->key);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_style_rule_match(ui_context* ui, ui_box* box, ui_style_rule* rule, list_info* buildList, list_info* tmpList)
|
|
|
|
{
|
2023-03-08 11:00:10 +00:00
|
|
|
ui_selector* selector = list_first_entry(&rule->pattern.l, ui_selector, listElt);
|
2023-03-07 18:06:04 +00:00
|
|
|
bool match = ui_style_selector_match(box, rule, selector);
|
|
|
|
|
2023-03-08 11:00:10 +00:00
|
|
|
selector = list_next_entry(&rule->pattern.l, selector, ui_selector, listElt);
|
2023-03-07 18:06:04 +00:00
|
|
|
while(match && selector && selector->op == UI_SEL_AND)
|
2023-03-06 18:33:01 +00:00
|
|
|
{
|
2023-03-07 18:06:04 +00:00
|
|
|
match = match && ui_style_selector_match(box, rule, selector);
|
2023-03-08 11:00:10 +00:00
|
|
|
selector = list_next_entry(&rule->pattern.l, selector, ui_selector, listElt);
|
2023-03-07 18:06:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(match)
|
|
|
|
{
|
|
|
|
if(!selector)
|
2023-03-06 18:33:01 +00:00
|
|
|
{
|
|
|
|
ui_apply_style_with_mask(box->targetStyle, rule->style, rule->mask);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//NOTE create derived rule if there's more than one selector
|
|
|
|
ui_style_rule* derived = mem_arena_alloc_type(&ui->frameArena, ui_style_rule);
|
|
|
|
derived->mask = rule->mask;
|
|
|
|
derived->style = rule->style;
|
2023-03-07 18:06:04 +00:00
|
|
|
derived->pattern.l = (list_info){&selector->listElt, rule->pattern.l.last};
|
2023-03-06 18:33:01 +00:00
|
|
|
|
2023-03-08 11:00:10 +00:00
|
|
|
list_append(buildList, &derived->buildElt);
|
|
|
|
list_append(tmpList, &derived->tmpElt);
|
2023-03-06 18:33:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_styling_prepass(ui_context* ui, ui_box* box, list_info* before, list_info* after)
|
|
|
|
{
|
|
|
|
//NOTE: inherit style from parent
|
|
|
|
if(box->parent)
|
|
|
|
{
|
2023-03-07 09:33:22 +00:00
|
|
|
ui_apply_style_with_mask(box->targetStyle,
|
|
|
|
box->parent->targetStyle,
|
|
|
|
UI_STYLE_MASK_INHERITED);
|
2023-03-06 18:33:01 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 15:43:58 +00:00
|
|
|
|
|
|
|
//NOTE: append box before rules to before and tmp
|
2023-03-06 18:33:01 +00:00
|
|
|
list_info tmpBefore = {0};
|
2023-03-08 11:03:33 +00:00
|
|
|
for_list(&box->beforeRules, rule, ui_style_rule, boxElt)
|
2023-03-06 18:33:01 +00:00
|
|
|
{
|
2023-03-08 11:00:10 +00:00
|
|
|
list_append(before, &rule->buildElt);
|
|
|
|
list_append(&tmpBefore, &rule->tmpElt);
|
2023-03-11 15:43:58 +00:00
|
|
|
}
|
|
|
|
//NOTE: match before rules
|
|
|
|
for_list(before, rule, ui_style_rule, buildElt)
|
|
|
|
{
|
2023-03-06 18:33:01 +00:00
|
|
|
ui_style_rule_match(ui, box, rule, before, &tmpBefore);
|
|
|
|
}
|
|
|
|
|
2023-03-11 15:43:58 +00:00
|
|
|
//NOTE: prepend box after rules to after and append them to tmp
|
2023-03-06 18:33:01 +00:00
|
|
|
list_info tmpAfter = {0};
|
2023-03-11 15:43:58 +00:00
|
|
|
for_list_reverse(&box->afterRules, rule, ui_style_rule, boxElt)
|
2023-03-06 18:33:01 +00:00
|
|
|
{
|
2023-03-11 15:43:58 +00:00
|
|
|
list_push(after, &rule->buildElt);
|
|
|
|
list_append(&tmpAfter, &rule->tmpElt);
|
2023-03-06 18:33:01 +00:00
|
|
|
}
|
2023-03-11 15:43:58 +00:00
|
|
|
|
|
|
|
//NOTE: match after rules
|
|
|
|
for_list(after, rule, ui_style_rule, buildElt)
|
2023-03-06 18:33:01 +00:00
|
|
|
{
|
|
|
|
ui_style_rule_match(ui, box, rule, after, &tmpAfter);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
2023-03-06 18:33:01 +00:00
|
|
|
//NOTE: compute static sizes
|
2023-03-09 15:02:46 +00:00
|
|
|
ui_box_animate_style(ui, box);
|
2022-08-14 16:19:40 +00:00
|
|
|
|
2023-03-06 18:33:01 +00:00
|
|
|
if(ui_box_hidden(box))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-07 09:33:22 +00:00
|
|
|
ui_style* style = &box->style;
|
2022-08-14 16:19:40 +00:00
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
mp_rect textBox = {0};
|
2023-03-08 18:53:28 +00:00
|
|
|
ui_size desiredSize[2] = {box->style.size.c[UI_AXIS_X],
|
|
|
|
box->style.size.c[UI_AXIS_Y]};
|
2022-08-15 08:20:29 +00:00
|
|
|
|
|
|
|
if( desiredSize[UI_AXIS_X].kind == UI_SIZE_TEXT
|
|
|
|
||desiredSize[UI_AXIS_Y].kind == UI_SIZE_TEXT)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
textBox = mg_text_bounding_box(style->font, style->fontSize, box->string);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i=0; i<UI_AXIS_COUNT; i++)
|
|
|
|
{
|
2022-08-15 08:20:29 +00:00
|
|
|
ui_size size = desiredSize[i];
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
if(size.kind == UI_SIZE_TEXT)
|
|
|
|
{
|
2023-03-08 18:53:28 +00:00
|
|
|
f32 margin = style->layout.margin.c[i];
|
2023-03-07 18:27:01 +00:00
|
|
|
box->rect.c[2+i] = textBox.c[2+i] + margin*2;
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
else if(size.kind == UI_SIZE_PIXELS)
|
|
|
|
{
|
2022-08-15 22:01:07 +00:00
|
|
|
box->rect.c[2+i] = size.value;
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 18:33:01 +00:00
|
|
|
//NOTE: descend in children
|
2023-03-08 11:03:33 +00:00
|
|
|
for_list(&box->children, child, ui_box, listElt)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-06 18:33:01 +00:00
|
|
|
ui_styling_prepass(ui, child, before, after);
|
|
|
|
}
|
|
|
|
|
|
|
|
//NOTE: remove temporary rules
|
2023-03-08 11:03:33 +00:00
|
|
|
for_list(&tmpBefore, rule, ui_style_rule, tmpElt)
|
2023-03-06 18:33:01 +00:00
|
|
|
{
|
2023-03-08 11:00:10 +00:00
|
|
|
list_remove(before, &rule->buildElt);
|
2023-03-06 18:33:01 +00:00
|
|
|
}
|
2023-03-08 11:03:33 +00:00
|
|
|
for_list(&tmpAfter, rule, ui_style_rule, tmpElt)
|
2023-03-06 18:33:01 +00:00
|
|
|
{
|
2023-03-08 11:00:10 +00:00
|
|
|
list_remove(after, &rule->buildElt);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 12:13:13 +00:00
|
|
|
bool ui_layout_downward_dependency(ui_box* child, int axis)
|
|
|
|
{
|
|
|
|
return( !ui_box_hidden(child)
|
|
|
|
&& !child->style.floating.c[axis]
|
|
|
|
&& child->style.size.c[axis].kind != UI_SIZE_PARENT
|
|
|
|
&& child->style.size.c[axis].kind != UI_SIZE_PARENT_MINUS_PIXELS);
|
|
|
|
}
|
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
void ui_layout_downward_dependent_size(ui_context* ui, ui_box* box, int axis)
|
|
|
|
{
|
2023-03-09 12:13:13 +00:00
|
|
|
//NOTE: layout children and compute spacing
|
2023-03-08 18:53:28 +00:00
|
|
|
f32 count = 0;
|
2023-03-09 12:13:13 +00:00
|
|
|
for_list(&box->children, child, ui_box, listElt)
|
|
|
|
{
|
|
|
|
if(!ui_box_hidden(child))
|
|
|
|
{
|
|
|
|
ui_layout_downward_dependent_size(ui, child, axis);
|
2023-03-07 18:06:04 +00:00
|
|
|
|
2023-03-09 12:13:13 +00:00
|
|
|
if( box->style.layout.axis == axis
|
|
|
|
&& !child->style.floating.c[axis])
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
box->spacing[axis] = maximum(0, count-1)*box->style.layout.spacing;
|
|
|
|
|
|
|
|
ui_size* size = &box->style.size.c[axis];
|
|
|
|
if(size->kind == UI_SIZE_CHILDREN)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-09 12:13:13 +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)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-09 12:13:13 +00:00
|
|
|
for_list(&box->children, child, ui_box, listElt)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-09 12:13:13 +00:00
|
|
|
if(ui_layout_downward_dependency(child, axis))
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2022-08-15 22:01:07 +00:00
|
|
|
sum += child->rect.c[2+axis];
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-09 12:13:13 +00:00
|
|
|
else
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-09 12:13:13 +00:00
|
|
|
for_list(&box->children, child, ui_box, listElt)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-09 12:13:13 +00:00
|
|
|
if(ui_layout_downward_dependency(child, axis))
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2022-08-15 22:01:07 +00:00
|
|
|
sum = maximum(sum, child->rect.c[2+axis]);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-08 18:53:28 +00:00
|
|
|
f32 margin = box->style.layout.margin.c[axis];
|
|
|
|
box->rect.c[2+axis] = sum + box->spacing[axis] + 2*margin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_layout_upward_dependent_size(ui_context* ui, ui_box* box, int axis)
|
|
|
|
{
|
2023-03-09 13:27:46 +00:00
|
|
|
//NOTE: re-compute/set size of children that depend on box's size
|
2023-03-08 18:53:28 +00:00
|
|
|
|
2023-03-09 12:13:13 +00:00
|
|
|
f32 margin = box->style.layout.margin.c[axis];
|
|
|
|
f32 availableSize = maximum(0, box->rect.c[2+axis] - box->spacing[axis] - 2*margin);
|
2023-03-09 11:36:38 +00:00
|
|
|
|
2023-03-09 12:13:13 +00:00
|
|
|
for_list(&box->children, child, ui_box, listElt)
|
2023-03-08 18:53:28 +00:00
|
|
|
{
|
2023-03-09 12:13:13 +00:00
|
|
|
ui_size* size = &child->style.size.c[axis];
|
|
|
|
if(size->kind == UI_SIZE_PARENT)
|
2023-03-08 18:53:28 +00:00
|
|
|
{
|
2023-03-09 12:13:13 +00:00
|
|
|
child->rect.c[2+axis] = availableSize * size->value;
|
2023-03-08 18:53:28 +00:00
|
|
|
}
|
2023-03-09 12:13:13 +00:00
|
|
|
else if(size->kind == UI_SIZE_PARENT_MINUS_PIXELS)
|
2023-03-09 11:36:38 +00:00
|
|
|
{
|
2023-03-09 12:13:13 +00:00
|
|
|
child->rect.c[2+axis] = maximum(0, availableSize - size->value);
|
2023-03-09 11:36:38 +00:00
|
|
|
}
|
2023-03-08 18:53:28 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 12:13:13 +00:00
|
|
|
//NOTE: solve downard conflicts
|
2023-03-11 15:27:21 +00:00
|
|
|
int overflowFlag = (UI_FLAG_ALLOW_OVERFLOW_X << axis);
|
2023-03-09 11:36:38 +00:00
|
|
|
f32 sum = 0;
|
|
|
|
|
|
|
|
if(box->style.layout.axis == axis)
|
|
|
|
{
|
2023-03-09 12:13:13 +00:00
|
|
|
//NOTE: if we're solving in the layout axis, first compute total sum of children and
|
|
|
|
// total slack available
|
2023-03-09 11:36:38 +00:00
|
|
|
f32 slack = 0;
|
|
|
|
|
|
|
|
for_list(&box->children, child, ui_box, listElt)
|
|
|
|
{
|
2023-03-09 12:13:13 +00:00
|
|
|
if( !ui_box_hidden(child)
|
|
|
|
&& !child->style.floating.c[axis])
|
2023-03-09 11:36:38 +00:00
|
|
|
{
|
|
|
|
sum += child->rect.c[2+axis];
|
|
|
|
slack += child->rect.c[2+axis] * child->style.size.c[axis].relax;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-11 15:27:21 +00:00
|
|
|
if(!(box->flags & overflowFlag))
|
2023-03-09 11:36:38 +00:00
|
|
|
{
|
2023-03-11 15:27:21 +00:00
|
|
|
//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 = ClampLowBound(totalContents - box->rect.c[2+axis], 0);
|
|
|
|
f32 alpha = Clamp(excess / slack, 0, 1);
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
for_list(&box->children, child, ui_box, listElt)
|
|
|
|
{
|
|
|
|
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];
|
|
|
|
}
|
2023-03-09 11:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
|
|
|
|
for_list(&box->children, child, ui_box, listElt)
|
|
|
|
{
|
|
|
|
if(!ui_box_hidden(child) && !child->style.floating.c[axis])
|
|
|
|
{
|
2023-03-11 15:27:21 +00:00
|
|
|
if(!(box->flags & overflowFlag))
|
|
|
|
{
|
|
|
|
f32 totalContents = child->rect.c[2+axis] + 2*box->style.layout.margin.c[axis];
|
|
|
|
f32 excess = ClampLowBound(totalContents - box->rect.c[2+axis], 0);
|
|
|
|
f32 relax = child->style.size.c[axis].relax;
|
|
|
|
child->rect.c[2+axis] -= minimum(excess, child->rect.c[2+axis]*relax);
|
|
|
|
}
|
2023-03-09 11:36:38 +00:00
|
|
|
sum = maximum(sum, child->rect.c[2+axis]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
box->childrenSum[axis] = sum;
|
|
|
|
|
|
|
|
//NOTE: recurse in children
|
|
|
|
for_list(&box->children, child, ui_box, listElt)
|
|
|
|
{
|
2023-03-09 12:13:13 +00:00
|
|
|
ui_layout_upward_dependent_size(ui, child, axis);
|
2023-03-09 11:36:38 +00:00
|
|
|
}
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ui_layout_compute_rect(ui_context* ui, ui_box* box, vec2 pos)
|
|
|
|
{
|
|
|
|
if(ui_box_hidden(box))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-15 22:01:07 +00:00
|
|
|
box->rect.x = pos.x;
|
|
|
|
box->rect.y = pos.y;
|
2022-08-14 16:19:40 +00:00
|
|
|
box->z = ui->z;
|
|
|
|
ui->z++;
|
|
|
|
|
2023-03-07 09:33:22 +00:00
|
|
|
ui_axis layoutAxis = box->style.layout.axis;
|
2022-08-14 16:19:40 +00:00
|
|
|
ui_axis secondAxis = (layoutAxis == UI_AXIS_X) ? UI_AXIS_Y : UI_AXIS_X;
|
2023-03-07 18:06:04 +00:00
|
|
|
f32 spacing = box->style.layout.spacing;
|
|
|
|
|
2023-03-08 18:53:28 +00:00
|
|
|
ui_align* align = box->style.layout.align.c;
|
2022-08-14 16:19:40 +00:00
|
|
|
|
2023-03-08 18:53:28 +00:00
|
|
|
vec2 origin = {box->rect.x,
|
|
|
|
box->rect.y};
|
2022-08-14 16:19:40 +00:00
|
|
|
vec2 currentPos = origin;
|
|
|
|
|
2023-03-07 18:06:04 +00:00
|
|
|
vec2 margin = {box->style.layout.margin.x,
|
|
|
|
box->style.layout.margin.y};
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
currentPos.x += margin.x;
|
|
|
|
currentPos.y += margin.y;
|
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
for(int i=0; i<UI_AXIS_COUNT; i++)
|
|
|
|
{
|
|
|
|
if(align[i] == UI_ALIGN_END)
|
|
|
|
{
|
2023-03-08 18:53:28 +00:00
|
|
|
currentPos.c[i] = origin.c[i] + box->rect.c[2+i] - (box->childrenSum[i] + box->spacing[i] + margin.c[i]);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(align[layoutAxis] == UI_ALIGN_CENTER)
|
|
|
|
{
|
2023-03-08 18:53:28 +00:00
|
|
|
currentPos.c[layoutAxis] = origin.c[layoutAxis]
|
|
|
|
+ 0.5*(box->rect.c[2+layoutAxis]
|
|
|
|
- (box->childrenSum[layoutAxis] + box->spacing[layoutAxis]));
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 11:03:33 +00:00
|
|
|
for_list(&box->children, child, ui_box, listElt)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
if(align[secondAxis] == UI_ALIGN_CENTER)
|
|
|
|
{
|
2023-03-08 18:53:28 +00:00
|
|
|
currentPos.c[secondAxis] = origin.c[secondAxis] + 0.5*(box->rect.c[2+secondAxis] - child->rect.c[2+secondAxis]);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vec2 childPos = currentPos;
|
|
|
|
for(int i=0; i<UI_AXIS_COUNT; i++)
|
|
|
|
{
|
2023-03-08 18:53:28 +00:00
|
|
|
if(child->style.floating.c[i])
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2022-08-15 22:01:07 +00:00
|
|
|
ui_style* style = child->targetStyle;
|
2023-03-11 18:05:25 +00:00
|
|
|
if((child->targetStyle->animationMask & (UI_STYLE_FLOAT_X << i))
|
2022-08-16 07:58:16 +00:00
|
|
|
&& !child->fresh)
|
2022-08-15 22:01:07 +00:00
|
|
|
{
|
2023-03-07 09:33:22 +00:00
|
|
|
ui_animate_f32(ui, &child->floatPos.c[i], child->style.floatTarget.c[i], style->animationTime);
|
2022-08-15 22:01:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-07 09:33:22 +00:00
|
|
|
child->floatPos.c[i] = child->style.floatTarget.c[i];
|
2022-08-15 22:01:07 +00:00
|
|
|
}
|
|
|
|
childPos.c[i] = origin.c[i] + child->floatPos.c[i];
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ui_layout_compute_rect(ui, child, childPos);
|
|
|
|
|
2023-03-08 18:53:28 +00:00
|
|
|
if(!child->style.floating.c[layoutAxis])
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-07 18:06:04 +00:00
|
|
|
currentPos.c[layoutAxis] += child->rect.c[2+layoutAxis] + spacing;
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_layout_find_next_hovered_recursive(ui_context* ui, ui_box* box, vec2 p)
|
|
|
|
{
|
|
|
|
if(ui_box_hidden(box))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hit = ui_rect_hit(box->rect, p);
|
|
|
|
if(hit && (box->flags & UI_FLAG_BLOCK_MOUSE))
|
|
|
|
{
|
|
|
|
ui->hovered = box;
|
|
|
|
}
|
|
|
|
if(hit || !(box->flags & UI_FLAG_CLIP))
|
|
|
|
{
|
2023-03-08 11:03:33 +00:00
|
|
|
for_list(&box->children, child, ui_box, listElt)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
ui_layout_find_next_hovered_recursive(ui, child, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_layout_find_next_hovered(ui_context* ui, vec2 p)
|
|
|
|
{
|
|
|
|
ui->hovered = 0;
|
|
|
|
ui_layout_find_next_hovered_recursive(ui, ui->root, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_solve_layout(ui_context* ui)
|
|
|
|
{
|
2023-03-06 18:33:01 +00:00
|
|
|
list_info beforeRules = {0};
|
|
|
|
list_info afterRules = {0};
|
|
|
|
|
2023-03-13 09:26:39 +00:00
|
|
|
//NOTE: style and compute static sizes
|
2023-03-06 18:33:01 +00:00
|
|
|
ui_styling_prepass(ui, ui->root, &beforeRules, &afterRules);
|
2022-08-14 16:19:40 +00:00
|
|
|
|
2023-03-13 09:26:39 +00:00
|
|
|
//NOTE: reparent overlay boxes
|
|
|
|
for_list(&ui->overlayList, box, ui_box, overlayElt)
|
|
|
|
{
|
|
|
|
if(box->parent)
|
|
|
|
{
|
|
|
|
list_remove(&box->parent->children, &box->listElt);
|
|
|
|
list_append(&ui->overlay->children, &box->listElt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//NOTE: compute layout
|
2022-08-14 16:19:40 +00:00
|
|
|
for(int axis=0; axis<UI_AXIS_COUNT; axis++)
|
|
|
|
{
|
|
|
|
ui_layout_downward_dependent_size(ui, ui->root, axis);
|
2023-03-07 18:27:01 +00:00
|
|
|
ui_layout_upward_dependent_size(ui, ui->root, axis);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
ui_layout_compute_rect(ui, ui->root, (vec2){0, 0});
|
|
|
|
|
|
|
|
vec2 p = ui_mouse_position();
|
|
|
|
ui_layout_find_next_hovered(ui, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Drawing
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2023-03-03 16:13:03 +00:00
|
|
|
void ui_rectangle_fill(mp_rect rect, f32 roundness)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
if(roundness)
|
|
|
|
{
|
2023-03-03 16:13:03 +00:00
|
|
|
mg_rounded_rectangle_fill(rect.x, rect.y, rect.w, rect.h, roundness);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-03 16:13:03 +00:00
|
|
|
mg_rectangle_fill(rect.x, rect.y, rect.w, rect.h);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 16:13:03 +00:00
|
|
|
void ui_rectangle_stroke(mp_rect rect, f32 roundness)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
if(roundness)
|
|
|
|
{
|
2023-03-03 16:13:03 +00:00
|
|
|
mg_rounded_rectangle_stroke(rect.x, rect.y, rect.w, rect.h, roundness);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-03 16:13:03 +00:00
|
|
|
mg_rectangle_stroke(rect.x, rect.y, rect.w, rect.h);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 16:13:03 +00:00
|
|
|
void ui_draw_box(ui_box* box)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
if(ui_box_hidden(box))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-07 09:33:22 +00:00
|
|
|
ui_style* style = &box->style;
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
if(box->flags & UI_FLAG_CLIP)
|
|
|
|
{
|
2023-03-08 18:53:28 +00:00
|
|
|
mg_clip_push(box->rect.x, box->rect.y, box->rect.w, box->rect.h);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(box->flags & UI_FLAG_DRAW_BACKGROUND)
|
|
|
|
{
|
2023-03-03 16:13:03 +00:00
|
|
|
mg_set_color(style->bgColor);
|
|
|
|
ui_rectangle_fill(box->rect, style->roundness);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 18:05:25 +00:00
|
|
|
if((box->flags & UI_FLAG_DRAW_PROC) && box->drawProc)
|
2022-08-19 17:46:31 +00:00
|
|
|
{
|
2023-03-11 18:05:25 +00:00
|
|
|
box->drawProc(box, box->drawData);
|
2022-08-19 17:46:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 11:03:33 +00:00
|
|
|
for_list(&box->children, child, ui_box, listElt)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-03 16:13:03 +00:00
|
|
|
ui_draw_box(child);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(box->flags & UI_FLAG_DRAW_TEXT)
|
|
|
|
{
|
|
|
|
mp_rect textBox = mg_text_bounding_box(style->font, style->fontSize, box->string);
|
|
|
|
|
2023-03-12 18:06:09 +00:00
|
|
|
f32 x = 0;
|
|
|
|
f32 y = 0;
|
|
|
|
switch(style->layout.align.x)
|
|
|
|
{
|
|
|
|
case UI_ALIGN_START:
|
|
|
|
x = box->rect.x + style->layout.margin.x;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UI_ALIGN_END:
|
|
|
|
x = box->rect.x + box->rect.w - style->layout.margin.x - textBox.w;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UI_ALIGN_CENTER:
|
|
|
|
x = box->rect.x + 0.5*(box->rect.w - textBox.w);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(style->layout.align.y)
|
|
|
|
{
|
|
|
|
case UI_ALIGN_START:
|
|
|
|
y = box->rect.y + style->layout.margin.y - textBox.y;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UI_ALIGN_END:
|
|
|
|
y = box->rect.y + box->rect.h - style->layout.margin.y - textBox.h + textBox.y;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UI_ALIGN_CENTER:
|
|
|
|
y = box->rect.y + 0.5*(box->rect.h - textBox.h) - textBox.y;
|
|
|
|
break;
|
|
|
|
}
|
2022-08-14 16:19:40 +00:00
|
|
|
|
2023-03-03 16:13:03 +00:00
|
|
|
mg_set_font(style->font);
|
|
|
|
mg_set_font_size(style->fontSize);
|
2023-03-06 18:33:01 +00:00
|
|
|
mg_set_color(style->color);
|
2022-08-14 16:19:40 +00:00
|
|
|
|
2023-03-03 16:13:03 +00:00
|
|
|
mg_move_to(x, y);
|
|
|
|
mg_text_outlines(box->string);
|
|
|
|
mg_fill();
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(box->flags & UI_FLAG_CLIP)
|
|
|
|
{
|
2023-03-08 18:53:28 +00:00
|
|
|
mg_clip_pop();
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(box->flags & UI_FLAG_DRAW_BORDER)
|
|
|
|
{
|
2023-03-03 16:13:03 +00:00
|
|
|
mg_set_width(style->borderSize);
|
|
|
|
mg_set_color(style->borderColor);
|
|
|
|
ui_rectangle_stroke(box->rect, style->roundness);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 16:13:03 +00:00
|
|
|
void ui_draw()
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
|
|
|
|
//NOTE: draw
|
2023-03-03 16:13:03 +00:00
|
|
|
bool oldTextFlip = mg_get_text_flip();
|
2023-03-08 12:19:18 +00:00
|
|
|
mg_set_text_flip(false);
|
2022-08-14 16:19:40 +00:00
|
|
|
|
2023-03-03 16:13:03 +00:00
|
|
|
ui_draw_box(ui->root);
|
2022-08-14 16:19:40 +00:00
|
|
|
|
2023-03-03 16:13:03 +00:00
|
|
|
mg_set_text_flip(oldTextFlip);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// frame begin/end
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
void ui_begin_frame(ui_style* defaultStyle, ui_style_mask defaultMask)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
|
|
|
|
mem_arena_clear(&ui->frameArena);
|
|
|
|
|
|
|
|
ui->frameCounter++;
|
|
|
|
f64 time = mp_get_time(MP_CLOCK_MONOTONIC);
|
|
|
|
ui->lastFrameDuration = time - ui->frameTime;
|
|
|
|
ui->frameTime = time;
|
|
|
|
|
|
|
|
ui->clipStack = 0;
|
|
|
|
ui->z = 0;
|
|
|
|
|
2023-03-08 14:03:35 +00:00
|
|
|
vec2 size = mg_canvas_size();
|
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
defaultMask &= UI_STYLE_COLOR
|
|
|
|
| UI_STYLE_BG_COLOR
|
|
|
|
| UI_STYLE_BORDER_COLOR
|
|
|
|
| UI_STYLE_FONT
|
|
|
|
| UI_STYLE_FONT_SIZE;
|
|
|
|
|
|
|
|
ui_style_match_before(ui_pattern_all(), defaultStyle, defaultMask);
|
|
|
|
ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, size.x},
|
|
|
|
.size.height = {UI_SIZE_PIXELS, size.y}},
|
|
|
|
UI_STYLE_SIZE);
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
ui->root = ui_box_begin("_root_", 0);
|
|
|
|
|
2023-03-08 14:03:35 +00:00
|
|
|
ui_style_mask contentStyleMask = UI_STYLE_SIZE
|
|
|
|
| UI_STYLE_LAYOUT
|
|
|
|
| UI_STYLE_FLOAT;
|
|
|
|
|
|
|
|
ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1},
|
|
|
|
.size.height = {UI_SIZE_PARENT, 1},
|
|
|
|
.layout = {UI_AXIS_Y, UI_ALIGN_START, UI_ALIGN_START},
|
2023-03-07 18:06:04 +00:00
|
|
|
.floating = {true, true},
|
|
|
|
.floatTarget = {0, 0}},
|
2023-03-08 14:03:35 +00:00
|
|
|
contentStyleMask);
|
2023-03-07 18:06:04 +00:00
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
ui_box* contents = ui_box_make("_contents_", 0);
|
2023-03-07 09:33:22 +00:00
|
|
|
|
2023-03-08 10:09:28 +00:00
|
|
|
ui_style_next(&(ui_style){.layout = {UI_AXIS_Y, UI_ALIGN_START, UI_ALIGN_START},
|
2023-03-07 18:06:04 +00:00
|
|
|
.floating = {true, true},
|
|
|
|
.floatTarget = {0, 0}},
|
|
|
|
UI_STYLE_LAYOUT | UI_STYLE_FLOAT_X | UI_STYLE_FLOAT_Y);
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
ui->overlay = ui_box_make("_overlay_", 0);
|
2023-03-13 09:26:39 +00:00
|
|
|
ui->overlayList = (list_info){0};
|
2023-03-06 18:33:01 +00:00
|
|
|
|
2023-03-08 10:09:28 +00:00
|
|
|
ui->nextBoxBeforeRules = (list_info){0};
|
|
|
|
ui->nextBoxAfterRules = (list_info){0};
|
2023-03-06 18:33:01 +00:00
|
|
|
ui->nextBoxTags = (list_info){0};
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
ui_box_push(contents);
|
|
|
|
}
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
void ui_end_frame(void)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
|
|
|
|
ui_box_pop();
|
|
|
|
|
|
|
|
ui_box* box = ui_box_end();
|
|
|
|
DEBUG_ASSERT(box == ui->root, "unbalanced box stack");
|
|
|
|
|
2022-08-15 08:20:29 +00:00
|
|
|
//TODO: check balancing of style stacks
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
//NOTE: layout
|
|
|
|
ui_solve_layout(ui);
|
|
|
|
|
|
|
|
//NOTE: prune unused boxes
|
|
|
|
for(int i=0; i<UI_BOX_MAP_BUCKET_COUNT; i++)
|
|
|
|
{
|
2023-03-08 11:03:33 +00:00
|
|
|
for_list_safe(&ui->boxMap[i], box, ui_box, bucketElt)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
if(box->frameCounter < ui->frameCounter)
|
|
|
|
{
|
2023-03-08 11:00:10 +00:00
|
|
|
list_remove(&ui->boxMap[i], &box->bucketElt);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Init / cleanup
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-08-19 17:46:31 +00:00
|
|
|
void ui_init(void)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2022-08-15 21:11:00 +00:00
|
|
|
ui_context* ui = &__uiThreadContext;
|
2022-08-14 16:19:40 +00:00
|
|
|
if(!ui->init)
|
|
|
|
{
|
2022-08-15 21:11:00 +00:00
|
|
|
__uiCurrentContext = &__uiThreadContext;
|
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
memset(ui, 0, sizeof(ui_context));
|
|
|
|
mem_arena_init(&ui->frameArena);
|
|
|
|
mem_pool_init(&ui->boxPool, sizeof(ui_box));
|
|
|
|
ui->init = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
void ui_cleanup(void)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
mem_arena_release(&ui->frameArena);
|
|
|
|
mem_pool_release(&ui->boxPool);
|
|
|
|
ui->init = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2023-03-09 15:02:46 +00:00
|
|
|
// label
|
2022-08-14 16:19:40 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
ui_sig ui_label_str8(str8 label)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-08 10:09:28 +00:00
|
|
|
ui_style_next(&(ui_style){.size.width = {UI_SIZE_TEXT, 0, 0},
|
2023-03-07 18:06:04 +00:00
|
|
|
.size.height = {UI_SIZE_TEXT, 0, 0}},
|
2023-03-08 10:09:28 +00:00
|
|
|
UI_STYLE_SIZE_WIDTH | UI_STYLE_SIZE_HEIGHT);
|
2023-03-07 18:06:04 +00:00
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
ui_flags flags = UI_FLAG_CLIP
|
|
|
|
| UI_FLAG_DRAW_TEXT;
|
2022-08-19 17:46:31 +00:00
|
|
|
ui_box* box = ui_box_make_str8(label, flags);
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
ui_sig sig = ui_box_sig(box);
|
|
|
|
return(sig);
|
|
|
|
}
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
ui_sig ui_label(const char* label)
|
|
|
|
{
|
2023-03-08 10:40:40 +00:00
|
|
|
return(ui_label_str8(STR8((char*)label)));
|
2022-08-19 17:46:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// button
|
|
|
|
//------------------------------------------------------------------------------
|
2023-03-11 18:05:25 +00:00
|
|
|
|
|
|
|
ui_sig ui_button_behavior(ui_box* box)
|
|
|
|
{
|
|
|
|
ui_sig sig = ui_box_sig(box);
|
|
|
|
|
|
|
|
if(sig.hovering)
|
|
|
|
{
|
|
|
|
ui_box_set_hot(box, true);
|
|
|
|
if(sig.dragging)
|
|
|
|
{
|
|
|
|
ui_box_activate(box);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui_box_set_hot(box, false);
|
|
|
|
}
|
|
|
|
if(!sig.dragging)
|
|
|
|
{
|
|
|
|
ui_box_deactivate(box);
|
|
|
|
}
|
|
|
|
return(sig);
|
|
|
|
}
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
ui_sig ui_button_str8(str8 label)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
|
2023-03-08 18:53:28 +00:00
|
|
|
ui_style defaultStyle = {.size.width = {UI_SIZE_TEXT},
|
|
|
|
.size.height = {UI_SIZE_TEXT},
|
2023-03-12 18:06:09 +00:00
|
|
|
.layout.align.x = UI_ALIGN_CENTER,
|
|
|
|
.layout.align.y = UI_ALIGN_CENTER,
|
2023-03-08 18:53:28 +00:00
|
|
|
.layout.margin.x = 5,
|
|
|
|
.layout.margin.y = 5,
|
2023-03-07 18:06:04 +00:00
|
|
|
.bgColor = {0.5, 0.5, 0.5, 1},
|
|
|
|
.borderColor = {0.2, 0.2, 0.2, 1},
|
2023-03-08 18:53:28 +00:00
|
|
|
.borderSize = 1,
|
2023-03-07 18:06:04 +00:00
|
|
|
.roundness = 10};
|
|
|
|
|
2023-03-08 10:09:28 +00:00
|
|
|
ui_style_mask defaultMask = UI_STYLE_SIZE_WIDTH
|
|
|
|
| UI_STYLE_SIZE_HEIGHT
|
2023-03-08 18:53:28 +00:00
|
|
|
| UI_STYLE_LAYOUT_MARGIN_X
|
|
|
|
| UI_STYLE_LAYOUT_MARGIN_Y
|
2023-03-12 18:06:09 +00:00
|
|
|
| UI_STYLE_LAYOUT_ALIGN_X
|
|
|
|
| UI_STYLE_LAYOUT_ALIGN_Y
|
2023-03-07 18:06:04 +00:00
|
|
|
| UI_STYLE_BG_COLOR
|
|
|
|
| UI_STYLE_BORDER_COLOR
|
|
|
|
| UI_STYLE_BORDER_SIZE
|
|
|
|
| UI_STYLE_ROUNDNESS;
|
|
|
|
|
2023-03-08 10:09:28 +00:00
|
|
|
ui_style_next(&defaultStyle, defaultMask);
|
2023-03-07 18:06:04 +00:00
|
|
|
|
|
|
|
ui_style activeStyle = {.bgColor = {0.3, 0.3, 0.3, 1},
|
|
|
|
.borderColor = {0.2, 0.2, 0.2, 1},
|
2023-03-08 18:53:28 +00:00
|
|
|
.borderSize = 2};
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_style_mask activeMask = UI_STYLE_BG_COLOR
|
|
|
|
| UI_STYLE_BORDER_COLOR
|
|
|
|
| UI_STYLE_BORDER_SIZE;
|
|
|
|
ui_pattern activePattern = {0};
|
|
|
|
ui_pattern_push(&ui->frameArena,
|
|
|
|
&activePattern,
|
|
|
|
(ui_selector){.kind = UI_SEL_STATUS,
|
|
|
|
.status = UI_ACTIVE|UI_HOVER});
|
2023-03-08 10:13:32 +00:00
|
|
|
ui_style_match_before(activePattern, &activeStyle, activeMask);
|
2023-03-07 18:06:04 +00:00
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
ui_flags flags = UI_FLAG_CLICKABLE
|
|
|
|
| UI_FLAG_CLIP
|
2023-03-07 18:06:04 +00:00
|
|
|
| UI_FLAG_DRAW_BACKGROUND
|
2022-08-14 16:19:40 +00:00
|
|
|
| UI_FLAG_DRAW_BORDER
|
|
|
|
| UI_FLAG_DRAW_TEXT
|
|
|
|
| UI_FLAG_HOT_ANIMATION
|
|
|
|
| UI_FLAG_ACTIVE_ANIMATION;
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
ui_box* box = ui_box_make_str8(label, flags);
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_tag_box(box, "button");
|
2023-03-06 18:33:01 +00:00
|
|
|
|
2023-03-11 18:05:25 +00:00
|
|
|
ui_sig sig = ui_button_behavior(box);
|
|
|
|
return(sig);
|
|
|
|
}
|
2022-08-14 16:19:40 +00:00
|
|
|
|
2023-03-11 18:05:25 +00:00
|
|
|
ui_sig ui_button(const char* label)
|
|
|
|
{
|
|
|
|
return(ui_button_str8(STR8((char*)label)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_checkbox_draw(ui_box* box, void* data)
|
|
|
|
{
|
|
|
|
bool checked = *(bool*)data;
|
|
|
|
if(checked)
|
2022-08-15 08:20:29 +00:00
|
|
|
{
|
2023-03-11 18:05:25 +00:00
|
|
|
mg_move_to(box->rect.x + 0.2*box->rect.w, box->rect.y + 0.5*box->rect.h);
|
|
|
|
mg_line_to(box->rect.x + 0.4*box->rect.w, box->rect.y + 0.75*box->rect.h);
|
|
|
|
mg_line_to(box->rect.x + 0.8*box->rect.w, box->rect.y + 0.2*box->rect.h);
|
|
|
|
|
|
|
|
mg_set_color(box->style.color);
|
|
|
|
mg_set_width(0.2*box->rect.w);
|
|
|
|
mg_set_joint(MG_JOINT_MITER);
|
|
|
|
mg_set_max_joint_excursion(0.2 * box->rect.h);
|
|
|
|
mg_stroke();
|
2022-08-15 08:20:29 +00:00
|
|
|
}
|
2023-03-11 18:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ui_sig ui_checkbox(const char* name, bool* checked)
|
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
|
|
|
|
ui_style defaultStyle = {.size.width = {UI_SIZE_PIXELS, 20},
|
|
|
|
.size.height = {UI_SIZE_PIXELS, 20},
|
|
|
|
.bgColor = {1, 1, 1, 1},
|
|
|
|
.color = {0, 0, 0, 1},
|
|
|
|
.borderColor = {0.2, 0.2, 0.2, 1},
|
|
|
|
.borderSize = 1,
|
|
|
|
.roundness = 5};
|
|
|
|
|
|
|
|
ui_style_mask defaultMask = UI_STYLE_SIZE_WIDTH
|
|
|
|
| UI_STYLE_SIZE_HEIGHT
|
|
|
|
| UI_STYLE_BG_COLOR
|
|
|
|
| UI_STYLE_COLOR
|
|
|
|
| UI_STYLE_BORDER_COLOR
|
|
|
|
| UI_STYLE_BORDER_SIZE
|
|
|
|
| UI_STYLE_ROUNDNESS;
|
|
|
|
|
|
|
|
ui_style_next(&defaultStyle, defaultMask);
|
|
|
|
|
|
|
|
ui_style activeStyle = {.bgColor = {0.5, 0.5, 0.5, 1},
|
|
|
|
.borderColor = {0.2, 0.2, 0.2, 1},
|
|
|
|
.borderSize = 2};
|
|
|
|
ui_style_mask activeMask = UI_STYLE_BG_COLOR
|
|
|
|
| UI_STYLE_BORDER_COLOR
|
|
|
|
| UI_STYLE_BORDER_SIZE;
|
|
|
|
ui_pattern activePattern = {0};
|
|
|
|
ui_pattern_push(&ui->frameArena,
|
|
|
|
&activePattern,
|
|
|
|
(ui_selector){.kind = UI_SEL_STATUS,
|
|
|
|
.status = UI_ACTIVE|UI_HOVER});
|
|
|
|
ui_style_match_before(activePattern, &activeStyle, activeMask);
|
|
|
|
|
|
|
|
ui_flags flags = UI_FLAG_CLICKABLE
|
|
|
|
| UI_FLAG_CLIP
|
|
|
|
| UI_FLAG_DRAW_BACKGROUND
|
|
|
|
| UI_FLAG_DRAW_PROC
|
|
|
|
| UI_FLAG_DRAW_BORDER
|
|
|
|
| UI_FLAG_HOT_ANIMATION
|
|
|
|
| UI_FLAG_ACTIVE_ANIMATION;
|
|
|
|
|
|
|
|
ui_box* box = ui_box_make(name, flags);
|
|
|
|
ui_tag_box(box, "checkbox");
|
|
|
|
|
|
|
|
ui_sig sig = ui_button_behavior(box);
|
|
|
|
if(sig.clicked)
|
2022-08-15 08:20:29 +00:00
|
|
|
{
|
2023-03-11 18:05:25 +00:00
|
|
|
*checked = !*checked;
|
2022-08-15 08:20:29 +00:00
|
|
|
}
|
2023-03-11 18:05:25 +00:00
|
|
|
ui_box_set_draw_proc(box, ui_checkbox_draw, checked);
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
return(sig);
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// slider / scrollbar
|
|
|
|
//------------------------------------------------------------------------------
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_box* ui_slider(const char* label, f32 thumbRatio, f32* scrollValue)
|
|
|
|
{
|
2023-03-08 10:13:32 +00:00
|
|
|
ui_style_match_before(ui_pattern_all(), &(ui_style){0}, UI_STYLE_LAYOUT);
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_box* frame = ui_box_begin(label, 0);
|
|
|
|
{
|
2023-03-08 10:09:28 +00:00
|
|
|
f32 beforeRatio = (*scrollValue) * (1. - thumbRatio);
|
|
|
|
f32 afterRatio = (1. - *scrollValue) * (1. - thumbRatio);
|
2023-03-07 18:06:04 +00:00
|
|
|
|
|
|
|
ui_axis trackAxis = (frame->rect.w > frame->rect.h) ? UI_AXIS_X : UI_AXIS_Y;
|
|
|
|
ui_axis secondAxis = (trackAxis == UI_AXIS_Y) ? UI_AXIS_X : UI_AXIS_Y;
|
|
|
|
f32 roundness = 0.5*frame->rect.c[2+secondAxis];
|
|
|
|
f32 animationTime = 0.5;
|
|
|
|
|
2023-03-08 10:09:28 +00:00
|
|
|
ui_style trackStyle = {.size.width = {UI_SIZE_PARENT, 1},
|
|
|
|
.size.height = {UI_SIZE_PARENT, 1},
|
|
|
|
.layout.axis = trackAxis,
|
|
|
|
.layout.align.x = UI_ALIGN_START,
|
|
|
|
.layout.align.y = UI_ALIGN_START,
|
|
|
|
.bgColor = {0.5, 0.5, 0.5, 1},
|
2023-03-07 18:06:04 +00:00
|
|
|
.roundness = roundness};
|
|
|
|
|
2023-03-08 10:09:28 +00:00
|
|
|
ui_style beforeStyle = trackStyle;
|
2023-03-08 18:53:28 +00:00
|
|
|
beforeStyle.size.c[trackAxis] = (ui_size){UI_SIZE_PARENT, beforeRatio};
|
2023-03-08 10:09:28 +00:00
|
|
|
|
|
|
|
ui_style afterStyle = trackStyle;
|
2023-03-08 18:53:28 +00:00
|
|
|
afterStyle.size.c[trackAxis] = (ui_size){UI_SIZE_PARENT, afterRatio};
|
2023-03-08 10:09:28 +00:00
|
|
|
|
|
|
|
ui_style thumbStyle = trackStyle;
|
2023-03-08 18:53:28 +00:00
|
|
|
thumbStyle.size.c[trackAxis] = (ui_size){UI_SIZE_PARENT, thumbRatio};
|
2023-03-08 10:09:28 +00:00
|
|
|
thumbStyle.bgColor = (mg_color){0.3, 0.3, 0.3, 1};
|
|
|
|
|
|
|
|
ui_style_mask styleMask = UI_STYLE_SIZE_WIDTH
|
|
|
|
| UI_STYLE_SIZE_HEIGHT
|
|
|
|
| UI_STYLE_LAYOUT
|
|
|
|
| UI_STYLE_BG_COLOR
|
|
|
|
| UI_STYLE_ROUNDNESS;
|
2023-03-07 18:06:04 +00:00
|
|
|
|
|
|
|
ui_flags trackFlags = UI_FLAG_CLIP
|
|
|
|
| UI_FLAG_DRAW_BACKGROUND
|
|
|
|
| UI_FLAG_HOT_ANIMATION
|
|
|
|
| UI_FLAG_ACTIVE_ANIMATION;
|
|
|
|
|
2023-03-08 10:09:28 +00:00
|
|
|
ui_style_next(&trackStyle, styleMask);
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_box* track = ui_box_begin("track", trackFlags);
|
|
|
|
|
2023-03-08 10:09:28 +00:00
|
|
|
ui_style_next(&beforeStyle, UI_STYLE_SIZE_WIDTH|UI_STYLE_SIZE_HEIGHT);
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_box* beforeSpacer = ui_box_make("before", 0);
|
2023-03-08 10:09:28 +00:00
|
|
|
|
2023-03-07 18:06:04 +00:00
|
|
|
|
|
|
|
ui_flags thumbFlags = UI_FLAG_CLICKABLE
|
|
|
|
| UI_FLAG_DRAW_BACKGROUND
|
|
|
|
| UI_FLAG_HOT_ANIMATION
|
|
|
|
| UI_FLAG_ACTIVE_ANIMATION;
|
|
|
|
|
2023-03-08 10:09:28 +00:00
|
|
|
ui_style_next(&thumbStyle, styleMask);
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_box* thumb = ui_box_make("thumb", thumbFlags);
|
|
|
|
|
2023-03-08 10:09:28 +00:00
|
|
|
|
|
|
|
ui_style_next(&afterStyle, UI_STYLE_SIZE_WIDTH|UI_STYLE_SIZE_HEIGHT);
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_box* afterSpacer = ui_box_make("after", 0);
|
2023-03-08 10:09:28 +00:00
|
|
|
|
2023-03-07 18:06:04 +00:00
|
|
|
ui_box_end();
|
|
|
|
|
|
|
|
//NOTE: interaction
|
|
|
|
ui_sig thumbSig = ui_box_sig(thumb);
|
|
|
|
if(thumbSig.dragging)
|
|
|
|
{
|
|
|
|
f32 trackExtents = track->rect.c[2+trackAxis] - thumb->rect.c[2+trackAxis];
|
|
|
|
f32 delta = thumbSig.delta.c[trackAxis]/trackExtents;
|
|
|
|
f32 oldValue = *scrollValue;
|
|
|
|
|
|
|
|
*scrollValue += delta;
|
|
|
|
*scrollValue = Clamp(*scrollValue, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ui_sig trackSig = ui_box_sig(track);
|
|
|
|
|
|
|
|
if(ui_box_active(frame))
|
|
|
|
{
|
|
|
|
//NOTE: activated from outside
|
|
|
|
ui_box_set_hot(track, true);
|
|
|
|
ui_box_set_hot(thumb, true);
|
|
|
|
ui_box_activate(track);
|
|
|
|
ui_box_activate(thumb);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(trackSig.hovering)
|
|
|
|
{
|
|
|
|
ui_box_set_hot(track, true);
|
|
|
|
ui_box_set_hot(thumb, true);
|
|
|
|
}
|
|
|
|
else if(thumbSig.wheel.c[trackAxis] == 0)
|
|
|
|
{
|
|
|
|
ui_box_set_hot(track, false);
|
|
|
|
ui_box_set_hot(thumb, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(thumbSig.dragging)
|
|
|
|
{
|
|
|
|
ui_box_activate(track);
|
|
|
|
ui_box_activate(thumb);
|
|
|
|
}
|
|
|
|
else if(thumbSig.wheel.c[trackAxis] == 0)
|
|
|
|
{
|
|
|
|
ui_box_deactivate(track);
|
|
|
|
ui_box_deactivate(thumb);
|
|
|
|
ui_box_deactivate(frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
} ui_box_end();
|
|
|
|
|
|
|
|
return(frame);
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// panels
|
|
|
|
//------------------------------------------------------------------------------
|
2023-03-09 13:27:46 +00:00
|
|
|
void ui_panel_begin(const char* str, ui_flags flags)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-09 13:27:46 +00:00
|
|
|
ui_box* box = ui_box_begin(str, flags | UI_FLAG_CLIP | UI_FLAG_BLOCK_MOUSE);
|
|
|
|
|
|
|
|
ui_style contentsStyle = {
|
|
|
|
.size.width = {UI_SIZE_CHILDREN},
|
|
|
|
.size.height = {UI_SIZE_CHILDREN},
|
|
|
|
.floating.x = true,
|
|
|
|
.floating.y = true,
|
|
|
|
.floatTarget = {-box->scroll.x, -box->scroll.y}};
|
2022-08-14 16:19:40 +00:00
|
|
|
|
2023-03-09 13:27:46 +00:00
|
|
|
ui_style_next(&contentsStyle, UI_STYLE_FLOAT);
|
|
|
|
ui_box_begin("contents", 0);
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 13:27:46 +00:00
|
|
|
void ui_panel_end(void)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2023-03-09 13:27:46 +00:00
|
|
|
ui_box* contents = ui_box_top();
|
|
|
|
ui_box_end();
|
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
ui_box* panel = ui_box_top();
|
2022-08-15 16:28:13 +00:00
|
|
|
ui_sig sig = ui_box_sig(panel);
|
2022-08-14 16:19:40 +00:00
|
|
|
|
2023-03-09 13:27:46 +00:00
|
|
|
f32 contentsW = ClampLowBound(contents->rect.w, panel->rect.w);
|
|
|
|
f32 contentsH = ClampLowBound(contents->rect.h, panel->rect.h);
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
contentsW = ClampLowBound(contentsW, 1);
|
|
|
|
contentsH = ClampLowBound(contentsH, 1);
|
|
|
|
|
2022-08-15 16:28:13 +00:00
|
|
|
ui_box* scrollBarX = 0;
|
|
|
|
ui_box* scrollBarY = 0;
|
|
|
|
|
2023-03-09 13:27:46 +00:00
|
|
|
bool needsScrollX = contentsW > panel->rect.w;
|
|
|
|
bool needsScrollY = contentsH > panel->rect.h;
|
|
|
|
|
|
|
|
if(needsScrollX)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2022-08-15 16:28:13 +00:00
|
|
|
f32 thumbRatioX = panel->rect.w / contentsW;
|
|
|
|
f32 sliderX = panel->scroll.x /(contentsW - panel->rect.w);
|
2022-08-14 16:19:40 +00:00
|
|
|
|
2023-03-09 13:27:46 +00:00
|
|
|
ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1., 0},
|
|
|
|
.size.height = {UI_SIZE_PIXELS, 10, 0},
|
|
|
|
.floating.x = true,
|
|
|
|
.floating.y = true,
|
|
|
|
.floatTarget = {0, panel->rect.h - 10}},
|
|
|
|
UI_STYLE_SIZE
|
|
|
|
|UI_STYLE_FLOAT);
|
|
|
|
|
|
|
|
scrollBarX = ui_slider("scrollerX", thumbRatioX, &sliderX);
|
2022-08-14 16:19:40 +00:00
|
|
|
|
2022-08-15 16:28:13 +00:00
|
|
|
panel->scroll.x = sliderX * (contentsW - panel->rect.w);
|
|
|
|
if(sig.hovering)
|
|
|
|
{
|
|
|
|
panel->scroll.x += sig.wheel.x;
|
|
|
|
ui_box_activate(scrollBarX);
|
|
|
|
}
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 13:27:46 +00:00
|
|
|
if(needsScrollY)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
2022-08-15 16:28:13 +00:00
|
|
|
f32 thumbRatioY = panel->rect.h / contentsH;
|
|
|
|
f32 sliderY = panel->scroll.y /(contentsH - panel->rect.h);
|
2022-08-14 16:19:40 +00:00
|
|
|
|
2023-03-09 13:27:46 +00:00
|
|
|
f32 spacerSize = needsScrollX ? 10 : 0;
|
|
|
|
|
|
|
|
ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, 10, 0},
|
|
|
|
.size.height = {UI_SIZE_PARENT_MINUS_PIXELS, spacerSize, 0},
|
|
|
|
.floating.x = true,
|
|
|
|
.floating.y = true,
|
|
|
|
.floatTarget = {panel->rect.w - 10, 0}},
|
|
|
|
UI_STYLE_SIZE
|
|
|
|
|UI_STYLE_FLOAT);
|
|
|
|
|
|
|
|
scrollBarY = ui_slider("scrollerY", thumbRatioY, &sliderY);
|
2022-08-14 16:19:40 +00:00
|
|
|
|
2022-08-15 16:28:13 +00:00
|
|
|
panel->scroll.y = sliderY * (contentsH - panel->rect.h);
|
|
|
|
if(sig.hovering)
|
|
|
|
{
|
|
|
|
panel->scroll.y += sig.wheel.y;
|
|
|
|
ui_box_activate(scrollBarY);
|
|
|
|
}
|
|
|
|
}
|
2022-08-16 07:58:16 +00:00
|
|
|
panel->scroll.x = Clamp(panel->scroll.x, 0, contentsW - panel->rect.w);
|
|
|
|
panel->scroll.y = Clamp(panel->scroll.y, 0, contentsH - panel->rect.h);
|
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
ui_box_end();
|
|
|
|
}
|
2023-03-09 13:27:46 +00:00
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// tooltips
|
|
|
|
//------------------------------------------------------------------------------
|
2023-03-09 15:08:42 +00:00
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
ui_sig ui_tooltip_begin(const char* name)
|
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
|
|
|
|
vec2 p = ui_mouse_position();
|
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
ui_style style = {.size.width = {UI_SIZE_CHILDREN},
|
|
|
|
.size.height = {UI_SIZE_CHILDREN},
|
|
|
|
.floating.x = true,
|
|
|
|
.floating.y = true,
|
|
|
|
.floatTarget = {p.x, p.y}};
|
|
|
|
ui_style_mask mask = UI_STYLE_SIZE | UI_STYLE_FLOAT;
|
|
|
|
|
|
|
|
ui_style_next(&style, mask);
|
|
|
|
|
2023-03-13 09:53:21 +00:00
|
|
|
ui_flags flags = UI_FLAG_OVERLAY
|
|
|
|
| UI_FLAG_DRAW_BACKGROUND
|
2022-08-14 16:19:40 +00:00
|
|
|
| UI_FLAG_DRAW_BORDER;
|
|
|
|
|
|
|
|
ui_box* tooltip = ui_box_make(name, flags);
|
|
|
|
ui_box_push(tooltip);
|
|
|
|
|
|
|
|
return(ui_box_sig(tooltip));
|
|
|
|
}
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
void ui_tooltip_end(void)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
ui_box_pop(); // tooltip
|
|
|
|
}
|
2023-03-09 15:02:46 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Menus
|
|
|
|
//------------------------------------------------------------------------------
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
void ui_menu_bar_begin(const char* name)
|
|
|
|
{
|
2023-03-09 15:02:46 +00:00
|
|
|
ui_style style = {.size.width = {UI_SIZE_PARENT, 1, 0},
|
|
|
|
.size.height = {UI_SIZE_CHILDREN},
|
|
|
|
.layout.axis = UI_AXIS_X,
|
|
|
|
.layout.spacing = 20,};
|
|
|
|
ui_style_mask mask = UI_STYLE_SIZE
|
|
|
|
| UI_STYLE_LAYOUT_AXIS
|
|
|
|
| UI_STYLE_LAYOUT_SPACING;
|
2023-03-09 13:27:46 +00:00
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
ui_style_next(&style, mask);
|
|
|
|
ui_box* bar = ui_box_begin(name, UI_FLAG_DRAW_BACKGROUND);
|
2023-03-09 13:27:46 +00:00
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
ui_sig sig = ui_box_sig(bar);
|
2023-03-04 18:24:30 +00:00
|
|
|
if(!sig.hovering && mp_mouse_released(MP_MOUSE_LEFT))
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
ui_box_deactivate(bar);
|
|
|
|
}
|
|
|
|
}
|
2023-03-09 15:02:46 +00:00
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
void ui_menu_bar_end(void)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
ui_box_end(); // menu bar
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_menu_begin(const char* label)
|
|
|
|
{
|
2023-03-13 09:26:39 +00:00
|
|
|
ui_box* container = ui_box_make(label, 0);
|
|
|
|
ui_box_push(container);
|
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
ui_style_next(&(ui_style){.size.width = {UI_SIZE_TEXT},
|
|
|
|
.size.height = {UI_SIZE_TEXT}},
|
|
|
|
UI_STYLE_SIZE);
|
|
|
|
|
2022-08-14 16:19:40 +00:00
|
|
|
ui_box* button = ui_box_make(label, UI_FLAG_CLICKABLE | UI_FLAG_DRAW_TEXT);
|
2023-03-13 09:26:39 +00:00
|
|
|
ui_box* bar = container->parent;
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
ui_sig sig = ui_box_sig(button);
|
|
|
|
ui_sig barSig = ui_box_sig(bar);
|
|
|
|
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
ui_style style = {.size.width = {UI_SIZE_CHILDREN},
|
|
|
|
.size.height = {UI_SIZE_CHILDREN},
|
|
|
|
.floating.x = true,
|
|
|
|
.floating.y = true,
|
|
|
|
.floatTarget = {button->rect.x,
|
|
|
|
button->rect.y + button->rect.h},
|
|
|
|
.layout.axis = UI_AXIS_Y,
|
|
|
|
.layout.spacing = 5,
|
|
|
|
.layout.margin.x = 0,
|
|
|
|
.layout.margin.y = 5,
|
|
|
|
.bgColor = {0.2, 0.2, 0.2, 1}};
|
|
|
|
|
|
|
|
ui_style_mask mask = UI_STYLE_SIZE
|
|
|
|
| UI_STYLE_FLOAT
|
|
|
|
| UI_STYLE_LAYOUT
|
|
|
|
| UI_STYLE_BG_COLOR;
|
|
|
|
|
2023-03-13 09:26:39 +00:00
|
|
|
ui_flags flags = UI_FLAG_OVERLAY
|
|
|
|
| UI_FLAG_DRAW_BACKGROUND
|
2022-08-14 16:19:40 +00:00
|
|
|
| UI_FLAG_DRAW_BORDER;
|
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
ui_style_next(&style, mask);
|
2023-03-13 09:26:39 +00:00
|
|
|
ui_box* menu = ui_box_make("panel", flags);
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
if(ui_box_active(bar))
|
|
|
|
{
|
|
|
|
if(sig.hovering)
|
|
|
|
{
|
|
|
|
ui_box_activate(button);
|
|
|
|
}
|
|
|
|
else if(barSig.hovering)
|
|
|
|
{
|
|
|
|
ui_box_deactivate(button);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui_box_deactivate(button);
|
|
|
|
if(sig.pressed)
|
|
|
|
{
|
|
|
|
ui_box_activate(bar);
|
|
|
|
ui_box_activate(button);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ui_box_set_closed(menu, !ui_box_active(button));
|
|
|
|
ui_box_push(menu);
|
|
|
|
}
|
|
|
|
|
2022-08-19 17:46:31 +00:00
|
|
|
void ui_menu_end(void)
|
2022-08-14 16:19:40 +00:00
|
|
|
{
|
|
|
|
ui_box_pop(); // menu
|
2023-03-13 09:26:39 +00:00
|
|
|
ui_box_pop(); // container
|
2022-08-14 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
ui_sig ui_menu_button(const char* name)
|
2022-08-15 08:20:29 +00:00
|
|
|
{
|
2023-03-09 15:02:46 +00:00
|
|
|
ui_context* ui = ui_get_context();
|
2022-08-15 08:20:29 +00:00
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
ui_style_next(&(ui_style){.size.width = {UI_SIZE_TEXT},
|
|
|
|
.size.height = {UI_SIZE_TEXT},
|
|
|
|
.layout.margin.x = 5,
|
|
|
|
.bgColor = {0, 0, 0, 0}},
|
|
|
|
UI_STYLE_SIZE
|
|
|
|
|UI_STYLE_LAYOUT_MARGIN_X
|
|
|
|
|UI_STYLE_BG_COLOR);
|
2022-08-15 08:20:29 +00:00
|
|
|
|
2023-03-09 16:38:41 +00:00
|
|
|
ui_pattern pattern = {0};
|
2023-03-09 15:02:46 +00:00
|
|
|
ui_pattern_push(&ui->frameArena, &pattern, (ui_selector){.kind = UI_SEL_STATUS, .status = UI_HOVER});
|
2022-08-15 08:20:29 +00:00
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
ui_style style = {.bgColor = {0, 0, 1, 1}};
|
|
|
|
ui_style_mask mask = UI_STYLE_BG_COLOR;
|
|
|
|
ui_style_match_before(pattern, &style, mask);
|
2022-08-15 08:20:29 +00:00
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
ui_flags flags = UI_FLAG_CLICKABLE
|
|
|
|
| UI_FLAG_CLIP
|
|
|
|
| UI_FLAG_DRAW_TEXT
|
|
|
|
| UI_FLAG_DRAW_BACKGROUND;
|
2022-08-15 08:20:29 +00:00
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
ui_box* box = ui_box_make(name, flags);
|
|
|
|
ui_sig sig = ui_box_sig(box);
|
|
|
|
return(sig);
|
2022-08-15 08:20:29 +00:00
|
|
|
}
|
|
|
|
|
2023-03-12 18:06:09 +00:00
|
|
|
void ui_select_popup_draw_arrow(ui_box* box, void* data)
|
|
|
|
{
|
2023-03-13 09:53:21 +00:00
|
|
|
f32 r = minimum(box->parent->style.roundness, box->rect.w);
|
|
|
|
f32 cr = r*4*(sqrt(2)-1)/3;
|
|
|
|
|
|
|
|
mg_move_to(box->rect.x, box->rect.y);
|
|
|
|
mg_line_to(box->rect.x + box->rect.w - r, box->rect.y);
|
|
|
|
mg_cubic_to(box->rect.x + box->rect.w - cr, box->rect.y,
|
|
|
|
box->rect.x + box->rect.w, box->rect.y + cr,
|
|
|
|
box->rect.x + box->rect.w, box->rect.y + r);
|
|
|
|
mg_line_to(box->rect.x + box->rect.w, box->rect.y + box->rect.h - r);
|
|
|
|
mg_cubic_to(box->rect.x + box->rect.w, box->rect.y + box->rect.h - cr,
|
|
|
|
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);
|
|
|
|
mg_line_to(box->rect.x, box->rect.y + box->rect.h);
|
|
|
|
|
|
|
|
mg_set_color(box->style.bgColor);
|
|
|
|
mg_fill();
|
|
|
|
|
|
|
|
mg_move_to(box->rect.x + 0.25*box->rect.w, box->rect.y + 0.45*box->rect.h);
|
|
|
|
mg_line_to(box->rect.x + 0.5*box->rect.w, box->rect.y + 0.75*box->rect.h);
|
|
|
|
mg_line_to(box->rect.x + 0.75*box->rect.w, box->rect.y + 0.45*box->rect.h);
|
2023-03-12 18:06:09 +00:00
|
|
|
|
|
|
|
mg_set_color(box->style.color);
|
|
|
|
mg_fill();
|
|
|
|
}
|
|
|
|
|
|
|
|
ui_select_popup_info ui_select_popup(const char* name, ui_select_popup_info* info)
|
|
|
|
{
|
|
|
|
ui_select_popup_info result = *info;
|
|
|
|
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
|
2023-03-13 09:26:39 +00:00
|
|
|
ui_container(name, 0)
|
2023-03-12 18:06:09 +00:00
|
|
|
{
|
2023-03-13 09:26:39 +00:00
|
|
|
ui_box* button = ui_box_make("button",
|
|
|
|
UI_FLAG_CLICKABLE
|
|
|
|
|UI_FLAG_DRAW_BACKGROUND
|
|
|
|
|UI_FLAG_DRAW_BORDER
|
|
|
|
|UI_FLAG_ALLOW_OVERFLOW_X
|
|
|
|
|UI_FLAG_CLIP);
|
|
|
|
|
|
|
|
f32 maxOptionWidth = 0;
|
|
|
|
f32 lineHeight = 0;
|
|
|
|
mp_rect bbox = {0};
|
2023-03-12 18:06:09 +00:00
|
|
|
for(int i=0; i<info->optionCount; i++)
|
|
|
|
{
|
2023-03-13 09:26:39 +00:00
|
|
|
bbox = mg_text_bounding_box(button->style.font, button->style.fontSize, info->options[i]);
|
|
|
|
maxOptionWidth = maximum(maxOptionWidth, bbox.w);
|
|
|
|
}
|
|
|
|
f32 buttonWidth = maxOptionWidth + 2*button->style.layout.margin.x + button->rect.h;
|
|
|
|
|
|
|
|
ui_style_box_before(button,
|
|
|
|
ui_pattern_owner(),
|
|
|
|
&(ui_style){.size.width = {UI_SIZE_PIXELS, buttonWidth},
|
|
|
|
.size.height = {UI_SIZE_CHILDREN},
|
|
|
|
.layout.margin.x = 5,
|
2023-03-13 09:53:21 +00:00
|
|
|
.layout.margin.y = 1,
|
|
|
|
.roundness = 5,
|
2023-03-13 09:26:39 +00:00
|
|
|
.borderSize = 1,
|
|
|
|
.borderColor = {0.3, 0.3, 0.3, 1}},
|
|
|
|
UI_STYLE_SIZE
|
|
|
|
|UI_STYLE_LAYOUT_MARGIN_X
|
|
|
|
|UI_STYLE_LAYOUT_MARGIN_Y
|
2023-03-13 09:53:21 +00:00
|
|
|
|UI_STYLE_ROUNDNESS
|
2023-03-13 09:26:39 +00:00
|
|
|
|UI_STYLE_BORDER_SIZE
|
|
|
|
|UI_STYLE_BORDER_COLOR);
|
|
|
|
ui_box_push(button);
|
|
|
|
{
|
|
|
|
ui_label_str8(info->options[info->selectedIndex]);
|
|
|
|
|
|
|
|
ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, button->rect.h},
|
|
|
|
.size.height = {UI_SIZE_PIXELS, button->rect.h},
|
|
|
|
.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}},
|
|
|
|
UI_STYLE_SIZE
|
|
|
|
|UI_STYLE_FLOAT
|
|
|
|
|UI_STYLE_COLOR
|
|
|
|
|UI_STYLE_BG_COLOR);
|
|
|
|
|
2023-03-13 09:53:21 +00:00
|
|
|
ui_box* arrow = ui_box_make("arrow", UI_FLAG_DRAW_PROC);
|
2023-03-13 09:26:39 +00:00
|
|
|
ui_box_set_draw_proc(arrow, ui_select_popup_draw_arrow, 0);
|
|
|
|
|
|
|
|
} ui_box_pop();
|
|
|
|
|
|
|
|
//panel
|
|
|
|
ui_box* panel = ui_box_make("panel",
|
|
|
|
UI_FLAG_DRAW_BACKGROUND
|
|
|
|
|UI_FLAG_BLOCK_MOUSE
|
|
|
|
|UI_FLAG_OVERLAY);
|
|
|
|
|
|
|
|
//TODO: set width to max(button.w, max child...)
|
|
|
|
f32 containerWidth = maximum(maxOptionWidth + 2*panel->style.layout.margin.x,
|
|
|
|
button->rect.w);
|
|
|
|
|
|
|
|
ui_style_box_before(panel,
|
|
|
|
ui_pattern_owner(),
|
|
|
|
&(ui_style){.size.width = {UI_SIZE_PIXELS, containerWidth},
|
|
|
|
.size.height = {UI_SIZE_CHILDREN},
|
|
|
|
.floating.x = true,
|
|
|
|
.floating.y = true,
|
|
|
|
.floatTarget = {button->rect.x,
|
|
|
|
button->rect.y + button->rect.h},
|
|
|
|
.layout.axis = UI_AXIS_Y,
|
|
|
|
.layout.margin.x = 0,
|
|
|
|
.layout.margin.y = 5,
|
|
|
|
.bgColor = {0.2, 0.2, 0.2, 1}},
|
|
|
|
UI_STYLE_SIZE
|
|
|
|
|UI_STYLE_FLOAT
|
|
|
|
|UI_STYLE_LAYOUT
|
|
|
|
|UI_STYLE_BG_COLOR);
|
|
|
|
|
|
|
|
ui_box_push(panel);
|
|
|
|
{
|
|
|
|
for(int i=0; i<info->optionCount; i++)
|
2023-03-12 18:06:09 +00:00
|
|
|
{
|
2023-03-13 09:26:39 +00:00
|
|
|
ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1},
|
|
|
|
.size.height = {UI_SIZE_TEXT},
|
|
|
|
.layout.axis = UI_AXIS_Y,
|
|
|
|
.layout.align.x = UI_ALIGN_START,
|
|
|
|
.layout.margin.x = 5,
|
|
|
|
.layout.margin.y = 2.5},
|
|
|
|
UI_STYLE_SIZE
|
|
|
|
|UI_STYLE_LAYOUT_AXIS
|
|
|
|
|UI_STYLE_LAYOUT_ALIGN_X
|
|
|
|
|UI_STYLE_LAYOUT_MARGIN_X
|
|
|
|
|UI_STYLE_LAYOUT_MARGIN_Y);
|
|
|
|
|
|
|
|
|
|
|
|
ui_pattern pattern = {0};
|
|
|
|
ui_pattern_push(&ui->frameArena, &pattern, (ui_selector){.kind = UI_SEL_STATUS, .status = UI_HOVER});
|
|
|
|
ui_style_match_before(pattern, &(ui_style){.bgColor = {0, 0, 1, 1}}, UI_STYLE_BG_COLOR);
|
|
|
|
|
|
|
|
ui_box* box = ui_box_make_str8(info->options[i],
|
|
|
|
UI_FLAG_DRAW_TEXT
|
|
|
|
|UI_FLAG_CLICKABLE
|
|
|
|
|UI_FLAG_DRAW_BACKGROUND);
|
|
|
|
ui_sig sig = ui_box_sig(box);
|
|
|
|
if(sig.pressed)
|
|
|
|
{
|
|
|
|
result.selectedIndex = i;
|
|
|
|
}
|
2023-03-12 18:06:09 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-13 09:26:39 +00:00
|
|
|
ui_box_pop();
|
2023-03-12 18:06:09 +00:00
|
|
|
|
2023-03-13 09:26:39 +00:00
|
|
|
if(ui_box_active(panel) && mp_mouse_pressed(MP_MOUSE_LEFT))
|
|
|
|
{
|
|
|
|
ui_box_deactivate(panel);
|
|
|
|
}
|
|
|
|
else if(ui_box_sig(button).pressed)
|
|
|
|
{
|
|
|
|
ui_box_activate(panel);
|
|
|
|
}
|
|
|
|
ui_box_set_closed(panel, !ui_box_active(panel));
|
2023-03-12 18:06:09 +00:00
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:02:46 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// text box
|
|
|
|
//------------------------------------------------------------------------------
|
2022-08-15 08:20:29 +00:00
|
|
|
str32 ui_edit_replace_selection_with_codepoints(ui_context* ui, str32 codepoints, str32 input)
|
|
|
|
{
|
|
|
|
u32 start = minimum(ui->editCursor, ui->editMark);
|
|
|
|
u32 end = maximum(ui->editCursor, ui->editMark);
|
|
|
|
|
|
|
|
str32 before = str32_slice(codepoints, 0, start);
|
|
|
|
str32 after = str32_slice(codepoints, end, codepoints.len);
|
|
|
|
|
2023-03-09 16:38:41 +00:00
|
|
|
str32_list list = {0};
|
2022-08-15 08:20:29 +00:00
|
|
|
str32_list_push(&ui->frameArena, &list, before);
|
|
|
|
str32_list_push(&ui->frameArena, &list, input);
|
|
|
|
str32_list_push(&ui->frameArena, &list, after);
|
|
|
|
|
|
|
|
codepoints = str32_list_join(&ui->frameArena, list);
|
|
|
|
|
|
|
|
ui->editCursor = start + input.len;
|
|
|
|
ui->editMark = ui->editCursor;
|
|
|
|
return(codepoints);
|
|
|
|
}
|
|
|
|
|
|
|
|
str32 ui_edit_delete_selection(ui_context* ui, str32 codepoints)
|
|
|
|
{
|
|
|
|
return(ui_edit_replace_selection_with_codepoints(ui, codepoints, (str32){0}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_edit_copy_selection_to_clipboard(ui_context* ui, str32 codepoints)
|
|
|
|
{
|
|
|
|
if(ui->editCursor == ui->editMark)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
u32 start = minimum(ui->editCursor, ui->editMark);
|
|
|
|
u32 end = maximum(ui->editCursor, ui->editMark);
|
|
|
|
str32 selection = str32_slice(codepoints, start, end);
|
|
|
|
str8 string = utf8_push_from_codepoints(&ui->frameArena, selection);
|
|
|
|
|
|
|
|
mp_clipboard_clear();
|
|
|
|
mp_clipboard_set_string(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
str32 ui_edit_replace_selection_with_clipboard(ui_context* ui, str32 codepoints)
|
|
|
|
{
|
|
|
|
str8 string = mp_clipboard_get_string(&ui->frameArena);
|
|
|
|
str32 input = utf8_push_to_codepoints(&ui->frameArena, string);
|
|
|
|
str32 result = ui_edit_replace_selection_with_codepoints(ui, codepoints, input);
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
UI_EDIT_MOVE,
|
|
|
|
UI_EDIT_SELECT,
|
|
|
|
UI_EDIT_SELECT_EXTEND,
|
|
|
|
UI_EDIT_DELETE,
|
|
|
|
UI_EDIT_CUT,
|
|
|
|
UI_EDIT_COPY,
|
|
|
|
UI_EDIT_PASTE,
|
|
|
|
UI_EDIT_SELECT_ALL } ui_edit_op;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
UI_EDIT_MOVE_NONE = 0,
|
|
|
|
UI_EDIT_MOVE_ONE,
|
|
|
|
UI_EDIT_MOVE_WORD,
|
|
|
|
UI_EDIT_MOVE_LINE } ui_edit_move;
|
|
|
|
|
|
|
|
typedef struct ui_edit_command
|
|
|
|
{
|
|
|
|
mp_key_code key;
|
2023-03-04 18:24:30 +00:00
|
|
|
mp_keymod_flags mods;
|
2022-08-15 08:20:29 +00:00
|
|
|
|
|
|
|
ui_edit_op operation;
|
|
|
|
ui_edit_move move;
|
|
|
|
int direction;
|
|
|
|
|
|
|
|
} ui_edit_command;
|
|
|
|
|
2023-03-09 18:44:55 +00:00
|
|
|
#if OS_WIN64
|
|
|
|
#define OS_COPY_PASTE_MOD MP_KEYMOD_CTRL
|
|
|
|
#elif OS_MACOS
|
2023-03-11 15:43:58 +00:00
|
|
|
#define OS_COPY_PASTE_MOD MP_KEYMOD_CMD
|
2023-03-09 18:44:55 +00:00
|
|
|
#endif
|
|
|
|
|
2022-08-15 08:20:29 +00:00
|
|
|
const ui_edit_command UI_EDIT_COMMANDS[] = {
|
|
|
|
//NOTE(martin): move one left
|
|
|
|
{
|
|
|
|
.key = MP_KEY_LEFT,
|
|
|
|
.operation = UI_EDIT_MOVE,
|
|
|
|
.move = UI_EDIT_MOVE_ONE,
|
|
|
|
.direction = -1
|
|
|
|
},
|
|
|
|
//NOTE(martin): move one right
|
|
|
|
{
|
|
|
|
.key = MP_KEY_RIGHT,
|
|
|
|
.operation = UI_EDIT_MOVE,
|
|
|
|
.move = UI_EDIT_MOVE_ONE,
|
|
|
|
.direction = 1
|
|
|
|
},
|
|
|
|
//NOTE(martin): move start
|
|
|
|
{
|
|
|
|
.key = MP_KEY_Q,
|
|
|
|
.mods = MP_KEYMOD_CTRL,
|
|
|
|
.operation = UI_EDIT_MOVE,
|
|
|
|
.move = UI_EDIT_MOVE_LINE,
|
|
|
|
.direction = -1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.key = MP_KEY_UP,
|
|
|
|
.operation = UI_EDIT_MOVE,
|
|
|
|
.move = UI_EDIT_MOVE_LINE,
|
|
|
|
.direction = -1
|
|
|
|
},
|
|
|
|
//NOTE(martin): move end
|
|
|
|
{
|
|
|
|
.key = MP_KEY_E,
|
|
|
|
.mods = MP_KEYMOD_CTRL,
|
|
|
|
.operation = UI_EDIT_MOVE,
|
|
|
|
.move = UI_EDIT_MOVE_LINE,
|
|
|
|
.direction = 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.key = MP_KEY_DOWN,
|
|
|
|
.operation = UI_EDIT_MOVE,
|
|
|
|
.move = UI_EDIT_MOVE_LINE,
|
|
|
|
.direction = 1
|
|
|
|
},
|
|
|
|
//NOTE(martin): select one left
|
|
|
|
{
|
|
|
|
.key = MP_KEY_LEFT,
|
|
|
|
.mods = MP_KEYMOD_SHIFT,
|
|
|
|
.operation = UI_EDIT_SELECT,
|
|
|
|
.move = UI_EDIT_MOVE_ONE,
|
|
|
|
.direction = -1
|
|
|
|
},
|
|
|
|
//NOTE(martin): select one right
|
|
|
|
{
|
|
|
|
.key = MP_KEY_RIGHT,
|
|
|
|
.mods = MP_KEYMOD_SHIFT,
|
|
|
|
.operation = UI_EDIT_SELECT,
|
|
|
|
.move = UI_EDIT_MOVE_ONE,
|
|
|
|
.direction = 1
|
|
|
|
},
|
|
|
|
//NOTE(martin): extend select to start
|
|
|
|
{
|
|
|
|
.key = MP_KEY_Q,
|
|
|
|
.mods = MP_KEYMOD_CTRL | MP_KEYMOD_SHIFT,
|
|
|
|
.operation = UI_EDIT_SELECT_EXTEND,
|
|
|
|
.move = UI_EDIT_MOVE_LINE,
|
|
|
|
.direction = -1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.key = MP_KEY_UP,
|
|
|
|
.mods = MP_KEYMOD_SHIFT,
|
|
|
|
.operation = UI_EDIT_SELECT_EXTEND,
|
|
|
|
.move = UI_EDIT_MOVE_LINE,
|
|
|
|
.direction = -1
|
|
|
|
},
|
|
|
|
//NOTE(martin): extend select to end
|
|
|
|
{
|
|
|
|
.key = MP_KEY_E,
|
|
|
|
.mods = MP_KEYMOD_CTRL | MP_KEYMOD_SHIFT,
|
|
|
|
.operation = UI_EDIT_SELECT_EXTEND,
|
|
|
|
.move = UI_EDIT_MOVE_LINE,
|
|
|
|
.direction = 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.key = MP_KEY_DOWN,
|
|
|
|
.mods = MP_KEYMOD_SHIFT,
|
|
|
|
.operation = UI_EDIT_SELECT_EXTEND,
|
|
|
|
.move = UI_EDIT_MOVE_LINE,
|
|
|
|
.direction = 1
|
|
|
|
},
|
|
|
|
//NOTE(martin): select all
|
|
|
|
{
|
|
|
|
.key = MP_KEY_Q,
|
2023-03-09 18:44:55 +00:00
|
|
|
.mods = OS_COPY_PASTE_MOD,
|
2022-08-15 08:20:29 +00:00
|
|
|
.operation = UI_EDIT_SELECT_ALL,
|
|
|
|
.move = UI_EDIT_MOVE_NONE
|
|
|
|
},
|
|
|
|
//NOTE(martin): delete
|
|
|
|
{
|
|
|
|
.key = MP_KEY_DELETE,
|
|
|
|
.operation = UI_EDIT_DELETE,
|
|
|
|
.move = UI_EDIT_MOVE_ONE,
|
|
|
|
.direction = 1
|
|
|
|
},
|
|
|
|
//NOTE(martin): backspace
|
|
|
|
{
|
|
|
|
.key = MP_KEY_BACKSPACE,
|
|
|
|
.operation = UI_EDIT_DELETE,
|
|
|
|
.move = UI_EDIT_MOVE_ONE,
|
|
|
|
.direction = -1
|
|
|
|
},
|
|
|
|
//NOTE(martin): cut
|
|
|
|
{
|
|
|
|
.key = MP_KEY_X,
|
2023-03-09 18:44:55 +00:00
|
|
|
.mods = OS_COPY_PASTE_MOD,
|
2022-08-15 08:20:29 +00:00
|
|
|
.operation = UI_EDIT_CUT,
|
|
|
|
.move = UI_EDIT_MOVE_NONE
|
|
|
|
},
|
|
|
|
//NOTE(martin): copy
|
|
|
|
{
|
|
|
|
.key = MP_KEY_C,
|
2023-03-09 18:44:55 +00:00
|
|
|
.mods = OS_COPY_PASTE_MOD,
|
2022-08-15 08:20:29 +00:00
|
|
|
.operation = UI_EDIT_COPY,
|
|
|
|
.move = UI_EDIT_MOVE_NONE
|
|
|
|
},
|
|
|
|
//NOTE(martin): paste
|
|
|
|
{
|
|
|
|
.key = MP_KEY_V,
|
2023-03-09 18:44:55 +00:00
|
|
|
.mods = OS_COPY_PASTE_MOD,
|
2022-08-15 08:20:29 +00:00
|
|
|
.operation = UI_EDIT_PASTE,
|
|
|
|
.move = UI_EDIT_MOVE_NONE
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const u32 UI_EDIT_COMMAND_COUNT = sizeof(UI_EDIT_COMMANDS)/sizeof(ui_edit_command);
|
|
|
|
|
2023-03-03 19:09:44 +00:00
|
|
|
void ui_edit_perform_move(ui_context* ui, ui_edit_move move, int direction, u32 textLen)
|
2022-08-15 08:20:29 +00:00
|
|
|
{
|
|
|
|
switch(move)
|
|
|
|
{
|
|
|
|
case UI_EDIT_MOVE_NONE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UI_EDIT_MOVE_ONE:
|
|
|
|
{
|
2023-03-03 19:09:44 +00:00
|
|
|
if(direction < 0 && ui->editCursor > 0)
|
2022-08-15 08:20:29 +00:00
|
|
|
{
|
2023-03-03 19:09:44 +00:00
|
|
|
ui->editCursor--;
|
2022-08-15 08:20:29 +00:00
|
|
|
}
|
2023-03-03 19:09:44 +00:00
|
|
|
else if(direction > 0 && ui->editCursor < textLen)
|
2022-08-15 08:20:29 +00:00
|
|
|
{
|
2023-03-03 19:09:44 +00:00
|
|
|
ui->editCursor++;
|
2022-08-15 08:20:29 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case UI_EDIT_MOVE_LINE:
|
|
|
|
{
|
|
|
|
if(direction < 0)
|
|
|
|
{
|
2023-03-03 19:09:44 +00:00
|
|
|
ui->editCursor = 0;
|
2022-08-15 08:20:29 +00:00
|
|
|
}
|
|
|
|
else if(direction > 0)
|
|
|
|
{
|
2023-03-03 19:09:44 +00:00
|
|
|
ui->editCursor = textLen;
|
2022-08-15 08:20:29 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case UI_EDIT_MOVE_WORD:
|
|
|
|
DEBUG_ASSERT(0, "not implemented yet");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
str32 ui_edit_perform_operation(ui_context* ui, ui_edit_op operation, ui_edit_move move, int direction, str32 codepoints)
|
|
|
|
{
|
|
|
|
switch(operation)
|
|
|
|
{
|
|
|
|
case UI_EDIT_MOVE:
|
|
|
|
{
|
|
|
|
//NOTE(martin): we place the cursor on the direction-most side of the selection
|
|
|
|
// before performing the move
|
|
|
|
u32 cursor = direction < 0 ?
|
2023-03-03 19:09:44 +00:00
|
|
|
minimum(ui->editCursor, ui->editMark) :
|
|
|
|
maximum(ui->editCursor, ui->editMark);
|
|
|
|
ui->editCursor = cursor;
|
2022-08-15 08:20:29 +00:00
|
|
|
|
|
|
|
if(ui->editCursor == ui->editMark || move != UI_EDIT_MOVE_ONE)
|
|
|
|
{
|
|
|
|
//NOTE: we special case move-one when there is a selection
|
|
|
|
// (just place the cursor at begining/end of selection)
|
2023-03-03 19:09:44 +00:00
|
|
|
ui_edit_perform_move(ui, move, direction, codepoints.len);
|
2022-08-15 08:20:29 +00:00
|
|
|
}
|
|
|
|
ui->editMark = ui->editCursor;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case UI_EDIT_SELECT:
|
|
|
|
{
|
2023-03-03 19:09:44 +00:00
|
|
|
ui_edit_perform_move(ui, move, direction, codepoints.len);
|
2022-08-15 08:20:29 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case UI_EDIT_SELECT_EXTEND:
|
|
|
|
{
|
2023-03-03 19:09:44 +00:00
|
|
|
if((direction > 0) != (ui->editCursor > ui->editMark))
|
|
|
|
{
|
|
|
|
u32 tmp = ui->editCursor;
|
|
|
|
ui->editCursor = ui->editMark;
|
|
|
|
ui->editMark = tmp;
|
|
|
|
}
|
|
|
|
ui_edit_perform_move(ui, move, direction, codepoints.len);
|
2022-08-15 08:20:29 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case UI_EDIT_DELETE:
|
|
|
|
{
|
|
|
|
if(ui->editCursor == ui->editMark)
|
|
|
|
{
|
2023-03-03 19:09:44 +00:00
|
|
|
ui_edit_perform_move(ui, move, direction, codepoints.len);
|
2022-08-15 08:20:29 +00:00
|
|
|
}
|
|
|
|
codepoints = ui_edit_delete_selection(ui, codepoints);
|
|
|
|
ui->editMark = ui->editCursor;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case UI_EDIT_CUT:
|
|
|
|
{
|
|
|
|
ui_edit_copy_selection_to_clipboard(ui, codepoints);
|
|
|
|
codepoints = ui_edit_delete_selection(ui, codepoints);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case UI_EDIT_COPY:
|
|
|
|
{
|
|
|
|
ui_edit_copy_selection_to_clipboard(ui, codepoints);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case UI_EDIT_PASTE:
|
|
|
|
{
|
|
|
|
codepoints = ui_edit_replace_selection_with_clipboard(ui, codepoints);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case UI_EDIT_SELECT_ALL:
|
|
|
|
{
|
|
|
|
ui->editCursor = 0;
|
|
|
|
ui->editMark = codepoints.len;
|
|
|
|
} break;
|
|
|
|
}
|
2022-08-15 15:20:44 +00:00
|
|
|
ui->editCursorBlinkStart = ui->frameTime;
|
2022-08-15 08:20:29 +00:00
|
|
|
|
|
|
|
return(codepoints);
|
|
|
|
}
|
|
|
|
|
2023-03-03 16:13:03 +00:00
|
|
|
void ui_text_box_render(ui_box* box, void* data)
|
2022-08-15 15:20:44 +00:00
|
|
|
{
|
|
|
|
str32 codepoints = *(str32*)data;
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
|
|
|
|
u32 firstDisplayedChar = 0;
|
|
|
|
if(ui_box_active(box))
|
|
|
|
{
|
|
|
|
firstDisplayedChar = ui->editFirstDisplayedChar;
|
|
|
|
}
|
|
|
|
|
2023-03-07 09:33:22 +00:00
|
|
|
ui_style* style = &box->style;
|
2022-08-15 15:20:44 +00:00
|
|
|
mg_font_extents extents = mg_font_get_scaled_extents(style->font, style->fontSize);
|
|
|
|
f32 lineHeight = extents.ascent + extents.descent;
|
|
|
|
|
|
|
|
str32 before = str32_slice(codepoints, 0, firstDisplayedChar);
|
|
|
|
mp_rect beforeBox = mg_text_bounding_box_utf32(style->font, style->fontSize, before);
|
|
|
|
|
2023-03-05 16:03:00 +00:00
|
|
|
f32 textMargin = 5; //TODO: make that configurable
|
2022-08-15 15:20:44 +00:00
|
|
|
|
|
|
|
f32 textX = textMargin + box->rect.x - beforeBox.w;
|
|
|
|
f32 textTop = box->rect.y + 0.5*(box->rect.h - lineHeight);
|
|
|
|
f32 textY = textTop + extents.ascent ;
|
|
|
|
|
2023-03-03 19:09:44 +00:00
|
|
|
if(box->active)
|
2022-08-15 15:20:44 +00:00
|
|
|
{
|
2023-03-03 19:09:44 +00:00
|
|
|
u32 selectStart = minimum(ui->editCursor, ui->editMark);
|
|
|
|
u32 selectEnd = maximum(ui->editCursor, ui->editMark);
|
2022-08-15 15:20:44 +00:00
|
|
|
|
2023-03-03 19:09:44 +00:00
|
|
|
str32 beforeSelect = str32_slice(codepoints, 0, selectStart);
|
|
|
|
mp_rect beforeSelectBox = mg_text_bounding_box_utf32(style->font, style->fontSize, beforeSelect);
|
2023-03-05 16:03:00 +00:00
|
|
|
beforeSelectBox.x += textX;
|
2023-03-03 19:09:44 +00:00
|
|
|
beforeSelectBox.y += textY;
|
2022-08-15 15:20:44 +00:00
|
|
|
|
2023-03-03 19:09:44 +00:00
|
|
|
if(selectStart != selectEnd)
|
|
|
|
{
|
|
|
|
str32 select = str32_slice(codepoints, selectStart, selectEnd);
|
|
|
|
str32 afterSelect = str32_slice(codepoints, selectEnd, codepoints.len);
|
|
|
|
mp_rect selectBox = mg_text_bounding_box_utf32(style->font, style->fontSize, select);
|
|
|
|
mp_rect afterSelectBox = mg_text_bounding_box_utf32(style->font, style->fontSize, afterSelect);
|
|
|
|
|
|
|
|
selectBox.x += beforeSelectBox.x + beforeSelectBox.w;
|
|
|
|
selectBox.y += textY;
|
|
|
|
|
|
|
|
mg_set_color_rgba(0, 0, 1, 1);
|
|
|
|
mg_rectangle_fill(selectBox.x, selectBox.y, selectBox.w, lineHeight);
|
|
|
|
|
|
|
|
mg_set_font(style->font);
|
|
|
|
mg_set_font_size(style->fontSize);
|
2023-03-06 18:33:01 +00:00
|
|
|
mg_set_color(style->color);
|
2023-03-03 19:09:44 +00:00
|
|
|
|
|
|
|
mg_move_to(textX, textY);
|
|
|
|
mg_codepoints_outlines(beforeSelect);
|
|
|
|
mg_fill();
|
|
|
|
|
|
|
|
mg_set_color_rgba(1, 1, 1, 1);
|
|
|
|
mg_codepoints_outlines(select);
|
|
|
|
mg_fill();
|
|
|
|
|
2023-03-06 18:33:01 +00:00
|
|
|
mg_set_color(style->color);
|
2023-03-03 19:09:44 +00:00
|
|
|
mg_codepoints_outlines(afterSelect);
|
|
|
|
mg_fill();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!((u64)(2*(ui->frameTime - ui->editCursorBlinkStart)) & 1))
|
|
|
|
{
|
|
|
|
f32 caretX = box->rect.x + textMargin - beforeBox.w + beforeSelectBox.w;
|
|
|
|
f32 caretY = textTop;
|
2023-03-09 17:09:04 +00:00
|
|
|
mg_set_color(style->color);
|
|
|
|
mg_rectangle_fill(caretX, caretY, 1, lineHeight);
|
2023-03-03 19:09:44 +00:00
|
|
|
}
|
|
|
|
mg_set_font(style->font);
|
|
|
|
mg_set_font_size(style->fontSize);
|
2023-03-06 18:33:01 +00:00
|
|
|
mg_set_color(style->color);
|
2022-08-15 15:20:44 +00:00
|
|
|
|
2023-03-03 19:09:44 +00:00
|
|
|
mg_move_to(textX, textY);
|
|
|
|
mg_codepoints_outlines(codepoints);
|
|
|
|
mg_fill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mg_set_font(style->font);
|
|
|
|
mg_set_font_size(style->fontSize);
|
2023-03-06 18:33:01 +00:00
|
|
|
mg_set_color(style->color);
|
2022-08-15 15:20:44 +00:00
|
|
|
|
2023-03-03 19:09:44 +00:00
|
|
|
mg_move_to(textX, textY);
|
|
|
|
mg_codepoints_outlines(codepoints);
|
|
|
|
mg_fill();
|
|
|
|
}
|
2022-08-15 15:20:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 08:20:29 +00:00
|
|
|
ui_text_box_result ui_text_box(const char* name, mem_arena* arena, str8 text)
|
|
|
|
{
|
|
|
|
ui_context* ui = ui_get_context();
|
|
|
|
|
|
|
|
ui_text_box_result result = {.text = text};
|
|
|
|
|
|
|
|
ui_flags frameFlags = UI_FLAG_CLICKABLE
|
|
|
|
| UI_FLAG_DRAW_BACKGROUND
|
|
|
|
| UI_FLAG_DRAW_BORDER
|
|
|
|
| UI_FLAG_CLIP
|
2023-03-11 18:05:25 +00:00
|
|
|
| UI_FLAG_DRAW_PROC
|
2022-08-15 08:20:29 +00:00
|
|
|
| UI_FLAG_SCROLLABLE;
|
|
|
|
|
|
|
|
ui_box* frame = ui_box_make(name, frameFlags);
|
2023-03-07 09:33:22 +00:00
|
|
|
ui_style* style = &frame->style;
|
2023-03-05 16:03:00 +00:00
|
|
|
f32 textMargin = 5; //TODO parameterize this margin! must be the same as in ui_text_box_render
|
|
|
|
|
2022-08-15 15:20:44 +00:00
|
|
|
mg_font_extents extents = mg_font_get_scaled_extents(style->font, style->fontSize);
|
2022-08-15 08:20:29 +00:00
|
|
|
|
|
|
|
ui_sig sig = ui_box_sig(frame);
|
|
|
|
|
|
|
|
if(sig.hovering)
|
|
|
|
{
|
|
|
|
ui_box_set_hot(frame, true);
|
|
|
|
|
|
|
|
if(sig.pressed)
|
|
|
|
{
|
|
|
|
if(!ui_box_active(frame))
|
|
|
|
{
|
|
|
|
ui_box_activate(frame);
|
|
|
|
|
|
|
|
//NOTE: focus
|
|
|
|
ui->focus = frame;
|
|
|
|
ui->editFirstDisplayedChar = 0;
|
|
|
|
ui->editCursor = 0;
|
|
|
|
ui->editMark = 0;
|
|
|
|
}
|
2023-03-05 14:59:57 +00:00
|
|
|
ui->editCursorBlinkStart = ui->frameTime;
|
2022-08-15 08:20:29 +00:00
|
|
|
}
|
|
|
|
|
2023-03-05 14:59:57 +00:00
|
|
|
if(sig.pressed || sig.dragging)
|
|
|
|
{
|
|
|
|
//NOTE: set cursor/extend selection on mouse press or drag
|
|
|
|
vec2 pos = ui_mouse_position();
|
|
|
|
f32 cursorX = pos.x - frame->rect.x - textMargin;
|
|
|
|
|
|
|
|
str32 codepoints = utf8_push_to_codepoints(&ui->frameArena, text);
|
|
|
|
i32 newCursor = codepoints.len;
|
|
|
|
f32 x = 0;
|
|
|
|
for(int i = ui->editFirstDisplayedChar; i<codepoints.len; i++)
|
|
|
|
{
|
|
|
|
mp_rect bbox = mg_text_bounding_box_utf32(style->font, style->fontSize, str32_slice(codepoints, i, i+1));
|
|
|
|
if(x + 0.5*bbox.w > cursorX)
|
|
|
|
{
|
|
|
|
newCursor = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
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(abs(newCursor - ui->editCursor) > abs(newCursor - ui->editMark))
|
|
|
|
{
|
|
|
|
i32 tmp = ui->editCursor;
|
|
|
|
ui->editCursor = ui->editMark;
|
|
|
|
ui->editMark = tmp;
|
|
|
|
}
|
|
|
|
//NOTE: set the new cursor, and set or leave the mark depending on mode
|
|
|
|
ui->editCursor = newCursor;
|
|
|
|
if(sig.pressed && !(mp_key_mods() & MP_KEYMOD_SHIFT))
|
|
|
|
{
|
|
|
|
ui->editMark = ui->editCursor;
|
|
|
|
}
|
|
|
|
}
|
2022-08-15 08:20:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui_box_set_hot(frame, false);
|
|
|
|
|
|
|
|
if(sig.pressed)
|
|
|
|
{
|
|
|
|
if(ui_box_active(frame))
|
|
|
|
{
|
|
|
|
ui_box_deactivate(frame);
|
|
|
|
|
|
|
|
//NOTE loose focus
|
|
|
|
ui->focus = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ui_box_active(frame))
|
|
|
|
{
|
2023-03-03 19:09:44 +00:00
|
|
|
str32 oldCodepoints = utf8_push_to_codepoints(&ui->frameArena, text);
|
|
|
|
str32 codepoints = oldCodepoints;
|
2022-08-15 08:20:29 +00:00
|
|
|
ui->editCursor = Clamp(ui->editCursor, 0, codepoints.len);
|
|
|
|
ui->editMark = Clamp(ui->editMark, 0, codepoints.len);
|
|
|
|
|
|
|
|
//NOTE replace selection with input codepoints
|
|
|
|
str32 input = mp_input_text_utf32(&ui->frameArena);
|
|
|
|
if(input.len)
|
|
|
|
{
|
|
|
|
codepoints = ui_edit_replace_selection_with_codepoints(ui, codepoints, input);
|
2022-08-15 15:20:44 +00:00
|
|
|
ui->editCursorBlinkStart = ui->frameTime;
|
2022-08-15 08:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//NOTE handle shortcuts
|
2023-03-04 18:24:30 +00:00
|
|
|
mp_keymod_flags mods = mp_key_mods();
|
2022-08-15 08:20:29 +00:00
|
|
|
|
|
|
|
for(int i=0; i<UI_EDIT_COMMAND_COUNT; i++)
|
|
|
|
{
|
|
|
|
const ui_edit_command* command = &(UI_EDIT_COMMANDS[i]);
|
2023-03-03 19:09:44 +00:00
|
|
|
|
2023-03-05 15:35:55 +00:00
|
|
|
if( (mp_key_pressed(command->key) || mp_key_repeated(command->key))
|
|
|
|
&& mods == command->mods)
|
2022-08-15 08:20:29 +00:00
|
|
|
{
|
|
|
|
codepoints = ui_edit_perform_operation(ui, command->operation, command->move, command->direction, codepoints);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 16:03:00 +00:00
|
|
|
//NOTE(martin): check changed/accepted
|
|
|
|
if(oldCodepoints.ptr != codepoints.ptr)
|
|
|
|
{
|
|
|
|
result.changed = true;
|
|
|
|
result.text = utf8_push_from_codepoints(arena, codepoints);
|
|
|
|
}
|
|
|
|
|
2023-03-04 18:24:30 +00:00
|
|
|
if(mp_key_pressed(MP_KEY_ENTER))
|
2022-08-15 15:20:44 +00:00
|
|
|
{
|
|
|
|
//TODO(martin): extract in gui_edit_complete() (and use below)
|
2023-03-05 16:03:00 +00:00
|
|
|
result.accepted = true;
|
2022-08-15 15:20:44 +00:00
|
|
|
ui_box_deactivate(frame);
|
|
|
|
ui->focus = 0;
|
|
|
|
}
|
2022-08-15 08:20:29 +00:00
|
|
|
|
2023-03-05 15:35:55 +00:00
|
|
|
//NOTE slide contents
|
|
|
|
{
|
|
|
|
if(ui->editCursor < ui->editFirstDisplayedChar)
|
|
|
|
{
|
|
|
|
ui->editFirstDisplayedChar = ui->editCursor;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i32 firstDisplayedChar = ui->editFirstDisplayedChar;
|
|
|
|
str32 firstToCursor = str32_slice(codepoints, firstDisplayedChar, ui->editCursor);
|
|
|
|
mp_rect firstToCursorBox = mg_text_bounding_box_utf32(style->font, style->fontSize, firstToCursor);
|
|
|
|
|
|
|
|
while(firstToCursorBox.w > (frame->rect.w - 2*textMargin))
|
|
|
|
{
|
|
|
|
firstDisplayedChar++;
|
|
|
|
firstToCursor = str32_slice(codepoints, firstDisplayedChar, ui->editCursor);
|
|
|
|
firstToCursorBox = mg_text_bounding_box_utf32(style->font, style->fontSize, firstToCursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->editFirstDisplayedChar = firstDisplayedChar;
|
|
|
|
}
|
|
|
|
}
|
2022-08-15 08:20:29 +00:00
|
|
|
|
2022-08-15 15:20:44 +00:00
|
|
|
//NOTE: set renderer
|
|
|
|
str32* renderCodepoints = mem_arena_alloc_type(&ui->frameArena, str32);
|
|
|
|
*renderCodepoints = str32_push_copy(&ui->frameArena, codepoints);
|
2023-03-11 18:05:25 +00:00
|
|
|
ui_box_set_draw_proc(frame, ui_text_box_render, renderCodepoints);
|
2022-08-15 15:20:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//NOTE: set renderer
|
|
|
|
str32* renderCodepoints = mem_arena_alloc_type(&ui->frameArena, str32);
|
|
|
|
*renderCodepoints = utf8_push_to_codepoints(&ui->frameArena, text);
|
2023-03-11 18:05:25 +00:00
|
|
|
ui_box_set_draw_proc(frame, ui_text_box_render, renderCodepoints);
|
2022-08-15 08:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
2022-08-14 16:19:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
#undef LOG_SUBSYSTEM
|