2013-09-13 22:20:20 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-01-07 11:47:11 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
#include "ui/UIListView.h"
|
|
|
|
#include "ui/UIHelper.h"
|
2013-10-15 18:00:03 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2015-09-11 14:59:46 +08:00
|
|
|
static const float DEFAULT_TIME_IN_SEC_FOR_SCROLL_TO_ITEM = 1.0f;
|
|
|
|
|
2014-02-24 18:56:45 +08:00
|
|
|
namespace ui {
|
2014-03-04 16:51:35 +08:00
|
|
|
|
|
|
|
IMPLEMENT_CLASS_GUI_INFO(ListView)
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
ListView::ListView():
|
2013-11-14 11:37:46 +08:00
|
|
|
_model(nullptr),
|
2014-05-12 09:51:23 +08:00
|
|
|
_gravity(Gravity::CENTER_VERTICAL),
|
2015-07-18 19:46:41 +08:00
|
|
|
_magneticType(MagneticType::NONE),
|
2015-09-08 21:15:35 +08:00
|
|
|
_magneticAllowedOutOfBoundary(true),
|
2013-11-08 20:29:49 +08:00
|
|
|
_itemsMargin(0.0f),
|
2017-06-15 14:56:54 +08:00
|
|
|
_leftPadding(0.0f),
|
|
|
|
_topPadding(0.0f),
|
|
|
|
_rightPadding(0.0f),
|
|
|
|
_bottomPadding(0.0f),
|
2016-06-29 09:48:03 +08:00
|
|
|
_scrollTime(DEFAULT_TIME_IN_SEC_FOR_SCROLL_TO_ITEM),
|
2015-07-20 17:50:02 +08:00
|
|
|
_curSelectedIndex(-1),
|
2015-09-14 11:51:03 +08:00
|
|
|
_innerContainerDoLayoutDirty(true),
|
2014-10-30 22:25:46 +08:00
|
|
|
_listViewEventListener(nullptr),
|
|
|
|
_listViewEventSelector(nullptr),
|
2014-05-12 09:51:23 +08:00
|
|
|
_eventCallback(nullptr)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2014-06-24 15:51:14 +08:00
|
|
|
this->setTouchEnabled(true);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
ListView::~ListView()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-14 11:37:46 +08:00
|
|
|
_listViewEventListener = nullptr;
|
|
|
|
_listViewEventSelector = nullptr;
|
2013-12-23 15:02:52 +08:00
|
|
|
_items.clear();
|
2014-03-31 11:34:26 +08:00
|
|
|
CC_SAFE_RELEASE(_model);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
ListView* ListView::create()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
ListView* widget = new (std::nothrow) ListView();
|
2013-09-13 22:20:20 +08:00
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
2013-11-14 11:37:46 +08:00
|
|
|
return nullptr;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
bool ListView::init()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
if (ScrollView::init())
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2015-09-16 17:59:56 +08:00
|
|
|
setDirection(Direction::VERTICAL);
|
2013-09-13 22:20:20 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::setItemModel(Widget *model)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2015-01-19 15:27:10 +08:00
|
|
|
if (nullptr == model)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2015-01-19 15:27:10 +08:00
|
|
|
CCLOG("Can't set a nullptr to item model!");
|
2013-11-06 16:04:06 +08:00
|
|
|
return;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_model);
|
|
|
|
_model = model;
|
|
|
|
CC_SAFE_RETAIN(_model);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2015-09-08 19:36:10 +08:00
|
|
|
void ListView::handleReleaseLogic(Touch *touch)
|
|
|
|
{
|
|
|
|
ScrollView::handleReleaseLogic(touch);
|
|
|
|
|
|
|
|
if(!_autoScrolling)
|
|
|
|
{
|
|
|
|
startMagneticScroll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 18:40:30 +08:00
|
|
|
void ListView::onItemListChanged()
|
|
|
|
{
|
|
|
|
_outOfBoundaryAmountDirty = true;
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::updateInnerContainerSize()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
switch (_direction)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2014-05-12 11:08:10 +08:00
|
|
|
case Direction::VERTICAL:
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-28 14:34:52 +08:00
|
|
|
size_t length = _items.size();
|
2017-12-14 11:32:34 +08:00
|
|
|
float totalHeight = (length == 0) ? 0.0f : (length - 1) * _itemsMargin + (_topPadding + _bottomPadding);
|
2013-12-26 16:17:52 +08:00
|
|
|
for (auto& item : _items)
|
2013-12-23 15:02:52 +08:00
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
totalHeight += item->getContentSize().height;
|
2013-12-23 15:02:52 +08:00
|
|
|
}
|
2014-06-20 10:40:16 +08:00
|
|
|
float finalWidth = _contentSize.width;
|
2013-11-06 16:04:06 +08:00
|
|
|
float finalHeight = totalHeight;
|
2013-12-23 15:02:52 +08:00
|
|
|
setInnerContainerSize(Size(finalWidth, finalHeight));
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-05-12 11:08:10 +08:00
|
|
|
case Direction::HORIZONTAL:
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-28 14:34:52 +08:00
|
|
|
size_t length = _items.size();
|
2017-12-14 11:32:34 +08:00
|
|
|
float totalWidth = (length == 0) ? 0.0f : (length - 1) * _itemsMargin + (_leftPadding + _rightPadding);
|
2013-12-26 16:17:52 +08:00
|
|
|
for (auto& item : _items)
|
2013-12-23 15:02:52 +08:00
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
totalWidth += item->getContentSize().width;
|
2013-12-23 15:02:52 +08:00
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
float finalWidth = totalWidth;
|
2014-06-20 10:40:16 +08:00
|
|
|
float finalHeight = _contentSize.height;
|
2013-12-23 15:02:52 +08:00
|
|
|
setInnerContainerSize(Size(finalWidth, finalHeight));
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-01-19 11:44:32 +08:00
|
|
|
|
|
|
|
void ListView::remedyVerticalLayoutParameter(LinearLayoutParameter* layoutParameter, ssize_t itemIndex)
|
|
|
|
{
|
2015-01-19 15:27:10 +08:00
|
|
|
CCASSERT(nullptr != layoutParameter, "Layout parameter can't be nullptr!");
|
|
|
|
|
|
|
|
switch (_gravity)
|
2015-01-19 11:44:32 +08:00
|
|
|
{
|
|
|
|
case Gravity::LEFT:
|
|
|
|
layoutParameter->setGravity(LinearLayoutParameter::LinearGravity::LEFT);
|
|
|
|
break;
|
|
|
|
case Gravity::RIGHT:
|
|
|
|
layoutParameter->setGravity(LinearLayoutParameter::LinearGravity::RIGHT);
|
|
|
|
break;
|
|
|
|
case Gravity::CENTER_HORIZONTAL:
|
|
|
|
layoutParameter->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-01-19 15:27:10 +08:00
|
|
|
if (0 == itemIndex)
|
2015-01-19 11:44:32 +08:00
|
|
|
{
|
2017-06-15 14:56:54 +08:00
|
|
|
layoutParameter->setMargin(Margin(_leftPadding, _topPadding, _rightPadding, 0.f));
|
|
|
|
}
|
|
|
|
else if (_items.size() - 1 == itemIndex)
|
|
|
|
{
|
|
|
|
layoutParameter->setMargin(Margin(_leftPadding, _itemsMargin, _rightPadding, _bottomPadding));
|
2015-01-19 11:44:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-15 14:56:54 +08:00
|
|
|
layoutParameter->setMargin(Margin(_leftPadding, _itemsMargin, _rightPadding, 0.0f));
|
2015-01-19 11:44:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::remedyHorizontalLayoutParameter(LinearLayoutParameter* layoutParameter, ssize_t itemIndex)
|
|
|
|
{
|
2015-01-19 15:27:10 +08:00
|
|
|
CCASSERT(nullptr != layoutParameter, "Layout parameter can't be nullptr!");
|
2015-01-19 11:44:32 +08:00
|
|
|
|
|
|
|
switch (_gravity)
|
|
|
|
{
|
|
|
|
case Gravity::TOP:
|
|
|
|
layoutParameter->setGravity(LinearLayoutParameter::LinearGravity::TOP);
|
|
|
|
break;
|
|
|
|
case Gravity::BOTTOM:
|
|
|
|
layoutParameter->setGravity(LinearLayoutParameter::LinearGravity::BOTTOM);
|
|
|
|
break;
|
|
|
|
case Gravity::CENTER_VERTICAL:
|
|
|
|
layoutParameter->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-01-19 15:27:10 +08:00
|
|
|
if (0 == itemIndex)
|
2015-01-19 11:44:32 +08:00
|
|
|
{
|
2017-06-15 14:56:54 +08:00
|
|
|
layoutParameter->setMargin(Margin(_leftPadding, _topPadding, 0.f, _bottomPadding));
|
|
|
|
}
|
|
|
|
else if (_items.size() == itemIndex)
|
|
|
|
{
|
|
|
|
layoutParameter->setMargin(Margin(_itemsMargin, _topPadding, _rightPadding, _bottomPadding));
|
2015-01-19 11:44:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-15 14:56:54 +08:00
|
|
|
layoutParameter->setMargin(Margin(_itemsMargin, _topPadding, 0.f, _bottomPadding));
|
2015-01-19 11:44:32 +08:00
|
|
|
}
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::remedyLayoutParameter(Widget *item)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2015-01-19 15:27:10 +08:00
|
|
|
CCASSERT(nullptr != item, "ListView Item can't be nullptr!");
|
2015-01-19 11:44:32 +08:00
|
|
|
|
|
|
|
LinearLayoutParameter* linearLayoutParameter = (LinearLayoutParameter*)(item->getLayoutParameter());
|
|
|
|
bool isLayoutParameterExists = true;
|
|
|
|
if (!linearLayoutParameter)
|
|
|
|
{
|
|
|
|
linearLayoutParameter = LinearLayoutParameter::create();
|
|
|
|
isLayoutParameterExists = false;
|
|
|
|
}
|
|
|
|
ssize_t itemIndex = getIndex(item);
|
|
|
|
|
2014-12-16 16:44:04 +08:00
|
|
|
switch (_direction)
|
|
|
|
{
|
2014-05-12 11:08:10 +08:00
|
|
|
case Direction::VERTICAL:
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2015-01-19 11:44:32 +08:00
|
|
|
this->remedyVerticalLayoutParameter(linearLayoutParameter, itemIndex);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
2013-11-06 16:04:06 +08:00
|
|
|
}
|
2014-05-12 11:08:10 +08:00
|
|
|
case Direction::HORIZONTAL:
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2015-01-19 11:44:32 +08:00
|
|
|
this->remedyHorizontalLayoutParameter(linearLayoutParameter, itemIndex);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
2013-11-06 16:04:06 +08:00
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-01-19 11:44:32 +08:00
|
|
|
if (!isLayoutParameterExists)
|
|
|
|
{
|
|
|
|
item->setLayoutParameter(linearLayoutParameter);
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::pushBackDefaultItem()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2015-01-19 15:27:10 +08:00
|
|
|
if (nullptr == _model)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* newItem = _model->clone();
|
2013-11-06 16:04:06 +08:00
|
|
|
remedyLayoutParameter(newItem);
|
|
|
|
addChild(newItem);
|
2015-09-14 11:20:17 +08:00
|
|
|
requestDoLayout();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
void ListView::insertDefaultItem(ssize_t index)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2015-01-19 15:27:10 +08:00
|
|
|
if (nullptr == _model)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-07-20 17:50:02 +08:00
|
|
|
insertCustomItem(_model->clone(), index);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2014-06-25 16:17:16 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::pushBackCustomItem(Widget* item)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
remedyLayoutParameter(item);
|
|
|
|
addChild(item);
|
2015-09-14 11:20:17 +08:00
|
|
|
requestDoLayout();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2014-05-22 17:49:19 +08:00
|
|
|
|
|
|
|
void ListView::addChild(cocos2d::Node *child, int zOrder, int tag)
|
|
|
|
{
|
|
|
|
ScrollView::addChild(child, zOrder, tag);
|
|
|
|
|
|
|
|
Widget* widget = dynamic_cast<Widget*>(child);
|
2015-01-19 15:27:10 +08:00
|
|
|
if (nullptr != widget)
|
2014-05-22 17:49:19 +08:00
|
|
|
{
|
|
|
|
_items.pushBack(widget);
|
2015-09-21 18:40:30 +08:00
|
|
|
onItemListChanged();
|
2014-05-22 17:49:19 +08:00
|
|
|
}
|
2014-06-25 11:27:48 +08:00
|
|
|
}
|
2014-06-25 16:17:16 +08:00
|
|
|
|
|
|
|
void ListView::addChild(cocos2d::Node *child)
|
|
|
|
{
|
|
|
|
ListView::addChild(child, child->getLocalZOrder(), child->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::addChild(cocos2d::Node *child, int zOrder)
|
|
|
|
{
|
|
|
|
ListView::addChild(child, zOrder, child->getName());
|
|
|
|
}
|
2014-06-25 11:27:48 +08:00
|
|
|
|
|
|
|
void ListView::addChild(Node* child, int zOrder, const std::string &name)
|
|
|
|
{
|
|
|
|
ScrollView::addChild(child, zOrder, name);
|
|
|
|
|
|
|
|
Widget* widget = dynamic_cast<Widget*>(child);
|
2015-01-19 15:27:10 +08:00
|
|
|
if (nullptr != widget)
|
2014-06-25 11:27:48 +08:00
|
|
|
{
|
|
|
|
_items.pushBack(widget);
|
2015-09-21 18:40:30 +08:00
|
|
|
onItemListChanged();
|
2014-06-25 11:27:48 +08:00
|
|
|
}
|
2014-05-22 17:49:19 +08:00
|
|
|
}
|
|
|
|
|
2016-07-26 03:06:01 +08:00
|
|
|
void ListView::removeChild(cocos2d::Node *child, bool cleanup)
|
2014-05-22 17:49:19 +08:00
|
|
|
{
|
|
|
|
Widget* widget = dynamic_cast<Widget*>(child);
|
2015-01-19 15:27:10 +08:00
|
|
|
if (nullptr != widget)
|
2014-12-16 16:44:04 +08:00
|
|
|
{
|
2015-07-20 17:50:02 +08:00
|
|
|
if (-1 != _curSelectedIndex)
|
|
|
|
{
|
|
|
|
auto removedIndex = getIndex(widget);
|
|
|
|
if (_curSelectedIndex > removedIndex)
|
|
|
|
{
|
|
|
|
_curSelectedIndex -= 1;
|
|
|
|
}
|
|
|
|
else if (_curSelectedIndex == removedIndex)
|
|
|
|
{
|
|
|
|
_curSelectedIndex = -1;
|
|
|
|
}
|
|
|
|
}
|
2014-05-22 17:49:19 +08:00
|
|
|
_items.eraseObject(widget);
|
2015-09-21 18:40:30 +08:00
|
|
|
onItemListChanged();
|
2014-05-22 17:49:19 +08:00
|
|
|
}
|
|
|
|
|
2016-07-26 03:06:01 +08:00
|
|
|
ScrollView::removeChild(child, cleanup);
|
2017-01-13 14:38:28 +08:00
|
|
|
requestDoLayout();
|
2014-05-22 17:49:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::removeAllChildren()
|
|
|
|
{
|
|
|
|
this->removeAllChildrenWithCleanup(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::removeAllChildrenWithCleanup(bool cleanup)
|
|
|
|
{
|
|
|
|
ScrollView::removeAllChildrenWithCleanup(cleanup);
|
2015-07-20 17:50:02 +08:00
|
|
|
_curSelectedIndex = -1;
|
2015-09-21 18:40:30 +08:00
|
|
|
_items.clear();
|
|
|
|
onItemListChanged();
|
2014-05-22 17:49:19 +08:00
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
void ListView::insertCustomItem(Widget* item, ssize_t index)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2015-07-20 17:50:02 +08:00
|
|
|
if (-1 != _curSelectedIndex)
|
|
|
|
{
|
|
|
|
if (_curSelectedIndex >= index)
|
|
|
|
{
|
|
|
|
_curSelectedIndex += 1;
|
|
|
|
}
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
_items.insert(index, item);
|
2015-09-21 18:40:30 +08:00
|
|
|
onItemListChanged();
|
|
|
|
|
2014-05-22 17:49:19 +08:00
|
|
|
ScrollView::addChild(item);
|
|
|
|
|
2013-11-06 16:04:06 +08:00
|
|
|
remedyLayoutParameter(item);
|
2015-09-14 11:20:17 +08:00
|
|
|
requestDoLayout();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
void ListView::removeItem(ssize_t index)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* item = getItem(index);
|
2015-01-19 15:27:10 +08:00
|
|
|
if (nullptr == item)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
return;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2014-05-22 17:49:19 +08:00
|
|
|
removeChild(item, true);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::removeLastItem()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
removeItem(_items.size() -1);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-12-27 16:01:03 +08:00
|
|
|
|
|
|
|
void ListView::removeAllItems()
|
|
|
|
{
|
|
|
|
removeAllChildren();
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2015-09-16 17:59:56 +08:00
|
|
|
Widget* ListView::getItem(ssize_t index) const
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-28 14:34:52 +08:00
|
|
|
if (index < 0 || index >= _items.size())
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
2013-11-14 11:37:46 +08:00
|
|
|
return nullptr;
|
2013-11-06 16:04:06 +08:00
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
return _items.at(index);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Vector<Widget*>& ListView::getItems()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
return _items;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
ssize_t ListView::getIndex(Widget *item) const
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2015-01-19 15:27:10 +08:00
|
|
|
if (nullptr == item)
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
return _items.getIndex(item);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2014-05-12 09:51:23 +08:00
|
|
|
void ListView::setGravity(Gravity gravity)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
if (_gravity == gravity)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_gravity = gravity;
|
2015-09-14 11:20:17 +08:00
|
|
|
requestDoLayout();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2015-07-18 19:46:41 +08:00
|
|
|
void ListView::setMagneticType(MagneticType magneticType)
|
|
|
|
{
|
|
|
|
_magneticType = magneticType;
|
2015-09-08 21:15:35 +08:00
|
|
|
_outOfBoundaryAmountDirty = true;
|
2015-09-08 19:36:10 +08:00
|
|
|
startMagneticScroll();
|
2015-07-18 19:46:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ListView::MagneticType ListView::getMagneticType() const
|
|
|
|
{
|
|
|
|
return _magneticType;
|
|
|
|
}
|
|
|
|
|
2015-09-08 22:35:37 +08:00
|
|
|
void ListView::setMagneticAllowedOutOfBoundary(bool magneticAllowedOutOfBoundary)
|
|
|
|
{
|
|
|
|
_magneticAllowedOutOfBoundary = magneticAllowedOutOfBoundary;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ListView::getMagneticAllowedOutOfBoundary() const
|
|
|
|
{
|
|
|
|
return _magneticAllowedOutOfBoundary;
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::setItemsMargin(float margin)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
if (_itemsMargin == margin)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_itemsMargin = margin;
|
2015-09-14 11:20:17 +08:00
|
|
|
requestDoLayout();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2014-03-01 22:34:25 +08:00
|
|
|
|
2014-05-27 10:40:20 +08:00
|
|
|
float ListView::getItemsMargin()const
|
2014-03-01 22:34:25 +08:00
|
|
|
{
|
|
|
|
return _itemsMargin;
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2017-06-15 14:56:54 +08:00
|
|
|
void ListView::setPadding(float l, float t, float r, float b)
|
|
|
|
{
|
|
|
|
if (l == _leftPadding && t == _topPadding && r == _rightPadding && b == _bottomPadding)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_leftPadding = l;
|
|
|
|
_topPadding = t;
|
|
|
|
_rightPadding = r;
|
|
|
|
_bottomPadding = b;
|
|
|
|
requestDoLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::setLeftPadding(float l)
|
|
|
|
{
|
|
|
|
if (l == _leftPadding)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_leftPadding = l;
|
|
|
|
requestDoLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::setTopPadding(float t)
|
|
|
|
{
|
|
|
|
if (t == _topPadding)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_topPadding = t;
|
|
|
|
requestDoLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::setRightPadding(float r)
|
|
|
|
{
|
|
|
|
if (r == _rightPadding)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_rightPadding = r;
|
|
|
|
requestDoLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::setBottomPadding(float b)
|
|
|
|
{
|
|
|
|
if (b == _bottomPadding)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_bottomPadding = b;
|
|
|
|
requestDoLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
float ListView::getLeftPadding() const
|
|
|
|
{
|
|
|
|
return _leftPadding;
|
|
|
|
}
|
|
|
|
|
|
|
|
float ListView::getTopPadding() const
|
|
|
|
{
|
|
|
|
return _topPadding;
|
|
|
|
}
|
|
|
|
|
|
|
|
float ListView::getRightPadding() const
|
|
|
|
{
|
|
|
|
return _rightPadding;
|
|
|
|
}
|
|
|
|
|
|
|
|
float ListView::getBottomPadding() const
|
|
|
|
{
|
|
|
|
return _bottomPadding;
|
|
|
|
}
|
|
|
|
|
2016-06-29 09:48:03 +08:00
|
|
|
void ListView::setScrollDuration(float time)
|
|
|
|
{
|
|
|
|
if (time >= 0)
|
|
|
|
_scrollTime = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
float ListView::getScrollDuration() const
|
|
|
|
{
|
|
|
|
return _scrollTime;
|
|
|
|
}
|
|
|
|
|
2014-05-12 11:08:10 +08:00
|
|
|
void ListView::setDirection(Direction dir)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
switch (dir)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2015-01-30 17:02:20 +08:00
|
|
|
case Direction::NONE:
|
|
|
|
case Direction::BOTH:
|
|
|
|
break;
|
2014-05-12 11:08:10 +08:00
|
|
|
case Direction::VERTICAL:
|
2014-05-12 18:00:54 +08:00
|
|
|
setLayoutType(Type::VERTICAL);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
2014-05-12 11:08:10 +08:00
|
|
|
case Direction::HORIZONTAL:
|
2014-05-12 18:00:54 +08:00
|
|
|
setLayoutType(Type::HORIZONTAL);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
2013-11-06 16:04:06 +08:00
|
|
|
return;
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
ScrollView::setDirection(dir);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-12-26 14:57:30 +08:00
|
|
|
|
2015-09-14 11:20:17 +08:00
|
|
|
void ListView::refreshView()
|
2013-12-26 14:57:30 +08:00
|
|
|
{
|
2015-09-14 11:20:17 +08:00
|
|
|
forceDoLayout();
|
2013-12-26 14:57:30 +08:00
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2015-09-14 11:51:03 +08:00
|
|
|
void ListView::requestDoLayout()
|
|
|
|
{
|
|
|
|
_innerContainerDoLayoutDirty = true;
|
|
|
|
}
|
|
|
|
|
2015-09-14 11:20:17 +08:00
|
|
|
void ListView::doLayout()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2015-09-14 11:51:03 +08:00
|
|
|
if(!_innerContainerDoLayoutDirty)
|
2015-09-14 11:20:17 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
ssize_t length = _items.size();
|
2015-09-14 11:20:17 +08:00
|
|
|
for (int i = 0; i < length; ++i)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* item = _items.at(i);
|
2014-01-19 03:35:27 +08:00
|
|
|
item->setLocalZOrder(i);
|
2013-11-06 16:04:06 +08:00
|
|
|
remedyLayoutParameter(item);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
updateInnerContainerSize();
|
2015-11-15 21:48:42 +08:00
|
|
|
_innerContainer->forceDoLayout();
|
2015-09-14 11:51:03 +08:00
|
|
|
_innerContainerDoLayoutDirty = false;
|
2013-12-23 15:02:52 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void ListView::addEventListenerListView(Ref *target, SEL_ListViewEvent selector)
|
2013-11-08 20:29:49 +08:00
|
|
|
{
|
|
|
|
_listViewEventListener = target;
|
|
|
|
_listViewEventSelector = selector;
|
|
|
|
}
|
2014-06-23 12:09:49 +08:00
|
|
|
|
|
|
|
|
2014-05-14 16:14:28 +08:00
|
|
|
void ListView::addEventListener(const ccListViewCallback& callback)
|
2014-05-12 09:51:23 +08:00
|
|
|
{
|
|
|
|
_eventCallback = callback;
|
|
|
|
}
|
|
|
|
|
2014-05-22 15:23:13 +08:00
|
|
|
void ListView::selectedItemEvent(TouchEventType event)
|
2013-11-08 20:29:49 +08:00
|
|
|
{
|
2014-08-01 20:58:42 +08:00
|
|
|
this->retain();
|
2014-05-22 15:23:13 +08:00
|
|
|
switch (event)
|
2013-11-08 20:29:49 +08:00
|
|
|
{
|
2014-05-22 15:23:13 +08:00
|
|
|
case TouchEventType::BEGAN:
|
2014-05-12 09:51:23 +08:00
|
|
|
{
|
2014-03-11 11:53:44 +08:00
|
|
|
if (_listViewEventListener && _listViewEventSelector)
|
|
|
|
{
|
|
|
|
(_listViewEventListener->*_listViewEventSelector)(this, LISTVIEW_ONSELECTEDITEM_START);
|
|
|
|
}
|
2014-05-12 09:51:23 +08:00
|
|
|
if (_eventCallback) {
|
|
|
|
_eventCallback(this,EventType::ON_SELECTED_ITEM_START);
|
|
|
|
}
|
2014-12-05 19:31:10 +08:00
|
|
|
if (_ccEventCallback)
|
|
|
|
{
|
|
|
|
_ccEventCallback(this, static_cast<int>(EventType::ON_SELECTED_ITEM_START));
|
|
|
|
}
|
2014-05-12 09:51:23 +08:00
|
|
|
}
|
|
|
|
break;
|
2014-03-11 11:53:44 +08:00
|
|
|
default:
|
2014-05-12 09:51:23 +08:00
|
|
|
{
|
2014-03-11 11:53:44 +08:00
|
|
|
if (_listViewEventListener && _listViewEventSelector)
|
|
|
|
{
|
|
|
|
(_listViewEventListener->*_listViewEventSelector)(this, LISTVIEW_ONSELECTEDITEM_END);
|
|
|
|
}
|
2014-05-12 09:51:23 +08:00
|
|
|
if (_eventCallback) {
|
|
|
|
_eventCallback(this, EventType::ON_SELECTED_ITEM_END);
|
|
|
|
}
|
2014-12-05 19:31:10 +08:00
|
|
|
if (_ccEventCallback)
|
|
|
|
{
|
|
|
|
_ccEventCallback(this, static_cast<int>(EventType::ON_SELECTED_ITEM_END));
|
|
|
|
}
|
2014-05-12 09:51:23 +08:00
|
|
|
}
|
|
|
|
break;
|
2013-11-08 20:29:49 +08:00
|
|
|
}
|
2014-08-01 20:58:42 +08:00
|
|
|
this->release();
|
2013-11-08 20:29:49 +08:00
|
|
|
}
|
|
|
|
|
2014-06-06 16:48:49 +08:00
|
|
|
void ListView::interceptTouchEvent(TouchEventType event, Widget *sender, Touch* touch)
|
2013-11-08 20:29:49 +08:00
|
|
|
{
|
2014-06-06 16:48:49 +08:00
|
|
|
ScrollView::interceptTouchEvent(event, sender, touch);
|
2015-08-06 22:50:53 +08:00
|
|
|
if (!_touchEnabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-05-22 15:23:13 +08:00
|
|
|
if (event != TouchEventType::MOVED)
|
2013-11-08 20:29:49 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* parent = sender;
|
2013-11-08 20:29:49 +08:00
|
|
|
while (parent)
|
|
|
|
{
|
2015-07-29 18:43:11 +08:00
|
|
|
if (parent && (parent->getParent() == _innerContainer))
|
2013-11-08 20:29:49 +08:00
|
|
|
{
|
|
|
|
_curSelectedIndex = getIndex(parent);
|
|
|
|
break;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
parent = dynamic_cast<Widget*>(parent->getParent());
|
2013-11-08 20:29:49 +08:00
|
|
|
}
|
2014-06-25 10:50:18 +08:00
|
|
|
if (sender->isHighlighted()) {
|
|
|
|
selectedItemEvent(event);
|
|
|
|
}
|
2013-11-08 20:29:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-05 18:25:00 +08:00
|
|
|
static Vec2 calculateItemPositionWithAnchor(Widget* item, const Vec2& itemAnchorPoint)
|
|
|
|
{
|
|
|
|
Vec2 origin(item->getLeftBoundary(), item->getBottomBoundary());
|
|
|
|
Size size = item->getContentSize();
|
|
|
|
return origin + Vec2(size.width * itemAnchorPoint.x, size.height * itemAnchorPoint.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Widget* findClosestItem(const Vec2& targetPosition, const Vector<Widget*>& items, const Vec2& itemAnchorPoint, ssize_t firstIndex, float distanceFromFirst, ssize_t lastIndex, float distanceFromLast)
|
|
|
|
{
|
|
|
|
CCASSERT(firstIndex >= 0 && lastIndex < items.size() && firstIndex <= lastIndex, "");
|
|
|
|
if (firstIndex == lastIndex)
|
|
|
|
{
|
|
|
|
return items.at(firstIndex);
|
|
|
|
}
|
|
|
|
if (lastIndex - firstIndex == 1)
|
|
|
|
{
|
|
|
|
if (distanceFromFirst <= distanceFromLast)
|
|
|
|
{
|
|
|
|
return items.at(firstIndex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return items.at(lastIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Binary search
|
|
|
|
ssize_t midIndex = (firstIndex + lastIndex) / 2;
|
|
|
|
Vec2 itemPosition = calculateItemPositionWithAnchor(items.at(midIndex), itemAnchorPoint);
|
|
|
|
float distanceFromMid = (targetPosition - itemPosition).length();
|
|
|
|
if (distanceFromFirst <= distanceFromLast)
|
|
|
|
{
|
|
|
|
// Left half
|
|
|
|
return findClosestItem(targetPosition, items, itemAnchorPoint, firstIndex, distanceFromFirst, midIndex, distanceFromMid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Right half
|
|
|
|
return findClosestItem(targetPosition, items, itemAnchorPoint, midIndex, distanceFromMid, lastIndex, distanceFromLast);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* ListView::getClosestItemToPosition(const Vec2& targetPosition, const Vec2& itemAnchorPoint) const
|
|
|
|
{
|
|
|
|
if (_items.empty())
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the closest item through binary search
|
|
|
|
ssize_t firstIndex = 0;
|
|
|
|
Vec2 firstPosition = calculateItemPositionWithAnchor(_items.at(firstIndex), itemAnchorPoint);
|
|
|
|
float distanceFromFirst = (targetPosition - firstPosition).length();
|
|
|
|
|
|
|
|
ssize_t lastIndex = _items.size() - 1;
|
|
|
|
Vec2 lastPosition = calculateItemPositionWithAnchor(_items.at(lastIndex), itemAnchorPoint);
|
|
|
|
float distanceFromLast = (targetPosition - lastPosition).length();
|
|
|
|
|
|
|
|
return findClosestItem(targetPosition, _items, itemAnchorPoint, firstIndex, distanceFromFirst, lastIndex, distanceFromLast);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* ListView::getClosestItemToPositionInCurrentView(const Vec2& positionRatioInView, const Vec2& itemAnchorPoint) const
|
|
|
|
{
|
|
|
|
// Calculate the target position
|
|
|
|
Size contentSize = getContentSize();
|
|
|
|
Vec2 targetPosition = -_innerContainer->getPosition();
|
|
|
|
targetPosition.x += contentSize.width * positionRatioInView.x;
|
|
|
|
targetPosition.y += contentSize.height * positionRatioInView.y;
|
|
|
|
return getClosestItemToPosition(targetPosition, itemAnchorPoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* ListView::getCenterItemInCurrentView() const
|
|
|
|
{
|
2015-09-11 14:59:46 +08:00
|
|
|
return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE, Vec2::ANCHOR_MIDDLE);
|
2015-07-05 18:25:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Widget* ListView::getLeftmostItemInCurrentView() const
|
|
|
|
{
|
|
|
|
if (_direction == Direction::HORIZONTAL)
|
|
|
|
{
|
2015-09-11 14:59:46 +08:00
|
|
|
return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE_LEFT, Vec2::ANCHOR_MIDDLE);
|
2015-07-05 18:25:00 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* ListView::getRightmostItemInCurrentView() const
|
|
|
|
{
|
|
|
|
if (_direction == Direction::HORIZONTAL)
|
|
|
|
{
|
2015-09-11 14:59:46 +08:00
|
|
|
return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE_RIGHT, Vec2::ANCHOR_MIDDLE);
|
2015-07-05 18:25:00 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* ListView::getTopmostItemInCurrentView() const
|
|
|
|
{
|
|
|
|
if (_direction == Direction::VERTICAL)
|
|
|
|
{
|
2015-09-11 14:59:46 +08:00
|
|
|
return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE_TOP, Vec2::ANCHOR_MIDDLE);
|
2015-07-05 18:25:00 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* ListView::getBottommostItemInCurrentView() const
|
|
|
|
{
|
|
|
|
if (_direction == Direction::VERTICAL)
|
|
|
|
{
|
2015-09-11 14:59:46 +08:00
|
|
|
return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE_BOTTOM, Vec2::ANCHOR_MIDDLE);
|
2015-07-05 18:25:00 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-09-13 18:21:02 +08:00
|
|
|
void ListView::jumpToBottom()
|
|
|
|
{
|
2015-09-14 11:20:17 +08:00
|
|
|
doLayout();
|
2015-09-13 18:21:02 +08:00
|
|
|
ScrollView::jumpToBottom();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::jumpToTop()
|
|
|
|
{
|
2015-09-14 11:20:17 +08:00
|
|
|
doLayout();
|
2015-09-13 18:21:02 +08:00
|
|
|
ScrollView::jumpToTop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::jumpToLeft()
|
|
|
|
{
|
2015-09-14 11:20:17 +08:00
|
|
|
doLayout();
|
2015-09-13 18:21:02 +08:00
|
|
|
ScrollView::jumpToLeft();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::jumpToRight()
|
|
|
|
{
|
2015-09-14 11:20:17 +08:00
|
|
|
doLayout();
|
2015-09-13 18:21:02 +08:00
|
|
|
ScrollView::jumpToRight();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::jumpToTopLeft()
|
|
|
|
{
|
2015-09-14 11:20:17 +08:00
|
|
|
doLayout();
|
2015-09-13 18:21:02 +08:00
|
|
|
ScrollView::jumpToTopLeft();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::jumpToTopRight()
|
|
|
|
{
|
2015-09-14 11:20:17 +08:00
|
|
|
doLayout();
|
2015-09-13 18:21:02 +08:00
|
|
|
ScrollView::jumpToTopRight();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::jumpToBottomLeft()
|
|
|
|
{
|
2015-09-14 11:20:17 +08:00
|
|
|
doLayout();
|
2015-09-13 18:21:02 +08:00
|
|
|
ScrollView::jumpToBottomLeft();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::jumpToBottomRight()
|
|
|
|
{
|
2015-09-14 11:20:17 +08:00
|
|
|
doLayout();
|
2015-09-13 18:21:02 +08:00
|
|
|
ScrollView::jumpToBottomRight();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::jumpToPercentVertical(float percent)
|
|
|
|
{
|
2015-09-14 11:20:17 +08:00
|
|
|
doLayout();
|
2015-09-13 18:21:02 +08:00
|
|
|
ScrollView::jumpToPercentVertical(percent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::jumpToPercentHorizontal(float percent)
|
|
|
|
{
|
2015-09-14 11:20:17 +08:00
|
|
|
doLayout();
|
2015-09-13 18:21:02 +08:00
|
|
|
ScrollView::jumpToPercentHorizontal(percent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::jumpToPercentBothDirection(const Vec2& percent)
|
|
|
|
{
|
2015-09-14 11:20:17 +08:00
|
|
|
doLayout();
|
2015-09-13 18:21:02 +08:00
|
|
|
ScrollView::jumpToPercentBothDirection(percent);
|
|
|
|
}
|
|
|
|
|
2015-09-21 20:44:49 +08:00
|
|
|
Vec2 ListView::calculateItemDestination(const Vec2& positionRatioInView, Widget* item, const Vec2& itemAnchorPoint)
|
2015-09-09 15:25:00 +08:00
|
|
|
{
|
2015-09-21 20:44:49 +08:00
|
|
|
const Size& contentSize = getContentSize();
|
2015-09-09 15:25:00 +08:00
|
|
|
Vec2 positionInView;
|
|
|
|
positionInView.x += contentSize.width * positionRatioInView.x;
|
|
|
|
positionInView.y += contentSize.height * positionRatioInView.y;
|
|
|
|
|
|
|
|
Vec2 itemPosition = calculateItemPositionWithAnchor(item, itemAnchorPoint);
|
|
|
|
return -(itemPosition - positionInView);
|
|
|
|
}
|
|
|
|
|
2015-09-16 17:59:56 +08:00
|
|
|
void ListView::jumpToItem(ssize_t itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint)
|
2015-09-09 15:25:00 +08:00
|
|
|
{
|
|
|
|
Widget* item = getItem(itemIndex);
|
|
|
|
if (item == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-09-14 11:20:17 +08:00
|
|
|
doLayout();
|
2015-09-09 15:25:00 +08:00
|
|
|
|
2015-09-21 20:44:49 +08:00
|
|
|
Vec2 destination = calculateItemDestination(positionRatioInView, item, itemAnchorPoint);
|
2015-09-19 22:16:36 +08:00
|
|
|
if(!_bounceEnabled)
|
|
|
|
{
|
|
|
|
Vec2 delta = destination - getInnerContainerPosition();
|
|
|
|
Vec2 outOfBoundary = getHowMuchOutOfBoundary(delta);
|
|
|
|
destination += outOfBoundary;
|
|
|
|
}
|
|
|
|
jumpToDestination(destination);
|
2015-09-09 15:25:00 +08:00
|
|
|
}
|
|
|
|
|
2015-09-16 17:59:56 +08:00
|
|
|
void ListView::scrollToItem(ssize_t itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint)
|
2015-09-11 14:59:46 +08:00
|
|
|
{
|
2016-06-29 09:48:03 +08:00
|
|
|
scrollToItem(itemIndex, positionRatioInView, itemAnchorPoint, _scrollTime);
|
2015-09-11 14:59:46 +08:00
|
|
|
}
|
|
|
|
|
2015-09-16 17:59:56 +08:00
|
|
|
void ListView::scrollToItem(ssize_t itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint, float timeInSec)
|
2015-09-04 14:25:21 +08:00
|
|
|
{
|
|
|
|
Widget* item = getItem(itemIndex);
|
|
|
|
if (item == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-09-21 20:44:49 +08:00
|
|
|
Vec2 destination = calculateItemDestination(positionRatioInView, item, itemAnchorPoint);
|
2015-09-11 14:59:46 +08:00
|
|
|
startAutoScrollToDestination(destination, timeInSec, true);
|
2015-09-04 14:25:21 +08:00
|
|
|
}
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
ssize_t ListView::getCurSelectedIndex() const
|
2013-11-08 20:29:49 +08:00
|
|
|
{
|
|
|
|
return _curSelectedIndex;
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2016-06-02 14:11:28 +08:00
|
|
|
void ListView::setCurSelectedIndex(int itemIndex)
|
|
|
|
{
|
|
|
|
Widget* item = getItem(itemIndex);
|
|
|
|
if (item == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_curSelectedIndex = itemIndex;
|
|
|
|
this->selectedItemEvent(cocos2d::ui::Widget::TouchEventType::ENDED);
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::onSizeChanged()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
ScrollView::onSizeChanged();
|
2015-09-14 11:20:17 +08:00
|
|
|
requestDoLayout();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
std::string ListView::getDescription() const
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-27 17:22:51 +08:00
|
|
|
return "ListView";
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* ListView::createCloneInstance()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
return ListView::create();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::copyClonedWidgetChildren(Widget* model)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-27 17:22:51 +08:00
|
|
|
auto& arrayItems = static_cast<ListView*>(model)->getItems();
|
2013-12-26 16:17:52 +08:00
|
|
|
for (auto& item : arrayItems)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
pushBackCustomItem(item->clone());
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::copySpecialProperties(Widget *widget)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
ListView* listViewEx = dynamic_cast<ListView*>(widget);
|
2013-11-06 16:04:06 +08:00
|
|
|
if (listViewEx)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
ScrollView::copySpecialProperties(widget);
|
2013-11-06 16:04:06 +08:00
|
|
|
setItemModel(listViewEx->_model);
|
|
|
|
setItemsMargin(listViewEx->_itemsMargin);
|
|
|
|
setGravity(listViewEx->_gravity);
|
2014-05-23 15:03:46 +08:00
|
|
|
_listViewEventListener = listViewEx->_listViewEventListener;
|
|
|
|
_listViewEventSelector = listViewEx->_listViewEventSelector;
|
|
|
|
_eventCallback = listViewEx->_eventCallback;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-08 21:15:35 +08:00
|
|
|
Vec2 ListView::getHowMuchOutOfBoundary(const Vec2& addition)
|
|
|
|
{
|
2015-09-08 22:35:37 +08:00
|
|
|
if(!_magneticAllowedOutOfBoundary || _items.empty())
|
2015-09-08 21:15:35 +08:00
|
|
|
{
|
|
|
|
return ScrollView::getHowMuchOutOfBoundary(addition);
|
|
|
|
}
|
2015-09-08 22:35:37 +08:00
|
|
|
else if(_magneticType == MagneticType::NONE || _magneticType == MagneticType::BOTH_END)
|
|
|
|
{
|
|
|
|
return ScrollView::getHowMuchOutOfBoundary(addition);
|
|
|
|
}
|
|
|
|
else if(addition == Vec2::ZERO && !_outOfBoundaryAmountDirty)
|
2015-09-08 21:15:35 +08:00
|
|
|
{
|
|
|
|
return _outOfBoundaryAmount;
|
|
|
|
}
|
|
|
|
|
2015-09-08 22:35:37 +08:00
|
|
|
// If it is allowed to be out of boundary by magnetic, adjust the boundaries according to the magnetic type.
|
2015-09-08 21:15:35 +08:00
|
|
|
float leftBoundary = _leftBoundary;
|
|
|
|
float rightBoundary = _rightBoundary;
|
|
|
|
float topBoundary = _topBoundary;
|
|
|
|
float bottomBoundary = _bottomBoundary;
|
|
|
|
{
|
2015-09-16 17:59:56 +08:00
|
|
|
ssize_t lastItemIndex = _items.size() - 1;
|
2015-09-08 22:35:37 +08:00
|
|
|
Size contentSize = getContentSize();
|
|
|
|
Vec2 firstItemAdjustment, lastItemAdjustment;
|
|
|
|
if(_magneticType == MagneticType::CENTER)
|
|
|
|
{
|
|
|
|
firstItemAdjustment = (contentSize - _items.at(0)->getContentSize()) / 2;
|
|
|
|
lastItemAdjustment = (contentSize - _items.at(lastItemIndex)->getContentSize()) / 2;
|
|
|
|
}
|
|
|
|
else if(_magneticType == MagneticType::LEFT)
|
|
|
|
{
|
|
|
|
lastItemAdjustment = contentSize - _items.at(lastItemIndex)->getContentSize();
|
|
|
|
}
|
|
|
|
else if(_magneticType == MagneticType::RIGHT)
|
|
|
|
{
|
|
|
|
firstItemAdjustment = contentSize - _items.at(0)->getContentSize();
|
|
|
|
}
|
|
|
|
else if(_magneticType == MagneticType::TOP)
|
|
|
|
{
|
|
|
|
lastItemAdjustment = contentSize - _items.at(lastItemIndex)->getContentSize();
|
|
|
|
}
|
|
|
|
else if(_magneticType == MagneticType::BOTTOM)
|
|
|
|
{
|
|
|
|
firstItemAdjustment = contentSize - _items.at(0)->getContentSize();
|
|
|
|
}
|
2015-09-08 21:15:35 +08:00
|
|
|
leftBoundary += firstItemAdjustment.x;
|
|
|
|
rightBoundary -= lastItemAdjustment.x;
|
|
|
|
topBoundary -= firstItemAdjustment.y;
|
|
|
|
bottomBoundary += lastItemAdjustment.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the actual amount
|
|
|
|
Vec2 outOfBoundaryAmount;
|
|
|
|
if(_innerContainer->getLeftBoundary() + addition.x > leftBoundary)
|
|
|
|
{
|
|
|
|
outOfBoundaryAmount.x = leftBoundary - (_innerContainer->getLeftBoundary() + addition.x);
|
|
|
|
}
|
|
|
|
else if(_innerContainer->getRightBoundary() + addition.x < rightBoundary)
|
|
|
|
{
|
|
|
|
outOfBoundaryAmount.x = rightBoundary - (_innerContainer->getRightBoundary() + addition.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_innerContainer->getTopBoundary() + addition.y < topBoundary)
|
|
|
|
{
|
|
|
|
outOfBoundaryAmount.y = topBoundary - (_innerContainer->getTopBoundary() + addition.y);
|
|
|
|
}
|
|
|
|
else if(_innerContainer->getBottomBoundary() + addition.y > bottomBoundary)
|
|
|
|
{
|
|
|
|
outOfBoundaryAmount.y = bottomBoundary - (_innerContainer->getBottomBoundary() + addition.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(addition == Vec2::ZERO)
|
|
|
|
{
|
|
|
|
_outOfBoundaryAmount = outOfBoundaryAmount;
|
|
|
|
_outOfBoundaryAmountDirty = false;
|
|
|
|
}
|
|
|
|
return outOfBoundaryAmount;
|
|
|
|
}
|
|
|
|
|
2015-09-08 22:35:37 +08:00
|
|
|
static Vec2 getAnchorPointByMagneticType(ListView::MagneticType magneticType)
|
2015-09-08 15:55:19 +08:00
|
|
|
{
|
|
|
|
switch(magneticType)
|
|
|
|
{
|
|
|
|
case ListView::MagneticType::NONE: return Vec2::ZERO;
|
|
|
|
case ListView::MagneticType::BOTH_END: return Vec2::ANCHOR_TOP_LEFT;
|
|
|
|
case ListView::MagneticType::CENTER: return Vec2::ANCHOR_MIDDLE;
|
|
|
|
case ListView::MagneticType::LEFT: return Vec2::ANCHOR_MIDDLE_LEFT;
|
|
|
|
case ListView::MagneticType::RIGHT: return Vec2::ANCHOR_MIDDLE_RIGHT;
|
|
|
|
case ListView::MagneticType::TOP: return Vec2::ANCHOR_MIDDLE_TOP;
|
|
|
|
case ListView::MagneticType::BOTTOM: return Vec2::ANCHOR_MIDDLE_BOTTOM;
|
|
|
|
}
|
|
|
|
return Vec2::ZERO;
|
|
|
|
}
|
2015-09-08 21:15:35 +08:00
|
|
|
|
2015-09-08 15:55:19 +08:00
|
|
|
void ListView::startAttenuatingAutoScroll(const Vec2& deltaMove, const Vec2& initialVelocity)
|
|
|
|
{
|
|
|
|
Vec2 adjustedDeltaMove = deltaMove;
|
|
|
|
|
|
|
|
if(!_items.empty() && _magneticType != MagneticType::NONE)
|
|
|
|
{
|
|
|
|
adjustedDeltaMove = flattenVectorByDirection(adjustedDeltaMove);
|
|
|
|
|
|
|
|
// If the destination is out of boundary, do nothing here. Because it will be handled by bouncing back.
|
|
|
|
if(getHowMuchOutOfBoundary(adjustedDeltaMove) == Vec2::ZERO)
|
|
|
|
{
|
|
|
|
MagneticType magType = _magneticType;
|
|
|
|
if(magType == MagneticType::BOTH_END)
|
|
|
|
{
|
2015-09-08 22:35:37 +08:00
|
|
|
if(_direction == Direction::HORIZONTAL)
|
2015-09-08 15:55:19 +08:00
|
|
|
{
|
|
|
|
magType = (adjustedDeltaMove.x > 0 ? MagneticType::LEFT : MagneticType::RIGHT);
|
|
|
|
}
|
2015-09-08 22:35:37 +08:00
|
|
|
else if(_direction == Direction::VERTICAL)
|
2015-09-08 15:55:19 +08:00
|
|
|
{
|
|
|
|
magType = (adjustedDeltaMove.y > 0 ? MagneticType::BOTTOM : MagneticType::TOP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust the delta move amount according to the magnetic type
|
|
|
|
Vec2 magneticAnchorPoint = getAnchorPointByMagneticType(magType);
|
|
|
|
Vec2 magneticPosition = -_innerContainer->getPosition();
|
|
|
|
magneticPosition.x += getContentSize().width * magneticAnchorPoint.x;
|
|
|
|
magneticPosition.y += getContentSize().height * magneticAnchorPoint.y;
|
|
|
|
|
|
|
|
Widget* pTargetItem = getClosestItemToPosition(magneticPosition - adjustedDeltaMove, magneticAnchorPoint);
|
|
|
|
Vec2 itemPosition = calculateItemPositionWithAnchor(pTargetItem, magneticAnchorPoint);
|
|
|
|
adjustedDeltaMove = magneticPosition - itemPosition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ScrollView::startAttenuatingAutoScroll(adjustedDeltaMove, initialVelocity);
|
|
|
|
}
|
2015-09-08 21:15:35 +08:00
|
|
|
|
2015-09-08 19:36:10 +08:00
|
|
|
void ListView::startMagneticScroll()
|
|
|
|
{
|
|
|
|
if(_items.empty() || _magneticType == MagneticType::NONE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the closest item
|
|
|
|
Vec2 magneticAnchorPoint = getAnchorPointByMagneticType(_magneticType);
|
|
|
|
Vec2 magneticPosition = -_innerContainer->getPosition();
|
|
|
|
magneticPosition.x += getContentSize().width * magneticAnchorPoint.x;
|
|
|
|
magneticPosition.y += getContentSize().height * magneticAnchorPoint.y;
|
|
|
|
|
|
|
|
Widget* pTargetItem = getClosestItemToPosition(magneticPosition, magneticAnchorPoint);
|
|
|
|
scrollToItem(getIndex(pTargetItem), magneticAnchorPoint, magneticAnchorPoint);
|
|
|
|
}
|
|
|
|
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2014-02-24 18:56:45 +08:00
|
|
|
NS_CC_END
|