Skip to content

Commit

Permalink
Attempt to load map from last used file on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Pippadi committed Jun 16, 2023
1 parent 370bc6e commit 25ca4fa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
66 changes: 38 additions & 28 deletions ui/mapeditor/mapeditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
)

type MapEditor struct {
parentApp fyne.App
parentWindow fyne.Window
mainContainer fyne.CanvasObject

Expand All @@ -29,11 +28,12 @@ type MapEditor struct {

StickConfigs map[wiimote.Stick]StickConfig `json:"StickConfig"`
stickConfigurators map[wiimote.Stick]*StickConfigurator

mapFile string
}

func New(a fyne.App, w fyne.Window) *MapEditor {
func New(w fyne.Window) *MapEditor {
m := new(MapEditor)
m.parentApp = a
m.parentWindow = w
m.buttons = make(map[wiimote.Keycode]*widget.Button)
m.selectors = make(map[wiimote.Keycode]*KeyChooser)
Expand Down Expand Up @@ -110,37 +110,43 @@ func (m *MapEditor) chooserChangeHandler(c wiimote.Keycode) func(fyne.KeyName) {
}

func (m *MapEditor) loadMap() {
dialog.ShowFileOpen(m.loadMapFromFile, m.parentWindow)
}

func (m *MapEditor) loadMapFromFile(file fyne.URIReadCloser, err error) {
if err != nil {
loggo.Error(err)
dialog.ShowError(err, m.parentWindow)
return
}
if file == nil {
return
}
defer file.Close()
dialog.ShowFileOpen(func(file fyne.URIReadCloser, err error) {
if err != nil {
loggo.Error(err)
dialog.ShowError(err, m.parentWindow)
return
}
if file == nil {
return
}
defer file.Close()

jsonBytes := make([]byte, 2048)
n, err := file.Read(jsonBytes)
jsonBytes = jsonBytes[:n]
if err != nil {
loggo.Error(err)
dialog.ShowError(errors.New("Unable to read file"), m.parentWindow)
}

jsonBytes := make([]byte, 2048)
n, err := file.Read(jsonBytes)
if err != nil {
loggo.Error(err)
dialog.ShowError(errors.New("Unable to read file"), m.parentWindow)
return
}
err = m.LoadMapFromBytes(jsonBytes)
if err != nil {
loggo.Error(err)
dialog.ShowError(errors.New("Unable to parse file"), m.parentWindow)
}
m.mapFile = file.URI().Path()
}, m.parentWindow,
)
}

jsonBytes = jsonBytes[:n]
err = json.Unmarshal(jsonBytes, m)
func (m *MapEditor) LoadMapFromBytes(jsonBytes []byte) error {
err := json.Unmarshal(jsonBytes, m)
if err != nil {
loggo.Error(err)
dialog.ShowError(errors.New("Unable to parse file"), m.parentWindow)
return
return err
}

m.updateButtonsFromMap()
return nil
}

func (m *MapEditor) saveMap() {
Expand Down Expand Up @@ -170,6 +176,8 @@ func (m *MapEditor) saveMapToFile(file fyne.URIWriteCloser, err error) {
loggo.Error(err)
dialog.ShowError(errors.New("Unable to write to file"), m.parentWindow)
}

m.mapFile = file.URI().Path()
}

func (m *MapEditor) remapBtnFor(c wiimote.Keycode) *widget.Button {
Expand Down Expand Up @@ -214,3 +222,5 @@ func (m *MapEditor) StickConfigFor(stick wiimote.Stick) StickConfig {
}
return cfg
}

func (m *MapEditor) MapFile() string { return m.mapFile }
11 changes: 10 additions & 1 deletion ui/ui.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ui

import (
"io/ioutil"
"math"
"time"

Expand Down Expand Up @@ -46,12 +47,20 @@ func (u *UI) Initialize() (err error) {
u.wwApp = app.NewWithID("com.github.Pippadi.WiiWill")
u.wwApp.Lifecycle().SetOnStopped(func() {
actor.SendStopMsg(u.Inbox())
u.wwApp.Preferences().SetString("MapFile", u.mapEditor.MapFile())
time.Sleep(500 * time.Millisecond) // Wait for preferences to persist
})
u.mainWindow = u.wwApp.NewWindow("WiiWill")

u.activityBar = widget.NewProgressBarInfinite()
u.statusLbl = widget.NewLabel("")
u.mapEditor = mapeditor.New(u.wwApp, u.mainWindow)
u.mapEditor = mapeditor.New(u.mainWindow)
if mf := u.wwApp.Preferences().String("MapFile"); mf != "" {
bytes, err := ioutil.ReadFile(mf)
if err == nil {
u.mapEditor.LoadMapFromBytes(bytes)
}
}

box := container.NewVBox(
u.mapEditor.UI(),
Expand Down

0 comments on commit 25ca4fa

Please sign in to comment.