2012-09-10 14:13:55 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2010 Sangwoo Im
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
#ifndef __CCSCROLLVIEW_H__
|
|
|
|
#define __CCSCROLLVIEW_H__
|
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCLayer.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCEventListenerTouch.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCActionTween.h"
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "extensions/ExtensionMacros.h"
|
2014-07-21 17:45:56 +08:00
|
|
|
#include "extensions/ExtensionExport.h"
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup GUI
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
class ScrollView;
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2014-07-25 14:38:02 +08:00
|
|
|
class CC_EX_DLL ScrollViewDelegate
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual ~ScrollViewDelegate() {}
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2014-07-17 06:10:59 +08:00
|
|
|
virtual void scrollViewDidScroll(ScrollView* view) {};
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2014-07-17 06:10:59 +08:00
|
|
|
virtual void scrollViewDidZoom(ScrollView* view) {};
|
2012-06-18 18:30:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-08-09 02:16:52 +08:00
|
|
|
* ScrollView support for cocos2d-x.
|
2012-06-18 18:30:07 +08:00
|
|
|
* It provides scroll view functionalities to cocos2d projects natively.
|
|
|
|
*/
|
2014-07-21 17:45:56 +08:00
|
|
|
class CC_EX_DLL ScrollView : public Layer, public ActionTweenDelegate
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-07-26 22:55:41 +08:00
|
|
|
enum class Direction
|
|
|
|
{
|
|
|
|
NONE = -1,
|
|
|
|
HORIZONTAL = 0,
|
|
|
|
VERTICAL,
|
|
|
|
BOTH
|
|
|
|
};
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* Returns an autoreleased scroll view object.
|
|
|
|
*
|
|
|
|
* @param size view size
|
|
|
|
* @param container parent object
|
|
|
|
* @return autoreleased scroll view object
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
static ScrollView* create(Size size, Node* container = NULL);
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an autoreleased scroll view object.
|
|
|
|
*
|
|
|
|
* @return autoreleased scroll view object
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
static ScrollView* create();
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-07-19 07:30:19 +08:00
|
|
|
ScrollView();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-19 07:30:19 +08:00
|
|
|
virtual ~ScrollView();
|
|
|
|
|
|
|
|
bool init();
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* Returns a scroll view object
|
|
|
|
*
|
|
|
|
* @param size view size
|
|
|
|
* @param container parent object
|
|
|
|
* @return scroll view object
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
bool initWithViewSize(Size size, Node* container = NULL);
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a new content offset. It ignores max/min offset. It just sets what's given. (just like UIKit's UIScrollView)
|
|
|
|
*
|
2013-08-01 21:40:13 +08:00
|
|
|
* @param offset The new offset.
|
|
|
|
* @param animated If true, the view will scroll to the new offset.
|
2012-06-18 18:30:07 +08:00
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
void setContentOffset(Vec2 offset, bool animated = false);
|
|
|
|
Vec2 getContentOffset();
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* Sets a new content offset. It ignores max/min offset. It just sets what's given. (just like UIKit's UIScrollView)
|
|
|
|
* You can override the animation duration with this method.
|
|
|
|
*
|
2013-08-01 21:40:13 +08:00
|
|
|
* @param offset The new offset.
|
|
|
|
* @param dt The animation duration.
|
2012-06-18 18:30:07 +08:00
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
void setContentOffsetInDuration(Vec2 offset, float dt);
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
void setZoomScale(float s);
|
|
|
|
/**
|
|
|
|
* Sets a new scale and does that for a predefined duration.
|
|
|
|
*
|
2013-08-01 21:40:13 +08:00
|
|
|
* @param s The new scale vale
|
|
|
|
* @param animated If true, scaling is animated
|
2012-06-18 18:30:07 +08:00
|
|
|
*/
|
|
|
|
void setZoomScale(float s, bool animated);
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float getZoomScale();
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a new scale for container in a given duration.
|
|
|
|
*
|
2013-08-01 21:40:13 +08:00
|
|
|
* @param s The new scale value
|
|
|
|
* @param dt The animation duration
|
2012-06-18 18:30:07 +08:00
|
|
|
*/
|
|
|
|
void setZoomScaleInDuration(float s, float dt);
|
2014-08-03 20:22:16 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set min scale
|
|
|
|
*
|
|
|
|
* @param minScale min scale
|
|
|
|
*/
|
|
|
|
void setMinScale(float minScale) {
|
|
|
|
_minScale = minScale;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Set max scale
|
|
|
|
*
|
|
|
|
* @param maxScale max scale
|
|
|
|
*/
|
|
|
|
void setMaxScale(float maxScale) {
|
|
|
|
_maxScale = maxScale;
|
|
|
|
}
|
|
|
|
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* Returns the current container's minimum offset. You may want this while you animate scrolling by yourself
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 minContainerOffset();
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* Returns the current container's maximum offset. You may want this while you animate scrolling by yourself
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 maxContainerOffset();
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* Determines if a given node's bounding box is in visible bounds
|
|
|
|
*
|
2013-08-01 21:40:13 +08:00
|
|
|
* @returns true if it is in visible bounds
|
2012-06-18 18:30:07 +08:00
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
bool isNodeVisible(Node * node);
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* Provided to make scroll view compatible with SWLayer's pause method
|
|
|
|
*/
|
2014-02-20 10:53:49 +08:00
|
|
|
void pause(Ref* sender);
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* Provided to make scroll view compatible with SWLayer's resume method
|
|
|
|
*/
|
2014-02-20 10:53:49 +08:00
|
|
|
void resume(Ref* sender);
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2013-10-25 16:06:52 +08:00
|
|
|
void setTouchEnabled(bool enabled);
|
2013-11-20 11:35:04 +08:00
|
|
|
bool isTouchEnabled() const;
|
2013-07-19 07:30:19 +08:00
|
|
|
bool isDragging() const {return _dragging;}
|
|
|
|
bool isTouchMoved() const { return _touchMoved; }
|
|
|
|
bool isBounceable() const { return _bounceable; }
|
2013-06-15 14:03:30 +08:00
|
|
|
void setBounceable(bool bBounceable) { _bounceable = bBounceable; }
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2012-06-19 10:34:15 +08:00
|
|
|
/**
|
2013-06-20 14:15:53 +08:00
|
|
|
* size to clip. Node boundingBox uses contentSize directly.
|
2012-06-19 10:34:15 +08:00
|
|
|
* It's semantically different what it actually means to common scroll views.
|
|
|
|
* Hence, this scroll view will use a separate size property.
|
|
|
|
*/
|
2013-07-19 07:30:19 +08:00
|
|
|
Size getViewSize() const { return _viewSize; }
|
2013-06-20 14:15:53 +08:00
|
|
|
void setViewSize(Size size);
|
2012-06-19 10:34:15 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Node * getContainer();
|
|
|
|
void setContainer(Node * pContainer);
|
2012-06-19 10:55:52 +08:00
|
|
|
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
2013-06-20 14:15:53 +08:00
|
|
|
* direction allowed to scroll. ScrollViewDirectionBoth by default.
|
2012-06-18 18:30:07 +08:00
|
|
|
*/
|
2013-07-26 22:55:41 +08:00
|
|
|
Direction getDirection() const { return _direction; }
|
|
|
|
virtual void setDirection(Direction eDirection) { _direction = eDirection; }
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
ScrollViewDelegate* getDelegate() { return _delegate; }
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @code
|
|
|
|
* when this function bound to js or lua,the input param are changed
|
|
|
|
* in js: var setDelegate(var jsObject)
|
|
|
|
* in lua: local setDelegate()
|
|
|
|
* @endcode
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
void setDelegate(ScrollViewDelegate* pDelegate) { _delegate = pDelegate; }
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2012-09-09 22:46:31 +08:00
|
|
|
void updateInset();
|
2013-07-19 07:30:19 +08:00
|
|
|
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* Determines whether it clips its children or not.
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
bool isClippingToBounds() { return _clippingToBounds; }
|
|
|
|
void setClippingToBounds(bool bClippingToBounds) { _clippingToBounds = bClippingToBounds; }
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
virtual bool onTouchBegan(Touch *touch, Event *event);
|
|
|
|
virtual void onTouchMoved(Touch *touch, Event *event);
|
|
|
|
virtual void onTouchEnded(Touch *touch, Event *event);
|
|
|
|
virtual void onTouchCancelled(Touch *touch, Event *event);
|
|
|
|
|
2013-07-19 07:30:19 +08:00
|
|
|
// Overrides
|
|
|
|
virtual void setContentSize(const Size & size) override;
|
|
|
|
virtual const Size& getContentSize() const override;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2014-05-31 07:42:05 +08:00
|
|
|
virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
|
2013-12-23 16:58:26 +08:00
|
|
|
|
|
|
|
using Node::addChild;
|
2013-07-19 07:30:19 +08:00
|
|
|
virtual void addChild(Node * child, int zOrder, int tag) override;
|
2014-06-25 11:27:48 +08:00
|
|
|
virtual void addChild(Node * child, int zOrder, const std::string &name) override;
|
2013-07-19 07:30:19 +08:00
|
|
|
|
2014-01-14 20:03:12 +08:00
|
|
|
/**
|
|
|
|
* CCActionTweenDelegate
|
|
|
|
*/
|
2014-01-14 20:27:14 +08:00
|
|
|
void updateTweenAction(float value, const std::string& key);
|
2013-07-19 07:30:19 +08:00
|
|
|
protected:
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* Relocates the container at the proper offset, in bounds of max/min offsets.
|
|
|
|
*
|
2013-08-01 21:40:13 +08:00
|
|
|
* @param animated If true, relocation is animated
|
2012-06-18 18:30:07 +08:00
|
|
|
*/
|
|
|
|
void relocateContainer(bool animated);
|
|
|
|
/**
|
|
|
|
* implements auto-scrolling behavior. change SCROLL_DEACCEL_RATE as needed to choose
|
|
|
|
* deacceleration speed. it must be less than 1.0f.
|
|
|
|
*
|
|
|
|
* @param dt delta
|
|
|
|
*/
|
|
|
|
void deaccelerateScrolling(float dt);
|
|
|
|
/**
|
|
|
|
* This method makes sure auto scrolling causes delegate to invoke its method
|
|
|
|
*/
|
|
|
|
void performedAnimatedScroll(float dt);
|
|
|
|
/**
|
|
|
|
* Expire animated scroll delegate calls
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
void stoppedAnimatedScroll(Node* node);
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* clip this view so that outside of the visible bounds can be hidden.
|
|
|
|
*/
|
|
|
|
void beforeDraw();
|
2014-01-07 17:46:24 +08:00
|
|
|
void onBeforeDraw();
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* retract what's done in beforeDraw so that there's no side effect to
|
|
|
|
* other nodes.
|
|
|
|
*/
|
|
|
|
void afterDraw();
|
2014-01-07 17:46:24 +08:00
|
|
|
void onAfterDraw();
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* Zoom handling
|
|
|
|
*/
|
|
|
|
void handleZoom();
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Rect getViewRect();
|
2014-08-04 22:20:47 +08:00
|
|
|
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* scroll view delegate
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
ScrollViewDelegate* _delegate;
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2013-07-26 22:55:41 +08:00
|
|
|
Direction _direction;
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* If YES, the view is being dragged.
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
bool _dragging;
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Content offset. Note that left-bottom point is the origin
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 _contentOffset;
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Container holds scroll view contents, Sets the scrollable container object of the scroll view
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
Node* _container;
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* Determiens whether user touch is moved after begin phase.
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
bool _touchMoved;
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* max inset point to limit scrolling by touch
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 _maxInset;
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* min inset point to limit scrolling by touch
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 _minInset;
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* Determines whether the scroll view is allowed to bounce or not.
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
bool _bounceable;
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
bool _clippingToBounds;
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* scroll speed
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 _scrollDistance;
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* Touch point
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 _touchPoint;
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* length between two fingers
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
float _touchLength;
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
2013-09-12 14:42:56 +08:00
|
|
|
* Touch objects to detect multitouch
|
2012-06-18 18:30:07 +08:00
|
|
|
*/
|
2013-09-12 14:42:56 +08:00
|
|
|
std::vector<Touch*> _touches;
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
2013-06-20 14:15:53 +08:00
|
|
|
* size to clip. Node boundingBox uses contentSize directly.
|
2012-06-18 18:30:07 +08:00
|
|
|
* It's semantically different what it actually means to common scroll views.
|
|
|
|
* Hence, this scroll view will use a separate size property.
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
Size _viewSize;
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* max and min scale
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
float _minScale, _maxScale;
|
2013-03-29 16:05:28 +08:00
|
|
|
/**
|
|
|
|
* scissor rect for parent, just for restoring GL_SCISSOR_BOX
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
Rect _parentScissorRect;
|
2013-06-15 14:03:30 +08:00
|
|
|
bool _scissorRestored;
|
2013-10-25 16:06:52 +08:00
|
|
|
|
|
|
|
/** Touch listener */
|
|
|
|
EventListenerTouchOneByOne* _touchListener;
|
2014-01-07 17:46:24 +08:00
|
|
|
|
|
|
|
CustomCommand _beforeDrawCommand;
|
|
|
|
CustomCommand _afterDrawCommand;
|
2012-06-18 18:30:07 +08:00
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of GUI group
|
|
|
|
/// @}
|
|
|
|
|
2012-06-18 18:30:07 +08:00
|
|
|
NS_CC_EXT_END
|
|
|
|
|
|
|
|
#endif /* __CCSCROLLVIEW_H__ */
|