axmol/cocos/gui/UIListView.cpp

473 lines
12 KiB
C++
Raw Normal View History

2013-09-13 22:20:20 +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.
****************************************************************************/
2013-09-13 22:20:20 +08:00
#include "gui/UIListView.h"
2013-11-06 16:04:06 +08:00
#include "gui/UIHelper.h"
#include "extensions/GUI/CCControlExtension/CCScale9Sprite.h"
2013-12-23 15:02:52 +08:00
NS_CC_BEGIN
namespace gui {
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),
2013-11-06 16:04:06 +08:00
_gravity(LISTVIEW_GRAVITY_CENTER_HORIZONTAL),
2013-11-08 20:29:49 +08:00
_itemsMargin(0.0f),
2013-11-14 11:37:46 +08:00
_listViewEventListener(nullptr),
_listViewEventSelector(nullptr),
2013-12-23 15:02:52 +08:00
_curSelectedIndex(0),
_refreshViewDirty(true)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
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();
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
{
2013-12-23 15:02:52 +08:00
ListView* widget = new 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
{
2013-11-06 16:04:06 +08:00
setLayoutType(LAYOUT_LINEAR_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
{
2013-11-06 16:04:06 +08:00
if (!model)
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
}
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
}
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
{
2013-11-06 16:04:06 +08:00
case SCROLLVIEW_DIR_VERTICAL:
2013-09-13 22:20:20 +08:00
{
2013-12-28 14:34:52 +08:00
size_t length = _items.size();
2013-12-23 15:02:52 +08:00
float totalHeight = (length - 1) * _itemsMargin;
2013-12-26 16:17:52 +08:00
for (auto& item : _items)
2013-12-23 15:02:52 +08:00
{
totalHeight += item->getSize().height;
}
2013-11-06 16:04:06 +08:00
float finalWidth = _size.width;
float finalHeight = totalHeight;
2013-12-23 15:02:52 +08:00
setInnerContainerSize(Size(finalWidth, finalHeight));
2013-09-13 22:20:20 +08:00
break;
}
2013-11-06 16:04:06 +08:00
case SCROLLVIEW_DIR_HORIZONTAL:
2013-09-13 22:20:20 +08:00
{
2013-12-28 14:34:52 +08:00
size_t length = _items.size();
2013-12-23 15:02:52 +08:00
float totalWidth = (length - 1) * _itemsMargin;
2013-12-26 16:17:52 +08:00
for (auto& item : _items)
2013-12-23 15:02:52 +08:00
{
totalWidth += item->getSize().width;
}
2013-11-06 16:04:06 +08:00
float finalWidth = totalWidth;
float finalHeight = _size.height;
2013-12-23 15:02:52 +08:00
setInnerContainerSize(Size(finalWidth, finalHeight));
2013-09-13 22:20:20 +08:00
break;
}
default:
break;
}
}
2013-12-23 15:02:52 +08:00
void ListView::remedyLayoutParameter(Widget *item)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (!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
}
2013-11-06 16:04:06 +08:00
switch (_direction) {
case SCROLLVIEW_DIR_VERTICAL:
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter(LAYOUT_PARAMETER_LINEAR));
2013-11-06 16:04:06 +08:00
if (!llp)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
LinearLayoutParameter* defaultLp = LinearLayoutParameter::create();
2013-11-06 16:04:06 +08:00
switch (_gravity) {
case LISTVIEW_GRAVITY_LEFT:
defaultLp->setGravity(LINEAR_GRAVITY_LEFT);
break;
case LISTVIEW_GRAVITY_RIGHT:
defaultLp->setGravity(LINEAR_GRAVITY_RIGHT);
break;
case LISTVIEW_GRAVITY_CENTER_HORIZONTAL:
defaultLp->setGravity(LINEAR_GRAVITY_CENTER_HORIZONTAL);
break;
default:
break;
}
if (getIndex(item) == 0)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
defaultLp->setMargin(MarginZero);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
else
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
defaultLp->setMargin(Margin(0.0f, _itemsMargin, 0.0f, 0.0f));
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
item->setLayoutParameter(defaultLp);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
else
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (getIndex(item) == 0)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
llp->setMargin(MarginZero);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
else
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
llp->setMargin(Margin(0.0f, _itemsMargin, 0.0f, 0.0f));
2013-11-06 16:04:06 +08:00
}
switch (_gravity) {
case LISTVIEW_GRAVITY_LEFT:
llp->setGravity(LINEAR_GRAVITY_LEFT);
break;
case LISTVIEW_GRAVITY_RIGHT:
llp->setGravity(LINEAR_GRAVITY_RIGHT);
break;
case LISTVIEW_GRAVITY_CENTER_HORIZONTAL:
llp->setGravity(LINEAR_GRAVITY_CENTER_HORIZONTAL);
break;
default:
break;
2013-09-13 22:20:20 +08:00
}
}
break;
2013-11-06 16:04:06 +08:00
}
case SCROLLVIEW_DIR_HORIZONTAL:
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter(LAYOUT_PARAMETER_LINEAR));
2013-11-06 16:04:06 +08:00
if (!llp)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
LinearLayoutParameter* defaultLp = LinearLayoutParameter::create();
2013-11-06 16:04:06 +08:00
switch (_gravity) {
case LISTVIEW_GRAVITY_TOP:
defaultLp->setGravity(LINEAR_GRAVITY_TOP);
break;
case LISTVIEW_GRAVITY_BOTTOM:
defaultLp->setGravity(LINEAR_GRAVITY_BOTTOM);
break;
case LISTVIEW_GRAVITY_CENTER_VERTICAL:
defaultLp->setGravity(LINEAR_GRAVITY_CENTER_VERTICAL);
break;
default:
break;
}
if (getIndex(item) == 0)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
defaultLp->setMargin(MarginZero);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
else
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
defaultLp->setMargin(Margin(_itemsMargin, 0.0f, 0.0f, 0.0f));
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
item->setLayoutParameter(defaultLp);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
else
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (getIndex(item) == 0)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
llp->setMargin(MarginZero);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
else
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
llp->setMargin(Margin(_itemsMargin, 0.0f, 0.0f, 0.0f));
2013-11-06 16:04:06 +08:00
}
switch (_gravity) {
case LISTVIEW_GRAVITY_TOP:
llp->setGravity(LINEAR_GRAVITY_TOP);
break;
case LISTVIEW_GRAVITY_BOTTOM:
llp->setGravity(LINEAR_GRAVITY_BOTTOM);
break;
case LISTVIEW_GRAVITY_CENTER_VERTICAL:
llp->setGravity(LINEAR_GRAVITY_CENTER_VERTICAL);
break;
default:
break;
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;
}
2013-11-06 16:04:06 +08:00
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
{
2013-11-06 16:04:06 +08:00
if (!_model)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-12-23 15:02:52 +08:00
Widget* newItem = _model->clone();
_items.pushBack(newItem);
2013-11-06 16:04:06 +08:00
remedyLayoutParameter(newItem);
addChild(newItem);
2013-12-23 15:02:52 +08:00
_refreshViewDirty = true;
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
{
2013-11-06 16:04:06 +08:00
if (!_model)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-12-23 15:02:52 +08:00
Widget* newItem = _model->clone();
_items.insert(index, newItem);
2013-11-06 16:04:06 +08:00
remedyLayoutParameter(newItem);
addChild(newItem);
2013-12-23 15:02:52 +08:00
_refreshViewDirty = true;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void ListView::pushBackCustomItem(Widget* item)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
_items.pushBack(item);
2013-11-06 16:04:06 +08:00
remedyLayoutParameter(item);
addChild(item);
2013-12-23 15:02:52 +08:00
_refreshViewDirty = true;
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
{
2013-12-23 15:02:52 +08:00
_items.insert(index, item);
2013-11-06 16:04:06 +08:00
remedyLayoutParameter(item);
addChild(item);
2013-12-23 15:02:52 +08:00
_refreshViewDirty = true;
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);
2013-11-06 16:04:06 +08:00
if (!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
}
2013-12-23 15:02:52 +08:00
_items.eraseObject(item);
2013-11-06 16:04:06 +08:00
removeChild(item);
2013-12-23 15:02:52 +08:00
_refreshViewDirty = 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
}
void ListView::removeAllItems()
{
_items.clear();
removeAllChildren();
}
2013-09-13 22:20:20 +08:00
2013-12-28 14:34:52 +08:00
Widget* ListView::getItem(ssize_t index)
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
{
2013-11-06 16:04:06 +08:00
if (!item)
{
return -1;
}
2013-12-23 15:02:52 +08:00
return _items.getIndex(item);
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void ListView::setGravity(ListViewGravity gravity)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (_gravity == gravity)
{
return;
}
_gravity = gravity;
2013-12-23 15:02:52 +08:00
_refreshViewDirty = true;
2013-09-13 22:20:20 +08:00
}
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;
2013-12-23 15:02:52 +08:00
_refreshViewDirty = true;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void ListView::setDirection(SCROLLVIEW_DIR 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
{
2013-11-06 16:04:06 +08:00
case SCROLLVIEW_DIR_VERTICAL:
setLayoutType(LAYOUT_LINEAR_VERTICAL);
2013-09-13 22:20:20 +08:00
break;
2013-11-06 16:04:06 +08:00
case SCROLLVIEW_DIR_HORIZONTAL:
setLayoutType(LAYOUT_LINEAR_HORIZONTAL);
2013-09-13 22:20:20 +08:00
break;
2013-11-06 16:04:06 +08:00
case SCROLLVIEW_DIR_BOTH:
return;
2013-09-13 22:20:20 +08:00
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
void ListView::requestRefreshView()
{
_refreshViewDirty = true;
}
2013-09-13 22:20:20 +08:00
2013-12-23 15:02:52 +08:00
void ListView::refreshView()
2013-09-13 22:20:20 +08:00
{
2013-12-28 14:34:52 +08:00
ssize_t length = _items.size();
2013-11-06 16:04:06 +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);
2013-11-06 16:04:06 +08:00
item->setZOrder(i);
remedyLayoutParameter(item);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
updateInnerContainerSize();
2013-09-13 22:20:20 +08:00
}
2013-11-08 20:29:49 +08:00
2013-12-23 15:02:52 +08:00
void ListView::sortAllChildren()
{
ScrollView::sortAllChildren();
if (_refreshViewDirty)
{
refreshView();
_refreshViewDirty = false;
}
}
void ListView::addEventListenerListView(Object *target, SEL_ListViewEvent selector)
2013-11-08 20:29:49 +08:00
{
_listViewEventListener = target;
_listViewEventSelector = selector;
}
2013-12-23 15:02:52 +08:00
void ListView::selectedItemEvent()
2013-11-08 20:29:49 +08:00
{
if (_listViewEventListener && _listViewEventSelector)
{
2013-11-14 13:42:10 +08:00
(_listViewEventListener->*_listViewEventSelector)(this, LISTVIEW_ONSELECTEDITEM);
2013-11-08 20:29:49 +08:00
}
}
2013-12-23 15:02:52 +08:00
void ListView::interceptTouchEvent(int handleState, Widget *sender, const Point &touchPoint)
2013-11-08 20:29:49 +08:00
{
2013-12-23 15:02:52 +08:00
ScrollView::interceptTouchEvent(handleState, sender, touchPoint);
2013-11-08 20:29:49 +08:00
if (handleState != 1)
{
2013-12-23 15:02:52 +08:00
Widget* parent = sender;
2013-11-08 20:29:49 +08:00
while (parent)
{
if (parent && parent->getParent() == _innerContainer)
{
_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
}
selectedItemEvent();
}
}
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
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();
_refreshViewDirty = true;
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);
2013-09-13 22:20:20 +08:00
}
}
}
2013-12-23 15:02:52 +08:00
NS_CC_END