Arkadodge/menu_list.odin

56 lines
1.3 KiB
Odin

package main
import rl "vendor:raylib"
import "core:math/ease"
MenuList :: struct($T: typeid) {
state: ^GameState,
position: Vec2,
line_size: f32,
font_size: f32,
active_element: T,
active_marker: Vec2,
tween: ^Tween,
elements: ^[T]cstring,
menu_pressed: proc(state: ^GameState, element: T),
}
menu_list_update :: proc(list: ^MenuList($T)) {
prev_element := cast(i8)list.active_element
cur_element := prev_element
if rl.IsKeyPressed(rl.KeyboardKey.DOWN) {
cur_element += 1
}
if rl.IsKeyPressed(rl.KeyboardKey.UP) {
cur_element -= 1
}
if prev_element != cur_element {
if cur_element < 0 { cur_element = len(T) -1 }
if cur_element == len(T) { cur_element = 0 }
list.active_element = cast(T)cur_element
if list.tween != nil {
tween_cancel(list.tween)
}
list.tween = tween_to(
&list.active_marker.y,
f32(list.active_element) * list.line_size,
0.5,
ease.Ease.Back_Out,
)
}
if rl.IsKeyPressed(rl.KeyboardKey.ENTER) || rl.IsKeyPressed(rl.KeyboardKey.SPACE) {
list.menu_pressed(list.state, list.active_element)
}
}
menu_list_draw :: proc(list: ^MenuList($T)) {
rl.DrawTextEx(FontUI, ">", list.position + list.active_marker + {-30, 0}, 48, 2, rl.WHITE)
for el, i in list.elements {
pos := list.position + {0, f32(i) * list.line_size}
rl.DrawTextEx(FontUI, el, pos, 48, 2, rl.WHITE)
}
}