JoystickControl.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #include "Base.h"
  2. #include "JoystickControl.h"
  3. namespace gameplay
  4. {
  5. JoystickControl::JoystickControl() : _radius(1.0f), _relative(true), _innerSize(NULL), _outerSize(NULL), _index(0)
  6. {
  7. }
  8. JoystickControl::~JoystickControl()
  9. {
  10. if (_innerSize)
  11. SAFE_DELETE(_innerSize);
  12. if (_outerSize)
  13. SAFE_DELETE(_outerSize);
  14. }
  15. JoystickControl* JoystickControl::create(const char* id, Theme::Style* style)
  16. {
  17. JoystickControl* joystickControl = new JoystickControl();
  18. joystickControl->_id = id ? id : "";
  19. joystickControl->initialize("Joystick", style, NULL);
  20. return joystickControl;
  21. }
  22. Control* JoystickControl::create(Theme::Style* style, Properties* properties)
  23. {
  24. JoystickControl* joystickControl = new JoystickControl();
  25. joystickControl->initialize("Joystick", style, properties);
  26. return joystickControl;
  27. }
  28. const Vector2& JoystickControl::getValue() const
  29. {
  30. return _value;
  31. }
  32. void JoystickControl::setInnerRegionSize(const Vector2& size)
  33. {
  34. if (_innerSize)
  35. _innerSize->set(size);
  36. }
  37. const Vector2& JoystickControl::getInnerRegionSize() const
  38. {
  39. if (_innerSize)
  40. return *_innerSize;
  41. else
  42. return Vector2::zero();
  43. }
  44. void JoystickControl::setOuterRegionSize(const Vector2& size)
  45. {
  46. if (_outerSize)
  47. _outerSize->set(size);
  48. }
  49. const Vector2& JoystickControl::getOuterRegionSize() const
  50. {
  51. if (_outerSize)
  52. return *_outerSize;
  53. else
  54. return Vector2::zero();
  55. }
  56. void JoystickControl::setRelative(bool relative)
  57. {
  58. _relative = relative;
  59. }
  60. bool JoystickControl::isRelative() const
  61. {
  62. return _relative;
  63. }
  64. unsigned int JoystickControl::getIndex() const
  65. {
  66. return _index;
  67. }
  68. void JoystickControl::initialize(const char* typeName, Theme::Style* style, Properties* properties)
  69. {
  70. Control::initialize(typeName, style, properties);
  71. if (!properties)
  72. {
  73. GP_WARN("JoystickControl creation without properties object is unsupported.");
  74. return;
  75. }
  76. Control::State state = getState();
  77. if (!properties->exists("radius"))
  78. {
  79. GP_WARN("JoystickControl: required attribute 'radius' is missing.");
  80. }
  81. else
  82. {
  83. _radius = properties->getFloat("radius");
  84. if (_radius < 1.0f)
  85. _radius = 1.0f;
  86. }
  87. if (properties->exists("relative"))
  88. {
  89. setRelative(properties->getBool("relative"));
  90. }
  91. else
  92. {
  93. setRelative(false);
  94. }
  95. Theme::ThemeImage* inner = getImage("inner", state);
  96. if (inner)
  97. {
  98. _innerSize = new Vector2();
  99. Vector2 innerSize;
  100. if (properties->getVector2("innerRegion", &innerSize))
  101. {
  102. _innerSize->set(innerSize.x, innerSize.y);
  103. }
  104. else
  105. {
  106. const Rectangle& rect = inner->getRegion();
  107. _innerSize->set(rect.width, rect.height);
  108. }
  109. }
  110. Theme::ThemeImage* outer = getImage("outer", state);
  111. if (outer)
  112. {
  113. _outerSize = new Vector2();
  114. Vector2 outerSize;
  115. if (properties->getVector2("outerRegion", &outerSize))
  116. {
  117. _outerSize->set(outerSize.x, outerSize.y);
  118. }
  119. else
  120. {
  121. const Rectangle& rect = outer->getRegion();
  122. _outerSize->set(rect.width, rect.height);
  123. }
  124. _screenRegion.width = _outerSize->x;
  125. _screenRegion.height = _outerSize->y;
  126. }
  127. else
  128. {
  129. if (inner)
  130. {
  131. const Rectangle& rect = inner->getRegion();
  132. _screenRegion.width = rect.width;
  133. _screenRegion.height = rect.height;
  134. }
  135. else
  136. {
  137. _screenRegion.width = _radius * 2.0f;
  138. _screenRegion.height = _screenRegion.width;
  139. }
  140. }
  141. _index = properties->getInt("index");
  142. }
  143. void JoystickControl::addListener(Control::Listener* listener, int eventFlags)
  144. {
  145. if ((eventFlags & Control::Listener::TEXT_CHANGED) == Control::Listener::TEXT_CHANGED)
  146. {
  147. GP_ERROR("TEXT_CHANGED event is not applicable to this control.");
  148. }
  149. Control::addListener(listener, eventFlags);
  150. }
  151. bool JoystickControl::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  152. {
  153. switch (evt)
  154. {
  155. case Touch::TOUCH_PRESS:
  156. {
  157. if (_contactIndex == INVALID_CONTACT_INDEX)
  158. {
  159. float dx = 0.0f;
  160. float dy = 0.0f;
  161. _contactIndex = (int)contactIndex;
  162. // Get the displacement of the touch from the centre.
  163. if (!_relative)
  164. {
  165. dx = x - _screenRegion.width * 0.5f;
  166. dy = _screenRegion.height * 0.5f - y;
  167. }
  168. else
  169. {
  170. _screenRegion.x = x + _bounds.x - _screenRegion.width * 0.5f;
  171. _screenRegion.y = y + _bounds.y - _screenRegion.height * 0.5f;
  172. }
  173. _displacement.set(dx, dy);
  174. // If the displacement is greater than the radius, then cap the displacement to the
  175. // radius.
  176. Vector2 value;
  177. if ((fabs(_displacement.x) > _radius) || (fabs(_displacement.y) > _radius))
  178. {
  179. _displacement.normalize();
  180. value.set(_displacement);
  181. _displacement.scale(_radius);
  182. }
  183. else
  184. {
  185. value.set(_displacement);
  186. GP_ASSERT(_radius);
  187. value.scale(1.0f / _radius);
  188. }
  189. // Check if the value has changed. Won't this always be the case?
  190. if (_value != value)
  191. {
  192. _value.set(value);
  193. _dirty = true;
  194. notifyListeners(Control::Listener::VALUE_CHANGED);
  195. }
  196. return true;
  197. }
  198. break;
  199. }
  200. case Touch::TOUCH_MOVE:
  201. {
  202. if (_contactIndex == (int) contactIndex)
  203. {
  204. float dx = x - ((_relative) ? _screenRegion.x - _bounds.x : 0.0f) - _screenRegion.width * 0.5f;
  205. float dy = -(y - ((_relative) ? _screenRegion.y - _bounds.y : 0.0f) - _screenRegion.height * 0.5f);
  206. _displacement.set(dx, dy);
  207. Vector2 value;
  208. if ((fabs(_displacement.x) > _radius) || (fabs(_displacement.y) > _radius))
  209. {
  210. _displacement.normalize();
  211. value.set(_displacement);
  212. _displacement.scale(_radius);
  213. }
  214. else
  215. {
  216. value.set(_displacement);
  217. GP_ASSERT(_radius);
  218. value.scale(1.0f / _radius);
  219. }
  220. if (_value != value)
  221. {
  222. _value.set(value);
  223. _dirty = true;
  224. notifyListeners(Control::Listener::VALUE_CHANGED);
  225. }
  226. return true;
  227. }
  228. break;
  229. }
  230. case Touch::TOUCH_RELEASE:
  231. {
  232. if (_contactIndex == (int) contactIndex)
  233. {
  234. _contactIndex = INVALID_CONTACT_INDEX;
  235. // Reset displacement and direction vectors.
  236. _displacement.set(0.0f, 0.0f);
  237. Vector2 value(_displacement);
  238. if (_value != value)
  239. {
  240. _value.set(value);
  241. _dirty = true;
  242. notifyListeners(Control::Listener::VALUE_CHANGED);
  243. }
  244. return true;
  245. }
  246. break;
  247. }
  248. }
  249. return Control::touchEvent(evt, x, y, contactIndex);
  250. }
  251. unsigned int JoystickControl::drawImages(Form* form, const Rectangle& clip)
  252. {
  253. Control::State state = getState();
  254. unsigned int drawCalls = 0;
  255. // If the JoystickControl is not absolute, then only draw if it is active.
  256. if (!_relative || (_relative && state == ACTIVE))
  257. {
  258. if (!_relative)
  259. {
  260. _screenRegion.x = _viewportClipBounds.x + (_viewportClipBounds.width - _screenRegion.width) / 2.0f;
  261. _screenRegion.y = _viewportClipBounds.y + (_viewportClipBounds.height - _screenRegion.height) / 2.0f;
  262. }
  263. SpriteBatch* batch = _style->getTheme()->getSpriteBatch();
  264. startBatch(form, batch);
  265. // Draw the outer image.
  266. Theme::ThemeImage* outer = getImage("outer", state);
  267. if (outer)
  268. {
  269. const Theme::UVs& uvs = outer->getUVs();
  270. const Vector4& color = outer->getColor();
  271. if (_relative)
  272. batch->draw(_screenRegion.x, _screenRegion.y, _outerSize->x, _outerSize->y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color);
  273. else
  274. batch->draw(_screenRegion.x, _screenRegion.y, _outerSize->x, _outerSize->y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
  275. ++drawCalls;
  276. }
  277. // Draw the inner image.
  278. Theme::ThemeImage* inner = getImage("inner", state);
  279. if (inner)
  280. {
  281. Vector2 position(_screenRegion.x, _screenRegion.y);
  282. // Adjust position to reflect displacement.
  283. position.x += _displacement.x;
  284. position.y += -_displacement.y;
  285. // Get the uvs and color and draw.
  286. const Theme::UVs& uvs = inner->getUVs();
  287. const Vector4& color = inner->getColor();
  288. if (_relative)
  289. batch->draw(position.x, position.y, _innerSize->x, _innerSize->y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color);
  290. else
  291. batch->draw(position.x, position.y, _innerSize->x, _innerSize->y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
  292. ++drawCalls;
  293. }
  294. finishBatch(form, batch);
  295. }
  296. return drawCalls;
  297. }
  298. const char* JoystickControl::getType() const
  299. {
  300. return "joystick";
  301. }
  302. }