-
Notifications
You must be signed in to change notification settings - Fork 5
/
director.go
65 lines (57 loc) · 1.8 KB
/
director.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
package stagehand
type SceneTransitionTrigger int
// A Directive is a struct that represents how a scene should be transitioned
type Directive[T any] struct {
Dest Scene[T]
Transition SceneTransition[T]
Trigger SceneTransitionTrigger
}
// A SceneDirector is a struct that manages the transitions between scenes
type SceneDirector[T any] struct {
SceneManager[T]
RuleSet map[Scene[T]][]Directive[T]
}
func NewSceneDirector[T any](scene Scene[T], state T, RuleSet map[Scene[T]][]Directive[T]) *SceneDirector[T] {
s := &SceneDirector[T]{RuleSet: RuleSet}
s.current = scene
scene.Load(state, s)
return s
}
// ProcessTrigger finds if a transition should be triggered
func (d *SceneDirector[T]) ProcessTrigger(trigger SceneTransitionTrigger) {
if prevTransition, ok := d.current.(SceneTransition[T]); ok {
// previous transition is still running, end it to process trigger
prevTransition.End()
}
for _, directive := range d.RuleSet[d.current.(Scene[T])] {
if directive.Trigger == trigger {
if directive.Transition != nil {
// With transition
// Equivalent to SwitchWithTransition
sc := d.current.(Scene[T])
directive.Transition.Start(sc, directive.Dest, d)
if c, ok := sc.(TransitionAwareScene[T]); ok {
directive.Dest.Load(c.PreTransition(directive.Dest), d)
} else {
directive.Dest.Load(sc.Unload(), d)
}
d.current = directive.Transition
} else {
// No transition
// Equivalent to SwitchTo
if c, ok := d.current.(Scene[T]); ok {
directive.Dest.Load(c.Unload(), d)
d.current = directive.Dest
}
}
}
}
}
func (d *SceneDirector[T]) ReturnFromTransition(scene, origin Scene[T]) {
if c, ok := scene.(TransitionAwareScene[T]); ok {
c.PostTransition(origin.Unload(), origin)
} else {
scene.Load(origin.Unload(), d)
}
d.current = scene
}