feat(gioui): add buttons to save and load instrument

This commit is contained in:
vsariola 2021-04-17 23:08:12 +03:00
parent 2a9284473a
commit 485b783341
3 changed files with 63 additions and 0 deletions

View File

@ -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 {

View File

@ -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)
}

View File

@ -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),