forked from gucio321/giu-animations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.go
136 lines (111 loc) · 3.01 KB
/
move.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
package animations
import (
"github.com/AllenDang/giu"
"github.com/AllenDang/imgui-go"
"log"
)
var _ giu.Disposable = &moveAnimationState{}
type moveAnimationState struct {
state bool
startPos imgui.Vec2
}
// Dispose implements giu.Disposable
func (m *moveAnimationState) Dispose() {
// noop
}
func (m *MoveAnimation) getState() *moveAnimationState {
if s := giu.Context.GetState(m.id); s != nil {
state, ok := s.(*moveAnimationState)
if !ok {
log.Panicf("error asserting type of move animation state: got %T, wanted *moveAnimationState", s)
}
return state
}
giu.Context.SetState(m.id, m.newState())
return m.getState()
}
func (m *MoveAnimation) newState() *moveAnimationState {
return &moveAnimationState{}
}
var _ Animation = &MoveAnimation{}
// MoveAnimation moves a widget from start position to destination.
// You can also specify a Bézier curve's points.
type MoveAnimation struct {
id string
widget func(starter StarterFunc) giu.Widget
posDelta imgui.Vec2
useBezier bool
bezier []imgui.Vec2
}
// Move creates new *MoveAnimations
// StartPos will be current cursor position
func Move(w func(starter StarterFunc) giu.Widget, posDelta imgui.Vec2) *MoveAnimation {
return &MoveAnimation{
id: giu.GenAutoID("MoveAnimation"),
widget: w,
posDelta: posDelta,
}
}
// StartPos allows to specify custom StartPos (item will be moved there immediately.
func (m *MoveAnimation) StartPos(startPos imgui.Vec2) *MoveAnimation {
m.getState().startPos = startPos
return m
}
// Bezier allows to specify Bezier Curve points.
func (m *MoveAnimation) Bezier(points ...imgui.Vec2) *MoveAnimation {
m.useBezier = true
m.bezier = points
return m
}
// Init implements Animation
func (m *MoveAnimation) Init() {
m.getState().startPos = imgui.CursorPos()
}
// Reset implements Animation
func (m *MoveAnimation) Reset() {
state := m.getState()
state.state = !state.state
}
// BuildNormal implements Animation
func (m *MoveAnimation) BuildNormal(starter StarterFunc) {
state := m.getState()
if state.state {
p := imgui.Vec2{
X: m.posDelta.X + state.startPos.X,
Y: m.posDelta.Y + state.startPos.Y,
}
imgui.SetCursorPos(p)
} else {
imgui.SetCursorPos(state.startPos)
}
m.widget(starter).Build()
}
// BuildAnimation implements Animation
func (m *MoveAnimation) BuildAnimation(animationPercentage, _ float32, starter StarterFunc) {
state := m.getState()
if !state.state {
animationPercentage = 1 - animationPercentage
}
var pos imgui.Vec2
if m.useBezier {
pts := []imgui.Vec2{state.startPos}
for _, b := range m.bezier {
pts = append(pts, imgui.Vec2{
X: b.X + state.startPos.X,
Y: b.Y + state.startPos.Y,
})
}
pts = append(pts, imgui.Vec2{
X: state.startPos.X + m.posDelta.X,
Y: state.startPos.Y + m.posDelta.Y,
})
pos = bezier(animationPercentage, pts)
} else {
pos = imgui.Vec2{
X: state.startPos.X + m.posDelta.X*animationPercentage,
Y: state.startPos.Y + m.posDelta.Y*animationPercentage,
}
}
imgui.SetCursorScreenPos(pos)
m.widget(starter).Build()
}