UI windows

This commit is contained in:
2024-12-29 19:14:10 +01:00
parent e80843c9de
commit 4ac24fa03c
4 changed files with 68 additions and 9 deletions

View File

@@ -1,5 +1,6 @@
MAX_VERT_BUFFERS :: 64;
MAX_BOXES :: 4096;
MAX_WINDOWS :: 128;
UI_Box_Flags :: enum_flags u32 {
NONE :: 0;
@@ -49,6 +50,7 @@ UI_Box :: struct {
hash: u32;
last_used_frame_index: u64;
root_for_window: *UI_Window;
parent: *UI_Box;
first_child: *UI_Box;
last_child: *UI_Box;
@@ -129,12 +131,25 @@ Textured_Vert_Buffer :: struct {
verts: [6] Textured_Vert;
}
UI_Window :: struct {
hash: u32;
root: *UI_Box;
title: string;
position: Vector2;
size: Vector2;
last_used_frame_index: u64;
}
UI_State :: struct {
begun: bool;
frame_index: u64;
root: *UI_Box;
boxes: Table(u32, UI_Box);
windows: Table(u32, UI_Window);
current_window: *UI_Window;
last_box: *UI_Box;
@@ -171,6 +186,22 @@ UI_State :: struct {
ui_state : UI_State;
ui_window_make :: (title: string, hash: u32) -> *UI_Window {
window := get_ui_window_or_create_new(hash);
return window;
}
ui_window_begin :: (title: string, identifier: s64 = 0, loc := #caller_location) {
assert(ui_state.current_window == null);
ui_state.current_window = ui_window_make(title, hash=get_hash(loc, identifier));
ui_state.current_window.last_used_frame_index = ui_state.frame_index;
}
ui_window_end :: () {
ui_state.current_window = null;
}
ui_box :: (flags: UI_Box_Flags, identifier: s64 = 0, loc := #caller_location) -> *UI_Box {
return ui_box_make(flags, hash=get_hash(loc, identifier));
}
@@ -194,6 +225,15 @@ ui_box_make :: (flags: UI_Box_Flags, hash: u32) -> *UI_Box {
box.num_children = 0;
box.flags = flags;
box.parent = null;
box.root_for_window = null;
if ui_state.current_window {
if ui_state.current_window.root == null {
ui_state.current_window.root = box;
box.root_for_window = ui_state.current_window;
}
}
// Set the links
box.parent = parent;
@@ -728,9 +768,24 @@ get_ui_box_or_create_new :: (hash: u32) -> *UI_Box, bool {
ptr.hash = hash;
return ptr, false;
return ptr, false;
}
get_ui_window_or_create_new :: (hash: u32) -> *UI_Window, bool {
ptr := table_find_pointer(*ui_state.windows, hash);
if ptr == null {
assert(ui_state.windows.count < MAX_WINDOWS);
table_add(*ui_state.windows, hash, .{});
ptr = table_find_pointer(*ui_state.windows, hash);
}
ptr.hash = hash;
return ptr, false;
}
// #### RENDERING
make_vert :: (x: float, y: float, color: Color) -> Colored_Vert {
vert : Colored_Vert;