CCControl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. };
  71. typedef void (Ref::*Handler)(Ref*, EventType);
  72. /** The possible state for a control. */
  73. enum class State
  74. {
  75. NORMAL = 1 << 0, // The normal, or default state of a control that is, enabled but neither selected nor highlighted.
  76. 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.
  77. 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.
  78. 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.
  79. };
  80. /** Creates a Control object */
  81. static Control* create();
  82. /** Tells whether the control is enabled. */
  83. virtual void setEnabled(bool bEnabled);
  84. virtual bool isEnabled() const;
  85. /** A Boolean value that determines the control selected state. */
  86. virtual void setSelected(bool bSelected);
  87. virtual bool isSelected() const;
  88. /** A Boolean value that determines whether the control is highlighted. */
  89. virtual void setHighlighted(bool bHighlighted);
  90. virtual bool isHighlighted() const;
  91. bool hasVisibleParents() const;
  92. /**
  93. * Updates the control layout using its current internal state.
  94. */
  95. virtual void needsLayout();
  96. /**
  97. * Sends action messages for the given control events.
  98. *
  99. * @param controlEvents A bitmask whose set flags specify the control events for
  100. * which action messages are sent. See "CCControlEvent" for bitmask constants.
  101. */
  102. virtual void sendActionsForControlEvents(EventType controlEvents);
  103. /**
  104. * Adds a target and action for a particular event (or events) to an internal
  105. * dispatch table.
  106. * The action message may optionally include the sender and the event as
  107. * parameters, in that order.
  108. * When you call this method, target is not retained.
  109. *
  110. * @param target The target object that is, the object to which the action
  111. * message is sent. It cannot be nil. The target is not retained.
  112. * @param action A selector identifying an action message. It cannot be NULL.
  113. * @param controlEvents A bitmask specifying the control events for which the
  114. * action message is sent. See "CCControlEvent" for bitmask constants.
  115. */
  116. virtual void addTargetWithActionForControlEvents(Ref* target, Handler action, EventType controlEvents);
  117. /**
  118. * Removes a target and action for a particular event (or events) from an
  119. * internal dispatch table.
  120. *
  121. * @param target The target object that is, the object to which the action
  122. * message is sent. Pass nil to remove all targets paired with action and the
  123. * specified control events.
  124. * @param action A selector identifying an action message. Pass NULL to remove
  125. * all action messages paired with target.
  126. * @param controlEvents A bitmask specifying the control events associated with
  127. * target and action. See "CCControlEvent" for bitmask constants.
  128. */
  129. virtual void removeTargetWithActionForControlEvents(Ref* target, Handler action, EventType controlEvents);
  130. /**
  131. * Returns a point corresponding to the touch location converted into the
  132. * control space coordinates.
  133. * @param touch A Touch object that represents a touch.
  134. */
  135. virtual Vec2 getTouchLocation(Touch* touch);
  136. virtual bool onTouchBegan(Touch* touch, Event* event) override;
  137. virtual void onTouchMoved(Touch* touch, Event* event) override;
  138. virtual void onTouchEnded(Touch* touch, Event* event) override;
  139. virtual void onTouchCancelled(Touch* touch, Event* event) override;
  140. /**
  141. * Returns a boolean value that indicates whether a touch is inside the bounds
  142. * of the receiver. The given touch must be relative to the world.
  143. *
  144. * @param touch A Touch object that represents a touch.
  145. *
  146. * @return Whether a touch is inside the receiver's rect.
  147. */
  148. virtual bool isTouchInside(Touch * touch);
  149. // Overrides
  150. virtual bool isOpacityModifyRGB() const override;
  151. virtual void setOpacityModifyRGB(bool bOpacityModifyRGB) override;
  152. CC_CONSTRUCTOR_ACCESS:
  153. /**
  154. * @js ctor
  155. */
  156. Control();
  157. /**
  158. * @js NA
  159. * @lua NA
  160. */
  161. virtual ~Control();
  162. virtual bool init(void) override;
  163. protected:
  164. /**
  165. * Returns an Invocation object able to construct messages using a given
  166. * target-action pair. (The invocation may optionally include the sender and
  167. * the event as parameters, in that order)
  168. *
  169. * @param target The target object.
  170. * @param action A selector identifying an action message.
  171. * @param controlEvent A control events for which the action message is sent.
  172. * See "CCControlEvent" for constants.
  173. *
  174. * @return an Invocation object able to construct messages using a given
  175. * target-action pair.
  176. */
  177. Invocation* invocationWithTargetAndActionForControlEvent(Ref* target, Handler action, EventType controlEvent);
  178. /**
  179. * Returns the Invocation list for the given control event. If the list does
  180. * not exist, it'll create an empty array before returning it.
  181. *
  182. * @param controlEvent A control events for which the action message is sent.
  183. * See "CCControlEvent" for constants.
  184. *
  185. * @return the Invocation list for the given control event.
  186. */
  187. Vector<Invocation*>& dispatchListforControlEvent(EventType controlEvent);
  188. /**
  189. * Adds a target and action for a particular event to an internal dispatch
  190. * table.
  191. * The action message may optionally include the sender and the event as
  192. * parameters, in that order.
  193. * When you call this method, target is not retained.
  194. *
  195. * @param target The target object that is, the object to which the action
  196. * message is sent. It cannot be nil. The target is not retained.
  197. * @param action A selector identifying an action message. It cannot be NULL.
  198. * @param controlEvent A control event for which the action message is sent.
  199. * See "CCControlEvent" for constants.
  200. */
  201. void addTargetWithActionForControlEvent(Ref* target, Handler action, EventType controlEvent);
  202. /**
  203. * Removes a target and action for a particular event from an internal dispatch
  204. * table.
  205. *
  206. * @param target The target object that is, the object to which the action
  207. * message is sent. Pass nil to remove all targets paired with action and the
  208. * specified control events.
  209. * @param action A selector identifying an action message. Pass NULL to remove
  210. * all action messages paired with target.
  211. * @param controlEvent A control event for which the action message is sent.
  212. * See "CCControlEvent" for constants.
  213. */
  214. void removeTargetWithActionForControlEvent(Ref* target, Handler action, EventType controlEvent);
  215. bool _enabled;
  216. bool _selected;
  217. bool _highlighted;
  218. /** True if all of the controls parents are visible */
  219. bool _hasVisibleParents;
  220. /**
  221. * Table of connection between the ControlEvents and their associated
  222. * target-actions pairs. For each ButtonEvents a list of NSInvocation
  223. * (which contains the target-action pair) is linked.
  224. */
  225. std::unordered_map<int, Vector<Invocation*>*> _dispatchTable;
  226. //CCRGBAProtocol
  227. bool _isOpacityModifyRGB;
  228. /** The current control state constant. */
  229. CC_SYNTHESIZE_READONLY(State, _state, State);
  230. private:
  231. CC_DISALLOW_COPY_AND_ASSIGN(Control);
  232. };
  233. CC_EX_DLL Control::EventType operator|(Control::EventType a, Control::EventType b);
  234. // end of GUI group
  235. /// @}
  236. /// @}
  237. NS_CC_EXT_END
  238. #endif