cycle through presets with right

This commit is contained in:
filifa 2024-09-02 22:17:11 -05:00
parent bf6bf28635
commit 21c0e3f447
2 changed files with 60 additions and 3 deletions

View File

@ -25,6 +25,7 @@ import (
) )
var preset string var preset string
var scriptPath string
func handleWindowEvent(event *sdl.WindowEvent, m *milkDropWindow) { func handleWindowEvent(event *sdl.WindowEvent, m *milkDropWindow) {
switch event.Event { switch event.Event {
@ -34,6 +35,13 @@ func handleWindowEvent(event *sdl.WindowEvent, m *milkDropWindow) {
} }
} }
func handleKeyboardEvent(event *sdl.KeyboardEvent, m *milkDropWindow) {
scancode := event.Keysym.Scancode
if event.Type == sdl.KEYDOWN && scancode == sdl.SCANCODE_RIGHT {
m.nextPreset()
}
}
func handleEvent(event sdl.Event, m *milkDropWindow) bool { func handleEvent(event sdl.Event, m *milkDropWindow) bool {
switch event.(type) { switch event.(type) {
case *sdl.QuitEvent: case *sdl.QuitEvent:
@ -41,6 +49,9 @@ func handleEvent(event sdl.Event, m *milkDropWindow) bool {
case *sdl.WindowEvent: case *sdl.WindowEvent:
event := event.(*sdl.WindowEvent) event := event.(*sdl.WindowEvent)
handleWindowEvent(event, m) handleWindowEvent(event, m)
case *sdl.KeyboardEvent:
event := event.(*sdl.KeyboardEvent)
handleKeyboardEvent(event, m)
} }
return true return true
@ -60,7 +71,7 @@ func milkbucket(cmd *cobra.Command, args []string) {
} }
defer sdl.Quit() defer sdl.Quit()
m, err := newMilkDropWindow(800, 600) m, err := newMilkDropWindow(800, 600, scriptPath)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -115,4 +126,5 @@ func init() {
// Cobra also supports local flags, which will only run // Cobra also supports local flags, which will only run
// when this action is called directly. // when this action is called directly.
rootCmd.Flags().StringVarP(&preset, "preset", "p", "", "Preset file to use") rootCmd.Flags().StringVarP(&preset, "preset", "p", "", "Preset file to use")
rootCmd.Flags().StringVarP(&scriptPath, "script", "s", "", "Script file to use")
} }

View File

@ -24,7 +24,9 @@ package cmd
import "C" import "C"
import ( import (
"container/ring"
"encoding/binary" "encoding/binary"
"encoding/json"
"errors" "errors"
"io" "io"
"os" "os"
@ -39,12 +41,55 @@ type milkDropWindow struct {
window *sdl.Window window *sdl.Window
context sdl.GLContext context sdl.GLContext
handle C.projectm_handle handle C.projectm_handle
preset *ring.Ring
} }
func newMilkDropWindow(width, height int32) (*milkDropWindow, error) { type script struct {
Presets []string
Times []float64
}
func readScript(scriptPath string) (*script, error) {
if scriptPath == "" {
return nil, errors.New("no script given")
}
f, err := os.Open(scriptPath)
if err != nil {
return nil, err
}
defer f.Close()
data, err := io.ReadAll(f)
if err != nil {
return nil, err
}
var s script
err = json.Unmarshal(data, &s)
return &s, err
}
func (m *milkDropWindow) nextPreset() {
m.preset = m.preset.Next()
m.loadPreset(m.preset.Value.(string))
}
func newMilkDropWindow(width, height int32, scriptPath string) (*milkDropWindow, error) {
var m milkDropWindow var m milkDropWindow
var err error var err error
s, err := readScript(scriptPath)
if err != nil {
return nil, err
}
m.preset = ring.New(len(s.Presets))
for _, preset := range s.Presets {
m.preset.Value = preset
m.preset = m.preset.Next()
}
m.window, err = sdl.CreateWindow("milkbucket", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, width, height, sdl.WINDOW_OPENGL|sdl.WINDOW_RESIZABLE) m.window, err = sdl.CreateWindow("milkbucket", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, width, height, sdl.WINDOW_OPENGL|sdl.WINDOW_RESIZABLE)
if err != nil { if err != nil {
return &m, err return &m, err
@ -74,7 +119,7 @@ func (m *milkDropWindow) destroy() {
func (m *milkDropWindow) loadPreset(preset string) { func (m *milkDropWindow) loadPreset(preset string) {
cPreset := C.CString(preset) cPreset := C.CString(preset)
defer C.free(unsafe.Pointer(cPreset)) defer C.free(unsafe.Pointer(cPreset))
C.projectm_load_preset_file(m.handle, cPreset, false) C.projectm_load_preset_file(m.handle, cPreset, true)
} }
func (m *milkDropWindow) render() (bool, error) { func (m *milkDropWindow) render() (bool, error) {