| 1 | /* | 
|---|
| 2 | The zlib/libpng License | 
|---|
| 3 |  | 
|---|
| 4 | Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com) | 
|---|
| 5 |  | 
|---|
| 6 | This software is provided 'as-is', without any express or implied warranty. In no event will | 
|---|
| 7 | the authors be held liable for any damages arising from the use of this software. | 
|---|
| 8 |  | 
|---|
| 9 | Permission is granted to anyone to use this software for any purpose, including commercial | 
|---|
| 10 | applications, and to alter it and redistribute it freely, subject to the following | 
|---|
| 11 | restrictions: | 
|---|
| 12 |  | 
|---|
| 13 |     1. The origin of this software must not be misrepresented; you must not claim that | 
|---|
| 14 |                 you wrote the original software. If you use this software in a product, | 
|---|
| 15 |                 an acknowledgment in the product documentation would be appreciated but is | 
|---|
| 16 |                 not required. | 
|---|
| 17 |  | 
|---|
| 18 |     2. Altered source versions must be plainly marked as such, and must not be | 
|---|
| 19 |                 misrepresented as being the original software. | 
|---|
| 20 |  | 
|---|
| 21 |     3. This notice may not be removed or altered from any source distribution. | 
|---|
| 22 | */ | 
|---|
| 23 | #ifndef OIS_MultiTouch_H | 
|---|
| 24 | #define OIS_MultiTouch_H | 
|---|
| 25 | #include "OISObject.h" | 
|---|
| 26 | #include "OISEvents.h" | 
|---|
| 27 |  | 
|---|
| 28 | #include <set> | 
|---|
| 29 | #include <vector> | 
|---|
| 30 |  | 
|---|
| 31 | #define OIS_MAX_NUM_TOUCHES 4   // 4 finger touches are probably the highest we'll ever get | 
|---|
| 32 |  | 
|---|
| 33 | namespace OIS | 
|---|
| 34 | { | 
|---|
| 35 |         /** | 
|---|
| 36 |                 Represents the state of the multi-touch device | 
|---|
| 37 |                 All members are valid for both buffered and non buffered mode | 
|---|
| 38 |         */ | 
|---|
| 39 |      | 
|---|
| 40 |         //! Touch Event type | 
|---|
| 41 |         enum MultiTypeEventTypeID | 
|---|
| 42 |         { | 
|---|
| 43 |                 MT_None = 0, MT_Pressed, MT_Released, MT_Moved, MT_Cancelled | 
|---|
| 44 |         }; | 
|---|
| 45 |  | 
|---|
| 46 |         class _OISExport MultiTouchState | 
|---|
| 47 |         { | 
|---|
| 48 |         public: | 
|---|
| 49 |                 MultiTouchState() : width(50), height(50), touchType(MT_None) {}; | 
|---|
| 50 |  | 
|---|
| 51 |                 /** Represents the height/width of your display area.. used if touch clipping | 
|---|
| 52 |                 or touch grabbed in case of X11 - defaults to 50.. Make sure to set this | 
|---|
| 53 |                 and change when your size changes.. */ | 
|---|
| 54 |                 mutable int width, height; | 
|---|
| 55 |  | 
|---|
| 56 |                 //! X Axis component | 
|---|
| 57 |                 Axis X; | 
|---|
| 58 |  | 
|---|
| 59 |                 //! Y Axis Component | 
|---|
| 60 |                 Axis Y; | 
|---|
| 61 |  | 
|---|
| 62 |                 //! Z Axis Component | 
|---|
| 63 |                 Axis Z; | 
|---|
| 64 |  | 
|---|
| 65 |         int touchType; | 
|---|
| 66 |  | 
|---|
| 67 |         inline bool touchIsType( MultiTypeEventTypeID touch ) const | 
|---|
| 68 |                 { | 
|---|
| 69 |                         return ((touchType & ( 1L << touch )) == 0) ? false : true; | 
|---|
| 70 |                 } | 
|---|
| 71 |          | 
|---|
| 72 |                 //! Clear all the values | 
|---|
| 73 |                 void clear() | 
|---|
| 74 |                 { | 
|---|
| 75 |                         X.clear(); | 
|---|
| 76 |                         Y.clear(); | 
|---|
| 77 |                         Z.clear(); | 
|---|
| 78 |             touchType = MT_None; | 
|---|
| 79 |                 } | 
|---|
| 80 |         }; | 
|---|
| 81 |  | 
|---|
| 82 |         /** Specialised for multi-touch events */ | 
|---|
| 83 |         class _OISExport MultiTouchEvent : public EventArg | 
|---|
| 84 |         { | 
|---|
| 85 |         public: | 
|---|
| 86 |                 MultiTouchEvent( Object *obj, const MultiTouchState &ms ) : EventArg(obj), state(ms) {} | 
|---|
| 87 |                 virtual ~MultiTouchEvent() {} | 
|---|
| 88 |  | 
|---|
| 89 |                 //! The state of the touch - including axes | 
|---|
| 90 |                 const MultiTouchState &state; | 
|---|
| 91 |         }; | 
|---|
| 92 |  | 
|---|
| 93 |         /** | 
|---|
| 94 |                 To receive buffered touch input, derive a class from this, and implement the | 
|---|
| 95 |                 methods here. Then set the call back to your MultiTouch instance with MultiTouch::setEventCallback | 
|---|
| 96 |         */ | 
|---|
| 97 |         class _OISExport MultiTouchListener | 
|---|
| 98 |         { | 
|---|
| 99 |         public: | 
|---|
| 100 |                 virtual ~MultiTouchListener() {} | 
|---|
| 101 |                 virtual bool touchMoved( const MultiTouchEvent &arg ) = 0; | 
|---|
| 102 |                 virtual bool touchPressed( const MultiTouchEvent &arg ) = 0; | 
|---|
| 103 |                 virtual bool touchReleased( const MultiTouchEvent &arg ) = 0; | 
|---|
| 104 |                 virtual bool touchCancelled( const MultiTouchEvent &arg ) = 0; | 
|---|
| 105 |         }; | 
|---|
| 106 |  | 
|---|
| 107 |         /** | 
|---|
| 108 |                 MultiTouch base class. To be implemented by specific system (ie. iPhone UITouch) | 
|---|
| 109 |                 This class is useful as you remain OS independent using this common interface. | 
|---|
| 110 |         */ | 
|---|
| 111 |         class _OISExport MultiTouch : public Object | 
|---|
| 112 |         { | 
|---|
| 113 |         public: | 
|---|
| 114 |                 virtual ~MultiTouch() {} | 
|---|
| 115 |  | 
|---|
| 116 |                 /** | 
|---|
| 117 |                 @remarks | 
|---|
| 118 |                         Register/unregister a MultiTouch Listener - Only one allowed for simplicity. If broadcasting | 
|---|
| 119 |                         is necessary, just broadcast from the callback you registered. | 
|---|
| 120 |                 @param touchListener | 
|---|
| 121 |                         Send a pointer to a class derived from MultiTouchListener or 0 to clear the callback | 
|---|
| 122 |                 */ | 
|---|
| 123 |                 virtual void setEventCallback( MultiTouchListener *touchListener ) {mListener = touchListener;} | 
|---|
| 124 |  | 
|---|
| 125 |                 /** @remarks Returns currently set callback.. or 0 */ | 
|---|
| 126 |                 MultiTouchListener* getEventCallback() {return mListener;} | 
|---|
| 127 |  | 
|---|
| 128 |                 /** @remarks Clear out the set of input states.  Should be called after input has been processed by the application */ | 
|---|
| 129 |         void clearStates(void) { mStates.clear(); } | 
|---|
| 130 |  | 
|---|
| 131 |                 /** @remarks Returns the state of the touch - is valid for both buffered and non buffered mode */ | 
|---|
| 132 |                 std::vector<MultiTouchState> getMultiTouchStates() const { return mStates; } | 
|---|
| 133 |          | 
|---|
| 134 |         /** @remarks Returns the first n touch states.  Useful if you know your app only needs to  | 
|---|
| 135 |                 process n touches.  The return value is a vector to allow random access */ | 
|---|
| 136 |         const std::vector<MultiTouchState> getFirstNTouchStates(int n) { | 
|---|
| 137 |             std::vector<MultiTouchState> states; | 
|---|
| 138 |             for( unsigned int i = 0; i < mStates.size(); i++ ) { | 
|---|
| 139 |                 if(!(mStates[i].touchIsType(MT_None))) { | 
|---|
| 140 |                     states.push_back(mStates[i]); | 
|---|
| 141 |                 } | 
|---|
| 142 |             } | 
|---|
| 143 |             return states; | 
|---|
| 144 |         } | 
|---|
| 145 |  | 
|---|
| 146 |         /** @remarks Returns the first n touch states.  Useful if you know your app only needs to  | 
|---|
| 147 |          process n touches.  The return value is a vector to allow random access */ | 
|---|
| 148 |         const std::vector<MultiTouchState> getMultiTouchStatesOfType(MultiTypeEventTypeID type) { | 
|---|
| 149 |             std::vector<MultiTouchState> states; | 
|---|
| 150 |             for( unsigned int i = 0; i < mStates.size(); i++ ) { | 
|---|
| 151 |                 if(mStates[i].touchIsType(type)) { | 
|---|
| 152 |                     states.push_back(mStates[i]); | 
|---|
| 153 |                 } | 
|---|
| 154 |             } | 
|---|
| 155 |             return states; | 
|---|
| 156 |         } | 
|---|
| 157 |          | 
|---|
| 158 |         protected: | 
|---|
| 159 |                 MultiTouch(const std::string &vendor, bool buffered, int devID, InputManager* creator) | 
|---|
| 160 |                         : Object(vendor, OISMultiTouch, buffered, devID, creator), mListener(0) {} | 
|---|
| 161 |  | 
|---|
| 162 |                 //! The state of the touch device, implemented in a vector to store the state from each finger touch | 
|---|
| 163 |         std::vector<MultiTouchState> mStates; | 
|---|
| 164 |  | 
|---|
| 165 |                 //! Used for buffered/actionmapping callback | 
|---|
| 166 |                 MultiTouchListener *mListener; | 
|---|
| 167 |         }; | 
|---|
| 168 | } | 
|---|
| 169 | #endif | 
|---|