2024-02-12 17:52:56 -08:00
|
|
|
package sectr
|
|
|
|
|
2024-03-02 07:24:09 -08:00
|
|
|
import "base:runtime"
|
|
|
|
|
2024-02-12 17:52:56 -08:00
|
|
|
// TODO(Ed) : This is in Raddbg base_types.h, consider moving outside of UI.
|
|
|
|
|
2024-02-22 18:19:29 -08:00
|
|
|
Corner :: enum i32 {
|
|
|
|
Invalid = -1,
|
|
|
|
_00,
|
|
|
|
_01,
|
|
|
|
_10,
|
|
|
|
_11,
|
|
|
|
TopLeft = _00,
|
|
|
|
TopRight = _01,
|
|
|
|
BottomLeft = _10,
|
|
|
|
BottomRight = _11,
|
|
|
|
Count = 4,
|
2024-02-13 23:27:40 -08:00
|
|
|
}
|
|
|
|
|
2024-02-22 18:19:29 -08:00
|
|
|
Side :: enum i32 {
|
|
|
|
Invalid = -1,
|
|
|
|
Min = 0,
|
|
|
|
Max = 1,
|
|
|
|
Count
|
|
|
|
}
|
|
|
|
|
|
|
|
// Side2 :: enum u32 {
|
|
|
|
// Top,
|
|
|
|
// Bottom,
|
|
|
|
// Left,
|
|
|
|
// Right,
|
|
|
|
// Count,
|
|
|
|
// }
|
|
|
|
|
|
|
|
UI_AnchorPresets :: enum u32 {
|
|
|
|
Top_Left,
|
|
|
|
Top_Right,
|
|
|
|
Bottom_Right,
|
|
|
|
Bottom_Left,
|
|
|
|
Center_Left,
|
|
|
|
Center_Top,
|
|
|
|
Center_Right,
|
|
|
|
Center_Bottom,
|
|
|
|
Center,
|
|
|
|
Left_Wide,
|
|
|
|
Top_Wide,
|
|
|
|
Right_Wide,
|
|
|
|
Bottom_Wide,
|
|
|
|
VCenter_Wide,
|
|
|
|
HCenter_Wide,
|
|
|
|
Full,
|
2024-02-13 23:27:40 -08:00
|
|
|
Count,
|
|
|
|
}
|
|
|
|
|
2024-02-22 18:19:29 -08:00
|
|
|
UI_Cursor :: struct {
|
|
|
|
placeholder : int,
|
2024-02-12 17:52:56 -08:00
|
|
|
}
|
|
|
|
|
2024-03-02 07:24:09 -08:00
|
|
|
UI_FramePassKind :: enum {
|
|
|
|
Generate,
|
|
|
|
Compute,
|
|
|
|
Logical,
|
|
|
|
}
|
|
|
|
|
|
|
|
UI_InteractState :: struct {
|
|
|
|
hot_time : f32,
|
|
|
|
active_time : f32,
|
|
|
|
disabled_time : f32,
|
|
|
|
}
|
|
|
|
|
2024-02-22 18:19:29 -08:00
|
|
|
UI_Key :: distinct u64
|
2024-02-12 17:52:56 -08:00
|
|
|
|
2024-02-22 18:19:29 -08:00
|
|
|
UI_Scalar :: f32
|
2024-02-12 17:52:56 -08:00
|
|
|
|
2024-02-22 18:19:29 -08:00
|
|
|
UI_ScalarConstraint :: struct {
|
|
|
|
min, max : UI_Scalar,
|
|
|
|
}
|
2024-02-12 17:52:56 -08:00
|
|
|
|
2024-02-22 18:19:29 -08:00
|
|
|
UI_Scalar2 :: [Axis2.Count]UI_Scalar
|
2024-02-12 17:52:56 -08:00
|
|
|
|
|
|
|
|
2024-03-02 07:24:09 -08:00
|
|
|
// UI_BoxFlags_Stack_Size :: 512
|
2024-02-27 04:50:57 -08:00
|
|
|
UI_Layout_Stack_Size :: 512
|
|
|
|
UI_Style_Stack_Size :: 512
|
2024-03-12 17:55:29 -07:00
|
|
|
UI_Parent_Stack_Size :: 512
|
2024-04-07 22:35:53 -07:00
|
|
|
// UI_Built_Boxes_Array_Size :: 8
|
2024-05-13 18:53:08 -07:00
|
|
|
UI_Built_Boxes_Array_Size :: 128 * Kilobyte
|
2024-02-27 04:50:57 -08:00
|
|
|
|
2024-02-22 18:19:29 -08:00
|
|
|
UI_State :: struct {
|
2024-02-27 04:50:57 -08:00
|
|
|
// TODO(Ed) : Use these
|
2024-05-13 07:40:29 -07:00
|
|
|
// build_arenas : [2]Arena,
|
|
|
|
// build_arena : ^ Arena,
|
2024-02-27 04:50:57 -08:00
|
|
|
|
|
|
|
built_box_count : i32,
|
|
|
|
|
|
|
|
caches : [2] HMapZPL( UI_Box ),
|
2024-03-08 00:34:21 -08:00
|
|
|
prev_cache : ^HMapZPL( UI_Box ),
|
|
|
|
curr_cache : ^HMapZPL( UI_Box ),
|
2024-02-12 17:52:56 -08:00
|
|
|
|
2024-05-13 18:53:08 -07:00
|
|
|
render_queue : Array(UI_RenderBoxInfo),
|
|
|
|
|
2024-05-12 22:56:39 -07:00
|
|
|
null_box : ^UI_Box, // This was used with the Linked list interface...
|
|
|
|
// TODO(Ed): Should we change our convention for null boxes to use the above and nil as an invalid state?
|
2024-03-08 00:34:21 -08:00
|
|
|
root : ^UI_Box,
|
2024-05-12 22:52:55 -07:00
|
|
|
// Children of the root node are unique in that they have their order preserved per frame
|
|
|
|
// This is to support overlapping frames
|
|
|
|
// So long as their parent-index is non-negative they'll be rendered
|
2024-02-22 20:15:29 -08:00
|
|
|
|
2024-02-27 04:50:57 -08:00
|
|
|
// Do we need to recompute the layout?
|
2024-05-13 07:40:29 -07:00
|
|
|
// layout_dirty : b32,
|
2024-02-22 18:19:29 -08:00
|
|
|
|
2024-02-27 04:50:57 -08:00
|
|
|
// TODO(Ed) : Look into using a build arena like Ryan does for these possibly (and thus have a linked-list stack)
|
2024-05-11 19:38:05 -07:00
|
|
|
layout_combo_stack : StackFixed( UI_LayoutCombo, UI_Style_Stack_Size ),
|
|
|
|
style_combo_stack : StackFixed( UI_StyleCombo, UI_Style_Stack_Size ),
|
|
|
|
parent_stack : StackFixed( ^UI_Box, UI_Parent_Stack_Size ),
|
2024-03-02 07:24:09 -08:00
|
|
|
// flag_stack : Stack( UI_BoxFlags, UI_BoxFlags_Stack_Size ),
|
2024-02-27 04:50:57 -08:00
|
|
|
|
2024-03-08 00:34:21 -08:00
|
|
|
hot : UI_Key,
|
2024-03-09 10:55:47 -08:00
|
|
|
hot_start_style : UI_Style,
|
|
|
|
|
|
|
|
active_mouse : [MouseBtn.count] UI_Key,
|
|
|
|
active : UI_Key,
|
|
|
|
active_start_signal : UI_Signal,
|
2024-03-08 00:34:21 -08:00
|
|
|
|
2024-02-27 04:50:57 -08:00
|
|
|
clipboard_copy : UI_Key,
|
|
|
|
last_clicked : UI_Key,
|
2024-02-22 18:19:29 -08:00
|
|
|
|
2024-03-08 15:45:27 -08:00
|
|
|
active_start_style : UI_Style,
|
2024-02-27 04:50:57 -08:00
|
|
|
|
2024-03-02 07:24:09 -08:00
|
|
|
last_pressed_key : [MouseBtn.count] UI_Key,
|
|
|
|
last_pressed_key_us : [MouseBtn.count] f32,
|
2024-02-22 20:15:29 -08:00
|
|
|
}
|
|
|
|
|
2024-05-11 19:38:05 -07:00
|
|
|
ui_startup :: proc( ui : ^ UI_State, cache_allocator : Allocator /* , cache_reserve_size : u64 */ )
|
2024-02-27 04:50:57 -08:00
|
|
|
{
|
2024-02-22 20:15:29 -08:00
|
|
|
ui := ui
|
|
|
|
ui^ = {}
|
|
|
|
|
2024-04-07 22:35:53 -07:00
|
|
|
// cache.ref in ui.caches.ref
|
|
|
|
for & cache in (& ui.caches) {
|
2024-02-27 04:50:57 -08:00
|
|
|
box_cache, allocation_error := zpl_hmap_init_reserve( UI_Box, cache_allocator, UI_Built_Boxes_Array_Size )
|
|
|
|
verify( allocation_error == AllocatorError.None, "Failed to allocate box cache" )
|
|
|
|
cache = box_cache
|
|
|
|
}
|
2024-04-07 22:35:53 -07:00
|
|
|
ui.curr_cache = (& ui.caches[1])
|
|
|
|
ui.prev_cache = (& ui.caches[0])
|
2024-05-12 22:52:55 -07:00
|
|
|
|
2024-05-13 18:53:08 -07:00
|
|
|
allocation_error : AllocatorError
|
|
|
|
ui.render_queue, allocation_error = array_init_reserve( UI_RenderBoxInfo, cache_allocator, UI_Built_Boxes_Array_Size, fixed_cap = true )
|
|
|
|
verify( allocation_error == AllocatorError.None, "Failed to allocate render queue" )
|
|
|
|
|
2024-03-12 17:55:29 -07:00
|
|
|
log("ui_startup completed")
|
2024-02-22 20:15:29 -08:00
|
|
|
}
|
|
|
|
|
2024-02-27 04:50:57 -08:00
|
|
|
ui_reload :: proc( ui : ^ UI_State, cache_allocator : Allocator )
|
|
|
|
{
|
2024-02-22 20:15:29 -08:00
|
|
|
// We need to repopulate Allocator references
|
2024-05-14 08:47:44 -07:00
|
|
|
for & cache in & ui.caches {
|
|
|
|
zpl_hmap_reload( & cache, cache_allocator)
|
2024-02-27 04:50:57 -08:00
|
|
|
}
|
2024-05-14 08:47:44 -07:00
|
|
|
ui.render_queue.backing = cache_allocator
|
2024-02-22 20:15:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(Ed) : Is this even needed?
|
2024-02-27 04:50:57 -08:00
|
|
|
ui_shutdown :: proc() {
|
|
|
|
}
|
|
|
|
|
2024-03-08 00:34:21 -08:00
|
|
|
ui_cursor_pos :: #force_inline proc "contextless" () -> Vec2 {
|
|
|
|
using state := get_state()
|
|
|
|
if ui_context == & state.project.workspace.ui {
|
2024-05-10 16:50:37 -07:00
|
|
|
return screen_to_ws_view_pos( input.mouse.pos )
|
2024-03-08 00:34:21 -08:00
|
|
|
}
|
|
|
|
else {
|
2024-05-12 22:52:55 -07:00
|
|
|
return input.mouse.pos
|
2024-03-08 00:34:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-15 15:48:20 -07:00
|
|
|
ui_drag_delta :: #force_inline proc "contextless" () -> Vec2 {
|
|
|
|
using state := get_state()
|
|
|
|
return ui_cursor_pos() - state.ui_context.active_start_signal.cursor_pos
|
|
|
|
}
|
|
|
|
|
2024-05-09 01:02:33 -07:00
|
|
|
ui_ws_drag_delta :: #force_inline proc "contextless" () -> Vec2 {
|
2024-03-08 00:34:21 -08:00
|
|
|
using state := get_state()
|
2024-05-10 16:50:37 -07:00
|
|
|
return screen_to_ws_view_pos(input.mouse.pos) - state.ui_context.active_start_signal.cursor_pos
|
2024-03-08 00:34:21 -08:00
|
|
|
}
|
|
|
|
|
2024-02-27 04:50:57 -08:00
|
|
|
ui_graph_build_begin :: proc( ui : ^ UI_State, bounds : Vec2 = {} )
|
|
|
|
{
|
2024-03-10 23:05:18 -07:00
|
|
|
profile(#procedure)
|
|
|
|
|
2024-05-12 22:52:55 -07:00
|
|
|
state := get_state()
|
2024-02-27 04:50:57 -08:00
|
|
|
get_state().ui_context = ui
|
|
|
|
using get_state().ui_context
|
2024-05-13 18:53:08 -07:00
|
|
|
|
2024-05-15 15:48:20 -07:00
|
|
|
stack_clear( & layout_combo_stack )
|
|
|
|
stack_clear( & style_combo_stack )
|
2024-05-13 18:53:08 -07:00
|
|
|
array_clear( render_queue )
|
2024-05-12 22:52:55 -07:00
|
|
|
|
2024-03-19 21:44:43 -07:00
|
|
|
temp := prev_cache
|
|
|
|
prev_cache = curr_cache
|
|
|
|
curr_cache = temp
|
|
|
|
// curr_cache, prev_cache = swap( curr_cache, prev_cache )
|
2024-03-02 07:24:09 -08:00
|
|
|
|
|
|
|
if ui.active == UI_Key(0) {
|
2024-03-09 10:55:47 -08:00
|
|
|
//ui.hot = UI_Key(0)
|
|
|
|
ui.active_start_signal = {}
|
|
|
|
}
|
2024-02-27 04:50:57 -08:00
|
|
|
|
2024-03-10 17:09:04 -07:00
|
|
|
ui.built_box_count = 0
|
2024-05-15 23:14:54 -07:00
|
|
|
root = ui_box_make( {}, str_intern(str_fmt_tmp("%s: root#001", ui == & state.screen_ui ? "Screen" : "Workspace" )).str)
|
2024-05-12 22:52:55 -07:00
|
|
|
if ui == & state.screen_ui {
|
|
|
|
root.layout.size = range2(Vec2(state.app_window.extent) * 2, {})
|
|
|
|
}
|
2024-02-27 04:50:57 -08:00
|
|
|
ui_parent_push(root)
|
2024-02-22 20:15:29 -08:00
|
|
|
}
|
|
|
|
|
2024-05-09 01:02:33 -07:00
|
|
|
ui_graph_build_end :: proc( ui : ^UI_State )
|
2024-02-22 20:15:29 -08:00
|
|
|
{
|
2024-03-10 23:05:18 -07:00
|
|
|
profile(#procedure)
|
|
|
|
|
2024-03-02 07:24:09 -08:00
|
|
|
ui_parent_pop() // Should be ui_context.root
|
2024-02-27 04:50:57 -08:00
|
|
|
|
|
|
|
// Regenerate the computed layout if dirty
|
2024-05-09 01:02:33 -07:00
|
|
|
ui_compute_layout( ui )
|
2024-02-27 04:50:57 -08:00
|
|
|
|
|
|
|
get_state().ui_context = nil
|
|
|
|
}
|
|
|
|
|
2024-05-09 01:02:33 -07:00
|
|
|
@(deferred_in = ui_graph_build_end)
|
2024-02-27 04:50:57 -08:00
|
|
|
ui_graph_build :: proc( ui : ^ UI_State ) {
|
|
|
|
ui_graph_build_begin( ui )
|
|
|
|
}
|
|
|
|
|
2024-03-14 21:02:28 -07:00
|
|
|
ui_key_from_string :: #force_inline proc "contextless" ( value : string ) -> UI_Key
|
2024-03-10 23:05:18 -07:00
|
|
|
{
|
|
|
|
// profile(#procedure)
|
|
|
|
USE_RAD_DEBUGGERS_METHOD :: true
|
|
|
|
|
|
|
|
key : UI_Key
|
|
|
|
|
|
|
|
when USE_RAD_DEBUGGERS_METHOD {
|
|
|
|
hash : u64
|
|
|
|
for str_byte in transmute([]byte) value {
|
|
|
|
hash = ((hash << 5) + hash) + u64(str_byte)
|
|
|
|
}
|
|
|
|
key = cast(UI_Key) hash
|
|
|
|
}
|
|
|
|
|
|
|
|
when ! USE_RAD_DEBUGGERS_METHOD {
|
|
|
|
key = cast(UI_Key) crc32( transmute([]byte) value )
|
|
|
|
}
|
|
|
|
|
2024-03-02 07:24:09 -08:00
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2024-02-27 04:50:57 -08:00
|
|
|
ui_parent_push :: proc( ui : ^ UI_Box ) {
|
|
|
|
stack := & get_state().ui_context.parent_stack
|
|
|
|
stack_push( & get_state().ui_context.parent_stack, ui )
|
|
|
|
}
|
2024-02-22 20:15:29 -08:00
|
|
|
|
2024-02-27 04:50:57 -08:00
|
|
|
ui_parent_pop :: proc() {
|
|
|
|
// If size_to_content is set, we need to compute the layout now.
|
2024-02-22 20:15:29 -08:00
|
|
|
|
2024-02-27 04:50:57 -08:00
|
|
|
// Check to make sure that the parent's children are the same for this frame,
|
|
|
|
// if its not we need to mark the layout as dirty.
|
2024-02-22 20:15:29 -08:00
|
|
|
|
2024-02-27 04:50:57 -08:00
|
|
|
stack_pop( & get_state().ui_context.parent_stack )
|
|
|
|
}
|
|
|
|
|
|
|
|
@(deferred_none = ui_parent_pop)
|
2024-05-09 23:08:36 -07:00
|
|
|
ui_parent :: #force_inline proc( ui : ^UI_Box) { ui_parent_push( ui ) }
|
2024-05-10 01:16:04 -07:00
|
|
|
|
2024-05-15 23:14:54 -07:00
|
|
|
ui_prev_cached_box :: #force_inline proc( box : ^UI_Box ) -> ^UI_Box {
|
|
|
|
return zpl_hmap_get( ui_context().prev_cache, cast(u64) box.key )
|
|
|
|
}
|
|
|
|
|
2024-05-10 01:16:04 -07:00
|
|
|
// Topmost ancestor that is not the root
|
|
|
|
ui_top_ancestor :: #force_inline proc "contextless" ( box : ^UI_Box ) -> (^UI_Box) {
|
|
|
|
using ui := get_state().ui_context
|
|
|
|
ancestor := box
|
|
|
|
for ; ancestor.parent != root; ancestor = ancestor.parent {}
|
|
|
|
return ancestor
|
|
|
|
}
|
2024-05-11 19:38:05 -07:00
|
|
|
|
|
|
|
ui_context :: #force_inline proc() -> ^UI_State { return get_state().ui_context }
|