6
6
This is a module for [ giu] ( https://github.com/AllenDang/giu ) providing an
7
7
animation system.
8
8
9
- ![ giu-animations demo] ( ./docs/demo.gif )
10
-
11
9
# Documentation
12
10
13
11
## How to use?
14
12
15
- For complete code, please check out [ ` _examples ` ] ( ./_examples )
13
+ For complete code, please check out [ examples ] ( ./_examples )
16
14
17
15
### important note
18
16
19
- Please make shure that you're using the same version of giu
17
+ Please make sure that you're using the same version of giu
20
18
as this project (technically, you need to use giu version
21
19
that uses the same imgui-go version as yours)
22
20
23
21
### Defining an animation
24
22
25
- At the moment, there are two implementations of animations:
23
+ At the moment, there are three implementations of animations:
24
+
26
25
- [ Transition] ( #transition ) - a smooth transition between two windows/sets of windows e.t.c.
27
26
** NOTE** applying this animation to single widgets is not implemented yet and may
28
27
not work as expected.
29
- - [ on-hover color change] ( #hover-color ) - you can apply this animation to any ** single widget**
30
- that ` imgui.IsItemHovered ` applies for.
31
- You can specify any style color you want.
32
- - [ Movement] ( #move ) - moves DrawCursor emulating moving an object (` giu.Widget ` ).
28
+ - [ Color Flow] ( #color-flow ) - you can apply this animation to any widget
29
+ You can configure this animation to make your button hover smoother or change it into a rainbow!
30
+ - [ Movement] ( #move ) - moves DrawCursor emulating moving an object (aka ` giu.Widget ` ).
33
31
34
32
Lets shortly discuss particular types of animations:
35
33
36
- #### transition
34
+ #### Transition
37
35
38
36
Lets look at the API:
37
+
39
38
``` go
40
- func Transition (renderer1, renderer2 func (starter func ()) *TransitionAnimation {...}
39
+ func Transition (renderers ... func (starter func (mode PlayMode ) )) *TransitionAnimation {...}
41
40
```
42
41
43
- ` renderer1 ` and ` renderer2 ` are just two stages of trasition.
44
- In stage 1, ` renderer1 ` is called, and in stage 2 - ` renderer2 ` .
45
- When animation is being plaid, both renderers are called and suitable
46
- alpha value is pushed to imgui style for both of them.
47
-
48
- The argument to the poth renderers is a pointer to Animator.Start (see later)
42
+ ` renderers ` are just [ key frames] ( key-frame ) of trasition.
43
+ In each stage appropiate renderer is called.
44
+ The argument to the renderers is a pointer to Animator.Start (see later)
49
45
so that you can call it to play the animation.
50
46
51
- #### Hover color
47
+ #### Color flow
52
48
53
49
``` go
54
- func HoverColor (
50
+ func ColorFlow (
55
51
widget giu.Widget ,
56
- hoverColor, normalColor func () color. RGBA ,
57
- hoverID, normalID giu. StyleColorID ,
58
- ) *HoverColorAnimation {...}
52
+ applying []giu. StyleColorID ,
53
+ colors ... func () color. RGBA ,
54
+ ) *ColorFlowAnimation {...}
59
55
```
60
56
61
- - The first argument is a widget that should apply to
62
- - hoverColor and normalColor are functions that returns hover and standard (non-hover) colors
63
- - hoverID, normalID - style IDs that hovering applies to .
57
+ - The first argument is a ** function ** producing a ** widget** that the animation should apply to
58
+ - next is the list of style-color identifiers. Color changes applies to all of them.
59
+ - and the last list of arguments are [ key frames ] ( #key-frame ) of color flow .
64
60
65
- There is also a variant of the above method called ` HoverColorStyle ` , which does not need
66
- ` hoverColor ` and ` normalColor ` arguments . These colors are obtained
61
+ There is also a variant of the above method called ` ColorFlowStyle ` , which does not need
62
+ colors list . These colors are obtained
67
63
by function like this:
64
+
68
65
``` go
69
66
func () color .RGBA {
70
67
return imgui.CurrentStyle ().GetStyleColor (styleID)
71
68
}
72
69
```
73
70
74
- #### Move
71
+ #### Move
75
72
76
73
``` go
77
- Move (
78
- w giu.Widget ,
79
- delta imgui.Vec2 ,
80
- ) *MoveAnimation {...}
74
+ func Move (w func (starter StarterFunc ) giu.Widget , steps ...*MoveStep) *MoveAnimation {...}
81
75
```
82
76
83
- This will mmove ` w ` from the position, it was
84
- at the moment of calling ` Move(...) ` (called ` start ` )
85
- to ` start ` + ` delta ` .
86
-
87
- ##### Easing
77
+ This will move ` w ` around the steps.
78
+ Lets take a closer look on steps now:
79
+
80
+ - You create a step with ` Step ` or ` StepVec ` methods.
81
+ - You have two options of specifying position:
82
+ - you can make it relative to the previous step. This way the system will
83
+ take position and add it to the position of the previous step
84
+ (and do it until it reaches first step or any step with absolute position)
85
+ - After calling ` Absolute() ` method of the MoveStep, its position becomes
86
+ absolute so that it does not rely on any previous step.
87
+ - An additional feature of Steps is a Bezier Curve implementation.
88
+ In order to enable it, simply call ` Bezier ` method and specify as meny points as you wish.
89
+
90
+ One more important thing to mention is the first step.
91
+ By default, position of the first step you specify ** will be treated
92
+ absolute, even thouth it wasn't set to be.** To change this
93
+ there are two additional methods of ` MoveAnimation ` .
94
+
95
+ - the first one is called ` StartPos ` and takes one argument of the following type:
96
+ ` func(startPos imgui.Vec2) *MoveStep ` . It is expected to return non-nil MoveStep.
97
+ ` startPos ` argument is the position of drawing cursor at the moment ** of first call** of
98
+ ` Animator ` .
99
+ - another method is tu simply call ` DefaultStartPos ` method. It takes no arguments and acts
100
+ like most users would like to use ` StartPos ` - it returns ` Step(startPos) ` .
101
+
102
+ ### Easing
88
103
89
104
There are some extra ways of playing animation flow:
90
105
@@ -108,24 +123,14 @@ const (
108
123
109
124
for further reference, see https://easings.net
110
125
111
- ##### Bezier curve
126
+ ### Note about StarterFunc
112
127
113
- Move animation supports [ Bezier Curve] ( https://pomax.github.io/bezierinfo/ ) .
114
- It meas that move animation could be very smooth if you find it necessary.
115
- You have two ways to calculate these points:
116
- - go through [ google] ( https://google.com ) and use tone of paper to understand this strange math or
117
- - just type random values and check what happens :smile :
128
+ This interface holds a reference to the part of ` AnimatorWidget ` responsible
129
+ for starting animations. At the moment, there are three functions
118
130
119
- The api looks as follows:
120
-
121
- ``` go
122
- func (m *MoveAnimation ) Bezier (controlPoints ...imgui .Vec2 ) *MoveAnimation {...}
123
- ```
124
-
125
- you can add as many control points as you which to.
126
- Each point will make the curve stranger.
127
- The only thing you need to remember is, that ** these points
128
- are relative to ` startPos ` ** . They will automatically become ` startPos ` + ` controlPoint ` .
131
+ - Start(PlayMode) go to the next KeyFrame (forwards or backwards)
132
+ - StartKF(base, destiny KeyFrame, mode PlayMode) go from ` base ` to ` destiny ` in ` mode ` direction (frame by frame)
133
+ - StartWhole(mode) - play from 0 to last Key Frame
129
134
130
135
### Using animator
131
136
@@ -138,24 +143,56 @@ animator's api is designed so that you don't need to do so every time.
138
143
As an argument to ` Animator(...) ` constuctor, you pass perviously created animation.
139
144
140
145
Animator has some useful methods:
146
+
141
147
- ` Duration ` allows you to specify animation's duration (default is 0.25 s)
142
148
- ` FPS ` sets Frames per second value for animation playback (default is 60)
143
- ** NOTE** it is not real application's FPS! It just describes how often
144
- animation's status is updated.
149
+ ** NOTE** it is not real application's FPS! It just describes how often
150
+ animation's status is updated.
145
151
- ` Start ` - this method you can use to invoke animation play.
146
152
- ` IsRunning ` returns true, if animation is being plaid right now.
147
153
154
+ #### ID
155
+
156
+ ` AnimatorWidget ` has a special ID method that allows you to specify
157
+ your own giu-ID. This ID is used to store an internal aniamtor's state
158
+ inside of giu context.
159
+ Using this method is extremely important if you want to avoid confusing panics
160
+ when using TransitionAnimation along with sub-animators inide that animation.
161
+ It may happen, that one animator receives the same ID as the previous one.
162
+ This may lead to unexpected behaviour or even panic! Its good practice to set
163
+ unique ID everywhere!
164
+
165
+ ### Auto triggering
166
+
167
+ Animator provides a simple way of automatical animations start.
168
+ You can do this by using ` Trigger ` method. This method takes three arguments:
169
+
170
+ - ` TriggerType ` (TriggerNever, TriggerOnChange, TriggerOnTrue) tells Animator when
171
+ to start the animation.
172
+ - play mode - tells how to start it
173
+ - and the last argument is a trigger function ` func() bool ` (** TIP** you can use any of ` imgui ` functions like ` imgui.IsItemHovered ` )
174
+
175
+ ### Key Frame
176
+
177
+ Key frames are states of animation with a specified animation states.
178
+ All other states between them are calculated on the go.
179
+ Key frames system in this module is not much experienced, but it should
180
+ suit needs of most users. For more information about implementation
181
+ of this system in particular animation types, see above.
182
+
148
183
## Creating your own animation
149
184
150
185
You can use this API to create your own animation.
151
186
To do soo, lets take a look on ` Animation ` interface.
187
+
152
188
``` go
153
189
type Animation interface {
154
190
Init ()
155
191
Reset ()
192
+ KeyFramesCount () int
156
193
157
- BuildNormal (starter func ())
158
- BuildAnimation (animationPercentage float32 , starter func ())
194
+ BuildNormal (currentKeyFrame KeyFrame , starter func ())
195
+ BuildAnimation (animationPercentage, animationPurePercentage float32 , startKeyFrame, destinationKeyFrame KeyFrame , starter func ())
159
196
}
160
197
```
161
198
@@ -170,6 +207,11 @@ you can put some initialization here.
170
207
171
208
Reset is called along with ` (*Animator).Start `
172
209
210
+ ### KeyFramesCount
211
+
212
+ Returns a number of key frames the animation implements.
213
+ This number determines behaviour of Animator while calling Start\*
214
+
173
215
### BuildNormal
174
216
175
217
is called when ` !(*Animator).IsRunning() `
@@ -186,7 +228,7 @@ You can do some calculations there.
186
228
# Contribution
187
229
188
230
If you implement something interessting, find any bugs, or
189
- improvements and would be so kind to open a PR,
231
+ improvements and if you would be so kind to open a PR,
190
232
your contribution is welcome!
191
233
192
234
# Motivation
@@ -196,4 +238,4 @@ But (as I'm an author of that system) I've decided to share it for public - feel
196
238
197
239
# License
198
240
199
- This project is shared under (attached) MIT License.
241
+ This project is shared under (attached) [ MIT License] ( LICENSE ) .
0 commit comments