2024-09-09 17:48:24 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import rl "vendor:raylib"
|
|
|
|
import "core:math/ease"
|
2024-09-09 19:05:38 +03:00
|
|
|
import "core:fmt"
|
2024-09-09 17:48:24 +03:00
|
|
|
|
|
|
|
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),
|
2024-09-09 19:05:38 +03:00
|
|
|
background: rl.Color,
|
|
|
|
mouse_pos: Vec2,
|
2024-09-09 17:48:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
menu_list_update :: proc(list: ^MenuList($T)) {
|
2024-09-09 19:05:38 +03:00
|
|
|
|
|
|
|
activate_element := false
|
2024-09-09 17:48:24 +03:00
|
|
|
prev_element := cast(i8)list.active_element
|
|
|
|
cur_element := prev_element
|
|
|
|
|
2024-09-09 19:05:38 +03:00
|
|
|
last_mouse_pos := list.mouse_pos
|
|
|
|
list.mouse_pos = rl.GetMousePosition()
|
|
|
|
|
|
|
|
size := menu_list_get_size(list)
|
|
|
|
if rl.CheckCollisionPointRec(list.mouse_pos, rl.Rectangle{
|
|
|
|
x = list.position.x,
|
|
|
|
y = list.position.y,
|
|
|
|
width = size.x,
|
|
|
|
height = size.y,
|
|
|
|
}) {
|
|
|
|
if last_mouse_pos != list.mouse_pos {
|
|
|
|
mouse_relative := list.mouse_pos - list.position
|
|
|
|
cur_element = i8(mouse_relative.y / list.line_size)
|
|
|
|
fmt.println(cur_element)
|
|
|
|
}
|
|
|
|
if rl.IsMouseButtonPressed(rl.MouseButton.LEFT) {
|
|
|
|
list.menu_pressed(list.state, list.active_element)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last_mouse_pos = list.mouse_pos
|
|
|
|
|
|
|
|
|
2024-09-09 17:48:24 +03:00
|
|
|
|
|
|
|
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,
|
2024-09-09 19:05:38 +03:00
|
|
|
0.25,
|
|
|
|
ease.Ease.Quadratic_Out,
|
2024-09-09 17:48:24 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
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)) {
|
2024-09-09 19:05:38 +03:00
|
|
|
if list.background[3] != 0 {
|
|
|
|
size := menu_list_get_size(list)
|
|
|
|
rl.DrawRectangleV(list.position - {40, 40}, size + {80, 80}, list.background)
|
|
|
|
}
|
2024-09-09 17:48:24 +03:00
|
|
|
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}
|
2024-09-09 19:05:38 +03:00
|
|
|
rl.DrawTextEx(FontUI, el, pos, list.font_size, 2, rl.WHITE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
menu_list_get_size :: proc(list: ^MenuList($T)) -> Vec2 {
|
|
|
|
size := Vec2{}
|
|
|
|
for el, i in list.elements {
|
|
|
|
line_size := rl.MeasureTextEx(FontUI, el, list.font_size, 2)
|
|
|
|
if line_size.x > size.x {
|
|
|
|
size.x = line_size.x
|
|
|
|
}
|
|
|
|
size.y += list.line_size
|
2024-09-09 17:48:24 +03:00
|
|
|
}
|
2024-09-09 19:05:38 +03:00
|
|
|
return size
|
2024-09-09 17:48:24 +03:00
|
|
|
}
|