cocos2dx封裝一個具有Layout功能的Point類 (提供原始碼)
(原創文章,轉載請註明原文出處: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);
相關文章
- cocos2dx原始碼:背景層封裝類原始碼封裝
- 一個C#封裝的加密解密類程式碼C#封裝加密解密
- 一個最簡單的類JQuery封裝jQuery封裝
- 一、類的封裝性封裝
- 【JavaScript框架封裝】實現一個類似於JQuery的動畫框架的封裝JavaScript框架封裝jQuery動畫
- PHP封裝的一個單例模式Mysql操作類PHP封裝單例模式MySql
- 【JavaScript框架封裝】實現一個類似於JQuery的CSS樣式框架的封裝JavaScript框架封裝jQueryCSS
- 驗證碼 生成 二三例(一般處理程式,封裝一個類)封裝
- 實現一個具有百度文庫文件轉換功能的工具類
- 如何封裝一個自動歸、解檔類封裝
- 【Android初級】如何實現一個具有選擇功能的對話方塊效果(附原始碼)Android原始碼
- Element原始碼分析系列1一Layout(佈局)原始碼
- 封裝一個通用的PopupWindow封裝
- Swift-定位,編碼/反編碼功能的封裝Swift封裝
- Flutter Dio原始碼分析(四)--封裝Flutter原始碼封裝
- JS功能封裝JS封裝
- android view layout原始碼分析AndroidView原始碼
- 將建立程式的API-posix_spawn封裝成一個程式類API封裝
- 【菜鳥學Java】3:封裝一個分頁類PageBeanJava封裝Bean
- java輔助開發的兩個封裝類Java封裝
- 封裝一個自己的js庫封裝JS
- 一個簡單的 Amqp 封裝MQ封裝
- php的curl封裝類PHP封裝
- cocos2dx原始碼:相框PhotoFrame原始碼
- JavaFx 生成二維碼工具類封裝Java封裝
- 封裝xunsearch類封裝
- JS 封裝類JS封裝
- 旅遊交友原始碼 旅行結伴原始碼 原始碼完全開源一同打包封裝APP原始碼封裝APP
- 爬蟲驗證碼的幾種處理方式,已封裝成類,文章末尾有原始碼!爬蟲封裝原始碼
- php用curl封裝一個http請求類(鏈式呼叫)PHP封裝HTTP
- Weex-iOS原始碼-CSS Layout 分析iOS原始碼CSS
- 一次Android動畫工具類的封裝Android動畫封裝
- 程式碼改變世界 | 如何封裝一個簡單的 Koa封裝
- AFN3.0封裝一層工具類封裝
- cocos2dx 很好的原始碼分析博文原始碼
- 一個基礎功能的視訊播放器(XXVideoPlayer)-AVPlayer封裝播放器IDE封裝
- 8.7 一個模組的封裝過程封裝
- Android 封裝一個通用的PopupWindowAndroid封裝