Control.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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)
  8. {
  9. }
  10. Control::Control(const Control& copy)
  11. {
  12. }
  13. Control::~Control()
  14. {
  15. }
  16. const char* Control::getID()
  17. {
  18. return _id.c_str();
  19. }
  20. const Rectangle& Control::getBounds(bool includePadding) const
  21. {
  22. return Rectangle();
  23. }
  24. void Control::setPosition(float x, float y)
  25. {
  26. _position.set(x, y);
  27. }
  28. const Vector2& Control::getPosition() const
  29. {
  30. return _position;
  31. }
  32. void Control::setSize(float width, float height)
  33. {
  34. _size.set(width, height);
  35. }
  36. const Vector2& Control::getSize() const
  37. {
  38. return _size;
  39. }
  40. void Control::setAutoSize(bool width, bool height)
  41. {
  42. _autoWidth = width;
  43. _autoHeight = height;
  44. }
  45. void Control::setStyle(Theme::Style* style)
  46. {
  47. _style = style;
  48. }
  49. Theme::Style* Control::getStyle() const
  50. {
  51. return _style;
  52. }
  53. void Control::setState(State state)
  54. {
  55. _state = state;
  56. }
  57. Control::State Control::getState()
  58. {
  59. return _state;
  60. }
  61. void Control::disable()
  62. {
  63. _state = STATE_DISABLED;
  64. }
  65. void Control::enable()
  66. {
  67. _state = STATE_NORMAL;
  68. }
  69. Theme::Style::OverlayType Control::getOverlayType() const
  70. {
  71. switch (_state)
  72. {
  73. case Control::STATE_NORMAL:
  74. return Theme::Style::OVERLAY_NORMAL;
  75. case Control::STATE_FOCUS:
  76. return Theme::Style::OVERLAY_FOCUS;
  77. case Control::STATE_ACTIVE:
  78. return Theme::Style::OVERLAY_ACTIVE;
  79. default:
  80. return Theme::Style::OVERLAY_NORMAL;
  81. }
  82. }
  83. void Control::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  84. {
  85. // Empty stub to be implemented by Button and its descendents.
  86. }
  87. void Control::keyEvent(Keyboard::KeyEvent evt, int key)
  88. {
  89. }
  90. void Control::update(const Vector2& position)
  91. {
  92. }
  93. void Control::drawBorder(SpriteBatch* spriteBatch, const Vector2& position)
  94. {
  95. Vector2 pos(position.x + _position.x, position.y + _position.y);
  96. // Get the border and background images for this control's current state.
  97. Theme::ContainerRegion* containerRegion = _style->getOverlay(getOverlayType())->getContainerRegion();
  98. if (containerRegion)
  99. {
  100. // Get the UVs.
  101. Theme::UVs topLeft, top, topRight, left, center, right, bottomLeft, bottom, bottomRight;
  102. topLeft = containerRegion->getUVs(Theme::ContainerRegion::TOP_LEFT);
  103. top = containerRegion->getUVs(Theme::ContainerRegion::TOP);
  104. topRight = containerRegion->getUVs(Theme::ContainerRegion::TOP_RIGHT);
  105. left = containerRegion->getUVs(Theme::ContainerRegion::LEFT);
  106. center = containerRegion->getUVs(Theme::ContainerRegion::CENTER);
  107. right = containerRegion->getUVs(Theme::ContainerRegion::RIGHT);
  108. bottomLeft = containerRegion->getUVs(Theme::ContainerRegion::BOTTOM_LEFT);
  109. bottom = containerRegion->getUVs(Theme::ContainerRegion::BOTTOM);
  110. bottomRight = containerRegion->getUVs(Theme::ContainerRegion::BOTTOM_RIGHT);
  111. // Calculate screen-space positions.
  112. Theme::Border border = containerRegion->getBorder();
  113. Theme::Padding padding = _style->getPadding();
  114. Vector4 borderColor = containerRegion->getColor();
  115. float midWidth = _size.x - border.left - border.right;
  116. float midHeight = _size.y - border.top - border.bottom;
  117. float midX = pos.x + border.left;
  118. float midY = pos.y + border.top;
  119. float rightX = pos.x + _size.x - border.right;
  120. float bottomY = pos.y + _size.y - border.bottom;
  121. // Draw themed border sprites.
  122. if (border.left && border.top)
  123. spriteBatch->draw(pos.x, pos.y, border.left, border.top, topLeft.u1, topLeft.v1, topLeft.u2, topLeft.v2, borderColor);
  124. if (border.top)
  125. spriteBatch->draw(pos.x + border.left, pos.y, midWidth, border.top, top.u1, top.v1, top.u2, top.v2, borderColor);
  126. if (border.right && border.top)
  127. spriteBatch->draw(rightX, pos.y, border.right, border.top, topRight.u1, topRight.v1, topRight.u2, topRight.v2, borderColor);
  128. if (border.left)
  129. spriteBatch->draw(pos.x, midY, border.left, midHeight, left.u1, left.v1, left.u2, left.v2, borderColor);
  130. if (border.left && border.right && border.top && border.bottom)
  131. spriteBatch->draw(pos.x + border.left, pos.y + border.top, _size.x - border.left - border.right, _size.y - border.top - border.bottom,
  132. center.u1, center.v1, center.u2, center.v2, borderColor);
  133. if (border.right)
  134. spriteBatch->draw(rightX, midY, border.right, midHeight,
  135. right.u1, right.v1, right.u2, right.v2, borderColor);
  136. if (border.bottom && border.left)
  137. spriteBatch->draw(pos.x, bottomY, border.left, border.bottom, bottomLeft.u1, bottomLeft.v1, bottomLeft.u2, bottomLeft.v2, borderColor);
  138. if (border.bottom)
  139. spriteBatch->draw(midX, bottomY, midWidth, border.bottom, bottom.u1, bottom.v1, bottom.u2, bottom.v2, borderColor);
  140. if (border.bottom && border.right)
  141. spriteBatch->draw(rightX, bottomY, border.right, border.bottom, bottomRight.u1, bottomRight.v1, bottomRight.u2, bottomRight.v2, borderColor);
  142. }
  143. }
  144. void Control::drawSprites(SpriteBatch* spriteBatch, const Vector2& position)
  145. {
  146. }
  147. void Control::drawText(const Vector2& position)
  148. {
  149. }
  150. bool Control::isDirty()
  151. {
  152. return _dirty;
  153. }
  154. }