summaryrefslogtreecommitdiff
path: root/src/player/TouchEvent.cpp
blob: c2541df908f65a4dd967ad47f1da8ac887ee2bd5 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
//  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
//
//  Original author of this file is igor@c-base.org
//

#include "TouchEvent.h"

#include "Player.h"
#include "AVGNode.h"

#include "../graphics/Bitmap.h"
#include "../graphics/Filterfill.h"
#include "../graphics/Pixel8.h"
#include "../base/Exception.h"

#include "../base/Logger.h"

using namespace std;

namespace avg {

TouchEvent::TouchEvent(int id, Type eventType, BlobPtr pBlob, const IntPoint& pos, 
        Source source, const glm::vec2& speed)
    : CursorEvent(id, eventType, pos, source),
      m_pBlob(pBlob),
      m_bHasHandOrientation(false)
{
    setSpeed(speed);
    if (pBlob) {
        m_Orientation = pBlob->getOrientation();
        m_Area = pBlob->getArea();
        m_Center = pBlob->getCenter();
        m_Eccentricity = pBlob->getEccentricity();
        const glm::vec2& axis0 = m_pBlob->getScaledBasis(0);
        const glm::vec2& axis1 = m_pBlob->getScaledBasis(1);
        if (glm::length(axis0) > glm::length(axis1)) {
            m_MajorAxis = axis0;
            m_MinorAxis = axis1;
        } else {
            m_MajorAxis = axis1;
            m_MinorAxis = axis0;
        }
    } else {
        m_Orientation = 0;
        m_Area = 20;
        m_Center = glm::vec2(0, 0);
        m_Eccentricity = 0;
        m_MajorAxis = glm::vec2(5, 0);
        m_MinorAxis = glm::vec2(0, 5);
    }
}

TouchEvent::TouchEvent(int id, Type eventType, const IntPoint& pos, Source source, 
        const glm::vec2& speed, float orientation, float area, float eccentricity, 
        glm::vec2 majorAxis, glm::vec2 minorAxis)
    : CursorEvent(id, eventType, pos, source),
      m_Orientation(orientation),
      m_Area(area),
      m_Eccentricity(eccentricity),
      m_MajorAxis(majorAxis),
      m_MinorAxis(minorAxis)
{
    setSpeed(speed);
}

TouchEvent::TouchEvent(int id, Type eventType, const IntPoint& pos, Source source,
        const glm::vec2& speed)
    : CursorEvent(id, eventType, pos, source),
      m_Orientation(0),
      m_Area(20),
      m_Eccentricity(0),
      m_MajorAxis(5, 0),
      m_MinorAxis(0, 5)
{
    setSpeed(speed);
}

TouchEvent::~TouchEvent()
{
}

CursorEventPtr TouchEvent::cloneAs(Type eventType) const
{
    TouchEventPtr pClone(new TouchEvent(*this));
    pClone->m_Type = eventType;
    return pClone;
}

float TouchEvent::getOrientation() const 
{
    return m_Orientation;
}

float TouchEvent::getArea() const 
{
    return m_Area;
}

const glm::vec2 & TouchEvent::getCenter() const 
{
    return m_Center;
}

float TouchEvent::getEccentricity() const 
{
    return m_Eccentricity;
}

const glm::vec2 & TouchEvent::getMajorAxis() const
{
    return m_MajorAxis;
}

const glm::vec2 & TouchEvent::getMinorAxis() const
{
    return m_MinorAxis;
}

const BlobPtr TouchEvent::getBlob() const
{
    return m_pBlob;
}

ContourSeq TouchEvent::getContour()
{
    if (m_pBlob) {
        return m_pBlob->getContour();
    } else {
        throw Exception(AVG_ERR_UNSUPPORTED, 
                "TouchEvent::getContour: No contour available.");
    }
}

float TouchEvent::getHandOrientation() const
{
    if (getSource() == Event::TOUCH) {
        if (m_bHasHandOrientation) {
            return m_HandOrientation;
        } else {
            glm::vec2 screenCenter = Player::get()->getRootNode()->getSize()/2.f;
            return getAngle(getPos()-screenCenter);
        }
    } else {
        throw Exception(AVG_ERR_UNSUPPORTED,
                "TouchEvent::getHandOrientation: Only supported for touch events.");
    }
}

void TouchEvent::addRelatedEvent(TouchEventPtr pEvent)
{
    m_RelatedEvents.push_back(pEvent);
    if (getSource() == Event::TOUCH && m_RelatedEvents.size() == 1) {
        TouchEventPtr pHandEvent = m_RelatedEvents.begin()->lock();
        m_HandOrientation = getAngle(pHandEvent->getPos()-getPos());
        m_bHasHandOrientation = true;
    }
}

vector<TouchEventPtr> TouchEvent::getRelatedEvents() const
{
    vector<TouchEventPtr> pRelatedEvents;
    vector<TouchEventWeakPtr>::const_iterator it;
    for (it = m_RelatedEvents.begin(); it != m_RelatedEvents.end(); ++it) {
        pRelatedEvents.push_back((*it).lock());
    }
    return pRelatedEvents;
}

void TouchEvent::removeBlob()
{
    m_pBlob = BlobPtr();
}

void TouchEvent::trace()
{
    CursorEvent::trace();
    AVG_TRACE(Logger::category::EVENTS,Logger::severity::DEBUG, "pos: " << getPos() 
            << ", ID: " << getCursorID()
            << ", Area: " << m_Area
            << ", Eccentricity: " << m_Eccentricity);
}
      
}