cocos2dx学习之UIListView.h

这篇具有很好参考价值的文章主要介绍了cocos2dx学习之UIListView.h。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

/****************************************************************************

Copyright (c) 2013-2017 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 __UILISTVIEW_H__

#define __UILISTVIEW_H__

#include "ui/UIScrollView.h"

#include "ui/GUIExport.h"

/**

* @addtogroup ui

* @{

*/

NS_CC_BEGIN

namespace ui{

/**

* ListView click item event type.

*/

typedef enum

{

LISTVIEW_ONSELECTEDITEM_START,

LISTVIEW_ONSELECTEDITEM_END

}ListViewEventType;

/**

* A callback which would be called when a ListView item is clicked.

*@deprecated Use `ccListViewCallback` instead.

*/

typedef void (Ref::*SEL_ListViewEvent)(Ref*,ListViewEventType);

#define listvieweventselector(_SELECTOR) (SEL_ListViewEvent)(&_SELECTOR)

/**

*@brief ListView is a view group that displays a list of scrollable items.

*The list items are inserted to the list by using `addChild` or `insertDefaultItem`.

* @warning The list item in ListView doesn't support cell reuse at the moment, if you have a large amount of data need to be displayed, use `TableView` instead.

* ListView is a subclass of `ScrollView`, so it shares many features of ScrollView.

*/

class CC_GUI_DLL ListView : public ScrollView

{

DECLARE_CLASS_GUI_INFO

public:

/**

* Gravity for docking elements in ListView.

*/

enum class Gravity

{

LEFT,

RIGHT,

CENTER_HORIZONTAL,

TOP,

BOTTOM,

CENTER_VERTICAL

};

/**

* ListView element item click event.

*/

enum class EventType

{

ON_SELECTED_ITEM_START,

ON_SELECTED_ITEM_END

};

/**

* ListView supports magnetic scroll.

* With CENTER type, ListView tries to align its items in center of current view.

* With BOTH_END type, ListView tries to align its items in left or right end if it is horizontal, top or bottom in vertical. The aligning side (left or right, top or bottom) is determined by user's scroll direction.

*/

enum class MagneticType

{

NONE,

CENTER,

BOTH_END,

LEFT,

RIGHT,

TOP,

BOTTOM,

};

/**

* ListView item click callback.

*/

typedef std::function<void(Ref*, EventType)> ccListViewCallback;

/**

* Default constructor

* @js ctor

* @lua new

*/

ListView();

/**

* Default destructor

* @js NA

* @lua NA

*/

virtual ~ListView();

/**

* Create an empty ListView.

*@return A ListView instance.

*/

static ListView* create();

/**

* Set an item model for listview.

*

* When calling `pushBackDefaultItem`, the model will be used as a blueprint and new model copy will be inserted into ListView.

* @param model Model in `Widget*`.

*/

void setItemModel(Widget* model);

/**

* Insert a default item(create by a cloned model) at the end of the listview.

*/

void pushBackDefaultItem();

/**

* Insert a default item(create by cloning model) into listview at a give index.

*@param index An index in ssize_t.

*/

void insertDefaultItem(ssize_t index);

/**

* Insert a custom item into the end of ListView.

*@param item An item in `Widget*`.

*/

void pushBackCustomItem(Widget* item);

/**

* @brief Insert a custom widget into ListView at a given index.

*

* @param item A widget pointer to be inserted.

* @param index A given index in ssize_t.

*/

void insertCustomItem(Widget* item, ssize_t index);

/**

* Removes the last item of ListView.

*/

void removeLastItem();

/**

* Remove an item at given index.

*

* @param index A given index in ssize_t.

*/

void removeItem(ssize_t index);

/**

* @brief Remove all items in current ListView.

*

*/

void removeAllItems();

/**

* Return an item at a given index.

*

* @param index A given index in ssize_t.

* @return A widget instance.

*/

Widget* getItem(ssize_t index)const;

/**

* Return all items in a ListView.

*@returns A vector of widget pointers.

*/

Vector<Widget*>& getItems();

/**

* Return the index of specified widget.

*

* @param item A widget pointer.

* @return The index of a given widget in ListView.

*/

ssize_t getIndex(Widget* item) const;

/**

* Set the gravity of ListView.

* @see `ListViewGravity`

*/

void setGravity(Gravity gravity);

/**

* Set magnetic type of ListView.

* @see `MagneticType`

*/

void setMagneticType(MagneticType magneticType);

/**

* Get magnetic type of ListView.

*/

MagneticType getMagneticType() const;

/**

* Set magnetic allowed out of boundary.

*/

void setMagneticAllowedOutOfBoundary(bool magneticAllowedOutOfBoundary);

/**

* Query whether the magnetic out of boundary is allowed.

*/

bool getMagneticAllowedOutOfBoundary() const;

/**

* Set the margin between each item in ListView.

*

* @param margin A margin in float.

*/

void setItemsMargin(float margin);

/**

* @brief Query margin between each item in ListView.

*

* @return A margin in float.

*/

float getItemsMargin()const;

/**

* Change padding with left, top, right, and bottom padding.

* @param l Left padding in float.

* @param t Top margin in float.

* @param r Right margin in float.

* @param b Bottom margin in float.

*/

void setPadding(float l, float t, float r, float b);

/**

* Change padding with left padding

* @param l Left padding in float.

*/

void setLeftPadding(float l);

/**

* Change padding with top padding

* @param t Top padding in float

*/

void setTopPadding(float t);

/**

* Change padding with right padding

* @param r Right padding in float

*/

void setRightPadding(float r);

/**

* Change padding with bottom padding

* @param b Bottom padding in float

*/

void setBottomPadding(float b);

/**

* Get the left padding in ListView

* @return Left padding in float

*/

float getLeftPadding() const;

/**

* Get the top padding in ListView

* @return Top padding in float

*/

float getTopPadding() const;

/**

* Get the right padding in ListView

* @return Right padding in float

*/

float getRightPadding() const;

/**

* Get the bottom padding in ListView

* @return Bottom padding in float

*/

float getBottomPadding() const;

/**

* Set the time in seconds to scroll between items.

* Subsequent calls of function 'scrollToItem', will take 'time' seconds for scrolling.

* @param time The seconds needed to scroll between two items. 'time' must be >= 0

* @see scrollToItem(ssize_t, const Vec2&, const Vec2&)

*/

void setScrollDuration(float time);

/**

* Get the time in seconds to scroll between items.

* @return The time in seconds to scroll between items

* @see setScrollDuration(float)

*/

float getScrollDuration() const;

//override methods

virtual void doLayout() override;

virtual void requestDoLayout() override;

virtual void addChild(Node* child)override;

virtual void addChild(Node * child, int localZOrder)override;

virtual void addChild(Node* child, int zOrder, int tag) override;

virtual void addChild(Node* child, int zOrder, const std::string &name) override;

virtual void removeAllChildren() override;

virtual void removeAllChildrenWithCleanup(bool cleanup) override;

virtual void removeChild(Node* child, bool cleanup = true) override;

/**

* @brief Query the closest item to a specific position in inner container.

*

* @param targetPosition Specifies the target position in inner container's coordinates.

* @param itemAnchorPoint Specifies an anchor point of each item for position to calculate distance.

* @return An item instance if list view is not empty. Otherwise, returns null.

*/

Widget* getClosestItemToPosition(const Vec2& targetPosition, const Vec2& itemAnchorPoint) const;

/**

* @brief Query the closest item to a specific position in current view.

* For instance, to find the item in the center of view, call 'getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE, Vec2::ANCHOR_MIDDLE)'.

*

* @param positionRatioInView Specifies the target position with ratio in list view's content size.

* @param itemAnchorPoint Specifies an anchor point of each item for position to calculate distance.

* @return An item instance if list view is not empty. Otherwise, returns null.

*/

Widget* getClosestItemToPositionInCurrentView(const Vec2& positionRatioInView, const Vec2& itemAnchorPoint) const;

/**

* @brief Query the center item

* @return An item instance.

*/

Widget* getCenterItemInCurrentView() const;

/**

* @brief Query the leftmost item in horizontal list

* @return An item instance.

*/

Widget* getLeftmostItemInCurrentView() const;

/**

* @brief Query the rightmost item in horizontal list

* @return An item instance.

*/

Widget* getRightmostItemInCurrentView() const;

/**

* @brief Query the topmost item in horizontal list

* @return An item instance.

*/

Widget* getTopmostItemInCurrentView() const;

/**

* @brief Query the bottommost item in horizontal list

* @return An item instance.

*/

Widget* getBottommostItemInCurrentView() const;

/**

* Override functions

*/

virtual void jumpToBottom() override;

virtual void jumpToTop() override;

virtual void jumpToLeft() override;

virtual void jumpToRight() override;

virtual void jumpToTopLeft() override;

virtual void jumpToTopRight() override;

virtual void jumpToBottomLeft() override;

virtual void jumpToBottomRight() override;

virtual void jumpToPercentVertical(float percent) override;

virtual void jumpToPercentHorizontal(float percent) override;

virtual void jumpToPercentBothDirection(const Vec2& percent) override;

/**

* @brief Jump to specific item

* @param itemIndex Specifies the item's index

* @param positionRatioInView Specifies the position with ratio in list view's content size.

* @param itemAnchorPoint Specifies an anchor point of each item for position to calculate distance.

*/

void jumpToItem(ssize_t itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint);

/**

* @brief Scroll to specific item

* @param positionRatioInView Specifies the position with ratio in list view's content size.

* @param itemAnchorPoint Specifies an anchor point of each item for position to calculate distance.

* @param timeInSec Scroll time

*/

void scrollToItem(ssize_t itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint);

void scrollToItem(ssize_t itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint, float timeInSec);

/**

* @brief Query current selected widget's index.

*

* @return An index of a selected item.

*/

ssize_t getCurSelectedIndex() const;

/**

* @brief Set current selected widget's index and call TouchEventType::ENDED event.

* @param itemIndex A index of a selected item.

*/

void setCurSelectedIndex(int itemIndex);

/**

* Add an event click callback to ListView, then one item of Listview is clicked, the callback will be called.

*@deprecated Use `addEventListener` instead.

*@param target A pointer of `Ref*` type.

*@param selector A member function pointer with type of `SEL_ListViewEvent`.

*/

CC_DEPRECATED_ATTRIBUTE void addEventListenerListView(Ref* target, SEL_ListViewEvent selector);

/**

* Add an event click callback to ListView, then one item of Listview is clicked, the callback will be called.

*@param callback A callback function with type of `ccListViewCallback`.

*/

void addEventListener(const ccListViewCallback& callback);

using ScrollView::addEventListener;

/**

* Changes scroll direction of scrollview.

*

* Direction Direction::VERTICAL means vertical scroll, Direction::HORIZONTAL means horizontal scroll.

* @param dir Set the list view's scroll direction.

*/

virtual void setDirection(Direction dir) override;

virtual std::string getDescription() const override;

/**

* @brief Refresh view and layout of ListView manually.

* This method will mark ListView content as dirty and the content view will be refreshed in the next frame.

* @deprecated Use method requestDoLayout() instead

*/

CC_DEPRECATED_ATTRIBUTE void requestRefreshView();

/**

* @brief Refresh content view of ListView.

* @deprecated Use method forceDoLayout() instead

*/

CC_DEPRECATED_ATTRIBUTE void refreshView();

CC_CONSTRUCTOR_ACCESS:

virtual bool init() override;

protected:

virtual void handleReleaseLogic(Touch *touch) override;

virtual void onItemListChanged();

virtual void remedyLayoutParameter(Widget* item);

void updateInnerContainerSize();

void remedyVerticalLayoutParameter(LinearLayoutParameter* layoutParameter, ssize_t itemIndex);

void remedyHorizontalLayoutParameter(LinearLayoutParameter* layoutParameter,ssize_t itemIndex);

virtual void onSizeChanged() override;

virtual Widget* createCloneInstance() override;

virtual void copySpecialProperties(Widget* model) override;

virtual void copyClonedWidgetChildren(Widget* model) override;

void selectedItemEvent(TouchEventType event);

virtual void interceptTouchEvent(Widget::TouchEventType event,Widget* sender,Touch* touch) override;

virtual Vec2 getHowMuchOutOfBoundary(const Vec2& addition = Vec2::ZERO) override;

virtual void startAttenuatingAutoScroll(const Vec2& deltaMove, const Vec2& initialVelocity) override;

void startMagneticScroll();

Vec2 calculateItemDestination(const Vec2& positionRatioInView, Widget* item, const Vec2& itemAnchorPoint);

protected:

Widget* _model;

Vector<Widget*> _items;

Gravity _gravity;

MagneticType _magneticType;

bool _magneticAllowedOutOfBoundary;

float _itemsMargin;

float _leftPadding;

float _topPadding;

float _rightPadding;

float _bottomPadding;

float _scrollTime;

ssize_t _curSelectedIndex;

bool _innerContainerDoLayoutDirty;

Ref* _listViewEventListener;

#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

SEL_ListViewEvent _listViewEventSelector;

#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

ccListViewCallback _eventCallback;

};

}

NS_CC_END

// end of ui group

/// @}

#endif /* defined(__ListView__) */文章来源地址https://www.toymoban.com/news/detail-676745.html

到了这里,关于cocos2dx学习之UIListView.h的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 《入门级-Cocos2dx4.0 塔防游戏开发》---第六课:欢迎界面开发(四、自定义精灵)

    目录  一、开发环境 二、开发内容 2. 1 新建自定义精灵 2.2 在welcome中创建新的menu 2.3 消息绑定以及消息响应 三、演示效果 四、知识点 4.1 内部消息响应 4.2 字体精灵说明 4.3 CC_SYNTHESIZE宏 操作系统:UOS1060专业版本。 cocos2dx:版本4.0 环境搭建教程:统信UOS下配置安装cocos2dx开发

    2024年02月11日
    浏览(42)
  • Pygame和Cocos2d

    Pygame和Cocos2d都是 Python 中常用的游戏引擎,但它们的设计目标、特点和使用场景略有不同。 Pygame与Cocos2d,目前是使用人数最多的两个Python游戏库。根据某知名产品点评网站的数据显示,Cocos2d排名第一,Pygame排名第二。 Pygame是一种基于SDL(Simple DirectMedia Layer)的Python库,主要

    2024年01月21日
    浏览(47)
  • cocos2D插件转3D插件

    cocos2D插件转3D插件

    2024年02月16日
    浏览(48)
  • Cocos2d-x实现文字颜色渐变

    更改CCLabel文件的updateColor()函数中的顶点颜色即可,代码如下: 效果图:  

    2024年02月12日
    浏览(44)
  • cocos2d-x C++与Lua交互

    Cocos版本: 3.10 Lua版本: 5.1.4 环境: window Visual Studio 2013 Lua 作为一种脚本语言, 它的运行需要有 宿主 的存在,通过 Lua虚拟栈 进行数据交互。 它的底层实现是 C 语言,C语言封装了很多的API接口,使得C/C++与Lua之间可以很方便的通信交互。 Lua的官网: https://www.lua.org/ 在coc

    2024年02月08日
    浏览(46)
  • 使用“Cocos引擎”创建的cpp工程如何在VS中调试Cocos2d-x源码

    前段时间Cocos2d-x更新了一个Cocos引擎,这是一个集合源码,IDE,Studio这一家老小的整合包,我们可以使用这个Cocos引擎来创建我们的项目。 在Cocos2d-x被整合到Cocos引擎之前,我们可以不那么方便地在我们创建的工程里调试Cocos2d-x的代码,当我们使用了整合后的Cocos引擎,调试

    2024年02月12日
    浏览(58)
  • cocos2d-x Android原生平台与Lua交互

    版本: cocos2d-x 语言: C++/Java/Lua cocos2d-x原生平台Android 接入第三方SDK, 需要了解 LuaJavaBridge 的使用。 它封装了用于 Java 和 Lua 的相互调用, 其调用通过 C++ 为中介,简要的流程: Lua调用Java: Lua - C++ - Java Java调用Lua: Java - C++ - Lua 以此方式来实现数据的交互, 接下来我们分别说下

    2024年02月08日
    浏览(49)
  • cocos2d-x 3.17 推箱子 0.1

    此版本为推箱子游戏的基础版本, 后续添加如下功能 人物动画 TiledMap 解析 射线碰撞检测 下一步提示, C++算法解析 道具, 可以回退一步 通过 cocos 命令新建一个项目, 将本项目的 Reources 目录和 Classes 目录复制过去即可 使用图集绘制 Sprite , 然后手动处理碰撞检测 游戏学论著翻译

    2024年02月08日
    浏览(49)
  • 绑定证书的cocos2d程序如何对wss长连接抓包

    点击上方 ↑ ↑ ↑ 蓝字[ 协议分析与还原 ]关注我们 “  解决在应用内绑定了证书的cocos2d应用wss长连接抓包的问题。 ” 在分析应用的过程中,不可避免地需要抓包。 没加密的还好说,随便找个工具抓下就可以分析了,有些应用数据使用tls加密了,但用的是系统内置的证书,

    2024年02月05日
    浏览(57)
  • win10 vs2022 搭建 cocos2d-x 3.17 开发环境

    https://cocos2d-x.org/download/ 也可以在 github 下载 https://github.com/cocos2d/cocos2d-x/tags https://docs.cocos2d-x.org/cocos2d-x/v3/zh/ https://docs.cocos2d-x.org/api-ref/cplusplus/v3x/index.html 需要 python2 , 安装后可以用 python2 的绝对路径运行 cocos 的 setup.py 如何没有把 python2 加入到环境变量, 可以修改引擎路径下

    2024年02月08日
    浏览(46)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包