Dropdown box

This commit is contained in:
Synthasmagoria 2026-07-24 00:01:19 +02:00
commit 4e9fe28e4d
2 changed files with 102 additions and 23 deletions

28
ui.odin
View file

@ -5,7 +5,7 @@ import "core:fmt"
import "core:mem"
import rl "libraries/raylib"
@(private="file") state_current: ^UiState
@(private="file") state_current: ^UiContext
UiElementType :: enum {
Input,
@ -20,7 +20,7 @@ UiElementData :: union {
UiElementData_Input,
UiElementData_Container,
UiElementData_Text,
UiElementData_DropDown,
UiElementData_Dropdown,
}
UiElement :: struct {
@ -35,7 +35,7 @@ UiElementId :: bit_field u32 {
element_index: u16 | 16,
}
UiState :: struct {
UiContext :: struct {
origin, position_left, position: v2,
same_line: bool,
style: UiStyle,
@ -51,7 +51,7 @@ UiStyle :: struct {
font_style: FontStyle,
}
ui_init :: proc(state: ^UiState, style: UiStyle, origin: v2, allocator: mem.Allocator) {
ui_init :: proc(state: ^UiContext, style: UiStyle, origin: v2, allocator: mem.Allocator) {
state^ = {
element_focus_id = ui_id_invalid(),
origin = origin,
@ -61,7 +61,7 @@ ui_init :: proc(state: ^UiState, style: UiStyle, origin: v2, allocator: mem.Allo
}
}
ui_start :: proc(state: ^UiState) {
ui_start :: proc(state: ^UiContext) {
state_current = state
clear(&state.elements)
state.id_current = {group_index = 0, element_index = 0}
@ -205,12 +205,16 @@ ui_mat4 :: proc(input_field_size: v2, inputs: [][]byte, min, max: f32) {
ui_input(input_field_size, {label = "", input = inputs[15], min = min, max = max})
}
UiElementData_DropDown :: struct {
UiElementCallback_Dropdown :: proc(previous, current: i32, data: rawptr)
UiElementData_Dropdown :: struct {
label: cstring,
active: ^i32,
callback: UiElementCallback_Dropdown,
callback_data: rawptr,
}
ui_dropdown :: proc(size: v2, data: UiElementData_DropDown) {
ui_dropdown :: proc(size: v2, data: UiElementData_Dropdown) {
state := state_current
area := _ui_advance_element(state, size)
state.id_current.element_index += 1
@ -221,15 +225,15 @@ ui_same_line :: proc() {
state_current.same_line = true
}
ui_element_is_editing :: proc(state: UiState, id: UiElementId) -> bool {
ui_element_is_editing :: proc(state: UiContext, id: UiElementId) -> bool {
return state.element_focus_id == id
}
ui_element_focus :: proc(state: ^UiState, id: UiElementId) {
ui_element_focus :: proc(state: ^UiContext, id: UiElementId) {
state.element_focus_id = id
}
ui_element_focus_reset :: proc(state: ^UiState) {
ui_element_focus_reset :: proc(state: ^UiContext) {
state.element_focus_id = ui_id_invalid()
}
@ -237,7 +241,7 @@ ui_id_invalid :: proc() -> UiElementId {
return {group_index = max(u16), element_index = max(u16)}
}
_ui_advance_element :: proc(state: ^UiState, size: v2) -> rect2 {
_ui_advance_element :: proc(state: ^UiContext, size: v2) -> rect2 {
if state.group_element_count > 0 {
if state.same_line {
previous_element := state.elements[len(state.elements) - 1]
@ -262,7 +266,7 @@ _ui_advance_element :: proc(state: ^UiState, size: v2) -> rect2 {
return area
}
_ui_advance_container :: proc(state: ^UiState) -> rect2 {
_ui_advance_container :: proc(state: ^UiContext) -> rect2 {
state.position += state.style.margin
return {**state.position, 0.0, 0.0}
}