From 485b783341da0c9c07de20c65603683c026e881c Mon Sep 17 00:00:00 2001 From: vsariola <5684185+vsariola@users.noreply.github.com> Date: Sat, 17 Apr 2021 23:08:12 +0300 Subject: [PATCH] feat(gioui): add buttons to save and load instrument --- tracker/gioui/files.go | 40 ++++++++++++++++++++++++++++++++++++ tracker/gioui/instruments.go | 19 +++++++++++++++++ tracker/gioui/tracker.go | 4 ++++ 3 files changed, 63 insertions(+) diff --git a/tracker/gioui/files.go b/tracker/gioui/files.go index 910cef7..aa184eb 100644 --- a/tracker/gioui/files.go +++ b/tracker/gioui/files.go @@ -84,6 +84,46 @@ func (t *Tracker) saveSong(filename string) bool { return true } +func (t *Tracker) SaveInstrument() bool { + filename, err := dialog.File().Filter("Sointu YAML instrument", "yml").Filter("Sointu JSON song", "json").Title(fmt.Sprintf("Save instrument %v", t.Instrument().Name)).Save() + if err != nil { + return false + } + var extension = filepath.Ext(filename) + var contents []byte + if extension == "json" { + contents, err = json.Marshal(t.Instrument()) + } else { + contents, err = yaml.Marshal(t.Instrument()) + } + if err != nil { + return false + } + if extension == "" { + filename = filename + ".yml" + } + ioutil.WriteFile(filename, contents, 0644) + return true +} + +func (t *Tracker) LoadInstrument() { + filename, err := dialog.File().Filter("Sointu YAML instrument", "yml").Filter("Sointu JSON instrument", "json").Title("Load instrument").Load() + if err != nil { + return + } + bytes, err := ioutil.ReadFile(filename) + if err != nil { + return + } + var instrument sointu.Instrument + if errJSON := json.Unmarshal(bytes, &instrument); errJSON != nil { + if errYaml := yaml.Unmarshal(bytes, &instrument); errYaml != nil { + return + } + } + t.SetInstrument(instrument) +} + func (t *Tracker) exportWav(pcm16 bool) { filename, err := dialog.File().Filter(".wav file", "wav").Title("Export .wav").Save() if err != nil { diff --git a/tracker/gioui/instruments.go b/tracker/gioui/instruments.go index 9ef50c9..afeb157 100644 --- a/tracker/gioui/instruments.go +++ b/tracker/gioui/instruments.go @@ -77,6 +77,16 @@ func (t *Tracker) layoutInstrumentHeader(gtx C) D { copyInstrumentBtnStyle.Inset = layout.UniformInset(unit.Dp(6)) copyInstrumentBtnStyle.Color = primaryColor + saveInstrumentBtnStyle := material.IconButton(t.Theme, t.SaveInstrumentBtn, widgetForIcon(icons.ContentSave)) + saveInstrumentBtnStyle.Background = transparent + saveInstrumentBtnStyle.Inset = layout.UniformInset(unit.Dp(6)) + saveInstrumentBtnStyle.Color = primaryColor + + loadInstrumentBtnStyle := material.IconButton(t.Theme, t.LoadInstrumentBtn, widgetForIcon(icons.FileFolderOpen)) + loadInstrumentBtnStyle.Background = transparent + loadInstrumentBtnStyle.Inset = layout.UniformInset(unit.Dp(6)) + loadInstrumentBtnStyle.Color = primaryColor + deleteInstrumentBtnStyle := material.IconButton(t.Theme, t.DeleteInstrumentBtn, widgetForIcon(icons.ActionDelete)) deleteInstrumentBtnStyle.Background = transparent deleteInstrumentBtnStyle.Inset = layout.UniformInset(unit.Dp(6)) @@ -99,6 +109,8 @@ func (t *Tracker) layoutInstrumentHeader(gtx C) D { return dims }), layout.Flexed(1, func(gtx C) D { return layout.Dimensions{Size: gtx.Constraints.Min} }), + layout.Rigid(saveInstrumentBtnStyle.Layout), + layout.Rigid(loadInstrumentBtnStyle.Layout), layout.Rigid(copyInstrumentBtnStyle.Layout), layout.Rigid(deleteInstrumentBtnStyle.Layout)) } @@ -119,6 +131,13 @@ func (t *Tracker) layoutInstrumentHeader(gtx C) D { for t.ConfirmInstrDelete.BtnCancel.Clicked() { t.ConfirmInstrDelete.Visible = false } + for t.SaveInstrumentBtn.Clicked() { + t.SaveInstrument() + } + + for t.LoadInstrumentBtn.Clicked() { + t.LoadInstrument() + } return Surface{Gray: 37, Focus: t.EditMode() == tracker.EditUnits || t.EditMode() == tracker.EditParameters}.Layout(gtx, header) } diff --git a/tracker/gioui/tracker.go b/tracker/gioui/tracker.go index e217901..3bf3d9d 100644 --- a/tracker/gioui/tracker.go +++ b/tracker/gioui/tracker.go @@ -46,6 +46,8 @@ type Tracker struct { SongLength *NumberInput PanicBtn *widget.Clickable CopyInstrumentBtn *widget.Clickable + SaveInstrumentBtn *widget.Clickable + LoadInstrumentBtn *widget.Clickable ParameterList *layout.List ParameterScrollBar *ScrollBar Parameters []*ParameterWidget @@ -145,6 +147,8 @@ func New(audioContext sointu.AudioContext, synthService sointu.SynthService, syn ClearUnitBtn: new(widget.Clickable), PanicBtn: new(widget.Clickable), CopyInstrumentBtn: new(widget.Clickable), + SaveInstrumentBtn: new(widget.Clickable), + LoadInstrumentBtn: new(widget.Clickable), TrackHexCheckBox: new(widget.Bool), Menus: make([]Menu, 2), MenuBar: make([]widget.Clickable, 2),