-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
75 lines (68 loc) · 1.65 KB
/
config.go
1
2
3
4
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package spots
import (
"encoding/json"
"fmt"
"image"
"io"
"io/ioutil"
"os"
"sort"
)
type InputPictureConfig struct {
Offset image.Point `json:"offset"`
TileWidth int `json:"tileWidth"`
TileHeight int `json:"tileHeight"`
TileSpots []image.Point `json:"tileSpots"`
Height int `json:"height"`
Width int `json:"width"`
}
func NewSpotsFromConfig(path string) (Spots, error) {
var ipc InputPictureConfig
err := ipc.FromFile(path)
if err != nil {
return nil, err
}
spots := ipc.ToSpots()
return spots, nil
}
func (ipc *InputPictureConfig) FromReader(r io.Reader) error {
dec := json.NewDecoder(r)
err := dec.Decode(&*ipc)
if err != nil {
return fmt.Errorf("can not decode json. error: %v", err)
}
return nil
}
func (ipc *InputPictureConfig) FromFile(path string) error {
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("can not read Config file %v. error: %v", path, err)
}
defer f.Close()
return ipc.FromReader(f)
}
func (ipc *InputPictureConfig) WriteToFile(path string) error {
jsonConfig, err := json.MarshalIndent(ipc, "", "\t")
if err != nil {
return err
}
return ioutil.WriteFile(path, jsonConfig, 0622)
}
func (ipc *InputPictureConfig) ToSpots() Spots {
points := make([]image.Point, ipc.Height*ipc.Width*len(ipc.TileSpots))
i := 0
for y := 0; y < ipc.Height; y++ {
for x := 0; x < ipc.Width; x++ {
for _, spot := range ipc.TileSpots {
points[i] = image.Point{
X: ipc.Offset.X + x*ipc.TileWidth + spot.X,
Y: ipc.Offset.Y + y*ipc.TileHeight + spot.Y,
}
i++
}
}
}
spots := NewSpots(ipc.Width, points)
sort.Sort(spots)
return spots
}