/
switch.go
242 lines (217 loc) · 5.89 KB
/
switch.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
233
234
235
236
237
238
239
240
241
242
package render
import (
"image"
"image/draw"
"sync"
"github.com/oakmound/oak/event"
"github.com/oakmound/oak/oakerr"
"github.com/oakmound/oak/physics"
"github.com/oakmound/oak/render/mod"
)
// The Switch type is intended for use to easily swap between multiple
// renderables that are drawn at the same position on the same layer.
// A common use case for this would be a character entitiy who switches
// their animation based on how they are moving or what they are doing.
//
// The Switch type removes the need to repeatedly draw and undraw elements
// of a character, which has a tendency to leave nothing drawn for a draw frame
// as the switch happens.
type Switch struct {
LayeredPoint
subRenderables map[string]Modifiable
curRenderable string
lock sync.RWMutex
}
// NewSwitch creates a new Switch from a map of names to modifiables
func NewSwitch(start string, m map[string]Modifiable) *Switch {
return &Switch{
LayeredPoint: NewLayeredPoint(0, 0, 0),
subRenderables: m,
curRenderable: start,
lock: sync.RWMutex{},
}
}
// Add makes a new entry in the Switch's map
func (c *Switch) Add(k string, v Modifiable) (err error) {
if _, ok := c.subRenderables[k]; ok {
err = oakerr.ExistingElement{
InputName: "k",
InputType: "string",
Overwritten: true,
}
}
c.lock.Lock()
c.subRenderables[k] = v
c.lock.Unlock()
return err
}
// Set sets the current renderable to the one specified
func (c *Switch) Set(k string) error {
c.lock.RLock()
if _, ok := c.subRenderables[k]; !ok {
return oakerr.InvalidInput{InputName: "k"}
}
c.lock.RUnlock()
c.curRenderable = k
return nil
}
// GetSub returns a keyed Modifiable from this Switch's map
func (c *Switch) GetSub(s string) Modifiable {
c.lock.RLock()
m := c.subRenderables[s]
c.lock.RUnlock()
return m
}
// Get returns the Switch's current key
func (c *Switch) Get() string {
return c.curRenderable
}
// SetOffsets sets the logical offset for the specified key
func (c *Switch) SetOffsets(k string, offsets physics.Vector) {
c.lock.RLock()
if r, ok := c.subRenderables[k]; ok {
r.SetPos(offsets.X(), offsets.Y())
}
c.lock.RUnlock()
}
// Copy creates a copy of the Switch
func (c *Switch) Copy() Modifiable {
newC := new(Switch)
newC.LayeredPoint = c.LayeredPoint.Copy()
newSubRenderables := make(map[string]Modifiable)
c.lock.RLock()
for k, v := range c.subRenderables {
newSubRenderables[k] = v.Copy()
}
c.lock.RUnlock()
newC.subRenderables = newSubRenderables
newC.curRenderable = c.curRenderable
newC.lock = sync.RWMutex{}
return newC
}
//GetRGBA returns the current renderables rgba
func (c *Switch) GetRGBA() *image.RGBA {
c.lock.RLock()
rgba := c.subRenderables[c.curRenderable].GetRGBA()
c.lock.RUnlock()
return rgba
}
// Modify performs the input modifications on all elements of the Switch
func (c *Switch) Modify(ms ...mod.Mod) Modifiable {
c.lock.RLock()
for _, r := range c.subRenderables {
r.Modify(ms...)
}
c.lock.RUnlock()
return c
}
// Filter filters all elements of the Switch with fs
func (c *Switch) Filter(fs ...mod.Filter) {
c.lock.RLock()
for _, r := range c.subRenderables {
r.Filter(fs...)
}
c.lock.RUnlock()
}
//DrawOffset draws the Switch at an offset from its logical location
func (c *Switch) DrawOffset(buff draw.Image, xOff float64, yOff float64) {
c.lock.RLock()
c.subRenderables[c.curRenderable].DrawOffset(buff, c.X()+xOff, c.Y()+yOff)
c.lock.RUnlock()
}
//Draw draws the Switch at its logical location
func (c *Switch) Draw(buff draw.Image) {
c.lock.RLock()
c.subRenderables[c.curRenderable].DrawOffset(buff, c.X(), c.Y())
c.lock.RUnlock()
}
// ShiftPos shifts the Switch's logical position
func (c *Switch) ShiftPos(x, y float64) {
c.SetPos(c.X()+x, c.Y()+y)
}
// GetDims gets the current Renderables dimensions
func (c *Switch) GetDims() (int, int) {
c.lock.RLock()
w, h := c.subRenderables[c.curRenderable].GetDims()
c.lock.RUnlock()
return w, h
}
// Pause stops the current Renderable if possible
func (c *Switch) Pause() {
c.lock.RLock()
if cp, ok := c.subRenderables[c.curRenderable].(CanPause); ok {
cp.Pause()
}
c.lock.RUnlock()
}
// Unpause tries to unpause the current Renderable if possible
func (c *Switch) Unpause() {
c.lock.RLock()
if cp, ok := c.subRenderables[c.curRenderable].(CanPause); ok {
cp.Unpause()
}
c.lock.RUnlock()
}
// IsInterruptable returns whether the current renderable is interruptable
func (c *Switch) IsInterruptable() bool {
c.lock.RLock()
defer c.lock.RUnlock()
if i, ok := c.subRenderables[c.curRenderable].(NonInterruptable); ok {
return i.IsInterruptable()
}
return true
}
// IsStatic returns whether the current renderable is static
func (c *Switch) IsStatic() bool {
c.lock.RLock()
defer c.lock.RUnlock()
if s, ok := c.subRenderables[c.curRenderable].(NonStatic); ok {
return s.IsStatic()
}
return true
}
// SetTriggerID sets the ID AnimationEnd will trigger on for animating subtypes.
// Todo: standardize this with the other interface Set functions so that it
// also only acts on the current subRenderable, or the other way around, or
// somehow offer both options
func (c *Switch) SetTriggerID(cid event.CID) {
c.lock.RLock()
for _, r := range c.subRenderables {
if t, ok := r.(Triggerable); ok {
t.SetTriggerID(cid)
}
}
c.lock.RUnlock()
}
// Revert will revert all parts of this Switch that can be reverted
func (c *Switch) Revert(mod int) {
c.lock.RLock()
for _, v := range c.subRenderables {
switch t := v.(type) {
case *Reverting:
t.Revert(mod)
}
}
c.lock.RUnlock()
}
// RevertAll will revert all parts of this Switch that can be reverted, back
// to their original state.
func (c *Switch) RevertAll() {
c.lock.RLock()
for _, v := range c.subRenderables {
switch t := v.(type) {
case *Reverting:
t.RevertAll()
}
}
c.lock.RUnlock()
}
func (c *Switch) update() {
c.lock.RLock()
for _, r := range c.subRenderables {
if u, ok := r.(updates); ok {
u.update()
}
}
c.lock.RUnlock()
}