/src/ois/src/iphone/iPhoneMultiTouch.mm

https://bitbucket.org/cabalistic/ogredeps/ · Objective C++ · 203 lines · 144 code · 30 blank · 29 comment · 25 complexity · c246653704ca937f507e02939ed7fe2a 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. #include "iphone/iPhoneMultiTouch.h"
  18. #include "iphone/iPhoneInputManager.h"
  19. using namespace OIS;
  20. //-------------------------------------------------------------------//
  21. iPhoneMultiTouch::iPhoneMultiTouch( InputManager* creator, bool buffered )
  22. : MultiTouch(creator->inputSystemName(), buffered, 0, creator)
  23. {
  24. iPhoneInputManager *man = static_cast<iPhoneInputManager*>(mCreator);
  25. man->_setMultiTouchUsed(true);
  26. [man->_getDelegate() setTouchObject:this];
  27. }
  28. iPhoneMultiTouch::~iPhoneMultiTouch()
  29. {
  30. iPhoneInputManager *man = static_cast<iPhoneInputManager*>(mCreator);
  31. man->_setMultiTouchUsed(false);
  32. [man->_getDelegate() setTouchObject:nil];
  33. }
  34. void iPhoneMultiTouch::_initialize()
  35. {
  36. // mTempState.clear();
  37. }
  38. void iPhoneMultiTouch::setBuffered( bool buffered )
  39. {
  40. mBuffered = buffered;
  41. }
  42. void iPhoneMultiTouch::capture()
  43. {
  44. #if 0
  45. for( std::multiset<MultiTouchState *>::iterator i = mStates.begin(), e = mStates.end(); i != e; ++i )
  46. {
  47. // Clear the state first
  48. dynamic_cast<MultiTouchState *>(*i)->clear();
  49. if(mTempState.X.rel || mTempState.Y.rel || mTempState.Z.rel)
  50. {
  51. MultiTouchState *iState = dynamic_cast<MultiTouchState *>(*i);
  52. // NSLog(@"%i %i %i", mTempState.X.rel, mTempState.Y.rel, mTempState.Z.rel);
  53. // Set new relative motion values
  54. iState->X.rel = mTempState.X.rel;
  55. iState->Y.rel = mTempState.Y.rel;
  56. iState->Z.rel = mTempState.Z.rel;
  57. // Update absolute position
  58. iState->X.abs += mTempState.X.rel;
  59. iState->Y.abs += mTempState.Y.rel;
  60. if(iState->X.abs > iState->width)
  61. iState->X.abs = iState->width;
  62. else if(iState->X.abs < 0)
  63. iState->X.abs = 0;
  64. if(iState->Y.abs > iState->height)
  65. iState->Y.abs = iState->height;
  66. else if(iState->Y.abs < 0)
  67. iState->Y.abs = 0;
  68. iState->Z.abs += mTempState.Z.rel;
  69. //Fire off event
  70. if(mListener && mBuffered)
  71. mListener->touchMoved(MultiTouchEvent(this, *iState));
  72. }
  73. }
  74. mTempState.clear();
  75. #endif
  76. }
  77. void iPhoneMultiTouch::_touchBegan(UITouch *touch)
  78. {
  79. UIView *touchView = static_cast<iPhoneInputManager*>(mCreator)->_getDelegate();
  80. CGPoint location = [touch locationInView:touchView];
  81. CGFloat contentScale = 1.0;
  82. #if __IPHONE_4_0
  83. if([touchView respondsToSelector:@selector(contentScaleFactor)])
  84. contentScale = [touchView contentScaleFactor];
  85. #endif
  86. MultiTouchState newState;
  87. newState.X.abs = location.x * contentScale;
  88. newState.Y.abs = location.y * contentScale;
  89. newState.touchType |= 1 << MT_Pressed;
  90. if( mListener && mBuffered )
  91. {
  92. mListener->touchPressed(MultiTouchEvent(this, newState));
  93. }
  94. else
  95. {
  96. mStates.push_back(newState);
  97. }
  98. }
  99. void iPhoneMultiTouch::_touchEnded(UITouch *touch)
  100. {
  101. UIView *touchView = static_cast<iPhoneInputManager*>(mCreator)->_getDelegate();
  102. CGPoint location = [touch locationInView:touchView];
  103. CGFloat contentScale = 1.0;
  104. #if __IPHONE_4_0
  105. if([touchView respondsToSelector:@selector(contentScaleFactor)])
  106. contentScale = [touchView contentScaleFactor];
  107. #endif
  108. MultiTouchState newState;
  109. newState.X.abs = location.x * contentScale;
  110. newState.Y.abs = location.y * contentScale;
  111. newState.touchType |= 1 << MT_Released;
  112. if( mListener && mBuffered )
  113. {
  114. mListener->touchReleased(MultiTouchEvent(this, newState));
  115. }
  116. else
  117. {
  118. mStates.push_back(newState);
  119. }
  120. }
  121. void iPhoneMultiTouch::_touchMoved(UITouch *touch)
  122. {
  123. UIView *touchView = static_cast<iPhoneInputManager*>(mCreator)->_getDelegate();
  124. CGPoint location = [touch locationInView:touchView];
  125. CGPoint previousLocation = [touch previousLocationInView:touchView];
  126. CGFloat contentScale = 1.0;
  127. #if __IPHONE_4_0
  128. if([touchView respondsToSelector:@selector(contentScaleFactor)])
  129. contentScale = [touchView contentScaleFactor];
  130. #endif
  131. MultiTouchState newState;
  132. newState.X.rel = (location.x - previousLocation.x) * contentScale;
  133. newState.Y.rel = (location.y - previousLocation.y) * contentScale;
  134. newState.X.abs = location.x * contentScale;
  135. newState.Y.abs = location.y * contentScale;
  136. newState.touchType |= 1 << MT_Moved;
  137. if( mListener && mBuffered )
  138. {
  139. mListener->touchMoved(MultiTouchEvent(this, newState));
  140. }
  141. else
  142. {
  143. mStates.push_back(newState);
  144. }
  145. }
  146. void iPhoneMultiTouch::_touchCancelled(UITouch *touch)
  147. {
  148. UIView *touchView = static_cast<iPhoneInputManager*>(mCreator)->_getDelegate();
  149. CGPoint location = [touch locationInView:touchView];
  150. CGFloat contentScale = 1.0;
  151. #if __IPHONE_4_0
  152. if([touchView respondsToSelector:@selector(contentScaleFactor)])
  153. contentScale = [touchView contentScaleFactor];
  154. #endif
  155. MultiTouchState newState;
  156. newState.X.abs = location.x * contentScale;
  157. newState.Y.abs = location.y * contentScale;
  158. newState.touchType |= 1 << MT_Cancelled;
  159. if( mListener && mBuffered )
  160. {
  161. mListener->touchCancelled(MultiTouchEvent(this, newState));
  162. }
  163. else
  164. {
  165. mStates.push_back(newState);
  166. }
  167. }