CheckBox.h 4.1 KB

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