VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
ControllerEvent.h
1 /*
2  * Copyright (C) 2014 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CONTROLLEREVENT_H
18 #define CONTROLLEREVENT_H
19 
20 #include <map>
21 #include "KeyCodes.h"
22 #include "JoystickAxisCodes.h"
23 #include "TriggerAxisCodes.h"
24 
35 public:
39  typedef std::map<KeyCode, bool> KeyStates;
43  typedef std::map<JoystickAxisCode, float> JoystickAxisStates;
47  typedef std::map<TriggerAxisCode, float> TriggerAxisStates;
48 
53  ControllerEvent(int deviceId = -1);
54 
55  // Needed to make a full polymorphic definition of ControllerEvent.
56  virtual ~ControllerEvent();
57 
61  bool hasDeviceId() const { return mDeviceId >= 0; }
66  int getDeviceId() const { return mDeviceId; }
70  void setDeviceId(int deviceId) { mDeviceId = deviceId; }
71 
75  bool hasKeyInfo() const { return !mKeyStates.empty(); }
81  bool hasKeyInfo(const KeyCode& keyCode) const { return mKeyStates.count(keyCode) > 0; }
86  bool isKeyPressed(const KeyCode& keyCode) const;
90  const KeyStates& getKeyStates() const { return mKeyStates; }
96  void setKeyState(const KeyCode& keyCode, bool isPressed) { mKeyStates[keyCode] = isPressed; }
97 
102  bool hasJoystickAxisInfo() const { return !mJoystickAxisStates.empty(); }
107  bool hasJoystickAxisInfo(const JoystickAxisCode& axis) const {
108  return mJoystickAxisStates.count(axis) > 0;
109  }
114  float getJoystickAxisValue(const JoystickAxisCode& axis) const;
118  const JoystickAxisStates& getJoystickAxisStates() const { return mJoystickAxisStates; }
124  void setJoystickAxisValue(const JoystickAxisCode& axis, float value);
125 
130  bool hasTriggerAxisInfo() const { return !mTriggerAxisStates.empty(); }
135  bool hasTriggerAxisInfo(const TriggerAxisCode& axis) const {
136  return mTriggerAxisStates.count(axis) > 0;
137  }
142  float getTriggerAxisValue(const TriggerAxisCode& axis) const;
146  const TriggerAxisStates& getTriggerAxisStates() const { return mTriggerAxisStates; }
152  void setTriggerAxisValue(const TriggerAxisCode& axis, float value);
153 
154 private:
155  int mDeviceId;
156  KeyStates mKeyStates;
157  JoystickAxisStates mJoystickAxisStates;
158  TriggerAxisStates mTriggerAxisStates;
159 };
160 
161 #endif // CONTROLLEREVENT_H
const TriggerAxisStates & getTriggerAxisStates() const
Returns trigger axis value changes for all trigger axes contained in this event.
Definition: ControllerEvent.h:146
bool hasKeyInfo(const KeyCode &keyCode) const
Returns whether or not this ControllerEvent contains any key press information for the specified key...
Definition: ControllerEvent.h:81
const JoystickAxisStates & getJoystickAxisStates() const
Returns joystick axis value changes for all joystick axes contained in this event.
Definition: ControllerEvent.h:118
int getDeviceId() const
Returns the deviceId for this ControllerEvent, or a negative number if it is "deviceless".
Definition: ControllerEvent.h:66
void setDeviceId(int deviceId)
Sets the deviceId for this ControllerEvent.
Definition: ControllerEvent.h:70
void setKeyState(const KeyCode &keyCode, bool isPressed)
Sets whether or not the key has been pressed or released.
Definition: ControllerEvent.h:96
bool hasKeyInfo() const
Returns whether or not this ControllerEvent contains any key press information.
Definition: ControllerEvent.h:75
bool hasTriggerAxisInfo() const
Returns whether or not this ControllerEvent contains any axis information for the specified trigger a...
Definition: ControllerEvent.h:130
A platform independent event generated by controller device.
Definition: ControllerEvent.h:34
bool hasJoystickAxisInfo() const
Returns whether or not this ControllerEvent contains any axis information for the specified joystick ...
Definition: ControllerEvent.h:102
void setJoystickAxisValue(const JoystickAxisCode &axis, float value)
Sets the [-1.0f, 1.0f]joystick axis value for the specified joystick axis.
void setTriggerAxisValue(const TriggerAxisCode &axis, float value)
Sets the [0.0, 1.0] trigger axis value for the specified trigger axis.
KeyCode
Key codes used to uniquely identify hardware input keys.
Definition: KeyCodes.h:26
float getJoystickAxisValue(const JoystickAxisCode &axis) const
Returns the [-1.0f, 1.0f] axis value for the specified joystick axis.
const KeyStates & getKeyStates() const
Returns pressed state changes for all keys contained in this event.
Definition: ControllerEvent.h:90
bool isKeyPressed(const KeyCode &keyCode) const
Returns whether or not the specified key is pressed.
bool hasJoystickAxisInfo(const JoystickAxisCode &axis) const
Returns whether or not this ControllerEvent contains any axis information.
Definition: ControllerEvent.h:107
ControllerEvent(int deviceId=-1)
Constructs a ControllerEvent for a controller identified by the specified deviceId.
std::map< TriggerAxisCode, float > TriggerAxisStates
Map of trigger axes to their changes in [0.0f, 1.0f] axis value.
Definition: ControllerEvent.h:47
JoystickAxisCode
Codes that uniquely identify each joystick axis, independent of platform.
Definition: JoystickAxisCodes.h:24
float getTriggerAxisValue(const TriggerAxisCode &axis) const
Returns the [0.0, 1.0] axis value for the specified trigger axis.
TriggerAxisCode
Codes that uniquely identify each trigger axis, independent of platform.
Definition: TriggerAxisCodes.h:24
bool hasTriggerAxisInfo(const TriggerAxisCode &axis) const
Returns the [0.0, 1.0] axis value for the specified trigger axis.
Definition: ControllerEvent.h:135
std::map< JoystickAxisCode, float > JoystickAxisStates
Map of joystick axes to their changes in [-1.0f, 1.0f] axis value.
Definition: ControllerEvent.h:43
std::map< KeyCode, bool > KeyStates
Map of keys to their changes in pressed state.
Definition: ControllerEvent.h:39
bool hasDeviceId() const
Returns whether or not this ControllerEvent has a valid (non-zero) deviceId.
Definition: ControllerEvent.h:61