CCScrollView.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /****************************************************************************
  2. Copyright (c) 2012 cocos2d-x.org
  3. Copyright (c) 2010 Sangwoo Im
  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 __CCSCROLLVIEW_H__
  22. #define __CCSCROLLVIEW_H__
  23. #include "2d/CCLayer.h"
  24. #include "base/CCEventListenerTouch.h"
  25. #include "2d/CCActionTween.h"
  26. #include "extensions/ExtensionMacros.h"
  27. #include "extensions/ExtensionExport.h"
  28. /**
  29. * @addtogroup ui
  30. * @{
  31. */
  32. NS_CC_EXT_BEGIN
  33. class ScrollView;
  34. class CC_EX_DLL ScrollViewDelegate
  35. {
  36. public:
  37. /**
  38. * @js NA
  39. * @lua NA
  40. */
  41. virtual ~ScrollViewDelegate() {}
  42. /**
  43. * @js NA
  44. * @lua NA
  45. */
  46. virtual void scrollViewDidScroll(ScrollView* view) {};
  47. /**
  48. * @js NA
  49. * @lua NA
  50. */
  51. virtual void scrollViewDidZoom(ScrollView* view) {};
  52. };
  53. /**
  54. * ScrollView support for cocos2d-x.
  55. * It provides scroll view functionalities to cocos2d projects natively.
  56. */
  57. class CC_EX_DLL ScrollView : public Layer, public ActionTweenDelegate
  58. {
  59. public:
  60. enum class Direction
  61. {
  62. NONE = -1,
  63. HORIZONTAL = 0,
  64. VERTICAL,
  65. BOTH
  66. };
  67. /**
  68. * Returns an autoreleased scroll view object.
  69. *
  70. * @param size view size
  71. * @param container parent object
  72. * @return autoreleased scroll view object
  73. */
  74. static ScrollView* create(Size size, Node* container = NULL);
  75. /**
  76. * Returns an autoreleased scroll view object.
  77. *
  78. * @return autoreleased scroll view object
  79. */
  80. static ScrollView* create();
  81. /**
  82. * @js ctor
  83. * @lua new
  84. */
  85. ScrollView();
  86. /**
  87. * @js NA
  88. * @lua NA
  89. */
  90. virtual ~ScrollView();
  91. bool init() override;
  92. /**
  93. * Returns a scroll view object
  94. *
  95. * @param size view size
  96. * @param container parent object
  97. * @return scroll view object
  98. */
  99. bool initWithViewSize(Size size, Node* container = NULL);
  100. /**
  101. * Sets a new content offset. It ignores max/min offset. It just sets what's given. (just like UIKit's UIScrollView)
  102. *
  103. * @param offset The new offset.
  104. * @param animated If true, the view will scroll to the new offset.
  105. */
  106. void setContentOffset(Vec2 offset, bool animated = false);
  107. Vec2 getContentOffset();
  108. /**
  109. * Sets a new content offset. It ignores max/min offset. It just sets what's given. (just like UIKit's UIScrollView)
  110. * You can override the animation duration with this method.
  111. *
  112. * @param offset The new offset.
  113. * @param dt The animation duration.
  114. */
  115. void setContentOffsetInDuration(Vec2 offset, float dt);
  116. /**
  117. * Halts the movement animation of the inner content started with setContentOffset() or setContentOffsetInDuration()
  118. */
  119. void stopAnimatedContentOffset();
  120. void setZoomScale(float s);
  121. /**
  122. * Sets a new scale and does that for a predefined duration.
  123. *
  124. * @param s The new scale vale
  125. * @param animated If true, scaling is animated
  126. */
  127. void setZoomScale(float s, bool animated);
  128. float getZoomScale();
  129. /**
  130. * Sets a new scale for container in a given duration.
  131. *
  132. * @param s The new scale value
  133. * @param dt The animation duration
  134. */
  135. void setZoomScaleInDuration(float s, float dt);
  136. /**
  137. * Set min scale
  138. *
  139. * @param minScale min scale
  140. */
  141. void setMinScale(float minScale) {
  142. _minScale = minScale;
  143. }
  144. /**
  145. * Set max scale
  146. *
  147. * @param maxScale max scale
  148. */
  149. void setMaxScale(float maxScale) {
  150. _maxScale = maxScale;
  151. }
  152. /**
  153. * Returns the current container's minimum offset. You may want this while you animate scrolling by yourself
  154. */
  155. Vec2 minContainerOffset();
  156. /**
  157. * Returns the current container's maximum offset. You may want this while you animate scrolling by yourself
  158. */
  159. Vec2 maxContainerOffset();
  160. /**
  161. * Determines if a given node's bounding box is in visible bounds
  162. *
  163. * @returns true if it is in visible bounds
  164. */
  165. bool isNodeVisible(Node * node);
  166. /**
  167. * Provided to make scroll view compatible with SWLayer's pause method
  168. */
  169. using Layer::pause; // fix warning
  170. void pause(Ref* sender);
  171. /**
  172. * Provided to make scroll view compatible with SWLayer's resume method
  173. */
  174. using Layer::resume; // fix warning
  175. void resume(Ref* sender);
  176. void setTouchEnabled(bool enabled);
  177. bool isTouchEnabled() const;
  178. bool isDragging() const {return _dragging;}
  179. bool isTouchMoved() const { return _touchMoved; }
  180. bool isBounceable() const { return _bounceable; }
  181. void setBounceable(bool bBounceable) { _bounceable = bBounceable; }
  182. /**
  183. * size to clip. Node boundingBox uses contentSize directly.
  184. * It's semantically different what it actually means to common scroll views.
  185. * Hence, this scroll view will use a separate size property.
  186. */
  187. Size getViewSize() const { return _viewSize; }
  188. void setViewSize(Size size);
  189. Node * getContainer();
  190. void setContainer(Node * pContainer);
  191. /**
  192. * direction allowed to scroll. ScrollViewDirectionBoth by default.
  193. */
  194. Direction getDirection() const { return _direction; }
  195. virtual void setDirection(Direction eDirection) { _direction = eDirection; }
  196. /**
  197. * @js NA
  198. * @lua NA
  199. */
  200. ScrollViewDelegate* getDelegate() { return _delegate; }
  201. /**
  202. * @code
  203. * when this function bound to js or lua,the input param are changed
  204. * in js: var setDelegate(var jsObject)
  205. * in lua: local setDelegate()
  206. * @endcode
  207. */
  208. void setDelegate(ScrollViewDelegate* pDelegate) { _delegate = pDelegate; }
  209. void updateInset();
  210. /**
  211. * Determines whether it clips its children or not.
  212. */
  213. bool isClippingToBounds() { return _clippingToBounds; }
  214. void setClippingToBounds(bool bClippingToBounds) { _clippingToBounds = bClippingToBounds; }
  215. virtual bool onTouchBegan(Touch *touch, Event *event) override;
  216. virtual void onTouchMoved(Touch *touch, Event *event) override;
  217. virtual void onTouchEnded(Touch *touch, Event *event) override;
  218. virtual void onTouchCancelled(Touch *touch, Event *event) override;
  219. // Overrides
  220. virtual void setContentSize(const Size & size) override;
  221. virtual const Size& getContentSize() const override;
  222. /**
  223. * @js NA
  224. * @lua NA
  225. */
  226. virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
  227. using Node::addChild;
  228. virtual void addChild(Node * child, int zOrder, int tag) override;
  229. virtual void addChild(Node * child, int zOrder, const std::string &name) override;
  230. virtual void removeAllChildren() override;
  231. virtual void removeAllChildrenWithCleanup(bool cleanup) override;
  232. virtual void removeChild(Node* child, bool cleanup = true) override;
  233. /**
  234. * CCActionTweenDelegate
  235. */
  236. void updateTweenAction(float value, const std::string& key) override;
  237. bool hasVisibleParents() const;
  238. protected:
  239. /**
  240. * Relocates the container at the proper offset, in bounds of max/min offsets.
  241. *
  242. * @param animated If true, relocation is animated
  243. */
  244. void relocateContainer(bool animated);
  245. /**
  246. * implements auto-scrolling behavior. change SCROLL_DEACCEL_RATE as needed to choose
  247. * deacceleration speed. it must be less than 1.0f.
  248. *
  249. * @param dt delta
  250. */
  251. void deaccelerateScrolling(float dt);
  252. /**
  253. * This method makes sure auto scrolling causes delegate to invoke its method
  254. */
  255. void performedAnimatedScroll(float dt);
  256. /**
  257. * Expire animated scroll delegate calls
  258. */
  259. void stoppedAnimatedScroll(Node* node);
  260. /**
  261. * clip this view so that outside of the visible bounds can be hidden.
  262. */
  263. void beforeDraw();
  264. void onBeforeDraw();
  265. /**
  266. * retract what's done in beforeDraw so that there's no side effect to
  267. * other nodes.
  268. */
  269. void afterDraw();
  270. void onAfterDraw();
  271. /**
  272. * Zoom handling
  273. */
  274. void handleZoom();
  275. Rect getViewRect();
  276. /**
  277. * scroll view delegate
  278. */
  279. ScrollViewDelegate* _delegate;
  280. Direction _direction;
  281. /**
  282. * If YES, the view is being dragged.
  283. */
  284. bool _dragging;
  285. /**
  286. * Content offset. Note that left-bottom point is the origin
  287. */
  288. Vec2 _contentOffset;
  289. /**
  290. * Container holds scroll view contents, Sets the scrollable container object of the scroll view
  291. */
  292. Node* _container;
  293. /**
  294. * Determines whether user touch is moved after begin phase.
  295. */
  296. bool _touchMoved;
  297. /**
  298. * max inset point to limit scrolling by touch
  299. */
  300. Vec2 _maxInset;
  301. /**
  302. * min inset point to limit scrolling by touch
  303. */
  304. Vec2 _minInset;
  305. /**
  306. * Determines whether the scroll view is allowed to bounce or not.
  307. */
  308. bool _bounceable;
  309. bool _clippingToBounds;
  310. /**
  311. * scroll speed
  312. */
  313. Vec2 _scrollDistance;
  314. /**
  315. * Touch point
  316. */
  317. Vec2 _touchPoint;
  318. /**
  319. * length between two fingers
  320. */
  321. float _touchLength;
  322. /**
  323. * Touch objects to detect multitouch
  324. */
  325. std::vector<Touch*> _touches;
  326. /**
  327. * size to clip. Node boundingBox uses contentSize directly.
  328. * It's semantically different what it actually means to common scroll views.
  329. * Hence, this scroll view will use a separate size property.
  330. */
  331. Size _viewSize;
  332. /**
  333. * max and min scale
  334. */
  335. float _minScale, _maxScale;
  336. /**
  337. * scissor rect for parent, just for restoring GL_SCISSOR_BOX
  338. */
  339. Rect _parentScissorRect;
  340. bool _scissorRestored;
  341. /** Touch listener */
  342. EventListenerTouchOneByOne* _touchListener;
  343. CustomCommand _beforeDrawCommand;
  344. CustomCommand _afterDrawCommand;
  345. /**
  346. * Action created with setContentOffsetInDuration(), saved so it can be halted
  347. */
  348. Action* _animatedScrollAction;
  349. };
  350. NS_CC_EXT_END
  351. // end of ui group
  352. /// @}
  353. #endif /* __CCSCROLLVIEW_H__ */