UICheckBox.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __UICHECKBOX_H__
  22. #define __UICHECKBOX_H__
  23. #include "ui/UIAbstractCheckButton.h"
  24. #include "ui/GUIExport.h"
  25. /**
  26. * @addtogroup ui
  27. * @{
  28. */
  29. NS_CC_BEGIN
  30. namespace ui {
  31. /**
  32. * Checkbox event type, there are two type here:
  33. * - selected state
  34. * - unselected state
  35. * @deprecated use `CheckBox::EventType` instead
  36. */
  37. typedef enum
  38. {
  39. CHECKBOX_STATE_EVENT_SELECTED,
  40. CHECKBOX_STATE_EVENT_UNSELECTED
  41. } CheckBoxEventType;
  42. /**
  43. * A callback which will be called after checkbox event happens.
  44. * @deprecated use `CheckBox::ccCheckBoxCallback` instead.
  45. */
  46. typedef void (Ref::*SEL_SelectedStateEvent)(Ref*,CheckBoxEventType);
  47. #define checkboxselectedeventselector(_SELECTOR) (SEL_SelectedStateEvent)(&_SELECTOR)
  48. /**
  49. * Checkbox is a specific type of two-states button that can be either checked or unchecked.
  50. */
  51. class CC_GUI_DLL CheckBox : public AbstractCheckButton
  52. {
  53. DECLARE_CLASS_GUI_INFO
  54. public:
  55. /**
  56. * CheckBox event type, currently only "selected" and "unselected" event are cared.
  57. */
  58. enum class EventType
  59. {
  60. SELECTED,
  61. UNSELECTED
  62. };
  63. /**
  64. * A callback which will be called after certain CheckBox event issue.
  65. * @see `CheckBox::EventType`
  66. */
  67. typedef std::function<void(Ref*,CheckBox::EventType)> ccCheckBoxCallback;
  68. /**
  69. * Default constructor.
  70. *
  71. * @lua new
  72. */
  73. CheckBox();
  74. /**
  75. * Default destructor.
  76. *
  77. * @lua NA
  78. */
  79. virtual ~CheckBox();
  80. /**
  81. * Create and return a empty CheckBox instance pointer.
  82. */
  83. static CheckBox* create();
  84. /**
  85. * Create an checkbox with various images.
  86. *
  87. * @param backGround backGround texture.
  88. * @param backGroundSelected backGround selected state texture.
  89. * @param cross cross texture.
  90. * @param backGroundDisabled backGround disabled state texture.
  91. * @param frontCrossDisabled cross dark state texture.
  92. * @param texType @see `Widget::TextureResType`
  93. *
  94. * @return A CheckBox instance pointer.
  95. */
  96. static CheckBox* create(const std::string& backGround,
  97. const std::string& backGroundSelected,
  98. const std::string& cross,
  99. const std::string& backGroundDisabled,
  100. const std::string& frontCrossDisabled,
  101. TextureResType texType = TextureResType::LOCAL);
  102. /**
  103. * Another factory method to create a CheckBox instance.
  104. * This method uses less resource to create a CheckBox.
  105. * @param backGround The background image name in `std::string`.
  106. * @param cross The cross image name in `std::string`.
  107. * @param texType The texture's resource type in `Widget::TextureResType`.
  108. * @return A CheckBox instance pointer
  109. */
  110. static CheckBox* create(const std::string& backGround,
  111. const std::string& cross,
  112. TextureResType texType = TextureResType::LOCAL);
  113. /**
  114. * Change Checkbox state to selected.
  115. *
  116. * @deprecated use `isSelected()` instead
  117. * @param selected True means the Checkbox will be selected, false means unselected.
  118. */
  119. CC_DEPRECATED_ATTRIBUTE void setSelectedState(bool selected){this->setSelected(selected);}
  120. /**
  121. * Query whether the CheckBox is selected or not.
  122. *
  123. * @deprecated use `setSelected(bool)` instead.
  124. * @return selected true that checkbox is selected, false otherwise.
  125. */
  126. CC_DEPRECATED_ATTRIBUTE bool getSelectedState()const{return this->isSelected();}
  127. /**Add a callback function which would be called when checkbox is selected or unselected.
  128. *@deprecated use `addEventListener(const ccCheckBoxCallback&)` instead
  129. *@param target A pointer type in Ref*.
  130. *@param selector A member function pointer in SEL_SelectedStateEvent.
  131. */
  132. CC_DEPRECATED_ATTRIBUTE void addEventListenerCheckBox(Ref* target,SEL_SelectedStateEvent selector);
  133. /**
  134. *Add a callback function which would be called when checkbox is selected or unselected.
  135. *@param callback A std::function with type @see `ccCheckBoxCallback`
  136. */
  137. void addEventListener(const ccCheckBoxCallback& callback);
  138. //override functions
  139. virtual std::string getDescription() const override;
  140. virtual void onTouchEnded(Touch *touch, Event *unusedEvent) override;
  141. protected:
  142. virtual void dispatchSelectChangedEvent(bool selected) override;
  143. virtual Widget* createCloneInstance() override;
  144. virtual void copySpecialProperties(Widget* model) override;
  145. protected:
  146. //if you use the old event callback, it will retain the _checkBoxEventListener
  147. Ref* _checkBoxEventListener;
  148. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  149. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  150. #elif _MSC_VER >= 1400 //vs 2005 or higher
  151. #pragma warning (push)
  152. #pragma warning (disable: 4996)
  153. #endif
  154. SEL_SelectedStateEvent _checkBoxEventSelector;
  155. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  156. #pragma GCC diagnostic warning "-Wdeprecated-declarations"
  157. #elif _MSC_VER >= 1400 //vs 2005 or higher
  158. #pragma warning (pop)
  159. #endif
  160. ccCheckBoxCallback _checkBoxEventCallback;
  161. };
  162. }
  163. NS_CC_END
  164. // end of ui group
  165. /// @}
  166. #endif /* defined(__CocoGUI__CheckBox__) */