forked from gucio321/giu-animations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transition.go
86 lines (68 loc) · 1.87 KB
/
transition.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
package animations
import (
"log"
"github.com/AllenDang/giu"
"github.com/AllenDang/imgui-go"
)
type transitionAnimationState struct {
layout int
}
func (s *transitionAnimationState) Dispose() {}
var _ Animation = &TransitionAnimation{}
type TransitionAnimation struct {
id string
renderer1, renderer2 func(starter func())
}
func Transition(renderer1, renderer2 func(starter func())) *TransitionAnimation {
return &TransitionAnimation{
id: giu.GenAutoID("transitionAnimation"),
renderer1: renderer1,
renderer2: renderer2,
}
}
func (t *TransitionAnimation) BuildAnimation(percentage, _ float32, starter StarterFunc) {
state := t.getState()
// it means the current layout is layout1, so increasing percentage
if state.layout == 1 {
percentage = 1 - percentage
}
imgui.PushStyleVarFloat(imgui.StyleVarAlpha, percentage)
t.renderer1(func() { starter(PlayAuto) })
imgui.PopStyleVar()
imgui.PushStyleVarFloat(imgui.StyleVarAlpha, 1-percentage)
t.renderer2(func() { starter(PlayAuto) })
imgui.PopStyleVar()
}
func (t *TransitionAnimation) Reset() {
state := t.getState()
if state.layout == 0 {
state.layout = 1
} else {
state.layout = 0
}
}
func (t *TransitionAnimation) Init() {
// noop
}
func (t *TransitionAnimation) BuildNormal(starter StarterFunc) {
state := t.getState()
if state.layout == 0 {
t.renderer1(func() { starter(PlayAuto) })
} else {
t.renderer2(func() { starter(PlayAuto) })
}
}
func (t *TransitionAnimation) getState() *transitionAnimationState {
if s := giu.Context.GetState(t.id); s != nil {
state, ok := s.(*transitionAnimationState)
if !ok {
log.Panicf("expected state type *transitionAnimationState, got %T", s)
}
return state
}
giu.Context.SetState(t.id, t.newState())
return t.getState()
}
func (t *TransitionAnimation) newState() *transitionAnimationState {
return &transitionAnimationState{}
}