-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsprites.go
232 lines (205 loc) · 5.89 KB
/
sprites.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"encoding/json"
"fmt"
"image"
"image/color"
"fyne.io/fyne/v2"
)
type SpriteStack struct {
sprites []*AnimatedSprite
fileCache map[string]image.Image
skin *Skin
actionHandler map[string]func() error
draggedItem int
}
func (s *SpriteStack) FindByID(name string) *AnimatedSprite {
for _, sprite := range s.sprites {
if sprite.ID == name {
return sprite
}
}
return nil
}
func (s *SpriteStack) Dragged(event *fyne.DragEvent) {
x := int(event.Position.X / scaleFactor)
y := int(event.Position.Y / scaleFactor)
if s.draggedItem < 0 {
for i := range s.sprites {
sp := s.sprites[len(s.sprites)-i-1]
if sp.Collision(x, y) {
s.draggedItem = len(s.sprites) - i - 1
break
}
}
if s.draggedItem < 0 {
return
}
}
fmt.Println(s.sprites[s.draggedItem].ID)
if s.sprites[s.draggedItem].DragAble &&
x > s.sprites[s.draggedItem].MinDragX &&
x < s.sprites[s.draggedItem].MaxDragX {
s.sprites[s.draggedItem].AbsolutePositionX = x
return
}
}
func (s *SpriteStack) DragEnd() {
if s.draggedItem < 0 {
s.sprites[s.draggedItem].dePressed()
}
s.draggedItem = -1
}
func (s *SpriteStack) DrawAtPosition(x, y int) color.Color {
for i := range s.sprites {
s := s.sprites[len(s.sprites)-i-1]
if s.Collision(x, y) {
return s.At(x, y)
}
}
return nil
}
func (s *SpriteStack) MouseDown(x, y int) {
for i := range s.sprites {
sp := s.sprites[len(s.sprites)-i-1]
if sp.Collision(x, y) {
sp.pressed()
return
}
}
}
func (s *SpriteStack) DoAtPosition(x, y int) {
for i := range s.sprites {
sp := s.sprites[len(s.sprites)-i-1]
if sp.Collision(x, y) {
sp.dePressed()
if sp.Action != "" && s.actionHandler != nil {
if fn, ok := s.actionHandler[sp.Action]; ok {
err := fn()
if err != nil {
// FIXME: Bubble this up
fmt.Println(fmt.Errorf("error calling action %s: %v", sp.Action, err))
}
}
return
}
return
}
}
}
func (s *SpriteStack) UnmarshalJSON(data []byte) error {
var tgt []*AnimatedSprite
if err := json.Unmarshal(data, &tgt); err != nil {
return err
}
for _, sprite := range tgt {
if err := sprite.Load(s.skin, s.fileCache); err != nil {
return fmt.Errorf("loading image in: %w", err)
}
}
s.sprites = tgt
return nil
}
// AnimatedSprite is a sprite that can contain two states (regular and down), the sprites for both are contained in
// image and downimage attributes respectively.
type AnimatedSprite struct {
ID string `json:"id"`
Action string `json:"action"`
AbsolutePositionX int `json:"absolutePositionX"`
AbsolutePositionY int `json:"absolutePositionY"`
Image Sprite `json:"image"`
DownImage *Sprite `json:"downImage"`
ActiveImage *Sprite `json:"activeImage"`
Tooltip string `json:"tooltip"`
ToggleAble bool `json:"isToggle"`
DragAble bool `json:"dragAble"`
MinDragX int `json:"minDrag"`
MaxDragX int `json:"maxDrag"`
// These are not part of the json, they are used to track the state of the sprite
Pressed bool `json:"-"`
Toggled bool `json:"-"`
}
func (s *AnimatedSprite) Collision(x, y int) bool {
inX := x >= s.AbsolutePositionX && x < s.AbsolutePositionX+s.Image.SpriteWidth
inY := y >= s.AbsolutePositionY && y < s.AbsolutePositionY+s.Image.SpriteHeight
return inX && inY
}
func (s *AnimatedSprite) Load(skin *Skin, fileCache map[string]image.Image) error {
if err := s.Image.Load(skin, fileCache); err != nil {
return fmt.Errorf("loading Sprite: %s in Animated Sprite %s: %w", s.Image.ID, s.ID, err)
}
if s.DownImage != nil {
if err := s.DownImage.Load(skin, fileCache); err != nil {
return fmt.Errorf("loading DownSprite: %s: %w", s.DownImage.ID, err)
}
}
if s.ActiveImage != nil {
if err := s.ActiveImage.Load(skin, fileCache); err != nil {
return fmt.Errorf("loading DownSprite: %s: %w", s.ActiveImage.ID, err)
}
}
return nil
}
// Sprite is a single sprite, they are used in the context of an AnimatedSprite as one of the two frames
type Sprite struct {
ID string `json:"id"`
File string `json:"file"`
Image image.Image `json:"-"`
SpritePositionX int `json:"spritePositionX"`
SpritePositionY int `json:"spritePositionY"`
SpriteHeight int `json:"spriteHeight"`
SpriteWidth int `json:"spriteWidth"`
}
func (s *Sprite) Load(skin *Skin, fileCache map[string]image.Image) error {
rawImg, ok := fileCache[s.File]
// I suspect that, due to this was done for fat32, the skins contain uppercase filenames.
if !ok {
f, err := skin.Open(s.File)
if err != nil {
return fmt.Errorf("opening file: %s: %w", s.File, err)
}
defer f.Close()
rawImg, _, err = image.Decode(f)
if err != nil {
return fmt.Errorf("decoding image: %s: %w", s.File, err)
}
fileCache[s.File] = rawImg
}
s.Image = rawImg
return nil
}
func (s *AnimatedSprite) pressed() {
s.Pressed = true
}
func (s *AnimatedSprite) dePressed() {
s.Pressed = false
// if this is not a toggle is a noop
s.Toggled = !s.Toggled
}
func (s *AnimatedSprite) ColorModel() color.Model {
return s.Image.Image.ColorModel()
}
func (s *AnimatedSprite) Bounds() image.Rectangle {
return s.Image.Image.Bounds()
}
func (s *Sprite) At(x, y int) color.Color {
return s.Image.At(x+s.SpritePositionX, y+s.SpritePositionY)
}
func (s *AnimatedSprite) At(x, y int) color.Color {
posX := x - s.AbsolutePositionX
posY := y - s.AbsolutePositionY
if s.Pressed && s.DownImage != nil {
return s.DownImage.At(posX, posY)
}
if s.ToggleAble && s.Toggled && s.ActiveImage != nil {
return s.ActiveImage.At(posX, posY)
}
return s.Image.At(posX, posY)
}
func (s *AnimatedSprite) DraggableSeek(percentage float64) {
if !s.DragAble || (percentage > 1 || percentage < 0) {
return
}
s.AbsolutePositionX = s.MinDragX + int(float64(s.MaxDragX-s.MinDragX)*percentage)
}
var _ image.Image = (*AnimatedSprite)(nil)