首先对UI的部分调整
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//ui部分的修改文件
1.widget的clk的判断方式,move的时候如果已经判定为nohighlight就应该不要再次进入highlight状态
void Widget::onTouchMoved(Touch *touch, Event *unusedEvent)
{
…
//点击中的状态移动的时候才判断,如果已经点击不中就不要响应后面的
if (_highlight)
setHighlighted(hitTest(_touchMovePosition, _hittedByCamera, nullptr));
…
}
2.checkbox点击方式修改(如果cancel掉就不应该触发点击,也不应该改变select的状态)
void CheckBox::onTouchEnded(Touch *touch, Event *unusedEvent)
{
if (_highlight)
{
if (_isSelected)
{
setSelected(false);
AbstractCheckButton::onTouchEnded(touch, unusedEvent);
dispatchSelectChangedEvent(false);
}
else
{
setSelected(true);
AbstractCheckButton::onTouchEnded(touch, unusedEvent);
dispatchSelectChangedEvent(true);
}
}
else
{
AbstractCheckButton::onTouchEnded(touch, unusedEvent);
}
}
3.UIScrollView支持fix方式,就是从顶部插入也不会移动特别使用用聊天框锁定(默认是底部插入不移动)
void SetFixInnerPosition(bool bFix){ m_bFixInnerPosition = bFix; }
void ScrollView::setInnerContainerSize(const Size &size)
{
…
if (!m_bFixInnerPosition)
{
// move children appropriately
{
Size newInnerSize = _innerContainer->getContentSize();
float offsetY = originalInnerSize.height - newInnerSize.height;
float offsetX = 0;
if (_innerContainer->getRightBoundary() <= _contentSize.width)
{
offsetX = originalInnerSize.width - newInnerSize.width;
}
if (offsetX != 0 || offsetY != 0)
{
Vec2 position = _innerContainer->getPosition() + Vec2(offsetX, offsetY);
setInnerContainerPosition(position);
}
}
}
…
}
4.UIListView改动的最多,这边只列出部分修改,事件增加改变item的消息(主要是解决选中item和非选中item展示的问题),事件增加点击位置
typedef enum
{
LISTVIEW_ONSELECTEDITEM_START,
LISTVIEW_ONSELECTEDITEM_END,
LISTVIEW_ONSELECTEDITEM_CHANGEITEM,
}ListViewEventType;
enum class EventType
{
ON_SELECTED_ITEM_START,
ON_SELECTED_ITEM_END,
ON_SELECTED_ITEM_CHANGE,
};
…
typedef std::function<void(Ref*, EventType, Touch*)> ccListViewCallback;
…
void selectedItemEvent(TouchEventType event, Widget sender, Touch pTouch);
void ListView::selectedItemEvent(TouchEventType event, Widget sender, Touch pTouch)
{
this->retain();
switch (event)
{
case TouchEventType::BEGAN:
{
if (_listViewEventListener && _listViewEventSelector)
{
(_listViewEventListener->_listViewEventSelector)(this, LISTVIEW_ONSELECTEDITEM_START);
}
if (_eventCallback) {
_eventCallback(this, EventType::ON_SELECTED_ITEM_START, pTouch);
}
if (_ccEventCallback)
{
_ccEventCallback(this, static_cast(EventType::ON_SELECTED_ITEM_START));
}
}
break;
default:
{
if (_listViewEventListener && _listViewEventSelector)
{
(_listViewEventListener->_listViewEventSelector)(this, LISTVIEW_ONSELECTEDITEM_END);
}
if (_eventCallback) {
_eventCallback(this, EventType::ON_SELECTED_ITEM_END, pTouch);
}
if (_ccEventCallback)
{
_ccEventCallback(this, static_cast(EventType::ON_SELECTED_ITEM_END));
}
//end发完计算
if (event == TouchEventType::ENDED)
{
//只有ended才选定是哪个,begin只是显示高亮
Widget* parent = sender;
while (parent)
{
if (parent && (parent->getParent() == _innerContainer))
{
_curSelectedIndex = getIndex(parent);
break;
}
parent = dynamic_cast<Widget*>(parent->getParent());
}
}
if (_listViewEventListener && _listViewEventSelector)
{
(_listViewEventListener->*_listViewEventSelector)(this, LISTVIEW_ONSELECTEDITEM_CHANGEITEM);
}
if (_eventCallback) {
_eventCallback(this, EventType::ON_SELECTED_ITEM_CHANGE, pTouch);
}
if (_ccEventCallback)
{
_ccEventCallback(this, static_cast<int>(EventType::ON_SELECTED_ITEM_CHANGE));
}
}
break;
}
this->release();
}
void ListView::interceptTouchEvent(TouchEventType event, Widget sender, Touch touch)
{
ScrollView::interceptTouchEvent(event, sender, touch);
if (!_touchEnabled)
{
return;
}
if (event != TouchEventType::MOVED)
{
if (sender->isHighlighted()) {
selectedItemEvent(event, sender, touch);
}
}
}
5.UIListView根据位置获取index的计算方式(我觉得是个bug,上面的修改+这个修改为后续动态加载和显示listview做基础)
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 (fabs(distanceFromFirst) <= fabs(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 = itemPosition.y - targetPosition.y;
if (distanceFromMid > 0)
{
// Right half
return findClosestItem(targetPosition, items, itemAnchorPoint, midIndex, distanceFromMid, lastIndex, distanceFromLast);
}
else
{
// Left half
return findClosestItem(targetPosition, items, itemAnchorPoint, firstIndex, distanceFromFirst, midIndex, distanceFromMid);
}
}
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 = firstPosition.y - targetPosition.y;
ssize_t lastIndex = _items.size() - 1;
Vec2 lastPosition = calculateItemPositionWithAnchor(_items.at(lastIndex), itemAnchorPoint);
float distanceFromLast = lastPosition.y - targetPosition.y;
return findClosestItem(targetPosition, _items, itemAnchorPoint, firstIndex, distanceFromFirst, lastIndex, distanceFromLast);
}
6.Editbox我这边删除了本身的editbox,是使用现在主流游戏的模式点击之后在键盘的上面增加输入,而不是在实际的显示位置,这个可以查看以前别人的文章