axmol/cocos/ui/UIPageView.h

370 lines
9.8 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
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/UIListView.h"
#include "ui/GUIExport.h"
/**
* @addtogroup ui
* @{
*/
NS_CC_BEGIN
2021-12-25 10:04:45 +08:00
namespace ui
{
2019-11-23 20:27:39 +08:00
class PageViewIndicator;
/**
*@brief Layout manager that allows the user to flip left & right and up & down through pages of data.
*
*/
class CC_GUI_DLL PageView : public ListView
{
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
DECLARE_CLASS_GUI_INFO
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
public:
/**
* Page turn event type.
*/
enum class EventType
{
TURNING
};
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Touch direction type.
*/
enum class TouchDirection
{
LEFT,
RIGHT,
UP,
DOWN
};
/**
* PageView page turn event callback.
*/
typedef std::function<void(Ref*, EventType)> ccPageViewCallback;
/**
* Default constructor
* @js ctor
* @lua new
*/
PageView();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Default destructor
* @js NA
* @lua NA
*/
virtual ~PageView();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Create an empty PageView.
*@return A PageView instance.
*/
static PageView* create();
/**
* Changes direction
* Direction Direction::VERTICAL means vertical scroll, Direction::HORIZONTAL means horizontal scroll.
* @param direction Set the page view's scroll direction.
*/
virtual void setDirection(Direction direction) override;
/**
* Insert a page into the end of PageView.
*
* @param page Page to be inserted.
*/
void addPage(Widget* page);
/**
* Insert a page into PageView at a given index.
*
* @param page Page to be inserted.
* @param idx A given index.
*/
void insertPage(Widget* page, int idx);
/**
* Remove a page of PageView.
*
* @param page Page to be removed.
*/
void removePage(Widget* page);
/**
* Remove a page at a given index of PageView.
*
* @param index A given index.
*/
void removePageAtIndex(ssize_t index);
/**
* @brief Remove all pages of the PageView.
*/
void removeAllPages();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Scroll to a page with a given index.
*
* @param idx A given index in the PageView. Index start from 0 to pageCount -1.
*/
void scrollToPage(ssize_t idx);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Scroll to a page with a given index and with a given scroll time.
*
* @param idx A given index in the PageView. Index start from 0 to pageCount -1.
* @param time Scroll time must be >= 0. Otherwise last set scroll time will be used.
*/
void scrollToPage(ssize_t idx, float time);
/**
* Scroll to a page with a given index.
*
* @param itemIndex A given index in the PageView. Index start from 0 to pageCount -1.
*/
void scrollToItem(ssize_t itemIndex);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Scroll to a item with a given index and with a given scroll time.
*
* @param idx A given index in the PageView. Index start from 0 to pageCount -1.
* @param time Scroll time must be >= 0. Otherwise last set scrolltime will be used.
*/
void scrollToItem(ssize_t idx, float time);
/**
* Gets current displayed page index.
* @return current page index.
*/
ssize_t getCurrentPageIndex();
/**
* 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 setCurrentPageIndex(ssize_t index);
/**
* @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.
*/
void addEventListener(const ccPageViewCallback& callback);
using ScrollView::addEventListener;
2021-12-25 10:04:45 +08:00
// override methods
2019-11-23 20:27:39 +08:00
virtual std::string getDescription() const override;
/**
* @brief Toggle page indicator enabled.
*
* @param enabled True if enable page indicator, false otherwise.
*/
void setIndicatorEnabled(bool enabled);
/**
* @brief Query page indicator state.
*
* @return True if page indicator is enabled, false otherwise.
*/
bool getIndicatorEnabled() const { return _indicator != nullptr; }
/**
* @brief Set the page indicator's position using anchor point.
*
* @param positionAsAnchorPoint The position as anchor point.
*/
void setIndicatorPositionAsAnchorPoint(const Vec2& positionAsAnchorPoint);
/**
* @brief Get the page indicator's position as anchor point.
*
* @return positionAsAnchorPoint
*/
const Vec2& getIndicatorPositionAsAnchorPoint() const;
/**
* @brief Set the page indicator's position in page view.
*
* @param position The position in page view
*/
void setIndicatorPosition(const Vec2& position);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Get the page indicator's position.
*
* @return positionAsAnchorPoint
*/
const Vec2& getIndicatorPosition() const;
/**
* @brief Set space between page indicator's index nodes.
*
* @param spaceBetweenIndexNodes Space between nodes in pixel.
*/
void setIndicatorSpaceBetweenIndexNodes(float spaceBetweenIndexNodes);
/**
* @brief Get the space between page indicator's index nodes.
*
* @return spaceBetweenIndexNodes
*/
float getIndicatorSpaceBetweenIndexNodes() const;
/**
* @brief Set color of page indicator's selected index.
*
* @param color New color for selected (current) index.
*/
void setIndicatorSelectedIndexColor(const Color3B& color);
/**
* @brief Get the color of page indicator's selected index.
*
* @return color
*/
const Color3B& getIndicatorSelectedIndexColor() const;
/**
* @brief Set color of page indicator's index nodes.
*
* @param color New indicator node color.
*/
void setIndicatorIndexNodesColor(const Color3B& color);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Get the color of page indicator's index nodes.
*
* @return color
*/
const Color3B& getIndicatorIndexNodesColor() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Set opacity of page indicator's selected index.
*
* @param color New opacity for selected (current) index.
*/
void setIndicatorSelectedIndexOpacity(uint8_t opacity);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Get the opacity of page indicator's selected index.
*
* @return opacity
*/
uint8_t getIndicatorSelectedIndexOpacity() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Set opacity of page indicator's index nodes.
*
* @param opacity New indicator node opacity.
*/
void setIndicatorIndexNodesOpacity(uint8_t opacity);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Get the opacity of page indicator's index nodes.
*
* @return opacity
*/
uint8_t getIndicatorIndexNodesOpacity() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Set scale of page indicator's index nodes.
*
* @param indexNodesScale Scale of index nodes.
*/
void setIndicatorIndexNodesScale(float indexNodesScale);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* sets texture for index nodes.
*
* @param fileName File name of texture.
* @param resType @see TextureResType .
*/
2021-12-26 23:26:34 +08:00
void setIndicatorIndexNodesTexture(std::string_view texName,
2021-12-25 10:04:45 +08:00
Widget::TextureResType texType = Widget::TextureResType::LOCAL);
2019-11-23 20:27:39 +08:00
/**
* @brief Get scale of page indicator's index nodes.
*
* @return indexNodesScale
*/
float getIndicatorIndexNodesScale() const;
void setAutoScrollStopEpsilon(float epsilon);
2021-12-25 10:04:45 +08:00
CC_CONSTRUCTOR_ACCESS : virtual bool init() override;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// override methods
2019-11-23 20:27:39 +08:00
virtual void doLayout() override;
protected:
void pageTurningEvent();
virtual float getAutoScrollStopEpsilon() const override;
2021-12-25 10:04:45 +08:00
virtual void remedyLayoutParameter(Widget* item) override;
2019-11-23 20:27:39 +08:00
virtual void moveInnerContainer(const Vec2& deltaMove, bool canStartBounceBack) override;
virtual void onItemListChanged() override;
virtual void onSizeChanged() override;
2021-12-25 10:04:45 +08:00
virtual void handleReleaseLogic(Touch* touch) override;
virtual void handlePressLogic(Touch* touch) override;
2019-11-23 20:27:39 +08:00
virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;
void refreshIndicatorPosition();
protected:
PageViewIndicator* _indicator;
Vec2 _indicatorPositionAsAnchorPoint;
ssize_t _currentPageIndex;
float _childFocusCancelOffset;
ccPageViewCallback _eventCallback;
float _autoScrollStopEpsilon;
ssize_t _previousPageIndex;
bool _isTouchBegin;
};
2021-12-25 10:04:45 +08:00
} // namespace ui
2019-11-23 20:27:39 +08:00
NS_CC_END
// end of ui group
/// @}
#endif /* defined(__PageView__) */