Skip to content

Commit

Permalink
Improve relation system to handle condition that anchor is set.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoguzhu committed May 22, 2018
1 parent 7f93ed1 commit 4e9c74e
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 118 deletions.
40 changes: 33 additions & 7 deletions libfairygui/Classes/GObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ GObject::~GObject()
{
removeFromParent();

if(_displayObject)
{
_displayObject->removeFromParent();
CC_SAFE_RELEASE(_displayObject);
}
if (_displayObject)
{
_displayObject->removeFromParent();
CC_SAFE_RELEASE(_displayObject);
}
for (int i = 0; i < 8; i++)
CC_SAFE_DELETE(_gears[i]);
CC_SAFE_DELETE(_relations);
Expand Down Expand Up @@ -133,6 +133,32 @@ void GObject::setPosition(float xv, float yv)
}
}

float GObject::getXMin() const
{
return _pivotAsAnchor ? (_position.x - _size.width * _pivot.x) : _position.x;
}

void GObject::setXMin(float value)
{
if (_pivotAsAnchor)
setPosition(value + _size.width * _pivot.x, _position.y);
else
setPosition(value, _position.y);
}

float GObject::getYMin() const
{
return _pivotAsAnchor ? (_position.y - _size.height * _pivot.y) : _position.y;
}

void GObject::setYMin(float value)
{
if (_pivotAsAnchor)
setPosition(_position.x, value + _size.height * _pivot.y);
else
setPosition(_position.x, value);
}

void GObject::setPixelSnapping(bool value)
{
if (_pixelSnapping != value)
Expand Down Expand Up @@ -186,7 +212,7 @@ void GObject::setSize(float wv, float hv, bool ignorePivot /*= false*/)

if (_parent != nullptr)
{
_relations->onOwnerSizeChanged(dWidth, dHeight);
_relations->onOwnerSizeChanged(dWidth, dHeight, _pivotAsAnchor || !ignorePivot);
_parent->setBoundsChangedFlag();
if (_group != nullptr)
_group->setBoundsChangedFlag(true);
Expand Down Expand Up @@ -928,7 +954,7 @@ void GObject::onTouchMove(EventContext* context)
_dragTesting = false;
if (!dispatchEvent(UIEventType::DragStart))
dragBegin(evt->getTouchId());
}
}

if (_draggingObject == this)
{
Expand Down
7 changes: 6 additions & 1 deletion libfairygui/Classes/GObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ class GObject : public UIEventDispatcher
void setY(float value);
const cocos2d::Vec2& getPosition()const { return _position; }
void setPosition(float xv, float yv);
float getXMin() const;
void setXMin(float value);
float getYMin() const;
void setYMin(float value);

bool getPixelSnapping() const { return _pixelSnapping; }
bool isPixelSnapping() const { return _pixelSnapping; }
void setPixelSnapping(bool value);

float getWidth() const { return _size.width; }
Expand All @@ -61,6 +65,7 @@ class GObject : public UIEventDispatcher

const cocos2d::Vec2& getPivot() const { return _pivot; }
void setPivot(float xv, float yv, bool asAnchor = false);
bool isPivotAsAnchor() const { return _pivotAsAnchor; }

float getScaleX() const { return _scale.x; }
void setScaleX(float value) { setScale(value, _scale.y); }
Expand Down
2 changes: 1 addition & 1 deletion libfairygui/Classes/GProgressBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void GProgressBar::tweenValue(double value, float duration)

void GProgressBar::update(double newValue)
{
float percent = MIN(newValue / _max, 1);
float percent = _max != 0 ? MIN(newValue / _max, 1) : 0;

if (_titleObject != nullptr)
{
Expand Down
Loading

0 comments on commit 4e9c74e

Please sign in to comment.