/src/ois/includes/OISMultiTouch.h

https://bitbucket.org/cabalistic/ogredeps/ · C Header · 169 lines · 84 code · 25 blank · 60 comment · 5 complexity · b23a0b33abf4a1a7a916e3633ad260ad MD5 · raw file

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