summaryrefslogtreecommitdiff
path: root/src/anim/SimpleAnim.cpp
blob: 76ea4ee9c517fc46fe5874298688460d0f9e90a3 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de

#include "SimpleAnim.h"

#include "../base/Exception.h"
#include "../base/MathHelper.h"
#include "../player/Player.h"

using namespace boost;
using namespace boost::python;
using namespace std;

namespace avg {
   
SimpleAnim::SimpleAnim(const object& node, const string& sAttrName, long long duration, 
        const object& startValue, const object& endValue, bool bUseInt, 
        const object& startCallback, const object& stopCallback)
    : AttrAnim(node, sAttrName, startCallback, stopCallback),
      m_Duration(duration),
      m_StartValue(startValue),
      m_EndValue(endValue),
      m_bUseInt(bUseInt)
{
}

SimpleAnim::~SimpleAnim()
{
    if (Player::exists() && isRunning()) {
        setStopped();
    }
}

void SimpleAnim::start(bool bKeepAttr)
{
    AttrAnim::start();
    if (bKeepAttr) {
        m_StartTime = calcStartTime();
    } else {
        m_StartTime = Player::get()->getFrameTime();
    }
    if (m_Duration == 0) {
        setValue(m_EndValue);
        remove();
    } else {
        step();
    }
}

void SimpleAnim::abort()
{
    if (isRunning()) {
        remove();
    }
}

template<class T>
object typedLERP(const object& startValue, const object& endValue, float part)
{
    T start = extract<T>(startValue);
    T end = extract<T>(endValue);
    T cur = start+(end-start)*part;
    return object(cur);
}

bool SimpleAnim::step()
{
    assert(isRunning());
    float t = ((float(Player::get()->getFrameTime())-m_StartTime)
            /m_Duration);
    if (t >= 1.0) {
        setValue(m_EndValue);
        remove();
        return true;
    } else {
        object curValue;
        float part = interpolate(t);
        if (isPythonType<float>(m_StartValue)) {
            curValue = typedLERP<float>(m_StartValue, m_EndValue, part);
            if (m_bUseInt) {
                float d = extract<float>(curValue);
                curValue = object(round(d));
            }
        } else if (isPythonType<glm::vec2>(m_StartValue)) {
            curValue = typedLERP<glm::vec2>(m_StartValue, m_EndValue, part);
            if (m_bUseInt) {
                glm::vec2 pt = extract<glm::vec2>(curValue);
                curValue = object(glm::vec2(round(pt.x), round(pt.y)));
            }
        } else {
            throw (Exception(AVG_ERR_TYPE, 
                    "Animated attributes must be either numbers or Point2D."));
        }
        setValue(curValue);
        return false;
    }
}

long long SimpleAnim::getStartTime() const
{
    return m_StartTime;
}

long long SimpleAnim::getDuration() const
{
    return m_Duration;
}

long long SimpleAnim::calcStartTime()
{
    float part;
    if (isPythonType<float>(m_StartValue)) {
        if (m_EndValue == m_StartValue) {
            part = 0;
        } else {
            part = getStartPart(extract<float>(m_StartValue), 
                    extract<float>(m_EndValue), extract<float>(getValue()));
        }
    } else if (isPythonType<glm::vec2>(m_StartValue)) {
        float start = glm::vec2(extract<glm::vec2>(m_StartValue)()).x;
        float end = glm::vec2(extract<glm::vec2>(m_EndValue)()).x;
        float cur = glm::vec2(extract<glm::vec2>(getValue())()).x;
        if (start == end) {
            start = glm::vec2(extract<glm::vec2>(m_StartValue)()).y;
            end = glm::vec2(extract<glm::vec2>(m_EndValue)()).y;
            cur = glm::vec2(extract<glm::vec2>(getValue())()).y;
        }
        if (start == end) {
            part = 0;
        } else {
            part = getStartPart(start, end, cur);
        }
    } else {
        throw (Exception(AVG_ERR_TYPE, 
                    "Animated attributes must be either numbers or Point2D."));
    }
    return Player::get()->getFrameTime()-(long long)(part*getDuration());
}

float SimpleAnim::getStartPart(float start, float end, float cur)
{
    float tstart = 0;
    float tend = 1;
    bool bDir = (start < end);
    for (int i=0; i<10; ++i) {
        float tmiddle = (tstart+tend)/2;
        float part = interpolate(tmiddle);
        float middle = start+(end-start)*part;
        if ((bDir && middle < cur) || (!bDir && middle >= cur)) {
            tstart = tmiddle;
        } else {
            tend = tmiddle;
        }
    }
    return (tend+tstart)/2;
}

void SimpleAnim::remove() 
{
    AnimPtr tempThis = shared_from_this();
    removeFromMap();
    setStopped();
}

}