1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
class ITAInterpoledValue {
public:
enum animation_types {linear, quad_in, quad_out};
protected:
float startValue;
float endValue;
int duration;
int animationType;
unsigned long long start_time;
float time_linear(float time) {
return time;
}
float time_quadraticIn(float time) {
return time * time;
}
float time_quadraticOut(float time) {
return time * (2 - time);
}
public:
virtual void setup(int duration, float start, float final, int atype) = 0;
virtual void setup(int duration, float final, int atype) = 0;
virtual bool update() = 0;
};
template <class T>
class TAInterpoledValue : public ITAInterpoledValue {
T &v;
public:
TAInterpoledValue(T & value) : v(value)
{
}
void setToValue(float va) {
v = va;
}
void setToEndValue() {
setToValue(endValue);
}
float getValue() {
return v;
}
void setup(int duration, float final, int atype) {
this->start_time = ofGetElapsedTimeMillis();
this->duration = duration * 1000;
this->startValue = getValue();
this->endValue = final;
this->animationType = atype;
}
void setup(int duration, float start, float final, int atype) {
this->startValue = start;
setup(duration, final, atype);
}
bool update() {
unsigned long long elapsed = ofGetElapsedTimeMillis() - start_time;
if (elapsed >= duration) {
setToEndValue();
return true;
}
else {
float time = (float)elapsed / (float)this->duration;
float time_computed;
switch (this->animationType) {
case linear:
default:
time_computed = time_linear(time);
case quad_in:
time_computed = time_quadraticIn(time);
case quad_out:
time_computed = time_quadraticOut(time);
}
float v = this->startValue + (this->endValue - this->startValue) * time_computed;
cout << "v:" << v << " time:" << elapsed << " start:" << startValue << " end:" << endValue << " duration:" << duration << endl;
setToValue(v);
return false;
}
}
};
class TAInterpolator
{
vector <ITAInterpoledValue *> inter_values;
public:
template <typename T2>
void interpolate(T2 &value, int duration, float final, int atype = 0) {
TAInterpoledValue <T2> *v = new TAInterpoledValue <T2>(value);
v->setup(duration, final, atype);
inter_values.push_back(v);
}
template <typename T2>
void interpolate(T2 &value, int duration, float start, float final, int atype = 0) {
TAInterpoledValue <T2> *v = new TAInterpoledValue <T2>(value);
v->setup(duration, start, final, atype);
inter_values.push_back(v);
}
void update() {
for (int i = 0; i < inter_values.size(); i++) {
if (inter_values[i]->update()) {
delete inter_values[i];
inter_values.erase(inter_values.begin() + i);
}
}
}
}; |