CheckBox.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. consumeEvents = <bool> // Whether the checkbox propagates input events to the Game's input event handler. Default is true.
  28. }
  29. @endverbatim
  30. */
  31. class CheckBox : public Button
  32. {
  33. friend class Container;
  34. public:
  35. /**
  36. * Create a new check box control.
  37. *
  38. * @param id The control's ID.
  39. * @param style The control's style.
  40. *
  41. * @return The new check box.
  42. * @script{create}
  43. */
  44. static CheckBox* create(const char* id, Theme::Style* style);
  45. /**
  46. * Gets whether this checkbox is checked.
  47. *
  48. * @return Whether this checkbox is checked.
  49. */
  50. bool isChecked();
  51. /**
  52. * Sets whether the checkbox is checked.
  53. *
  54. * @param checked TRUE if the checkbox is checked; FALSE if the checkbox is not checked.
  55. */
  56. void setChecked(bool checked);
  57. /**
  58. * Set the size to draw the checkbox icon.
  59. *
  60. * @param width The width to draw the checkbox icon.
  61. * @param height The height to draw the checkbox icon.
  62. */
  63. void setImageSize(float width, float height);
  64. /**
  65. * Get the size at which the checkbox icon will be drawn.
  66. *
  67. * @return The size of the checkbox icon.
  68. */
  69. const Vector2& getImageSize() const;
  70. /**
  71. * @see Control::getType
  72. */
  73. const char* getType() const;
  74. /**
  75. * Add a listener to be notified of specific events affecting
  76. * this control. Event types can be OR'ed together.
  77. * E.g. To listen to touch-press and touch-release events,
  78. * pass <code>Control::Listener::TOUCH | Control::Listener::RELEASE</code>
  79. * as the second parameter.
  80. *
  81. * @param listener The listener to add.
  82. * @param eventFlags The events to listen for.
  83. */
  84. virtual void addListener(Control::Listener* listener, int eventFlags);
  85. protected:
  86. /**
  87. * Constructor.
  88. */
  89. CheckBox();
  90. /**
  91. * Destructor.
  92. */
  93. ~CheckBox();
  94. /**
  95. * Create a checkbox with a given style and properties.
  96. *
  97. * @param style The style to apply to this checkbox.
  98. * @param properties The properties to set on this checkbox.
  99. *
  100. * @return The new checkbox.
  101. */
  102. static CheckBox* create(Theme::Style* style, Properties* properties);
  103. /**
  104. * Touch callback on touch events. Controls return true if they consume the touch event.
  105. *
  106. * @param evt The touch event that occurred.
  107. * @param x The x position of the touch in pixels. Left edge is zero.
  108. * @param y The y position of the touch in pixels. Top edge is zero.
  109. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  110. *
  111. * @return Whether the touch event was consumed by the control.
  112. *
  113. * @see Touch::TouchEvent
  114. */
  115. bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  116. /**
  117. * Called when a control's properties change. Updates this control's internal rendering
  118. * properties, such as its text viewport.
  119. *
  120. * @param container This control's parent container.
  121. * @param offset The position offset.
  122. */
  123. void update(const Control* container, const Vector2& offset);
  124. /**
  125. * Draw the checkbox icon associated with this control.
  126. *
  127. * @param spriteBatch The sprite batch containing this control's icons.
  128. * @param clip The container position this control is relative to.
  129. */
  130. void drawImages(SpriteBatch* spriteBatch, const Rectangle& clip);
  131. /**
  132. * Whether this checkbox is currently checked.
  133. */
  134. bool _checked;
  135. /**
  136. * The size to draw the checkbox icon, if different from its size in the texture.
  137. */
  138. Vector2 _imageSize;
  139. /**
  140. * The Theme::ThemeImage to display for the checkbox.
  141. */
  142. Theme::ThemeImage* _image;
  143. private:
  144. /*
  145. * Constructor.
  146. */
  147. CheckBox(const CheckBox& copy);
  148. };
  149. }
  150. #endif