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
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup ui
|
|
|
|
* @{
|
|
|
|
*/
|
2015-03-26 15:47:14 +08:00
|
|
|
NS_CC_BEGIN
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
*PageView page turn event type.
|
2015-03-26 15:47:14 +08:00
|
|
|
*@deprecated Use `PageView::EventType` instead.
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2014-07-10 00:45:27 +08:00
|
|
|
typedef enum
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
PAGEVIEW_EVENT_TURNING,
|
|
|
|
}PageViewEventType;
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
*A callback which would be called when a PageView turning event is happening.
|
2015-03-26 15:47:14 +08:00
|
|
|
*@deprecated Use `PageView::ccPageViewCallback` instead.
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
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)
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
2015-08-01 18:18:46 +08:00
|
|
|
*@brief Layout manager that allows the user to flip left & right and up & down through pages of data.
|
2015-03-25 18:17:58 +08:00
|
|
|
*
|
|
|
|
*/
|
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:
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
* Page turn event tpye.
|
|
|
|
*/
|
2014-05-12 10:31:52 +08:00
|
|
|
enum class EventType
|
|
|
|
{
|
|
|
|
TURNING
|
|
|
|
};
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
* Touch direction type.
|
|
|
|
*/
|
2014-05-12 10:31:52 +08:00
|
|
|
enum class TouchDirection
|
|
|
|
{
|
|
|
|
LEFT,
|
2015-08-01 18:18:46 +08:00
|
|
|
RIGHT,
|
|
|
|
UP,
|
|
|
|
DOWN
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class Direction
|
|
|
|
{
|
|
|
|
HORIZONTAL,
|
|
|
|
VERTICAL
|
2014-05-12 10:31:52 +08:00
|
|
|
};
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
*PageView page turn event callback.
|
|
|
|
*/
|
2014-05-12 10:31:52 +08:00
|
|
|
typedef std::function<void(Ref*,EventType)> ccPageViewCallback;
|
2015-03-25 18:17:58 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
/**
|
|
|
|
* Default constructor
|
2015-03-28 16:02:47 +08:00
|
|
|
* @js ctor
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua new
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
|
|
|
PageView();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default destructor
|
2015-03-28 16:02:47 +08:00
|
|
|
* @js NA
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua NA
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
|
|
|
virtual ~PageView();
|
|
|
|
|
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
* Create an empty PageView.
|
|
|
|
*@return A PageView instance.
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
|
|
|
static PageView* create();
|
|
|
|
|
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
* Add a widget as a page of PageView in a given index.
|
2014-03-11 17:13:54 +08:00
|
|
|
*
|
2015-03-25 18:17:58 +08:00
|
|
|
* @param widget Widget to be added to pageview.
|
|
|
|
* @param pageIdx A given index.
|
|
|
|
* @param forceCreate If `forceCreate` is true and `widget` isn't exists, pageview would create a default page and add it.
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
|
|
|
void addWidgetToPage(Widget* widget, ssize_t pageIdx, bool forceCreate);
|
|
|
|
|
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
* Insert a page into the end of PageView.
|
2014-03-11 17:13:54 +08:00
|
|
|
*
|
2015-03-25 18:17:58 +08:00
|
|
|
* @param page Page to be inserted.
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
|
|
|
void addPage(Layout* page);
|
|
|
|
|
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
* Insert a page into PageView at a given index.
|
2014-03-11 17:13:54 +08:00
|
|
|
*
|
2015-03-25 18:17:58 +08:00
|
|
|
* @param page Page to be inserted.
|
|
|
|
* @param idx A given index.
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
|
|
|
void insertPage(Layout* page, int idx);
|
|
|
|
|
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
* Remove a page of PageView.
|
2014-03-11 17:13:54 +08:00
|
|
|
*
|
2015-03-25 18:17:58 +08:00
|
|
|
* @param page Page to be removed.
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
|
|
|
void removePage(Layout* page);
|
|
|
|
|
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
* Remove a page at a given index of PageView.
|
2014-03-11 17:13:54 +08:00
|
|
|
*
|
2015-03-25 18:17:58 +08:00
|
|
|
* @param index A given index.
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
|
|
|
void removePageAtIndex(ssize_t index);
|
2015-08-01 18:18:46 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes scroll direction of PageView
|
|
|
|
*
|
|
|
|
* @see `Direction`
|
|
|
|
* @param direction Scroll direction enum.
|
|
|
|
* @since v3.8
|
|
|
|
*/
|
|
|
|
void setDirection(Direction direction);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query scroll direction of PageView.
|
|
|
|
*
|
|
|
|
* @see `Direction`
|
|
|
|
* @since v3.8
|
|
|
|
* @return PageView scroll direction.
|
|
|
|
*/
|
|
|
|
Direction getDirection()const;
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Remove all pages of the PageView.
|
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
void removeAllPages();
|
|
|
|
|
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
* Scroll to a page with a given index.
|
2014-03-11 17:13:54 +08:00
|
|
|
*
|
2015-07-15 17:02:02 +08:00
|
|
|
* @param idx A given index in the PageView. Index start from 0 to pageCount -1.
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
|
|
|
void scrollToPage(ssize_t idx);
|
2015-07-15 17:02:02 +08:00
|
|
|
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
* Gets current displayed page index.
|
2014-03-11 17:13:54 +08:00
|
|
|
* @return current page index.
|
|
|
|
*/
|
|
|
|
ssize_t getCurPageIndex() const;
|
2015-07-15 17:02:02 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Jump to a page with a given index without scrolling.
|
|
|
|
* This is the different between scrollToPage.
|
|
|
|
*
|
|
|
|
* @param index A given index in PageView. Index start from 0 to pageCount -1.
|
|
|
|
*/
|
|
|
|
void setCurPageIndex(ssize_t index);
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get all the pages in the PageView.
|
|
|
|
* @return A vector of Layout pionters.
|
|
|
|
*/
|
2014-05-22 11:43:02 +08:00
|
|
|
Vector<Layout*>& getPages();
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get a page at a given index
|
|
|
|
*
|
|
|
|
* @param index A given index.
|
|
|
|
* @return A layout pointer in PageView container.
|
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
Layout* getPage(ssize_t index);
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
* Add a page turn callback to PageView, then when one page is turning, the callback will be called.
|
2015-03-26 15:47:14 +08:00
|
|
|
*@deprecated Use `PageView::addEventListener` instead.
|
2015-03-25 18:17:58 +08:00
|
|
|
*@param target A pointer of `Ref*` type.
|
|
|
|
*@param selector A member function pointer with signature of `SEL_PageViewEvent`.
|
|
|
|
*/
|
2014-05-12 10:31:52 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE void addEventListenerPageView(Ref *target, SEL_PageViewEvent selector);
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Add a page turn callback to PageView, then when one page is turning, the callback will be called.
|
|
|
|
*
|
|
|
|
* @param callback A page turning callback.
|
|
|
|
*/
|
2014-05-14 16:14:28 +08:00
|
|
|
void addEventListener(const ccPageViewCallback& callback);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
//override methods
|
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;
|
|
|
|
virtual void update(float dt) override;
|
2014-05-12 18:00:54 +08:00
|
|
|
virtual void setLayoutType(Type type) override{};
|
|
|
|
virtual Type getLayoutType() const override{return Type::ABSOLUTE;};
|
2014-03-11 17:13:54 +08:00
|
|
|
virtual std::string getDescription() const override;
|
2015-03-30 15:47:47 +08:00
|
|
|
/**
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
virtual void onEnter() override;
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*@brief If you don't specify the value, the pageView will turn page when scrolling at the half width of a page.
|
|
|
|
*@param threshold A threshold in float.
|
2014-08-14 11:14:24 +08:00
|
|
|
*/
|
|
|
|
void setCustomScrollThreshold(float threshold);
|
2015-03-25 18:17:58 +08:00
|
|
|
|
2014-08-14 11:14:24 +08:00
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
*@brief Query the custom scroll threshold of the PageView.
|
|
|
|
*@return Custom scroll threshold in float.
|
2014-08-14 11:14:24 +08:00
|
|
|
*/
|
|
|
|
float getCustomScrollThreshold()const;
|
2015-03-25 18:17:58 +08:00
|
|
|
|
2014-08-14 11:14:24 +08:00
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
*@brief Set using user defined scroll page threshold or not.
|
2014-08-14 11:14:24 +08:00
|
|
|
* If you set it to false, then the default scroll threshold is pageView.width / 2
|
2015-03-25 18:17:58 +08:00
|
|
|
*@param flag True if using custom scroll threshold, false otherwise.
|
2014-08-14 11:14:24 +08:00
|
|
|
*/
|
|
|
|
void setUsingCustomScrollThreshold(bool flag);
|
2015-03-25 18:17:58 +08:00
|
|
|
|
2014-08-14 11:14:24 +08:00
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
*@brief Query whether use user defined scroll page threshold or not.
|
|
|
|
*@return True if using custom scroll threshold, false otherwise.
|
2014-08-14 11:14:24 +08:00
|
|
|
*/
|
|
|
|
bool isUsingCustomScrollThreshold()const;
|
2014-03-11 17:13:54 +08:00
|
|
|
|
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;
|
2015-08-01 18:18:46 +08:00
|
|
|
float getPositionYByIndex(ssize_t idx)const;
|
2014-05-27 10:44:31 +08:00
|
|
|
ssize_t getPageCount()const;
|
2014-05-21 18:30:25 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void updateBoundaryPages();
|
2015-08-01 18:18:46 +08:00
|
|
|
virtual bool scrollPages(Vec2 touchOffset);
|
|
|
|
void movePages(Vec2 offset);
|
2014-03-11 17:13:54 +08:00
|
|
|
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) ;
|
2015-03-15 02:33:15 +08:00
|
|
|
virtual void interceptTouchEvent(TouchEventType event, Widget* sender,Touch *touch) override;
|
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,
|
2015-08-01 18:18:46 +08:00
|
|
|
RIGHT,
|
|
|
|
UP,
|
|
|
|
DOWN
|
2014-05-22 14:09:01 +08:00
|
|
|
};
|
|
|
|
bool _isAutoScrolling;
|
|
|
|
float _autoScrollDistance;
|
|
|
|
float _autoScrollSpeed;
|
|
|
|
AutoScrollDirection _autoScrollDirection;
|
2015-08-01 18:18:46 +08:00
|
|
|
Direction _direction;
|
2014-05-22 14:09:01 +08:00
|
|
|
|
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-08-14 11:14:24 +08:00
|
|
|
float _customScrollThreshold;
|
2014-08-14 14:36:30 +08:00
|
|
|
bool _usingCustomScrollThreshold;
|
2014-08-14 11:14:24 +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
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2015-03-26 15:47:14 +08:00
|
|
|
NS_CC_END
|
2015-03-25 18:17:58 +08:00
|
|
|
// end of ui group
|
|
|
|
/// @}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
#endif /* defined(__PageView__) */
|