Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
Mouse.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 "Mouse.h"
22 
23 #include "MouseListener.h"
24 
25 #include <algorithm>
26 #include <assert.h>
27 
28 #include "platform/Environment.h"
29 #include "platform/Graphics.h"
30 #include "platform/Image.h"
31 
32 using namespace HFCore;
33 
34 #include "debug/CrtDbgNew.h"
35 
37 {
38  mIsVisible = true;
39  mId = id;
40  mButtonsEnabled = true;
41  mIsButtonDown[BUTTON_LEFT] = false;
42  mIsButtonDown[BUTTON_RIGHT] = false;
44  mIsInBounds = false;
45 }
46 
48 {
49 }
50 
51 void Mouse::setVisible(bool visible)
52 {
53  mIsVisible = visible;
54 }
55 
57 {
58  return mIsVisible;
59 }
60 
62 {
63  assert(std::find(mListeners.begin(), mListeners.end(), listener) == mListeners.end());
64  mListeners.push_back(listener);
65 }
66 
68 {
69  assert(std::find(mListeners.begin(), mListeners.end(), listener) != mListeners.end());
70  mListeners.erase(std::find(mListeners.begin(), mListeners.end(), listener));
71 }
72 
74 {
75  mPosition.x = mouse.x;
76  mPosition.y = mouse.y;
77  mIsInBounds = true;
78  if (mListeners.size() > 0 && isEnabled())
79  {
80  uint32_hf numListeners = mListeners.size();
81  for (uint32_hf i = 0; i < numListeners; i++)
82  {
83  mouse.self = this;
84  mListeners[i]->mouseMove(mouse);
85  }
86  }
87 }
88 
90 {
91  mIsButtonDown[mouse.button] = true;
92  if (mListeners.size() > 0 && isEnabled() && mButtonsEnabled)
93  {
94  uint32_hf numListeners = mListeners.size();
95  for (uint32_hf i = 0; i < numListeners; i++)
96  {
97  mouse.self = this;
98  mListeners[i]->mouseButtonDown(mouse);
99  }
100  }
101 }
102 
104 {
105  mIsButtonDown[mouse.button] = false;
106  if (mListeners.size() > 0 && isEnabled() && mButtonsEnabled)
107  {
108  uint32_hf numListeners = mListeners.size();
109  for (uint32_hf i = 0; i < numListeners; i++)
110  {
111  mouse.self = this;
112  mListeners[i]->mouseButtonUp(mouse);
113  }
114  }
115 }
116 
118 {
119  if (mListeners.size() > 0 && isEnabled() && mButtonsEnabled)
120  {
121  uint32_hf numListeners = mListeners.size();
122  for (uint32_hf i = 0; i < numListeners; i++)
123  {
124  mouse.self = this;
125  mListeners[i]->mouseWheel(mouse);
126  }
127  }
128 }
129 
131 {
132  setVisible(true);
133 
134  mIsInBounds = true;
135 
136  if (mListeners.size() > 0 && isEnabled())
137  {
138  uint32_hf numListeners = mListeners.size();
139  for (uint32_hf i = 0; i < numListeners; i++)
140  {
141  mouse.self = this;
142  mListeners[i]->mouseEnter(mouse);
143  }
144  }
145 }
146 
148 {
149  setVisible(false);
150 
151  mIsInBounds = false;
152 
153  if (mListeners.size() > 0 && isEnabled())
154  {
155  uint32_hf numListeners = mListeners.size();
156  for (uint32_hf i = 0; i < numListeners; i++)
157  {
158  mouse.self = this;
159  mListeners[i]->mouseLeave(mouse);
160  }
161  }
162 }
163 
165 {
166  return mPosition;
167 }
168 
169 void Mouse::setPosition(const Vector2 &pos)
170 {
171  mPosition = pos;
172 }
173 
175 {
176  Mouse::MouseInfo mouse;
177  mouse.self = this;
178  mouse.windowID = windowID;
179  mouse.x = getPosition().x;
180  mouse.y = getPosition().y;
181  mouse.xrel = 0.0f;
182  mouse.yrel = 0.0f;
184  mouse.clickCount = 0;
185  mouse.wheelDelta = 0;
186 
187  return mouse;
188 }
189 
190 void Mouse::setConnected(bool connected)
191 {
192  // if there's no change, return:
193  if (connected == isConnected())
194  {
195  return;
196  }
197 
198  // connect the controller:
199  Controller::setConnected(connected);
200 
201  // fire notification:
202  if (connected)
203  {
204  HFCORE_ENV->fireMouseAdded(mId);
205  }
206  else
207  {
208  HFCORE_ENV->fireMouseRemoved(mId);
209  }
210 }
211 
212 void Mouse::setEnabled(bool enabled)
213 {
214  Controller::setEnabled(enabled);
215  envDebugLog("-----------> mouse %d: enabled=%d\r\n", mId, enabled);
216 }
217 
219 {
220  return mButtonsEnabled;
221 }
222 
223 void Mouse::setButtonsEnabled(bool enabled)
224 {
225  envDebugLog("-----------> mouse %d: buttons=%d\r\n", mId, enabled);
226  mButtonsEnabled = enabled;
227 }
228 
230 {
231  assert(button == BUTTON_LEFT ||
232  button == BUTTON_RIGHT ||
233  button == BUTTON_MIDDLE);
234 
235  return mIsButtonDown[button];
236 }
237 
239 {
240  return mIsInBounds;
241 }
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
Mouse::MouseInfo getMouseInfoDefaults(uint32_hf windowID)
Get initialized MouseInfo structure.
Definition: Mouse.cpp:174
Interface for all rendering related things.
No button pressed.
Definition: Mouse.h:57
void setVisible(bool visible)
Set Mouse visibility.
Definition: Mouse.cpp:51
void fireDownEvent(MouseInfo mouse)
Called internally when a Mouse button down event occurs. It calls MouseListener::mouseButtonDown().
Definition: Mouse.cpp:89
Interface for MouseListener callback methods.
bool isButtonDown(Button button)
Determines whether given Mouse button is down or not.
Definition: Mouse.cpp:229
fp32_hf y
Y coordinate, relative to window.
Definition: Mouse.h:69
void setPosition(const Vector2 &pos)
Set Mouse position.
Definition: Mouse.cpp:169
void addListener(MouseListener *listener)
Add a new listener class to receive MouseListener callbacks.
Definition: Mouse.cpp:61
int int32_hf
A type definition for int.
Definition: HFDataTypes.h:349
void removeListener(MouseListener *listener)
Remove a listener class from receiving further MouseListener callbacks.
Definition: Mouse.cpp:67
uint32_hf windowID
Window ID with mouse focus, if any.
Definition: Mouse.h:67
int32_hf wheelDelta
Mouse wheel delta value (scroll forward = -120, scroll backward = 120)
Definition: Mouse.h:74
void fireLeaveEvent(MouseInfo mouse)
Called internally when a Mouse leave event occurs. It calls MouseListener::mouseLeave().
Definition: Mouse.cpp:147
fp32_hf x
X coordinate, relative to window.
Definition: Mouse.h:68
bool getButtonsEnabled()
Determines whether Mouse buttons is enabled or not.
Definition: Mouse.cpp:218
Interface for input type Mouse.
const Vector2 & getPosition()
Get Mouse position.
Definition: Mouse.cpp:164
virtual void setEnabled(bool enabled)
Set controller enabled or disabled.
Definition: Controller.cpp:47
Button
Mouse button enum.
Definition: Mouse.h:52
Interface for Resource type Image.
bool mIsVisible
Mouse visibility.
Definition: Mouse.h:261
A two-dimensional floating point vector and associated math functions.
Definition: Vector2.h:42
Vector2 mPosition
Vector2 holding Mouse position.
Definition: Mouse.h:256
A structure that contains Mouse event information.
Definition: Mouse.h:63
virtual bool isEnabled()
Check whether controller is enabled.
Definition: Controller.cpp:52
Mouse * self
Pointer to Mouse.
Definition: Mouse.h:65
Right button.
Definition: Mouse.h:56
bool mIsInBounds
Mouse in bounds.
Definition: Mouse.h:276
Button button
Mouse Button enum value.
Definition: Mouse.h:72
Left button.
Definition: Mouse.h:54
virtual void setConnected(bool connected)
Set controller connected or disconnected.
Definition: Mouse.cpp:190
fp32_hf y
Y coordinate of the vector.
Definition: Vector2.h:289
bool isInBounds()
Determines whether the Mouse is in bounds or not.
Definition: Mouse.cpp:238
bool mIsButtonDown[3]
Mouse button array for holding button down or not for Left, Middle and Right buttons.
Definition: Mouse.h:282
int32_hf mId
Mouse ID.
Definition: Mouse.h:266
Definition: Actor.h:34
MouseListenerVector mListeners
Vector holding MouseListener classes.
Definition: Mouse.h:251
Singleton interface for platform specific things.
fp32_hf xrel
Relative motion in the X direction.
Definition: Mouse.h:70
void fireEnterEvent(MouseInfo mouse)
Called internally when a Mouse enter event occurs. It calls MouseListener::mouseEnter().
Definition: Mouse.cpp:130
void setButtonsEnabled(bool enabled)
Set Mouse buttons enabled / disabled.
Definition: Mouse.cpp:223
Mouse(int32_hf id)
Constructor.
Definition: Mouse.cpp:36
Middle button.
Definition: Mouse.h:55
#define HFCORE_ENV
Get HFCore::Environment instance shortcut macro.
Definition: Environment.h:68
bool mButtonsEnabled
Mouse buttons enabled / disabled.
Definition: Mouse.h:271
virtual bool isConnected()
Check whether controller is connected.
Definition: Controller.cpp:42
virtual void setEnabled(bool enabled)
Set controller enabled or disabled.
Definition: Mouse.cpp:212
bool isVisible()
Determines whether Mouse is visible or not.
Definition: Mouse.cpp:56
void fireUpEvent(MouseInfo mouse)
Called internally when a Mouse button up event occurs. It calls MouseListener::mouseButtonUp().
Definition: Mouse.cpp:103
virtual void setConnected(bool connected)
Set controller connected or disconnected.
Definition: Controller.cpp:37
fp32_hf yrel
Relative motion in the Y direction.
Definition: Mouse.h:71
void fireMoveEvent(MouseInfo mouse)
Called internally when a Mouse motion event occurs. It calls MouseListener::mouseMove().
Definition: Mouse.cpp:73
virtual ~Mouse()
Destructor.
Definition: Mouse.cpp:47
#define envDebugLog(format, args...)
Shortcut macro for logging to &#39;debug.log&#39;.
Definition: Environment.h:2364
void fireWheelEvent(MouseInfo mouse)
Called internally when a Mouse wheel event occurs. It calls MouseListener::mouseWheel().
Definition: Mouse.cpp:117
int32_hf clickCount
Mouse click count.
Definition: Mouse.h:73
Interface for MouseListener callback methods.
Definition: MouseListener.h:87