2014-10-09 18:28:09 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "UILayoutComponent.h"
|
|
|
|
#include "2d/CCNode.h"
|
|
|
|
#include "GUIDefine.h"
|
|
|
|
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
LayoutComponent::LayoutComponent()
|
2014-10-30 22:25:46 +08:00
|
|
|
: _usingPercentContentSize(false)
|
|
|
|
, _referencePoint(ReferencePoint::BOTTOM_LEFT)
|
|
|
|
, _usingPercentPosition(false)
|
|
|
|
, _actived(true)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
|
|
|
_name = __LAYOUT_COMPONENT_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
LayoutComponent::~LayoutComponent()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LayoutComponent::init()
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!Component::init())
|
|
|
|
{
|
|
|
|
ret = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//put layout component initalized code here
|
|
|
|
|
|
|
|
} while (0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Size
|
2014-10-21 11:40:34 +08:00
|
|
|
Vec2 LayoutComponent::getOwnerContentSize()const
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
|
|
|
return this->getOwner()->getContentSize();
|
|
|
|
}
|
2014-10-17 19:13:37 +08:00
|
|
|
void LayoutComponent::setOwnerContentSize(const Vec2& size)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-10-17 19:13:37 +08:00
|
|
|
this->getOwner()->setContentSize(Size(size.x,size.y));
|
|
|
|
|
|
|
|
Node* parentNode = this->getOwner()->getParent();
|
|
|
|
if (parentNode != NULL && _actived)
|
|
|
|
{
|
|
|
|
Size parentSize = parentNode->getContentSize();
|
|
|
|
|
|
|
|
if (parentSize.width != 0 && parentSize.height != 0)
|
|
|
|
{
|
|
|
|
_percentContentSize = Point(size.x/parentSize.width,size.y/parentSize.height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_percentContentSize = Point(0,0);
|
|
|
|
}
|
|
|
|
}
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Vec2& LayoutComponent::getPercentContentSize()const
|
|
|
|
{
|
|
|
|
return _percentContentSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayoutComponent::setPercentContentSize(const Vec2& percent)
|
|
|
|
{
|
2014-10-17 19:13:37 +08:00
|
|
|
_percentContentSize = percent;
|
|
|
|
|
|
|
|
Node* parentNode = this->getOwner()->getParent();
|
|
|
|
if (parentNode != NULL && _actived)
|
|
|
|
{
|
|
|
|
Size parentSize = parentNode->getContentSize();
|
|
|
|
if (_usingPercentContentSize)
|
|
|
|
{
|
|
|
|
this->getOwner()->setContentSize(Size(percent.x*parentSize.width,percent.y*parentSize.height));
|
|
|
|
}
|
|
|
|
}
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LayoutComponent::isUsingPercentContentSize()
|
|
|
|
{
|
|
|
|
return _usingPercentContentSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayoutComponent::setUsingPercentContentSize(bool flag)
|
|
|
|
{
|
|
|
|
_usingPercentContentSize = flag;
|
|
|
|
|
|
|
|
Node* parentNode = this->getOwner()->getParent();
|
|
|
|
if (parentNode != NULL && _actived)
|
|
|
|
{
|
|
|
|
Size parentSize = parentNode->getContentSize();
|
2014-10-17 19:13:37 +08:00
|
|
|
if (_usingPercentContentSize)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-10-17 19:13:37 +08:00
|
|
|
Size baseSize = this->getOwner()->getContentSize();
|
|
|
|
if (parentSize.width != 0)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-10-17 19:13:37 +08:00
|
|
|
_percentContentSize.x = baseSize.width/parentSize.width;
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-17 19:13:37 +08:00
|
|
|
_percentContentSize.x = 0;
|
|
|
|
baseSize.width = 0;
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
2014-10-17 19:13:37 +08:00
|
|
|
|
|
|
|
if (parentSize.height != 0)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-10-17 19:13:37 +08:00
|
|
|
_percentContentSize.y = baseSize.height/parentSize.height;
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
2014-10-17 19:13:37 +08:00
|
|
|
else
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-10-17 19:13:37 +08:00
|
|
|
_percentContentSize.y = 0;
|
|
|
|
baseSize.height = 0;
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
2014-10-17 19:13:37 +08:00
|
|
|
|
|
|
|
this->getOwner()->setContentSize(baseSize);
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Position
|
|
|
|
bool LayoutComponent::isUsingPercentPosition()
|
|
|
|
{
|
|
|
|
return _usingPercentPosition;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setUsingPercentPosition(bool flag)
|
|
|
|
{
|
|
|
|
_usingPercentPosition = flag;
|
2014-10-17 19:13:37 +08:00
|
|
|
|
|
|
|
Node* parentNode = this->getOwner()->getParent();
|
|
|
|
if (parentNode != NULL && _actived)
|
|
|
|
{
|
|
|
|
Size parentSize = parentNode->getContentSize();
|
|
|
|
|
|
|
|
if (_usingPercentPosition)
|
|
|
|
{
|
|
|
|
if (parentSize.width != 0)
|
|
|
|
{
|
|
|
|
_percentPosition.x = _relativePosition.x/parentSize.width;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_percentPosition.x = 0;
|
|
|
|
_relativePosition.x = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parentSize.height != 0)
|
|
|
|
{
|
|
|
|
_percentPosition.y = _relativePosition.y/parentSize.height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_percentPosition.y = 0;
|
|
|
|
_relativePosition.y = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Point inversePoint = this->converPointWithReferencePointAndSize(_relativePosition,parentSize);
|
|
|
|
this->getOwner()->setPosition(inversePoint);
|
|
|
|
}
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Vec2& LayoutComponent::getPercentPosition()
|
|
|
|
{
|
|
|
|
return _percentPosition;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setPercentPosition(const Vec2& percent)
|
|
|
|
{
|
2014-10-17 19:13:37 +08:00
|
|
|
_percentPosition = percent;
|
|
|
|
|
|
|
|
Node* parentNode = this->getOwner()->getParent();
|
|
|
|
if (parentNode != NULL && _actived)
|
|
|
|
{
|
|
|
|
Size parentSize = parentNode->getContentSize();
|
|
|
|
_relativePosition = Point(_percentPosition.x*parentSize.width,_percentPosition.y*parentSize.height);
|
|
|
|
Point inversePoint = this->converPointWithReferencePointAndSize(_relativePosition,parentSize);
|
|
|
|
this->getOwner()->setPosition(inversePoint);
|
|
|
|
}
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Vec2& LayoutComponent::getOwnerPosition()const
|
|
|
|
{
|
|
|
|
return this->getOwner()->getPosition();
|
|
|
|
}
|
|
|
|
void LayoutComponent::setOwnerPosition(const Vec2& point)
|
|
|
|
{
|
2014-10-17 19:13:37 +08:00
|
|
|
Node* parentNode = this->getOwner()->getParent();
|
|
|
|
if (parentNode != NULL && _actived)
|
|
|
|
{
|
|
|
|
Size parentSize = parentNode->getContentSize();
|
|
|
|
|
|
|
|
Point inversePoint = this->converPointWithReferencePointAndSize(point,parentSize);
|
|
|
|
this->getOwner()->setPosition(point);
|
|
|
|
_relativePosition = inversePoint;
|
|
|
|
if (parentSize.width != 0 && parentSize.height != 0)
|
|
|
|
{
|
|
|
|
_percentPosition = Point(_relativePosition.x/parentSize.width,_relativePosition.y/parentSize.height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_percentPosition = Point(0,0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->getOwner()->setPosition(point);
|
|
|
|
if (_referencePoint == ReferencePoint::BOTTOM_LEFT)
|
|
|
|
{
|
|
|
|
_relativePosition = point;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Vec2& LayoutComponent::getRelativePosition()
|
|
|
|
{
|
|
|
|
return _relativePosition;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setRelativePosition(const Vec2& position)
|
|
|
|
{
|
2014-10-17 19:13:37 +08:00
|
|
|
_relativePosition = position;
|
2014-10-09 18:28:09 +08:00
|
|
|
|
|
|
|
Node* parentNode = this->getOwner()->getParent();
|
|
|
|
if (parentNode != NULL && _actived)
|
|
|
|
{
|
|
|
|
Size parentSize = parentNode->getContentSize();
|
|
|
|
|
2014-10-17 19:13:37 +08:00
|
|
|
Point inversePoint = this->converPointWithReferencePointAndSize(_relativePosition,parentSize);
|
|
|
|
this->getOwner()->setPosition(inversePoint);
|
|
|
|
if (parentSize.width != 0 && parentSize.height != 0)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-10-17 19:13:37 +08:00
|
|
|
_percentPosition = Point(_relativePosition.x/parentSize.width,_relativePosition.y/parentSize.height);
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
2014-10-17 19:13:37 +08:00
|
|
|
else
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-10-17 19:13:37 +08:00
|
|
|
_percentPosition = Point(0,0);
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-17 19:13:37 +08:00
|
|
|
LayoutComponent::ReferencePoint LayoutComponent::getReferencePoint()
|
|
|
|
{
|
|
|
|
return _referencePoint;
|
|
|
|
}
|
|
|
|
void LayoutComponent::setReferencePoint(ReferencePoint point)
|
|
|
|
{
|
|
|
|
_referencePoint = point;
|
|
|
|
this->setRelativePosition(_relativePosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayoutComponent::setActiveEnable(bool enable)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
|
|
|
_actived = enable;
|
|
|
|
}
|
2014-10-17 19:13:37 +08:00
|
|
|
|
|
|
|
Vec2 LayoutComponent::converPointWithReferencePointAndSize(const Vec2& point,const Size& size)
|
|
|
|
{
|
|
|
|
Point inversePoint = point;
|
|
|
|
switch (_referencePoint)
|
|
|
|
{
|
|
|
|
case ReferencePoint::TOP_LEFT:
|
|
|
|
inversePoint.y = size.height - inversePoint.y;
|
|
|
|
break;
|
|
|
|
case ReferencePoint::BOTTOM_RIGHT:
|
|
|
|
inversePoint.x = size.width - inversePoint.x;
|
|
|
|
break;
|
|
|
|
case ReferencePoint::TOP_RIGHT:
|
|
|
|
inversePoint.x = size.width - inversePoint.x;
|
|
|
|
inversePoint.y = size.height - inversePoint.y;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return inversePoint;
|
|
|
|
}
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|