~ubuntu-branches/ubuntu/trusty/libavg/trusty-proposed

« back to all changes in this revision

Viewing changes to src/player/Contact.cpp

  • Committer: Package Import Robot
  • Author(s): OXullo Intersecans
  • Date: 2011-12-06 22:44:56 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20111206224456-qc7250z3ya1vi8s9
Tags: 1.7.0-0ubuntu1
* New upstream release (LP: #899183)
* Remove patches 0002-libav-0.7.patch, 0003-fglrx-segfault-on-startup.patch
  now merged to upstream
* Remove unnecessary .la files
* Update debian/watch file
* Fix debian/copyright dep-5 compliancy
* Update standards to version 3.9.2
* Add man pages for avg_checktouch, avg_checkvsync, avg_showsvg
* Minor debian/rules enhancement
* Add librsvg2-dev, libgdk-pixbuf2.0-dev to Build-Depends
* Proper transition to dh_python2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
//  libavg - Media Playback Engine. 
 
3
//  Copyright (C) 2003-2011 Ulrich von Zadow
 
4
//
 
5
//  This library is free software; you can redistribute it and/or
 
6
//  modify it under the terms of the GNU Lesser General Public
 
7
//  License as published by the Free Software Foundation; either
 
8
//  version 2 of the License, or (at your option) any later version.
 
9
//
 
10
//  This library is distributed in the hope that it will be useful,
 
11
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
//  Lesser General Public License for more details.
 
14
//
 
15
//  You should have received a copy of the GNU Lesser General Public
 
16
//  License along with this library; if not, write to the Free Software
 
17
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
//
 
19
//  Current versions can be found at www.libavg.de
 
20
//
 
21
 
 
22
#include "Contact.h"
 
23
 
 
24
#include "CursorEvent.h"
 
25
#include "BoostPython.h"
 
26
#include "Player.h"
 
27
 
 
28
#include "../base/Exception.h"
 
29
#include "../base/StringHelper.h"
 
30
 
 
31
#include <iostream>
 
32
 
 
33
using namespace std;
 
34
 
 
35
namespace avg {
 
36
 
 
37
int Contact::s_LastListenerID = 0;
 
38
 
 
39
Contact::Contact(CursorEventPtr pEvent)
 
40
    : m_bSendingEvents(false),
 
41
      m_bCurListenerIsDead(false),
 
42
      m_CursorID(pEvent->getCursorID()),
 
43
      m_DistanceTravelled(0)
 
44
{
 
45
    m_Events.push_back(pEvent);
 
46
}
 
47
 
 
48
Contact::~Contact()
 
49
{
 
50
}
 
51
 
 
52
int Contact::connectListener(PyObject* pMotionCallback, PyObject* pUpCallback)
 
53
{
 
54
    s_LastListenerID++;
 
55
    pair<int, Listener> val = 
 
56
            pair<int, Listener>(s_LastListenerID, Listener(pMotionCallback, pUpCallback));
 
57
    m_ListenerMap.insert(val);
 
58
    return s_LastListenerID;
 
59
}
 
60
 
 
61
void Contact::disconnectListener(int id)
 
62
{
 
63
    map<int, Listener>::iterator it = m_ListenerMap.find(id);
 
64
    if (it == m_ListenerMap.end() || (m_CurListenerID == id && m_bCurListenerIsDead)) {
 
65
        throw Exception(AVG_ERR_INVALID_ARGS, 
 
66
                "Contact.disconnectListener: id " + toString(id) + " is not connected.");
 
67
    }
 
68
    if (m_bSendingEvents && m_CurListenerID == id) {
 
69
        m_bCurListenerIsDead = true;
 
70
    } else {
 
71
        m_ListenerMap.erase(it);
 
72
    }
 
73
}
 
74
 
 
75
long long Contact::getAge() const
 
76
{
 
77
    return m_Events.back()->getWhen() - m_Events[0]->getWhen();
 
78
}
 
79
 
 
80
double Contact::getDistanceFromStart() const
 
81
{
 
82
    return getMotionVec().getNorm();
 
83
}
 
84
 
 
85
double Contact::getMotionAngle() const
 
86
{
 
87
    DPoint motion = getMotionVec();
 
88
    if (motion == DPoint(0,0)) {
 
89
        return 0;
 
90
    } else {
 
91
        return motion.getAngle();
 
92
    }
 
93
}
 
94
 
 
95
DPoint Contact::getMotionVec() const
 
96
{
 
97
    return m_Events.back()->getPos() - m_Events[0]->getPos();
 
98
}
 
99
 
 
100
double Contact::getDistanceTravelled() const
 
101
{
 
102
    return m_DistanceTravelled;
 
103
}
 
104
 
 
105
vector<CursorEventPtr> Contact::getEvents() const
 
106
{
 
107
    return m_Events;
 
108
}
 
109
 
 
110
void Contact::addEvent(CursorEventPtr pEvent)
 
111
{
 
112
    pEvent->setCursorID(m_CursorID);
 
113
    pEvent->setContact(shared_from_this());
 
114
    calcSpeed(pEvent, m_Events.back());
 
115
    updateDistanceTravelled(m_Events.back(), pEvent);
 
116
    m_Events.back()->removeBlob();
 
117
    m_Events.push_back(pEvent);
 
118
}
 
119
 
 
120
bool Contact::hasListeners() const
 
121
{
 
122
    return !(m_ListenerMap.empty() || 
 
123
            (m_ListenerMap.size() == 1 && m_bCurListenerIsDead));
 
124
}
 
125
 
 
126
void Contact::sendEventToListeners(CursorEventPtr pCursorEvent)
 
127
{
 
128
    m_bSendingEvents = true;
 
129
    AVG_ASSERT(pCursorEvent->getContact() == shared_from_this());
 
130
    EventPtr pEvent = boost::dynamic_pointer_cast<Event>(pCursorEvent);
 
131
    m_bCurListenerIsDead = false;
 
132
    for (map<int, Listener>::iterator it = m_ListenerMap.begin(); 
 
133
            it != m_ListenerMap.end();)
 
134
    {
 
135
        Listener listener = it->second;
 
136
        m_CurListenerID = it->first;
 
137
        m_bCurListenerIsDead = false;
 
138
        switch (pCursorEvent->getType()) {
 
139
            case Event::CURSORMOTION:
 
140
                if (listener.m_pMotionCallback != Py_None) {
 
141
                    boost::python::call<void>(listener.m_pMotionCallback, pEvent);
 
142
                }
 
143
                break;
 
144
            case Event::CURSORUP:
 
145
                if (listener.m_pUpCallback != Py_None) {
 
146
                    boost::python::call<void>(listener.m_pUpCallback, pEvent);
 
147
                }
 
148
                break;
 
149
            default:
 
150
                AVG_ASSERT(false);
 
151
        }
 
152
        map<int, Listener>::iterator lastIt = it;
 
153
        ++it;
 
154
        if (m_bCurListenerIsDead) {
 
155
            m_ListenerMap.erase(lastIt);
 
156
            m_bCurListenerIsDead = false;
 
157
        }
 
158
    }
 
159
    m_bSendingEvents = false;
 
160
}
 
161
 
 
162
int Contact::getID() const
 
163
{
 
164
    return m_CursorID;
 
165
}
 
166
 
 
167
bool Contact::operator ==(const Contact& other) const
 
168
{
 
169
    return (shared_from_this() == other.shared_from_this());
 
170
}
 
171
 
 
172
bool Contact::operator !=(const Contact& other) const
 
173
{
 
174
    return (shared_from_this() != other.shared_from_this());
 
175
}
 
176
 
 
177
long Contact::getHash() const
 
178
{
 
179
    return long(&*shared_from_this());
 
180
    
 
181
}
 
182
 
 
183
void Contact::calcSpeed(CursorEventPtr pEvent, CursorEventPtr pOldEvent)
 
184
{
 
185
    if (pEvent->getSpeed() == DPoint(0,0)) {
 
186
        DPoint posDiff = pEvent->getPos() - pOldEvent->getPos();
 
187
        long long timeDiff = pEvent->getWhen() - pOldEvent->getWhen();
 
188
        if (timeDiff != 0) {
 
189
            pEvent->setSpeed(posDiff/double(timeDiff));
 
190
        }
 
191
    }
 
192
}
 
193
 
 
194
void Contact::updateDistanceTravelled(CursorEventPtr pEvent1, CursorEventPtr pEvent2)
 
195
{
 
196
    double dist = (pEvent2->getPos() - pEvent1->getPos()).getNorm();
 
197
    m_DistanceTravelled += dist;
 
198
}
 
199
 
 
200
void Contact::dumpListeners(string sFuncName)
 
201
{
 
202
    cerr << "  " << sFuncName << ": ";
 
203
    for (map<int, Listener>::iterator it = m_ListenerMap.begin(); 
 
204
            it != m_ListenerMap.end(); ++it)
 
205
    {
 
206
        cerr << it->first << ", ";
 
207
    }
 
208
    cerr << endl;
 
209
}
 
210
 
 
211
Contact::Listener::Listener(PyObject * pMotionCallback, PyObject * pUpCallback)
 
212
{
 
213
    Py_INCREF(pMotionCallback);
 
214
    m_pMotionCallback = pMotionCallback;
 
215
    Py_INCREF(pUpCallback);
 
216
    m_pUpCallback = pUpCallback;
 
217
}
 
218
 
 
219
Contact::Listener::Listener(const Listener& other)
 
220
{
 
221
    Py_INCREF(other.m_pMotionCallback);
 
222
    m_pMotionCallback = other.m_pMotionCallback;
 
223
    Py_INCREF(other.m_pUpCallback);
 
224
    m_pUpCallback = other.m_pUpCallback;
 
225
}
 
226
 
 
227
Contact::Listener::~Listener()
 
228
{
 
229
    Py_DECREF(m_pMotionCallback);
 
230
    Py_DECREF(m_pUpCallback);
 
231
}
 
232
 
 
233
 
 
234
}
 
235