From 109a86e19f84c5bdb09d9605619dc313283acaa2 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Sat, 4 Nov 2017 15:38:41 +0800 Subject: [PATCH] =?UTF-8?q?=E9=94=9A=E7=82=B9=E5=90=8D=E7=A7=B0=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E6=94=AF=E7=82=B9=EF=BC=9B=E4=BF=AE=E5=A4=8D=E4=BA=86?= =?UTF-8?q?=E9=83=A8=E5=88=86bug=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConsoleDemo/main.cpp | 9 +- Demo/main.cpp | 8 +- Easy2D/{Node => Common}/EFont.cpp | 0 Easy2D/{Base => Common}/EObject.cpp | 0 Easy2D/{Node => Common}/ETexture.cpp | 0 Easy2D/Easy2D.vcxproj | 7 +- Easy2D/Easy2D.vcxproj.filters | 19 +- Easy2D/Node/ENode.cpp | 44 ++--- Easy2D/easy2d.h | 1 + Easy2D/ebase.h | 21 -- Easy2D/ecommon.h | 280 +++++++++++++++------------ Easy2D/enodes.h | 165 ++-------------- Easy2D/etypedef.h | 137 +++++++++++++ 13 files changed, 352 insertions(+), 339 deletions(-) rename Easy2D/{Node => Common}/EFont.cpp (100%) rename Easy2D/{Base => Common}/EObject.cpp (100%) rename Easy2D/{Node => Common}/ETexture.cpp (100%) create mode 100644 Easy2D/etypedef.h diff --git a/ConsoleDemo/main.cpp b/ConsoleDemo/main.cpp index 1babfc78..c1fe64c5 100644 --- a/ConsoleDemo/main.cpp +++ b/ConsoleDemo/main.cpp @@ -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); diff --git a/Demo/main.cpp b/Demo/main.cpp index b564cd24..af192623 100644 --- a/Demo/main.cpp +++ b/Demo/main.cpp @@ -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()) diff --git a/Easy2D/Node/EFont.cpp b/Easy2D/Common/EFont.cpp similarity index 100% rename from Easy2D/Node/EFont.cpp rename to Easy2D/Common/EFont.cpp diff --git a/Easy2D/Base/EObject.cpp b/Easy2D/Common/EObject.cpp similarity index 100% rename from Easy2D/Base/EObject.cpp rename to Easy2D/Common/EObject.cpp diff --git a/Easy2D/Node/ETexture.cpp b/Easy2D/Common/ETexture.cpp similarity index 100% rename from Easy2D/Node/ETexture.cpp rename to Easy2D/Common/ETexture.cpp diff --git a/Easy2D/Easy2D.vcxproj b/Easy2D/Easy2D.vcxproj index 352074d5..2415a343 100644 --- a/Easy2D/Easy2D.vcxproj +++ b/Easy2D/Easy2D.vcxproj @@ -208,10 +208,12 @@ - + + + @@ -237,8 +239,6 @@ - - @@ -262,6 +262,7 @@ + diff --git a/Easy2D/Easy2D.vcxproj.filters b/Easy2D/Easy2D.vcxproj.filters index ed54ad4e..4cd196cf 100644 --- a/Easy2D/Easy2D.vcxproj.filters +++ b/Easy2D/Easy2D.vcxproj.filters @@ -36,9 +36,6 @@ Win - - Base - Base @@ -54,12 +51,6 @@ Node - - Node - - - Node - Action @@ -210,6 +201,15 @@ Listener + + Common + + + Common + + + Common + @@ -226,5 +226,6 @@ + \ No newline at end of file diff --git a/Easy2D/Node/ENode.cpp b/Easy2D/Node/ENode.cpp index da267a46..46857eac 100644 --- a/Easy2D/Node/ENode.cpp +++ b/Easy2D/Node/ENode.cpp @@ -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; } diff --git a/Easy2D/easy2d.h b/Easy2D/easy2d.h index 223b2079..2d4a73f2 100644 --- a/Easy2D/easy2d.h +++ b/Easy2D/easy2d.h @@ -17,6 +17,7 @@ #include "emacros.h" +#include "etypedef.h" #include "ecommon.h" #include "ebase.h" #include "emanagers.h" diff --git a/Easy2D/ebase.h b/Easy2D/ebase.h index 1e6506b5..0138f484 100644 --- a/Easy2D/ebase.h +++ b/Easy2D/ebase.h @@ -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 { diff --git a/Easy2D/ecommon.h b/Easy2D/ecommon.h index d0aa5eae..6177b88b 100644 --- a/Easy2D/ecommon.h +++ b/Easy2D/ecommon.h @@ -1,115 +1,10 @@ #pragma once -#include -#include -#include -#include +#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 -using EVector = std::vector; - class EColor { @@ -412,28 +307,167 @@ public: }; -// 定时器回调函数(参数为该定时器被调用的次数,从 0 开始) -typedef std::function TIMER_CALLBACK; +class EObjectManager; -// 按钮点击回调函数 -typedef std::function BUTTON_CLICK_CALLBACK; +class EObject +{ + friend EObjectManager; -// 按键消息监听回调函数 -typedef std::function KEY_LISTENER_CALLBACK; +public: + EObject(); -// 鼠标消息监听回调函数 -typedef std::function MOUSE_LISTENER_CALLBACK; + virtual ~EObject(); -// 鼠标点击消息监听回调函数(参数为点击位置) -typedef std::function 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 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; +}; } \ No newline at end of file diff --git a/Easy2D/enodes.h b/Easy2D/enodes.h index 2c3855b4..0fe9cb64 100644 --- a/Easy2D/enodes.h +++ b/Easy2D/enodes.h @@ -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 { diff --git a/Easy2D/etypedef.h b/Easy2D/etypedef.h new file mode 100644 index 00000000..091fb6e7 --- /dev/null +++ b/Easy2D/etypedef.h @@ -0,0 +1,137 @@ +#pragma once +#include +#include +#include +#include + +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 +using EVector = std::vector; + +// 定时器回调函数(参数为该定时器被调用的次数,从 0 开始) +typedef std::function TIMER_CALLBACK; + +// 按钮点击回调函数 +typedef std::function BUTTON_CLICK_CALLBACK; + +// 按键消息监听回调函数 +typedef std::function KEY_LISTENER_CALLBACK; + +// 鼠标消息监听回调函数 +typedef std::function MOUSE_LISTENER_CALLBACK; + +// 鼠标点击消息监听回调函数(参数为点击位置) +typedef std::function 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 MOUSE_DRAG_LISTENER_CALLBACK; + +} \ No newline at end of file