diff --git a/ConsoleDemo/ConsoleDemo.vcxproj b/ConsoleDemo/ConsoleDemo.vcxproj
index 8811716c..5d8465c9 100644
--- a/ConsoleDemo/ConsoleDemo.vcxproj
+++ b/ConsoleDemo/ConsoleDemo.vcxproj
@@ -23,7 +23,7 @@
{70931955-FE2D-4A50-93C6-6955A730B0FE}
Win32Proj
ConsoleDemo
- 10.0.16299.0
+ 10.0.15063.0
diff --git a/ConsoleDemo/main.cpp b/ConsoleDemo/main.cpp
index dc9f6c7c..b3112a46 100644
--- a/ConsoleDemo/main.cpp
+++ b/ConsoleDemo/main.cpp
@@ -23,23 +23,23 @@ int main()
}
});*/
- auto listener = new EKeyPressListener([=] {
- if (EKeyMsg::isCapitalLockOn())
+ auto listener = new EKeyboardPressListener([=] {
+ if (EKeyboardMsg::isCapitalLockOn())
{
- if (EKeyMsg::getVal() == EKeyMsg::KEY::LEFT)
+ if (EKeyboardMsg::getVal() == EKeyboardMsg::KEY::LEFT)
{
node->move(-3, 0);
}
- if (EKeyMsg::getVal() == EKeyMsg::KEY::RIGHT)
+ if (EKeyboardMsg::getVal() == EKeyboardMsg::KEY::RIGHT)
{
node->move(3, 0);
}
}
});
- listener->bindWithNode(node);
+ listener->bindWith(node);
- EMsgManager::bindListenerWithScene(listener, scene);
+ EMsgManager::bindListener(listener, scene);
app.enterScene(scene);
diff --git a/Demo/Demo.vcxproj.user b/Demo/Demo.vcxproj.user
new file mode 100644
index 00000000..be250787
--- /dev/null
+++ b/Demo/Demo.vcxproj.user
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file
diff --git a/Demo/main.cpp b/Demo/main.cpp
index fc1cbffb..53fb42be 100644
--- a/Demo/main.cpp
+++ b/Demo/main.cpp
@@ -14,34 +14,20 @@ int WINAPI WinMain(
{
auto scene = new EScene();
- /*auto node = new ENode(L"node1");
- node->setPos(50, 80);
- node->setSize(30, 30);
- scene->add(node);
-
- auto node2 = new ENode(L"node2");
- node2->setPos(20, 20);
- node2->setSize(40, 40);
- node->addChild(node2);
-
- auto mlistener = new EMouseClickListener([](EPoint p) {
- EApp::getCurrentScene()->getChild(L"node1")->setPos(p.x, p.y);
- });
-
- mlistener->bindWith(node);*/
-
auto sprite = new ESprite(L"test.png");
sprite->setAnchor(0.5f, 0.5f);
- sprite->setPos(sprite->getWidth() / 2, sprite->getHeight() / 2);
+ sprite->setPos(EApp::getWidth() / 2, sprite->getHeight() / 2);
auto sprite2 = new ESprite(L"test.png");
+ sprite2->setPos(30, 0);
sprite2->setScale(0.5);
sprite2->setRotation(45);
sprite->addChild(sprite2);
- auto mlistener = new EMouseClickListener([=](EPoint p) {
- sprite->setRotation(sprite->getRotation() + 10);
+ auto mlistener = new EMouseDoubleClickListener([=](EPoint p) {
+ //sprite->setRotation(sprite->getRotation() + 10);
+ EApp::setWindowSize(640, 480);
});
mlistener->bindWith(sprite);
diff --git a/Easy2D/Base/EApp.cpp b/Easy2D/Base/EApp.cpp
index 90feea4b..18695bbc 100644
--- a/Easy2D/Base/EApp.cpp
+++ b/Easy2D/Base/EApp.cpp
@@ -2,29 +2,41 @@
#include "..\Win\winbase.h"
#include "..\emsg.h"
#include "..\etools.h"
+#include "..\enodes.h"
#include
-#include
#include
-using namespace std::chrono;
+#include
+#pragma comment (lib ,"imm32.lib")
+
using namespace std::this_thread;
+using namespace std::chrono;
+// ΨһʵÀýÖ¸Õë
e2d::EApp * s_pInstance = nullptr;
+// ³¡¾°Õ»
std::stack s_SceneStack;
e2d::EApp::EApp()
: m_bRunning(false)
+ , nAnimationInterval(17LL)
, m_ClearColor(EColor::Black)
, m_pCurrentScene(nullptr)
, m_pNextScene(nullptr)
{
+ ASSERT(s_pInstance == nullptr, "EApp instance already exists!");
s_pInstance = this; // ±£´æÊµÀý¶ÔÏó
+
+ CoInitialize(NULL);
}
e2d::EApp::~EApp()
{
- SafeReleaseInterface(&GetFactory());
+ // ÊÍ·Å×ÊÔ´
SafeReleaseInterface(&GetRenderTarget());
+ SafeReleaseInterface(&GetFactory());
+
+ CoUninitialize();
}
e2d::EApp * e2d::EApp::get()
@@ -33,58 +45,34 @@ e2d::EApp * e2d::EApp::get()
return s_pInstance; // »ñÈ¡ EApp µÄΨһʵÀý
}
-bool e2d::EApp::init(e2d::EString title, e2d::ESize size, bool bShowConsole /* = false */)
+bool e2d::EApp::init(const EString &title, UINT32 width, UINT32 height, bool showConsole /* = false */)
{
- return init(title, size.cx, size.cy, bShowConsole);
+ return init(title, width, height, WS_OVERLAPPEDWINDOW, showConsole);
}
-bool e2d::EApp::init(e2d::EString title, UINT32 width, UINT32 height, bool bShowConsole /* = false */)
+bool e2d::EApp::init(const EString &title, UINT32 width, UINT32 height, int windowStyle, bool showConsole /* = false */)
{
HRESULT hr;
- CoInitialize(NULL);
- // ¹Ø±Õ¿ØÖÆÌ¨
- if (bShowConsole)
- {
- // ²éÕÒÊÇ·ñÒѾ´æÔÚ¿ØÖÆÌ¨
- if (!GetConsoleWindow())
- {
- // ÏÔʾһ¸öпØÖÆÌ¨
- if (AllocConsole())
- {
- FILE *stream;
- freopen_s(&stream, "CONOUT$", "w+t", stdout);
- freopen_s(&stream, "CONOUT$", "w+t", stderr);
- freopen_s(&stream, "CONIN$", "r+t", stdin);
- }
- else
- {
- MessageBox(nullptr, L"Alloc Console Failed!", L"Error", MB_OK);
- }
- }
- }
- else
- {
- FreeConsole();
- }
+ // ÏÔʾ»ò¹Ø±Õ¿ØÖÆÌ¨
+ EApp::showConsole(showConsole);
- // ³õʼ»¯ device-indpendent ×ÊÔ´
- // ±ÈÈç Direct2D factory.
+ // ´´½¨É豸ÎÞ¹Ø×ÊÔ´
hr = _createDeviceIndependentResources();
if (SUCCEEDED(hr))
{
// ×¢²á´°¿ÚÀà
WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
- wcex.style = CS_HREDRAW | CS_VREDRAW;
+ wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = EApp::WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = sizeof(LONG_PTR);
wcex.hInstance = HINST_THISCOMPONENT;
- wcex.hbrBackground = NULL;
+ wcex.hbrBackground = (HBRUSH)(GetStockObject(BLACK_BRUSH));
wcex.lpszMenuName = NULL;
wcex.hCursor = LoadCursor(NULL, IDI_APPLICATION);
- wcex.lpszClassName = L"E2DApp";
+ wcex.lpszClassName = L"Easy2DApp";
RegisterClassEx(&wcex);
@@ -97,17 +85,31 @@ bool e2d::EApp::init(e2d::EString title, UINT32 width, UINT32 height, bool bShow
// to create its own windows.
GetFactory()->GetDesktopDpi(&dpiX, &dpiY);
- m_sTitle = title;
+ width = static_cast(ceil(width * dpiX / 96.f));
+ height = static_cast(ceil(height * dpiY / 96.f));
- // Create the window.
+ // »ñÈ¡ÆÁÄ»·Ö±æÂÊ
+ int screenWidth = GetSystemMetrics(SM_CXSCREEN);
+ int screenHeight = GetSystemMetrics(SM_CYSCREEN);
+ // ´´½¨ÆÁÄ»¾ÓÖеľØÐÎ
+ RECT rtWindow;
+ rtWindow.left = (screenWidth - width) / 2;
+ rtWindow.top = (screenHeight - height) / 2;
+ rtWindow.right = rtWindow.left + width;
+ rtWindow.bottom = rtWindow.top + height;
+ // ¼ÆËã¿Í»§Çø´óС
+ AdjustWindowRectEx(&rtWindow, windowStyle, FALSE, 0L);
+ // ±£´æ´°¿ÚÃû³Æ
+ m_sTitle = title;
+ // ´´½¨´°¿Ú
GetHWnd() = CreateWindow(
- L"E2DApp",
+ L"Easy2DApp",
m_sTitle.c_str(),
- WS_OVERLAPPEDWINDOW | CS_DBLCLKS,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- static_cast(ceil(width * dpiX / 96.f)),
- static_cast(ceil(height * dpiY / 96.f)),
+ windowStyle,
+ rtWindow.left,
+ rtWindow.top,
+ rtWindow.right - rtWindow.left,
+ rtWindow.bottom - rtWindow.top,
NULL,
NULL,
HINST_THISCOMPONENT,
@@ -115,28 +117,15 @@ bool e2d::EApp::init(e2d::EString title, UINT32 width, UINT32 height, bool bShow
);
hr = GetHWnd() ? S_OK : E_FAIL;
- if (FAILED(hr))
+
+ if (SUCCEEDED(hr))
{
- UnregisterClass(L"E2DApp", HINST_THISCOMPONENT);
- MessageBox(nullptr, L"Create Window Failed!", L"Error", MB_OK);
+ // ½ûÓÃÊäÈë·¨
+ this->setKeyboardLayoutEnable(false);
}
else
{
- // »ñÈ¡ÆÁÄ»·Ö±æÂÊ
- int screenWidth = GetSystemMetrics(SM_CXSCREEN);
- int screenHeight = GetSystemMetrics(SM_CYSCREEN);
- // »ñÈ¡´°¿Ú´óС£¨°üº¬²Ëµ¥À¸£©
- tagRECT rcWindow;
- GetWindowRect(GetHWnd(), &rcWindow);
- // ÉèÖô°¿ÚÔÚÆÁÄ»¾ÓÖÐ
- MoveWindow(
- GetHWnd(),
- (screenWidth - (rcWindow.right - rcWindow.left)) / 2,
- (screenHeight - (rcWindow.bottom - rcWindow.top)) / 2,
- (rcWindow.right - rcWindow.left),
- (rcWindow.bottom - rcWindow.top),
- FALSE
- );
+ UnregisterClass(L"E2DApp", HINST_THISCOMPONENT);
}
}
@@ -148,12 +137,59 @@ bool e2d::EApp::init(e2d::EString title, UINT32 width, UINT32 height, bool bShow
return SUCCEEDED(hr);
}
+void e2d::EApp::showConsole(bool show)
+{
+ // ²éÕÒÒÑ´æÔڵĿØÖÆÌ¨¾ä±ú
+ HWND hwnd = GetConsoleWindow();
+ static FILE * stdoutstream = nullptr;
+ static FILE * stdinstream = nullptr;
+ static FILE * stderrstream = nullptr;
+ // ¹Ø±Õ¿ØÖÆÌ¨
+ if (show)
+ {
+ if (hwnd)
+ {
+ ShowWindow(hwnd, SW_SHOWNORMAL);
+ }
+ else
+ {
+ // ÏÔʾһ¸öпØÖÆÌ¨
+ if (AllocConsole())
+ {
+ freopen_s(&stdoutstream, "CONOUT$", "w+t", stdout);
+ freopen_s(&stderrstream, "CONOUT$", "w+t", stderr);
+ freopen_s(&stdinstream, "CONIN$", "r+t", stdin);
+ }
+ else
+ {
+ MessageBox(nullptr, L"Alloc Console Failed!", L"Error", MB_OK);
+ }
+ }
+ }
+ else
+ {
+ if (hwnd)
+ {
+ if (stdoutstream)
+ {
+ fclose(stdoutstream);
+ fclose(stdinstream);
+ fclose(stderrstream);
+
+ stdoutstream = stdinstream = stderrstream = nullptr;
+ }
+ FreeConsole();
+ }
+ }
+}
+
// ÔËÐÐÓÎÏ·
void e2d::EApp::run()
{
- ASSERT(m_pNextScene != nullptr, "Next scene NULL pointer exception.");
+ ASSERT(GetHWnd() != nullptr, "Cannot find Game Window.");
// ½øÈëµÚÒ»¸ö³¡¾°
_enterNextScene();
+ ASSERT(m_pCurrentScene != nullptr, "Current scene NULL pointer exception.");
// ÏÔʾ´°¿Ú
ShowWindow(GetHWnd(), SW_SHOWNORMAL);
UpdateWindow(GetHWnd());
@@ -164,55 +200,58 @@ void e2d::EApp::run()
while (m_bRunning)
{
- if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
+ // ´¦Àí´°¿ÚÏûÏ¢
+ while (PeekMessage(&msg, GetHWnd(), 0, 0, PM_REMOVE))
{
- // ´¦Àí´°¿ÚÏûÏ¢
- if (msg.message == WM_QUIT)
- {
- m_bRunning = false;
- break;
- }
TranslateMessage(&msg);
DispatchMessage(&msg);
}
- else
- {
- // Ö´ÐÐÖ÷Ñ»·
- _mainLoop();
- }
+ // Ö´ÐÐÖ÷Ñ»·
+ _mainLoop();
}
- // ÓÎÏ·½áÊøºóÔÙÖ´ÐÐÒ»´ÎÑ»·
- _onControl();
- // ÊÍ·ÅËùÓÐÄÚ´æÕ¼ÓÃ
- free();
- CoUninitialize();
+ // ¹Ø±Õ¿ØÖÆÌ¨
+ EApp::showConsole(false);
+ // ÊÍ·ÅËùÓÐÄÚ´æ×ÊÔ´
+ this->free();
+}
+
+void e2d::EApp::setFPS(UINT32 fps)
+{
+ fps = min(max(fps, 30), 120);
+ nAnimationInterval = 1000 / fps;
+}
+
+bool e2d::EApp::onExit()
+{
+ return true;
}
void e2d::EApp::_mainLoop()
{
- static steady_clock::time_point nNow;
- static steady_clock::time_point nLast = steady_clock::now();
- // Ö¡¼ä¸ô
- static LONGLONG nAnimationInterval = 17LL;
// ʱ¼ä¼ä¸ô
static LONGLONG nInterval = 0LL;
// ¹ÒÆðʱ³¤
static LONGLONG nWaitMS = 0L;
-
// ˢмÆÊ±
- nNow = steady_clock::now();
+ static steady_clock::time_point tLast = steady_clock::now();
+
+ GetNow() = steady_clock::now();
// ¼ÆËãʱ¼ä¼ä¸ô
- nInterval = duration_cast(nNow - nLast).count();
+ nInterval = GetInterval(tLast);
// Åжϼä¸ôʱ¼äÊÇ·ñ×ã¹»
if (nInterval >= nAnimationInterval)
{
// ¼Ç¼µ±Ç°Ê±¼ä
- nLast = nNow;
- // Ö´ÐÐÓÎÏ·¿ØÖÆ
+ tLast = GetNow();
+ // ÓÎÏ·¿ØÖÆÁ÷³Ì
_onControl();
// Ë¢ÐÂÓÎÏ·»Ãæ
- _onRender();
+ if (!_onRender())
+ {
+ MessageBox(GetHWnd(), L"Game Render Failed!", L"Error", MB_OK);
+ this->quit();
+ }
}
else
{
@@ -221,7 +260,7 @@ void e2d::EApp::_mainLoop()
// ¹ÒÆðỊ̈߳¬ÊÍ·Å CPU Õ¼ÓÃ
if (nWaitMS > 1LL)
{
- std::this_thread::sleep_for(milliseconds(nWaitMS));
+ sleep_for(milliseconds(nWaitMS));
}
}
}
@@ -237,15 +276,15 @@ void e2d::EApp::_onControl()
// ¶ÏÑÔµ±Ç°³¡¾°·Ç¿Õ
ASSERT(m_pCurrentScene != nullptr, "Current scene NULL pointer exception.");
- //Timer::__exec(); // ¶¨Ê±Æ÷Ö´ÐгÌÐò
+ ETimerManager::TimerProc(); // ¶¨Ê±Æ÷Ö´ÐгÌÐò
//ActionManager::__exec(); // ¶¯×÷¹ÜÀíÆ÷Ö´ÐгÌÐò
- EObjectManager::__flush(); // Ë¢ÐÂÄÚ´æ³Ø
+ EObjectManager::__flush(); // Ë¢ÐÂÄÚ´æ³Ø
}
// This method discards device-specific
// resources if the Direct3D device dissapears during execution and
// recreates the resources the next time it's invoked.
-void e2d::EApp::_onRender()
+bool e2d::EApp::_onRender()
{
HRESULT hr = S_OK;
@@ -254,12 +293,14 @@ void e2d::EApp::_onRender()
if (SUCCEEDED(hr))
{
GetRenderTarget()->BeginDraw();
-
+ // ʹÓñ³¾°É«Çå¿ÕÆÁÄ»
GetRenderTarget()->Clear(D2D1::ColorF(m_ClearColor));
-
- m_pCurrentScene->_onRender(); // »æÖƵ±Ç°³¡¾°
-
+ // »æÖƵ±Ç°³¡¾°
+ m_pCurrentScene->_onRender();
+
hr = GetRenderTarget()->EndDraw();
+ // ˢнçÃæ
+ UpdateWindow(GetHWnd());
}
if (hr == D2DERR_RECREATE_TARGET)
@@ -268,13 +309,10 @@ void e2d::EApp::_onRender()
_discardDeviceResources();
}
- if (FAILED(hr))
- {
- exit(EXIT_FAILURE);
- }
+ return SUCCEEDED(hr);
}
-void e2d::EApp::setWindowSize(int width, int height)
+void e2d::EApp::setWindowSize(UINT32 width, UINT32 height)
{
// »ñÈ¡ÆÁÄ»·Ö±æÂÊ
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
@@ -292,12 +330,7 @@ void e2d::EApp::setWindowSize(int width, int height)
MoveWindow(GetHWnd(), (screenWidth - width) / 2, (screenHeight - height) / 2, width, height, TRUE);
}
-void e2d::EApp::setWindowSize(e2d::ESize size)
-{
- setWindowSize(size.cx, size.cy);
-}
-
-void e2d::EApp::setWindowTitle(e2d::EString title)
+void e2d::EApp::setWindowTitle(const EString &title)
{
// ÉèÖô°¿Ú±êÌâ
SetWindowText(GetHWnd(), title.c_str());
@@ -310,11 +343,6 @@ e2d::EString e2d::EApp::getTitle()
return get()->m_sTitle;
}
-e2d::ESize e2d::EApp::getSize()
-{
- return e2d::ESize(GetRenderTarget()->GetPixelSize().width, GetRenderTarget()->GetPixelSize().height);
-}
-
UINT32 e2d::EApp::getWidth()
{
return GetRenderTarget()->GetPixelSize().width;
@@ -338,6 +366,7 @@ void e2d::EApp::enterScene(EScene * scene, bool save /* = true */)
void e2d::EApp::backScene()
{
+ ASSERT(s_SceneStack.size(), "Scene stack now is empty!");
// ´ÓÕ»¶¥È¡³ö³¡¾°Ö¸Õ룬×÷ΪÏÂÒ»³¡¾°
get()->m_pNextScene = s_SceneStack.top();
s_SceneStack.pop();
@@ -364,36 +393,64 @@ e2d::EScene * e2d::EApp::getCurrentScene()
return get()->m_pCurrentScene;
}
-void e2d::EApp::setAppName(e2d::EString appname)
+void e2d::EApp::setAppName(const EString &appname)
{
s_pInstance->m_sAppName = appname;
}
e2d::EString e2d::EApp::getAppName()
{
+ if (s_pInstance->m_sAppName.empty())
+ s_pInstance->m_sAppName = s_pInstance->m_sTitle;
return s_pInstance->m_sAppName;
}
-void e2d::EApp::setBkColor(EColor::Enum color)
+void e2d::EApp::setBkColor(EColor color)
{
get()->m_ClearColor = color;
}
-void e2d::EApp::close()
+void e2d::EApp::setKeyboardLayoutEnable(bool value)
+{
+ static HIMC hImc = NULL;
+
+ if (value)
+ {
+ if (hImc != NULL)
+ {
+ ImmAssociateContext(GetHWnd(), hImc);
+ hImc = NULL;
+ }
+ }
+ else
+ {
+ if (hImc == NULL)
+ {
+ hImc = ImmAssociateContext(GetHWnd(), NULL);
+ }
+ }
+}
+
+HWND e2d::EApp::getHWnd()
+{
+ return GetHWnd();
+}
+
+void e2d::EApp::closeWindow()
{
ShowWindow(GetHWnd(), SW_HIDE);
}
-void e2d::EApp::show()
+void e2d::EApp::showWindow()
{
- ShowWindow(GetHWnd(), SW_NORMAL);
+ ShowWindow(GetHWnd(), SW_SHOWNORMAL);
}
void e2d::EApp::free()
{
// Êͷų¡¾°ÄÚ´æ
- SafeDelete(&get()->m_pCurrentScene);
- SafeDelete(&get()->m_pNextScene);
+ SafeDelete(&m_pCurrentScene);
+ SafeDelete(&m_pNextScene);
// Çå¿Õ³¡¾°Õ»
while (s_SceneStack.size())
{
@@ -401,8 +458,10 @@ void e2d::EApp::free()
SafeDelete(&temp);
s_SceneStack.pop();
}
+ // ɾ³ýͼƬ»º´æ
+ ESprite::clearCache();
// ɾ³ýËùÓж¨Ê±Æ÷¡¢¼àÌýÆ÷ºÍ¶¯»
- //Timer::clearAllTimers();
+ ETimerManager::clearAllTimers();
EMsgManager::clearAllKeyboardListeners();
EMsgManager::clearAllMouseListeners();
//ActionManager::clearAllActions();
@@ -486,7 +545,7 @@ void e2d::EApp::_discardDeviceResources()
// If the application receives a WM_SIZE message, this method
// re2d::ESizes the render target appropriately.
-void e2d::EApp::_onResize(UINT width, UINT height)
+void e2d::EApp::_onResize(UINT32 width, UINT32 height)
{
if (GetRenderTarget())
{
@@ -504,9 +563,10 @@ LRESULT e2d::EApp::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam
if (message == WM_CREATE)
{
+ // »ñÈ¡·¢ËÍ WM_CREATE ÏûÏ¢µÄ EApp ʵÀý¶ÔÏóÖ¸Õë
LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
e2d::EApp *pEApp = (e2d::EApp *)pcs->lpCreateParams;
-
+ // ±£´æ EApp Ö¸Õëµ½ GWLP_USERDATA ×Ö¶Î
::SetWindowLongPtrW(
hWnd,
GWLP_USERDATA,
@@ -517,6 +577,7 @@ LRESULT e2d::EApp::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam
}
else
{
+ // ´Ó GWLP_USERDATA ×Ö¶ÎÈ¡³ö EApp Ö¸Õë
e2d::EApp *pEApp = reinterpret_cast(static_cast(
::GetWindowLongPtrW(
hWnd,
@@ -529,17 +590,7 @@ LRESULT e2d::EApp::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam
{
switch (message)
{
- /*case WM_ACTIVATE:
- {
- if (LOWORD(wParam) == WA_INACTIVE)
- {
- MSG msg;
- do
- {
- GetMessage(&msg, nullptr, 0, 0);
- } while (msg.wParam != WA_ACTIVE);
- }
- }*/
+ // ´¦ÀíÊó±êÏûÏ¢
case WM_LBUTTONUP:
case WM_LBUTTONDOWN:
case WM_LBUTTONDBLCLK:
@@ -555,16 +606,20 @@ LRESULT e2d::EApp::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam
EMsgManager::MouseProc(message, wParam, lParam);
}
result = 0;
+ wasHandled = true;
break;
+ // ´¦Àí°´¼üÏûÏ¢
case WM_KEYDOWN:
case WM_KEYUP:
{
EMsgManager::KeyboardProc(message, wParam, lParam);
}
result = 0;
+ wasHandled = true;
break;
+ // ´¦Àí´°¿Ú´óС±ä»¯ÏûÏ¢
case WM_SIZE:
{
UINT width = LOWORD(lParam);
@@ -575,14 +630,17 @@ LRESULT e2d::EApp::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam
wasHandled = true;
break;
+ // ´¦Àí·Ö±æÂʱ仯ÏûÏ¢
case WM_DISPLAYCHANGE:
{
+ // ÖØ»æ¿Í»§Çø
InvalidateRect(hWnd, NULL, FALSE);
}
result = 0;
wasHandled = true;
break;
+ // ÖØ»æ´°¿Ú
case WM_PAINT:
{
pEApp->_onRender();
@@ -592,12 +650,24 @@ LRESULT e2d::EApp::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam
wasHandled = true;
break;
+ // ´°¿Ú¹Ø±ÕÏûÏ¢
+ case WM_CLOSE:
+ {
+ if (pEApp->onExit())
+ {
+ DestroyWindow(hWnd);
+ }
+ }
+ result = 1;
+ wasHandled = true;
+ break;
+
+ // ´°¿Ú±»Ïú»Ù
case WM_DESTROY:
{
- if (GetConsoleWindow())
- {
- FreeConsole();
- }
+ // Í˳ö³ÌÐò
+ pEApp->quit();
+ // ·¢ËÍÍ˳öÏûÏ¢
PostQuitMessage(0);
}
result = 1;
diff --git a/Easy2D/Base/EScene.cpp b/Easy2D/Base/EScene.cpp
index f5205e43..aa36dafe 100644
--- a/Easy2D/Base/EScene.cpp
+++ b/Easy2D/Base/EScene.cpp
@@ -1,17 +1,21 @@
#include "..\ebase.h"
#include "..\enodes.h"
#include "..\emsg.h"
+#include "..\etools.h"
#include
e2d::EScene::EScene()
: m_bWillSave(true)
, m_bSortNeeded(false)
+ , m_Root(new ENode())
{
+ m_Root->_onEnter();
+ m_Root->_setParentScene(this);
}
e2d::EScene::~EScene()
{
- clearAllChildren();
+ m_Root->autoRelease();
}
void e2d::EScene::onEnter()
@@ -24,39 +28,15 @@ void e2d::EScene::onExit()
void e2d::EScene::_onRender()
{
- this->_sortChildren();
-
- GetRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity());
-
- // ·ÃÎÊËùÓнڵã
- for (auto child : m_vChildren)
- {
- child->_callOn();
- }
-}
-
-void e2d::EScene::_sortChildren()
-{
- if (m_bSortNeeded)
- {
- m_bSortNeeded = false;
-
- // ×Ó½ÚµãÅÅÐò
- std::sort(
- std::begin(m_vChildren),
- std::end(m_vChildren),
- [](ENode * n1, ENode * n2) {
- return n1->getOrder() < n2->getOrder();
- }
- );
- }
+ m_Root->_callOn();
}
void e2d::EScene::_onEnter()
{
// ÆôÓó¡¾°ÉϵÄËùÓж¨Ê±Æ÷¡¢¼àÌýÆ÷ºÍ¶¯»
- //Timer::notifyAllSceneTimers(m_pNextScene);
- EMsgManager::notifyAllListenersBindWithScene(this);
+ ETimerManager::_notifyAllTimersBindedWith(this);
+ EMsgManager::_notifyAllMouseListenersBindedWith(this);
+ EMsgManager::_notifyAllKeyboardListenersBindedWith(this);
//ActionManager::notifyAllSceneActions(m_pNextScene);
}
@@ -64,94 +44,61 @@ void e2d::EScene::_onExit()
{
if (m_bWillSave)
{
- //Timer::waitAllSceneTimers(m_pCurrentScene);
- EMsgManager::waitAllListenersBindWithScene(this);
+ ETimerManager::_waitAllTimersBindedWith(this);
+ EMsgManager::_waitAllMouseListenersBindedWith(this);
+ EMsgManager::_waitAllKeyboardListenersBindedWith(this);
//ActionManager::waitAllSceneActions(m_pCurrentScene);
}
else
{
- //Timer::clearAllSceneTimers(m_pCurrentScene);
- EMsgManager::clearAllListenersBindWithScene(this);
+ ETimerManager::clearAllTimersBindedWith(this);
+ EMsgManager::clearAllMouseListenersBindedWith(this);
+ EMsgManager::clearAllKeyboardListenersBindedWith(this);
//ActionManager::stopAllSceneActions(m_pCurrentScene);
}
}
void e2d::EScene::add(ENode * child, int order /* = 0 */)
{
- ASSERT(child != nullptr, "Scene::add NULL pointer exception.");
- ASSERT(child->getParentScene() == nullptr, "Child already added. It can't be added again!");
-
- if (child)
- {
- child->setParentScene(this);
-
- child->setOrder(order);
-
- child->retain();
-
- m_vChildren.push_back(child);
-
- m_bSortNeeded = true;
- }
+ m_Root->addChild(child, order);
}
-bool e2d::EScene::remove(ENode * child, bool autoRelease /* = true */)
+bool e2d::EScene::remove(ENode * child, bool release /* = false */)
{
- if (child == nullptr) return false;
-
- // ѰÕÒÊÇ·ñÓÐÏàͬ½Úµã
- std::vector::iterator iter;
- for (iter = m_vChildren.begin(); iter != m_vChildren.end(); iter++)
- {
- // ÕÒµ½Ïàͬ½Úµã
- if (*iter == child)
- {
- if (autoRelease)
- (*iter)->autoRelease();
- // ¶ÔÏóµÄÒýÓüÆÊý¼õÒ»
- (*iter)->release();
- // È¥µô¸Ã½Úµã
- m_vChildren.erase(iter);
- return true;
- }
- }
- // δÕÒµ½¸Ã½Úµã·µ»Ø false
- return false;
+ return m_Root->removeChild(child, release);
}
-std::vector& e2d::EScene::getChildren()
+void e2d::EScene::remove(const EString &childName, bool release /* = false */)
{
- return m_vChildren;
+ return m_Root->removeChild(childName, release);
+}
+
+e2d::EVector& e2d::EScene::getChildren()
+{
+ return m_Root->m_vChildren;
}
size_t e2d::EScene::getChildrenCount() const
{
- return m_vChildren.size();
+ return m_Root->getChildrenCount();
}
-e2d::ENode * e2d::EScene::getChild(EString childName) const
+e2d::ENode * e2d::EScene::getChild(const EString &childName)
{
- return ENode::getChild(childName, m_vChildren);
+ return m_Root->getChild(childName);
}
void e2d::EScene::clearAllChildren()
{
- // ËùÓнڵãµÄÒýÓüÆÊý¼õÒ»
- for (auto child : m_vChildren)
- {
- child->autoRelease();
- child->release();
- }
- // Çå¿Õ´¢´æ½ÚµãµÄÈÝÆ÷
- m_vChildren.clear();
+ m_Root->clearAllChildren();
}
void e2d::EScene::bindListener(EMouseListener * listener)
{
- EMsgManager::bindListenerWith(listener, this);
+ EMsgManager::bindListener(listener, this);
}
void e2d::EScene::bindListener(EKeyboardListener * listener)
{
- EMsgManager::bindListenerWith(listener, this);
+ EMsgManager::bindListener(listener, this);
}
diff --git a/Easy2D/ETimer.cpp b/Easy2D/ETimer.cpp
new file mode 100644
index 00000000..b98605b6
--- /dev/null
+++ b/Easy2D/ETimer.cpp
@@ -0,0 +1,104 @@
+#include "etools.h"
+
+e2d::ETimer::ETimer()
+ : m_bRunning(false)
+ , m_bWaiting(false)
+ , m_nRunTimes(0)
+ , m_pParentScene(nullptr)
+ , m_pParentNode(nullptr)
+ , m_Callback([](int) {})
+ , m_nInterval(20LL)
+{
+}
+
+e2d::ETimer::ETimer(const EString & name)
+ : ETimer()
+{
+ m_sName = name;
+}
+
+e2d::ETimer::ETimer(const TIMER_CALLBACK & callback, LONGLONG delay /* = 20LL */)
+ : ETimer()
+{
+ m_Callback = callback;
+}
+
+e2d::ETimer::ETimer(const EString & name, const TIMER_CALLBACK & callback, LONGLONG delay /* = 20LL */)
+ : ETimer()
+{
+ m_sName = name;
+ m_Callback = callback;
+}
+
+bool e2d::ETimer::isRunning() const
+{
+ return m_bRunning && !m_bWaiting;
+}
+
+bool e2d::ETimer::isWaiting() const
+{
+ return m_bWaiting;
+}
+
+void e2d::ETimer::start()
+{
+ m_bRunning = true;
+ m_tLast = std::chrono::steady_clock::now();
+}
+
+void e2d::ETimer::stop()
+{
+ m_bRunning = false;
+}
+
+void e2d::ETimer::_wait()
+{
+ m_bWaiting = true;
+}
+
+void e2d::ETimer::_notify()
+{
+ m_bWaiting = false;
+ m_tLast = std::chrono::steady_clock::now();
+}
+
+e2d::EString e2d::ETimer::getName() const
+{
+ return m_sName;
+}
+
+e2d::EScene * e2d::ETimer::getParentScene() const
+{
+ return m_pParentScene;
+}
+
+e2d::ENode * e2d::ETimer::getParentNode() const
+{
+ return m_pParentNode;
+}
+
+void e2d::ETimer::setName(const EString & name)
+{
+ m_sName = name;
+}
+
+void e2d::ETimer::setInterval(LONGLONG interval)
+{
+ m_nInterval = max(interval, 0);
+}
+
+void e2d::ETimer::bindWith(EScene * pParentScene)
+{
+ ETimerManager::bindTimer(this, pParentScene);
+}
+
+void e2d::ETimer::bindWith(ENode * pParentNode)
+{
+ ETimerManager::bindTimer(this, pParentNode);
+}
+
+void e2d::ETimer::_runCallback()
+{
+ m_Callback(m_nRunTimes);
+ m_nRunTimes++;
+}
diff --git a/Easy2D/Easy2D.vcxproj b/Easy2D/Easy2D.vcxproj
index 135e7a09..192e15f9 100644
--- a/Easy2D/Easy2D.vcxproj
+++ b/Easy2D/Easy2D.vcxproj
@@ -195,9 +195,10 @@
+
-
+
diff --git a/Easy2D/Easy2D.vcxproj.filters b/Easy2D/Easy2D.vcxproj.filters
index 9405f3d5..9f37157b 100644
--- a/Easy2D/Easy2D.vcxproj.filters
+++ b/Easy2D/Easy2D.vcxproj.filters
@@ -54,12 +54,6 @@
Tool
-
- Msg\Listener
-
-
- Msg\Listener
-
Node
@@ -75,6 +69,15 @@
Msg\Listener
+
+ Tool
+
+
+ Msg\Listener
+
+
+ Msg\Listener
+
diff --git a/Easy2D/Msg/EMsgManager.cpp b/Easy2D/Msg/EMsgManager.cpp
index 3d3644a4..0a0a5cd2 100644
--- a/Easy2D/Msg/EMsgManager.cpp
+++ b/Easy2D/Msg/EMsgManager.cpp
@@ -1,86 +1,86 @@
#include "..\emsg.h"
+#include "..\enodes.h"
#include "..\Win\winbase.h"
-#include
// Êó±êÏûÏ¢
-e2d::EMouseMsg mouseMsg;
+e2d::EMouseMsg s_MouseMsg;
// °´¼üÏûÏ¢
-e2d::EKeyMsg keyMsg;
+e2d::EKeyboardMsg s_KeyboardMsg;
// Êó±êÏûÏ¢¼àÌýÆ÷
-std::vector s_vMouseListeners;
+e2d::EVector s_vMouseListeners;
// °´¼üÏûÏ¢¼àÌýÆ÷
-std::vector s_vKeyboardListeners;
+e2d::EVector s_vKeyboardListeners;
-DWORD e2d::EMouseMsg::getX()
+DWORD e2d::EMouseMsg::getPosX()
{
- return LOWORD(mouseMsg.m_lParam);
+ return LOWORD(s_MouseMsg.m_lParam);
}
-DWORD e2d::EMouseMsg::getY()
+DWORD e2d::EMouseMsg::getPosY()
{
- return HIWORD(mouseMsg.m_lParam);
+ return HIWORD(s_MouseMsg.m_lParam);
}
e2d::EPoint e2d::EMouseMsg::getPos()
{
- return EPoint(LOWORD(mouseMsg.m_lParam), HIWORD(mouseMsg.m_lParam));
+ return EPoint(LOWORD(s_MouseMsg.m_lParam), HIWORD(s_MouseMsg.m_lParam));
}
bool e2d::EMouseMsg::isLButtonDown()
{
- return GET_KEYSTATE_WPARAM(mouseMsg.m_wParam) == MK_LBUTTON;
+ return GET_KEYSTATE_WPARAM(s_MouseMsg.m_wParam) == MK_LBUTTON;
}
bool e2d::EMouseMsg::isMButtonDown()
{
- return GET_KEYSTATE_WPARAM(mouseMsg.m_wParam) == MK_MBUTTON;
+ return GET_KEYSTATE_WPARAM(s_MouseMsg.m_wParam) == MK_MBUTTON;
}
bool e2d::EMouseMsg::isRButtonDown()
{
- return GET_KEYSTATE_WPARAM(mouseMsg.m_wParam) == MK_RBUTTON;
+ return GET_KEYSTATE_WPARAM(s_MouseMsg.m_wParam) == MK_RBUTTON;
}
bool e2d::EMouseMsg::isShiftDown()
{
- return GET_KEYSTATE_WPARAM(mouseMsg.m_wParam) == MK_SHIFT;
+ return GET_KEYSTATE_WPARAM(s_MouseMsg.m_wParam) == MK_SHIFT;
}
bool e2d::EMouseMsg::isCtrlDown()
{
- return GET_KEYSTATE_WPARAM(mouseMsg.m_wParam) == MK_CONTROL;
+ return GET_KEYSTATE_WPARAM(s_MouseMsg.m_wParam) == MK_CONTROL;
}
DWORD e2d::EMouseMsg::getWheelDelta()
{
- return GET_WHEEL_DELTA_WPARAM(mouseMsg.m_wParam);
+ return GET_WHEEL_DELTA_WPARAM(s_MouseMsg.m_wParam);
}
e2d::EMouseMsg::MOUSE_MSG e2d::EMouseMsg::getMsg()
{
- return MOUSE_MSG(mouseMsg.m_nMsg);
+ return MOUSE_MSG(s_MouseMsg.m_nMsg);
}
-e2d::EKeyMsg::KEYBOARD_MSG e2d::EKeyMsg::getMsg()
+e2d::EKeyboardMsg::KEYBOARD_MSG e2d::EKeyboardMsg::getMsg()
{
- return KEYBOARD_MSG(keyMsg.m_nMsg);
+ return KEYBOARD_MSG(s_KeyboardMsg.m_nMsg);
}
-e2d::EKeyMsg::KEY e2d::EKeyMsg::getVal()
+e2d::EKeyboardMsg::KEY e2d::EKeyboardMsg::getVal()
{
- return KEY(keyMsg.m_wParam);
+ return KEY(s_KeyboardMsg.m_wParam);
}
-DWORD e2d::EKeyMsg::getCount()
+DWORD e2d::EKeyboardMsg::getCount()
{
- return (((DWORD)keyMsg.m_lParam) & 0x0000FFFF);
+ return (((DWORD)s_KeyboardMsg.m_lParam) & 0x0000FFFF);
}
-bool e2d::EKeyMsg::isKeyDown(KEY key)
+bool e2d::EKeyboardMsg::isKeyDown(KEY key)
{
if (::GetAsyncKeyState((int)key) & 0x8000)
{
@@ -89,7 +89,7 @@ bool e2d::EKeyMsg::isKeyDown(KEY key)
return false;
}
-bool e2d::EKeyMsg::isCapitalLockOn()
+bool e2d::EKeyboardMsg::isCapitalLockOn()
{
if (::GetKeyState(VK_CAPITAL) & 0x0001)
{
@@ -98,7 +98,7 @@ bool e2d::EKeyMsg::isCapitalLockOn()
return false;
}
-bool e2d::EKeyMsg::isNumpadLockOn()
+bool e2d::EKeyboardMsg::isNumpadLockOn()
{
if (::GetKeyState(VK_NUMLOCK) & 0x0001)
{
@@ -107,7 +107,7 @@ bool e2d::EKeyMsg::isNumpadLockOn()
return false;
}
-bool e2d::EKeyMsg::isScrollLockOn()
+bool e2d::EKeyboardMsg::isScrollLockOn()
{
if (::GetKeyState(VK_SCROLL) & 0x0001)
{
@@ -121,15 +121,15 @@ bool e2d::EKeyMsg::isScrollLockOn()
void e2d::EMsgManager::MouseProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// ±£´æÊó±êÏûÏ¢
- mouseMsg.m_nMsg = message;
- mouseMsg.m_wParam = wParam;
- mouseMsg.m_lParam = lParam;
+ s_MouseMsg.m_nMsg = message;
+ s_MouseMsg.m_wParam = wParam;
+ s_MouseMsg.m_lParam = lParam;
// Ö´ÐÐÊó±êÏûÏ¢¼àÌýº¯Êý
for (auto mlistener : s_vMouseListeners)
{
if (mlistener->isRunning())
{
- mlistener->runCallback();
+ mlistener->_runCallback();
}
}
}
@@ -137,21 +137,25 @@ void e2d::EMsgManager::MouseProc(UINT message, WPARAM wParam, LPARAM lParam)
void e2d::EMsgManager::KeyboardProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// ±£´æ°´¼üÏûÏ¢
- keyMsg.m_nMsg = message;
- keyMsg.m_wParam = wParam;
- keyMsg.m_lParam = lParam;
+ s_KeyboardMsg.m_nMsg = message;
+ s_KeyboardMsg.m_wParam = wParam;
+ s_KeyboardMsg.m_lParam = lParam;
// Ö´Ðа´¼üÏûÏ¢¼àÌýº¯Êý
for (auto klistener : s_vKeyboardListeners)
{
if (klistener->isRunning())
{
- klistener->runCallback();
+ klistener->_runCallback();
}
}
}
-void e2d::EMsgManager::bindListenerWith(e2d::EMouseListener * listener, EScene * pParentScene)
+void e2d::EMsgManager::bindListener(e2d::EMouseListener * listener, EScene * pParentScene)
{
+ ASSERT(
+ (!listener->m_pParentNode) && (!listener->m_pParentScene),
+ "The listener is already binded, it cannot bind again!"
+ );
WARN_IF(listener == nullptr, "EMouseListener NULL pointer exception!");
WARN_IF(pParentScene == nullptr, "Bind EMouseListener with a NULL EScene pointer!");
@@ -164,8 +168,12 @@ void e2d::EMsgManager::bindListenerWith(e2d::EMouseListener * listener, EScene *
}
}
-void e2d::EMsgManager::bindListenerWith(EKeyboardListener * listener, EScene * pParentScene)
+void e2d::EMsgManager::bindListener(EKeyboardListener * listener, EScene * pParentScene)
{
+ ASSERT(
+ (!listener->m_pParentNode) && (!listener->m_pParentScene),
+ "The listener is already binded, it cannot bind again!"
+ );
WARN_IF(listener == nullptr, "EKeyboardListener NULL pointer exception!");
WARN_IF(pParentScene == nullptr, "Bind EKeyboardListener with a NULL EScene pointer!");
@@ -178,8 +186,12 @@ void e2d::EMsgManager::bindListenerWith(EKeyboardListener * listener, EScene * p
}
}
-void e2d::EMsgManager::bindListenerWith(EMouseListener * listener, ENode * pParentNode)
+void e2d::EMsgManager::bindListener(EMouseListener * listener, ENode * pParentNode)
{
+ ASSERT(
+ (!listener->m_pParentNode) && (!listener->m_pParentScene),
+ "The listener is already binded, it cannot bind again!"
+ );
WARN_IF(listener == nullptr, "EMouseListener NULL pointer exception!");
WARN_IF(pParentNode == nullptr, "Bind EMouseListener with a NULL ENode pointer!");
@@ -192,8 +204,12 @@ void e2d::EMsgManager::bindListenerWith(EMouseListener * listener, ENode * pPare
}
}
-void e2d::EMsgManager::bindListenerWith(EKeyboardListener * listener, ENode * pParentNode)
+void e2d::EMsgManager::bindListener(EKeyboardListener * listener, ENode * pParentNode)
{
+ ASSERT(
+ (!listener->m_pParentNode) && (!listener->m_pParentScene),
+ "The listener is already binded, it cannot bind again!"
+ );
WARN_IF(listener == nullptr, "EKeyboardListener NULL pointer exception!");
WARN_IF(pParentNode == nullptr, "Bind EKeyboardListener with a NULL ENode pointer!");
@@ -206,9 +222,8 @@ void e2d::EMsgManager::bindListenerWith(EKeyboardListener * listener, ENode * pP
}
}
-void e2d::EMsgManager::startListener(EString name)
+void e2d::EMsgManager::startMouseListeners(const EString & name)
{
- // Æô¶¯Êó±êÏûÏ¢¼àÌýÆ÷
for (auto l : s_vMouseListeners)
{
if (l->getName() == name)
@@ -216,19 +231,10 @@ void e2d::EMsgManager::startListener(EString name)
l->start();
}
}
- // Æô¶¯°´¼üÏûÏ¢¼àÌýÆ÷
- for (auto l : s_vKeyboardListeners)
- {
- if (l->getName() == name)
- {
- l->start();
- }
- }
}
-void e2d::EMsgManager::stopListener(EString name)
+void e2d::EMsgManager::stopMouseListeners(const EString & name)
{
- // Í£Ö¹Êó±êÏûÏ¢¼àÌýÆ÷
for (auto l : s_vMouseListeners)
{
if (l->getName() == name)
@@ -236,20 +242,12 @@ void e2d::EMsgManager::stopListener(EString name)
l->stop();
}
}
- // Í£Ö¹°´¼üÏûÏ¢¼àÌýÆ÷
- for (auto l : s_vKeyboardListeners)
- {
- if (l->getName() == name)
- {
- l->stop();
- }
- }
}
-void e2d::EMsgManager::delListener(EString name)
+void e2d::EMsgManager::delMouseListeners(const EString & name)
{
// ɾ³ýÊó±êÏûÏ¢¼àÌýÆ÷
- std::vector::iterator mIter;
+ EVector::iterator mIter;
for (mIter = s_vMouseListeners.begin(); mIter != s_vMouseListeners.end();)
{
if ((*mIter)->getName() == name)
@@ -263,8 +261,36 @@ void e2d::EMsgManager::delListener(EString name)
mIter++;
}
}
+}
+
+void e2d::EMsgManager::startKeyboardListeners(const EString & name)
+{
+ // Æô¶¯°´¼üÏûÏ¢¼àÌýÆ÷
+ for (auto l : s_vKeyboardListeners)
+ {
+ if (l->getName() == name)
+ {
+ l->start();
+ }
+ }
+}
+
+void e2d::EMsgManager::stopKeyboardListeners(const EString & name)
+{
+ // Í£Ö¹°´¼üÏûÏ¢¼àÌýÆ÷
+ for (auto l : s_vKeyboardListeners)
+ {
+ if (l->getName() == name)
+ {
+ l->stop();
+ }
+ }
+}
+
+void e2d::EMsgManager::delKeyboardListeners(const EString & name)
+{
// ɾ³ý°´¼üÏûÏ¢¼àÌýÆ÷
- std::vector::iterator kIter;
+ EVector::iterator kIter;
for (kIter = s_vKeyboardListeners.begin(); kIter != s_vKeyboardListeners.end();)
{
if ((*kIter)->getName() == name)
@@ -280,259 +306,360 @@ void e2d::EMsgManager::delListener(EString name)
}
}
-void e2d::EMsgManager::startAllMouseListener()
+void e2d::EMsgManager::startAllMouseListenersBindedWith(EScene * pParentScene)
{
for (auto l : s_vMouseListeners)
{
- if (!l->isWaiting())
+ if (l->getParentScene() == pParentScene)
{
l->start();
}
}
+ for (auto child : pParentScene->getChildren())
+ {
+ EMsgManager::startAllMouseListenersBindedWith(child);
+ }
}
-void e2d::EMsgManager::stopAllMouseListener()
+void e2d::EMsgManager::stopAllMouseListenersBindedWith(EScene * pParentScene)
{
for (auto l : s_vMouseListeners)
{
- if (!l->isWaiting())
+ if (l->getParentScene() == pParentScene)
{
l->stop();
}
}
+ for (auto child : pParentScene->getChildren())
+ {
+ EMsgManager::stopAllMouseListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::startAllMouseListenersBindedWith(ENode * pParentNode)
+{
+ for (auto l : s_vMouseListeners)
+ {
+ if (l->getParentNode() == pParentNode)
+ {
+ l->start();
+ }
+ }
+ for (auto child : pParentNode->getChildren())
+ {
+ EMsgManager::startAllMouseListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::stopAllMouseListenersBindedWith(ENode * pParentNode)
+{
+ for (auto l : s_vMouseListeners)
+ {
+ if (l->getParentNode() == pParentNode)
+ {
+ l->stop();
+ }
+ }
+ for (auto child : pParentNode->getChildren())
+ {
+ EMsgManager::stopAllMouseListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::startAllKeyboardListenersBindedWith(EScene * pParentScene)
+{
+ for (auto l : s_vKeyboardListeners)
+ {
+ if (l->getParentScene() == pParentScene)
+ {
+ l->start();
+ }
+ }
+ for (auto child : pParentScene->getChildren())
+ {
+ EMsgManager::startAllKeyboardListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::stopAllKeyboardListenersBindedWith(EScene * pParentScene)
+{
+ for (auto l : s_vKeyboardListeners)
+ {
+ if (l->getParentScene() == pParentScene)
+ {
+ l->stop();
+ }
+ }
+ for (auto child : pParentScene->getChildren())
+ {
+ EMsgManager::stopAllKeyboardListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::startAllKeyboardListenersBindedWith(ENode * pParentNode)
+{
+ for (auto l : s_vKeyboardListeners)
+ {
+ if (l->getParentNode() == pParentNode)
+ {
+ l->start();
+ }
+ }
+ for (auto child : pParentNode->getChildren())
+ {
+ EMsgManager::startAllKeyboardListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::stopAllKeyboardListenersBindedWith(ENode * pParentNode)
+{
+ for (auto l : s_vKeyboardListeners)
+ {
+ if (l->getParentNode() == pParentNode)
+ {
+ l->stop();
+ }
+ }
+ for (auto child : pParentNode->getChildren())
+ {
+ EMsgManager::stopAllKeyboardListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::clearAllMouseListenersBindedWith(EScene * pParentScene)
+{
+ for (size_t i = 0; i < s_vMouseListeners.size();)
+ {
+ auto t = s_vMouseListeners[i];
+ if (t->getParentScene() == pParentScene)
+ {
+ t->autoRelease();
+ t->release();
+ s_vMouseListeners.erase(s_vMouseListeners.begin() + i);
+ }
+ else
+ {
+ i++;
+ }
+ }
+ for (auto child : pParentScene->getChildren())
+ {
+ EMsgManager::clearAllMouseListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::clearAllKeyboardListenersBindedWith(EScene * pParentScene)
+{
+ for (size_t i = 0; i < s_vKeyboardListeners.size();)
+ {
+ auto t = s_vKeyboardListeners[i];
+ if (t->getParentScene() == pParentScene)
+ {
+ t->autoRelease();
+ t->release();
+ s_vKeyboardListeners.erase(s_vKeyboardListeners.begin() + i);
+ }
+ else
+ {
+ i++;
+ }
+ }
+ for (auto child : pParentScene->getChildren())
+ {
+ EMsgManager::clearAllKeyboardListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::clearAllMouseListenersBindedWith(ENode * pParentNode)
+{
+ for (size_t i = 0; i < s_vMouseListeners.size();)
+ {
+ auto t = s_vMouseListeners[i];
+ if (t->getParentNode() == pParentNode)
+ {
+ t->autoRelease();
+ t->release();
+ s_vMouseListeners.erase(s_vMouseListeners.begin() + i);
+ }
+ else
+ {
+ i++;
+ }
+ }
+ for (auto child : pParentNode->getChildren())
+ {
+ EMsgManager::clearAllMouseListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::clearAllKeyboardListenersBindedWith(ENode * pParentNode)
+{
+ for (size_t i = 0; i < s_vKeyboardListeners.size();)
+ {
+ auto t = s_vKeyboardListeners[i];
+ if (t->getParentNode() == pParentNode)
+ {
+ t->autoRelease();
+ t->release();
+ s_vKeyboardListeners.erase(s_vKeyboardListeners.begin() + i);
+ }
+ else
+ {
+ i++;
+ }
+ }
+ for (auto child : pParentNode->getChildren())
+ {
+ EMsgManager::clearAllKeyboardListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::_waitAllMouseListenersBindedWith(EScene * pParentScene)
+{
+ for (auto l : s_vMouseListeners)
+ {
+ if (l->getParentScene() == pParentScene)
+ {
+ l->_wait();
+ }
+ }
+ for (auto child : pParentScene->getChildren())
+ {
+ EMsgManager::_waitAllMouseListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::_notifyAllMouseListenersBindedWith(EScene * pParentScene)
+{
+ for (auto l : s_vMouseListeners)
+ {
+ if (l->getParentScene() == pParentScene)
+ {
+ l->_notify();
+ }
+ }
+ for (auto child : pParentScene->getChildren())
+ {
+ EMsgManager::_notifyAllMouseListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::_waitAllMouseListenersBindedWith(ENode * pParentNode)
+{
+ for (auto l : s_vMouseListeners)
+ {
+ if (l->getParentNode() == pParentNode)
+ {
+ l->_wait();
+ }
+ }
+ for (auto child : pParentNode->getChildren())
+ {
+ EMsgManager::_waitAllMouseListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::_notifyAllMouseListenersBindedWith(ENode * pParentNode)
+{
+ for (auto l : s_vMouseListeners)
+ {
+ if (l->getParentNode() == pParentNode)
+ {
+ l->_notify();
+ }
+ }
+ for (auto child : pParentNode->getChildren())
+ {
+ EMsgManager::_notifyAllMouseListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::_waitAllKeyboardListenersBindedWith(EScene * pParentScene)
+{
+ for (auto l : s_vKeyboardListeners)
+ {
+ if (l->getParentScene() == pParentScene)
+ {
+ l->_wait();
+ }
+ }
+ for (auto child : pParentScene->getChildren())
+ {
+ EMsgManager::_waitAllKeyboardListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::_notifyAllKeyboardListenersBindedWith(EScene * pParentScene)
+{
+ for (auto l : s_vKeyboardListeners)
+ {
+ if (l->getParentScene() == pParentScene)
+ {
+ l->_notify();
+ }
+ }
+ for (auto child : pParentScene->getChildren())
+ {
+ EMsgManager::_notifyAllKeyboardListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::_waitAllKeyboardListenersBindedWith(ENode * pParentNode)
+{
+ for (auto l : s_vKeyboardListeners)
+ {
+ if (l->getParentNode() == pParentNode)
+ {
+ l->_wait();
+ }
+ }
+ for (auto child : pParentNode->getChildren())
+ {
+ EMsgManager::_waitAllKeyboardListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::_notifyAllKeyboardListenersBindedWith(ENode * pParentNode)
+{
+ for (auto l : s_vKeyboardListeners)
+ {
+ if (l->getParentNode() == pParentNode)
+ {
+ l->_notify();
+ }
+ }
+ for (auto child : pParentNode->getChildren())
+ {
+ EMsgManager::_notifyAllKeyboardListenersBindedWith(child);
+ }
+}
+
+void e2d::EMsgManager::startAllMouseListeners()
+{
+ EMsgManager::startAllMouseListenersBindedWith(EApp::getCurrentScene());
+}
+
+void e2d::EMsgManager::stopAllMouseListeners()
+{
+ EMsgManager::stopAllMouseListenersBindedWith(EApp::getCurrentScene());
}
void e2d::EMsgManager::clearAllMouseListeners()
{
- for (auto l : s_vMouseListeners)
- {
- l->autoRelease();
- l->release();
- }
- s_vMouseListeners.clear();
+ EMsgManager::clearAllMouseListenersBindedWith(EApp::getCurrentScene());
}
-void e2d::EMsgManager::startAllKeyboardListener()
+void e2d::EMsgManager::startAllKeyboardListeners()
{
- for (auto l : s_vKeyboardListeners)
- {
- if (!l->isWaiting())
- {
- l->start();
- }
- }
+ EMsgManager::startAllKeyboardListenersBindedWith(EApp::getCurrentScene());
}
-void e2d::EMsgManager::stopAllKeyboardListener()
+void e2d::EMsgManager::stopAllKeyboardListeners()
{
- for (auto l : s_vKeyboardListeners)
- {
- if (!l->isWaiting())
- {
- l->stop();
- }
- }
+ EMsgManager::stopAllKeyboardListenersBindedWith(EApp::getCurrentScene());
}
void e2d::EMsgManager::clearAllKeyboardListeners()
{
- for (auto l : s_vKeyboardListeners)
- {
- l->autoRelease();
- l->release();
- }
- s_vKeyboardListeners.clear();
-}
-
-void e2d::EMsgManager::startAllMouseListenersBindWithScene(EScene * pParentScene)
-{
- // Æô¶¯Êó±êÏûÏ¢¼àÌýÆ÷
- for (auto l : s_vMouseListeners)
- {
- if (l->getParentScene() == pParentScene)
- {
- l->start();
- }
- }
-}
-
-void e2d::EMsgManager::stopAllMouseListenersBindWithScene(EScene * pParentScene)
-{
- // Í£Ö¹Êó±êÏûÏ¢¼àÌýÆ÷
- for (auto l : s_vMouseListeners)
- {
- if (l->getParentScene() == pParentScene)
- {
- l->stop();
- }
- }
-}
-
-void e2d::EMsgManager::startAllKeyboardListenersBindWithScene(EScene * pParentScene)
-{
- // Æô¶¯°´¼üÏûÏ¢¼àÌýÆ÷
- for (auto l : s_vKeyboardListeners)
- {
- if (l->getParentScene() == pParentScene)
- {
- l->start();
- }
- }
-}
-
-void e2d::EMsgManager::stopAllKeyboardListenersBindWithScene(EScene * pParentScene)
-{
- // Í£Ö¹°´¼üÏûÏ¢¼àÌýÆ÷
- for (auto l : s_vKeyboardListeners)
- {
- if (l->getParentScene() == pParentScene)
- {
- l->stop();
- }
- }
-}
-
-void e2d::EMsgManager::waitAllListenersBindWithScene(EScene * scene)
-{
- // ¹ÒÆðÊó±êÏûÏ¢¼àÌýÆ÷
- for (auto l : s_vMouseListeners)
- {
- if (l->getParentScene() == scene)
- {
- l->wait();
- }
- }
- // ¹ÒÆð°´¼üÏûÏ¢¼àÌýÆ÷
- for (auto l : s_vKeyboardListeners)
- {
- if (l->getParentScene() == scene)
- {
- l->wait();
- }
- }
-}
-
-void e2d::EMsgManager::notifyAllListenersBindWithScene(EScene * scene)
-{
- // ÖØÆôÊó±êÏûÏ¢¼àÌýÆ÷
- for (auto l : s_vMouseListeners)
- {
- if (l->getParentScene() == scene)
- {
- l->notify();
- }
- }
- // ÖØÆô°´¼üÏûÏ¢¼àÌýÆ÷
- for (auto l : s_vKeyboardListeners)
- {
- if (l->getParentScene() == scene)
- {
- l->notify();
- }
- }
-}
-
-void e2d::EMsgManager::clearAllListenersBindWithScene(EScene * scene)
-{
- std::vector::iterator mIter;
- for (mIter = s_vMouseListeners.begin(); mIter != s_vMouseListeners.end();)
- {
- if ((*mIter)->getParentScene() == scene)
- {
- (*mIter)->autoRelease();
- (*mIter)->release();
- mIter = s_vMouseListeners.erase(mIter);
- }
- else
- {
- mIter++;
- }
- }
- std::vector::iterator kIter;
- for (kIter = s_vKeyboardListeners.begin(); kIter != s_vKeyboardListeners.end();)
- {
- if ((*kIter)->getParentScene() == scene)
- {
- (*kIter)->autoRelease();
- (*kIter)->release();
- kIter = s_vKeyboardListeners.erase(kIter);
- }
- else
- {
- kIter++;
- }
- }
-}
-
-void e2d::EMsgManager::waitAllListenersBindWithNode(ENode * pParentNode)
-{
- // ¹ÒÆðÊó±êÏûÏ¢¼àÌýÆ÷
- for (auto l : s_vMouseListeners)
- {
- if (l->getParentNode() == pParentNode)
- {
- l->wait();
- }
- }
- // ¹ÒÆð°´¼üÏûÏ¢¼àÌýÆ÷
- for (auto l : s_vKeyboardListeners)
- {
- if (l->getParentNode() == pParentNode)
- {
- l->wait();
- }
- }
-}
-
-void e2d::EMsgManager::notifyAllListenersBindWithNode(ENode * pParentNode)
-{
- // ÖØÆôÊó±êÏûÏ¢¼àÌýÆ÷
- for (auto l : s_vMouseListeners)
- {
- if (l->getParentNode() == pParentNode)
- {
- l->notify();
- }
- }
- // ÖØÆô°´¼üÏûÏ¢¼àÌýÆ÷
- for (auto l : s_vKeyboardListeners)
- {
- if (l->getParentNode() == pParentNode)
- {
- l->notify();
- }
- }
-}
-
-void e2d::EMsgManager::clearAllListenersBindWithNode(ENode * pParentNode)
-{
- std::vector::iterator mIter;
- for (mIter = s_vMouseListeners.begin(); mIter != s_vMouseListeners.end();)
- {
- if ((*mIter)->getParentNode() == pParentNode)
- {
- (*mIter)->autoRelease();
- (*mIter)->release();
- mIter = s_vMouseListeners.erase(mIter);
- }
- else
- {
- mIter++;
- }
- }
- std::vector::iterator kIter;
- for (kIter = s_vKeyboardListeners.begin(); kIter != s_vKeyboardListeners.end();)
- {
- if ((*kIter)->getParentNode() == pParentNode)
- {
- (*kIter)->autoRelease();
- (*kIter)->release();
- kIter = s_vKeyboardListeners.erase(kIter);
- }
- else
- {
- kIter++;
- }
- }
-}
-
+ EMsgManager::clearAllKeyboardListenersBindedWith(EApp::getCurrentScene());
+}
\ No newline at end of file
diff --git a/Easy2D/Msg/Listener/EKeyPressListener.cpp b/Easy2D/Msg/Listener/EKeyPressListener.cpp
deleted file mode 100644
index 3b2a22bd..00000000
--- a/Easy2D/Msg/Listener/EKeyPressListener.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
-#include "..\..\emsg.h"
-
-e2d::EKeyPressListener::EKeyPressListener()
- : EKeyboardListener()
-{
-}
-
-e2d::EKeyPressListener::EKeyPressListener(EString name)
- : EKeyboardListener(name)
-{
-}
-
-e2d::EKeyPressListener::EKeyPressListener(const KEY_LISTENER_CALLBACK & callback)
- : EKeyboardListener(callback)
-{
-}
-
-e2d::EKeyPressListener::EKeyPressListener(EString name, const KEY_LISTENER_CALLBACK & callback)
- : EKeyboardListener(name, callback)
-{
-}
-
-void e2d::EKeyPressListener::runCallback()
-{
- if (EKeyMsg::getMsg() == EKeyMsg::KEYBOARD_MSG::KEY_DOWN)
- {
- m_callback();
- }
-}
diff --git a/Easy2D/Msg/Listener/EKeyboardListener.cpp b/Easy2D/Msg/Listener/EKeyboardListener.cpp
index 8c467803..cb362318 100644
--- a/Easy2D/Msg/Listener/EKeyboardListener.cpp
+++ b/Easy2D/Msg/Listener/EKeyboardListener.cpp
@@ -5,7 +5,7 @@ e2d::EKeyboardListener::EKeyboardListener()
{
}
-e2d::EKeyboardListener::EKeyboardListener(EString name)
+e2d::EKeyboardListener::EKeyboardListener(const EString & name)
: EListener(name)
{
}
@@ -13,23 +13,23 @@ e2d::EKeyboardListener::EKeyboardListener(EString name)
e2d::EKeyboardListener::EKeyboardListener(const KEY_LISTENER_CALLBACK & callback)
: EListener()
{
- m_callback = callback;
+ m_Callback = callback;
}
-e2d::EKeyboardListener::EKeyboardListener(EString name, const KEY_LISTENER_CALLBACK & callback)
+e2d::EKeyboardListener::EKeyboardListener(const EString & name, const KEY_LISTENER_CALLBACK & callback)
: EListener(name)
{
- m_callback = callback;
+ m_Callback = callback;
}
-void e2d::EKeyboardListener::runCallback()
+void e2d::EKeyboardListener::_runCallback()
{
- m_callback();
+ m_Callback();
}
void e2d::EKeyboardListener::setCallback(const KEY_LISTENER_CALLBACK & callback)
{
- m_callback = callback;
+ m_Callback = callback;
}
void e2d::EKeyboardListener::bindWith(EScene * pParentScene)
@@ -38,7 +38,7 @@ void e2d::EKeyboardListener::bindWith(EScene * pParentScene)
if (pParentScene)
{
- EMsgManager::bindListenerWith(this, pParentScene);
+ EMsgManager::bindListener(this, pParentScene);
}
}
@@ -48,6 +48,6 @@ void e2d::EKeyboardListener::bindWith(ENode * pParentNode)
if (pParentNode != nullptr && m_pParentScene == nullptr)
{
- EMsgManager::bindListenerWith(this, pParentNode);
+ EMsgManager::bindListener(this, pParentNode);
}
}
\ No newline at end of file
diff --git a/Easy2D/Msg/Listener/EKeyboardPressListener.cpp b/Easy2D/Msg/Listener/EKeyboardPressListener.cpp
new file mode 100644
index 00000000..67b3081e
--- /dev/null
+++ b/Easy2D/Msg/Listener/EKeyboardPressListener.cpp
@@ -0,0 +1,29 @@
+#include "..\..\emsg.h"
+
+e2d::EKeyboardPressListener::EKeyboardPressListener()
+ : EKeyboardListener()
+{
+}
+
+e2d::EKeyboardPressListener::EKeyboardPressListener(const EString & name)
+ : EKeyboardListener(name)
+{
+}
+
+e2d::EKeyboardPressListener::EKeyboardPressListener(const KEY_LISTENER_CALLBACK & callback)
+ : EKeyboardListener(callback)
+{
+}
+
+e2d::EKeyboardPressListener::EKeyboardPressListener(const EString & name, const KEY_LISTENER_CALLBACK & callback)
+ : EKeyboardListener(name, callback)
+{
+}
+
+void e2d::EKeyboardPressListener::_runCallback()
+{
+ if (EKeyboardMsg::getMsg() == EKeyboardMsg::KEYBOARD_MSG::KEY_DOWN)
+ {
+ m_Callback();
+ }
+}
diff --git a/Easy2D/Msg/Listener/EListener.cpp b/Easy2D/Msg/Listener/EListener.cpp
index 56284616..ff8b384d 100644
--- a/Easy2D/Msg/Listener/EListener.cpp
+++ b/Easy2D/Msg/Listener/EListener.cpp
@@ -3,13 +3,12 @@
e2d::EListener::EListener()
: m_bRunning(false)
, m_bWaiting(false)
- , m_sName(L"")
, m_pParentScene(nullptr)
, m_pParentNode(nullptr)
{
}
-e2d::EListener::EListener(EString name)
+e2d::EListener::EListener(const EString & name)
: EListener()
{
m_sName = name;
@@ -35,12 +34,12 @@ void e2d::EListener::stop()
m_bRunning = false;
}
-void e2d::EListener::wait()
+void e2d::EListener::_wait()
{
m_bWaiting = true;
}
-void e2d::EListener::notify()
+void e2d::EListener::_notify()
{
m_bWaiting = false;
}
@@ -60,7 +59,7 @@ e2d::ENode * e2d::EListener::getParentNode() const
return m_pParentNode;
}
-void e2d::EListener::setName(EString name)
+void e2d::EListener::setName(const EString & name)
{
m_sName = name;
}
diff --git a/Easy2D/Msg/Listener/EMouseClickListener.cpp b/Easy2D/Msg/Listener/EMouseClickListener.cpp
index 367046bd..ceed23dd 100644
--- a/Easy2D/Msg/Listener/EMouseClickListener.cpp
+++ b/Easy2D/Msg/Listener/EMouseClickListener.cpp
@@ -6,7 +6,7 @@ e2d::EMouseClickListener::EMouseClickListener()
{
}
-e2d::EMouseClickListener::EMouseClickListener(EString name)
+e2d::EMouseClickListener::EMouseClickListener(const EString & name)
: EMouseListener(name)
, m_bPressed(false)
{
@@ -14,19 +14,19 @@ e2d::EMouseClickListener::EMouseClickListener(EString name)
e2d::EMouseClickListener::EMouseClickListener(const MOUSE_CLICK_LISTENER_CALLBACK & callback)
: EMouseListener()
- , m_callback(callback)
+ , m_Callback(callback)
, m_bPressed(false)
{
}
-e2d::EMouseClickListener::EMouseClickListener(EString name, const MOUSE_CLICK_LISTENER_CALLBACK & callback)
+e2d::EMouseClickListener::EMouseClickListener(const EString & name, const MOUSE_CLICK_LISTENER_CALLBACK & callback)
: EMouseListener(name)
- , m_callback(callback)
+ , m_Callback(callback)
, m_bPressed(false)
{
}
-void e2d::EMouseClickListener::runCallback()
+void e2d::EMouseClickListener::_runCallback()
{
if (EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DOWN ||
EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DBLCLK)
@@ -35,12 +35,12 @@ void e2d::EMouseClickListener::runCallback()
}
else if (m_bPressed && EMouseMsg::getMsg() == EMouseMsg::LBUTTON_UP)
{
- m_callback(EMouseMsg::getPos());
+ m_Callback(EMouseMsg::getPos());
m_bPressed = false;
}
}
void e2d::EMouseClickListener::setCallback(const MOUSE_CLICK_LISTENER_CALLBACK & callback)
{
- m_callback = callback;
+ m_Callback = callback;
}
diff --git a/Easy2D/Msg/Listener/EMouseDoubleClickListener.cpp b/Easy2D/Msg/Listener/EMouseDoubleClickListener.cpp
index 1a8ee4f8..6b92f791 100644
--- a/Easy2D/Msg/Listener/EMouseDoubleClickListener.cpp
+++ b/Easy2D/Msg/Listener/EMouseDoubleClickListener.cpp
@@ -6,7 +6,7 @@ e2d::EMouseDoubleClickListener::EMouseDoubleClickListener()
{
}
-e2d::EMouseDoubleClickListener::EMouseDoubleClickListener(EString name)
+e2d::EMouseDoubleClickListener::EMouseDoubleClickListener(const EString & name)
: EMouseListener(name)
, m_bPressed(false)
{
@@ -14,32 +14,36 @@ e2d::EMouseDoubleClickListener::EMouseDoubleClickListener(EString name)
e2d::EMouseDoubleClickListener::EMouseDoubleClickListener(const MOUSE_DBLCLK_LISTENER_CALLBACK & callback)
: EMouseListener()
- , m_callback(callback)
+ , m_Callback(callback)
, m_bPressed(false)
{
}
-e2d::EMouseDoubleClickListener::EMouseDoubleClickListener(EString name, const MOUSE_DBLCLK_LISTENER_CALLBACK & callback)
+e2d::EMouseDoubleClickListener::EMouseDoubleClickListener(const EString & name, const MOUSE_DBLCLK_LISTENER_CALLBACK & callback)
: EMouseListener(name)
- , m_callback(callback)
+ , m_Callback(callback)
, m_bPressed(false)
{
}
-void e2d::EMouseDoubleClickListener::runCallback()
+void e2d::EMouseDoubleClickListener::_runCallback()
{
- if (EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DBLCLK)
+ if (EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DOWN)
+ {
+ m_bPressed = false;
+ }
+ else if (EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DBLCLK)
{
m_bPressed = true;
}
else if (m_bPressed && EMouseMsg::getMsg() == EMouseMsg::LBUTTON_UP)
{
- m_callback(EMouseMsg::getPos());
+ m_Callback(EMouseMsg::getPos());
m_bPressed = false;
}
}
void e2d::EMouseDoubleClickListener::setCallback(const MOUSE_DBLCLK_LISTENER_CALLBACK & callback)
{
- m_callback = callback;
+ m_Callback = callback;
}
diff --git a/Easy2D/Msg/Listener/EMouseDragListener.cpp b/Easy2D/Msg/Listener/EMouseDragListener.cpp
index 1eb9c705..f41ff094 100644
--- a/Easy2D/Msg/Listener/EMouseDragListener.cpp
+++ b/Easy2D/Msg/Listener/EMouseDragListener.cpp
@@ -5,24 +5,24 @@ e2d::EMouseDragListener::EMouseDragListener()
{
}
-e2d::EMouseDragListener::EMouseDragListener(EString name)
+e2d::EMouseDragListener::EMouseDragListener(const EString & name)
: EMouseListener(name)
{
}
e2d::EMouseDragListener::EMouseDragListener(const MOUSE_DRAG_LISTENER_CALLBACK & callback)
: EMouseListener()
- , m_callback(callback)
+ , m_Callback(callback)
{
}
-e2d::EMouseDragListener::EMouseDragListener(EString name, const MOUSE_DRAG_LISTENER_CALLBACK & callback)
+e2d::EMouseDragListener::EMouseDragListener(const EString & name, const MOUSE_DRAG_LISTENER_CALLBACK & callback)
: EMouseListener(name)
- , m_callback(callback)
+ , m_Callback(callback)
{
}
-void e2d::EMouseDragListener::runCallback()
+void e2d::EMouseDragListener::_runCallback()
{
if (EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DOWN ||
EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DBLCLK)
@@ -31,11 +31,11 @@ void e2d::EMouseDragListener::runCallback()
}
else if (EMouseMsg::isLButtonDown() && EMouseMsg::getMsg() == EMouseMsg::MOVE)
{
- m_callback(m_Begin, EMouseMsg::getPos());
+ m_Callback(m_Begin, EMouseMsg::getPos());
}
}
void e2d::EMouseDragListener::setCallback(const MOUSE_DRAG_LISTENER_CALLBACK & callback)
{
- m_callback = callback;
+ m_Callback = callback;
}
diff --git a/Easy2D/Msg/Listener/EMouseListener.cpp b/Easy2D/Msg/Listener/EMouseListener.cpp
index 0087de44..5a4d45aa 100644
--- a/Easy2D/Msg/Listener/EMouseListener.cpp
+++ b/Easy2D/Msg/Listener/EMouseListener.cpp
@@ -5,7 +5,7 @@ e2d::EMouseListener::EMouseListener()
{
}
-e2d::EMouseListener::EMouseListener(EString name)
+e2d::EMouseListener::EMouseListener(const EString & name)
: EListener(name)
{
}
@@ -13,23 +13,23 @@ e2d::EMouseListener::EMouseListener(EString name)
e2d::EMouseListener::EMouseListener(const MOUSE_LISTENER_CALLBACK & callback)
: EListener()
{
- m_callback = callback;
+ m_Callback = callback;
}
-e2d::EMouseListener::EMouseListener(EString name, const MOUSE_LISTENER_CALLBACK & callback)
+e2d::EMouseListener::EMouseListener(const EString & name, const MOUSE_LISTENER_CALLBACK & callback)
: EListener(name)
{
- m_callback = callback;
+ m_Callback = callback;
}
-void e2d::EMouseListener::runCallback()
+void e2d::EMouseListener::_runCallback()
{
- m_callback();
+ m_Callback();
}
void e2d::EMouseListener::setCallback(const MOUSE_LISTENER_CALLBACK & callback)
{
- m_callback = callback;
+ m_Callback = callback;
}
void e2d::EMouseListener::bindWith(EScene * pParentScene)
@@ -38,7 +38,7 @@ void e2d::EMouseListener::bindWith(EScene * pParentScene)
if (pParentScene)
{
- EMsgManager::bindListenerWith(this, pParentScene);
+ EMsgManager::bindListener(this, pParentScene);
}
}
@@ -48,6 +48,6 @@ void e2d::EMouseListener::bindWith(ENode * pParentNode)
if (pParentNode != nullptr && m_pParentScene == nullptr)
{
- EMsgManager::bindListenerWith(this, pParentNode);
+ EMsgManager::bindListener(this, pParentNode);
}
}
diff --git a/Easy2D/Msg/Listener/EMousePressListener.cpp b/Easy2D/Msg/Listener/EMousePressListener.cpp
index ef0801e4..dec9b7fa 100644
--- a/Easy2D/Msg/Listener/EMousePressListener.cpp
+++ b/Easy2D/Msg/Listener/EMousePressListener.cpp
@@ -5,33 +5,33 @@ e2d::EMousePressListener::EMousePressListener()
{
}
-e2d::EMousePressListener::EMousePressListener(EString name)
+e2d::EMousePressListener::EMousePressListener(const EString & name)
: EMouseListener(name)
{
}
e2d::EMousePressListener::EMousePressListener(const MOUSE_PRESS_LISTENER_CALLBACK & callback)
: EMouseListener()
- , m_callback(callback)
+ , m_Callback(callback)
{
}
-e2d::EMousePressListener::EMousePressListener(EString name, const MOUSE_PRESS_LISTENER_CALLBACK & callback)
+e2d::EMousePressListener::EMousePressListener(const EString & name, const MOUSE_PRESS_LISTENER_CALLBACK & callback)
: EMouseListener(name)
- , m_callback(callback)
+ , m_Callback(callback)
{
}
-void e2d::EMousePressListener::runCallback()
+void e2d::EMousePressListener::_runCallback()
{
if (EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DOWN ||
EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DBLCLK)
{
- m_callback(EMouseMsg::getPos());
+ m_Callback(EMouseMsg::getPos());
}
}
void e2d::EMousePressListener::setCallback(const MOUSE_PRESS_LISTENER_CALLBACK & callback)
{
- m_callback = callback;
+ m_Callback = callback;
}
diff --git a/Easy2D/Node/ENode.cpp b/Easy2D/Node/ENode.cpp
index 6c190c3c..e5f6adf1 100644
--- a/Easy2D/Node/ENode.cpp
+++ b/Easy2D/Node/ENode.cpp
@@ -1,13 +1,11 @@
#include "..\enodes.h"
+#include "..\emsg.h"
+#include "..\etools.h"
#include "..\Win\winbase.h"
#include
e2d::ENode::ENode()
: m_nOrder(0)
- , m_fPosX(0)
- , m_fPosY(0)
- , m_fWidth(0)
- , m_fHeight(0)
, m_fScaleX(1.0f)
, m_fScaleY(1.0f)
, m_fRotation(0)
@@ -19,6 +17,7 @@ e2d::ENode::ENode()
, m_fAnchorY(0)
, m_Matri(D2D1::Matrix3x2F::Identity())
, m_bVisiable(true)
+ , m_bDisplayedInScene(false)
, m_pParent(nullptr)
, m_pParentScene(nullptr)
, m_nHashName(0)
@@ -27,7 +26,7 @@ e2d::ENode::ENode()
{
}
-e2d::ENode::ENode(EString name)
+e2d::ENode::ENode(const EString & name)
: ENode()
{
setName(name);
@@ -37,6 +36,14 @@ e2d::ENode::~ENode()
{
}
+void e2d::ENode::onEnter()
+{
+}
+
+void e2d::ENode::onExit()
+{
+}
+
void e2d::ENode::_callOn()
{
if (!m_bVisiable)
@@ -89,6 +96,53 @@ void e2d::ENode::_onRender()
{
}
+void e2d::ENode::_onEnter()
+{
+ if (!this->m_bDisplayedInScene && this->isVisiable())
+ {
+ this->m_bDisplayedInScene = true;
+
+ ETimerManager::_notifyAllTimersBindedWith(this);
+ EMsgManager::_notifyAllMouseListenersBindedWith(this);
+ EMsgManager::_notifyAllKeyboardListenersBindedWith(this);
+ this->onEnter();
+
+ for (const auto &child : m_vChildren)
+ {
+ child->_onEnter();
+ }
+ }
+}
+
+void e2d::ENode::_onExit()
+{
+ if (this->m_bDisplayedInScene)
+ {
+ this->m_bDisplayedInScene = false;
+
+ ETimerManager::_waitAllTimersBindedWith(this);
+ EMsgManager::_waitAllMouseListenersBindedWith(this);
+ EMsgManager::_waitAllKeyboardListenersBindedWith(this);
+ this->onExit();
+
+ for (const auto &child : m_vChildren)
+ {
+ child->_onExit();
+ }
+ }
+}
+
+void e2d::ENode::_onClear()
+{
+ ETimerManager::clearAllTimersBindedWith(this);
+ EMsgManager::clearAllMouseListenersBindedWith(this);
+ EMsgManager::clearAllKeyboardListenersBindedWith(this);
+ for (const auto &child : m_vChildren)
+ {
+ child->_onClear();
+ }
+}
+
void e2d::ENode::_sortChildren()
{
if (m_bSortChildrenNeeded)
@@ -110,26 +164,26 @@ void e2d::ENode::_updateTransformToReal()
{
// ¼ÆËãêµã×ø±ê
D2D1_POINT_2F anchorPos = D2D1::Point2F(
- m_fWidth * m_fAnchorX,
- m_fHeight * m_fAnchorY
+ m_Size.width * m_fAnchorX,
+ m_Size.height * m_fAnchorY
);
// ¼ÆËã×óÉϽÇ×ø±ê
D2D1_POINT_2F upperLeftCorner = D2D1::Point2F(
- m_fPosX - m_fWidth * m_fAnchorX,
- m_fPosY - m_fHeight * m_fAnchorY
+ m_Pos.x - m_Size.width * m_fAnchorX,
+ m_Pos.y - m_Size.height * m_fAnchorY
);
// ¶þά¾ØÐα任
m_Matri = D2D1::Matrix3x2F::Scale(
m_fScaleX,
m_fScaleY,
anchorPos
- ) * D2D1::Matrix3x2F::Rotation(
- m_fRotation,
- anchorPos
) * D2D1::Matrix3x2F::Skew(
m_fSkewAngleX,
m_fSkewAngleY,
anchorPos
+ ) * D2D1::Matrix3x2F::Rotation(
+ m_fRotation,
+ anchorPos
) * D2D1::Matrix3x2F::Translation(
upperLeftCorner.x,
upperLeftCorner.y
@@ -178,24 +232,54 @@ bool e2d::ENode::isVisiable() const
return m_bVisiable;
}
-float e2d::ENode::getX() const
+e2d::EString e2d::ENode::getName() const
{
- return m_fPosX;
+ return m_sName;
}
-float e2d::ENode::getY() const
+float e2d::ENode::getPosX() const
{
- return m_fPosY;
+ return m_Pos.x;
+}
+
+float e2d::ENode::getPosY() const
+{
+ return m_Pos.y;
+}
+
+e2d::EPoint e2d::ENode::getPos() const
+{
+ return m_Pos;
}
float e2d::ENode::getWidth() const
{
- return m_fWidth;
+ return m_Size.width * m_fScaleX;
}
float e2d::ENode::getHeight() const
{
- return m_fHeight;
+ return m_Size.height * m_fScaleY;
+}
+
+float e2d::ENode::getRealWidth() const
+{
+ return m_Size.width;
+}
+
+float e2d::ENode::getRealHeight() const
+{
+ return m_Size.height;
+}
+
+e2d::ESize e2d::ENode::getRealSize() const
+{
+ return m_Size;
+}
+
+e2d::ESize e2d::ENode::getSize() const
+{
+ return ESize(getWidth(), getHeight());
}
float e2d::ENode::getScaleX() const
@@ -238,48 +322,63 @@ void e2d::ENode::setOrder(int order)
m_nOrder = order;
}
-void e2d::ENode::setX(float x)
+void e2d::ENode::setPosX(float x)
{
- this->setPos(x, m_fPosY);
+ this->setPos(x, m_Pos.y);
}
-void e2d::ENode::setY(float y)
+void e2d::ENode::setPosY(float y)
{
- this->setPos(m_fPosX, y);
+ this->setPos(m_Pos.x, y);
+}
+
+void e2d::ENode::setPos(const EPoint & p)
+{
+ this->setPos(p.x, p.y);
}
void e2d::ENode::setPos(float x, float y)
{
- if (m_fPosX == x && m_fPosY == y)
+ if (m_Pos.x == x && m_Pos.y == y)
return;
- m_fPosX = x;
- m_fPosY = y;
+ m_Pos.x = x;
+ m_Pos.y = y;
m_bTransformChildrenNeeded = true;
}
void e2d::ENode::move(float x, float y)
{
- this->setPos(m_fPosX + x, m_fPosY + y);
+ this->setPos(m_Pos.x + x, m_Pos.y + y);
+}
+
+void e2d::ENode::move(const EVec & v)
+{
+ this->move(v.x, v.y);
}
void e2d::ENode::setWidth(float width)
{
- this->setSize(width, m_fHeight);
+ this->setSize(width, m_Size.height);
}
void e2d::ENode::setHeight(float height)
{
- this->setSize(m_fWidth, height);
+ this->setSize(m_Size.width, height);
+}
+
+void e2d::ENode::setSize(const ESize & size)
+{
+ this->setSize(size.width, size.height);
}
void e2d::ENode::setSize(float width, float height)
{
- if (m_fWidth == width && m_fHeight == height)
+ if (m_Size.width == width && m_Size.height == height)
return;
- m_fWidth = width;
- m_fHeight = height;
+ m_Size.width = width;
+ m_Size.height = height;
m_bTransformChildrenNeeded = true;
}
@@ -382,7 +481,7 @@ void e2d::ENode::setParent(ENode * parent)
void e2d::ENode::addChild(ENode * child, int order /* = 0 */)
{
WARN_IF(child == nullptr, "ENode::addChild NULL pointer exception.");
- ASSERT(child->m_pParent == nullptr, "Child already added. It can't be added again!");
+ ASSERT(child->m_pParent == nullptr, "ENode already added. It can't be added again!");
if (child)
{
@@ -401,6 +500,16 @@ void e2d::ENode::addChild(ENode * child, int order /* = 0 */)
_updateOpacity(child);
+ if (this->m_pParentScene)
+ {
+ child->_setParentScene(this->m_pParentScene);
+ }
+
+ if (this->m_bDisplayedInScene)
+ {
+ child->_onEnter();
+ }
+
m_bSortChildrenNeeded = true;
}
}
@@ -415,7 +524,7 @@ e2d::EScene * e2d::ENode::getParentScene() const
return m_pParentScene;
}
-std::vector& e2d::ENode::getChildren()
+e2d::EVector& e2d::ENode::getChildren()
{
return m_vChildren;
}
@@ -425,19 +534,14 @@ size_t e2d::ENode::getChildrenCount() const
return m_vChildren.size();
}
-e2d::ENode * e2d::ENode::getChild(EString name) const
-{
- return ENode::getChild(name, this->m_vChildren);
-}
-
-e2d::ENode * e2d::ENode::getChild(EString name, const std::vector &children)
+e2d::ENode * e2d::ENode::getChild(const EString & name)
{
WARN_IF(name.empty(), "Invalid ENode name.");
std::hash h;
size_t hash = h(name);
- for (const auto& child : children)
+ for (const auto& child : m_vChildren)
{
// ²»Í¬µÄÃû³Æ¿ÉÄÜ»áÓÐÏàͬµÄ Hash Öµ£¬µ«ÊÇÏÈ±È½Ï Hash ¿ÉÒÔÌáÉýËÑË÷ËÙ¶È
if (child->m_nHashName == hash && child->m_sName == name)
@@ -446,14 +550,6 @@ e2d::ENode * e2d::ENode::getChild(EString name, const std::vector &child
return nullptr;
}
-void e2d::ENode::setParentScene(EScene * scene)
-{
- if (m_pParentScene)
- {
- m_pParentScene = scene;
- }
-}
-
void e2d::ENode::removeFromParent(bool release /* = false */)
{
if (m_pParent)
@@ -462,13 +558,13 @@ void e2d::ENode::removeFromParent(bool release /* = false */)
}
}
-void e2d::ENode::removeChild(ENode * child, bool release /* = false */)
+bool e2d::ENode::removeChild(ENode * child, bool release /* = false */)
{
WARN_IF(child == nullptr, "ENode::removeChild NULL pointer exception.");
if (m_vChildren.empty())
{
- return;
+ return false;
}
if (child)
@@ -480,16 +576,22 @@ void e2d::ENode::removeChild(ENode * child, bool release /* = false */)
{
m_vChildren.erase(m_vChildren.begin() + i);
child->m_pParent = nullptr;
+ if (child->m_pParentScene)
+ {
+ child->_setParentScene(nullptr);
+ }
+ child->_onExit();
child->release();
if (release)
child->autoRelease();
- return;
+ return true;
}
}
}
+ return false;
}
-void e2d::ENode::removeChild(EString childName, bool release /* = false */)
+void e2d::ENode::removeChild(const EString & childName, bool release /* = false */)
{
WARN_IF(childName.empty(), "Invalid ENode name.");
@@ -498,8 +600,10 @@ void e2d::ENode::removeChild(EString childName, bool release /* = false */)
return;
}
+ // ¼ÆËãÃû³Æ Hash Öµ
std::hash h;
size_t hash = h(childName);
+
size_t size = m_vChildren.size();
for (size_t i = 0; i < size; i++)
{
@@ -508,6 +612,11 @@ void e2d::ENode::removeChild(EString childName, bool release /* = false */)
{
m_vChildren.erase(m_vChildren.begin() + i);
child->m_pParent = nullptr;
+ if (child->m_pParentScene)
+ {
+ child->_setParentScene(nullptr);
+ }
+ child->_onExit();
child->release();
if (release)
child->autoRelease();
@@ -516,12 +625,36 @@ void e2d::ENode::removeChild(EString childName, bool release /* = false */)
}
}
+void e2d::ENode::clearAllChildren(bool release /* = false */)
+{
+ // ËùÓнڵãµÄÒýÓüÆÊý¼õÒ»
+ for (auto child : m_vChildren)
+ {
+ if (release)
+ {
+ child->_onClear();
+ child->autoRelease();
+ }
+ else
+ {
+ child->_onExit();
+ }
+ child->release();
+ }
+ // Çå¿Õ´¢´æ½ÚµãµÄÈÝÆ÷
+ m_vChildren.clear();
+}
+
void e2d::ENode::setVisiable(bool value)
{
m_bVisiable = value;
+ if (m_bDisplayedInScene == false)
+ {
+ this->_onEnter();
+ }
}
-void e2d::ENode::setName(EString name)
+void e2d::ENode::setName(const EString & name)
{
WARN_IF(name.empty(), "Invalid ENode name.");
@@ -534,3 +667,12 @@ void e2d::ENode::setName(EString name)
m_nHashName = h(name);
}
}
+
+void e2d::ENode::_setParentScene(EScene * scene)
+{
+ m_pParentScene = scene;
+ for (const auto &child : m_vChildren)
+ {
+ child->_setParentScene(scene);
+ }
+}
\ No newline at end of file
diff --git a/Easy2D/Node/ERectangle.cpp b/Easy2D/Node/ERectangle.cpp
index 4e302905..62676b57 100644
--- a/Easy2D/Node/ERectangle.cpp
+++ b/Easy2D/Node/ERectangle.cpp
@@ -6,19 +6,19 @@ e2d::ERectangle::ERectangle()
{
}
-e2d::EColor::Enum e2d::ERectangle::getColor() const
+e2d::EColor e2d::ERectangle::getColor() const
{
return m_Color;
}
-void e2d::ERectangle::setColor(EColor::Enum color)
+void e2d::ERectangle::setColor(EColor color)
{
m_Color = color;
}
void e2d::ERectangle::_onRender()
{
- D2D1_RECT_F rectangle = D2D1::RectF(0, 0, m_fWidth, m_fHeight);
+ D2D1_RECT_F rectangle = D2D1::RectF(0, 0, getWidth(), getHeight());
GetSolidColorBrush()->SetColor(D2D1::ColorF(m_Color, m_fDisplayOpacity));
GetRenderTarget()->FillRectangle(&rectangle, GetSolidColorBrush());
}
diff --git a/Easy2D/Node/ESprite.cpp b/Easy2D/Node/ESprite.cpp
index e284503a..f32be238 100644
--- a/Easy2D/Node/ESprite.cpp
+++ b/Easy2D/Node/ESprite.cpp
@@ -1,33 +1,49 @@
#include "..\enodes.h"
#include