Exemple #1
0
void Widget::setContentSize(const cocos2d::Size &contentSize)
{
    ProtectedNode::setContentSize(contentSize);
    
    _customSize = contentSize;
    if (_ignoreSize)
    {
        _contentSize = getVirtualRendererSize();
    }
    
    if (_running)
    {
        Widget* widgetParent = getWidgetParent();
        Size pSize;
        if (widgetParent)
        {
            pSize = widgetParent->getContentSize();
        }
        else
        {
            pSize = _parent->getContentSize();
        }
        float spx = 0.0f;
        float spy = 0.0f;
        if (pSize.width > 0.0f)
        {
            spx = _customSize.width / pSize.width;
        }
        if (pSize.height > 0.0f)
        {
            spy = _customSize.height / pSize.height;
        }
        _sizePercent = Vec2(spx, spy);
    }
    onSizeChanged();
}
Exemple #2
0
bool Widget::onTouchBegan(Touch *touch, Event *unusedEvent)
{
    _hitted = false;
    if (isVisible() && isEnabled() && isAncestorsEnabled() && isAncestorsVisible(this) )
    {
        _touchBeganPosition = touch->getLocation();
        if(hitTest(_touchBeganPosition) && isClippingParentContainsPoint(_touchBeganPosition))
        {
            _hitted = true;
        }
    }
    if (!_hitted)
    {
        return false;
    }
    setHighlighted(true);
    Widget* widgetParent = getWidgetParent();
    if (widgetParent)
    {
        widgetParent->interceptTouchEvent(TouchEventType::BEGAN, this, touch);
    }
    pushDownEvent();
    return true;
}
bool Widget::onTouchBegan(Touch *touch, Event *unusedEvent)
{
    _hitted = false;
    if (isVisible() && isEnabled() && isAncestorsEnabled() && isAncestorsVisible(this) )
    {
        _touchStartPos = touch->getLocation();
        if(hitTest(_touchStartPos) && clippingParentAreaContainPoint(_touchStartPos))
        {
            _hitted = true;
        }
    }
    if (!_hitted)
    {
        return false;
    }
    setHighlighted(true);
    Widget* widgetParent = getWidgetParent();
    if (widgetParent)
    {
        widgetParent->checkChildInfo(0,this,_touchStartPos);
    }
    pushDownEvent();
    return true;
}
void Widget::updateSizeAndPosition()
{
    switch (_sizeType)
    {
        case SIZE_ABSOLUTE:
        {
            if (_ignoreSize)
            {
                _size = getContentSize();
            }
            else
            {
                _size = _customSize;
            }
            Widget* widgetParent = getWidgetParent();
            if (widgetParent)
            {
                Size pSize = widgetParent->getSize();
                float spx = 0.0f;
                float spy = 0.0f;
                if (pSize.width > 0.0f)
                {
                    spx = _customSize.width / pSize.width;
                }
                if (pSize.height > 0.0f)
                {
                    spy = _customSize.height / pSize.height;
                }
                _sizePercent = Point(spx, spy);
            }
            else
            {
                Size pSize = _parent->getContentSize();
                float spx = 0.0f;
                float spy = 0.0f;
                if (pSize.width > 0.0f)
                {
                    spx = _customSize.width / pSize.width;
                }
                if (pSize.height > 0.0f)
                {
                    spy = _customSize.height / pSize.height;
                }
                _sizePercent = Point(spx, spy);
            }
            break;
        }
        case SIZE_PERCENT:
        {
            Widget* widgetParent = getWidgetParent();
            if (widgetParent)
            {
                Size cSize = Size(widgetParent->getSize().width * _sizePercent.x , widgetParent->getSize().height * _sizePercent.y);
                if (_ignoreSize)
                {
                    _size = getContentSize();
                }
                else
                {
                    _size = cSize;
                }
                _customSize = cSize;
            }
            else
            {
                Size cSize = Size(_parent->getContentSize().width * _sizePercent.x , _parent->getContentSize().height * _sizePercent.y);
                if (_ignoreSize)
                {
                    _size = getContentSize();
                }
                else
                {
                    _size = cSize;
                }
                _customSize = cSize;
            }
        }
            break;
        default:
            break;
    }
    onSizeChanged();
    Point absPos = getPosition();
    switch (_positionType)
    {
        case POSITION_ABSOLUTE:
        {
            Widget* widgetParent = getWidgetParent();
            if (widgetParent)
            {
                Size pSize = widgetParent->getSize();
                if (pSize.width <= 0.0f || pSize.height <= 0.0f)
                {
                    _positionPercent = Point::ZERO;
                }
                else
                {
                    _positionPercent = Point(absPos.x / pSize.width, absPos.y / pSize.height);
                }
            }
            else
            {
                Size pSize = _parent->getContentSize();
                if (pSize.width <= 0.0f || pSize.height <= 0.0f)
                {
                    _positionPercent = Point::ZERO;
                }
                else
                {
                    _positionPercent = Point(absPos.x / pSize.width, absPos.y / pSize.height);
                }
            }
            break;
        }
        case POSITION_PERCENT:
        {
            Widget* widgetParent = getWidgetParent();
            if (widgetParent)
            {
                Size parentSize = widgetParent->getSize();
                absPos = Point(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y);
            }
            else
            {
                Size parentSize = _parent->getContentSize();
                absPos = Point(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y);
            }
            break;
        }
        default:
            break;
    }
    setPosition(absPos);
}