Joystick.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "Base.h"
  2. #include "Joystick.h"
  3. namespace gameplay
  4. {
  5. Joystick::Joystick() : _absolute(true)
  6. {
  7. }
  8. Joystick::Joystick(const Joystick& copy)
  9. {
  10. }
  11. Joystick::~Joystick()
  12. {
  13. }
  14. Joystick* Joystick::create(const char* id, Theme::Style* style)
  15. {
  16. GP_ASSERT(style);
  17. Joystick* joystick = new Joystick();
  18. if (id)
  19. joystick->_id = id;
  20. joystick->setStyle(style);
  21. return joystick;
  22. }
  23. Joystick* Joystick::create(Theme::Style* style, Properties* properties)
  24. {
  25. Joystick* joystick = new Joystick();
  26. joystick->initialize(style, properties);
  27. joystick->_consumeInputEvents = false;
  28. return joystick;
  29. }
  30. void Joystick::initialize(Theme::Style* style, Properties* properties)
  31. {
  32. GP_ASSERT(properties);
  33. Control::initialize(style, properties);
  34. if (!properties->exists("radius"))
  35. {
  36. GP_ERROR("Failed to load joystick; required attribute 'radius' is missing.");
  37. return;
  38. }
  39. _radius = properties->getFloat("radius");
  40. if (properties->exists("absolute"))
  41. {
  42. setAbsolute(properties->getBool("absolute"));
  43. }
  44. Vector4 v;
  45. if (properties->getVector4("region", &v))
  46. {
  47. _absolute = false;
  48. _region = _bounds;
  49. _bounds.x = v.x;
  50. _bounds.y = v.y;
  51. _bounds.width = v.z;
  52. _bounds.height = v.w;
  53. }
  54. }
  55. void Joystick::addListener(Control::Listener* listener, int eventFlags)
  56. {
  57. if ((eventFlags & Listener::TEXT_CHANGED) == Listener::TEXT_CHANGED)
  58. {
  59. GP_ERROR("TEXT_CHANGED event is not applicable to this control.");
  60. }
  61. _consumeInputEvents = true;
  62. Control::addListener(listener, eventFlags);
  63. }
  64. bool Joystick::touchEvent(Touch::TouchEvent touchEvent, int x, int y, unsigned int contactIndex)
  65. {
  66. switch (touchEvent)
  67. {
  68. case Touch::TOUCH_PRESS:
  69. {
  70. float dx = 0.0f;
  71. float dy = 0.0f;
  72. if (_absolute)
  73. {
  74. dx = x - _bounds.width * 0.5f;
  75. dy = _bounds.height * 0.5f - y;
  76. }
  77. else
  78. {
  79. _region.x = x + _bounds.x - _region.width * 0.5f;
  80. _region.y = y + _bounds.y - _region.height * 0.5f;
  81. }
  82. if ((dx >= -_radius && dx <= _radius) && (dy >= -_radius && dy <= _radius))
  83. {
  84. _contactIndex = (int) contactIndex;
  85. notifyListeners(Listener::PRESS);
  86. _displacement.set(0.0f, 0.0f);
  87. Vector2 value(0.0f, 0.0f);
  88. if (_value != value)
  89. {
  90. _value.set(value);
  91. _dirty = true;
  92. notifyListeners(Listener::VALUE_CHANGED);
  93. }
  94. _state = ACTIVE;
  95. return _consumeInputEvents;
  96. }
  97. else
  98. {
  99. _state = NORMAL;
  100. }
  101. break;
  102. }
  103. case Touch::TOUCH_MOVE:
  104. {
  105. float dx = x - ((!_absolute) ? _region.x - _bounds.x : 0.0f) - _region.width * 0.5f;
  106. float dy = -(y - ((!_absolute) ? _region.y - _bounds.y : 0.0f) - _region.height * 0.5f);
  107. if (((dx * dx) + (dy * dy)) <= (_radius * _radius))
  108. {
  109. GP_ASSERT(_radius);
  110. Vector2 value(dx, dy);
  111. value.scale(1.0f / _radius);
  112. if (_value != value)
  113. {
  114. _value.set(value);
  115. _dirty = true;
  116. notifyListeners(Listener::VALUE_CHANGED);
  117. }
  118. }
  119. else
  120. {
  121. Vector2 value(dx, dy);
  122. value.normalize();
  123. value.scale(_radius);
  124. value.normalize();
  125. if (_value != value)
  126. {
  127. _value.set(value);
  128. _dirty = true;
  129. notifyListeners(Listener::VALUE_CHANGED);
  130. }
  131. }
  132. _displacement.set(dx, dy);
  133. return _consumeInputEvents;
  134. }
  135. case Touch::TOUCH_RELEASE:
  136. {
  137. _contactIndex = INVALID_CONTACT_INDEX;
  138. notifyListeners(Listener::RELEASE);
  139. // Reset displacement and direction vectors.
  140. _displacement.set(0.0f, 0.0f);
  141. Vector2 value(0.0f, 0.0f);
  142. if (_value != value)
  143. {
  144. _value.set(value);
  145. _dirty = true;
  146. notifyListeners(Listener::VALUE_CHANGED);
  147. }
  148. _state = NORMAL;
  149. return _consumeInputEvents;
  150. }
  151. }
  152. return false;
  153. }
  154. void Joystick::update(const Control* container, const Vector2& offset)
  155. {
  156. Control::update(container, offset);
  157. _clearBounds.x -= _radius;
  158. _clearBounds.y -= _radius;
  159. float radiusx2 = _radius + _radius;
  160. _clearBounds.width += radiusx2;
  161. _clearBounds.height += radiusx2;
  162. }
  163. void Joystick::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  164. {
  165. GP_ASSERT(spriteBatch);
  166. spriteBatch->begin();
  167. // If the joystick is not absolute, then only draw if it is active.
  168. if (_absolute || (!_absolute && _state == ACTIVE))
  169. {
  170. if (_absolute)
  171. _region = _bounds;
  172. // Draw the outer image.
  173. Theme::ThemeImage* outer = getImage("outer", _state);
  174. if (outer)
  175. {
  176. // Get the uvs and color and draw.
  177. const Theme::UVs& uvs = outer->getUVs();
  178. const Vector4& color = outer->getColor();
  179. spriteBatch->draw(_region.x, _region.y, _region.width, _region.height, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color);
  180. }
  181. // Draw the inner image.
  182. Theme::ThemeImage* inner = getImage("inner", _state);
  183. if (inner)
  184. {
  185. Rectangle region = _region;
  186. // Adjust position to reflect displacement.
  187. if (((_displacement.x * _displacement.x) + (_displacement.y * _displacement.y)) <= (_radius * _radius))
  188. {
  189. region.x += _displacement.x;
  190. region.y += -_displacement.y;
  191. }
  192. else
  193. {
  194. // Find the point on the joystick's circular bound where the
  195. // vector intersects. This is the position of the inner image.
  196. Vector2 delta = Vector2(_displacement.x, -_displacement.y);
  197. delta.normalize();
  198. delta.scale(_radius);
  199. region.x += delta.x;
  200. region.y += delta.y;
  201. }
  202. // Get the uvs and color and draw.
  203. const Theme::UVs& uvs = inner->getUVs();
  204. const Vector4& color = inner->getColor();
  205. spriteBatch->draw(region.x, region.y, _region.width, _region.height, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color);
  206. }
  207. }
  208. spriteBatch->end();
  209. }
  210. const char* Joystick::getType() const
  211. {
  212. return "joystick";
  213. }
  214. }