2014-03-11 17:13:54 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __UIPAGEVIEW_H__
|
|
|
|
#define __UIPAGEVIEW_H__
|
|
|
|
|
|
|
|
#include "ui/UILayout.h"
|
2014-07-21 17:45:56 +08:00
|
|
|
#include "ui/GUIExport.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
typedef enum
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
PAGEVIEW_EVENT_TURNING,
|
|
|
|
}PageViewEventType;
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
typedef void (Ref::*SEL_PageViewEvent)(Ref*, PageViewEventType);
|
2014-03-11 17:13:54 +08:00
|
|
|
#define pagevieweventselector(_SELECTOR)(SEL_PageViewEvent)(&_SELECTOR)
|
|
|
|
|
2014-07-21 17:45:56 +08:00
|
|
|
class CC_GUI_DLL PageView : public Layout
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
DECLARE_CLASS_GUI_INFO
|
|
|
|
|
|
|
|
public:
|
2014-05-12 10:31:52 +08:00
|
|
|
enum class EventType
|
|
|
|
{
|
|
|
|
TURNING
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class TouchDirection
|
|
|
|
{
|
|
|
|
LEFT,
|
|
|
|
RIGHT
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::function<void(Ref*,EventType)> ccPageViewCallback;
|
2014-03-11 17:13:54 +08:00
|
|
|
/**
|
|
|
|
* 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, ssize_t pageIdx, bool forceCreate);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Push back a page to pageview.
|
|
|
|
*
|
|
|
|
* @param page page to be added to pageview.
|
|
|
|
*/
|
|
|
|
void addPage(Layout* page);
|
|
|
|
|
|
|
|
/**
|
2014-05-22 15:08:53 +08:00
|
|
|
* Insert a page to pageview.
|
2014-03-11 17:13:54 +08:00
|
|
|
*
|
|
|
|
* @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(ssize_t index);
|
|
|
|
|
|
|
|
void removeAllPages();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scroll pageview to index.
|
|
|
|
*
|
|
|
|
* @param idx index of page.
|
|
|
|
*/
|
|
|
|
void scrollToPage(ssize_t idx);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets current page index.
|
|
|
|
*
|
|
|
|
* @return current page index.
|
|
|
|
*/
|
|
|
|
ssize_t getCurPageIndex() const;
|
|
|
|
|
2014-07-15 15:56:07 +08:00
|
|
|
|
2014-05-22 11:43:02 +08:00
|
|
|
Vector<Layout*>& getPages();
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
Layout* getPage(ssize_t index);
|
|
|
|
|
|
|
|
// event
|
2014-05-12 10:31:52 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE void addEventListenerPageView(Ref *target, SEL_PageViewEvent selector);
|
2014-05-14 16:14:28 +08:00
|
|
|
void addEventListener(const ccPageViewCallback& callback);
|
2014-03-11 17:13:54 +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;
|
|
|
|
|
|
|
|
//override "update" method of widget.
|
|
|
|
virtual void update(float dt) override;
|
|
|
|
/**
|
|
|
|
* Sets LayoutType.
|
|
|
|
*
|
|
|
|
* @see LayoutType
|
|
|
|
*
|
2014-05-22 15:08:53 +08:00
|
|
|
* @param type LayoutType
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
2014-05-12 18:00:54 +08:00
|
|
|
virtual void setLayoutType(Type type) override{};
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets LayoutType.
|
|
|
|
*
|
|
|
|
* @see LayoutType
|
|
|
|
*
|
|
|
|
* @return LayoutType
|
|
|
|
*/
|
2014-05-12 18:00:54 +08:00
|
|
|
virtual Type getLayoutType() const override{return Type::ABSOLUTE;};
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the "class name" of widget.
|
|
|
|
*/
|
|
|
|
virtual std::string getDescription() const override;
|
|
|
|
|
|
|
|
virtual void onEnter() override;
|
|
|
|
|
2014-03-21 13:44:29 +08:00
|
|
|
CC_CONSTRUCTOR_ACCESS:
|
|
|
|
virtual bool init() override;
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
protected:
|
2014-05-12 10:31:52 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
Layout* createPage();
|
2014-05-27 10:44:31 +08:00
|
|
|
float getPositionXByIndex(ssize_t idx)const;
|
|
|
|
ssize_t getPageCount()const;
|
2014-05-21 18:30:25 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void updateBoundaryPages();
|
|
|
|
virtual bool scrollPages(float touchOffset);
|
|
|
|
void movePages(float offset);
|
|
|
|
void pageTurningEvent();
|
2014-05-22 11:43:02 +08:00
|
|
|
void updateAllPagesSize();
|
|
|
|
void updateAllPagesPosition();
|
2014-05-22 14:09:01 +08:00
|
|
|
void autoScroll(float dt);
|
2014-05-21 15:44:08 +08:00
|
|
|
|
2014-06-06 16:48:49 +08:00
|
|
|
virtual void handleMoveLogic(Touch *touch) ;
|
|
|
|
virtual void handleReleaseLogic(Touch *touch) ;
|
|
|
|
virtual void interceptTouchEvent(TouchEventType event, Widget* sender,Touch *touch) ;
|
2014-05-22 13:51:44 +08:00
|
|
|
|
2014-05-22 15:23:13 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
virtual void onSizeChanged() override;
|
|
|
|
virtual Widget* createCloneInstance() override;
|
|
|
|
virtual void copySpecialProperties(Widget* model) override;
|
|
|
|
virtual void copyClonedWidgetChildren(Widget* model) override;
|
2014-05-22 13:51:44 +08:00
|
|
|
|
|
|
|
virtual void doLayout() override;
|
2014-05-21 15:44:08 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
protected:
|
2014-05-22 14:09:01 +08:00
|
|
|
enum class AutoScrollDirection
|
|
|
|
{
|
|
|
|
LEFT,
|
|
|
|
RIGHT
|
|
|
|
};
|
|
|
|
bool _isAutoScrolling;
|
|
|
|
float _autoScrollDistance;
|
|
|
|
float _autoScrollSpeed;
|
|
|
|
AutoScrollDirection _autoScrollDirection;
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
ssize_t _curPageIdx;
|
2014-05-22 11:43:02 +08:00
|
|
|
Vector<Layout*> _pages;
|
2014-05-21 16:07:33 +08:00
|
|
|
|
2014-05-22 09:56:42 +08:00
|
|
|
TouchDirection _touchMoveDirection;
|
2014-06-06 16:48:49 +08:00
|
|
|
|
2014-05-22 09:56:42 +08:00
|
|
|
Widget* _leftBoundaryChild;
|
|
|
|
Widget* _rightBoundaryChild;
|
2014-06-06 16:48:49 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
float _leftBoundary;
|
|
|
|
float _rightBoundary;
|
2014-05-22 14:09:01 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
float _childFocusCancelOffset;
|
2014-05-12 10:31:52 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
Ref* _pageViewEventListener;
|
2014-05-12 10:31:52 +08:00
|
|
|
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
#elif _MSC_VER >= 1400 //vs 2005 or higher
|
|
|
|
#pragma warning (push)
|
|
|
|
#pragma warning (disable: 4996)
|
|
|
|
#endif
|
2014-03-11 17:13:54 +08:00
|
|
|
SEL_PageViewEvent _pageViewEventSelector;
|
2014-05-12 10:31:52 +08:00
|
|
|
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
|
|
|
|
#pragma GCC diagnostic warning "-Wdeprecated-declarations"
|
|
|
|
#elif _MSC_VER >= 1400 //vs 2005 or higher
|
|
|
|
#pragma warning (pop)
|
|
|
|
#endif
|
|
|
|
ccPageViewCallback _eventCallback;
|
2014-03-11 17:13:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif /* defined(__PageView__) */
|