Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
GamePad.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 "GamePad.h"
22 
23 #include "GamePadListener.h"
24 #include "MouseListener.h"
25 
26 #include <algorithm>
27 #include <assert.h>
28 
29 #include "platform/Environment.h"
30 #include "platform/Graphics.h"
31 #include "platform/Image.h"
32 
33 using namespace HFCore;
34 
35 #include "debug/CrtDbgNew.h"
36 
38 {
39  mId = id;
40  mInstanceId = id;
41  mName = "";
42  mNumAxis = 0;
43  mNumBalls = 0;
44  mNumButtons = 0;
45  mNumHats = 0;
46 }
47 
49 {
50 }
51 
53 {
54  assert(std::find(mListeners.begin(), mListeners.end(), listener) == mListeners.end());
55  mListeners.push_back(listener);
56 }
57 
59 {
60  assert(std::find(mListeners.begin(), mListeners.end(), listener) != mListeners.end());
61  mListeners.erase(std::find(mListeners.begin(), mListeners.end(), listener));
62 }
63 
65 {
66  if (mListeners.size() > 0 && isEnabled())
67  {
68  uint32_hf numListeners = mListeners.size();
69  for (uint32_hf i = 0; i < numListeners; i++)
70  {
71  axis.self = this;
72  mListeners[i]->gamePadAxisMotion(axis);
73  }
74  }
75 }
76 
78 {
79  if (mListeners.size() > 0 && isEnabled())
80  {
81  uint32_hf numListeners = mListeners.size();
82  for (uint32_hf i = 0; i < numListeners; i++)
83  {
84  ball.self = this;
85  mListeners[i]->gamePadBallMotion(ball);
86  }
87  }
88 }
89 
91 {
92  if (mListeners.size() > 0 && isEnabled())
93  {
94  uint32_hf numListeners = mListeners.size();
95  for (uint32_hf i = 0; i < numListeners; i++)
96  {
97  hat.self = this;
98  mListeners[i]->gamePadHatMotion(hat);
99  }
100  }
101 }
102 
104 {
105  if (mListeners.size() > 0 && isEnabled())
106  {
107  uint32_hf numListeners = mListeners.size();
108  for (uint32_hf i = 0; i < numListeners; i++)
109  {
110  button.self = this;
111  mListeners[i]->gamePadButtonDown(button);
112  }
113  }
114 }
115 
117 {
118  if (mListeners.size() > 0 && isEnabled())
119  {
120  uint32_hf numListeners = mListeners.size();
121  for (uint32_hf i = 0; i < numListeners; i++)
122  {
123  button.self = this;
124  mListeners[i]->gamePadButtonUp(button);
125  }
126  }
127 }
128 
129 std::string GamePad::getName()
130 {
131  return mName;
132 }
133 
135 {
136  return mInstanceId;
137 }
138 
140 {
141  return mNumAxis;
142 }
143 
145 {
146  return mNumBalls;
147 }
148 
150 {
151  return mNumButtons;
152 }
153 
155 {
156  return mNumHats;
157 }
158 
159 void GamePad::setEnabled(bool enabled)
160 {
161  Controller::setEnabled(enabled);
162 }
163 
164 void GamePad::setConnected(bool connected)
165 {
166  // if there's no change, return:
167  if (connected == isConnected())
168  {
169  return;
170  }
171 
172  // connect the controller:
173  Controller::setConnected(connected);
174 
175  // fire notification:
176  if (connected)
177  {
178  HFCORE_ENV->fireGamePadAdded(mId);
179  envEngineLog("GamePad(%d) connected.\r\n", mId);
180  }
181  else
182  {
183  HFCORE_ENV->fireGamePadRemoved(mId);
184  envEngineLog("GamePad(%d) disconnected.\r\n", mId);
185  }
186 }
unsigned int uint32_hf
A type definition for unsigned int.
Definition: HFDataTypes.h:354
void fireHatMotionEvent(GamePadHatInfo hat)
Called internally when a GamePad hat event occurs. It calls GamePadListener::gamePadHatMotion().
Definition: GamePad.cpp:90
int32_hf mNumAxis
Number of GamePad axis.
Definition: GamePad.h:291
Interface for all rendering related things.
virtual std::string getName()
Get GamePad name.
Definition: GamePad.cpp:129
GamePad * self
Pointer to GamePad.
Definition: GamePad.h:85
void fireAxisMotionEvent(GamePadAxisInfo axis)
Called internally when a GamePad axis motion event occurs. It calls GamePadListener::gamePadAxisMotio...
Definition: GamePad.cpp:64
virtual int32_hf getNumButtons()
Get number of GamePad buttons available.
Definition: GamePad.cpp:149
Interface for MouseListener callback methods.
A structure that contains GamePad / Joystick hat position change event information.
Definition: GamePad.h:110
std::string mName
GamePad name.
Definition: GamePad.h:286
int32_hf mNumBalls
Number of GamePad balls.
Definition: GamePad.h:296
A structure that contains GamePad / Joystick button event information.
Definition: GamePad.h:123
int int32_hf
A type definition for int.
Definition: HFDataTypes.h:349
int32_hf mInstanceId
GamePad instance id.
Definition: GamePad.h:281
virtual ~GamePad()
Destructor.
Definition: GamePad.cpp:48
std::vector< GamePadListener * > mListeners
Vector holding GamePadListener classes.
Definition: GamePad.h:271
GamePad * self
Pointer to GamePad.
Definition: GamePad.h:112
int32_hf mNumButtons
Number of GamePad buttons.
Definition: GamePad.h:301
GamePad * self
Pointer to GamePad.
Definition: GamePad.h:98
virtual int32_hf getNumAxis()
Get number of GamePad axis available.
Definition: GamePad.cpp:139
virtual int32_hf getNumHats()
Get number of GamePad hats available.
Definition: GamePad.cpp:154
A structure that contains GamePad / Joystick trackball motion event information.
Definition: GamePad.h:96
virtual void setEnabled(bool enabled)
Set controller enabled or disabled.
Definition: Controller.cpp:47
Interface for input type GamePad / Joystick.
Interface for Resource type Image.
void fireBallMotionEvent(GamePadBallInfo ball)
Called internally when a GamePad ball event occurs. It calls GamePadListener::gamePadBallMotion()).
Definition: GamePad.cpp:77
virtual void setConnected(bool connected)
Set controller connected or disconnected.
Definition: GamePad.cpp:164
Interface for GamePadListener callback methods.
int32_hf mNumHats
Number of GamePad hats.
Definition: GamePad.h:306
Interface for GamePadListener callback methods.
virtual bool isEnabled()
Check whether controller is enabled.
Definition: Controller.cpp:52
void removeListener(GamePadListener *listener)
Remove a listener class from receiving further GamePadListener callbacks.
Definition: GamePad.cpp:58
void fireButtonUpEvent(GamePadButtonInfo button)
Called internally when a GamePad button up event occurs. It calls GamePadListener::gamePadButtonUp()...
Definition: GamePad.cpp:116
void addListener(GamePadListener *listener)
Add a new listener class to receive GamePadListener callbacks.
Definition: GamePad.cpp:52
virtual void setEnabled(bool enabled)
Set controller enabled or disabled.
Definition: GamePad.cpp:159
void fireButtonDownEvent(GamePadButtonInfo button)
Called internally when a GamePad button down event occurs. It calls GamePadListener::gamePadButtonDow...
Definition: GamePad.cpp:103
virtual int32_hf getInstanceId()
Get GamePad instance id.
Definition: GamePad.cpp:134
Definition: Actor.h:34
Singleton interface for platform specific things.
#define envEngineLog(format, args...)
Shortcut macro for logging to &#39;engine.log&#39;.
Definition: Environment.h:2369
#define HFCORE_ENV
Get HFCore::Environment instance shortcut macro.
Definition: Environment.h:68
virtual bool isConnected()
Check whether controller is connected.
Definition: Controller.cpp:42
int32_hf mId
GamePad ID.
Definition: GamePad.h:276
virtual void setConnected(bool connected)
Set controller connected or disconnected.
Definition: Controller.cpp:37
A structure that contains GamePad / Joystick axis motion event information.
Definition: GamePad.h:83
GamePad * self
Pointer to GamePad.
Definition: GamePad.h:125
virtual int32_hf getNumBalls()
Get number of GamePad balls available.
Definition: GamePad.cpp:144
GamePad(int32_hf id)
Constructor.
Definition: GamePad.cpp:37