CCControl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (c) 2012 cocos2d-x.org
  3. * http://www.cocos2d-x.org
  4. *
  5. * Copyright 2011 Yannick Loriot.
  6. * http://yannickloriot.com
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. * Converted to c++ / cocos2d-x by Angus C
  27. */
  28. #ifndef __CCCONTROL_H__
  29. #define __CCCONTROL_H__
  30. #include "CCControlUtils.h"
  31. #include "2d/CCLayer.h"
  32. #include "extensions/ExtensionExport.h"
  33. NS_CC_EXT_BEGIN
  34. class Invocation;
  35. /**
  36. * @addtogroup GUI
  37. * @{
  38. * @addtogroup control_extension
  39. * @{
  40. */
  41. /** Number of kinds of control event. */
  42. #define kControlEventTotalNumber 9
  43. /*
  44. * @class
  45. * Control is inspired by the UIControl API class from the UIKit library of
  46. * CocoaTouch. It provides a base class for control Sprites such as Button
  47. * or Slider that convey user intent to the application.
  48. *
  49. * The goal of Control is to define an interface and base implementation for
  50. * preparing action messages and initially dispatching them to their targets when
  51. * certain events occur.
  52. *
  53. * To use the Control you have to subclass it.
  54. */
  55. class CC_EX_DLL Control : public Layer
  56. {
  57. public:
  58. /** Kinds of possible events for the control objects. */
  59. enum class CC_EX_DLL EventType
  60. {
  61. TOUCH_DOWN = 1 << 0, // A touch-down event in the control.
  62. DRAG_INSIDE = 1 << 1, // An event where a finger is dragged inside the bounds of the control.
  63. DRAG_OUTSIDE = 1 << 2, // An event where a finger is dragged just outside the bounds of the control.
  64. DRAG_ENTER = 1 << 3, // An event where a finger is dragged into the bounds of the control.
  65. DRAG_EXIT = 1 << 4, // An event where a finger is dragged from within a control to outside its bounds.
  66. TOUCH_UP_INSIDE = 1 << 5, // A touch-up event in the control where the finger is inside the bounds of the control.
  67. TOUCH_UP_OUTSIDE = 1 << 6, // A touch-up event in the control where the finger is outside the bounds of the control.
  68. TOUCH_CANCEL = 1 << 7, // A system event canceling the current touches for the control.
  69. VALUE_CHANGED = 1 << 8, // A touch dragging or otherwise manipulating a control, causing it to emit a series of different values.
  70. ALL = TOUCH_DOWN | DRAG_INSIDE | DRAG_OUTSIDE | DRAG_ENTER | DRAG_EXIT | TOUCH_UP_INSIDE | TOUCH_UP_OUTSIDE | TOUCH_UP_OUTSIDE | TOUCH_CANCEL | TOUCH_CANCEL
  71. };
  72. typedef void (Ref::*Handler)(Ref*, EventType);
  73. /** The possible state for a control. */
  74. enum class State
  75. {
  76. NORMAL = 1 << 0, // The normal, or default state of a control that is, enabled but neither selected nor highlighted.
  77. HIGH_LIGHTED = 1 << 1, // Highlighted state of a control. A control enters this state when a touch down, drag inside or drag enter is performed. You can retrieve and set this value through the highlighted property.
  78. DISABLED = 1 << 2, // Disabled state of a control. This state indicates that the control is currently disabled. You can retrieve and set this value through the enabled property.
  79. SELECTED = 1 << 3 // Selected state of a control. This state indicates that the control is currently selected. You can retrieve and set this value through the selected property.
  80. };
  81. /** Creates a Control object */
  82. static Control* create();
  83. /** Tells whether the control is enabled. */
  84. virtual void setEnabled(bool bEnabled);
  85. virtual bool isEnabled() const;
  86. /** A Boolean value that determines the control selected state. */
  87. virtual void setSelected(bool bSelected);
  88. virtual bool isSelected() const;
  89. /** A Boolean value that determines whether the control is highlighted. */
  90. virtual void setHighlighted(bool bHighlighted);
  91. virtual bool isHighlighted() const;
  92. bool hasVisibleParents() const;
  93. /**
  94. * Updates the control layout using its current internal state.
  95. */
  96. virtual void needsLayout();
  97. /**
  98. * Sends action messages for the given control events.
  99. *
  100. * @param controlEvents A bitmask whose set flags specify the control events for
  101. * which action messages are sent. See "CCControlEvent" for bitmask constants.
  102. */
  103. virtual void sendActionsForControlEvents(EventType controlEvents);
  104. /**
  105. * Adds a target and action for a particular event (or events) to an internal
  106. * dispatch table.
  107. * The action message may optionally include the sender and the event as
  108. * parameters, in that order.
  109. * When you call this method, target is not retained.
  110. *
  111. * @param target The target object that is, the object to which the action
  112. * message is sent. It cannot be nil. The target is not retained.
  113. * @param action A selector identifying an action message. It cannot be NULL.
  114. * @param controlEvents A bitmask specifying the control events for which the
  115. * action message is sent. See "CCControlEvent" for bitmask constants.
  116. */
  117. virtual void addTargetWithActionForControlEvents(Ref* target, Handler action, EventType controlEvents);
  118. /**
  119. * Removes a target and action for a particular event (or events) from an
  120. * internal dispatch table.
  121. *
  122. * @param target The target object that is, the object to which the action
  123. * message is sent. Pass nil to remove all targets paired with action and the
  124. * specified control events.
  125. * @param action A selector identifying an action message. Pass NULL to remove
  126. * all action messages paired with target.
  127. * @param controlEvents A bitmask specifying the control events associated with
  128. * target and action. See "CCControlEvent" for bitmask constants.
  129. */
  130. virtual void removeTargetWithActionForControlEvents(Ref* target, Handler action, EventType controlEvents);
  131. /**
  132. * Returns a point corresponding to the touch location converted into the
  133. * control space coordinates.
  134. * @param touch A Touch object that represents a touch.
  135. */
  136. virtual Vec2 getTouchLocation(Touch* touch);
  137. virtual bool onTouchBegan(Touch* touch, Event* event) override;
  138. virtual void onTouchMoved(Touch* touch, Event* event) override;
  139. virtual void onTouchEnded(Touch* touch, Event* event) override;
  140. virtual void onTouchCancelled(Touch* touch, Event* event) override;
  141. /**
  142. * Returns a boolean value that indicates whether a touch is inside the bounds
  143. * of the receiver. The given touch must be relative to the world.
  144. *
  145. * @param touch A Touch object that represents a touch.
  146. *
  147. * @return Whether a touch is inside the receiver's rect.
  148. */
  149. virtual bool isTouchInside(Touch * touch);
  150. //add djd scrollview button按钮不吞事件
  151. virtual void setControlSwallowTouches(bool bSwall);
  152. // Overrides
  153. virtual bool isOpacityModifyRGB() const override;
  154. virtual void setOpacityModifyRGB(bool bOpacityModifyRGB) override;
  155. CC_CONSTRUCTOR_ACCESS:
  156. /**
  157. * @js ctor
  158. */
  159. Control();
  160. /**
  161. * @js NA
  162. * @lua NA
  163. */
  164. virtual ~Control();
  165. virtual bool init(void) override;
  166. protected:
  167. /**
  168. * Returns an Invocation object able to construct messages using a given
  169. * target-action pair. (The invocation may optionally include the sender and
  170. * the event as parameters, in that order)
  171. *
  172. * @param target The target object.
  173. * @param action A selector identifying an action message.
  174. * @param controlEvent A control events for which the action message is sent.
  175. * See "CCControlEvent" for constants.
  176. *
  177. * @return an Invocation object able to construct messages using a given
  178. * target-action pair.
  179. */
  180. Invocation* invocationWithTargetAndActionForControlEvent(Ref* target, Handler action, EventType controlEvent);
  181. /**
  182. * Returns the Invocation list for the given control event. If the list does
  183. * not exist, it'll create an empty array before returning it.
  184. *
  185. * @param controlEvent A control events for which the action message is sent.
  186. * See "CCControlEvent" for constants.
  187. *
  188. * @return the Invocation list for the given control event.
  189. */
  190. Vector<Invocation*>& dispatchListforControlEvent(EventType controlEvent);
  191. /**
  192. * Adds a target and action for a particular event to an internal dispatch
  193. * table.
  194. * The action message may optionally include the sender and the event as
  195. * parameters, in that order.
  196. * When you call this method, target is not retained.
  197. *
  198. * @param target The target object that is, the object to which the action
  199. * message is sent. It cannot be nil. The target is not retained.
  200. * @param action A selector identifying an action message. It cannot be NULL.
  201. * @param controlEvent A control event for which the action message is sent.
  202. * See "CCControlEvent" for constants.
  203. */
  204. void addTargetWithActionForControlEvent(Ref* target, Handler action, EventType controlEvent);
  205. /**
  206. * Removes a target and action for a particular event from an internal dispatch
  207. * table.
  208. *
  209. * @param target The target object that is, the object to which the action
  210. * message is sent. Pass nil to remove all targets paired with action and the
  211. * specified control events.
  212. * @param action A selector identifying an action message. Pass NULL to remove
  213. * all action messages paired with target.
  214. * @param controlEvent A control event for which the action message is sent.
  215. * See "CCControlEvent" for constants.
  216. */
  217. void removeTargetWithActionForControlEvent(Ref* target, Handler action, EventType controlEvent);
  218. bool _enabled;
  219. bool _selected;
  220. bool _highlighted;
  221. /** True if all of the controls parents are visible */
  222. bool _hasVisibleParents;
  223. /**
  224. * Table of connection between the ControlEvents and their associated
  225. * target-actions pairs. For each ButtonEvents a list of NSInvocation
  226. * (which contains the target-action pair) is linked.
  227. */
  228. std::unordered_map<int, Vector<Invocation*>*> _dispatchTable;
  229. //CCRGBAProtocol
  230. bool _isOpacityModifyRGB;
  231. /** The current control state constant. */
  232. CC_SYNTHESIZE_READONLY(State, _state, State);
  233. private:
  234. CC_DISALLOW_COPY_AND_ASSIGN(Control);
  235. };
  236. CC_EX_DLL Control::EventType operator|(Control::EventType a, Control::EventType b);
  237. // end of GUI group
  238. /// @}
  239. /// @}
  240. NS_CC_EXT_END
  241. #endif