axmol/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIDragPanel.cpp

1252 lines
30 KiB
C++
Raw Normal View History

2013-09-13 22:20:20 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
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.
****************************************************************************/
#include "UIDragPanel.h"
#include "../../System/UILayer.h"
NS_CC_EXT_BEGIN
UIDragPanel::UIDragPanel()
2013-09-17 20:13:23 +08:00
: _innerContainer(NULL)
, _touchPressed(false)
, _touchMoved(false)
, _touchReleased(false)
, _touchCanceld(false)
, _touchStartNodeSpace(Point::ZERO)
, _touchStartWorldSpace(Point::ZERO)
, _touchEndWorldSpace(Point::ZERO)
, _slidTime(0.0f)
, _moveType(DRAGPANEL_MOVE_TYPE_AUTOMOVE)
, _autoMoveDuration(0.5f)
, _autoMoveEaseRate(2.0f)
, _eventLister(NULL)
, _eventSelector(NULL)
, _berthDirection(DRAGPANEL_BERTH_DIR_NONE)
, _bounceEnable(false)
, _bounceDirection(DRAGPANEL_BOUNCE_DIR_NONE)
, _bounceDuration(0.5f)
, _bounceEaseRate(2.0f)
, _runningAction(false)
, _actionType(0)
, _actionWidget(NULL)
, _duration(0.0f)
, _elapsed(0.0f)
, _firstTick(false)
, _positionDelta(Point::ZERO)
, _startPosition(Point::ZERO)
, _previousPosition(Point::ZERO)
, _endPosition(Point::ZERO)
2013-09-13 22:20:20 +08:00
{
}
UIDragPanel::~UIDragPanel()
{
}
UIDragPanel* UIDragPanel::create()
{
UIDragPanel* widget = new UIDragPanel();
if (widget && widget->init())
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return NULL;
}
bool UIDragPanel::init()
{
if (Layout::init())
{
setUpdateEnabled(true);
setTouchEnabled(true);
setClippingEnabled(true);
return true;
}
return false;
}
void UIDragPanel::initRenderer()
{
Layout::initRenderer();
2013-09-17 20:13:23 +08:00
_innerContainer = Layout::create();
Layout::addChild(_innerContainer);
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::releaseResoures()
{
setUpdateEnabled(false);
removeAllChildren();
2013-09-16 15:32:52 +08:00
_renderer->removeAllChildrenWithCleanup(true);
_renderer->removeFromParentAndCleanup(true);
_renderer->release();
2013-09-13 22:20:20 +08:00
2013-09-17 20:13:23 +08:00
Layout::removeChild(_innerContainer);
2013-09-13 22:20:20 +08:00
2013-09-16 15:32:52 +08:00
_children->release();
2013-09-13 22:20:20 +08:00
}
bool UIDragPanel::onTouchBegan(const Point &touchPoint)
{
bool pass = Layout::onTouchBegan(touchPoint);
handlePressLogic(touchPoint);
return pass;
}
void UIDragPanel::onTouchMoved(const Point &touchPoint)
{
Layout::onTouchMoved(touchPoint);
handleMoveLogic(touchPoint);
}
void UIDragPanel::onTouchEnded(const Point &touchPoint)
{
Layout::onTouchEnded(touchPoint);
handleReleaseLogic(touchPoint);
}
void UIDragPanel::onTouchCancelled(const Point &touchPoint)
{
Layout::onTouchCancelled(touchPoint);
}
void UIDragPanel::onTouchLongClicked(const Point &touchPoint)
{
}
void UIDragPanel::update(float dt)
{
// widget action
2013-09-17 20:13:23 +08:00
if (_runningAction)
2013-09-13 22:20:20 +08:00
{
if (actionIsDone())
{
actionDone();
actionStop();
}
else
{
actionStep(dt);
}
}
recordSlidTime(dt);
}
bool UIDragPanel::addChild(UIWidget *widget)
{
2013-09-17 20:13:23 +08:00
_innerContainer->addChild(widget);
2013-09-13 22:20:20 +08:00
return true;
}
bool UIDragPanel::removeChild(UIWidget *child)
{
bool value = false;
2013-09-17 20:13:23 +08:00
if (_innerContainer->removeChild(child))
2013-09-13 22:20:20 +08:00
{
value = true;
}
return value;
}
void UIDragPanel::removeAllChildren()
{
2013-09-17 20:13:23 +08:00
_innerContainer->removeAllChildren();
2013-09-13 22:20:20 +08:00
}
Array* UIDragPanel::getChildren()
{
2013-09-17 20:13:23 +08:00
return _innerContainer->getChildren();
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::onSizeChanged()
{
Layout::onSizeChanged();
2013-09-17 20:13:23 +08:00
Size innerSize = _innerContainer->getSize();
2013-09-13 22:20:20 +08:00
float orginInnerSizeWidth = innerSize.width;
float orginInnerSizeHeight = innerSize.height;
2013-09-16 15:32:52 +08:00
float innerSizeWidth = MAX(orginInnerSizeWidth, _size.width);
float innerSizeHeight = MAX(orginInnerSizeHeight, _size.height);
2013-09-17 20:13:23 +08:00
_innerContainer->setSize(Size(innerSizeWidth, innerSizeHeight));
2013-09-13 22:20:20 +08:00
}
const Size& UIDragPanel::getInnerContainerSize() const
{
2013-09-17 20:13:23 +08:00
return _innerContainer->getContentSize();
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::setInnerContainerSize(const cocos2d::Size &size)
{
2013-09-16 15:32:52 +08:00
float innerSizeWidth = _size.width;
float innerSizeHeight = _size.height;
if (size.width < _size.width)
2013-09-13 22:20:20 +08:00
{
CCLOG("Inner width <= scrollview width, it will be force sized!");
}
else
{
innerSizeWidth = size.width;
}
2013-09-16 15:32:52 +08:00
if (size.height < _size.height)
2013-09-13 22:20:20 +08:00
{
CCLOG("Inner height <= scrollview height, it will be force sized!");
}
else
{
innerSizeHeight = size.height;
}
2013-09-17 20:13:23 +08:00
_innerContainer->setSize(Size(innerSizeWidth, innerSizeHeight));
_innerContainer->setPosition(Point(0, _size.height - _innerContainer->getSize().height));
2013-09-13 22:20:20 +08:00
}
const Point& UIDragPanel::getInnerContainerPosition() const
{
2013-09-17 20:13:23 +08:00
return _innerContainer->getPosition();
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::setInnerContainerPosition(const Point &point, bool animated)
{
2013-09-17 20:13:23 +08:00
Point delta = point - _innerContainer->getPosition();
2013-09-16 20:54:13 +08:00
2013-09-17 20:13:23 +08:00
// Point delta = ccpSub(point, _innerContainer->getPosition());
2013-09-13 22:20:20 +08:00
setInnerContainerOffset(delta, animated);
}
void UIDragPanel::setInnerContainerOffset(const Point &offset, bool animated)
{
if (animated)
{
Point delta = offset;
if (checkToBoundaryWithDeltaPosition(delta))
{
delta = calculateToBoundaryDeltaPosition(delta);
}
2013-09-17 20:13:23 +08:00
actionStartWithWidget(_innerContainer);
moveByWithDuration(_autoMoveDuration, delta);
2013-09-13 22:20:20 +08:00
}
else
{
setInnerContainerOffset(offset);
}
}
void UIDragPanel::setInnerContainerOffset(const Point &offset)
{
Point delta = offset;
if (checkToBoundaryWithDeltaPosition(delta))
{
delta = calculateToBoundaryDeltaPosition(delta);
}
moveWithDelta(delta);
if (checkBerth())
{
berthEvent();
}
}
void UIDragPanel::handlePressLogic(const Point &touchPoint)
{
// check inner rect < drag panel rect
if (checkContainInnerRect())
{
2013-09-17 20:13:23 +08:00
_touchPressed = false;
2013-09-13 22:20:20 +08:00
return;
}
2013-09-17 20:13:23 +08:00
_touchPressed = true;
_touchMoved = false;
_touchReleased = false;
_touchCanceld = false;
2013-09-13 22:20:20 +08:00
2013-09-17 20:13:23 +08:00
if (_runningAction)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
switch (_moveType)
2013-09-13 22:20:20 +08:00
{
case DRAGPANEL_MOVE_TYPE_AUTOMOVE:
stopAutoMove();
actionStop();
break;
case DRAGPANEL_MOVE_TYPE_BOUNCE:
2013-09-17 20:13:23 +08:00
_touchPressed = false;
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
}
2013-09-16 15:32:52 +08:00
Point nsp = _renderer->convertToNodeSpace(touchPoint);
2013-09-17 20:13:23 +08:00
_touchStartNodeSpace = nsp;
2013-09-13 22:20:20 +08:00
2013-09-17 20:13:23 +08:00
_touchStartWorldSpace = touchPoint;
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::handleMoveLogic(const Point &touchPoint)
{
2013-09-17 20:13:23 +08:00
if (!_touchPressed)
2013-09-13 22:20:20 +08:00
{
return;
}
// check touch out of drag panel boundary
2013-09-17 20:13:23 +08:00
if (_touchCanceld)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-17 20:13:23 +08:00
_touchMoved = true;
2013-09-13 22:20:20 +08:00
2013-09-16 15:32:52 +08:00
Point nsp = _renderer->convertToNodeSpace(touchPoint);
2013-09-17 20:13:23 +08:00
Point delta = nsp - _touchStartNodeSpace;
// Point delta = ccpSub(nsp, _touchStartNodeSpace);
_touchStartNodeSpace = nsp;
2013-09-13 22:20:20 +08:00
// reset berth dir to none
2013-09-17 20:13:23 +08:00
if (!_bounceEnable)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
_berthDirection = DRAGPANEL_BERTH_DIR_NONE;
2013-09-13 22:20:20 +08:00
}
// check will berth (bounce disable)
2013-09-17 20:13:23 +08:00
if (!_bounceEnable)
2013-09-13 22:20:20 +08:00
{
if (checkToBoundaryWithDeltaPosition(delta))
{
delta = calculateToBoundaryDeltaPosition(delta);
}
}
// move
moveWithDelta(delta);
// check bounce or berth
2013-09-17 20:13:23 +08:00
if (_bounceEnable)
2013-09-13 22:20:20 +08:00
{
// bounce
if (!hitTest(touchPoint))
{
2013-09-17 20:13:23 +08:00
_touchMoved = false;
2013-09-13 22:20:20 +08:00
if (checkNeedBounce())
{
2013-09-17 20:13:23 +08:00
_touchCanceld = true;
2013-09-13 22:20:20 +08:00
startBounce();
}
}
}
else
{
// berth
if (checkBerth())
{
berthEvent();
}
}
}
void UIDragPanel::handleReleaseLogic(const Point &touchPoint)
{
2013-09-17 20:13:23 +08:00
if (!_touchPressed)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-17 20:13:23 +08:00
_touchPressed = false;
_touchMoved = false;
_touchReleased = true;
_touchCanceld = false;
2013-09-13 22:20:20 +08:00
// check touch out of drag panel boundary
2013-09-17 20:13:23 +08:00
if (_touchCanceld)
2013-09-13 22:20:20 +08:00
{
return;
}
if (hitTest(touchPoint))
{
2013-09-17 20:13:23 +08:00
_touchEndWorldSpace = touchPoint;
2013-09-13 22:20:20 +08:00
startAutoMove();
}
}
void UIDragPanel::checkChildInfo(int handleState, UIWidget *sender, const Point &touchPoint)
{
interceptTouchEvent(handleState, sender, touchPoint);
}
void UIDragPanel::interceptTouchEvent(int handleState, UIWidget *sender, const Point &touchPoint)
{
switch (handleState)
{
case 0:
handlePressLogic(touchPoint);
break;
case 1:
{
2013-09-16 20:54:13 +08:00
// float offset = ccpDistance(sender->getTouchStartPos(), touchPoint);
2013-09-13 22:20:20 +08:00
float offset = sender->getTouchStartPos().getDistance(touchPoint);
if (offset > 5.0)
{
sender->setFocused(false);
handleMoveLogic(touchPoint);
}
}
break;
case 2:
handleReleaseLogic(touchPoint);
break;
case 3:
break;
}
}
void UIDragPanel::recordSlidTime(float dt)
{
2013-09-17 20:13:23 +08:00
if (_touchPressed)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
_slidTime += dt;
2013-09-13 22:20:20 +08:00
}
}
// check if dragpanel rect contain inner rect
bool UIDragPanel::checkContainInnerRect()
{
2013-09-16 15:32:52 +08:00
float width = _size.width;
float height = _size.height;
2013-09-17 20:13:23 +08:00
float innerWidth = _innerContainer->getSize().width;
float innerHeight = _innerContainer->getSize().height;
2013-09-13 22:20:20 +08:00
if (innerWidth <= width && innerHeight <= height)
{
return true;
}
return false;
}
// move
void UIDragPanel::moveWithDelta(const Point &delta)
2013-09-16 20:54:13 +08:00
{
2013-09-17 20:13:23 +08:00
Point newPos = _innerContainer->getPosition() + delta;
// Point newPos = ccpAdd(_innerContainer->getPosition(), delta);
_innerContainer->setPosition(newPos);
2013-09-13 22:20:20 +08:00
}
// auto move
void UIDragPanel::autoMove()
{
2013-09-17 20:13:23 +08:00
if (_bounceEnable)
2013-09-13 22:20:20 +08:00
{
if (checkNeedBounce())
{
stopAutoMove();
startBounce();
}
}
}
void UIDragPanel::autoMoveOver()
{
stopAutoMove();
if (checkBerth())
{
berthEvent();
2013-09-17 20:13:23 +08:00
_berthDirection = DRAGPANEL_BERTH_DIR_NONE;
2013-09-13 22:20:20 +08:00
}
}
void UIDragPanel::startAutoMove()
{
2013-09-17 20:13:23 +08:00
_moveType = DRAGPANEL_MOVE_TYPE_AUTOMOVE;
2013-09-13 22:20:20 +08:00
actionStop();
2013-09-16 20:54:13 +08:00
2013-09-17 20:13:23 +08:00
Point delta = _touchEndWorldSpace - _touchStartWorldSpace;
// Point delta = ccpSub(m_touchEndWorldSpace, _touchStartWorldSpace);
delta.x /= _slidTime * 60;
delta.y /= _slidTime * 60;
_slidTime = 0.0;
2013-09-13 22:20:20 +08:00
// bounceEnable is disable
2013-09-17 20:13:23 +08:00
if (!_bounceEnable)
2013-09-13 22:20:20 +08:00
{
if (checkToBoundaryWithDeltaPosition(delta))
{
delta = calculateToBoundaryDeltaPosition(delta);
}
}
2013-09-17 20:13:23 +08:00
actionStartWithWidget(_innerContainer);
moveByWithDuration(_autoMoveDuration, delta);
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::stopAutoMove()
{
2013-09-17 20:13:23 +08:00
_moveType = DRAGPANEL_MOVE_TYPE_NONE;
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::setAutoMoveDuration(float duration)
{
2013-09-17 20:13:23 +08:00
_autoMoveDuration = duration;
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::setAutoMoveEaseRate(float rate)
{
2013-09-17 20:13:23 +08:00
_autoMoveEaseRate = rate;
2013-09-13 22:20:20 +08:00
}
// berth
// check if move to boundary
bool UIDragPanel::checkToBoundaryWithDeltaPosition(const Point& delta)
{
2013-09-17 20:13:23 +08:00
float innerLeft = _innerContainer->getLeftInParent();
float innerTop = _innerContainer->getTopInParent();
float innerRight = _innerContainer->getRightInParent();
float innerBottom = _innerContainer->getBottomInParent();
2013-09-13 22:20:20 +08:00
float left = 0;
2013-09-16 15:32:52 +08:00
float top = _size.height;
float right = _size.width;
2013-09-13 22:20:20 +08:00
float bottom = 0;
bool toLeftBottom = false;
bool toLeftTop = false;
bool toRightBottom = false;
bool toRightTop = false;
bool toLeft = false;
bool toRight = false;
bool toTop = false;
bool toBottom = false;
if (innerLeft + delta.x > left && innerBottom + delta.y > bottom) // left bottom
{
toLeftBottom = true;
}
else if (innerLeft + delta.x > left && innerTop + delta.y < top) // left top
{
toLeftTop = true;
}
else if (innerRight + delta.x < right && innerBottom + delta.y > bottom) // right bottom
{
toRightBottom = true;
}
else if (innerRight + delta.x < right && innerTop + delta.y < top) // right top
{
toRightTop = true;
}
else if (innerLeft + delta.x > left) // left
{
toLeft = true;
}
else if (innerRight + delta.x < right) // right
{
toRight = true;
}
else if (innerTop + delta.y < top) // top
{
toTop = true;
}
else if (innerBottom + delta.y > bottom) // bottom
{
toBottom = true;
}
if (toLeft || toTop || toRight || toBottom
|| toLeftBottom || toLeftTop || toRightBottom || toRightTop)
{
return true;
}
return false;
}
Point UIDragPanel::calculateToBoundaryDeltaPosition(const Point& paramDelta)
{
2013-09-17 20:13:23 +08:00
float innerLeft = _innerContainer->getLeftInParent();
float innerTop = _innerContainer->getTopInParent();
float innerRight = _innerContainer->getRightInParent();
float innerBottom = _innerContainer->getBottomInParent();
2013-09-13 22:20:20 +08:00
float left = 0;
2013-09-16 15:32:52 +08:00
float top = _size.height;
float right = _size.width;
2013-09-13 22:20:20 +08:00
float bottom = 0;
Point delta = paramDelta;
if (innerLeft + delta.x > left && innerBottom + delta.y > bottom) // left bottom
{
delta.x = left - innerLeft;
delta.y = bottom - innerBottom;
}
else if (innerLeft + delta.x > left && innerTop + delta.y < top) // left top
{
delta.x = left - innerLeft;
delta.y = top - innerTop;
}
else if (innerRight + delta.x < right && innerBottom + delta.y > bottom) // right bottom
{
delta.x = right - innerRight;
delta.y = bottom - innerBottom;
}
else if (innerRight + delta.x < right && innerTop + delta.y < top) // right bottom
{
delta.x = right - innerRight;
delta.y = top - innerTop;
}
else if (innerLeft + delta.x > left) // left
{
delta.x = left - innerLeft;
}
else if (innerRight + delta.x < right) // right
{
delta.x = right - innerRight;
}
else if (innerTop + delta.y < top) // top
{
delta.y = top - innerTop;
}
else if (innerBottom + delta.y > bottom) // bottom
{
delta.y = bottom - innerBottom;
}
return delta;
}
bool UIDragPanel::isBerth()
{
2013-09-17 20:13:23 +08:00
return _berthDirection != DRAGPANEL_BERTH_DIR_NONE;
2013-09-13 22:20:20 +08:00
}
// check berth
bool UIDragPanel::checkBerth()
{
2013-09-17 20:13:23 +08:00
float innerLeft = _innerContainer->getLeftInParent();
float innerTop = _innerContainer->getTopInParent();
float innerRight = _innerContainer->getRightInParent();
float innerBottom = _innerContainer->getBottomInParent();
2013-09-13 22:20:20 +08:00
float left = 0;
2013-09-16 15:32:52 +08:00
float top = _size.height;
float right = _size.width;
2013-09-13 22:20:20 +08:00
float bottom = 0;
if (innerLeft == left && innerBottom == bottom) // left bottom
{
2013-09-17 20:13:23 +08:00
_berthDirection = DRAGPANEL_BERTH_DIR_LEFTBOTTOM;
2013-09-13 22:20:20 +08:00
}
else if (innerLeft == left && innerTop == top) // left top
{
2013-09-17 20:13:23 +08:00
_berthDirection = DRAGPANEL_BERTH_DIR_LFETTOP;
2013-09-13 22:20:20 +08:00
}
else if (innerRight == right && innerBottom == bottom) // right bottom
{
2013-09-17 20:13:23 +08:00
_berthDirection = DRAGPANEL_BERTH_DIR_RIGHTBOTTOM;
2013-09-13 22:20:20 +08:00
}
else if (innerRight == right && innerTop == top) // right top
{
2013-09-17 20:13:23 +08:00
_berthDirection = DRAGPANEL_BERTH_DIR_RIGHTTOP;
2013-09-13 22:20:20 +08:00
}
else if (innerLeft == left) // left
{
2013-09-17 20:13:23 +08:00
_berthDirection = DRAGPANEL_BERTH_DIR_LEFT;
2013-09-13 22:20:20 +08:00
}
else if (innerRight == right) // right
{
2013-09-17 20:13:23 +08:00
_berthDirection = DRAGPANEL_BERTH_DIR_RIGHT;
2013-09-13 22:20:20 +08:00
}
else if (innerTop == top) // top
{
2013-09-17 20:13:23 +08:00
_berthDirection = DRAGPANEL_BERTH_DIR_TOP;
2013-09-13 22:20:20 +08:00
}
else if (innerBottom == bottom) // bottom
{
2013-09-17 20:13:23 +08:00
_berthDirection = DRAGPANEL_BERTH_DIR_BOTTOM;
2013-09-13 22:20:20 +08:00
}
2013-09-17 20:13:23 +08:00
if (_berthDirection != DRAGPANEL_BERTH_DIR_NONE)
2013-09-13 22:20:20 +08:00
{
return true;
}
return false;
}
void UIDragPanel::berthEvent()
{
2013-09-17 20:13:23 +08:00
switch (_berthDirection)
2013-09-13 22:20:20 +08:00
{
case DRAGPANEL_BERTH_DIR_LEFTBOTTOM:
berthToLeftBottomEvent();
break;
case DRAGPANEL_BERTH_DIR_LFETTOP:
berthToLeftTopEvent();
break;
case DRAGPANEL_BERTH_DIR_RIGHTBOTTOM:
berthToRightBottomEvent();
break;
case DRAGPANEL_BERTH_DIR_RIGHTTOP:
berthToRightTopEvent();
break;
case DRAGPANEL_BERTH_DIR_LEFT:
berthToLeftEvent();
break;
case DRAGPANEL_BERTH_DIR_TOP:
berthToTopEvent();
break;
case DRAGPANEL_BERTH_DIR_RIGHT:
berthToRightEvent();
break;
case DRAGPANEL_BERTH_DIR_BOTTOM:
berthToBottomEvent();
break;
default:
break;
}
}
void UIDragPanel::berthToLeftBottomEvent()
{
2013-09-17 20:13:23 +08:00
if (_eventLister && _eventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
(_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_LEFTBOTTOM);
2013-09-13 22:20:20 +08:00
}
}
void UIDragPanel::berthToLeftTopEvent()
{
2013-09-17 20:13:23 +08:00
if (_eventLister && _eventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
(_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_LFETTOP);
2013-09-13 22:20:20 +08:00
}
}
void UIDragPanel::berthToRightBottomEvent()
{
2013-09-17 20:13:23 +08:00
if (_eventLister && _eventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
(_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_RIGHTBOTTOM);
2013-09-13 22:20:20 +08:00
}
}
void UIDragPanel::berthToRightTopEvent()
{
2013-09-17 20:13:23 +08:00
if (_eventLister && _eventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
(_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_RIGHTTOP);
2013-09-13 22:20:20 +08:00
}
}
void UIDragPanel::berthToLeftEvent()
{
2013-09-17 20:13:23 +08:00
if (_eventLister && _eventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
(_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_LEFT);
2013-09-13 22:20:20 +08:00
}
}
void UIDragPanel::berthToTopEvent()
{
2013-09-17 20:13:23 +08:00
if (_eventLister && _eventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
(_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_TOP);
2013-09-13 22:20:20 +08:00
}
}
void UIDragPanel::berthToRightEvent()
{
2013-09-17 20:13:23 +08:00
if (_eventLister && _eventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
(_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_RIGHT);
2013-09-13 22:20:20 +08:00
}
}
void UIDragPanel::berthToBottomEvent()
{
2013-09-17 20:13:23 +08:00
if (_eventLister && _eventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
(_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_BOTTOM);
2013-09-13 22:20:20 +08:00
}
}
2013-09-17 20:13:23 +08:00
void UIDragPanel::addEventListener(Object *target, SEL_DragPanelEvent selector)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
_eventLister = target;
_eventSelector = selector;
2013-09-13 22:20:20 +08:00
}
// bounce
bool UIDragPanel::isBounceEnable()
{
2013-09-17 20:13:23 +08:00
return _bounceEnable;
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::setBounceEnable(bool bounce)
{
2013-09-17 20:13:23 +08:00
_bounceEnable = bounce;
2013-09-13 22:20:20 +08:00
}
bool UIDragPanel::checkNeedBounce()
{
2013-09-17 20:13:23 +08:00
float innerLeft = _innerContainer->getLeftInParent();
float innerTop = _innerContainer->getTopInParent();
float innerRight = _innerContainer->getRightInParent();
float innerBottom = _innerContainer->getBottomInParent();
2013-09-13 22:20:20 +08:00
float left = 0;
2013-09-16 15:32:52 +08:00
float top = _size.height;
float right = _size.width;
2013-09-13 22:20:20 +08:00
float bottom = 0;
bool need = ((innerLeft > left && innerBottom > bottom)
|| (innerLeft > left && innerTop < top)
|| (innerRight < right && innerBottom > bottom)
|| (innerRight < right && innerTop < top)
|| (innerLeft > left)
|| (innerTop < top)
|| (innerRight < right)
|| (innerBottom > bottom));
return need;
}
void UIDragPanel::startBounce()
{
2013-09-17 20:13:23 +08:00
if (_moveType == DRAGPANEL_MOVE_TYPE_BOUNCE)
2013-09-13 22:20:20 +08:00
{
return;
}
actionStop();
2013-09-17 20:13:23 +08:00
_moveType = DRAGPANEL_MOVE_TYPE_BOUNCE;
2013-09-13 22:20:20 +08:00
bounceToCorner();
}
void UIDragPanel::stopBounce()
{
2013-09-17 20:13:23 +08:00
_moveType = DRAGPANEL_MOVE_TYPE_NONE;
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::bounceToCorner()
{
2013-09-17 20:13:23 +08:00
float innerLeft = _innerContainer->getLeftInParent();
float innerTop = _innerContainer->getTopInParent();
float innerRight = _innerContainer->getRightInParent();
float innerBottom = _innerContainer->getBottomInParent();
2013-09-13 22:20:20 +08:00
2013-09-16 15:32:52 +08:00
float width = _size.width;
float height = _size.height;
2013-09-13 22:20:20 +08:00
float left = 0;
float top = height;
float right = width;
float bottom = 0;
float from_x = 0;
float from_y = 0;
float to_x = 0;
float to_y = 0;
Point delta = Point::ZERO;
if (innerLeft > left && innerBottom > bottom) // left bottom
{
from_x = innerLeft;
from_y = innerBottom;
to_x = left;
to_y = bottom;
2013-09-17 20:13:23 +08:00
_bounceDirection = DRAGPANEL_BOUNCE_DIR_LEFTBOTTOM;
2013-09-13 22:20:20 +08:00
}
else if (innerLeft > left && innerTop < top) // left top
{
from_x = innerLeft;
from_y = innerTop;
to_x = left;
to_y = top;
2013-09-17 20:13:23 +08:00
_bounceDirection = DRAGPANEL_BOUNCE_DIR_LEFTTOP;
2013-09-13 22:20:20 +08:00
}
else if (innerRight < right && innerBottom > bottom) // right bottom
{
from_x = innerRight;
from_y = innerBottom;
to_x = right;
to_y = bottom;
2013-09-17 20:13:23 +08:00
_bounceDirection = DRAGPANEL_BOUNCE_DIR_RIGHTBOTTOM;
2013-09-13 22:20:20 +08:00
}
else if (innerRight < right && innerTop < top) // right top
{
from_x = innerRight;
from_y = innerTop;
to_x = right;
to_y = top;
2013-09-17 20:13:23 +08:00
_bounceDirection = DRAGPANEL_BOUNCE_DIR_RIGHTTOP;
2013-09-13 22:20:20 +08:00
}
else if (innerLeft > left) // left
{
from_x = innerLeft;
from_y = innerBottom;
to_x = left;
to_y = from_y;
2013-09-17 20:13:23 +08:00
_bounceDirection = DRAGPANEL_BOUNCE_DIR_LEFT;
2013-09-13 22:20:20 +08:00
}
else if (innerTop < top) // top
{
from_x = innerLeft;
from_y = innerTop;
to_x = from_x;
to_y = top;
2013-09-17 20:13:23 +08:00
_bounceDirection = DRAGPANEL_BOUNCE_DIR_TOP;
2013-09-13 22:20:20 +08:00
}
else if (innerRight < right) // right
{
from_x = innerRight;
from_y = innerBottom;
to_x = right;
to_y = from_y;
2013-09-17 20:13:23 +08:00
_bounceDirection = DRAGPANEL_BOUNCE_DIR_RIGHT;
2013-09-13 22:20:20 +08:00
}
else if (innerBottom > bottom) // bottom
{
from_x = innerLeft;
from_y = innerBottom;
to_x = from_x;
to_y = bottom;
2013-09-17 20:13:23 +08:00
_bounceDirection = DRAGPANEL_BOUNCE_DIR_BOTTOM;
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
delta = Point(to_x, to_y) - Point(from_x, from_y);
// delta = ccpSub(ccp(to_x, to_y), ccp(from_x, from_y));
2013-09-13 22:20:20 +08:00
2013-09-17 20:13:23 +08:00
actionStartWithWidget(_innerContainer);
moveByWithDuration(_bounceDuration, delta);
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::bounceOver()
{
stopBounce();
2013-09-17 20:13:23 +08:00
switch (_bounceDirection)
2013-09-13 22:20:20 +08:00
{
case DRAGPANEL_BOUNCE_DIR_LEFTBOTTOM:
bounceToLeftBottomEvent();
break;
case DRAGPANEL_BOUNCE_DIR_LEFTTOP:
bounceToLeftTopEvent();
break;
case DRAGPANEL_BOUNCE_DIR_RIGHTBOTTOM:
bounceToRightBottomEvent();
break;
case DRAGPANEL_BOUNCE_DIR_RIGHTTOP:
bounceToRightTopEvent();
break;
case DRAGPANEL_BOUNCE_DIR_LEFT:
bounceToLeftEvent();
break;
case DRAGPANEL_BOUNCE_DIR_TOP:
bounceToTopEvent();
break;
case DRAGPANEL_BOUNCE_DIR_RIGHT:
bounceToRightEvent();
break;
case DRAGPANEL_BOUNCE_DIR_BOTTOM:
bounceToBottomEvent();
break;
default:
break;
}
2013-09-17 20:13:23 +08:00
_bounceDirection = DRAGPANEL_BOUNCE_DIR_NONE;
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::bounceToLeftBottomEvent()
{
2013-09-17 20:13:23 +08:00
if (_eventLister && _eventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
(_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_LEFTBOTTOM);
2013-09-13 22:20:20 +08:00
}
}
void UIDragPanel::bounceToLeftTopEvent()
{
2013-09-17 20:13:23 +08:00
if (_eventLister && _eventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
(_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_LEFTTOP);
2013-09-13 22:20:20 +08:00
}
}
void UIDragPanel::bounceToRightBottomEvent()
{
2013-09-17 20:13:23 +08:00
if (_eventLister && _eventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
(_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_RIGHTBOTTOM);
2013-09-13 22:20:20 +08:00
}
}
void UIDragPanel::bounceToRightTopEvent()
{
2013-09-17 20:13:23 +08:00
if (_eventLister && _eventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
(_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_RIGHTTOP);
2013-09-13 22:20:20 +08:00
}
}
void UIDragPanel::bounceToLeftEvent()
{
2013-09-17 20:13:23 +08:00
if (_eventLister && _eventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
(_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_LEFT);
2013-09-13 22:20:20 +08:00
}
}
void UIDragPanel::bounceToTopEvent()
{
2013-09-17 20:13:23 +08:00
if (_eventLister && _eventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
(_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_TOP);
2013-09-13 22:20:20 +08:00
}
}
void UIDragPanel::bounceToRightEvent()
{
2013-09-17 20:13:23 +08:00
if (_eventLister && _eventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
(_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_RIGHT);
2013-09-13 22:20:20 +08:00
}
}
void UIDragPanel::bounceToBottomEvent()
{
2013-09-17 20:13:23 +08:00
if (_eventLister && _eventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
(_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_BOTTOM);
2013-09-13 22:20:20 +08:00
}
}
// widget action
void UIDragPanel::actionWithDuration(float duration)
{
2013-09-17 20:13:23 +08:00
_duration = duration;
2013-09-13 22:20:20 +08:00
2013-09-17 20:13:23 +08:00
if (_duration == 0)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
_duration = FLT_EPSILON;
2013-09-13 22:20:20 +08:00
}
2013-09-17 20:13:23 +08:00
_elapsed = 0;
_firstTick = true;
2013-09-13 22:20:20 +08:00
}
bool UIDragPanel::actionIsDone()
{
2013-09-17 20:13:23 +08:00
bool value = (_elapsed >= _duration);
2013-09-13 22:20:20 +08:00
return value;
}
void UIDragPanel::actionStartWithWidget(UIWidget *widget)
{
2013-09-17 20:13:23 +08:00
_runningAction = true;
_actionWidget = widget;
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::actionStep(float dt)
{
2013-09-17 20:13:23 +08:00
if (_firstTick)
2013-09-13 22:20:20 +08:00
{
2013-09-17 20:13:23 +08:00
_firstTick = false;
_elapsed = 0;
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-17 20:13:23 +08:00
_elapsed += dt;
2013-09-13 22:20:20 +08:00
}
actionUpdate(MAX (0,
2013-09-17 20:13:23 +08:00
MIN(1, _elapsed /
MAX(_duration, FLT_EPSILON)
2013-09-13 22:20:20 +08:00
)
)
);
}
void UIDragPanel::actionUpdate(float dt)
{
2013-09-17 20:13:23 +08:00
switch (_actionType)
2013-09-13 22:20:20 +08:00
{
case 1: // move by
moveByUpdate(dt);
break;
case 2: // move to
moveToUpdate(dt);
break;
default:
break;
}
}
void UIDragPanel::actionStop()
{
2013-09-17 20:13:23 +08:00
_runningAction = false;
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::actionDone()
{
2013-09-17 20:13:23 +08:00
switch (_moveType)
2013-09-13 22:20:20 +08:00
{
case DRAGPANEL_MOVE_TYPE_AUTOMOVE:
autoMoveOver();
break;
case DRAGPANEL_MOVE_TYPE_BOUNCE:
bounceOver();
break;
default:
break;
}
}
// move by
void UIDragPanel::moveByWithDuration(float duration, const Point& deltaPosition)
{
actionWithDuration(duration);
2013-09-17 20:13:23 +08:00
_positionDelta = deltaPosition;
2013-09-13 22:20:20 +08:00
moveByInit();
2013-09-17 20:13:23 +08:00
_actionType = 1;
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::moveByInit()
{
2013-09-17 20:13:23 +08:00
_previousPosition = _startPosition = _actionWidget->getPosition();
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::moveByUpdate(float t)
{
float easeRate = 0.0f;
2013-09-17 20:13:23 +08:00
switch (_moveType)
2013-09-13 22:20:20 +08:00
{
case DRAGPANEL_MOVE_TYPE_AUTOMOVE:
2013-09-17 20:13:23 +08:00
easeRate = _autoMoveEaseRate;
2013-09-13 22:20:20 +08:00
break;
case DRAGPANEL_MOVE_TYPE_BOUNCE:
2013-09-17 20:13:23 +08:00
easeRate = _bounceEaseRate;
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
t = powf(t, 1 / easeRate);
2013-09-17 20:13:23 +08:00
Point currentPos = _actionWidget->getPosition();
Point diff = currentPos - _previousPosition;
_startPosition = _startPosition + diff;
// Point diff = ccpSub(currentPos, _previousPosition);
// _startPosition = ccpAdd( _startPosition, diff);
2013-09-13 22:20:20 +08:00
2013-09-17 20:13:23 +08:00
// Point newPos = ccpAdd( _startPosition, ccpMult(_positionDelta, t) );
Point newPos = _startPosition + (_positionDelta * t);
2013-09-16 20:54:13 +08:00
2013-09-17 20:13:23 +08:00
_actionWidget->setPosition(newPos);
_previousPosition = newPos;
2013-09-13 22:20:20 +08:00
2013-09-17 20:13:23 +08:00
switch (_moveType)
2013-09-13 22:20:20 +08:00
{
case DRAGPANEL_MOVE_TYPE_AUTOMOVE:
autoMove();
break;
default:
break;
}
}
// move to
void UIDragPanel::moveToWithDuration(float duration, const Point& position)
{
actionWithDuration(duration);
2013-09-17 20:13:23 +08:00
_endPosition = position;
2013-09-13 22:20:20 +08:00
moveToInit();
2013-09-17 20:13:23 +08:00
_actionType = 2;
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::moveToInit()
{
moveByInit();
2013-09-17 20:13:23 +08:00
_positionDelta = _endPosition - _actionWidget->getPosition();
// _positionDelta = ccpSub( _endPosition, _actionWidget->getPosition() );
2013-09-13 22:20:20 +08:00
}
void UIDragPanel::moveToUpdate(float t)
{
moveByUpdate(t);
}
Layout* UIDragPanel::getInnerContainer()
{
2013-09-17 20:13:23 +08:00
return _innerContainer;
2013-09-13 22:20:20 +08:00
}
2013-09-17 17:59:20 +08:00
const char* UIDragPanel::getDescription() const
{
return "DragPanel";
}
2013-09-13 22:20:20 +08:00
NS_CC_EXT_END