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