GestureSample.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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__)
  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. unsigned int fontSize = 18;
  79. _font->start();
  80. int y = 0;
  81. size_t count = 0;
  82. for (std::list<std::string>::const_iterator it = _eventLog.begin(); it != _eventLog.end(); ++it)
  83. {
  84. ++count;
  85. _font->drawText(it->c_str(), 0, y, fontColor, fontSize);
  86. y += fontSize;
  87. if (y > (int)getHeight())
  88. {
  89. _eventLog.resize(count);
  90. break;
  91. }
  92. }
  93. int x = getWidth() - 200;
  94. y = getHeight() - fontSize * 6;
  95. if (isGestureSupported(Gesture::GESTURE_TAP))
  96. {
  97. _font->drawText("Tap supported", x, y, fontColor, fontSize);
  98. y += fontSize;
  99. }
  100. if (isGestureSupported(Gesture::GESTURE_SWIPE))
  101. {
  102. _font->drawText("Swipe supported", x, y, fontColor, fontSize);
  103. y += fontSize;
  104. }
  105. if (isGestureSupported(Gesture::GESTURE_PINCH))
  106. {
  107. _font->drawText("Pinch supported", x, y, fontColor, fontSize);
  108. y += fontSize;
  109. }
  110. if (isGestureSupported(Gesture::GESTURE_LONG_TAP))
  111. {
  112. _font->drawText("Long tap supported", x, y, fontColor, fontSize);
  113. y += fontSize;
  114. }
  115. if (isGestureSupported(Gesture::GESTURE_DRAG))
  116. {
  117. _font->drawText("Drag supported", x, y, fontColor, fontSize);
  118. y += fontSize;
  119. }
  120. if (isGestureSupported(Gesture::GESTURE_DROP))
  121. {
  122. _font->drawText("Drop supported", x, y, fontColor, fontSize);
  123. y += fontSize;
  124. }
  125. _font->finish();
  126. }
  127. void GestureSample::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  128. {
  129. switch (evt)
  130. {
  131. case Touch::TOUCH_PRESS:
  132. if (x < 30 && y < 30)
  133. {
  134. displayKeyboard(true);
  135. }
  136. break;
  137. case Touch::TOUCH_RELEASE:
  138. break;
  139. case Touch::TOUCH_MOVE:
  140. break;
  141. };
  142. }
  143. void GestureSample::gestureSwipeEvent(int x, int y, int direction)
  144. {
  145. std::ostringstream convert;
  146. convert << "Swipe (";
  147. if (direction & Gesture::SWIPE_DIRECTION_UP)
  148. {
  149. convert << "U";
  150. }
  151. if (direction & Gesture::SWIPE_DIRECTION_DOWN)
  152. {
  153. convert << "D";
  154. }
  155. if (direction & Gesture::SWIPE_DIRECTION_LEFT)
  156. {
  157. convert << "L";
  158. }
  159. if (direction & Gesture::SWIPE_DIRECTION_RIGHT)
  160. {
  161. convert << "R";
  162. }
  163. convert << ") " << x << ", " << y;
  164. _eventLog.push_front(convert.str());
  165. }
  166. void GestureSample::gesturePinchEvent(int x, int y, float scale)
  167. {
  168. std::ostringstream convert;
  169. convert << "Pinch " << x << ", " << y << " scale(" << scale << ")";
  170. _eventLog.push_front(convert.str());
  171. }
  172. void GestureSample::gestureTapEvent(int x, int y)
  173. {
  174. std::ostringstream convert;
  175. convert << "Tap " << x << ", " << y;
  176. _eventLog.push_front(convert.str());
  177. }
  178. void GestureSample::gestureLongTapEvent(int x, int y, float duration)
  179. {
  180. std::ostringstream convert;
  181. convert << "Long tap " << x << ", " << y << " (" << duration << "ms)";
  182. _eventLog.push_front(convert.str());
  183. }
  184. void GestureSample::gestureDragEvent(int x, int y)
  185. {
  186. std::ostringstream convert;
  187. convert << "Drag " << x << ", " << y;
  188. _eventLog.push_front(convert.str());
  189. }
  190. void GestureSample::gestureDropEvent(int x, int y)
  191. {
  192. std::ostringstream convert;
  193. convert << "Drop " << x << ", " << y;
  194. _eventLog.push_front(convert.str());
  195. }