CheckBox.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #ifndef CHECKBOX_H_
  2. #define CHECKBOX_H_
  3. #include "Theme.h"
  4. #include "Properties.h"
  5. #include "Touch.h"
  6. #include "Button.h"
  7. namespace gameplay
  8. {
  9. /**
  10. * Defines a checkbox UI control. This is a button that toggles between two icons when clicked.
  11. *
  12. * The following properties are available for checkboxes:
  13. @verbatim
  14. checkBox <checkBoxID>
  15. {
  16. style = <styleID>
  17. alignment = <Control::Alignment constant> // Note: 'position' will be ignored.
  18. position = <x, y>
  19. autoWidth = <bool>
  20. autoHeight = <bool>
  21. size = <width, height>
  22. width = <width> // Can be used in place of 'size', e.g. with 'autoHeight = true'
  23. height = <height> // Can be used in place of 'size', e.g. with 'autoWidth = true'
  24. text = <string>
  25. checked = <bool>
  26. iconSize = <width, height> // The size to draw the checkbox icon, if different from its size in the texture.
  27. }
  28. @endverbatim
  29. */
  30. class CheckBox : public Button
  31. {
  32. friend class Container;
  33. public:
  34. /**
  35. * Gets whether this checkbox is checked.
  36. *
  37. * @return Whether this checkbox is checked.
  38. */
  39. bool isChecked();
  40. /**
  41. * Sets whether the checkbox is checked.
  42. *
  43. * @param checked TRUE if the checkbox is checked; FALSE if the checkbox is not checked.
  44. */
  45. void setChecked(bool checked);
  46. /**
  47. * Set the size to draw the checkbox icon.
  48. *
  49. * @param width The width to draw the checkbox icon.
  50. * @param height The height to draw the checkbox icon.
  51. */
  52. void setImageSize(float width, float height);
  53. /**
  54. * Get the size at which the checkbox icon will be drawn.
  55. *
  56. * @return The size of the checkbox icon.
  57. */
  58. const Vector2& getImageSize() const;
  59. /**
  60. * Add a listener to be notified of specific events affecting
  61. * this control. Event types can be OR'ed together.
  62. * E.g. To listen to touch-press and touch-release events,
  63. * pass <code>Control::Listener::TOUCH | Control::Listener::RELEASE</code>
  64. * as the second parameter.
  65. *
  66. * @param listener The listener to add.
  67. * @param eventFlags The events to listen for.
  68. */
  69. virtual void addListener(Control::Listener* listener, int eventFlags);
  70. protected:
  71. /**
  72. * Constructor.
  73. */
  74. CheckBox();
  75. /**
  76. * Destructor.
  77. */
  78. ~CheckBox();
  79. /**
  80. * Create a checkbox with a given style and properties.
  81. *
  82. * @param style The style to apply to this checkbox.
  83. * @param properties The properties to set on this checkbox.
  84. *
  85. * @return The new checkbox.
  86. */
  87. static CheckBox* create(Theme::Style* style, Properties* properties);
  88. /**
  89. * Touch callback on touch events. Controls return true if they consume the touch event.
  90. *
  91. * @param evt The touch event that occurred.
  92. * @param x The x position of the touch in pixels. Left edge is zero.
  93. * @param y The y position of the touch in pixels. Top edge is zero.
  94. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  95. *
  96. * @return Whether the touch event was consumed by the control.
  97. *
  98. * @see Touch::TouchEvent
  99. */
  100. bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  101. /**
  102. * Called when a control's properties change. Updates this control's internal rendering
  103. * properties, such as its text viewport.
  104. *
  105. * @param clip The clipping rectangle of this control's parent container.
  106. */
  107. void update(const Rectangle& clip, const Vector2& offset = Vector2::zero());
  108. /**
  109. * Draw the checkbox icon associated with this control.
  110. *
  111. * @param spriteBatch The sprite batch containing this control's icons.
  112. * @param clip The container position this control is relative to.
  113. */
  114. void drawImages(SpriteBatch* spriteBatch, const Rectangle& clip);
  115. /**
  116. * Whether this checkbox is currently checked.
  117. */
  118. bool _checked;
  119. /**
  120. * The size to draw the checkbox icon, if different from its size in the texture.
  121. */
  122. Vector2 _imageSize;
  123. /**
  124. * The Theme::ThemeImage to display for the checkbox.
  125. */
  126. Theme::ThemeImage* _image;
  127. private:
  128. /*
  129. * Constructor.
  130. */
  131. CheckBox(const CheckBox& copy);
  132. };
  133. }
  134. #endif