axmol/cocos/gui/UIPageView.h

223 lines
6.8 KiB
C
Raw Normal View History

2013-09-13 22:20:20 +08:00
/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
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.
****************************************************************************/
2013-09-13 22:20:20 +08:00
#ifndef __UIPAGEVIEW_H__
#define __UIPAGEVIEW_H__
2013-11-06 16:04:06 +08:00
#include "gui/UILayout.h"
#include "gui/UIScrollInterface.h"
2013-09-13 22:20:20 +08:00
2013-12-23 15:02:52 +08:00
NS_CC_BEGIN
namespace ui {
2013-09-13 22:20:20 +08:00
2013-09-17 20:13:23 +08:00
typedef enum
{
PAGEVIEW_EVENT_TURNING,
}PageViewEventType;
typedef void (Ref::*SEL_PageViewEvent)(Ref*, PageViewEventType);
2013-09-17 20:13:23 +08:00
#define pagevieweventselector(_SELECTOR)(SEL_PageViewEvent)(&_SELECTOR)
2013-09-13 22:20:20 +08:00
typedef enum {
PAGEVIEW_TOUCHLEFT,
PAGEVIEW_TOUCHRIGHT
}PVTouchDir;
2013-12-23 15:02:52 +08:00
class PageView : public Layout , public UIScrollInterface
2013-09-13 22:20:20 +08:00
{
2014-03-04 16:51:35 +08:00
DECLARE_CLASS_GUI_INFO
2013-09-13 22:20:20 +08:00
public:
/**
* Default constructor
*/
2013-12-23 15:02:52 +08:00
PageView();
2013-09-13 22:20:20 +08:00
/**
* Default destructor
*/
2013-12-23 15:02:52 +08:00
virtual ~PageView();
2013-09-13 22:20:20 +08:00
/**
* Allocates and initializes.
*/
2013-12-23 15:02:52 +08:00
static PageView* create();
2013-09-13 22:20:20 +08:00
/**
* Add a widget to a page of pageview.
*
* @param widget widget to be added to pageview.
*
* @param pageIdx index of page.
*
* @param forceCreate if force create and there is no page exsit, pageview would create a default page for adding widget.
*/
2013-12-28 14:34:52 +08:00
void addWidgetToPage(Widget* widget, ssize_t pageIdx, bool forceCreate);
2013-09-13 22:20:20 +08:00
/**
* Push back a page to pageview.
*
* @param page page to be added to pageview.
*/
2013-12-23 15:02:52 +08:00
void addPage(Layout* page);
2013-09-13 22:20:20 +08:00
/**
* Inert a page to pageview.
*
* @param page page to be added to pageview.
*/
2013-12-23 15:02:52 +08:00
void insertPage(Layout* page, int idx);
2013-09-13 22:20:20 +08:00
/**
* Remove a page of pageview.
*
* @param page page which will be removed.
*/
2013-12-23 15:02:52 +08:00
void removePage(Layout* page);
2013-09-13 22:20:20 +08:00
/**
* Remove a page at index of pageview.
*
* @param index index of page.
*/
2013-12-28 14:34:52 +08:00
void removePageAtIndex(ssize_t index);
2013-09-13 22:20:20 +08:00
2013-11-13 20:01:57 +08:00
void removeAllPages();
2013-09-13 22:20:20 +08:00
/**
* scroll pageview to index.
*
* @param idx index of page.
*/
2013-12-28 14:34:52 +08:00
void scrollToPage(ssize_t idx);
2013-09-13 22:20:20 +08:00
/**
* Gets current page index.
*
* @return current page index.
*/
2013-12-28 14:34:52 +08:00
ssize_t getCurPageIndex() const;
2013-09-13 22:20:20 +08:00
2013-12-23 15:02:52 +08:00
Vector<Layout*>& getPages();
2013-11-06 16:04:06 +08:00
2013-12-28 14:34:52 +08:00
Layout* getPage(ssize_t index);
2013-11-13 20:01:57 +08:00
2013-09-17 20:13:23 +08:00
// event
void addEventListenerPageView(Ref *target, SEL_PageViewEvent selector);
2013-11-06 16:04:06 +08:00
2013-09-13 22:20:20 +08:00
2013-11-13 20:01:57 +08:00
2013-09-13 22:20:20 +08:00
2013-12-23 15:02:52 +08:00
virtual bool onTouchBegan(Touch *touch, Event *unusedEvent) override;
virtual void onTouchMoved(Touch *touch, Event *unusedEvent) override;
virtual void onTouchEnded(Touch *touch, Event *unusedEvent) override;
virtual void onTouchCancelled(Touch *touch, Event *unusedEvent) override;
2013-09-13 22:20:20 +08:00
//override "update" method of widget.
2013-11-14 13:42:10 +08:00
virtual void update(float dt) override;
2013-11-13 20:01:57 +08:00
/**
* Sets LayoutType.
*
* @see LayoutType
*
* @param LayoutType
*/
2013-11-14 13:42:10 +08:00
virtual void setLayoutType(LayoutType type) override{};
2013-11-13 20:01:57 +08:00
/**
* Gets LayoutType.
*
* @see LayoutType
*
* @return LayoutType
*/
2013-11-14 13:42:10 +08:00
virtual LayoutType getLayoutType() const override{return LAYOUT_ABSOLUTE;};
2013-11-13 20:01:57 +08:00
2013-09-17 17:59:20 +08:00
/**
* Returns the "class name" of widget.
*/
2013-12-23 15:02:52 +08:00
virtual std::string getDescription() const override;
2014-01-09 09:58:27 +08:00
virtual void onEnter() override;
2013-09-13 22:20:20 +08:00
protected:
2013-12-23 15:02:52 +08:00
virtual void addChild(Node * child) override;
virtual void addChild(Node * child, int zOrder) override;
virtual void addChild(Node* child, int zOrder, int tag) override;
virtual void removeChild(Node* widget, bool cleanup = true) override;
2013-11-14 13:42:10 +08:00
virtual void removeAllChildren() override;
virtual void removeAllChildrenWithCleanup(bool cleanup) override;
2013-12-23 15:02:52 +08:00
virtual Vector<Node*>& getChildren() override{return Widget::getChildren();};
virtual const Vector<Node*>& getChildren() const override{return Widget::getChildren();};
virtual ssize_t getChildrenCount() const override {return Widget::getChildrenCount();};
virtual Node * getChildByTag(int tag) override {return Widget::getChildByTag(tag);};
virtual Widget* getChildByName(const char* name) override {return Widget::getChildByName(name);};
2013-11-14 13:42:10 +08:00
virtual bool init() override;
2013-12-23 15:02:52 +08:00
Layout* createPage();
2013-12-28 14:34:52 +08:00
float getPositionXByIndex(ssize_t idx);
2013-09-13 22:20:20 +08:00
void updateBoundaryPages();
2013-12-23 15:02:52 +08:00
virtual void handlePressLogic(const Point &touchPoint) override;
virtual void handleMoveLogic(const Point &touchPoint) override;
virtual void handleReleaseLogic(const Point &touchPoint) override;
virtual void interceptTouchEvent(int handleState, Widget* sender, const Point &touchPoint) override;
virtual void checkChildInfo(int handleState, Widget* sender, const Point &touchPoint) override;
2013-09-13 22:20:20 +08:00
virtual bool scrollPages(float touchOffset);
void movePages(float offset);
void pageTurningEvent();
void updateChildrenSize();
void updateChildrenPosition();
2013-11-14 13:42:10 +08:00
virtual void onSizeChanged() override;
2013-12-23 15:02:52 +08:00
virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;
virtual void copyClonedWidgetChildren(Widget* model) override;
virtual void setClippingEnabled(bool enabled) override {Layout::setClippingEnabled(enabled);};
virtual void doLayout() override{if (!_doLayoutDirty){return;} _doLayoutDirty = false;};
2013-09-13 22:20:20 +08:00
protected:
2013-12-28 14:34:52 +08:00
ssize_t _curPageIdx;
2013-12-23 15:02:52 +08:00
Vector<Layout*> _pages;
2013-09-16 20:54:13 +08:00
PVTouchDir _touchMoveDir;
float _touchStartLocation;
float _touchMoveStartLocation;
2013-12-23 15:02:52 +08:00
Point _movePagePoint;
Widget* _leftChild;
Widget* _rightChild;
2013-09-16 20:54:13 +08:00
float _leftBoundary;
float _rightBoundary;
bool _isAutoScrolling;
float _autoScrollDistance;
float _autoScrollSpeed;
int _autoScrollDir;
float _childFocusCancelOffset;
Ref* _pageViewEventListener;
2013-11-08 18:43:06 +08:00
SEL_PageViewEvent _pageViewEventSelector;
2013-11-06 16:04:06 +08:00
2013-09-13 22:20:20 +08:00
};
}
2013-12-23 15:02:52 +08:00
NS_CC_END
2013-09-13 22:20:20 +08:00
2013-12-23 15:02:52 +08:00
#endif /* defined(__PageView__) */