diff --git a/core/Action/ActionSequence.cpp b/core/Action/ActionSequence.cpp index dccdbc8f..84ee4a97 100644 --- a/core/Action/ActionSequence.cpp +++ b/core/Action/ActionSequence.cpp @@ -5,7 +5,7 @@ e2d::ActionSequence::ActionSequence() : { } -e2d::ActionSequence::ActionSequence(std::initializer_list& vActions) : +e2d::ActionSequence::ActionSequence(const std::initializer_list& vActions) : m_nActionIndex(0) { this->add(vActions); @@ -82,7 +82,7 @@ void e2d::ActionSequence::add(Action * action) } } -void e2d::ActionSequence::add(std::initializer_list& vActions) +void e2d::ActionSequence::add(const std::initializer_list& vActions) { for (const auto &action : vActions) { diff --git a/core/Action/Animation.cpp b/core/Action/Animation.cpp index 425facef..7529bc3f 100644 --- a/core/Action/Animation.cpp +++ b/core/Action/Animation.cpp @@ -12,7 +12,7 @@ e2d::Animation::Animation(double interval) { } -e2d::Animation::Animation(std::initializer_list& vImages) +e2d::Animation::Animation(const std::initializer_list& vImages) { this->add(vImages); } @@ -77,7 +77,7 @@ void e2d::Animation::add(Image * frame) } } -void e2d::Animation::add(std::initializer_list& vImages) +void e2d::Animation::add(const std::initializer_list& vImages) { for (const auto &image : vImages) { diff --git a/core/Base/Input.cpp b/core/Base/Input.cpp index 9e5ac035..6214cf58 100644 --- a/core/Base/Input.cpp +++ b/core/Base/Input.cpp @@ -142,23 +142,25 @@ void Input::__updateDeviceState() ScreenToClient(Window::getHWnd(), &s_MousePosition); } -bool Input::isKeyDown(int nKeyCode) +bool Input::isKeyDown(KeyCode nKeyCode) { - if (s_KeyBuffer[nKeyCode] & 0x80) + if (s_KeyBuffer[static_cast(nKeyCode)] & 0x80) return true; return false; } -bool Input::isKeyPress(int nKeyCode) +bool Input::isKeyPress(KeyCode nKeyCode) { - if ((s_KeyBuffer[nKeyCode] & 0x80) && !(s_KeyRecordBuffer[nKeyCode] & 0x80)) + if ((s_KeyBuffer[static_cast(nKeyCode)] & 0x80) && + !(s_KeyRecordBuffer[static_cast(nKeyCode)] & 0x80)) return true; return false; } -bool Input::isKeyRelease(int nKeyCode) +bool Input::isKeyRelease(KeyCode nKeyCode) { - if (!(s_KeyBuffer[nKeyCode] & 0x80) && (s_KeyRecordBuffer[nKeyCode] & 0x80)) + if (!(s_KeyBuffer[static_cast(nKeyCode)] & 0x80) && + (s_KeyRecordBuffer[static_cast(nKeyCode)] & 0x80)) return true; return false; } diff --git a/core/Manager/CollisionManager.cpp b/core/Manager/CollisionManager.cpp index 204586ff..3c77c152 100644 --- a/core/Manager/CollisionManager.cpp +++ b/core/Manager/CollisionManager.cpp @@ -9,6 +9,9 @@ static std::vector s_vShapes; static std::vector s_vListeners; // 碰撞触发状态 static bool s_bCollisionEnable = false; +// 发生碰撞的节点 +static e2d::Node * s_pActiveNode = nullptr; +static e2d::Node * s_pPassiveNode = nullptr; void e2d::CollisionManager::setEnable(bool bEnable) @@ -18,6 +21,9 @@ void e2d::CollisionManager::setEnable(bool bEnable) void e2d::CollisionManager::__update() { + if (s_vListeners.size() == 0) + return; + for (size_t i = 0; i < s_vListeners.size(); i++) { auto pListener = s_vListeners[i]; @@ -72,19 +78,23 @@ void e2d::CollisionManager::__updateShape(e2d::Shape * pActiveShape) if (IsCollideWith(pActiveNode, pPassiveNode->getHashName())) { // 判断两形状交集情况 - int relation = pActiveShape->getRelationWith(pPassiveShape); + Relation relation = pActiveShape->getRelationWith(pPassiveShape); // 忽略 UNKNOWN 和 DISJOINT 情况 if (relation != Relation::UNKNOWN && relation != Relation::DISJOINT) { - pActiveNode->onCollide(pPassiveNode, relation); - pPassiveNode->onCollide(pActiveNode, pPassiveShape->getRelationWith(pActiveShape)); - pCurrentScene->onCollide(pActiveNode, pPassiveNode); + s_pActiveNode = pActiveNode; + s_pPassiveNode = pPassiveNode; + pActiveNode->onCollide(pPassiveNode); + pPassiveNode->onCollide(pActiveNode); + pCurrentScene->onCollide(); CollisionManager::__update(); } } } } } + s_pActiveNode = nullptr; + s_pPassiveNode = nullptr; } void e2d::CollisionManager::__add(CollisionListener * pListener) @@ -196,6 +206,16 @@ std::vector e2d::CollisionManager::getAll() return s_vListeners; } +e2d::Node * e2d::CollisionManager::getNode1() +{ + return s_pActiveNode; +} + +e2d::Node * e2d::CollisionManager::getNode2() +{ + return s_pPassiveNode; +} + void e2d::CollisionManager::__addShape(Shape * pShape) { if (pShape) diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index e0dd7d53..91f8e80f 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -602,7 +602,7 @@ void e2d::Node::addCollider(String collliderName) m_vColliders.insert(hash); } -void e2d::Node::addCollider(std::initializer_list& vCollliderName) +void e2d::Node::addCollider(const std::initializer_list& vCollliderName) { for (const auto &name : vCollliderName) { @@ -656,7 +656,7 @@ void e2d::Node::addChild(Node * child, int order /* = 0 */) } } -void e2d::Node::addChild(std::initializer_list& vNodes, int order) +void e2d::Node::addChild(const std::initializer_list& vNodes, int order) { for (const auto &node : vNodes) { @@ -932,7 +932,7 @@ bool e2d::Node::isIntersectWith(const Node * pNode) const // 如果存在形状,用形状判断 if (this->m_pShape && pNode->m_pShape) { - int relation = this->m_pShape->getRelationWith(pNode->m_pShape); + Relation relation = this->m_pShape->getRelationWith(pNode->m_pShape); if ((relation != Relation::UNKNOWN) && (relation != Relation::DISJOINT)) { diff --git a/core/Node/Text.cpp b/core/Node/Text.cpp index 5515b3a0..224c7854 100644 --- a/core/Node/Text.cpp +++ b/core/Node/Text.cpp @@ -164,7 +164,7 @@ void e2d::Text::setLineSpacing(double fLineSpacing) } } -void e2d::Text::setAlignment(UINT32 nAlign) +void e2d::Text::setAlignment(TextAlign nAlign) { if (m_nAlign != nAlign) { diff --git a/core/Shape/Shape.cpp b/core/Shape/Shape.cpp index 1df9c657..3016a8a8 100644 --- a/core/Shape/Shape.cpp +++ b/core/Shape/Shape.cpp @@ -65,7 +65,7 @@ void e2d::Shape::_render() } } -int e2d::Shape::getRelationWith(Shape * pShape) const +e2d::Relation e2d::Shape::getRelationWith(Shape * pShape) const { if (m_pTransformedShape && pShape->m_pTransformedShape) { @@ -79,10 +79,10 @@ int e2d::Shape::getRelationWith(Shape * pShape) const &relation ); - return relation; + return Relation(relation); } } - return 0; + return Relation::UNKNOWN; } void e2d::Shape::_transform() diff --git a/core/eactions.h b/core/eactions.h index d38b6185..44eec7a0 100644 --- a/core/eactions.h +++ b/core/eactions.h @@ -402,7 +402,7 @@ public: // 创建顺序动作 ActionSequence( - std::initializer_list& vActions /* 动作数组 */ + const std::initializer_list& vActions /* 动作数组 */ ); virtual ~ActionSequence(); @@ -414,7 +414,7 @@ public: // 在结尾添加多个动作 void add( - std::initializer_list& vActions /* 动作数组 */ + const std::initializer_list& vActions /* 动作数组 */ ); // 获取该动作的拷贝对象 @@ -517,7 +517,7 @@ public: // 创建帧动画 Animation( - std::initializer_list& vImages + const std::initializer_list& vImages ); virtual ~Animation(); @@ -529,7 +529,7 @@ public: // 添加多个关键帧 void add( - std::initializer_list& vImages /* 关键帧数组 */ + const std::initializer_list& vImages /* 关键帧数组 */ ); // 设置每一帧的时间间隔 @@ -670,7 +670,7 @@ namespace e2d // 创建顺序动作 ActionSequence* Sequence( - std::initializer_list& vActions /* 动作数组 */ + const std::initializer_list& vActions /* 动作数组 */ ); // 创建延时动作 @@ -687,7 +687,7 @@ namespace e2d // 创建特定帧间隔的帧动画 Animation* Animate( double interval, /* 帧间隔(秒) */ - std::initializer_list& vFrames /* 关键帧数组 */ + const std::initializer_list& vFrames /* 关键帧数组 */ ); // 创建执行函数对象的动作 @@ -761,7 +761,7 @@ namespace e2d return new (std::nothrow) ActionTwo(pActionFirst, pActionSecond, bAtSameTime); } - inline e2d::ActionSequence * e2d::action::Sequence(std::initializer_list& vActions) + inline e2d::ActionSequence * e2d::action::Sequence(const std::initializer_list& vActions) { auto action = new (std::nothrow) ActionSequence(); if (action) @@ -781,7 +781,7 @@ namespace e2d return new (std::nothrow) ActionLoop(action, times); } - inline e2d::Animation * e2d::action::Animate(double interval, std::initializer_list& vFrames) + inline e2d::Animation * e2d::action::Animate(double interval, const std::initializer_list& vFrames) { auto animation = new (std::nothrow) Animation(interval); if (animation) diff --git a/core/ebase.h b/core/ebase.h index 35d42cd4..39325b4d 100644 --- a/core/ebase.h +++ b/core/ebase.h @@ -152,17 +152,17 @@ class Input public: // 检测键盘某按键是否正被按下 static bool isKeyDown( - int nKeyCode + KeyCode nKeyCode ); // 检测键盘某按键是否被点击 static bool isKeyPress( - int nKeyCode + KeyCode nKeyCode ); // 检测键盘某按键是否正在松开 static bool isKeyRelease( - int nKeyCode + KeyCode nKeyCode ); // 检测鼠标左键是否正被按下 diff --git a/core/ecommon.h b/core/ecommon.h index 58d9ff5a..c1b419de 100644 --- a/core/ecommon.h +++ b/core/ecommon.h @@ -195,7 +195,7 @@ private: class Color { public: - enum COMMON_VALUE + enum : UINT32 { ALICE_BLUE = 0xF0F8FF, AQUA = 0x00FFFF, @@ -262,11 +262,12 @@ public: }; }; + // 字体粗细值 class FontWeight { public: - enum COMMON_VALUE + enum : UINT32 { THIN = 100, EXTRA_LIGHT = 200, @@ -290,96 +291,84 @@ public: // 文本对齐方式 -class TextAlign +enum class TextAlign : int { -public: - enum COMMON_VALUE - { - LEFT, /* 左对齐 */ - RIGHT, /* 右对齐 */ - CENTER /* 居中对齐 */ - }; + LEFT, /* 左对齐 */ + RIGHT, /* 右对齐 */ + CENTER /* 居中对齐 */ }; // 键值集合 -class KeyCode +enum class KeyCode : int { -public: - enum VALUE - { - UP = 0xC8, - LEFT = 0xCB, - RIGHT = 0xCD, - DOWN = 0xD0, - ENTER = 0x1C, - SPACE = 0x39, - ESC = 0x01, - BACK = 0x0E, - TAB = 0x0F, - PAUSE = 0xC5, - Q = 0x10, - W = 0x11, - E = 0x12, - R = 0x13, - T = 0x14, - Y = 0x15, - U = 0x16, - I = 0x17, - O = 0x18, - P = 0x19, - A = 0x1E, - S = 0x1F, - D = 0x20, - F = 0x21, - G = 0x22, - H = 0x23, - J = 0x24, - K = 0x25, - L = 0x26, - Z = 0x2C, - X = 0x2D, - C = 0x2E, - V = 0x2F, - B = 0x30, - N = 0x31, - M = 0x32, - NUM1 = 0x02, - NUM2 = 0x03, - NUM3 = 0x04, - NUM4 = 0x05, - NUM5 = 0x06, - NUM6 = 0x07, - NUM7 = 0x08, - NUM8 = 0x09, - NUM9 = 0x0A, - NUM0 = 0x0B, - NUMPAD7 = 0x47, - NUMPAD8 = 0x48, - NUMPAD9 = 0x49, - NUMPAD4 = 0x4B, - NUMPAD5 = 0x4C, - NUMPAD6 = 0x4D, - NUMPAD1 = 0x4F, - NUMPAD2 = 0x50, - NUMPAD3 = 0x51, - NUMPAD0 = 0x52 - }; + UP = 0xC8, + LEFT = 0xCB, + RIGHT = 0xCD, + DOWN = 0xD0, + ENTER = 0x1C, + SPACE = 0x39, + ESC = 0x01, + BACK = 0x0E, + TAB = 0x0F, + PAUSE = 0xC5, + Q = 0x10, + W = 0x11, + E = 0x12, + R = 0x13, + T = 0x14, + Y = 0x15, + U = 0x16, + I = 0x17, + O = 0x18, + P = 0x19, + A = 0x1E, + S = 0x1F, + D = 0x20, + F = 0x21, + G = 0x22, + H = 0x23, + J = 0x24, + K = 0x25, + L = 0x26, + Z = 0x2C, + X = 0x2D, + C = 0x2E, + V = 0x2F, + B = 0x30, + N = 0x31, + M = 0x32, + NUM1 = 0x02, + NUM2 = 0x03, + NUM3 = 0x04, + NUM4 = 0x05, + NUM5 = 0x06, + NUM6 = 0x07, + NUM7 = 0x08, + NUM8 = 0x09, + NUM9 = 0x0A, + NUM0 = 0x0B, + NUMPAD7 = 0x47, + NUMPAD8 = 0x48, + NUMPAD9 = 0x49, + NUMPAD4 = 0x4B, + NUMPAD5 = 0x4C, + NUMPAD6 = 0x4D, + NUMPAD1 = 0x4F, + NUMPAD2 = 0x50, + NUMPAD3 = 0x51, + NUMPAD0 = 0x52 }; // 形状交集关系 -class Relation +enum class Relation : int { -public: - enum VALUE - { - UNKNOWN = 0, /* 关系不确定 */ - DISJOINT = 1, /* 没有交集 */ - IS_CONTAINED = 2, /* 完全被包含 */ - CONTAINS = 3, /* 完全包含 */ - OVERLAP = 4 /* 部分重叠 */ - }; + UNKNOWN = 0, /* 关系不确定 */ + DISJOINT = 1, /* 没有交集 */ + IS_CONTAINED = 2, /* 完全被包含 */ + CONTAINS = 3, /* 完全包含 */ + OVERLAP = 4 /* 部分重叠 */ }; @@ -541,12 +530,9 @@ public: virtual void onExit() {} // 重写这个函数,它将在碰撞发生时自动执行 - virtual void onCollide( - Node * pActiveNode, /* 主动发生碰撞的节点 */ - Node * pPassiveNode /* 被动发生碰撞的节点 */ - ) {} + virtual void onCollide() {} - // 重写这个函数,它将在关闭窗口时执行 + // 重写这个函数,它将在关闭窗口时执行(返回 false 将阻止窗口关闭) virtual bool onCloseWindow() { return true; } // 重写这个函数,它将在每一帧画面刷新时执行 @@ -603,7 +589,7 @@ class Shape : public: // 形状类别 - enum TYPE + enum class TYPE : int { RECTANGLE, /* 矩形 */ CIRCLE, /* 圆形 */ @@ -616,7 +602,7 @@ public: virtual ~Shape(); // 判断两形状的交集关系 - virtual int getRelationWith( + virtual Relation getRelationWith( Shape * pShape ) const; diff --git a/core/emanagers.h b/core/emanagers.h index 4b5ed438..7965a7a3 100644 --- a/core/emanagers.h +++ b/core/emanagers.h @@ -401,6 +401,12 @@ public: // 获取全部监听器 static std::vector getAll(); + // 获取发生碰撞的节点 1 + static Node* getNode1(); + + // 获取发生碰撞的节点 2 + static Node* getNode2(); + private: // 添加碰撞监听 static void __add( diff --git a/core/enodes.h b/core/enodes.h index aef41611..835ee864 100644 --- a/core/enodes.h +++ b/core/enodes.h @@ -39,8 +39,7 @@ public: // 碰撞处理 virtual void onCollide( - Node* pCollisionNode, /* 发生碰撞的节点 */ - int nRelation /* 碰撞关系,取值为 Relation::VALUE 中的一种 */ + Node* pNode /* 发生碰撞的节点 */ ) {} // 获取节点显示状态 @@ -328,7 +327,7 @@ public: // 添加多个可碰撞节点的名称 virtual void addCollider( - std::initializer_list& vCollliderName /* 名称数组 */ + const std::initializer_list& vCollliderName /* 名称数组 */ ); // 移除可碰撞节点的名称 @@ -344,7 +343,7 @@ public: // 添加多个子节点 virtual void addChild( - std::initializer_list& vNodes, /* 节点数组 */ + const std::initializer_list& vNodes, /* 节点数组 */ int order = 0 /* 渲染顺序 */ ); @@ -459,10 +458,10 @@ protected: Shape * m_pShape; Scene * m_pParentScene; Node * m_pParent; - D2D1::Matrix3x2F m_MatriInitial; - D2D1::Matrix3x2F m_MatriFinal; - std::set m_vColliders; - std::vector m_vChildren; + D2D1::Matrix3x2F m_MatriInitial; + D2D1::Matrix3x2F m_MatriFinal; + std::set m_vColliders; + std::vector m_vChildren; }; @@ -623,7 +622,7 @@ public: // 设置对齐方式(默认为 TextAlign::LEFT) void setAlignment( - UINT32 nAlign + TextAlign nAlign ); // 设置下划线(默认值为 false) @@ -654,8 +653,8 @@ protected: bool m_bWrappingEnable; float m_fWrappingWidth; Font m_Font; - UINT32 m_nAlign; float m_fLineSpacing; + TextAlign m_nAlign; IDWriteTextFormat * m_pDWriteTextFormat; IDWriteTextLayout * m_pDWriteTextLayout; };