GestureSample.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/common/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. GP_ASSERT(anySupported == isGestureSupported(Gesture::GESTURE_ANY_SUPPORTED));
  40. }
  41. void GestureSample::finalize()
  42. {
  43. SAFE_RELEASE(_font);
  44. unregisterGesture(Gesture::GESTURE_TAP);
  45. unregisterGesture(Gesture::GESTURE_SWIPE);
  46. unregisterGesture(Gesture::GESTURE_PINCH);
  47. }
  48. void GestureSample::update(float elapsedTime)
  49. {
  50. }
  51. void GestureSample::render(float elapsedTime)
  52. {
  53. // Clear the color and depth buffers
  54. clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0);
  55. // Draw text
  56. Vector4 fontColor(1.0f, 1.0f, 1.0f, 1.0f);
  57. _font->start();
  58. int y = 0;
  59. size_t count = 0;
  60. for (std::list<std::string>::const_iterator it = _eventLog.begin(); it != _eventLog.end(); ++it)
  61. {
  62. ++count;
  63. _font->drawText(it->c_str(), 0, y, fontColor, _font->getSize());
  64. y += _font->getSize();
  65. if (y > (int)getHeight())
  66. {
  67. _eventLog.resize(count);
  68. break;
  69. }
  70. }
  71. int x = getWidth() - 200;
  72. y = getHeight() - _font->getSize() * 3;
  73. if (isGestureSupported(Gesture::GESTURE_TAP))
  74. {
  75. _font->drawText("Tap supported", x, y, fontColor, _font->getSize());
  76. y += _font->getSize();
  77. }
  78. if (isGestureSupported(Gesture::GESTURE_SWIPE))
  79. {
  80. _font->drawText("Swipe supported", x, y, fontColor, _font->getSize());
  81. y += _font->getSize();
  82. }
  83. if (isGestureSupported(Gesture::GESTURE_PINCH))
  84. {
  85. _font->drawText("Pinch supported", x, y, fontColor, _font->getSize());
  86. y += _font->getSize();
  87. }
  88. _font->finish();
  89. }
  90. void GestureSample::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  91. {
  92. switch (evt)
  93. {
  94. case Touch::TOUCH_PRESS:
  95. if (x < 30 && y < 30)
  96. {
  97. displayKeyboard(true);
  98. }
  99. break;
  100. case Touch::TOUCH_RELEASE:
  101. break;
  102. case Touch::TOUCH_MOVE:
  103. break;
  104. };
  105. }
  106. void GestureSample::gestureSwipeEvent(int x, int y, int direction)
  107. {
  108. std::ostringstream convert;
  109. convert << "Swipe (";
  110. if (direction & Gesture::SWIPE_DIRECTION_UP)
  111. {
  112. convert << "U";
  113. }
  114. if (direction & Gesture::SWIPE_DIRECTION_DOWN)
  115. {
  116. convert << "D";
  117. }
  118. if (direction & Gesture::SWIPE_DIRECTION_LEFT)
  119. {
  120. convert << "L";
  121. }
  122. if (direction & Gesture::SWIPE_DIRECTION_RIGHT)
  123. {
  124. convert << "R";
  125. }
  126. convert << ") " << x << ", " << y;
  127. _eventLog.push_front(convert.str());
  128. }
  129. void GestureSample::gesturePinchEvent(int x, int y, float scale)
  130. {
  131. std::ostringstream convert;
  132. convert << "Pinch " << x << ", " << y << " scale(" << scale << ")";
  133. _eventLog.push_front(convert.str());
  134. }
  135. void GestureSample::gestureTapEvent(int x, int y)
  136. {
  137. std::ostringstream convert;
  138. convert << "Tap " << x << ", " << y;
  139. _eventLog.push_front(convert.str());
  140. }