Sirikata
libogre/include/sirikata/ogre/input/SDLInputDevice.hpp
Go to the documentation of this file.
00001 /*  Sirikata Input Plugin -- plugins/input
00002  *  SDLInputDevice.hpp
00003  *
00004  *  Copyright (c) 2009, Patrick Reiter Horn
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions are
00009  *  met:
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in
00014  *    the documentation and/or other materials provided with the
00015  *    distribution.
00016  *  * Neither the name of Sirikata nor the names of its contributors may
00017  *    be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00024  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #ifndef SIRIKATA_INPUT_SDLInputDevice_HPP__
00034 #define SIRIKATA_INPUT_SDLInputDevice_HPP__
00035 
00036 #include <sirikata/ogre/input/InputDevice.hpp>
00037 
00038 struct SDL_MouseMotionEvent;
00039 struct _SDL_Joystick;
00040 
00041 namespace Sirikata {
00042 namespace Input {
00043 
00044 class SDLInputManager;
00045 
00046 class SDLMouse;
00047 typedef std::tr1::shared_ptr<SDLMouse> SDLMousePtr;
00048 class SDLJoystick;
00049 typedef std::tr1::shared_ptr<SDLJoystick> SDLJoystickPtr;
00050 class SDLKeyboard;
00051 typedef std::tr1::shared_ptr<SDLKeyboard> SDLKeyboardPtr;
00052 
00053 class SIRIKATA_OGRE_EXPORT SDLMouse: public PointerDevice {
00054     unsigned int mWhich;
00055     unsigned int mNumButtons;
00056 public:
00057     SDLMouse(SDLInputManager *inputManager, unsigned int which);
00058     virtual ~SDLMouse();
00059 
00060     SDLInputManager* inputManager();
00061 
00062     enum Axes {WHEELX=NUM_POINTER_AXES, WHEELY,
00063           PRESSURE, CURSORZ, ROTATION, TILT, NUM_AXES};
00064     virtual int getNumButtons() const;
00065     virtual std::string getButtonName(unsigned int button) const;
00066     virtual unsigned int getNumAxes() const {
00067         // Constant... each axis has a special purpose.
00068         // Don't include axes labeled "for future use".
00069         return PRESSURE + 1;
00070     }
00071     virtual void setRelativeMode(bool enabled);
00072 
00073     virtual std::string getAxisName(unsigned int axis) const {
00074         switch (axis) {
00075           case AXIS_CURSORX:
00076             return "x (Cursor)";
00077           case AXIS_CURSORY:
00078             return "y (Cursor)";
00079           case AXIS_RELX:
00080             return "x (Relative)";
00081           case AXIS_RELY:
00082             return "y (Relative)";
00083           case WHEELX:
00084             return "screll-x";
00085           case WHEELY:
00086             return "scroll-y";
00087           case CURSORZ:
00088             return "z";
00089           case PRESSURE:
00090             return "pressure";
00091           case ROTATION:
00092             return "rotation";
00093           case TILT:
00094             return "tilt";
00095         }
00096         return std::string();
00097     }
00098 
00099     void fireMotion(const SDLMousePtr &thisptr,
00100                     const ::SDL_MouseMotionEvent &ev);
00101     void fireWheel(const SDLMousePtr &thisptr,
00102                    int xrel,
00103                    int yrel);
00104 };
00105 
00106 class SDLDigitalHatswitch;
00107 
00108 class SIRIKATA_OGRE_EXPORT SDLJoystick : public InputDevice {
00109 private:
00110     std::string mName;
00111     std::vector<std::string> mButtonNames;
00112     ::_SDL_Joystick *mJoy;
00113     unsigned int mWhich;
00114     unsigned int mNumGeneralAxes;
00115     unsigned int mNumBalls;
00116     unsigned int mNumButtons;
00117     unsigned int mNumHats;
00118     enum Directions {HAT_UP, HAT_RIGHT, HAT_DOWN, HAT_LEFT, HAT_MAX};
00119 public:
00120 
00122     SDLJoystick(unsigned int which);
00123     virtual ~SDLJoystick();
00124 
00125     SDLInputManager* inputManager();
00126 
00127     virtual std::string getButtonName(unsigned int button) const;
00128     virtual std::string getAxisName(unsigned int button) const;
00129     void setButtonName(unsigned int button, std::string name);
00130     virtual unsigned int getNumAxes() const;
00131     virtual int getNumButtons() const;
00132 
00133     void fireHat(const SDLJoystickPtr &thisptr,
00134                  unsigned int hatNumber,
00135                  int hatValue);
00136     void fireBall(const SDLJoystickPtr &thisptr,
00137                   unsigned int ballNumber,
00138                   int xrel,
00139                   int yrel);
00140 };
00141 
00142 class SIRIKATA_OGRE_EXPORT SDLKeyboard : public InputDevice {
00143     unsigned int mWhich;
00144 public:
00145     SDLKeyboard(unsigned int which);
00146     virtual ~SDLKeyboard();
00147 
00148     SDLInputManager* inputManager();
00149 
00150     enum Directions {UP, RIGHT, DOWN, LEFT, MAX_DIRECTION};
00151     virtual std::string getButtonName(unsigned int button) const;
00152     // Full keyboard--pointless to list all the possible buttons.
00153     virtual int getNumButtons() const { return -1; }
00154     virtual bool isKeyboard() { return true; }
00155     virtual unsigned int getNumAxes() const { return 0; }
00156     virtual std::string getAxisName(unsigned int) const {
00157         return std::string();
00158     }
00159 };
00160 
00161 
00162 SIRIKATA_OGRE_EXPORT bool keyIsModifier(Input::KeyButton b);
00163 SIRIKATA_OGRE_EXPORT String keyButtonString(Input::KeyButton b);
00164 SIRIKATA_OGRE_EXPORT String keyModifiersString(Input::Modifier m);
00165 SIRIKATA_OGRE_EXPORT String mouseButtonString(Input::MouseButton b);
00166 SIRIKATA_OGRE_EXPORT String axisString(Input::AxisIndex i);
00167 SIRIKATA_OGRE_EXPORT Input::KeyButton keyButtonFromStrings(std::vector<String>& parts);
00168 SIRIKATA_OGRE_EXPORT Input::Modifier keyModifiersFromStrings(std::vector<String>& parts);
00169 SIRIKATA_OGRE_EXPORT Input::MouseButton mouseButtonFromStrings(std::vector<String>& parts);
00170 SIRIKATA_OGRE_EXPORT Input::AxisIndex axisFromStrings(std::vector<String>& parts);
00171 
00172 }
00173 }
00174 
00175 #endif