save.go 867 Bytes
Newer Older
Loïck Bonniot's avatar
Loïck Bonniot committed
1
2
package gui

Loïck Bonniot's avatar
Loïck Bonniot committed
3
4
// This file handles open/save feature.

Loïck Bonniot's avatar
Loïck Bonniot committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import (
	"encoding/json"
	"io/ioutil"
	"strconv"
)

func (w *Window) Save(filename string) {
	data, err := json.Marshal(w.scene)
	if err != nil {
		w.StatusBar().ShowMessage(err.Error())
		return
	}

	err = ioutil.WriteFile(filename, data, 0600)
	if err != nil {
		w.StatusBar().ShowMessage(err.Error())
		return
	}
	w.StatusBar().ShowMessage("Saved file as " + filename)
}

func (w *Window) Open(filename string) {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		w.StatusBar().ShowMessage(err.Error())
		return
	}

	newScene := &Scene{}
	err = json.Unmarshal(data, newScene)
	if err != nil {
		w.StatusBar().ShowMessage(err.Error())
		return
	}

	w.scene = newScene
	w.StatusBar().ShowMessage("Imported file from " + filename + " (" + strconv.Itoa(len(w.scene.Events)) + " events)")
	w.initScene()
}