锚点名称改为支点;修复了部分bug。

This commit is contained in:
Nomango 2017-11-04 15:38:41 +08:00
parent 55e1846d68
commit 109a86e19f
13 changed files with 352 additions and 339 deletions

View File

@ -6,20 +6,19 @@ int main()
if (app.init(L"Easy2D Demo", 640, 640))
{
app.showConsole();
auto scene = new EScene();
auto sprite = new ESprite(L"test2.png");
sprite->setAnchor(-1, 0);
auto sprite = new ESprite(L"haha.png");
// 获取窗口宽度
float width = EApp::getWidth();
// 获取窗口高度
float height = EApp::getHeight();
// 移动精灵的位置
sprite->setPos(width / 2, height / 2);
//sprite->setAnchor(0, 0);
sprite->setPos(width / 2 + sprite->getWidth(), height / 2);
scene->add(sprite);
sprite->runAction(new EActionLoop(new EActionRotateBy(1, 60)));
scene->getRoot()->runAction(new EActionLoop(new EActionRotateBy(1, 60)));
app.enterScene(scene);

View File

@ -9,7 +9,6 @@ int WINAPI WinMain(
)
{
EApp app;
if (app.init(L"Easy2D Demo", 640, 480))
{
float w = EApp::getWidth();
@ -21,7 +20,6 @@ int WINAPI WinMain(
//text->setWordWrapping(true);
//text->setWordWrappingWidth(130);
text->setRotation(40);
text->setAnchor(0.5f, 0.5f);
//text->runAction(new EActionLoop(new EActionTwo(new EActionFadeOut(1), new EActionFadeIn(1))));
scene->add(text);
@ -41,14 +39,14 @@ int WINAPI WinMain(
animation->addFrame(new ESpriteFrame(L"atlas.png", 61, 982, 34, 24));
animation->addFrame(new ESpriteFrame(L"atlas.png", 117, 982, 34, 24));
bird->runAction(new EActionLoop(animation));
bird->setAnchor(0.5f, 0.5f);
bird->setPivot(0.5f, 0.5f);
bird->setPos(EApp::getWidth() / 2, EApp::getHeight() / 2);
scene2->add(bird);
auto btnStart = new ESprite(L"atlas.png", 702, 234, 116, 70);
btnStart->setAnchor(0.5f, 0.5f);
btnStart->setPivot(0.5f, 0.5f);
auto btnStartSelected = new ESprite(L"atlas.png", 702, 234, 116, 70);
btnStartSelected->setAnchor(0.5f, 0.5f);
btnStartSelected->setPivot(0.5f, 0.5f);
btnStartSelected->setPosY(5);
auto button = new EButton(btnStart, btnStartSelected, [=] {
if (EApp::isPaused())

View File

@ -208,10 +208,12 @@
<ClCompile Include="Action\EActionTwoAtSameTime.cpp" />
<ClCompile Include="Action\EActionGradual.cpp" />
<ClCompile Include="Base\EApp.cpp" />
<ClCompile Include="Base\EObject.cpp" />
<ClCompile Include="Base\EScene.cpp" />
<ClCompile Include="Common\EFont.cpp" />
<ClCompile Include="Common\EKeyboardMsg.cpp" />
<ClCompile Include="Common\EMouseMsg.cpp" />
<ClCompile Include="Common\EObject.cpp" />
<ClCompile Include="Common\ETexture.cpp" />
<ClCompile Include="Geometry\ECircle.cpp" />
<ClCompile Include="Geometry\EEllipse.cpp" />
<ClCompile Include="Geometry\EGeometry.cpp" />
@ -237,8 +239,6 @@
<ClCompile Include="Node\ESprite.cpp" />
<ClCompile Include="Node\ESpriteFrame.cpp" />
<ClCompile Include="Node\EText.cpp" />
<ClCompile Include="Node\EFont.cpp" />
<ClCompile Include="Node\ETexture.cpp" />
<ClCompile Include="Tool\EFileUtils.cpp" />
<ClCompile Include="Tool\EMusicUtils.cpp" />
<ClCompile Include="Tool\ERandom.cpp" />
@ -262,6 +262,7 @@
<ClInclude Include="enodes.h" />
<ClInclude Include="etools.h" />
<ClInclude Include="etransitions.h" />
<ClInclude Include="etypedef.h" />
<ClInclude Include="Win\winbase.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -36,9 +36,6 @@
<ClCompile Include="Win\winbase.cpp">
<Filter>Win</Filter>
</ClCompile>
<ClCompile Include="Base\EObject.cpp">
<Filter>Base</Filter>
</ClCompile>
<ClCompile Include="Base\EApp.cpp">
<Filter>Base</Filter>
</ClCompile>
@ -54,12 +51,6 @@
<ClCompile Include="Node\EText.cpp">
<Filter>Node</Filter>
</ClCompile>
<ClCompile Include="Node\EFont.cpp">
<Filter>Node</Filter>
</ClCompile>
<ClCompile Include="Node\ETexture.cpp">
<Filter>Node</Filter>
</ClCompile>
<ClCompile Include="Action\EAction.cpp">
<Filter>Action</Filter>
</ClCompile>
@ -210,6 +201,15 @@
<ClCompile Include="Listener\EListenerPhysicsContact.cpp">
<Filter>Listener</Filter>
</ClCompile>
<ClCompile Include="Common\EFont.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="Common\EObject.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="Common\ETexture.cpp">
<Filter>Common</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Win\winbase.h">
@ -226,5 +226,6 @@
<ClInclude Include="egeometry.h" />
<ClInclude Include="elisteners.h" />
<ClInclude Include="emanagers.h" />
<ClInclude Include="etypedef.h" />
</ItemGroup>
</Project>

View File

@ -15,8 +15,8 @@ e2d::ENode::ENode()
, m_fSkewAngleY(0)
, m_fDisplayOpacity(1.0f)
, m_fRealOpacity(1.0f)
, m_fAnchorX(0.5f)
, m_fAnchorY(0.5f)
, m_fPivotX(0.5f)
, m_fPivotY(0.5f)
, m_Matri(D2D1::Matrix3x2F::Identity())
, m_bVisiable(true)
, m_bDisplayedInScene(false)
@ -172,27 +172,27 @@ void e2d::ENode::_sortChildren()
void e2d::ENode::_updateTransformToReal()
{
// 计算锚点坐标
D2D1_POINT_2F anchorPos = D2D1::Point2F(
getRealWidth() * m_fAnchorX,
getRealHeight() * m_fAnchorY
D2D1_POINT_2F pivot = D2D1::Point2F(
getRealWidth() * m_fPivotX,
getRealHeight() * m_fPivotY
);
// 计算左上角坐标
D2D1_POINT_2F upperLeftCorner = D2D1::Point2F(
m_Pos.x - getRealWidth() * m_fAnchorX,
m_Pos.y - getRealHeight() * m_fAnchorY
m_Pos.x - getRealWidth() * m_fPivotX,
m_Pos.y - getRealHeight() * m_fPivotY
);
// 二维矩形变换
m_Matri = D2D1::Matrix3x2F::Scale(
m_fScaleX,
m_fScaleY,
anchorPos
pivot
) * D2D1::Matrix3x2F::Skew(
m_fSkewAngleX,
m_fSkewAngleY,
anchorPos
pivot
) * D2D1::Matrix3x2F::Rotation(
m_fRotation,
anchorPos
pivot
) * D2D1::Matrix3x2F::Translation(
upperLeftCorner.x,
upperLeftCorner.y
@ -299,14 +299,14 @@ e2d::ESize e2d::ENode::getRealSize() const
return m_Size;
}
float e2d::ENode::getAnchorX() const
float e2d::ENode::getPivotX() const
{
return m_fAnchorX;
return m_fPivotX;
}
float e2d::ENode::getAnchorY() const
float e2d::ENode::getPivotY() const
{
return m_fAnchorY;
return m_fPivotY;
}
e2d::ESize e2d::ENode::getSize() const
@ -488,23 +488,23 @@ void e2d::ENode::setOpacity(float opacity)
_updateOpacity(this);
}
void e2d::ENode::setAnchorX(float anchorX)
void e2d::ENode::setPivotX(float pivotX)
{
this->setAnchor(anchorX, m_fAnchorY);
this->setPivot(pivotX, m_fPivotY);
}
void e2d::ENode::setAnchorY(float anchorY)
void e2d::ENode::setPivotY(float pivotY)
{
this->setAnchor(m_fAnchorX, anchorY);
this->setPivot(m_fPivotX, pivotY);
}
void e2d::ENode::setAnchor(float anchorX, float anchorY)
void e2d::ENode::setPivot(float pivotX, float pivotY)
{
if (m_fAnchorX == anchorX && m_fAnchorY == anchorY)
if (m_fPivotX == pivotX && m_fPivotY == pivotY)
return;
m_fAnchorX = anchorX;
m_fAnchorY = anchorY;
m_fPivotX = min(max(pivotX, 0), 1);
m_fPivotY = min(max(pivotY, 0), 1);
m_bTransformChildrenNeeded = true;
}

View File

@ -17,6 +17,7 @@
#include "emacros.h"
#include "etypedef.h"
#include "ecommon.h"
#include "ebase.h"
#include "emanagers.h"

View File

@ -192,27 +192,6 @@ protected:
};
class EObject
{
friend EObjectManager;
public:
EObject();
virtual ~EObject();
// 引用计数加一
void retain();
// 引用计数减一
void release();
private:
int m_nRefCount;
bool m_bManaged;
};
class EScene :
public EObject
{

View File

@ -1,115 +1,10 @@
#pragma once
#include <Windows.h>
#include <string>
#include <vector>
#include <functional>
#include "emacros.h"
#include "etypedef.h"
namespace e2d
{
struct EWindowStyle
{
LPCTSTR m_pIconID; /* 程序图标 ID */
bool m_bNoClose; /* 禁用关闭按钮 */
bool m_bNoMiniSize; /* 禁用最小化按钮 */
bool m_bTopMost; /* 窗口置顶 */
EWindowStyle()
{
m_pIconID = 0;
m_bNoClose = false;
m_bNoMiniSize = false;
m_bTopMost = false;
}
EWindowStyle(
LPCTSTR pIconID
)
{
m_pIconID = pIconID;
m_bNoClose = false;
m_bNoMiniSize = false;
m_bTopMost = false;
}
EWindowStyle(
LPCTSTR pIconID,
bool bNoClose,
bool bNoMiniSize,
bool bTopMost
)
{
m_pIconID = pIconID;
m_bNoClose = bNoClose;
m_bNoMiniSize = bNoMiniSize;
m_bTopMost = bTopMost;
}
};
struct EPoint
{
float x;
float y;
EPoint()
{
x = 0;
y = 0;
}
EPoint(float x, float y)
{
this->x = x;
this->y = y;
}
EPoint operator + (EPoint const & p)
{
return EPoint(x + p.x, y + p.y);
}
EPoint operator - (EPoint const & p)
{
return EPoint(x - p.x, y - p.y);
}
};
typedef EPoint EVec;
struct ESize
{
float width;
float height;
ESize()
{
width = 0;
height = 0;
}
ESize(float width, float height)
{
this->width = width;
this->height = height;
}
ESize operator + (ESize const & size)
{
return ESize(width + size.width, height + size.height);
}
ESize operator - (ESize const & size)
{
return ESize(width - size.width, height - size.height);
}
};
typedef std::wstring EString;
template<typename T>
using EVector = std::vector<T>;
class EColor
{
@ -412,28 +307,167 @@ public:
};
// 定时器回调函数(参数为该定时器被调用的次数,从 0 开始)
typedef std::function<void(int)> TIMER_CALLBACK;
class EObjectManager;
// 按钮点击回调函数
typedef std::function<void()> BUTTON_CLICK_CALLBACK;
class EObject
{
friend EObjectManager;
// 按键消息监听回调函数
typedef std::function<void()> KEY_LISTENER_CALLBACK;
public:
EObject();
// 鼠标消息监听回调函数
typedef std::function<void()> MOUSE_LISTENER_CALLBACK;
virtual ~EObject();
// 鼠标点击消息监听回调函数(参数为点击位置)
typedef std::function<void(EPoint mousePos)> MOUSE_CLICK_LISTENER_CALLBACK;
// 引用计数加一
void retain();
// 鼠标按下消息监听回调函数(参数为按下位置)
typedef MOUSE_CLICK_LISTENER_CALLBACK MOUSE_PRESS_LISTENER_CALLBACK;
// 引用计数减一
void release();
// 鼠标双击消息监听回调函数(参数为双击位置)
typedef MOUSE_CLICK_LISTENER_CALLBACK MOUSE_DBLCLK_LISTENER_CALLBACK;
private:
int m_nRefCount;
bool m_bManaged;
};
// 鼠标拖动消息监听回调函数(参数为拖动前位置和拖动后位置)
typedef std::function<void(EPoint begin, EPoint end)> MOUSE_DRAG_LISTENER_CALLBACK;
class EText;
class EFont :
public EObject
{
friend EText;
public:
EFont();
EFont(
EString fontFamily,
float fontSize = 22,
UINT32 color = EColor::WHITE,
UINT32 fontWeight = EFontWeight::REGULAR,
bool italic = false
);
virtual ~EFont();
// 获取当前字号
float getFontSize() const;
// 获取当前字体粗细值
UINT32 getFontWeight() const;
// 获取文字颜色
UINT32 getColor() const;
// 是否是斜体
bool isItalic() const;
// 设置字体
void setFamily(
EString fontFamily
);
// 设置字号
void setSize(
float fontSize
);
// 设置字体粗细值
void setWeight(
UINT32 fontWeight
);
// 设置文字颜色
void setColor(
UINT32 color
);
// 设置文字斜体
void setItalic(
bool value
);
protected:
// 创建文字格式
void _initTextFormat();
// 获取文字格式
IDWriteTextFormat * _getTextFormat();
protected:
EString m_sFontFamily;
float m_fFontSize;
UINT32 m_FontWeight;
UINT32 m_Color;
bool m_bItalic;
bool m_bRecreateNeeded;
IDWriteTextFormat * m_pTextFormat;
};
class ESprite;
class ETexture :
public EObject
{
friend ESprite;
public:
// 创建一个空的纹理
ETexture();
// 从本地文件中读取资源
ETexture(
const EString & fileName
);
// 读取程序资源
ETexture(
LPCTSTR resourceName,
LPCTSTR resourceType
);
virtual ~ETexture();
// 从本地文件中读取资源
void loadFromFile(
const EString & fileName
);
// 读取程序资源
void loadFromResource(
LPCTSTR resourceName,
LPCTSTR resourceType
);
// 获取源图片宽度
virtual float getSourceWidth() const;
// 获取源图片高度
virtual float getSourceHeight() const;
// 获取源图片大小
virtual ESize getSourceSize() const;
// 预加载资源
static bool preload(
const EString & fileName
);
// 预加载资源
static bool preload(
LPCTSTR resourceName,
LPCTSTR resourceType
);
// 清空缓存
static void clearCache();
protected:
ID2D1Bitmap * _getBitmap();
protected:
ID2D1Bitmap * m_pBitmap;
};
}

View File

@ -66,10 +66,10 @@ public:
virtual ESize getRealSize() const;
// 获取节点的锚点
virtual float getAnchorX() const;
virtual float getPivotX() const;
// 获取节点的锚点
virtual float getAnchorY() const;
virtual float getPivotY() const;
// 获取节点大小
virtual ESize getSize() const;
@ -223,23 +223,23 @@ public:
float opacity
);
// 设置纵向锚点
// 设置支点的横向位置
// 默认为 0.5f, 范围 [0, 1]
virtual void setAnchorX(
float anchorX
virtual void setPivotX(
float pivotX
);
// 设置横向锚点
// 设置支点的纵向位置
// 默认为 0.5f, 范围 [0, 1]
virtual void setAnchorY(
float anchorY
virtual void setPivotY(
float pivotY
);
// 设置锚点
// 设置支点位置
// 默认为 (0.5f, 0.5f), 范围 [0, 1]
virtual void setAnchor(
float anchorX,
float anchorY
virtual void setPivot(
float pivotX,
float pivotY
);
// 设置节点形状
@ -375,8 +375,8 @@ protected:
float m_fSkewAngleY;
float m_fDisplayOpacity;
float m_fRealOpacity;
float m_fAnchorX;
float m_fAnchorY;
float m_fPivotX;
float m_fPivotY;
int m_nOrder;
bool m_bVisiable;
bool m_bDisplayedInScene;
@ -390,70 +390,6 @@ protected:
};
class ETexture :
public EObject
{
friend ESprite;
public:
// 创建一个空的纹理
ETexture();
// 从本地文件中读取资源
ETexture(
const EString & fileName
);
// 读取程序资源
ETexture(
LPCTSTR resourceName,
LPCTSTR resourceType
);
virtual ~ETexture();
// 从本地文件中读取资源
void loadFromFile(
const EString & fileName
);
// 读取程序资源
void loadFromResource(
LPCTSTR resourceName,
LPCTSTR resourceType
);
// 获取源图片宽度
virtual float getSourceWidth() const;
// 获取源图片高度
virtual float getSourceHeight() const;
// 获取源图片大小
virtual ESize getSourceSize() const;
// 预加载资源
static bool preload(
const EString & fileName
);
// 预加载资源
static bool preload(
LPCTSTR resourceName,
LPCTSTR resourceType
);
// 清空缓存
static void clearCache();
protected:
ID2D1Bitmap * _getBitmap();
protected:
ID2D1Bitmap * m_pBitmap;
};
class ESpriteFrame :
public EObject
{
@ -639,79 +575,6 @@ protected:
};
class EFont :
public EObject
{
friend EText;
public:
EFont();
EFont(
EString fontFamily,
float fontSize = 22,
UINT32 color = EColor::WHITE,
UINT32 fontWeight = EFontWeight::REGULAR,
bool italic = false
);
virtual ~EFont();
// 获取当前字号
float getFontSize() const;
// 获取当前字体粗细值
UINT32 getFontWeight() const;
// 获取文字颜色
UINT32 getColor() const;
// 是否是斜体
bool isItalic() const;
// 设置字体
void setFamily(
EString fontFamily
);
// 设置字号
void setSize(
float fontSize
);
// 设置字体粗细值
void setWeight(
UINT32 fontWeight
);
// 设置文字颜色
void setColor(
UINT32 color
);
// 设置文字斜体
void setItalic(
bool value
);
protected:
// 创建文字格式
void _initTextFormat();
// 获取文字格式
IDWriteTextFormat * _getTextFormat();
protected:
EString m_sFontFamily;
float m_fFontSize;
UINT32 m_FontWeight;
UINT32 m_Color;
bool m_bItalic;
bool m_bRecreateNeeded;
IDWriteTextFormat * m_pTextFormat;
};
class EText :
public ENode
{

137
Easy2D/etypedef.h Normal file
View File

@ -0,0 +1,137 @@
#pragma once
#include <Windows.h>
#include <string>
#include <vector>
#include <functional>
namespace e2d
{
struct EPoint
{
float x;
float y;
EPoint()
{
x = 0;
y = 0;
}
EPoint(float x, float y)
{
this->x = x;
this->y = y;
}
EPoint operator + (EPoint const & p)
{
return EPoint(x + p.x, y + p.y);
}
EPoint operator - (EPoint const & p)
{
return EPoint(x - p.x, y - p.y);
}
};
struct ESize
{
float width;
float height;
ESize()
{
width = 0;
height = 0;
}
ESize(float width, float height)
{
this->width = width;
this->height = height;
}
ESize operator + (ESize const & size)
{
return ESize(width + size.width, height + size.height);
}
ESize operator - (ESize const & size)
{
return ESize(width - size.width, height - size.height);
}
};
struct EWindowStyle
{
LPCTSTR m_pIconID; /* 程序图标 ID */
bool m_bNoClose; /* 禁用关闭按钮 */
bool m_bNoMiniSize; /* 禁用最小化按钮 */
bool m_bTopMost; /* 窗口置顶 */
EWindowStyle()
{
m_pIconID = 0;
m_bNoClose = false;
m_bNoMiniSize = false;
m_bTopMost = false;
}
EWindowStyle(
LPCTSTR pIconID
)
{
m_pIconID = pIconID;
m_bNoClose = false;
m_bNoMiniSize = false;
m_bTopMost = false;
}
EWindowStyle(
LPCTSTR pIconID,
bool bNoClose,
bool bNoMiniSize,
bool bTopMost
)
{
m_pIconID = pIconID;
m_bNoClose = bNoClose;
m_bNoMiniSize = bNoMiniSize;
m_bTopMost = bTopMost;
}
};
typedef EPoint EVec;
typedef std::wstring EString;
template<typename T>
using EVector = std::vector<T>;
// 定时器回调函数(参数为该定时器被调用的次数,从 0 开始)
typedef std::function<void(int)> TIMER_CALLBACK;
// 按钮点击回调函数
typedef std::function<void()> BUTTON_CLICK_CALLBACK;
// 按键消息监听回调函数
typedef std::function<void()> KEY_LISTENER_CALLBACK;
// 鼠标消息监听回调函数
typedef std::function<void()> MOUSE_LISTENER_CALLBACK;
// 鼠标点击消息监听回调函数(参数为点击位置)
typedef std::function<void(EPoint mousePos)> MOUSE_CLICK_LISTENER_CALLBACK;
// 鼠标按下消息监听回调函数(参数为按下位置)
typedef MOUSE_CLICK_LISTENER_CALLBACK MOUSE_PRESS_LISTENER_CALLBACK;
// 鼠标双击消息监听回调函数(参数为双击位置)
typedef MOUSE_CLICK_LISTENER_CALLBACK MOUSE_DBLCLK_LISTENER_CALLBACK;
// 鼠标拖动消息监听回调函数(参数为拖动前位置和拖动后位置)
typedef std::function<void(EPoint begin, EPoint end)> MOUSE_DRAG_LISTENER_CALLBACK;
}