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__
|
|
|
|
|
2013-02-16 19:36:21 +08:00
|
|
|
#include "cocos2d.h"
|
2012-07-20 14:05:52 +08:00
|
|
|
#include "ExtensionMacros.h"
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup GUI
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2012-06-18 18:30:07 +08:00
|
|
|
typedef enum {
|
2012-09-10 13:39:12 +08:00
|
|
|
kCCScrollViewDirectionNone = -1,
|
|
|
|
kCCScrollViewDirectionHorizontal = 0,
|
|
|
|
kCCScrollViewDirectionVertical,
|
|
|
|
kCCScrollViewDirectionBoth
|
2012-06-18 18:30:07 +08:00
|
|
|
} CCScrollViewDirection;
|
|
|
|
|
|
|
|
class CCScrollView;
|
|
|
|
|
2012-07-20 14:05:52 +08:00
|
|
|
class CCScrollViewDelegate
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~CCScrollViewDelegate() {}
|
|
|
|
virtual void scrollViewDidScroll(CCScrollView* view) = 0;
|
|
|
|
virtual void scrollViewDidZoom(CCScrollView* view) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ScrollView support for cocos2d for iphone.
|
|
|
|
* It provides scroll view functionalities to cocos2d projects natively.
|
|
|
|
*/
|
2012-07-20 14:05:52 +08:00
|
|
|
class CCScrollView : public CCLayer
|
2012-06-18 18:30:07 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CCScrollView();
|
|
|
|
virtual ~CCScrollView();
|
|
|
|
|
|
|
|
bool init();
|
|
|
|
virtual void registerWithTouchDispatcher();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an autoreleased scroll view object.
|
|
|
|
*
|
|
|
|
* @param size view size
|
|
|
|
* @param container parent object
|
|
|
|
* @return autoreleased scroll view object
|
|
|
|
*/
|
|
|
|
static CCScrollView* create(CCSize size, CCNode* container = NULL);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an autoreleased scroll view object.
|
|
|
|
*
|
|
|
|
* @param size view size
|
|
|
|
* @param container parent object
|
|
|
|
* @return autoreleased scroll view object
|
|
|
|
*/
|
|
|
|
static CCScrollView* create();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a scroll view object
|
|
|
|
*
|
|
|
|
* @param size view size
|
|
|
|
* @param container parent object
|
|
|
|
* @return scroll view object
|
|
|
|
*/
|
|
|
|
bool initWithViewSize(CCSize size, CCNode* container = NULL);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a new content offset. It ignores max/min offset. It just sets what's given. (just like UIKit's UIScrollView)
|
|
|
|
*
|
|
|
|
* @param offset new offset
|
|
|
|
* @param If YES, the view scrolls to the new offset
|
|
|
|
*/
|
|
|
|
void setContentOffset(CCPoint offset, bool animated = false);
|
|
|
|
CCPoint getContentOffset();
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param offset new offset
|
|
|
|
* @param animation duration
|
|
|
|
*/
|
|
|
|
void setContentOffsetInDuration(CCPoint offset, float dt);
|
|
|
|
|
|
|
|
void setZoomScale(float s);
|
|
|
|
/**
|
|
|
|
* Sets a new scale and does that for a predefined duration.
|
|
|
|
*
|
|
|
|
* @param s a new scale vale
|
|
|
|
* @param animated if YES, scaling is animated
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param s a new scale value
|
|
|
|
* @param animation duration
|
|
|
|
*/
|
|
|
|
void setZoomScaleInDuration(float s, float dt);
|
|
|
|
/**
|
|
|
|
* Returns the current container's minimum offset. You may want this while you animate scrolling by yourself
|
|
|
|
*/
|
|
|
|
CCPoint minContainerOffset();
|
|
|
|
/**
|
|
|
|
* Returns the current container's maximum offset. You may want this while you animate scrolling by yourself
|
|
|
|
*/
|
|
|
|
CCPoint maxContainerOffset();
|
|
|
|
/**
|
|
|
|
* Determines if a given node's bounding box is in visible bounds
|
|
|
|
*
|
|
|
|
* @return YES if it is in visible bounds
|
|
|
|
*/
|
|
|
|
bool isNodeVisible(CCNode * node);
|
|
|
|
/**
|
|
|
|
* Provided to make scroll view compatible with SWLayer's pause method
|
|
|
|
*/
|
|
|
|
void pause(CCObject* sender);
|
|
|
|
/**
|
|
|
|
* Provided to make scroll view compatible with SWLayer's resume method
|
|
|
|
*/
|
|
|
|
void resume(CCObject* sender);
|
|
|
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
bool isDragging() {return _dragging;}
|
|
|
|
bool isTouchMoved() { return _touchMoved; }
|
|
|
|
bool isBounceable() { return _bounceable; }
|
|
|
|
void setBounceable(bool bBounceable) { _bounceable = bBounceable; }
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2012-06-19 10:34:15 +08:00
|
|
|
/**
|
|
|
|
* size to clip. CCNode boundingBox uses contentSize directly.
|
|
|
|
* It's semantically different what it actually means to common scroll views.
|
|
|
|
* Hence, this scroll view will use a separate size property.
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
CCSize getViewSize() { return _viewSize; }
|
2012-06-19 10:34:15 +08:00
|
|
|
void setViewSize(CCSize size);
|
|
|
|
|
2012-06-19 10:51:40 +08:00
|
|
|
CCNode * getContainer();
|
|
|
|
void setContainer(CCNode * pContainer);
|
2012-06-19 10:55:52 +08:00
|
|
|
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* direction allowed to scroll. CCScrollViewDirectionBoth by default.
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
CCScrollViewDirection getDirection() { return _direction; }
|
|
|
|
virtual void setDirection(CCScrollViewDirection eDirection) { _direction = eDirection; }
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CCScrollViewDelegate* getDelegate() { return _delegate; }
|
|
|
|
void setDelegate(CCScrollViewDelegate* pDelegate) { _delegate = pDelegate; }
|
2012-06-18 18:30:07 +08:00
|
|
|
|
|
|
|
/** override functions */
|
|
|
|
// optional
|
|
|
|
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
|
|
|
|
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
|
|
|
|
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
|
|
|
|
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
|
|
|
|
|
2012-07-27 18:20:11 +08:00
|
|
|
virtual void setContentSize(const CCSize & size);
|
2013-05-13 15:07:47 +08:00
|
|
|
virtual const CCSize& getContentSize() const;
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2012-09-09 22:46:31 +08:00
|
|
|
void updateInset();
|
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
|
|
|
|
|
|
|
virtual void visit();
|
|
|
|
virtual void addChild(CCNode * child, int zOrder, int tag);
|
|
|
|
virtual void addChild(CCNode * child, int zOrder);
|
|
|
|
virtual void addChild(CCNode * child);
|
|
|
|
void setTouchEnabled(bool e);
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Relocates the container at the proper offset, in bounds of max/min offsets.
|
|
|
|
*
|
|
|
|
* @param animated If YES, relocation is animated
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
void stoppedAnimatedScroll(CCNode* node);
|
|
|
|
/**
|
|
|
|
* clip this view so that outside of the visible bounds can be hidden.
|
|
|
|
*/
|
|
|
|
void beforeDraw();
|
|
|
|
/**
|
|
|
|
* retract what's done in beforeDraw so that there's no side effect to
|
|
|
|
* other nodes.
|
|
|
|
*/
|
|
|
|
void afterDraw();
|
|
|
|
/**
|
|
|
|
* Zoom handling
|
|
|
|
*/
|
|
|
|
void handleZoom();
|
|
|
|
|
|
|
|
protected:
|
2013-02-16 14:18:13 +08:00
|
|
|
CCRect getViewRect();
|
|
|
|
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* current zoom scale
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
float _zoomScale;
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* min zoom scale
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
float _minZoomScale;
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* max zoom scale
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
float _maxZoomScale;
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* scroll view delegate
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
CCScrollViewDelegate* _delegate;
|
2012-06-18 18:30:07 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CCScrollViewDirection _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
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
CCPoint _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-15 14:03:30 +08:00
|
|
|
CCNode* _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
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
CCPoint _maxInset;
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* min inset point to limit scrolling by touch
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
CCPoint _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
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
CCPoint _scrollDistance;
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* Touch point
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
CCPoint _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
|
|
|
/**
|
|
|
|
* UITouch objects to detect multitouch
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
CCArray* _touches;
|
2012-06-18 18:30:07 +08:00
|
|
|
/**
|
|
|
|
* size to clip. CCNode boundingBox uses contentSize directly.
|
|
|
|
* It's semantically different what it actually means to common scroll views.
|
|
|
|
* Hence, this scroll view will use a separate size property.
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
CCSize _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-15 14:03:30 +08:00
|
|
|
CCRect _parentScissorRect;
|
|
|
|
bool _scissorRestored;
|
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__ */
|