]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu/anim/animation.c
more anim math functions, some restructuring.. need to make the animated sliders...
[divverent/nexuiz.git] / data / qcsrc / menu / anim / animation.c
1 #ifdef INTERFACE
2 CLASS(Animation) EXTENDS(Object)
3         METHOD(Animation, setTimeStartEnd, void(entity, float, float))
4         METHOD(Animation, setTimeStartDuration, void(entity, float, float))
5         METHOD(Animation, setValueStartEnd, void(entity, float, float))
6         METHOD(Animation, setValueStartDelta, void(entity, float, float))
7         METHOD(Animation, setObjectSetter, void(entity, entity, void(entity, float)))
8         METHOD(Animation, setMath, void(entity, float(float, float, float, float)))
9         METHOD(Animation, tick, void(entity, float))
10         METHOD(Animation, isStopped, float(entity))
11         METHOD(Animation, stopAnim, void(entity))
12         METHOD(Animation, resumeAnim, void(entity))
13         METHOD(Animation, isFinished, float(entity))
14         METHOD(Animation, finishAnim, void(entity))
15         ATTRIB(Animation, object, entity, NULL)
16         ATTRIB(Animation, setter, void(entity, float), setterDummy)
17         ATTRIB(Animation, math, float(float, float, float, float), animLinear)
18         ATTRIB(Animation, value, float, 0)
19         ATTRIB(Animation, startTime, float, 0)
20         ATTRIB(Animation, duration, float, 0)
21         ATTRIB(Animation, startValue, float, 0)
22         ATTRIB(Animation, delta, float, 0)
23         ATTRIB(Animation, stopped, float, FALSE)
24         ATTRIB(Animation, finished, float, FALSE)
25 ENDCLASS(Animation)
26 entity makeHostedAnimation(entity, void(entity, float), float(float, float, float, float), float, float, float);
27 entity makeAnimation(entity, void(entity, float), float(float, float, float, float), float, float, float);
28 float animLinear(float, float, float, float);
29 float animQuadIn(float, float, float, float);
30 float animQuadOut(float, float, float, float);
31 float animQuadInOut(float, float, float, float);
32 void setterDummy(entity, float);
33 #endif
34
35 #ifdef IMPLEMENTATION
36 entity makeHostedAnimation(entity obj, void(entity, float) setter, float(float, float, float, float) func, float duration, float start, float end)
37 {
38         entity me;
39         me = makeAnimation(obj, setter, func, duration, start, end);
40         anim.addAnim(anim, me);
41         return me;
42 }
43
44 entity makeAnimation(entity obj, void(entity, float) setter, float(float, float, float, float) func, float duration, float start, float end)
45 {
46         entity me;
47         me = spawnAnimation();
48         me.setObjectSetter(me, obj, setter);
49         me.setMath(me, func);
50         me.setTimeStartDuration(me, time, duration);
51         me.setValueStartEnd(me, start, end);
52         return me;
53 }
54
55 void setTimeStartEndAnimation(entity me, float s, float e)
56 {
57         me.startTime = s;
58         me.duration = e - s;
59 }
60
61 void setTimeStartDurationAnimation(entity me, float s, float d)
62 {
63         me.startTime = s;
64         me.duration = d;
65 }
66
67 void setValueStartEndAnimation(entity me, float s, float e)
68 {
69         me.startValue = s;
70         me.delta = e - s;
71 }
72
73 void setValueStartDeltaAnimation(entity me, float s, float d)
74 {
75         me.startValue = s;
76         me.delta = d;
77 }
78
79 void setObjectSetterAnimation(entity me, entity o, void(entity, float) s)
80 {
81         me.object = o;
82         me.setter = s;
83 }
84
85 void setMathAnimation(entity me, float(float, float, float, float) func)
86 {
87         me.math = func;
88 }
89
90 void tickAnimation(entity me, float time)
91 {
92         if (me.isStopped(me) || me.isFinished(me))
93                 return;
94
95         if (time >= (me.startTime + me.duration))
96                 me.finishAnim(me);
97         else
98                 me.value = me.math((time - me.startTime), me.duration, me.startValue, me.delta);
99
100         me.setter(me.object, me.value);
101 }
102
103 float isStoppedAnimation(entity me)
104 {
105         return me.stopped;
106 }
107
108 void stopAnimAnimation(entity me)
109 {
110         me.stopped = TRUE;
111 }
112
113 void resumeAnimAnimation(entity me)
114 {
115         me.stopped = FALSE;
116 }
117
118 float isFinishedAnimation(entity me)
119 {
120         return me.finished;
121 }
122
123 void finishAnimAnimation(entity me)
124 {
125         me.value = me.delta + me.startValue;
126         me.finished = TRUE;
127 }
128
129 float animLinear(float time, float duration, float start, float delta)
130 {
131         return (delta * (time / duration)) + start;
132 }
133
134 float animQuadIn(float time, float duration, float start, float delta)
135 {
136         float frac = time / duration;
137         return (delta * frac * frac) + start;
138 }
139
140 float animQuadOut(float time, float duration, float start, float delta)
141 {
142         float frac = time / duration;
143         return (-delta * frac * (frac - 2)) + start;
144 }
145
146 float animQuadInOut(float time, float duration, float start, float delta)
147 {
148         if (time < (duration / 2))
149         {
150                 return animQuadIn(time, (duration / 2), start, (delta / 2));
151         }
152         else
153         {
154                 return animQuadOut((time - (duration / 2)), (duration / 2), (start + (delta / 2)), (delta / 2));
155         }
156 }
157
158 void setterDummy(entity object, float value)
159 {
160 }
161
162 #endif