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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/* -------------------------------------------------------
Copyright (c) 2011 Alberto G. Salguero (alberto.salguero (at) uca.es)
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions of
the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------- */
#ifndef OICS_InputControlSystem_H_
#define OICS_InputControlSystem_H_
#include "ICSPrerequisites.h"
#include "ICSControl.h"
#include "ICSChannel.h"
#define ICS_LOG(text) if(mLog) mLog->logMessage( ("ICS: " + std::string(text)).c_str() );
#define ICS_MAX_JOYSTICK_AXIS 16
#define ICS_MOUSE_BINDING_MARGIN 30
#define ICS_JOYSTICK_AXIS_BINDING_MARGIN 10000
#define ICS_JOYSTICK_SLIDER_BINDING_MARGIN 10000
#define ICS_MOUSE_AXIS_BINDING_NULL_VALUE std::numeric_limits<int>::max()
namespace ICS
{
class DllExport InputControlSystemLog
{
public:
virtual void logMessage(const char* text) = 0;
virtual ~InputControlSystemLog() = default;
};
class DllExport InputControlSystem
{
public:
enum NamedAxis { X = -1, Y = -2, Z = -3, UNASSIGNED = -4 };
enum POVAxis { NorthSouth = 0, EastWest = 1 };
typedef NamedAxis MouseAxis; // MouseAxis is deprecated. It will be removed in future versions
typedef std::map<int, SDL_GameController*> JoystickInstanceMap;
typedef std::list<int> JoystickIDList;
typedef struct
{
int index;
POVAxis axis;
} POVBindingPair;
InputControlSystem(std::string file = "", bool active = true
, DetectingBindingListener* detectingBindingListener = NULL
, InputControlSystemLog* log = NULL, size_t channelCount = 16);
~InputControlSystem();
std::string getFileName(){ return mFileName; };
std::string getBaseFileName();
void setDetectingBindingListener(DetectingBindingListener* detectingBindingListener){ mDetectingBindingListener = detectingBindingListener; };
DetectingBindingListener* getDetectingBindingListener(){ return mDetectingBindingListener; };
void setJoystickDeadZone(float deadZone){ mDeadZone = deadZone; };
// in seconds
void update(float timeSinceLastFrame);
Channel* getChannel(int i){ return mChannels.at(i); };
float getChannelValue(int i);
inline int getChannelCount(){ return (int)mChannels.size(); };
Control* getControl(int i){ return mControls.at(i); };
float getControlValue(int i);
inline int getControlCount(){ return (int)mControls.size(); };
inline void addControl(Control* control){ mControls.push_back(control); };
Control* findControl(std::string name);
inline void activate(){ this->mActive = true; };
inline void deactivate(){ this->mActive = false; };
void controllerAdded (int deviceID, const SDL_ControllerDeviceEvent &args);
void controllerRemoved(const SDL_ControllerDeviceEvent &args);
JoystickIDList& getJoystickIdList(){ return mJoystickIDList; };
JoystickInstanceMap& getJoystickInstanceMap(){ return mJoystickInstanceMap; };
// MouseListener
void mouseMoved(const SDL_MouseMotionEvent &evt);
void mousePressed(const SDL_MouseButtonEvent &evt, Uint8);
void mouseReleased(const SDL_MouseButtonEvent &evt, Uint8);
// KeyListener
void keyPressed(const SDL_KeyboardEvent &evt);
void keyReleased(const SDL_KeyboardEvent &evt);
// ControllerListener
void buttonPressed(int deviceID, const SDL_ControllerButtonEvent &evt);
void buttonReleased(int deviceID, const SDL_ControllerButtonEvent &evt);
void axisMoved(int deviceID, const SDL_ControllerAxisEvent &evt);
void addKeyBinding(Control* control, SDL_Scancode key, Control::ControlChangingDirection direction);
bool isKeyBound(SDL_Scancode key) const;
void addMouseAxisBinding(Control* control, NamedAxis axis, Control::ControlChangingDirection direction);
void addMouseButtonBinding(Control* control, unsigned int button, Control::ControlChangingDirection direction);
bool isMouseButtonBound(unsigned int button) const;
void addJoystickAxisBinding(Control* control, int deviceID, int axis, Control::ControlChangingDirection direction);
void addJoystickButtonBinding(Control* control, int deviceID, unsigned int button, Control::ControlChangingDirection direction);
bool isJoystickButtonBound(int deviceID, unsigned int button) const;
bool isJoystickAxisBound(int deviceID, unsigned int axis) const;
void removeKeyBinding(SDL_Scancode key);
void removeMouseAxisBinding(NamedAxis axis);
void removeMouseButtonBinding(unsigned int button);
void removeJoystickAxisBinding(int deviceID, int axis);
void removeJoystickButtonBinding(int deviceID, unsigned int button);
SDL_Scancode getKeyBinding(Control* control, ICS::Control::ControlChangingDirection direction);
NamedAxis getMouseAxisBinding(Control* control, ICS::Control::ControlChangingDirection direction);
unsigned int getMouseButtonBinding(Control* control, ICS::Control::ControlChangingDirection direction);
int getJoystickAxisBinding(Control* control, int deviceID, ICS::Control::ControlChangingDirection direction);
unsigned int getJoystickButtonBinding(Control* control, int deviceID, ICS::Control::ControlChangingDirection direction);
std::string scancodeToString(SDL_Scancode key);
void enableDetectingBindingState(Control* control, Control::ControlChangingDirection direction);
void cancelDetectingBindingState();
bool detectingBindingState();
bool save(const std::string& fileName);
void adjustMouseRegion (Uint16 width, Uint16 height);
protected:
void loadKeyBinders(TiXmlElement* xmlControlNode);
void loadMouseAxisBinders(TiXmlElement* xmlControlNode);
void loadMouseButtonBinders(TiXmlElement* xmlControlNode);
void loadJoystickAxisBinders(TiXmlElement* xmlControlNode);
void loadJoystickButtonBinders(TiXmlElement* xmlControlNode);
void loadJoystickPOVBinders(TiXmlElement* xmlControlNode);
void loadJoystickSliderBinders(TiXmlElement* xmlControlNode);
void addMouseAxisBinding_(Control* control, int axis, Control::ControlChangingDirection direction);
void removeMouseAxisBinding_(int axis);
protected:
typedef struct {
Control::ControlChangingDirection direction;
Control* control;
} ControlKeyBinderItem;
typedef ControlKeyBinderItem ControlAxisBinderItem;
typedef ControlKeyBinderItem ControlButtonBinderItem;
typedef ControlKeyBinderItem ControlPOVBinderItem;
typedef ControlKeyBinderItem ControlSliderBinderItem;
typedef struct {
Control* control;
Control::ControlChangingDirection direction;
} PendingActionItem;
std::list<PendingActionItem> mPendingActions;
std::string mFileName;
float mDeadZone;
typedef std::map<SDL_Scancode, ControlKeyBinderItem> ControlsKeyBinderMapType; // <Scancode, [direction, control]>
typedef std::map<int, ControlAxisBinderItem> ControlsAxisBinderMapType; // <axis, [direction, control]>
typedef std::map<int, ControlButtonBinderItem> ControlsButtonBinderMapType; // <button, [direction, control]>
typedef std::map<int, ControlsAxisBinderMapType> JoystickAxisBinderMapType; // <joystick_id, <axis, [direction, control]> >
typedef std::map<int, ControlsButtonBinderMapType> JoystickButtonBinderMapType; // <joystick_id, <button, [direction, control]> >
ControlsAxisBinderMapType mControlsMouseAxisBinderMap; // <axis, [direction, control]>
ControlsButtonBinderMapType mControlsMouseButtonBinderMap; // <int, [direction, control]>
JoystickAxisBinderMapType mControlsJoystickAxisBinderMap; // <axis, [direction, control]>
JoystickButtonBinderMapType mControlsJoystickButtonBinderMap; // <button, [direction, control]>
std::vector<Control *> mControls;
std::vector<Channel *> mChannels;
ControlsKeyBinderMapType mControlsKeyBinderMap;
bool mActive;
InputControlSystemLog* mLog;
DetectingBindingListener* mDetectingBindingListener;
Control* mDetectingBindingControl;
Control::ControlChangingDirection mDetectingBindingDirection;
bool mXmouseAxisBinded;
bool mYmouseAxisBinded;
JoystickIDList mJoystickIDList;
JoystickInstanceMap mJoystickInstanceMap;
int mMouseAxisBindingInitialValues[3];
private:
Uint16 mClientWidth;
Uint16 mClientHeight;
/* ----------------------------------------------------------------------------------------
* OPENMW CODE STARTS HERE
* Mouse Wheel support added by Michael Stopa (Stomy) */
public:
enum class MouseWheelClick : int { UNASSIGNED = 0, UP = 1, DOWN = 2, LEFT = 3, RIGHT = 4};
void mouseWheelMoved(const SDL_MouseWheelEvent &evt);
void addMouseWheelBinding(Control* control, MouseWheelClick click, Control::ControlChangingDirection direction);
void removeMouseWheelBinding(MouseWheelClick click);
MouseWheelClick getMouseWheelBinding(Control* control, ICS::Control::ControlChangingDirection direction);
bool isMouseWheelBound(MouseWheelClick button) const;
protected:
void loadMouseWheelBinders(TiXmlElement* xmlControlNode);
ControlsButtonBinderMapType mControlsMouseWheelBinderMap;
/* OPENMW CODE ENDS HERE
* ------------------------------------------------------------------------------------- */
};
class DllExport DetectingBindingListener
{
public:
virtual void keyBindingDetected(InputControlSystem* ICS, Control* control
, SDL_Scancode key, Control::ControlChangingDirection direction);
virtual void mouseAxisBindingDetected(InputControlSystem* ICS, Control* control
, InputControlSystem::NamedAxis axis, Control::ControlChangingDirection direction);
virtual void mouseButtonBindingDetected(InputControlSystem* ICS, Control* control
, unsigned int button, Control::ControlChangingDirection direction);
virtual void joystickAxisBindingDetected(InputControlSystem* ICS, int deviceID, Control* control
, int axis, Control::ControlChangingDirection direction);
virtual void joystickButtonBindingDetected(InputControlSystem* ICS, int deviceID, Control* control
, unsigned int button, Control::ControlChangingDirection direction);
/* ----------------------------------------------------------------------------------------
* OPENMW CODE STARTS HERE
* Mouse Wheel support added by Michael Stopa (Stomy) */
virtual void mouseWheelBindingDetected(InputControlSystem* ICS, Control* control,
InputControlSystem::MouseWheelClick click,
Control::ControlChangingDirection direction);
virtual ~DetectingBindingListener() = default;
/* OPENMW CODE ENDS HERE
* ------------------------------------------------------------------------------------- */
};
extern const float ICS_MAX;
}
#endif
|