GestureSample.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "GestureSample.h"
  2. #include "SamplesGame.h"
  3. #include <sstream>
  4. // Linux and windows don't support gestures right now
  5. #if defined(__ANDROID__) || defined(__APPLE__) || defined(__QNX__)
  6. #if defined(ADD_SAMPLE)
  7. ADD_SAMPLE("Input", "Gestures", GestureSample, 2);
  8. #endif
  9. #endif
  10. GestureSample::GestureSample()
  11. : _font(NULL)
  12. {
  13. }
  14. void GestureSample::initialize()
  15. {
  16. setMultiTouch(true);
  17. // Load font
  18. _font = Font::create("res/ui/arial.gpb");
  19. assert(_font);
  20. bool anySupported = false;
  21. if (isGestureSupported(Gesture::GESTURE_TAP))
  22. {
  23. anySupported = true;
  24. registerGesture(Gesture::GESTURE_TAP);
  25. GP_ASSERT(isGestureRegistered(Gesture::GESTURE_TAP));
  26. }
  27. if (isGestureSupported(Gesture::GESTURE_SWIPE))
  28. {
  29. anySupported = true;
  30. registerGesture(Gesture::GESTURE_SWIPE);
  31. GP_ASSERT(isGestureRegistered(Gesture::GESTURE_SWIPE));
  32. }
  33. if (isGestureSupported(Gesture::GESTURE_PINCH))
  34. {
  35. anySupported = true;
  36. registerGesture(Gesture::GESTURE_PINCH);
  37. GP_ASSERT(isGestureRegistered(Gesture::GESTURE_PINCH));
  38. }
  39. if (isGestureSupported(Gesture::GESTURE_LONG_TAP))
  40. {
  41. anySupported = true;
  42. registerGesture(Gesture::GESTURE_LONG_TAP);
  43. GP_ASSERT(isGestureRegistered(Gesture::GESTURE_LONG_TAP));
  44. }
  45. if (isGestureSupported(Gesture::GESTURE_DRAG))
  46. {
  47. anySupported = true;
  48. registerGesture(Gesture::GESTURE_DRAG);
  49. GP_ASSERT(isGestureRegistered(Gesture::GESTURE_DRAG));
  50. }
  51. if (isGestureSupported(Gesture::GESTURE_DROP))
  52. {
  53. anySupported = true;
  54. registerGesture(Gesture::GESTURE_DROP);
  55. GP_ASSERT(isGestureRegistered(Gesture::GESTURE_DROP));
  56. }
  57. GP_ASSERT(anySupported == isGestureSupported(Gesture::GESTURE_ANY_SUPPORTED));
  58. }
  59. void GestureSample::finalize()
  60. {
  61. SAFE_RELEASE(_font);
  62. unregisterGesture(Gesture::GESTURE_TAP);
  63. unregisterGesture(Gesture::GESTURE_SWIPE);
  64. unregisterGesture(Gesture::GESTURE_PINCH);
  65. unregisterGesture(Gesture::GESTURE_LONG_TAP);
  66. unregisterGesture(Gesture::GESTURE_DRAG);
  67. unregisterGesture(Gesture::GESTURE_DROP);
  68. }
  69. void GestureSample::update(float elapsedTime)
  70. {
  71. }
  72. void GestureSample::render(float elapsedTime)
  73. {
  74. // Clear the color and depth buffers
  75. clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0);
  76. // Draw text
  77. Vector4 fontColor(1.0f, 1.0f, 1.0f, 1.0f);
  78. _font->start();
  79. int y = 0;
  80. size_t count = 0;
  81. for (std::list<std::string>::const_iterator it = _eventLog.begin(); it != _eventLog.end(); ++it)
  82. {
  83. ++count;
  84. _font->drawText(it->c_str(), 0, y, fontColor, _font->getSize());
  85. y += _font->getSize();
  86. if (y > (int)getHeight())
  87. {
  88. _eventLog.resize(count);
  89. break;
  90. }
  91. }
  92. int x = getWidth() - 200;
  93. y = getHeight() - _font->getSize() * 6;
  94. if (isGestureSupported(Gesture::GESTURE_TAP))
  95. {
  96. _font->drawText("Tap supported", x, y, fontColor, _font->getSize());
  97. y += _font->getSize();
  98. }
  99. if (isGestureSupported(Gesture::GESTURE_SWIPE))
  100. {
  101. _font->drawText("Swipe supported", x, y, fontColor, _font->getSize());
  102. y += _font->getSize();
  103. }
  104. if (isGestureSupported(Gesture::GESTURE_PINCH))
  105. {
  106. _font->drawText("Pinch supported", x, y, fontColor, _font->getSize());
  107. y += _font->getSize();
  108. }
  109. if (isGestureSupported(Gesture::GESTURE_LONG_TAP))
  110. {
  111. _font->drawText("Long tap supported", x, y, fontColor, _font->getSize());
  112. y += _font->getSize();
  113. }
  114. if (isGestureSupported(Gesture::GESTURE_DRAG))
  115. {
  116. _font->drawText("Drag supported", x, y, fontColor, _font->getSize());
  117. y += _font->getSize();
  118. }
  119. if (isGestureSupported(Gesture::GESTURE_DROP))
  120. {
  121. _font->drawText("Drop supported", x, y, fontColor, _font->getSize());
  122. y += _font->getSize();
  123. }
  124. _font->finish();
  125. }
  126. void GestureSample::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  127. {
  128. switch (evt)
  129. {
  130. case Touch::TOUCH_PRESS:
  131. if (x < 30 && y < 30)
  132. {
  133. displayKeyboard(true);
  134. }
  135. break;
  136. case Touch::TOUCH_RELEASE:
  137. break;
  138. case Touch::TOUCH_MOVE:
  139. break;
  140. };
  141. }
  142. void GestureSample::gestureSwipeEvent(int x, int y, int direction)
  143. {
  144. std::ostringstream convert;
  145. convert << "Swipe (";
  146. if (direction & Gesture::SWIPE_DIRECTION_UP)
  147. {
  148. convert << "U";
  149. }
  150. if (direction & Gesture::SWIPE_DIRECTION_DOWN)
  151. {
  152. convert << "D";
  153. }
  154. if (direction & Gesture::SWIPE_DIRECTION_LEFT)
  155. {
  156. convert << "L";
  157. }
  158. if (direction & Gesture::SWIPE_DIRECTION_RIGHT)
  159. {
  160. convert << "R";
  161. }
  162. convert << ") " << x << ", " << y;
  163. _eventLog.push_front(convert.str());
  164. }
  165. void GestureSample::gesturePinchEvent(int x, int y, float scale)
  166. {
  167. std::ostringstream convert;
  168. convert << "Pinch " << x << ", " << y << " scale(" << scale << ")";
  169. _eventLog.push_front(convert.str());
  170. }
  171. void GestureSample::gestureTapEvent(int x, int y)
  172. {
  173. std::ostringstream convert;
  174. convert << "Tap " << x << ", " << y;
  175. _eventLog.push_front(convert.str());
  176. }
  177. void GestureSample::gestureLongTapEvent(int x, int y, float duration)
  178. {
  179. std::ostringstream convert;
  180. convert << "Long tap " << x << ", " << y << " (" << duration << "ms)";
  181. _eventLog.push_front(convert.str());
  182. }
  183. void GestureSample::gestureDragEvent(int x, int y)
  184. {
  185. std::ostringstream convert;
  186. convert << "Drag " << x << ", " << y;
  187. _eventLog.push_front(convert.str());
  188. }
  189. void GestureSample::gestureDropEvent(int x, int y)
  190. {
  191. std::ostringstream convert;
  192. convert << "Drop " << x << ", " << y;
  193. _eventLog.push_front(convert.str());
  194. }