cocos2dx封裝一個具有Layout功能的Point類 (提供原始碼)

峻峰飛陽發表於2018-01-11

(原創文章,轉載請註明原文出處:http://blog.csdn.net/while0/article/details/79032004)


基於cocos2dx開發遊戲,免不了設定節點或精靈的位置,這些位置座標常常不是一個絕對座標值,而是相對於其它節點的相對座標。例如:精靈A與精靈B左對齊,精靈A與精靈B中心對齊等等。


計算這些相對座標值,每次都需要進行計算,計算時要考慮到精靈的anchorPoint, scale等,比較繁瑣,一不留神就搞錯了,除錯來除錯去浪費時間。本文封裝了一個很方便的工具類,幫助你計算這些相對座標。


直接上原始碼:

GmbsPoint.h (無cpp檔案)

#ifndef __GMBSPOINT_H__
#define __GMBSPOINT_H__

#include "cocos2d.h"
#include "GmbsGrid.h"

NS_CC_BEGIN

class GmbsPoint : public Point
{
public:
    Node *m_targetNode;

    GmbsPoint(Node* targetNode = NULL)
    {
        m_targetNode = targetNode;
        if (targetNode)
        {
            Point pt = targetNode->getPosition();
            x = pt.x;
            y = pt.y;
        }
    }

    GmbsPoint(Node* targetNode, float xx, float yy)
    {
        m_targetNode = targetNode;
        x = xx;
        y = yy;
    }

    GmbsPoint& reset(Node* targetNode, float xx = 0, float yy = 0)
    {
        m_targetNode = targetNode;
        x = xx;
        y = yy;
        return *this;
    }

public:
    GmbsPoint& leftAlign(Node* baseNode, float leftPadding = 0)
    {
        Point baseAnchorPoint(0, 0);
        Point basePoint = baseNode->getPosition();
        Node* baseParent = baseNode->getParent();
        if (baseParent != NULL)
            basePoint = baseParent->convertToWorldSpace(basePoint);
        if (!baseNode->isIgnoreAnchorPointForPosition())
            baseAnchorPoint = baseNode->getAnchorPoint();
        basePoint.x -= baseNode->getContentSize().width * baseAnchorPoint.x * getScaleX(baseNode);

        Point point(basePoint.x + leftPadding, basePoint.y);
        Point anchorPoint(0, 0);
        if (!m_targetNode->isIgnoreAnchorPointForPosition())
            anchorPoint = m_targetNode->getAnchorPoint();
        point.x += m_targetNode->getContentSize().width * anchorPoint.x * getScaleX(m_targetNode);
        point = m_targetNode->getParent()->convertToNodeSpace(point);
        x = point.x;
        return *this;
    }

    GmbsPoint& leftAlign(GmbsGrid& baseGrid, float leftPadding = 0)
    {
        return leftAlign(baseGrid.m_ownerNode, leftPadding + baseGrid.origin.x);
    }

    GmbsPoint& rightAlign(Node* baseNode, float rightPadding = 0)
    {
        Point baseAnchorPoint(0, 0);
        Point basePoint = baseNode->getPosition();
        Node* baseParent = baseNode->getParent();
        if (baseParent != NULL)
            basePoint = baseParent->convertToWorldSpace(basePoint);
        if (!baseNode->isIgnoreAnchorPointForPosition())
            baseAnchorPoint = baseNode->getAnchorPoint();
        basePoint.x += baseNode->getContentSize().width * (1 - baseAnchorPoint.x) * getScaleX(baseNode);

        Point point(basePoint.x - rightPadding, basePoint.y);
        Point anchorPoint(0, 0);
        if (!m_targetNode->isIgnoreAnchorPointForPosition())
            anchorPoint = m_targetNode->getAnchorPoint();
        point.x -= m_targetNode->getContentSize().width * (1 - anchorPoint.x) * getScaleX(m_targetNode);
        point = m_targetNode->getParent()->convertToNodeSpace(point);
        x = point.x;
        return *this;
    }

    GmbsPoint& rightAlign(GmbsGrid& baseGrid, float rightPadding = 0)
    {
        Node* baseNode = baseGrid.m_ownerNode;
        Point baseAnchorPoint(0, 0);
        Point basePoint = baseNode->getPosition();
        Node* baseParent = baseNode->getParent();
        if (baseParent != NULL)
            basePoint = baseParent->convertToWorldSpace(basePoint);
        if (!baseNode->isIgnoreAnchorPointForPosition())
            baseAnchorPoint = baseNode->getAnchorPoint();
        basePoint.x += baseGrid.origin.x + baseGrid.size.width - baseNode->getContentSize().width * baseAnchorPoint.x * getScaleX(baseNode);

        Point point(basePoint.x - rightPadding, basePoint.y);
        Point anchorPoint(0, 0);
        if (!m_targetNode->isIgnoreAnchorPointForPosition())
            anchorPoint = m_targetNode->getAnchorPoint();
        point.x -= m_targetNode->getContentSize().width * (1 - anchorPoint.x) * getScaleX(m_targetNode);
        point = m_targetNode->getParent()->convertToNodeSpace(point);
        x = point.x;
        return *this;
    }

    GmbsPoint& topAlign(Node* baseNode, float topPadding = 0)
    {
        Point baseAnchorPoint(0, 0);
        Point basePoint = baseNode->getPosition();
        Node* baseParent = baseNode->getParent();
        if (baseParent != NULL)
            basePoint = baseParent->convertToWorldSpace(basePoint);
        if (!baseNode->isIgnoreAnchorPointForPosition())
            baseAnchorPoint = baseNode->getAnchorPoint();
        basePoint.y += baseNode->getContentSize().height * (1 - baseAnchorPoint.y) * getScaleY(baseNode);

        Point point(basePoint.x, basePoint.y - topPadding);
        Point anchorPoint(0, 0);
        if (!m_targetNode->isIgnoreAnchorPointForPosition())
            anchorPoint = m_targetNode->getAnchorPoint();
        point.y -= m_targetNode->getContentSize().height * (1 - anchorPoint.y) * getScaleY(m_targetNode);
        point = m_targetNode->getParent()->convertToNodeSpace(point);
        y = point.y;
        return *this;
    }

    GmbsPoint& topAlign(GmbsGrid& baseGrid, float topPadding = 0)
    {
        Node* baseNode = baseGrid.m_ownerNode;
        Point baseAnchorPoint(0, 0);
        Point basePoint = baseNode->getPosition();
        Node* baseParent = baseNode->getParent();
        if (baseParent != NULL)
            basePoint = baseParent->convertToWorldSpace(basePoint);
        if (!baseNode->isIgnoreAnchorPointForPosition())
            baseAnchorPoint = baseNode->getAnchorPoint();
        basePoint.y += baseGrid.origin.y + baseGrid.size.height - baseNode->getContentSize().height * baseAnchorPoint.y * getScaleY(baseNode);

        Point point(basePoint.x, basePoint.y - topPadding);
        Point anchorPoint(0, 0);
        if (!m_targetNode->isIgnoreAnchorPointForPosition())
            anchorPoint = m_targetNode->getAnchorPoint();
        point.y -= m_targetNode->getContentSize().height * (1 - anchorPoint.y) * getScaleY(m_targetNode);
        point = m_targetNode->getParent()->convertToNodeSpace(point);
        y = point.y;
        return *this;
    }

    GmbsPoint& bottomAlign(Node* baseNode, float bottomPadding = 0)
    {
        Point baseAnchorPoint(0, 0);
        Point basePoint = baseNode->getPosition();
        Node* baseParent = baseNode->getParent();
        if (baseParent != NULL)
            basePoint = baseParent->convertToWorldSpace(basePoint);
        if (!baseNode->isIgnoreAnchorPointForPosition())
            baseAnchorPoint = baseNode->getAnchorPoint();
        basePoint.y -= baseNode->getContentSize().height * baseAnchorPoint.y * getScaleY(baseNode);

        Point point(basePoint.x, basePoint.y + bottomPadding);
        Point anchorPoint(0, 0);
        if (!m_targetNode->isIgnoreAnchorPointForPosition())
            anchorPoint = m_targetNode->getAnchorPoint();
        point.y += m_targetNode->getContentSize().height * anchorPoint.y * getScaleY(m_targetNode);
        point = m_targetNode->getParent()->convertToNodeSpace(point);
        y = point.y;
        return *this;
    }

    GmbsPoint& bottomAlign(GmbsGrid& baseGrid, float bottomPadding = 0)
    {
        return bottomAlign(baseGrid.m_ownerNode, bottomPadding + baseGrid.origin.y);
    }

    GmbsPoint& xMiddleAlign(Node* baseNode, float padding = 0)
    {
        Point baseAnchorPoint(0, 0);
        Point basePoint = baseNode->getPosition();
        Node* baseParent = baseNode->getParent();
        if (baseParent != NULL)
            basePoint = baseParent->convertToWorldSpace(basePoint);
        if (!baseNode->isIgnoreAnchorPointForPosition())
            baseAnchorPoint = baseNode->getAnchorPoint();
        basePoint.x += baseNode->getContentSize().width * (0.5 - baseAnchorPoint.x) * getScaleX(baseNode);

        Point point(basePoint.x + padding, basePoint.y);
        Point anchorPoint(0, 0);
        if (!m_targetNode->isIgnoreAnchorPointForPosition())
            anchorPoint = m_targetNode->getAnchorPoint();
        point.x -= m_targetNode->getContentSize().width * (0.5 - anchorPoint.x) * getScaleX(m_targetNode);
        point = m_targetNode->getParent()->convertToNodeSpace(point);
        x = point.x;
        return *this;
    }

    GmbsPoint& xMiddleAlign(GmbsGrid& baseGrid, float padding = 0)
    {
        Node* baseNode = baseGrid.m_ownerNode;
        Point baseAnchorPoint(0, 0);
        Point basePoint = baseNode->getPosition();
        Node* baseParent = baseNode->getParent();
        if (baseParent != NULL)
            basePoint = baseParent->convertToWorldSpace(basePoint);
        if (!baseNode->isIgnoreAnchorPointForPosition())
            baseAnchorPoint = baseNode->getAnchorPoint();
        basePoint.x += baseGrid.origin.x + baseGrid.size.width/2 - baseNode->getContentSize().width * baseAnchorPoint.x * getScaleX(baseNode);
        
        Point point(basePoint.x + padding, basePoint.y);
        Point anchorPoint(0, 0);
        if (!m_targetNode->isIgnoreAnchorPointForPosition())
            anchorPoint = m_targetNode->getAnchorPoint();
        point.x -= m_targetNode->getContentSize().width * (0.5 - anchorPoint.x) * getScaleX(m_targetNode);
        point = m_targetNode->getParent()->convertToNodeSpace(point);
        x = point.x;
        return *this;
    }

    GmbsPoint& yMiddleAlign(Node* baseNode, float padding = 0)
    {
        Point baseAnchorPoint(0, 0);
        Point basePoint = baseNode->getPosition();
        Node* baseParent = baseNode->getParent();
        if (baseParent != NULL)
            basePoint = baseParent->convertToWorldSpace(basePoint);
        if (!baseNode->isIgnoreAnchorPointForPosition())
            baseAnchorPoint = baseNode->getAnchorPoint();
        basePoint.y += baseNode->getContentSize().height * (0.5 - baseAnchorPoint.y) * getScaleY(baseNode);

        Point point(basePoint.x, basePoint.y + padding);
        Point anchorPoint(0, 0);
        if (!m_targetNode->isIgnoreAnchorPointForPosition())
            anchorPoint = m_targetNode->getAnchorPoint();
        point.y -= m_targetNode->getContentSize().height * (0.5 - anchorPoint.y) * getScaleY(m_targetNode);
        point = m_targetNode->getParent()->convertToNodeSpace(point);
        y = point.y;
        return *this;
    }

    GmbsPoint& yMiddleAlign(GmbsGrid& baseGrid, float padding = 0)
    {
        Node* baseNode = baseGrid.m_ownerNode;
        Point baseAnchorPoint(0, 0);
        Point basePoint = baseNode->getPosition();
        Node* baseParent = baseNode->getParent();
        if (baseParent != NULL)
            basePoint = baseParent->convertToWorldSpace(basePoint);
        if (!baseNode->isIgnoreAnchorPointForPosition())
            baseAnchorPoint = baseNode->getAnchorPoint();
        basePoint.y += baseGrid.origin.y + baseGrid.size.height/2 - baseNode->getContentSize().height * baseAnchorPoint.y * getScaleY(baseNode);

        Point point(basePoint.x, basePoint.y + padding);
        Point anchorPoint(0, 0);
        if (!m_targetNode->isIgnoreAnchorPointForPosition())
            anchorPoint = m_targetNode->getAnchorPoint();
        point.y -= m_targetNode->getContentSize().height * (0.5 - anchorPoint.y) * getScaleY(m_targetNode);
        point = m_targetNode->getParent()->convertToNodeSpace(point);
        y = point.y;
        return *this;
    }

    GmbsPoint& rightTo(Node* baseNode, float rightPadding = 0)
    {
        Point baseAnchorPoint(0, 0);
        Point basePoint = baseNode->getPosition();
        Node* baseParent = baseNode->getParent();
        if (baseParent != NULL)
            basePoint = baseParent->convertToWorldSpace(basePoint);
        if (!baseNode->isIgnoreAnchorPointForPosition())
            baseAnchorPoint = baseNode->getAnchorPoint();
        basePoint.x -= baseNode->getContentSize().width * baseAnchorPoint.x * getScaleX(baseNode);

        Point point(basePoint.x - rightPadding, basePoint.y);
        Point anchorPoint(0, 0);
        if (!m_targetNode->isIgnoreAnchorPointForPosition())
            anchorPoint = m_targetNode->getAnchorPoint();
        point.x -= m_targetNode->getContentSize().width * (1 - anchorPoint.x) * getScaleX(m_targetNode);;
        point = m_targetNode->getParent()->convertToNodeSpace(point);
        x = point.x;
        return *this;
    }

    GmbsPoint& leftTo(Node* baseNode, float leftPadding = 0)
    {
        Point baseAnchorPoint(0, 0);
        Point basePoint = baseNode->getPosition();
        Node* baseParent = baseNode->getParent();
        if (baseParent != NULL)
            basePoint = baseParent->convertToWorldSpace(basePoint);
        if (!baseNode->isIgnoreAnchorPointForPosition())
            baseAnchorPoint = baseNode->getAnchorPoint();
        basePoint.x += baseNode->getContentSize().width * (1 - baseAnchorPoint.x) * getScaleX(baseNode);

        Point point(basePoint.x + leftPadding, basePoint.y);
        Point anchorPoint(0, 0);
        if (!m_targetNode->isIgnoreAnchorPointForPosition())
            anchorPoint = m_targetNode->getAnchorPoint();
        point.x += m_targetNode->getContentSize().width * anchorPoint.x * getScaleX(m_targetNode);;
        point = m_targetNode->getParent()->convertToNodeSpace(point);
        x = point.x;
        return *this;
    }

    GmbsPoint& bottomTo(Node* baseNode, float bottomPadding = 0)
    {
        Point baseAnchorPoint(0, 0);
        Point basePoint = baseNode->getPosition();
        Node* baseParent = baseNode->getParent();
        if (baseParent != NULL)
            basePoint = baseParent->convertToWorldSpace(basePoint);
        if (!baseNode->isIgnoreAnchorPointForPosition())
            baseAnchorPoint = baseNode->getAnchorPoint();
        basePoint.y += baseNode->getContentSize().height * (1 - baseAnchorPoint.y) * getScaleY(baseNode);

        Point point(basePoint.x, basePoint.y + bottomPadding);
        Point anchorPoint(0, 0);
        if (!m_targetNode->isIgnoreAnchorPointForPosition())
            anchorPoint = m_targetNode->getAnchorPoint();
        point.y += m_targetNode->getContentSize().height * anchorPoint.y * getScaleY(m_targetNode);;
        point = m_targetNode->getParent()->convertToNodeSpace(point);
        y = point.y;
        return *this;
    }

    GmbsPoint& topTo(Node* baseNode, float topPadding = 0)
    {
        Point baseAnchorPoint(0, 0);
        Point basePoint = baseNode->getPosition();
        Node* baseParent = baseNode->getParent();
        if (baseParent != NULL)
            basePoint = baseParent->convertToWorldSpace(basePoint);
        if (!baseNode->isIgnoreAnchorPointForPosition())
            baseAnchorPoint = baseNode->getAnchorPoint();
        basePoint.y -= baseNode->getContentSize().height * baseAnchorPoint.y * getScaleY(baseNode);;

        Point point(basePoint.x, basePoint.y - topPadding);
        Point anchorPoint(0, 0);
        if (!m_targetNode->isIgnoreAnchorPointForPosition())
            anchorPoint = m_targetNode->getAnchorPoint();
        point.y -= m_targetNode->getContentSize().height * (1 - anchorPoint.y) * getScaleY(m_targetNode);;;
        point = m_targetNode->getParent()->convertToNodeSpace(point);
        y = point.y;
        return *this;
    }

    static float getScaleX(Node* node)
    {
        float scale = node->getScaleX();
        Node* parent = node->getParent();
        while (parent != NULL)
        {
            scale *= parent->getScaleX();
            parent = parent->getParent();
        }
        return scale;
    }

    static float getScaleY(Node* node)
    {
        float scale = node->getScaleY();
        Node* parent = node->getParent();
        while (parent != NULL)
        {
            scale *= parent->getScaleY();
            parent = parent->getParent();
        }
        return scale;
    }
};

NS_CC_END

#endif
GmbsGrid.h (無cpp檔案)

#ifndef __GMBSGRID_H__
#define __GMBSGRID_H__

#include "cocos2d.h"

NS_CC_BEGIN

class GmbsGrid : public Rect
{
protected:
    int m_xNum;
    int m_yNum;
    GmbsGrid** m_children;

public:
    Node *m_ownerNode;

    GmbsGrid(Node* ownerNode, int xNum, int yNum)
    {
        m_ownerNode = ownerNode;
    	origin = Point(0, 0);
    	size = ownerNode->getContentSize();
    	m_xNum = xNum;
    	m_yNum = yNum;
    	m_children = (GmbsGrid**)malloc(xNum*yNum*sizeof(GmbsGrid*));
    	memset(m_children, 0, xNum*yNum*sizeof(GmbsGrid*));
    }

    GmbsGrid(Node* ownerNode, Rect& rect, int xNum, int yNum)
    {
    	m_ownerNode = ownerNode;
    	origin = rect.origin;
    	size = rect.size;
    	m_xNum = xNum;
    	m_yNum = yNum;
    	m_children = (GmbsGrid**)malloc(xNum*yNum*sizeof(GmbsGrid*));
    	memset(m_children, 0, xNum*yNum*sizeof(GmbsGrid*));
    }

    GmbsGrid(Rect& rect, int xNum, int yNum)
    {
    	m_ownerNode = NULL;
    	origin = rect.origin;
    	size = rect.size;
    	m_xNum = xNum;
    	m_yNum = yNum;
    	m_children = (GmbsGrid**)malloc(xNum*yNum*sizeof(GmbsGrid*));
    	memset(m_children, 0, xNum*yNum*sizeof(GmbsGrid*));
    }

    virtual ~GmbsGrid()
    {
    	release();
    }

    void release()
    {
		for (int j = 0; j < m_yNum; j++)
		{
			for (int i = 0; i < m_xNum; i++)
			{
				if (m_children[j*m_xNum + i] != NULL)
				{
				    delete m_children[j*m_xNum + i];
				    m_children[j*m_xNum + i] = NULL;
				}
			}
		}
	}

    GmbsGrid& child(int x, int y)
    {
    	if (m_children[y*m_xNum + x] == NULL)
    	{
    		Rect rect;
    		rect.size.setSize(size.width/m_xNum, size.height/m_yNum);
    		rect.origin.setPoint(origin.x + rect.size.width*x, origin.y + rect.size.height*y);
    		m_children[y*m_xNum + x] = new GmbsGrid(m_ownerNode, rect, 1, 1);
    	}
    	return *m_children[y*m_xNum + x];
    }

    //counting from up to down
    GmbsGrid& childUtd(int x, int y)
    {
        int yNew = m_yNum - 1 - y;
    	if (m_children[yNew*m_xNum + x] == NULL)
    	{
    		Rect rect;
    		rect.size.setSize(size.width/m_xNum, size.height/m_yNum);
    		rect.origin.setPoint(origin.x + rect.size.width*x, origin.y + rect.size.height*yNew);
    		m_children[yNew*m_xNum + x] = new GmbsGrid(m_ownerNode, rect, 1, 1);
    	}
    	return *m_children[yNew*m_xNum + x];
    }

    void setOwnerNode(Node* ownerNode)
    {
    	m_ownerNode = ownerNode;
    }
};

NS_CC_END

#endif

舉例:

1) spriteA與spriteB中心對齊:

GmbsPoint pt(spriteA);
pt.xMiddleAlign(spriteB).yMiddleAlign(spriteB);
spriteA->setPosition(pt);


2) spriteA與spriteB左對齊且底對齊:

GmbsPoint pt(spriteA);  
pt.leftAlign(spriteB).bottomAlign(spriteB);  
spriteA->setPosition(pt);

3) spriteA在spriteB左側,且相距間隔為10,它們底部對齊:

GmbsPoint pt(spriteA);  
pt.leftTo(spriteB, 10).bottomAlign(spriteB);  
spriteA->setPosition(pt);



相關文章