/**************************************************************************** Copyright (c) 2013 cocos2d-x.org 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. ****************************************************************************/ #ifndef __UIPAGEVIEW_H__ #define __UIPAGEVIEW_H__ #include "../../Layouts/UILayout.h" #include "UIScrollInterface.h" NS_CC_BEGIN namespace ui { typedef enum { PAGEVIEW_EVENT_TURNING, }PageViewEventType; typedef void (CCObject::*SEL_PageViewEvent)(CCObject*, PageViewEventType); #define pagevieweventselector(_SELECTOR)(SEL_PageViewEvent)(&_SELECTOR) typedef enum { PAGEVIEW_TOUCHLEFT, PAGEVIEW_TOUCHRIGHT }PVTouchDir; class PageView : public Layout , public UIScrollInterface { DECLARE_CLASS_GUI_INFO public: /** * Default constructor */ PageView(); /** * Default destructor */ virtual ~PageView(); /** * Allocates and initializes. */ static PageView* create(); /** * 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. */ void addWidgetToPage(Widget* widget, int pageIdx, bool forceCreate); /** * Push back a page to pageview. * * @param page page to be added to pageview. */ void addPage(Layout* page); /** * Inert a page to pageview. * * @param page page to be added to pageview. */ void insertPage(Layout* page, int idx); /** * Remove a page of pageview. * * @param page page which will be removed. */ void removePage(Layout* page); /** * Remove a page at index of pageview. * * @param index index of page. */ void removePageAtIndex(int index); void removeAllPages(); /** * scroll pageview to index. * * @param idx index of page. */ void scrollToPage(int idx); /** * Gets current page index. * * @return current page index. */ int getCurPageIndex() const; CCArray* getPages(); Layout* getPage(int index); // event void addEventListenerPageView(CCObject *target, SEL_PageViewEvent selector); virtual bool onTouchBegan(CCTouch *touch, CCEvent *unusedEvent); virtual void onTouchMoved(CCTouch *touch, CCEvent *unusedEvent); virtual void onTouchEnded(CCTouch *touch, CCEvent *unusedEvent); virtual void onTouchCancelled(CCTouch *touch, CCEvent *unusedEvent); //override "update" method of widget. virtual void update(float dt); /** * Sets LayoutType. * * @see LayoutType * * @param LayoutType */ virtual void setLayoutType(LayoutType type){}; /** * Gets LayoutType. * * @see LayoutType * * @return LayoutType */ virtual LayoutType getLayoutType() const {return LAYOUT_ABSOLUTE;}; /** * Returns the "class name" of widget. */ virtual std::string getDescription() const; virtual void onEnter(); protected: virtual void addChild(CCNode * child); virtual void addChild(CCNode * child, int zOrder); virtual void addChild(CCNode* child, int zOrder, int tag); virtual void removeChild(CCNode* widget); virtual void removeChild(CCNode* widget, bool cleanup); virtual void removeAllChildren(); virtual void removeAllChildrenWithCleanup(bool cleanup); virtual CCArray* getChildren(){return Widget::getChildren();}; virtual unsigned int getChildrenCount() const {return Widget::getChildrenCount();}; virtual CCNode * getChildByTag(int tag) {return Widget::getChildByTag(tag);}; virtual Widget* getChildByName(const char* name) {return Widget::getChildByName(name);}; virtual bool init(); Layout* createPage(); float getPositionXByIndex(int idx); void updateBoundaryPages(); virtual void handlePressLogic(const CCPoint &touchPoint); virtual void handleMoveLogic(const CCPoint &touchPoint); virtual void handleReleaseLogic(const CCPoint &touchPoint); virtual void interceptTouchEvent(int handleState, Widget* sender, const CCPoint &touchPoint); virtual void checkChildInfo(int handleState, Widget* sender, const CCPoint &touchPoint); virtual bool scrollPages(float touchOffset); void movePages(float offset); void pageTurningEvent(); void updateChildrenSize(); void updateChildrenPosition(); virtual void onSizeChanged(); virtual Widget* createCloneInstance(); virtual void copySpecialProperties(Widget* model); virtual void copyClonedWidgetChildren(Widget* model); virtual void setClippingEnabled(bool enabled) {Layout::setClippingEnabled(enabled);}; virtual void doLayout() {if (!_doLayoutDirty){return;} _doLayoutDirty = false;}; protected: int _curPageIdx; CCArray* _pages; PVTouchDir _touchMoveDir; float _touchStartLocation; float _touchMoveStartLocation; CCPoint _movePagePoint; Widget* _leftChild; Widget* _rightChild; float _leftBoundary; float _rightBoundary; bool _isAutoScrolling; float _autoScrollDistance; float _autoScrollSpeed; int _autoScrollDir; float _childFocusCancelOffset; CCObject* _pageViewEventListener; SEL_PageViewEvent _pageViewEventSelector; }; } NS_CC_END #endif /* defined(__PageView__) */