summaryrefslogtreecommitdiff
path: root/src/anim/StateAnim.cpp
blob: 4858c2369edbb8fc53761a29c3ba4ea5d7f44d9c (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
//
//  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 "StateAnim.h"

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

using namespace boost::python;
using namespace std;

namespace avg {

AnimState::AnimState(const string& sName, AnimPtr pAnim, const string& sNextName)
    : m_sName(sName),
      m_pAnim(pAnim),
      m_sNextName(sNextName)
{
}

AnimState::AnimState()
{
}

StateAnim::StateAnim(const vector<AnimState>& states)
    : Anim(object(), object()),
      m_bDebug(false)
{
    vector<AnimState>::const_iterator it;
    for (it=states.begin(); it != states.end(); ++it) {
        m_States[(*it).m_sName] = *it;
        it->m_pAnim->setHasParent();
    }
}

StateAnim::~StateAnim()
{
    setState("");
}

void StateAnim::abort()
{
    setState("");
}

void StateAnim::setState(const std::string& sName, bool bKeepAttr)
{
    if (m_sCurStateName == sName) {
        return;
    }
    if (!m_sCurStateName.empty()) {
        m_States[m_sCurStateName].m_pAnim->abort();
    }
    switchToNewState(sName, bKeepAttr);
}

const std::string& StateAnim::getState() const
{
    return m_sCurStateName;
}

void StateAnim::setDebug(bool bDebug)
{
    m_bDebug = bDebug;
}
    
bool StateAnim::step()
{
    // Make sure the object isn't deleted until the end of the method.
    AnimPtr tempThis = shared_from_this();  

    if (!m_sCurStateName.empty()) {
        const AnimState& curState = m_States[m_sCurStateName];
        bool bDone = curState.m_pAnim->step();
        if (bDone) {
            switchToNewState(curState.m_sNextName, false);
        }
    }
    return false;
}

void StateAnim::switchToNewState(const string& sName, bool bKeepAttr)
{
    if (m_bDebug) {
        cerr << this << " State change: '" << m_sCurStateName << "' --> '" << sName 
                << "'" << endl;
    }
    string sOldStateName = m_sCurStateName;
    m_sCurStateName = sName;
    if (!sName.empty()) {
        map<string, AnimState>::iterator it = m_States.find(sName);
        if (it == m_States.end()) {
            throw Exception(AVG_ERR_INVALID_ARGS, "StateAnim: State "+sName+" unknown.");
        } else {
            it->second.m_pAnim->start(bKeepAttr);
        }
        if (sOldStateName == "") {
            Anim::start(false);
        }
    } else {
        Anim::setStopped();
    }
}

}