-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from joelschutz/1.1
1.1 Release
- Loading branch information
Showing
12 changed files
with
631 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
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) { | ||
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, orgin Scene[T]) { | ||
if c, ok := scene.(TransitionAwareScene[T]); ok { | ||
c.PostTransition(orgin.Unload(), orgin) | ||
} else { | ||
scene.Load(orgin.Unload(), d) | ||
} | ||
d.current = scene | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package stagehand | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSceneDirector_NewSceneDirector(t *testing.T) { | ||
mockScene := &MockScene{} | ||
ruleSet := make(map[Scene[int]][]Directive[int]) | ||
|
||
director := NewSceneDirector[int](mockScene, 1, ruleSet) | ||
|
||
assert.NotNil(t, director) | ||
assert.Equal(t, mockScene, director.current) | ||
} | ||
|
||
func TestSceneDirector_ProcessTrigger(t *testing.T) { | ||
mockScene := &MockScene{} | ||
mockScene2 := &MockScene{} | ||
ruleSet := make(map[Scene[int]][]Directive[int]) | ||
|
||
director := NewSceneDirector[int](mockScene, 1, ruleSet) | ||
|
||
rule := Directive[int]{Dest: mockScene2, Trigger: 2} | ||
ruleSet[mockScene] = []Directive[int]{rule} | ||
|
||
// Call the ProcessTrigger method with wrong trigger | ||
director.ProcessTrigger(1) | ||
assert.NotEqual(t, rule.Dest, director.current) | ||
|
||
// Call the ProcessTrigger method with correct trigger | ||
director.ProcessTrigger(2) | ||
assert.Equal(t, rule.Dest, director.current) | ||
} | ||
|
||
func TestSceneDirector_ProcessTriggerWithTransition(t *testing.T) { | ||
mockScene := &MockScene{} | ||
mockTransition := &baseTransitionImplementation{} | ||
ruleSet := make(map[Scene[int]][]Directive[int]) | ||
|
||
director := NewSceneDirector[int](mockScene, 1, ruleSet) | ||
|
||
rule := Directive[int]{Dest: &MockScene{}, Trigger: 2, Transition: mockTransition} | ||
ruleSet[mockScene] = []Directive[int]{rule} | ||
|
||
// Call the ProcessTrigger method with wrong trigger | ||
director.ProcessTrigger(1) | ||
assert.NotEqual(t, rule.Transition, director.current) | ||
|
||
// Call the ProcessTrigger method with correct trigger | ||
director.ProcessTrigger(2) | ||
assert.Equal(t, rule.Transition, director.current) | ||
|
||
rule.Transition.End() | ||
assert.Equal(t, rule.Dest, director.current) | ||
} | ||
|
||
func TestSceneDirector_ProcessTriggerWithTransitionAwareness(t *testing.T) { | ||
mockScene := &MockTransitionAwareScene{} | ||
mockTransition := &baseTransitionImplementation{} | ||
ruleSet := make(map[Scene[int]][]Directive[int]) | ||
|
||
director := NewSceneDirector[int](mockScene, 1, ruleSet) | ||
|
||
rule := Directive[int]{Dest: &MockTransitionAwareScene{}, Trigger: 2, Transition: mockTransition} | ||
ruleSet[mockScene] = []Directive[int]{rule} | ||
|
||
// Call the ProcessTrigger method with wrong trigger | ||
director.ProcessTrigger(1) | ||
assert.NotEqual(t, rule.Transition, director.current) | ||
|
||
// Call the ProcessTrigger method with correct trigger | ||
director.ProcessTrigger(2) | ||
assert.Equal(t, rule.Transition, director.current) | ||
|
||
rule.Transition.End() | ||
assert.Equal(t, rule.Dest, director.current) | ||
} |
Oops, something went wrong.