Control.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef CONTROL_H_
  2. #define CONTROL_H_
  3. #include "Ref.h"
  4. #include "Rectangle.h"
  5. #include "Vector2.h"
  6. #include "SpriteBatch.h"
  7. #include "Theme.h"
  8. #include "Touch.h"
  9. #include "Keyboard.h"
  10. namespace gameplay
  11. {
  12. class Control : public Ref
  13. {
  14. public:
  15. enum State
  16. {
  17. STATE_NORMAL,
  18. STATE_FOCUS,
  19. STATE_ACTIVE,
  20. STATE_DISABLED
  21. };
  22. const char* getID();
  23. /**
  24. * Get the actual bounding box of this Control, local to its Container,
  25. * after any calculations performed due to the Container's Layout or settings of auto-size.
  26. * Always includes the Control's border.
  27. * Can optionally include the Control's padding.
  28. * Query getPosition() and getSize() to learn the bounds without border or padding.
  29. */
  30. const Rectangle& getBounds(bool includePadding) const;
  31. /**
  32. * Set the position of this Control relative to its parent Container.
  33. *
  34. * @param x The x coordinate.
  35. * @param y The y coordinate.
  36. */
  37. void setPosition(float x, float y);
  38. /**
  39. * Get the position of this Control relative to its parent Container.
  40. *
  41. * @return The position vector.
  42. */
  43. const Vector2& getPosition() const;
  44. /**
  45. * Set the size of this Control, including its border and padding.
  46. *
  47. * @param width The width.
  48. * @param height The height.
  49. */
  50. void setSize(float width, float height);
  51. /**
  52. * Get the size of this Control, including its border and padding.
  53. *
  54. * @return The size vector.
  55. */
  56. const Vector2& getSize() const;
  57. /**
  58. * Set width and/or height to auto-size to size a Control to tightly fit
  59. * its text and themed visual elements (CheckBox / RadioButton toggle etc.).
  60. *
  61. * Similarly set this on the width and/or height of a Container to tightly fit
  62. * the Container around all its children.
  63. *
  64. * @param width Whether to automatically determine this Control's width.
  65. * @param height Whether to automatically determine this Control's height.
  66. */
  67. void setAutoSize(bool width, bool height);
  68. void setState(State state);
  69. State getState();
  70. void disable();
  71. void enable();
  72. Theme::Style::OverlayType getOverlayType() const;
  73. /**
  74. * Defaults to empty stub.
  75. */
  76. virtual void touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  77. virtual void keyEvent(Keyboard::KeyEvent evt, int key);
  78. virtual void update(const Vector2& position);
  79. /**
  80. * Draws the themed border and background of a control.
  81. */
  82. void drawBorder(SpriteBatch* spriteBatch, const Vector2& position);
  83. virtual void drawSprites(SpriteBatch* spriteBatch, const Vector2& position);
  84. virtual void drawText(const Vector2& position);
  85. /**
  86. * Returns whether this Control has been modified since the last time
  87. * isDirty() was called, and resets its dirty flag.
  88. */
  89. virtual bool isDirty();
  90. void setStyle(Theme::Style* Style);
  91. Theme::Style* getStyle() const;
  92. void themeChanged();
  93. protected:
  94. Control();
  95. Control(const Control& copy);
  96. virtual ~Control();
  97. std::string _id;
  98. State _state; // Determines overlay used during draw().
  99. Vector2 _size;
  100. Vector2 _position;
  101. Vector2 _border;
  102. Vector2 _padding;
  103. bool _autoWidth;
  104. bool _autoHeight;
  105. bool _dirty;
  106. Theme::Style* _style;
  107. };
  108. }
  109. #endif