Control.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "Base.h"
  2. #include "Control.h"
  3. namespace gameplay
  4. {
  5. Control::Control()
  6. : _id(""), _state(Control::STATE_NORMAL), _size(Vector2::zero()), _position(Vector2::zero()), _border(Vector2::zero()), _padding(Vector2::zero()),
  7. _autoWidth(true), _autoHeight(true), _dirty(true), _consumeTouchEvents(true)
  8. {
  9. }
  10. Control::Control(const Control& copy)
  11. {
  12. }
  13. Control::~Control()
  14. {
  15. }
  16. void Control::init(Theme::Style* style, Properties* properties)
  17. {
  18. _style = style;
  19. properties->getVector2("position", &_position);
  20. properties->getVector2("size", &_size);
  21. _state = Control::getStateFromString(properties->getString("state"));
  22. const char* id = properties->getId();
  23. if (id)
  24. {
  25. _id = id;
  26. }
  27. }
  28. const char* Control::getID()
  29. {
  30. return _id.c_str();
  31. }
  32. const Rectangle Control::getBounds(bool includePadding) const
  33. {
  34. // TODO
  35. return Rectangle();
  36. }
  37. void Control::setPosition(float x, float y)
  38. {
  39. _position.set(x, y);
  40. }
  41. const Vector2& Control::getPosition() const
  42. {
  43. return _position;
  44. }
  45. void Control::setSize(float width, float height)
  46. {
  47. _size.set(width, height);
  48. }
  49. const Vector2& Control::getSize() const
  50. {
  51. return _size;
  52. }
  53. void Control::setAutoSize(bool width, bool height)
  54. {
  55. _autoWidth = width;
  56. _autoHeight = height;
  57. }
  58. void Control::setStyle(Theme::Style* style)
  59. {
  60. _style = style;
  61. }
  62. Theme::Style* Control::getStyle() const
  63. {
  64. return _style;
  65. }
  66. void Control::setState(State state)
  67. {
  68. _state = state;
  69. }
  70. Control::State Control::getState()
  71. {
  72. return _state;
  73. }
  74. void Control::disable()
  75. {
  76. _state = STATE_DISABLED;
  77. }
  78. void Control::enable()
  79. {
  80. _state = STATE_NORMAL;
  81. }
  82. bool Control::isEnabled()
  83. {
  84. return _state != STATE_DISABLED;
  85. }
  86. Theme::Style::OverlayType Control::getOverlayType() const
  87. {
  88. switch (_state)
  89. {
  90. case Control::STATE_NORMAL:
  91. return Theme::Style::OVERLAY_NORMAL;
  92. case Control::STATE_FOCUS:
  93. return Theme::Style::OVERLAY_FOCUS;
  94. case Control::STATE_ACTIVE:
  95. return Theme::Style::OVERLAY_ACTIVE;
  96. case Control::STATE_DISABLED:
  97. return Theme::Style::OVERLAY_DISABLED;
  98. default:
  99. return Theme::Style::OVERLAY_NORMAL;
  100. }
  101. }
  102. void Control::setConsumeTouchEvents(bool consume)
  103. {
  104. _consumeTouchEvents = consume;
  105. }
  106. bool Control::getConsumeTouchEvents()
  107. {
  108. return _consumeTouchEvents;
  109. }
  110. bool Control::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  111. {
  112. // Empty stub to be implemented by Button and its descendents.
  113. return _consumeTouchEvents;
  114. }
  115. void Control::keyEvent(Keyboard::KeyEvent evt, int key)
  116. {
  117. }
  118. void Control::update(const Vector2& position)
  119. {
  120. }
  121. void Control::drawBorder(SpriteBatch* spriteBatch, const Vector2& position)
  122. {
  123. Vector2 pos(position.x + _position.x, position.y + _position.y);
  124. // Get the border and background images for this control's current state.
  125. Theme::ContainerRegion* containerRegion = _style->getOverlay(getOverlayType())->getContainerRegion();
  126. if (containerRegion)
  127. {
  128. // Get the UVs.
  129. Theme::UVs topLeft, top, topRight, left, center, right, bottomLeft, bottom, bottomRight;
  130. topLeft = containerRegion->getUVs(Theme::ContainerRegion::TOP_LEFT);
  131. top = containerRegion->getUVs(Theme::ContainerRegion::TOP);
  132. topRight = containerRegion->getUVs(Theme::ContainerRegion::TOP_RIGHT);
  133. left = containerRegion->getUVs(Theme::ContainerRegion::LEFT);
  134. center = containerRegion->getUVs(Theme::ContainerRegion::CENTER);
  135. right = containerRegion->getUVs(Theme::ContainerRegion::RIGHT);
  136. bottomLeft = containerRegion->getUVs(Theme::ContainerRegion::BOTTOM_LEFT);
  137. bottom = containerRegion->getUVs(Theme::ContainerRegion::BOTTOM);
  138. bottomRight = containerRegion->getUVs(Theme::ContainerRegion::BOTTOM_RIGHT);
  139. // Calculate screen-space positions.
  140. Theme::Border border = containerRegion->getBorder();
  141. Theme::Padding padding = _style->getPadding();
  142. Vector4 borderColor = containerRegion->getColor();
  143. float midWidth = _size.x - border.left - border.right;
  144. float midHeight = _size.y - border.top - border.bottom;
  145. float midX = pos.x + border.left;
  146. float midY = pos.y + border.top;
  147. float rightX = pos.x + _size.x - border.right;
  148. float bottomY = pos.y + _size.y - border.bottom;
  149. // Draw themed border sprites.
  150. if (!border.left && !border.right && !border.top && !border.bottom)
  151. {
  152. // No border, just draw the image.
  153. spriteBatch->draw(pos.x, pos.y, _size.x, _size.y, center.u1, center.v1, center.u2, center.v2, borderColor);
  154. }
  155. else
  156. {
  157. if (border.left && border.top)
  158. spriteBatch->draw(pos.x, pos.y, border.left, border.top, topLeft.u1, topLeft.v1, topLeft.u2, topLeft.v2, borderColor);
  159. if (border.top)
  160. spriteBatch->draw(pos.x + border.left, pos.y, midWidth, border.top, top.u1, top.v1, top.u2, top.v2, borderColor);
  161. if (border.right && border.top)
  162. spriteBatch->draw(rightX, pos.y, border.right, border.top, topRight.u1, topRight.v1, topRight.u2, topRight.v2, borderColor);
  163. if (border.left)
  164. spriteBatch->draw(pos.x, midY, border.left, midHeight, left.u1, left.v1, left.u2, left.v2, borderColor);
  165. if (border.left && border.right && border.top && border.bottom)
  166. spriteBatch->draw(pos.x + border.left, pos.y + border.top, _size.x - border.left - border.right, _size.y - border.top - border.bottom,
  167. center.u1, center.v1, center.u2, center.v2, borderColor);
  168. if (border.right)
  169. spriteBatch->draw(rightX, midY, border.right, midHeight, right.u1, right.v1, right.u2, right.v2, borderColor);
  170. if (border.bottom && border.left)
  171. spriteBatch->draw(pos.x, bottomY, border.left, border.bottom, bottomLeft.u1, bottomLeft.v1, bottomLeft.u2, bottomLeft.v2, borderColor);
  172. if (border.bottom)
  173. spriteBatch->draw(midX, bottomY, midWidth, border.bottom, bottom.u1, bottom.v1, bottom.u2, bottom.v2, borderColor);
  174. if (border.bottom && border.right)
  175. spriteBatch->draw(rightX, bottomY, border.right, border.bottom, bottomRight.u1, bottomRight.v1, bottomRight.u2, bottomRight.v2, borderColor);
  176. }
  177. }
  178. }
  179. void Control::drawSprites(SpriteBatch* spriteBatch, const Vector2& position)
  180. {
  181. }
  182. void Control::drawText(const Vector2& position)
  183. {
  184. }
  185. bool Control::isDirty()
  186. {
  187. return _dirty;
  188. }
  189. Control::State Control::getStateFromString(const char* state)
  190. {
  191. if (!state)
  192. {
  193. return STATE_NORMAL;
  194. }
  195. if (strcmp(state, "STATE_NORMAL") == 0)
  196. {
  197. return STATE_NORMAL;
  198. }
  199. else if (strcmp(state, "STATE_ACTIVE") == 0)
  200. {
  201. return STATE_ACTIVE;
  202. }
  203. else if (strcmp(state, "STATE_FOCUS") == 0)
  204. {
  205. return STATE_FOCUS;
  206. }
  207. else if (strcmp(state, "STATE_DISABLED") == 0)
  208. {
  209. return STATE_DISABLED;
  210. }
  211. return STATE_NORMAL;
  212. }
  213. }