]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/anim/keyframe.qc
0e194bf6b32cf9114b328a2f6eb5f866a55c2a09
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / anim / keyframe.qc
1 #ifndef ANIM_KEYFRAME_H
2 #define ANIM_KEYFRAME_H
3 CLASS(Keyframe, Animation)
4         METHOD(Keyframe, addEasing, entity(entity, float, float, float(float, float, float, float)))
5         METHOD(Keyframe, addAnim, void(entity, entity))
6         METHOD(Keyframe, calcValue, float(entity, float, float, float, float))
7         ATTRIB(Keyframe, currentChild, entity, NULL)
8         ATTRIB(Keyframe, firstChild, entity, NULL)
9         ATTRIB(Keyframe, lastChild, entity, NULL)
10 ENDCLASS(Keyframe)
11 entity makeHostedKeyframe(entity, void(entity, float), float, float, float);
12 entity makeKeyframe(entity, void(entity, float), float, float, float);
13 float getNewChildStart(entity);
14 float getNewChildDuration(entity, float);
15 float getNewChildValue(entity);
16 #endif
17
18 #ifdef IMPLEMENTATION
19 entity makeHostedKeyframe(entity obj, void(entity, float) objSetter, float animDuration, float animStart, float animEnd)
20 {
21         entity me;
22         me = makeKeyframe(obj, objSetter, animDuration, animStart, animEnd);
23         anim.addAnim(anim, me);
24         return me;
25 }
26
27 entity makeKeyframe(entity obj, void(entity, float) objSetter, float animDuration, float animStart, float animEnd)
28 {
29         entity me;
30         me = NEW(Keyframe);
31         me.configureAnimation(me, obj, objSetter, time, animDuration, animStart, animEnd);
32         return me;
33 }
34
35 entity Keyframe_addEasing(entity me, float animDurationTime, float animEnd, float(float, float, float, float) func)
36 {
37         entity other;
38         other = makeEasing(me.object, me.setter, func, getNewChildStart(me), getNewChildDuration(me, animDurationTime), getNewChildValue(me), animEnd);
39         me.addAnim(me, other);
40         return other;
41 }
42
43 float getNewChildStart(entity me)
44 {
45         if (me.lastChild)
46                 return (me.lastChild.startTime + me.lastChild.duration);
47         else
48                 return 0;
49 }
50
51 float getNewChildDuration(entity me, float durationTime)
52 {
53         float dura, maxDura;
54         maxDura = me.duration;
55         if (me.lastChild) maxDura = maxDura - (me.lastChild.startTime + me.lastChild.duration);
56         dura = durationTime;
57         if (0 >= dura || dura > maxDura) dura = maxDura;
58         return dura;
59 }
60
61 float getNewChildValue(entity me)
62 {
63         if (me.lastChild)
64                 return (me.lastChild.startValue + me.lastChild.delta);
65         else
66                 return me.startValue;
67 }
68
69 void Keyframe_addAnim(entity me, entity other)
70 {
71         if(other.parent)
72                 error("Can't add already added anim!");
73
74         if(other.isFinished(other))
75                 error("Can't add finished anim!");
76
77         other.parent = me;
78
79         entity l;
80         l = me.lastChild;
81
82         if(l)
83                 l.nextSibling = other;
84         else
85         {
86                 me.currentChild = other;
87                 me.firstChild = other;
88         }
89
90         other.prevSibling = l;
91         other.nextSibling = NULL;
92         me.lastChild = other;
93 }
94
95 float Keyframe_calcValue(entity me, float tickTime, float animDuration, float animStartValue, float animDelta)
96 {
97         if (me.currentChild)
98                 if (me.currentChild.isFinished(me.currentChild))
99                         me.currentChild = me.currentChild.nextSibling;
100
101         if (me.currentChild)
102         {
103                 me.currentChild.tick(me.currentChild, tickTime);
104                 return me.currentChild.value;
105         }
106
107         return animStartValue + animDelta;
108 }
109 #endif