Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
Touch.cpp
1 /*
2  Harmonic Flow Framework
3  Copyright (C) 2011-2018 Andreas Widen <andreas@harmonicflow.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "Touch.h"
22 
23 #include "TouchListener.h"
24 
25 #include <assert.h>
26 #include <algorithm>
27 
28 #include "platform/Environment.h"
29 
30 using namespace HFCore;
31 
32 #include "debug/CrtDbgNew.h"
33 
35 {
36  mId = id;
37 }
38 
40 {
41 }
42 
44 {
45  return mPosition;
46 }
47 
48 void Touch::setPosition(const Vector2 &pos)
49 {
50  mPosition = pos;
51 }
52 
54 {
55  assert(std::find(mListeners.begin(), mListeners.end(),
56  listener) == mListeners.end());
57 
58  mListeners.push_back(listener);
59 }
60 
62 {
63  assert(std::find(mListeners.begin(), mListeners.end(), listener) !=
64  mListeners.end());
65 
66  mListeners.erase(std::find(mListeners.begin(), mListeners.end(), listener));
67 }
68 
70 {
71  envDebugLog("Touch::fireDownEvent(): id: %d, x: %f, y: %f.\r\n",
72  mId, touch.x, touch.y);
73 
74  // remember pos:
75  mPosition.x = touch.x;
76  mPosition.y = touch.y;
77  if (mListeners.size() > 0 && isEnabled())
78  {
79  uint32_hf numListeners = mListeners.size();
80  for (uint32_hf i = 0; i < numListeners; i++)
81  {
82  touch.self = this;
83  mListeners[i]->touchDown(touch);
84  }
85  }
86 }
87 
89 {
90  envDebugLog("Touch::fireUpEvent(): id: %d, x: %f, y: %f.\r\n",
91  mId, touch.x, touch.y);
92 
93  // remember pos:
94  mPosition.x = touch.x;
95  mPosition.y = touch.y;
96  if (mListeners.size() > 0 && isEnabled())
97  {
98  uint32_hf numListeners = mListeners.size();
99  for (uint32_hf i = 0; i < numListeners; i++)
100  {
101  touch.self = this;
102  mListeners[i]->touchUp(touch);
103  }
104  }
105 }
106 
108 {
109  envDebugLog("Touch::fireMoveEvent(): id: %d, x: %f, y: %f.\r\n",
110  mId, touch.x, touch.y);
111 
112  // remember pos:
113  mPosition.x = touch.x;
114  mPosition.y = touch.y;
115  if (mListeners.size() > 0 && isEnabled())
116  {
117  uint32_hf numListeners = mListeners.size();
118  for (uint32_hf i = 0; i < numListeners; i++)
119  {
120  touch.self = this;
121  mListeners[i]->touchMove(touch);
122  }
123  }
124 }
125 
126 void Touch::setConnected(bool connected)
127 {
128  // if there's no change, return:
129  if (connected == isConnected())
130  {
131  return;
132  }
133 
134  // connect the controller:
135  Controller::setConnected(connected);
136 
137  // fire notification:
138  if (connected)
139  {
140  HFCORE_ENV->fireTouchAdded(mId);
141  }
142  else
143  {
144  HFCORE_ENV->fireTouchRemoved(mId);
145  }
146 }
147 
148 void Touch::setEnabled(bool enabled)
149 {
150  Controller::setEnabled(enabled);
151  envDebugLog("-----------> touch %d: enabled=%d\r\n", mId, enabled);
152 }
unsigned int uint32_hf
A type definition for unsigned int.
Definition: HFDataTypes.h:354
fp32_hf x
X coordinate of the vector.
Definition: Vector2.h:284
fp32_hf x
X coordinate, relative to window.
Definition: Touch.h:55
void setPosition(const Vector2 &pos)
Set Touch position.
Definition: Touch.cpp:48
Touch(int32_hf id)
Constructor.
Definition: Touch.cpp:34
TouchListenerVector mListeners
Vector holding TouchListener classes.
Definition: Touch.h:152
int int32_hf
A type definition for int.
Definition: HFDataTypes.h:349
void fireDownEvent(TouchInfo touch)
Called internally when a Touch down event occurs. It calls TouchListener::touchDown().
Definition: Touch.cpp:69
Interface for TouchListener callback methods.
Definition: TouchListener.h:85
void addListener(TouchListener *listener)
Add a new listener class to receive TouchListener callbacks.
Definition: Touch.cpp:53
Interface for TouchListener callback methods.
virtual void setEnabled(bool enabled)
Set controller enabled or disabled.
Definition: Controller.cpp:47
A structure that contains Touch event information.
Definition: Touch.h:50
Touch * self
Pointer to Touch.
Definition: Touch.h:52
Interface for input type Touch.
A two-dimensional floating point vector and associated math functions.
Definition: Vector2.h:42
int32_hf mId
Touch ID.
Definition: Touch.h:162
virtual bool isEnabled()
Check whether controller is enabled.
Definition: Controller.cpp:52
fp32_hf y
Y coordinate, relative to window.
Definition: Touch.h:56
fp32_hf y
Y coordinate of the vector.
Definition: Vector2.h:289
virtual ~Touch()
Destructor.
Definition: Touch.cpp:39
void fireUpEvent(TouchInfo touch)
Called internally when a Touch up event occurs. It calls TouchListener::touchUp().
Definition: Touch.cpp:88
Definition: Actor.h:34
virtual void setEnabled(bool enabled)
Set controller enabled or disabled.
Definition: Touch.cpp:148
virtual void setConnected(bool connected)
Set controller connected or disconnected.
Definition: Touch.cpp:126
Singleton interface for platform specific things.
#define HFCORE_ENV
Get HFCore::Environment instance shortcut macro.
Definition: Environment.h:68
const Vector2 & getPosition()
Get Touch position.
Definition: Touch.cpp:43
virtual bool isConnected()
Check whether controller is connected.
Definition: Controller.cpp:42
void fireMoveEvent(TouchInfo touch)
Called internally when a Touch move/drag event occurs. It calls TouchListener::touchMove().
Definition: Touch.cpp:107
virtual void setConnected(bool connected)
Set controller connected or disconnected.
Definition: Controller.cpp:37
Vector2 mPosition
Vector2 holding touch position.
Definition: Touch.h:157
#define envDebugLog(format, args...)
Shortcut macro for logging to &#39;debug.log&#39;.
Definition: Environment.h:2364
void removeListener(TouchListener *listener)
Remove a listener class from receiving further TouchListener callbacks.
Definition: Touch.cpp:61