From b14c4720a0b9655242bb5921e1de3c059bc0967f Mon Sep 17 00:00:00 2001 From: Synthasmagoria Date: Tue, 21 Jul 2026 20:32:41 +0200 Subject: [PATCH] imgui - part 2 --- program.odin | 9 +++++- ui.odin | 87 ++++++++++++++++++++++++++++------------------------ 2 files changed, 55 insertions(+), 41 deletions(-) diff --git a/program.odin b/program.odin index 7aff58b..81e33ea 100644 --- a/program.odin +++ b/program.odin @@ -2,6 +2,7 @@ package main import rl "libraries/raylib" import "base:runtime" +import "core:fmt" WINDOW_WIDTH :: 1280 WINDOW_HEIGHT :: 720 @@ -15,6 +16,8 @@ init :: proc() { rl.SetTargetFPS(WINDOW_FRAMERATE) } +value: i32 + update :: proc() { rl.BeginDrawing() defer rl.EndDrawing() @@ -31,7 +34,9 @@ update :: proc() { ui_button(&ui_state, ui_button_size, {label = "Hi"}) ui_same_line(&ui_state) ui_button(&ui_state, ui_button_size, {label = "Hi"}) + ui_input(&ui_state, ui_button_size, {label = "", value = &value, min = 0, max = 10}) ui_container_end(&ui_state) + ui_button(&ui_state, ui_button_size, {label = "notheunoehunh"}) ui_elements := ui_end(ui_state) for element in ui_elements { @@ -40,6 +45,9 @@ update :: proc() { data := element.data.(UiElementData_Button) rl.GuiButton(element.area, data.label) case .Input: + data := element.data.(UiElementData_Input) + value := rl.GuiValueBox(element.area, "", data.value, data.min, data.max, true) + fmt.println(value) case .Container: data := element.data.(UiElementData_Container) if len(data.label) == 0 { @@ -47,7 +55,6 @@ update :: proc() { } else { rl.GuiGroupBox(element.area, data.label) } - } } diff --git a/ui.odin b/ui.odin index 06a75b7..8fd5c5f 100644 --- a/ui.odin +++ b/ui.odin @@ -16,18 +16,6 @@ UiElementData :: union { UiElementData_Container, } -UiElementData_Button :: struct { - label: cstring, -} - -UiElementData_Container :: struct { - label: cstring, -} - -UiElementData_Input :: struct { - label: cstring, -} - UiElement :: struct { area: rect2, type: UiElementType, @@ -47,20 +35,67 @@ UiStyle :: struct { margin, padding: f32, } +ui_start :: proc(state: ^UiState) { + clear(&state.elements) + state.position_left = state.origin + state.position = state.position_left +} + +ui_end :: proc(state: UiState) -> []UiElement { + return state.elements[:] +} + +UiElementData_Button :: struct { + label: cstring, +} + ui_button :: proc(state: ^UiState, size: v2, data: UiElementData_Button) { area := _ui_advance_element(state, size) append(&state.elements, UiElement{area = area, type = .Button, data = data}) } +UiElementData_Container :: struct { + label: cstring, +} + ui_container_begin :: proc(state: ^UiState, data: UiElementData_Container) { assert(state.container == nil) append(&state.elements, UiElement{area = _ui_advance_container(state), type = .Container, data = data}) + state.position_left += state.style.margin + state.style.padding state.container = &state.elements[len(state.elements) - 1] } ui_container_end :: proc(state: ^UiState) { assert(state.container != nil) + state.position_left.x = state.origin.x + state.position_left.y = state.container.area.y + state.container.area.height + state.position = state.position_left state.container = nil + state.group_element_count = 0 +} + +UiElementData_Input :: struct { + label: cstring, + value: ^i32, + min, max: i32, +} + +ui_input :: proc(state: ^UiState, size: v2, data: UiElementData_Input) { + area := _ui_advance_element(state, size) + append(&state.elements, UiElement{area = area, type = .Input, data = data}) +} + +ui_same_line :: proc(state: ^UiState) { + state.same_line = true +} + +ui_init :: proc(state: ^UiState, style: UiStyle, origin: v2, allocator: mem.Allocator) { + state^ = { + origin = origin, + position = origin, + style = style, + elements = make(type_of(state.elements), 0, 512, allocator) + } } _ui_advance_element :: proc(state: ^UiState, size: v2) -> rect2 { @@ -91,31 +126,3 @@ _ui_advance_container :: proc(state: ^UiState) -> rect2 { state.position += state.style.margin return {**state.position, 0.0, 0.0} } - -ui_input :: proc(state: ^UiState, size: v2, data: UiElementData_Input) { - area := _ui_advance_element(state, size) - append(&state.elements, UiElement{area = area, type = .Input, data = data}) -} - -ui_same_line :: proc(state: ^UiState) { - state.same_line = true -} - -ui_start :: proc(state: ^UiState) { - clear(&state.elements) - state.position_left = state.origin - state.position = state.position_left -} - -ui_end :: proc(state: UiState) -> []UiElement { - return state.elements[:] -} - -ui_init :: proc(state: ^UiState, style: UiStyle, origin: v2, allocator: mem.Allocator) { - state^ = { - origin = origin, - position = origin, - style = style, - elements = make(type_of(state.elements), 0, 512, allocator) - } -}