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