Dropdown box
This commit is contained in:
parent
af44a9c367
commit
4e9fe28e4d
2 changed files with 102 additions and 23 deletions
97
program.odin
97
program.odin
|
|
@ -20,12 +20,28 @@ FontStyle :: struct {
|
|||
size, spacing: f32
|
||||
}
|
||||
|
||||
ProgramState :: enum {
|
||||
Normal,
|
||||
Message,
|
||||
}
|
||||
|
||||
ProgramMessageType :: enum {
|
||||
ChangeMatrixTypeClearData,
|
||||
}
|
||||
|
||||
@rodata program_messages := [ProgramMessageType]cstring {
|
||||
.ChangeMatrixTypeClearData = "Changing the matrix type will delete all current matrices.\nAre you sure you want to do this?",
|
||||
}
|
||||
|
||||
Program :: struct {
|
||||
ui_state: UiState,
|
||||
ui_state: UiContext,
|
||||
matrix_field_pool: FieldPool(f32),
|
||||
matrix_mode: MatrixType,
|
||||
matrix_mode_perceived: MatrixType,
|
||||
matrix_mode_actual: MatrixType,
|
||||
grid: Grid,
|
||||
font_style: FontStyle,
|
||||
state: ProgramState,
|
||||
message_type: ProgramMessageType,
|
||||
}
|
||||
|
||||
Grid :: struct {
|
||||
|
|
@ -45,6 +61,11 @@ field_pool_get :: proc(pool: ^FieldPool($T), count: int) -> (values: []T, inputs
|
|||
return fixed_pool.get(&pool.values, count), fixed_pool.get(&pool.inputs, count)
|
||||
}
|
||||
|
||||
field_pool_clear :: proc(pool: ^FieldPool($T)) {
|
||||
fixed_pool.clear(&pool.values)
|
||||
fixed_pool.clear(&pool.inputs)
|
||||
}
|
||||
|
||||
field_pool_init :: proc(pool: ^FieldPool($T), capacity: int, allocator := context.allocator) {
|
||||
fixed_pool.init(&pool.values, capacity, allocator)
|
||||
fixed_pool.init(&pool.inputs, capacity, allocator)
|
||||
|
|
@ -193,7 +214,17 @@ init :: proc() {
|
|||
ui_init(&program.ui_state, ui_style, {0, 0}, context.temp_allocator)
|
||||
}
|
||||
|
||||
update :: proc() {
|
||||
program_set_message_state :: proc(program: ^Program, message: ProgramMessageType) {
|
||||
program.state = .Message
|
||||
program.message_type = message
|
||||
}
|
||||
|
||||
program_matrix_mode_dropdown_callback :: proc(previous, current: i32, data: rawptr) {
|
||||
program := cast(^Program)data
|
||||
program_set_message_state(program, .ChangeMatrixTypeClearData)
|
||||
}
|
||||
|
||||
program_build_ui :: proc() -> []UiElement {
|
||||
matrix_add_button_callback_data := UiButton_AddMat2CallbackData {
|
||||
program = &program,
|
||||
mat = mat2{0, 1, 2, 3},
|
||||
|
|
@ -210,8 +241,12 @@ update :: proc() {
|
|||
ui_button(ui_button_size_top_bar, {label = "#12#"})
|
||||
ui_same_line()
|
||||
matrix_options := enum_to_raygui_options_string(MatrixType, context.temp_allocator)
|
||||
matrix_mode_ref := cast(^i32)&program.matrix_mode
|
||||
ui_dropdown(ui_dropdown_size_top_bar, {label = matrix_options, active = matrix_mode_ref})
|
||||
matrix_mode_ref := cast(^i32)&program.matrix_mode_perceived
|
||||
ui_dropdown(ui_dropdown_size_top_bar, {
|
||||
label = matrix_options,
|
||||
active = matrix_mode_ref,
|
||||
callback = program_matrix_mode_dropdown_callback,
|
||||
callback_data = cast(rawptr)&program})
|
||||
ui_container_end()
|
||||
ui_container_begin({label = "Matrices"})
|
||||
field_pool := &program.matrix_field_pool
|
||||
|
|
@ -227,12 +262,10 @@ update :: proc() {
|
|||
}
|
||||
}
|
||||
ui_container_end()
|
||||
ui_elements := ui_end()
|
||||
|
||||
rl.BeginDrawing()
|
||||
defer rl.EndDrawing()
|
||||
rl.ClearBackground(rl.BLACK)
|
||||
return ui_end()
|
||||
}
|
||||
|
||||
program_render_ui :: proc(ui_elements: []UiElement) {
|
||||
for element in ui_elements {
|
||||
switch element.type {
|
||||
case .Button:
|
||||
|
|
@ -259,17 +292,59 @@ update :: proc() {
|
|||
style := program.ui_state.style
|
||||
draw_text(data.text, rect_get_tl(element.area), style.font_style, data.color)
|
||||
case .DropDown:
|
||||
data := element.data.(UiElementData_DropDown)
|
||||
data := element.data.(UiElementData_Dropdown)
|
||||
edit_mode := ui_element_is_editing(program.ui_state, element.id)
|
||||
active_previous := data.active^
|
||||
if rl.GuiDropdownBox(element.area, data.label, data.active, edit_mode) {
|
||||
if edit_mode {
|
||||
ui_element_focus_reset(&program.ui_state)
|
||||
if active_previous != data.active^ && data.callback != nil {
|
||||
data.callback(active_previous, data.active^, data.callback_data)
|
||||
}
|
||||
} else {
|
||||
ui_element_focus(&program.ui_state, element.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
update :: proc() {
|
||||
ui_elements := program_build_ui()
|
||||
|
||||
rl.BeginDrawing()
|
||||
defer rl.EndDrawing()
|
||||
rl.ClearBackground(rl.BLACK)
|
||||
|
||||
if program.state != .Normal {
|
||||
rl.GuiDisable()
|
||||
}
|
||||
program_render_ui(ui_elements)
|
||||
if program.state != .Normal {
|
||||
rl.GuiEnable()
|
||||
}
|
||||
|
||||
switch program.state {
|
||||
case .Normal:
|
||||
case .Message:
|
||||
switch program.message_type {
|
||||
case .ChangeMatrixTypeClearData:
|
||||
size := v2{224.0, 128.0}
|
||||
position := v2(iv2{rl.GetRenderWidth(), rl.GetRenderHeight()} / 2.0) - size / 2.0
|
||||
area := rect2{**position, **size}
|
||||
action := rl.GuiMessageBox(area, "Message", program_messages[program.message_type], "Confirm;Cancel")
|
||||
switch action {
|
||||
case 0:
|
||||
case 1:
|
||||
field_pool_clear(&program.matrix_field_pool)
|
||||
program.matrix_mode_actual = program.matrix_mode_perceived
|
||||
program.state = .Normal
|
||||
case 2:
|
||||
program.matrix_mode_perceived = program.matrix_mode_actual
|
||||
program.state = .Normal
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free_all(context.temp_allocator)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue