Button.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef BUTTON_H_
  2. #define BUTTON_H_
  3. #include "Label.h"
  4. #include "Touch.h"
  5. #include "Theme.h"
  6. #include "Properties.h"
  7. namespace gameplay
  8. {
  9. /**
  10. * Defines a button UI control. This is essentially a label that can have a callback method set on it.
  11. *
  12. * The following properties are available for buttons:
  13. *
  14. * button <Button ID>
  15. * {
  16. * style = <Style ID>
  17. * position = <x, y>
  18. * size = <width, height>
  19. * text = <string>
  20. * }
  21. */
  22. class Button : public Label
  23. {
  24. friend class Container;
  25. class Callback;
  26. public:
  27. /**
  28. * Set a callback method on this button. The callback will be triggered when the button is
  29. * clicked -- i.e. consecutive TOUCH_PRESS and TOUCH_RELEASE events that both fall within
  30. * the bounds of the button.
  31. *
  32. * @param instance The object to call the method on.
  33. * @param callbackMethod The method to call.
  34. */
  35. template <class ClassType>
  36. void setCallback(ClassType* instance, void (ClassType::*callbackMethod)(Control*));
  37. protected:
  38. /**
  39. * Create a button with a given style and properties.
  40. *
  41. * @param style The style to apply to this button.
  42. * @param properties The properties to set on this button.
  43. *
  44. * @return The new button.
  45. */
  46. static Button* create(Theme::Style* style, Properties* properties);
  47. /**
  48. * Touch callback on touch events. Controls return true if they consume the touch event.
  49. *
  50. * @param evt The touch event that occurred.
  51. * @param x The x position of the touch in pixels. Left edge is zero.
  52. * @param y The y position of the touch in pixels. Top edge is zero.
  53. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  54. *
  55. * @return Whether the touch event was consumed by the control.
  56. *
  57. * @see Touch::TouchEvent
  58. */
  59. bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  60. Button();
  61. virtual ~Button();
  62. Callback* _callback; // The callback method pointer interface.
  63. private:
  64. Button(const Button& copy);
  65. /**
  66. * Abstract callback interface.
  67. */
  68. class Callback
  69. {
  70. public:
  71. virtual ~Callback() { }
  72. virtual void trigger(Control* button) = 0;
  73. };
  74. /**
  75. * Implementation of the callback interface for a specific class.
  76. */
  77. template <class ClassType>
  78. class CallbackImpl : public Callback
  79. {
  80. typedef void (ClassType::*CallbackMethod)(Control*);
  81. public:
  82. CallbackImpl(ClassType* instance, CallbackMethod method);
  83. void trigger(Control* control);
  84. private:
  85. ClassType* _instance;
  86. CallbackMethod _method;
  87. };
  88. };
  89. template <class ClassType>
  90. Button::CallbackImpl<ClassType>::CallbackImpl(ClassType* instance, CallbackMethod method)
  91. : _instance(instance), _method(method)
  92. {
  93. }
  94. template <class ClassType>
  95. void Button::setCallback(ClassType* instance, void (ClassType::*callbackMethod)(Control*))
  96. {
  97. _callback = new CallbackImpl<ClassType>(instance, callbackMethod);
  98. }
  99. template <class ClassType>
  100. void Button::CallbackImpl<ClassType>::trigger(Control* control)
  101. {
  102. (_instance->*_method)(control);
  103. }
  104. }
  105. #endif