Beispiel #1
0
void* DBCCFactory::generateDisplay(const ITextureAtlas *textureAtlas, const TextureData *textureData, const DisplayData *displayData) const
{
    DBCCTextureAtlas *dbccTextureAtlas = (DBCCTextureAtlas*)(textureAtlas);

    if (!dbccTextureAtlas || !textureData) return nullptr;

    auto texture = dbccTextureAtlas->getTexture();
    assert(texture);

    const float x = textureData->region.x;
    const float y = textureData->region.y;
    const bool rotated = textureData->rotated;
    const float width = rotated ? textureData->region.height : textureData->region.width;
    const float height = rotated ? textureData->region.width : textureData->region.height;
    cocos2d::Rect rect(x, y, width, height);
    cocos2d::Vec2 offset;
    cocos2d::Size originSize(width, height);

    cocos2d::Node *display = nullptr;

    if (textureData->frame)
    {
        float frameX = -textureData->frame->x;
        float frameY = -textureData->frame->y;
        originSize.width = textureData->frame->width;
        originSize.height = textureData->frame->height;
        // offset = trimed center - origin center
        // y use cocos2d coordinates
        offset.x = (width - originSize.width) * 0.5 + frameX;
        offset.y = (originSize.height - height)*0.5 - frameY;

        auto spriteFrame = cocos2d::SpriteFrame::createWithTexture(texture, rect,
            textureData->rotated, offset, originSize);
        display = cocos2d::Sprite::createWithSpriteFrame(spriteFrame);
    }
    else
    {
        display = cocos2d::Sprite::createWithTexture(texture, rect, rotated);
    }
    // sprite
    
    display->setCascadeColorEnabled(true);
    display->setCascadeOpacityEnabled(true);
    display->retain();
    float pivotX = 0.f;
    float pivotY = 0.f;

    if (displayData)
    {
        pivotX = displayData->pivot.x;
        pivotY = displayData->pivot.y;
    }
    display->setAnchorPoint(cocos2d::Vec2(pivotX / originSize.width, 1.f - pivotY / originSize.height));
    display->setContentSize(originSize);
    return display;
}
bool CTriangleShapeView::HitTest(sf::Vector2f const & local) const
{
	sf::Vector2f a = m_triangle.getPoint(0);
	sf::Vector2f b = m_triangle.getPoint(1);
	sf::Vector2f c = m_triangle.getPoint(2);
	sf::FloatRect frame = GetFrame();
	sf::Vector2f originSize({ frame.width / 2.f, frame.height / 2.f });
	a = { frame.left, frame.top - originSize.y };
	b = { frame.left + b.x - originSize.x,  frame.top + b.y - originSize.y };
	c = { frame.left + c.x - originSize.x, frame.top + c.y - originSize.y };

	return (((local.x - a.x)*(b.y - a.y) - (local.y - a.y)*(b.x - a.x)) <= 0 &&
		((local.x - b.x)*(c.y - b.y) - (local.y - b.y)*(c.x - b.x)) <= 0 &&
		((local.x - a.x)*(c.y - a.y) - (local.y - a.y)*(c.x - a.x)) >= 0);
}
Beispiel #3
0
void CCSlot::_updateFrame()
{
    const auto frameDisplay = (DBCCSprite*)(this->_rawDisplay);

    if (this->_display && this->_displayIndex >= 0)
    {
        const unsigned displayIndex = this->_displayIndex;
        const auto rawDisplayData = displayIndex < this->_displayDataSet->displays.size() ? this->_displayDataSet->displays[displayIndex] : nullptr;
        const auto replacedDisplayData = displayIndex < this->_replacedDisplayDataSet.size() ? this->_replacedDisplayDataSet[displayIndex] : nullptr;
        const auto currentDisplayData = replacedDisplayData ? replacedDisplayData : rawDisplayData;
        const auto currentTextureData = static_cast<CCTextureData*>(currentDisplayData->textureData);

        if (currentTextureData)
        {
            if (!currentTextureData->texture)
            {
                const auto textureAtlasTexture = static_cast<CCTextureAtlasData*>(currentTextureData->parent)->texture;
                if (textureAtlasTexture)
                {
                    cocos2d::Rect rect(currentTextureData->region.x, currentTextureData->region.y, currentTextureData->region.width, currentTextureData->region.height);
                    cocos2d::Vec2 offset(0.f, 0.f);
                    cocos2d::Size originSize(currentTextureData->region.width, currentTextureData->region.height);

                    /*if (currentTextureData->frame) 
                    {
                        offset.setPoint(-currentTextureData->frame->x, -currentTextureData->frame->y);
                        originSize.setSize(currentTextureData->frame->width, currentTextureData->frame->height);
                    }*/

                    currentTextureData->texture = cocos2d::SpriteFrame::createWithTexture(textureAtlasTexture, rect, currentTextureData->rotated, offset, originSize); // TODO multiply textureAtlas
                    currentTextureData->texture->retain();
                }
            }

            const auto currentTexture = this->_armature->_replacedTexture ? static_cast<cocos2d::Texture2D*>(this->_armature->_replacedTexture) : (currentTextureData->texture ? currentTextureData->texture->getTexture() : nullptr);

            if (currentTexture)
            {
                if (this->_meshData && this->_display == this->_meshDisplay)
                {
                    const auto& region = currentTextureData->region;
                    const auto& textureAtlasSize = currentTextureData->texture->getTexture()->getContentSizeInPixels();
                    auto displayVertices = new cocos2d::V3F_C4B_T2F[(unsigned)(this->_meshData->uvs.size() / 2)]; // does cocos2dx release it?
                    auto vertexIndices = new unsigned short[this->_meshData->vertexIndices.size()]; // does cocos2dx release it?
                    cocos2d::Rect boundsRect(999999.f, 999999.f, -999999.f, -999999.f);

                    for (std::size_t i = 0, l = this->_meshData->uvs.size(); i < l; i += 2)
                    {
                        const auto iH = (unsigned)(i / 2);
                        const auto x = this->_meshData->vertices[i];
                        const auto y = this->_meshData->vertices[i + 1];
                        cocos2d::V3F_C4B_T2F vertexData;
                        vertexData.vertices.set(x, -y, 0.f);
                        vertexData.texCoords.u = (region.x + this->_meshData->uvs[i] * region.width) / textureAtlasSize.width;
                        vertexData.texCoords.v = (region.y + this->_meshData->uvs[i + 1] * region.height) / textureAtlasSize.height;
                        vertexData.colors = cocos2d::Color4B::WHITE;
                        displayVertices[iH] = vertexData;

                        if (boundsRect.origin.x > x)
                        {
                            boundsRect.origin.x = x;
                        }

                        if (boundsRect.size.width < x) 
                        {
                            boundsRect.size.width = x;
                        }

                        if (boundsRect.origin.y > -y)
                        {
                            boundsRect.origin.y = -y;
                        }

                        if (boundsRect.size.height < -y)
                        {
                            boundsRect.size.height = -y;
                        }
                    }

                    boundsRect.size.width -= boundsRect.origin.x;
                    boundsRect.size.height -= boundsRect.origin.y;

                    for (std::size_t i = 0, l = this->_meshData->vertexIndices.size(); i < l; ++i)
                    {
                        vertexIndices[i] = this->_meshData->vertexIndices[i];
                    }

                    // In cocos2dx render meshDisplay and frameDisplay are the same display
                    frameDisplay->setSpriteFrame(currentTextureData->texture); // polygonInfo will be override
                    if (currentTexture != currentTextureData->texture->getTexture())
                    {
                        frameDisplay->setTexture(currentTexture); // Relpace texture // polygonInfo will be override
                    }

                    //
                    cocos2d::PolygonInfo polygonInfo;
                    auto& triangles = polygonInfo.triangles;
                    triangles.verts = displayVertices;
                    triangles.indices = vertexIndices;
                    triangles.vertCount = (unsigned)(this->_meshData->uvs.size() / 2);
                    triangles.indexCount = (unsigned)(this->_meshData->vertexIndices.size());
                    polygonInfo.rect = boundsRect; // Copy
                    frameDisplay->setPolygonInfo(polygonInfo);
                    frameDisplay->setContentSize(boundsRect.size);

                    if (this->_meshData->skinned)
                    {
                        frameDisplay->setScale(1.f, 1.f);
                        frameDisplay->setRotationSkewX(0.f);
                        frameDisplay->setRotationSkewY(0.f);
                        frameDisplay->setPosition(0.f, 0.f);
                    }

                    frameDisplay->setAnchorPoint(cocos2d::Vec2::ZERO);
                }
                else
                {
                    cocos2d::Vec2 pivot(currentDisplayData->pivot.x, currentDisplayData->pivot.y);

                    const auto& rectData = currentTextureData->frame ? *currentTextureData->frame : currentTextureData->region;
                    auto width = rectData.width;
                    auto height = rectData.height;
                    if (!currentTextureData->frame && currentTextureData->rotated)
                    {
                        width = rectData.height;
                        height = rectData.width;
                    }

                    if (currentDisplayData->isRelativePivot)
                    {
                        pivot.x *= width;
                        pivot.y *= height;
                    }

                    if (currentTextureData->frame)
                    {
                        pivot.x += currentTextureData->frame->x;
                        pivot.y += currentTextureData->frame->y;
                    }

                    if (rawDisplayData && rawDisplayData != currentDisplayData)
                    {
                        pivot.x += rawDisplayData->transform.x - currentDisplayData->transform.x;
                        pivot.y += rawDisplayData->transform.y - currentDisplayData->transform.y;
                    }

                    pivot.x = pivot.x / currentTextureData->region.width;
                    pivot.y = 1.f - pivot.y / currentTextureData->region.height;

                    frameDisplay->setSpriteFrame(currentTextureData->texture); // polygonInfo will be override

                    if (currentTexture != currentTextureData->texture->getTexture())
                    {
                        frameDisplay->setTexture(currentTexture); // Relpace texture // polygonInfo will be override
                    }

                    frameDisplay->setAnchorPoint(pivot);
                }

                this->_updateVisible();

                return;
            }
        }
    }

    frameDisplay->setTexture(nullptr);
    frameDisplay->setTextureRect(cocos2d::Rect::ZERO);
    frameDisplay->setAnchorPoint(cocos2d::Vec2::ZERO);
    frameDisplay->setVisible(false);
}