move script loading to root

This commit is contained in:
filifa 2024-09-02 23:22:56 -05:00
parent 5c95c5346f
commit 86ef3a0e82
2 changed files with 42 additions and 41 deletions

View File

@ -17,6 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"encoding/json"
"io"
"log"
"os"
@ -27,6 +29,31 @@ import (
var preset string
var scriptPath string
type script struct {
Presets []string
Times []float64
}
func (s *script) load(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
data, err := io.ReadAll(f)
if err != nil {
return err
}
err = json.Unmarshal(data, s)
if err != nil {
return err
}
return nil
}
func handleWindowEvent(event *sdl.WindowEvent, m *milkDropWindow) {
switch event.Event {
case sdl.WINDOWEVENT_RESIZED:
@ -67,22 +94,30 @@ func milkbucket(cmd *cobra.Command, args []string) {
log.Fatal("nothing to read from stdin")
}
var presets []string
if preset != "" {
presets = []string{preset}
}
if scriptPath != "" {
var s script
s.load(scriptPath)
presets = s.Presets
}
err = sdl.Init(sdl.INIT_VIDEO)
if err != nil {
panic(err)
}
defer sdl.Quit()
m, err := newMilkDropWindow(800, 600, scriptPath)
m, err := newMilkDropWindow(800, 600, presets)
if err != nil {
panic(err)
}
defer m.destroy()
if preset != "" {
m.setupPresets([]string{preset})
}
m.loadPreset(false)
running := true

View File

@ -26,7 +26,6 @@ import "C"
import (
"container/ring"
"encoding/binary"
"encoding/json"
"errors"
"io"
"os"
@ -44,34 +43,6 @@ type milkDropWindow struct {
preset *ring.Ring
}
type script struct {
Presets []string
Times []float64
}
func (m *milkDropWindow) loadScript(scriptPath string) error {
f, err := os.Open(scriptPath)
if err != nil {
return err
}
defer f.Close()
data, err := io.ReadAll(f)
if err != nil {
return err
}
var s script
err = json.Unmarshal(data, &s)
if err != nil {
return err
}
m.setupPresets(s.Presets)
return nil
}
func (m *milkDropWindow) setupPresets(presets []string) {
m.preset = ring.New(len(presets))
for _, preset := range presets {
@ -90,16 +61,11 @@ func (m *milkDropWindow) prevPreset() {
m.loadPreset(true)
}
func newMilkDropWindow(width, height int32, scriptPath string) (*milkDropWindow, error) {
func newMilkDropWindow(width, height int32, presets []string) (*milkDropWindow, error) {
var m milkDropWindow
var err error
if scriptPath != "" {
err = m.loadScript(scriptPath)
if err != nil {
return nil, err
}
}
m.setupPresets(presets)
m.window, err = sdl.CreateWindow("milkbucket", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, width, height, sdl.WINDOW_OPENGL|sdl.WINDOW_RESIZABLE)
if err != nil {