CheckBox.h 4.8 KB

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