support for prompts

This commit is contained in:
Ivar Fatland
2026-05-12 13:41:12 +02:00
parent 1db82da82b
commit fc920e2b36
2 changed files with 55 additions and 22 deletions
+3 -1
View File
@@ -21,7 +21,9 @@ func main() {
choices = slices.DeleteFunc(choices, func(l string) bool { choices = slices.DeleteFunc(choices, func(l string) bool {
return l == "" return l == ""
}) })
choice, ok := picker.RaylibPicker(choices) choice, ok := picker.Picker(
choices,
)
if ok { if ok {
fmt.Println(choices[choice]) fmt.Println(choices[choice])
} }
+52 -21
View File
@@ -16,14 +16,47 @@ const textBoxHeight = fontSize + 6
//go:embed gomono-font/Go-Mono.ttf //go:embed gomono-font/Go-Mono.ttf
var font_tff []byte var font_tff []byte
type State struct { func style() {
Font rl.Font rg.SetFont(rl.LoadFontFromMemory(".ttf", font_tff, fontSize, nil))
rg.SetStyle(rg.DEFAULT, rg.TEXT_SIZE, fontSize)
rg.SetStyle(
rg.DEFAULT,
rg.BASE_COLOR_PRESSED,
rg.NewColorPropertyValue(rl.Black),
)
rg.SetStyle(
rg.DEFAULT,
rg.BORDER_COLOR_PRESSED,
rg.NewColorPropertyValue(rl.White),
)
rg.SetStyle(
rg.DEFAULT,
rg.TEXT_COLOR_PRESSED,
rg.NewColorPropertyValue(rl.White),
)
} }
func RaylibPicker(options []string) (choiceIdx int, ok bool) { type RaylibPickerOptions struct {
state := initialize(len(options)) Prompt string
rg.SetFont(state.Font) }
rg.SetStyle(rg.DEFAULT, rg.TEXT_SIZE, fontSize) var defaultPickerOptions = RaylibPickerOptions{}
type PickerOption func(*RaylibPickerOptions)
func WithPrompt(prompt string) PickerOption {
return func(opts *RaylibPickerOptions) {
opts.Prompt = prompt
}
}
func Picker(options []string, pickerOptions ...PickerOption) (choiceIdx int, ok bool) {
pickerOpts := defaultPickerOptions
for _, opt := range pickerOptions {
opt(&pickerOpts)
}
initialize(len(options))
// TODO kinda dumb with additional initialization here // TODO kinda dumb with additional initialization here
originalOrder := make([]matching.Matched, len(options)) originalOrder := make([]matching.Matched, len(options))
for i := range options { for i := range options {
@@ -54,6 +87,17 @@ func RaylibPicker(options []string) (choiceIdx int, ok bool) {
} }
rg.TextBox(bounds, &input, 1024, true) rg.TextBox(bounds, &input, 1024, true)
if input == "" {
promptBounds := bounds
promptBounds.X += promptBounds.Height
promptBounds.Width -= promptBounds.Height
rg.DrawText(
pickerOpts.Prompt,
promptBounds,
int32(rg.TEXT_ALIGN_LEFT),
rl.Gray,
)
}
if prevInput != input { if prevInput != input {
if len(input) > 0 { if len(input) > 0 {
matched = matching.FindAll(input, options) matched = matching.FindAll(input, options)
@@ -119,7 +163,7 @@ func RaylibPicker(options []string) (choiceIdx int, ok bool) {
return 0, false return 0, false
} }
func initialize(nOptions int) State { func initialize(nOptions int) {
rl.SetTraceLogLevel(rl.LogError) rl.SetTraceLogLevel(rl.LogError)
rl.InitWindow(0, 0, "raymenu") rl.InitWindow(0, 0, "raymenu")
rl.SetWindowState(rl.FlagWindowUndecorated) rl.SetWindowState(rl.FlagWindowUndecorated)
@@ -141,10 +185,7 @@ func initialize(nOptions int) State {
monitorWidth/2-rl.GetScreenWidth()/2, monitorWidth/2-rl.GetScreenWidth()/2,
monitorHeight/2-rl.GetScreenHeight()/2, monitorHeight/2-rl.GetScreenHeight()/2,
) )
style()
return State{
Font: rl.LoadFontFromMemory(".ttf", font_tff, fontSize, nil),
}
} }
func isPressedRepeat(key int32) bool { func isPressedRepeat(key int32) bool {
@@ -152,16 +193,6 @@ func isPressedRepeat(key int32) bool {
} }
func screenBounds() rl.Rectangle {
return rl.Rectangle{
X: 0,
Y: 0,
Width: float32(rl.GetScreenWidth()),
Height: float32(rl.GetScreenHeight()),
}
}
var yOffset float32 = 0.0 var yOffset float32 = 0.0
func resetBounds() { func resetBounds() {