imgui part 1
This commit is contained in:
parent
85840db0f1
commit
22feae4b45
3 changed files with 257 additions and 83 deletions
108
program.odin
108
program.odin
|
|
@ -1,9 +1,6 @@
|
|||
package main
|
||||
|
||||
import rl "libraries/raylib"
|
||||
import "core:fmt"
|
||||
import "core:strings"
|
||||
import "core:mem"
|
||||
import "base:runtime"
|
||||
|
||||
WINDOW_WIDTH :: 1280
|
||||
|
|
@ -11,72 +8,6 @@ WINDOW_HEIGHT :: 720
|
|||
WINDOW_FRAMERATE :: 30
|
||||
WINDOW_CAPTION :: "Transformation Visualizer"
|
||||
|
||||
v2 :: [2]f32
|
||||
rect2 :: rl.Rectangle
|
||||
|
||||
UiElementType :: enum {
|
||||
Input,
|
||||
Button,
|
||||
}
|
||||
|
||||
UiElementData :: union {
|
||||
UiButtonData,
|
||||
}
|
||||
|
||||
UiButtonData :: struct {
|
||||
label: cstring,
|
||||
}
|
||||
|
||||
UiElement :: struct {
|
||||
area: rect2,
|
||||
type: UiElementType,
|
||||
data: UiElementData,
|
||||
}
|
||||
|
||||
UiState :: struct {
|
||||
origin, position: v2,
|
||||
same_line: bool,
|
||||
style: UiStyle,
|
||||
elements: [dynamic]UiElement,
|
||||
}
|
||||
|
||||
UiStyle :: struct {
|
||||
margin, padding: f32,
|
||||
}
|
||||
|
||||
ui_button :: proc(state: ^UiState, size: v2, data: UiButtonData) {
|
||||
element := UiElement{area = {**state.position, **size}, type = .Button, data = data}
|
||||
if state.same_line {
|
||||
state.position.x += size.x + state.style.padding
|
||||
state.same_line = false
|
||||
} else {
|
||||
state.position.y += size.y + state.style.padding
|
||||
}
|
||||
append(&state.elements, element)
|
||||
}
|
||||
|
||||
ui_same_line :: proc(state: ^UiState) {
|
||||
state.same_line = true
|
||||
}
|
||||
|
||||
ui_start :: proc(state: ^UiState) {
|
||||
clear(&state.elements)
|
||||
state.position = state.origin + state.style.margin
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
init :: proc() {
|
||||
rl.SetTraceLogLevel(.WARNING)
|
||||
rl.SetConfigFlags({.WINDOW_RESIZABLE, .VSYNC_HINT})
|
||||
|
|
@ -85,33 +16,40 @@ init :: proc() {
|
|||
}
|
||||
|
||||
update :: proc() {
|
||||
ui_style := UiStyle{margin = 4.0, padding = 4.0}
|
||||
ui_state: UiState
|
||||
ui_init(&ui_state, ui_style, {0, 0}, context.temp_allocator)
|
||||
ui_start(&ui_state)
|
||||
ui_button_size := v2{192.0, 32.0}
|
||||
ui_same_line(&ui_state)
|
||||
ui_button(&ui_state, ui_button_size, {label = "Hi"})
|
||||
ui_same_line(&ui_state)
|
||||
ui_button(&ui_state, ui_button_size, {label = "Hi"})
|
||||
ui_same_line(&ui_state)
|
||||
ui_button(&ui_state, ui_button_size, {label = "Hi"})
|
||||
ui_elements := ui_end(ui_state)
|
||||
|
||||
rl.BeginDrawing()
|
||||
defer rl.EndDrawing()
|
||||
rl.ClearBackground(rl.BLACK)
|
||||
|
||||
ui_style := UiStyle{margin = 4.0, padding = 4.0}
|
||||
ui_state: UiState
|
||||
ui_init(&ui_state, ui_style, {0, 0}, context.temp_allocator)
|
||||
ui_start(&ui_state)
|
||||
ui_container_begin(&ui_state, {label = ""})
|
||||
ui_button_size := v2{192.0, 32.0}
|
||||
ui_button(&ui_state, ui_button_size, {label = "Hi"})
|
||||
ui_same_line(&ui_state)
|
||||
ui_button(&ui_state, ui_button_size, {label = "Hi"})
|
||||
ui_same_line(&ui_state)
|
||||
ui_button(&ui_state, ui_button_size, {label = "Hi"})
|
||||
ui_container_end(&ui_state)
|
||||
ui_elements := ui_end(ui_state)
|
||||
|
||||
for element in ui_elements {
|
||||
fmt.println(element)
|
||||
switch element.type {
|
||||
case .Button:
|
||||
data := element.data.(UiButtonData)
|
||||
data := element.data.(UiElementData_Button)
|
||||
rl.GuiButton(element.area, data.label)
|
||||
case .Input:
|
||||
case .Container:
|
||||
data := element.data.(UiElementData_Container)
|
||||
if len(data.label) == 0 {
|
||||
rl.DrawRectangleRoundedLinesEx(element.area, 0.1, 2, 1.0, rl.WHITE)
|
||||
} else {
|
||||
rl.GuiGroupBox(element.area, data.label)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
fmt.println()
|
||||
|
||||
free_all(context.temp_allocator)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue