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 + +struct ResKey +{ + ResKey() { resNameHash = 0; resTypeHash = 0; } + + bool operator < (ResKey const& key) const + { + if (resNameHash > key.resNameHash) + return true; + else if (resNameHash == key.resNameHash) + return resTypeHash > key.resTypeHash; + else + return false; + } + + size_t resNameHash; + size_t resTypeHash; +}; + static std::map s_mBitmapsFromFile; -//static std::map s_mBitmapsFromResource; +static std::map s_mBitmapsFromResource; -static ID2D1Bitmap * GetBitmapFromFile(e2d::EString fileName); -static ID2D1Bitmap * GetBitmapFromResource(e2d::EString resourceName, e2d::EString resourceType); - -static bool _loadBitmapFromFile(e2d::EString fileName); -static bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceType); +static ID2D1Bitmap * GetBitmapFromFile(const e2d::EString & fileName); +static ID2D1Bitmap * GetBitmapFromResource(const e2d::EString & resourceName, const e2d::EString & resourceType); e2d::ESprite::ESprite() { } -e2d::ESprite::ESprite(EString imageFileName) +e2d::ESprite::ESprite(const EString & imageFileName) : ESprite() { setImage(imageFileName); } -e2d::ESprite::ESprite(EString resourceName, EString resourceType) +e2d::ESprite::ESprite(const EString & resourceName, const EString & resourceType) : ESprite() { setImage(resourceName, resourceType); } -void e2d::ESprite::setImage(EString fileName) +void e2d::ESprite::setImage(const EString & fileName) { WARN_IF(fileName.empty(), "ESprite cannot load bitmap from NULL file name."); @@ -44,7 +60,7 @@ void e2d::ESprite::setImage(EString fileName) this->setSize(pBitmap->GetSize().width, pBitmap->GetSize().height); } -void e2d::ESprite::setImage(EString resourceName, EString resourceType) +void e2d::ESprite::setImage(const EString & resourceName, const EString & resourceType) { WARN_IF(resourceName.empty() || resourceType.empty(), "ESprite cannot load bitmap from NULL resource."); @@ -75,17 +91,22 @@ void e2d::ESprite::_onRender() // Draw bitmap GetRenderTarget()->DrawBitmap( pBitmap, - D2D1::RectF(0, 0, m_fWidth, m_fHeight), + D2D1::RectF(0, 0, getRealWidth(), getRealHeight()), m_fDisplayOpacity ); } } - - - -bool _loadBitmapFromFile(e2d::EString fileName) +bool e2d::ESprite::preloadImage(const EString & fileName) { + std::hash h; + size_t hash = h(fileName); + + if (s_mBitmapsFromFile.find(hash) != s_mBitmapsFromFile.end()) + { + return true; + } + HRESULT hr = S_OK; IWICBitmapDecoder *pDecoder = nullptr; @@ -94,6 +115,7 @@ bool _loadBitmapFromFile(e2d::EString fileName) IWICFormatConverter *pConverter = nullptr; ID2D1Bitmap *pBitmap = nullptr; + // ´´½¨½âÂëÆ÷ hr = GetImagingFactory()->CreateDecoderFromFilename( fileName.c_str(), NULL, @@ -104,18 +126,19 @@ bool _loadBitmapFromFile(e2d::EString fileName) if (SUCCEEDED(hr)) { - // Create the initial frame. + // ´´½¨³õʼ»¯¿ò¼Ü hr = pDecoder->GetFrame(0, &pSource); } if (SUCCEEDED(hr)) { - // Convert the image format to 32bppPBGRA + // ´´½¨Í¼Æ¬¸ñʽת»»Æ÷ // (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED). hr = GetImagingFactory()->CreateFormatConverter(&pConverter); } if (SUCCEEDED(hr)) { + // ͼƬ¸ñʽת»»³É 32bbpPBGRA hr = pConverter->Initialize( pSource, GUID_WICPixelFormat32bppPBGRA, @@ -127,7 +150,7 @@ bool _loadBitmapFromFile(e2d::EString fileName) } if (SUCCEEDED(hr)) { - // Create a Direct2D bitmap from the WIC bitmap. + // ´Ó WIC λͼ´´½¨Ò»¸ö Direct2D λͼ hr = GetRenderTarget()->CreateBitmapFromWicBitmap( pConverter, NULL, @@ -136,6 +159,7 @@ bool _loadBitmapFromFile(e2d::EString fileName) } if (SUCCEEDED(hr)) { + // ±£´æÍ¼Æ¬Ö¸ÕëºÍͼƬµÄ Hash Ãû std::hash h; size_t hash = h(fileName); @@ -146,6 +170,7 @@ bool _loadBitmapFromFile(e2d::EString fileName) ); } + // ÊÍ·ÅÏà¹Ø×ÊÔ´ SafeReleaseInterface(&pDecoder); SafeReleaseInterface(&pSource); SafeReleaseInterface(&pStream); @@ -154,10 +179,21 @@ bool _loadBitmapFromFile(e2d::EString fileName) return SUCCEEDED(hr); } -bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceType) +bool e2d::ESprite::preloadImage(const EString & resourceName, const EString & resourceType) { + std::hash h; + + ResKey key; + key.resNameHash = h(resourceName); + key.resTypeHash = h(resourceType); + + if (s_mBitmapsFromResource.find(key) != s_mBitmapsFromResource.end()) + { + return true; + } + HRESULT hr = S_OK; - + IWICBitmapDecoder *pDecoder = nullptr; IWICBitmapFrameDecode *pSource = nullptr; IWICStream *pStream = nullptr; @@ -170,29 +206,29 @@ bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceTyp void *pImageFile = nullptr; DWORD imageFileSize = 0; - // Locate the resource. - imageResHandle = FindResourceW(HINST_THISCOMPONENT, resourceName.c_str(), resourceType.c_str()); + // ¶¨Î»×ÊÔ´ + imageResHandle = ::FindResourceW(HINST_THISCOMPONENT, resourceName.c_str(), resourceType.c_str()); hr = imageResHandle ? S_OK : E_FAIL; if (SUCCEEDED(hr)) { - // Load the resource. - imageResDataHandle = LoadResource(HINST_THISCOMPONENT, imageResHandle); + // ¼ÓÔØ×ÊÔ´ + imageResDataHandle = ::LoadResource(HINST_THISCOMPONENT, imageResHandle); hr = imageResDataHandle ? S_OK : E_FAIL; } if (SUCCEEDED(hr)) { - // Lock it to get a system memory pointer. - pImageFile = LockResource(imageResDataHandle); + // »ñÈ¡ÎļþÖ¸Õ룬²¢Ëø¶¨×ÊÔ´ + pImageFile = ::LockResource(imageResDataHandle); hr = pImageFile ? S_OK : E_FAIL; } if (SUCCEEDED(hr)) { - // Calculate the size. + // ¼ÆËã´óС imageFileSize = SizeofResource(HINST_THISCOMPONENT, imageResHandle); hr = imageFileSize ? S_OK : E_FAIL; @@ -200,13 +236,13 @@ bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceTyp if (SUCCEEDED(hr)) { - // Create a WIC stream to map onto the memory. + // ´´½¨ WIC Á÷ hr = GetImagingFactory()->CreateStream(&pStream); } if (SUCCEEDED(hr)) { - // Initialize the stream with the memory pointer and size. + // ³õʼ»¯Á÷ hr = pStream->InitializeFromMemory( reinterpret_cast(pImageFile), imageFileSize @@ -215,7 +251,7 @@ bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceTyp if (SUCCEEDED(hr)) { - // Create a decoder for the stream. + // ´´½¨Á÷µÄ½âÂëÆ÷ hr = GetImagingFactory()->CreateDecoderFromStream( pStream, NULL, @@ -226,19 +262,20 @@ bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceTyp if (SUCCEEDED(hr)) { - // Create the initial frame. + // ´´½¨³õʼ»¯¿ò¼Ü hr = pDecoder->GetFrame(0, &pSource); } if (SUCCEEDED(hr)) { - // Convert the image format to 32bppPBGRA + // ´´½¨Í¼Æ¬¸ñʽת»»Æ÷ // (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED). hr = GetImagingFactory()->CreateFormatConverter(&pConverter); } if (SUCCEEDED(hr)) { + // ͼƬ¸ñʽת»»³É 32bppPBGRA hr = pConverter->Initialize( pSource, GUID_WICPixelFormat32bppPBGRA, @@ -251,7 +288,7 @@ bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceTyp if (SUCCEEDED(hr)) { - //create a Direct2D bitmap from the WIC bitmap. + // ´Ó WIC λͼ´´½¨Ò»¸ö Direct2D λͼ hr = GetRenderTarget()->CreateBitmapFromWicBitmap( pConverter, NULL, @@ -259,6 +296,22 @@ bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceTyp ); } + if (SUCCEEDED(hr)) + { + std::hash h; + + ResKey key; + key.resNameHash = h(resourceName); + key.resTypeHash = h(resourceType); + + s_mBitmapsFromResource.insert( + std::map::value_type( + key, + pBitmap) + ); + } + + // ÊÍ·ÅÏà¹Ø×ÊÔ´ SafeReleaseInterface(&pDecoder); SafeReleaseInterface(&pSource); SafeReleaseInterface(&pStream); @@ -268,22 +321,47 @@ bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceTyp return SUCCEEDED(hr); } -ID2D1Bitmap * GetBitmapFromFile(e2d::EString fileName) +void e2d::ESprite::clearCache() { + for (auto child : s_mBitmapsFromFile) + { + SafeReleaseInterface(&child.second); + } + for (auto child : s_mBitmapsFromResource) + { + SafeReleaseInterface(&child.second); + } + s_mBitmapsFromFile.clear(); + s_mBitmapsFromResource.clear(); +} + + + + +ID2D1Bitmap * GetBitmapFromFile(const e2d::EString & fileName) +{ + if (!e2d::ESprite::preloadImage(fileName)) + { + return nullptr; + } + std::hash h; size_t hash = h(fileName); - if (s_mBitmapsFromFile.find(hash) == s_mBitmapsFromFile.end()) - { - if (!_loadBitmapFromFile(fileName)) - { - return nullptr; - } - } return s_mBitmapsFromFile.at(hash); } -ID2D1Bitmap * GetBitmapFromResource(e2d::EString resourceName, e2d::EString resourceType) +ID2D1Bitmap * GetBitmapFromResource(const e2d::EString & resourceName, const e2d::EString & resourceType) { - return nullptr; + if (!e2d::ESprite::preloadImage(resourceName, resourceType)) + { + return nullptr; + } + + ResKey key; + std::hash h; + key.resNameHash = h(resourceName); + key.resTypeHash = h(resourceType); + + return s_mBitmapsFromResource.at(key); } \ No newline at end of file diff --git a/Easy2D/Tool/EObjectManager.cpp b/Easy2D/Tool/EObjectManager.cpp index dc5e982c..6f113d0f 100644 --- a/Easy2D/Tool/EObjectManager.cpp +++ b/Easy2D/Tool/EObjectManager.cpp @@ -1,5 +1,4 @@ #include "..\etools.h" -#include // EObjectManager ÊͷųصÄʵÏÖ»úÖÆ£º /// EObject ÀàÖеÄÒýÓüÆÊý£¨m_nRefCount£©±£Ö¤ÁËÖ¸ÕëµÄʹÓð²È« @@ -9,7 +8,7 @@ /// ÈÃÆä×Ô¶¯ÊÍ·Å // ÊͷųØÈÝÆ÷ -static std::vector s_vPool; +static e2d::EVector s_vPool; // ±êÖ¾ÊͷųØÖ´ÐÐ״̬ static bool s_bNotifyed = false; @@ -19,11 +18,11 @@ void e2d::EObjectManager::__flush() s_bNotifyed = false; // ´´½¨µü´úÆ÷ - static std::vector::iterator iter; + static EVector::iterator iter; // Ñ­»·±éÀúÈÝÆ÷ÖеÄËùÓжÔÏó for (iter = s_vPool.begin(); iter != s_vPool.end();) { - if ((*iter)->m_bAutoRelease && (*iter)->m_nRefCount == 0) + if ((*iter)->m_bAutoRelease && (*iter)->m_nRefCount <= 0) { // Èô¶ÔÏóµÄÒýÓõļÆÊýΪ 0, ÊͷŸöÔÏó delete (*iter); diff --git a/Easy2D/Tool/ETimerManager.cpp b/Easy2D/Tool/ETimerManager.cpp index 08fb0b33..dfa19f05 100644 --- a/Easy2D/Tool/ETimerManager.cpp +++ b/Easy2D/Tool/ETimerManager.cpp @@ -1 +1,264 @@ -#include "..\etools.h" \ No newline at end of file +#include "..\etools.h" +#include "..\enodes.h" +#include "..\Win\winbase.h" + +static e2d::EVector s_vTimers; + +void e2d::ETimerManager::bindTimer(ETimer * timer, EScene * pParentScene) +{ + ASSERT( + (!timer->m_pParentScene) && (!timer->m_pParentNode), + "The timer is already binded, it cannot bind again!" + ); + WARN_IF(timer == nullptr, "ETimer NULL pointer exception!"); + WARN_IF(pParentScene == nullptr, "Bind ETimer with a NULL EScene pointer!"); + + if (timer && pParentScene) + { + timer->start(); + timer->retain(); + timer->m_pParentScene = pParentScene; + s_vTimers.push_back(timer); + } +} + +void e2d::ETimerManager::bindTimer(ETimer * timer, ENode * pParentNode) +{ + ASSERT( + (!timer->m_pParentScene) && (!timer->m_pParentNode), + "The timer is already binded, it cannot bind again!" + ); + WARN_IF(timer == nullptr, "ETimer NULL pointer exception!"); + WARN_IF(pParentNode == nullptr, "Bind ETimer with a NULL ENode pointer!"); + + if (timer && pParentNode) + { + timer->start(); + timer->retain(); + timer->m_pParentNode = pParentNode; + s_vTimers.push_back(timer); + } +} + +void e2d::ETimerManager::startTimers(const EString & name) +{ + for (auto t : s_vTimers) + { + if (t->getName() == name) + { + t->start(); + } + } +} + +void e2d::ETimerManager::stopTimers(const EString & name) +{ + for (auto t : s_vTimers) + { + if (t->getName() == name) + { + t->stop(); + } + } +} + +void e2d::ETimerManager::delTimers(const EString & name) +{ + EVector::iterator mIter; + for (mIter = s_vTimers.begin(); mIter != s_vTimers.end();) + { + if ((*mIter)->getName() == name) + { + (*mIter)->autoRelease(); + (*mIter)->release(); + mIter = s_vTimers.erase(mIter); + } + else + { + mIter++; + } + } +} + +void e2d::ETimerManager::startAllTimersBindedWith(EScene * pParentScene) +{ + for (auto t : s_vTimers) + { + if (t->getParentScene() == pParentScene) + { + t->start(); + } + } + for (auto child : pParentScene->getChildren()) + { + ETimerManager::startAllTimersBindedWith(child); + } +} + +void e2d::ETimerManager::stopAllTimersBindedWith(EScene * pParentScene) +{ + for (auto t : s_vTimers) + { + if (t->getParentScene() == pParentScene) + { + t->stop(); + } + } + for (auto child : pParentScene->getChildren()) + { + ETimerManager::stopAllTimersBindedWith(child); + } +} + +void e2d::ETimerManager::clearAllTimersBindedWith(EScene * pParentScene) +{ + for (size_t i = 0; i < s_vTimers.size();) + { + auto t = s_vTimers[i]; + if (t->getParentScene() == pParentScene) + { + t->autoRelease(); + t->release(); + s_vTimers.erase(s_vTimers.begin() + i); + } + else + { + i++; + } + } + for (auto child : pParentScene->getChildren()) + { + ETimerManager::clearAllTimersBindedWith(child); + } +} + +void e2d::ETimerManager::startAllTimersBindedWith(ENode * pParentNode) +{ + for (auto t : s_vTimers) + { + if (t->getParentNode() == pParentNode) + { + t->start(); + } + } + for (auto child : pParentNode->getChildren()) + { + ETimerManager::startAllTimersBindedWith(child); + } +} + +void e2d::ETimerManager::stopAllTimersBindedWith(ENode * pParentNode) +{ + for (auto t : s_vTimers) + { + if (t->getParentNode() == pParentNode) + { + t->stop(); + } + } + for (auto child : pParentNode->getChildren()) + { + ETimerManager::startAllTimersBindedWith(child); + } +} + +void e2d::ETimerManager::clearAllTimersBindedWith(ENode * pParentNode) +{ + for (size_t i = 0; i < s_vTimers.size();) + { + auto t = s_vTimers[i]; + if (t->getParentNode() == pParentNode) + { + t->autoRelease(); + t->release(); + s_vTimers.erase(s_vTimers.begin() + i); + } + else + { + i++; + } + } + for (auto child : pParentNode->getChildren()) + { + ETimerManager::clearAllTimersBindedWith(child); + } +} + +void e2d::ETimerManager::_notifyAllTimersBindedWith(EScene * pParentScene) +{ + for (auto t : s_vTimers) + { + if (t->getParentScene() == pParentScene) + t->_notify(); + } + for (auto child : pParentScene->getChildren()) + { + _notifyAllTimersBindedWith(child); + } +} + +void e2d::ETimerManager::_waitAllTimersBindedWith(EScene * pParentScene) +{ + for (auto t : s_vTimers) + { + if (t->getParentScene() == pParentScene) + t->_wait(); + } + for (auto child : pParentScene->getChildren()) + { + _waitAllTimersBindedWith(child); + } +} + +void e2d::ETimerManager::_notifyAllTimersBindedWith(ENode * pParentNode) +{ + for (auto t : s_vTimers) + { + if (t->getParentNode() == pParentNode) + t->_notify(); + } + for (auto child : pParentNode->getChildren()) + { + _notifyAllTimersBindedWith(child); + } +} + +void e2d::ETimerManager::_waitAllTimersBindedWith(ENode * pParentNode) +{ + for (auto t : s_vTimers) + { + if (t->getParentNode() == pParentNode) + t->_notify(); + } + for (auto child : pParentNode->getChildren()) + { + _waitAllTimersBindedWith(child); + } +} + +void e2d::ETimerManager::startAllTimers() +{ + ETimerManager::startAllTimersBindedWith(EApp::getCurrentScene()); +} + +void e2d::ETimerManager::stopAllTimers() +{ + ETimerManager::stopAllTimersBindedWith(EApp::getCurrentScene()); +} + +void e2d::ETimerManager::clearAllTimers() +{ + ETimerManager::clearAllTimersBindedWith(EApp::getCurrentScene()); +} + +void e2d::ETimerManager::TimerProc() +{ + for (auto t : s_vTimers) + { + if (GetInterval(t->m_tLast) >= t->m_nInterval) + { + t->_runCallback(); + t->m_tLast = GetNow(); + } + } +} \ No newline at end of file diff --git a/Easy2D/Win/winbase.cpp b/Easy2D/Win/winbase.cpp index 60ace85c..e130dbbb 100644 --- a/Easy2D/Win/winbase.cpp +++ b/Easy2D/Win/winbase.cpp @@ -1,43 +1,46 @@ #include "winbase.h" +using namespace std::chrono; -HWND hwnd = nullptr; -ID2D1Factory * pDirect2dFactory = nullptr; -ID2D1HwndRenderTarget * pRenderTarget = nullptr; -ID2D1SolidColorBrush * m_pSolidBrush = nullptr; -IWICImagingFactory * pIWICFactory = nullptr; + +HWND s_HWnd = nullptr; +ID2D1Factory * s_pDirect2dFactory = nullptr; +ID2D1HwndRenderTarget * s_pRenderTarget = nullptr; +ID2D1SolidColorBrush * s_pSolidBrush = nullptr; +IWICImagingFactory * s_pIWICFactory = nullptr; +steady_clock::time_point s_tNow; HWND &GetHWnd() { - return hwnd; + return s_HWnd; } ID2D1Factory * &GetFactory() { - return pDirect2dFactory; + return s_pDirect2dFactory; } IWICImagingFactory * &GetImagingFactory() { - if (!pIWICFactory) + if (!s_pIWICFactory) { CoCreateInstance( CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, - reinterpret_cast(&pIWICFactory) + reinterpret_cast(&s_pIWICFactory) ); } - return pIWICFactory; + return s_pIWICFactory; } ID2D1HwndRenderTarget * &GetRenderTarget() { - if (!pRenderTarget) + if (!s_pRenderTarget) { RECT rc; - GetClientRect(hwnd, &rc); + GetClientRect(s_HWnd, &rc); D2D1_SIZE_U size = D2D1::SizeU( rc.right - rc.left, @@ -46,23 +49,32 @@ ID2D1HwndRenderTarget * &GetRenderTarget() // Create a Direct2D render target. HRESULT hr; - hr = pDirect2dFactory->CreateHwndRenderTarget( + hr = s_pDirect2dFactory->CreateHwndRenderTarget( D2D1::RenderTargetProperties(), - D2D1::HwndRenderTargetProperties(hwnd, size), - &pRenderTarget + D2D1::HwndRenderTargetProperties(s_HWnd, size), + &s_pRenderTarget ); ASSERT(SUCCEEDED(hr), "Create Render Target Failed!"); } - return pRenderTarget; + return s_pRenderTarget; } ID2D1SolidColorBrush * &GetSolidColorBrush() { - if (!m_pSolidBrush) + if (!s_pSolidBrush) { - pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::White), &m_pSolidBrush); + s_pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::White), &s_pSolidBrush); } - return m_pSolidBrush; + return s_pSolidBrush; } +steady_clock::time_point &GetNow() +{ + return s_tNow; +} + +long long GetInterval(steady_clock::time_point tLast) +{ + return duration_cast(s_tNow - tLast).count(); +} \ No newline at end of file diff --git a/Easy2D/Win/winbase.h b/Easy2D/Win/winbase.h index 5185704f..117df460 100644 --- a/Easy2D/Win/winbase.h +++ b/Easy2D/Win/winbase.h @@ -1,12 +1,6 @@ #pragma once #include "..\emacros.h" -#include -#include -#include -#include -#pragma comment(lib, "d2d1.lib") -#pragma comment(lib, "dwrite.lib") -#pragma comment(lib, "windowscodecs.lib") +#include #ifndef HINST_THISCOMPONENT @@ -25,6 +19,11 @@ ID2D1SolidColorBrush * &GetSolidColorBrush(); IWICImagingFactory * &GetImagingFactory(); +std::chrono::steady_clock::time_point &GetNow(); + +long long GetInterval(std::chrono::steady_clock::time_point tLast); + + template inline void SafeReleaseInterface( Interface **ppInterfaceToRelease diff --git a/Easy2D/ebase.h b/Easy2D/ebase.h index db301a82..288119ee 100644 --- a/Easy2D/ebase.h +++ b/Easy2D/ebase.h @@ -1,7 +1,6 @@ #pragma once #include "emacros.h" #include "ecommon.h" -#include // Base Classes @@ -22,48 +21,58 @@ public: ~EApp(); - // »ñÈ¡³ÌÐòʵÀý - static EApp * get(); - - // Register the window class and call methods for instantiating drawing resources + // ³õʼ»¯ÓÎÏ·½çÃæ bool init( - e2d::EString title, - e2d::ESize size, - bool bShowConsole = false + const EString &title, /* ´°¿Ú±êÌâ */ + UINT32 width, /* ´°¿Ú¿í¶È */ + UINT32 height, /* ´°¿Ú¸ß¶È */ + bool showConsole = false/* ÊÇ·ñÏÔʾ¿ØÖÆÌ¨ */ ); - // Register the window class and call methods for instantiating drawing resources + // ³õʼ»¯ÓÎÏ·½çÃæ bool init( - e2d::EString title, - UINT32 width, - UINT32 height, - bool bShowConsole = false + const EString &title, /* ´°¿Ú±êÌâ */ + UINT32 width, /* ´°¿Ú¿í¶È */ + UINT32 height, /* ´°¿Ú¸ß¶È */ + int windowStyle, /* ´°¿ÚÑùʽ */ + bool showConsole = false/* ÊÇ·ñÏÔʾ¿ØÖÆÌ¨ */ ); // Æô¶¯³ÌÐò void run(); - // Ð޸Ĵ°¿Ú´óС - static void setWindowSize( - int width, - int height + // Ô¤Éè»­ÃæÖ¡Êý + void setFPS( + UINT32 fps + ); + + // Í˳ö³ÌÐòʱµÄÖ´ÐгÌÐò + virtual bool onExit(); + + // ÊÍ·ÅËùÓÐÄÚ´æ×ÊÔ´ + void free(); + + // »ñÈ¡³ÌÐòʵÀý + static EApp * get(); + + // ÏÔʾ»òÒþ²Ø¿ØÖÆÌ¨£¨Ä¬ÈÏÒþ²Ø£© + static void showConsole( + bool show ); // Ð޸Ĵ°¿Ú´óС static void setWindowSize( - e2d::ESize size + UINT32 width, + UINT32 height ); // ÉèÖô°¿Ú±êÌâ static void setWindowTitle( - e2d::EString title + const EString &title ); // »ñÈ¡´°¿Ú±êÌâ - static e2d::EString getTitle(); - - // »ñÈ¡´°¿Ú´óС - static e2d::ESize getSize(); + static EString getTitle(); // »ñÈ¡´°¿Ú¿í¶È static UINT32 getWidth(); @@ -74,7 +83,7 @@ public: // Çл»³¡¾° static void enterScene( EScene * scene, - bool save = true + bool saveCurrentScene = true ); // ·µ»ØÉÏÒ»³¡¾° @@ -87,26 +96,31 @@ public: static EScene * getCurrentScene(); // »ñÈ¡ AppName - static e2d::EString getAppName(); + static EString getAppName(); // ÉèÖà AppName static void setAppName( - e2d::EString appname + const EString &appname ); // Ð޸Ĵ°¿Ú±³¾°É« static void setBkColor( - EColor::Enum color + EColor color ); - // ÊÍ·ÅËùÓÐÄÚ´æ×ÊÔ´ - static void free(); + // ÉèÖóÌÐòÊÇ·ñÏìÓ¦ÊäÈë·¨ + static void setKeyboardLayoutEnable( + bool value + ); - // ¹Ø±Õ´°¿Ú - static void close(); + // »ñÈ¡´°¿Ú¾ä±ú + static HWND getHWnd(); + + // Òþ²Ø´°¿Ú + static void closeWindow(); // ÏÔʾ´°¿Ú - static void show(); + static void showWindow(); // ÖÕÖ¹³ÌÐò static void quit(); @@ -115,31 +129,34 @@ public: static void end(); protected: - // Initialize device-independent resources. + // ´´½¨É豸ÎÞ¹Ø×ÊÔ´ HRESULT _createDeviceIndependentResources(); - // Initialize device-dependent resources. + // ´´½¨É豸Ïà¹Ø×ÊÔ´ HRESULT _createDeviceResources(); - // Release device-dependent resource. + // ÊÍ·ÅÉ豸Ïà¹Ø×ÊÔ´ void _discardDeviceResources(); + // ÓÎÏ·Ö÷Ñ­»· void _mainLoop(); + // ÓÎÏ·¿ØÖÆÁ÷³Ì void _onControl(); - // Draw content. - void _onRender(); + // äÖȾÓÎÏ·»­Ãæ + bool _onRender(); + // ½øÈëÏÂÒ»³¡¾° void _enterNextScene(); - // ReSize the render target. + // ÖØ¶¨ render target ´óС void _onResize( - UINT width, - UINT height + UINT32 width, + UINT32 height ); - // The windows procedure. + // ´°¿Ú³ÌÐò static LRESULT CALLBACK WndProc( HWND hWnd, UINT message, @@ -151,10 +168,11 @@ protected: bool m_bRunning; EString m_sTitle; EString m_sAppName; - EColor::Enum m_ClearColor; + EColor m_ClearColor; + LONGLONG nAnimationInterval; - EScene * m_pCurrentScene; - EScene * m_pNextScene; + EScene * m_pCurrentScene; + EScene * m_pNextScene; }; @@ -173,28 +191,34 @@ public: // ÖØÐ´Õâ¸öº¯Êý£¬Ëü½«ÔÚÀ뿪Õâ¸ö³¡¾°Ê±×Ô¶¯Ö´ÐÐ virtual void onExit(); - // Ìí¼Ó×Ó³ÉÔ±µ½³¡¾° + // Ìí¼Ó×ӽڵ㵽³¡¾° void add( - e2d::ENode * child, + ENode * child, int zOrder = 0 ); - // ɾ³ý×Ó³ÉÔ± + // ɾ³ý×Ó½Úµã bool remove( - e2d::ENode * child, - bool autoRelease = true + ENode * child, + bool release = false + ); + + // ¸ù¾ÝÃû³ÆÉ¾³ý×Ó½Úµã + void remove( + const EString &childName, + bool release = false ); // »ñÈ¡ËùÓÐ×Ó½Úµã - std::vector &getChildren(); + EVector &getChildren(); // »ñÈ¡×Ó½ÚµãÊýÁ¿ size_t getChildrenCount() const; // ¸ù¾ÝÃû³Æ»ñÈ¡×Ó½Úµã ENode * getChild( - EString childName - ) const; + const EString &childName + ); // Çå¿ÕËùÓÐ×Ó³ÉÔ± void clearAllChildren(); @@ -209,9 +233,6 @@ protected: // äÖȾ³¡¾°»­Ãæ void _onRender(); - // ×Ó½ÚµãÅÅÐò - void _sortChildren(); - // ½øÈ볡¾°Ê±Ðèµ÷Óøú¯Êý virtual void _onEnter(); @@ -221,7 +242,7 @@ protected: protected: bool m_bSortNeeded; bool m_bWillSave; - std::vector m_vChildren; + ENode * const m_Root; }; diff --git a/Easy2D/ecommon.h b/Easy2D/ecommon.h index 1d226fd5..3a9e14f8 100644 --- a/Easy2D/ecommon.h +++ b/Easy2D/ecommon.h @@ -1,7 +1,7 @@ #pragma once #include #include -#include +#include #include namespace e2d @@ -9,18 +9,46 @@ namespace e2d typedef std::wstring EString; +template +using EVector = std::vector; -typedef CSize ESize; -typedef CPoint EPoint; -typedef EPoint EVector; -typedef CRect ERect; - - -typedef struct +struct EPoint { - FLOAT width; - FLOAT height; -} ESize_F; + EPoint() + { + x = 0; + y = 0; + } + + EPoint(float x, float y) + { + this->x = x; + this->y = y; + } + + float x; + float y; +}; + +typedef EPoint EVec; + +struct ESize +{ + ESize() + { + width = 0; + height = 0; + } + + ESize(float width, float height) + { + this->width = width; + this->height = height; + } + + float width; + float height; +}; typedef std::function KEY_LISTENER_CALLBACK; @@ -30,155 +58,152 @@ 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; +typedef std::function TIMER_CALLBACK; -class EColor + + +enum EColor { -public: - - enum Enum - { - AliceBlue = 0xF0F8FF, - AntiqueWhite = 0xFAEBD7, - Aqua = 0x00FFFF, - Aquamarine = 0x7FFFD4, - Azure = 0xF0FFFF, - Beige = 0xF5F5DC, - Bisque = 0xFFE4C4, - Black = 0x000000, - BlanchedAlmond = 0xFFEBCD, - Blue = 0x0000FF, - BlueViolet = 0x8A2BE2, - Brown = 0xA52A2A, - BurlyWood = 0xDEB887, - CadetBlue = 0x5F9EA0, - Chartreuse = 0x7FFF00, - Chocolate = 0xD2691E, - Coral = 0xFF7F50, - CornflowerBlue = 0x6495ED, - Cornsilk = 0xFFF8DC, - Crimson = 0xDC143C, - Cyan = 0x00FFFF, - DarkBlue = 0x00008B, - DarkCyan = 0x008B8B, - DarkGoldenrod = 0xB8860B, - DarkGray = 0xA9A9A9, - DarkGreen = 0x006400, - DarkKhaki = 0xBDB76B, - DarkMagenta = 0x8B008B, - DarkOliveGreen = 0x556B2F, - DarkOrange = 0xFF8C00, - DarkOrchid = 0x9932CC, - DarkRed = 0x8B0000, - DarkSalmon = 0xE9967A, - DarkSeaGreen = 0x8FBC8F, - DarkSlateBlue = 0x483D8B, - DarkSlateGray = 0x2F4F4F, - DarkTurquoise = 0x00CED1, - DarkViolet = 0x9400D3, - DeepPink = 0xFF1493, - DeepSkyBlue = 0x00BFFF, - DimGray = 0x696969, - DodgerBlue = 0x1E90FF, - Firebrick = 0xB22222, - FloralWhite = 0xFFFAF0, - ForestGreen = 0x228B22, - Fuchsia = 0xFF00FF, - Gainsboro = 0xDCDCDC, - GhostWhite = 0xF8F8FF, - Gold = 0xFFD700, - Goldenrod = 0xDAA520, - Gray = 0x808080, - Green = 0x008000, - GreenYellow = 0xADFF2F, - Honeydew = 0xF0FFF0, - HotPink = 0xFF69B4, - IndianRed = 0xCD5C5C, - Indigo = 0x4B0082, - Ivory = 0xFFFFF0, - Khaki = 0xF0E68C, - Lavender = 0xE6E6FA, - LavenderBlush = 0xFFF0F5, - LawnGreen = 0x7CFC00, - LemonChiffon = 0xFFFACD, - LightBlue = 0xADD8E6, - LightCoral = 0xF08080, - LightCyan = 0xE0FFFF, - LightGoldenrodYellow = 0xFAFAD2, - LightGreen = 0x90EE90, - LightGray = 0xD3D3D3, - LightPink = 0xFFB6C1, - LightSalmon = 0xFFA07A, - LightSeaGreen = 0x20B2AA, - LightSkyBlue = 0x87CEFA, - LightSlateGray = 0x778899, - LightSteelBlue = 0xB0C4DE, - LightYellow = 0xFFFFE0, - Lime = 0x00FF00, - LimeGreen = 0x32CD32, - Linen = 0xFAF0E6, - Magenta = 0xFF00FF, - Maroon = 0x800000, - MediumAquamarine = 0x66CDAA, - MediumBlue = 0x0000CD, - MediumOrchid = 0xBA55D3, - MediumPurple = 0x9370DB, - MediumSeaGreen = 0x3CB371, - MediumSlateBlue = 0x7B68EE, - MediumSpringGreen = 0x00FA9A, - MediumTurquoise = 0x48D1CC, - MediumVioletRed = 0xC71585, - MidnightBlue = 0x191970, - MintCream = 0xF5FFFA, - MistyRose = 0xFFE4E1, - Moccasin = 0xFFE4B5, - NavajoWhite = 0xFFDEAD, - Navy = 0x000080, - OldLace = 0xFDF5E6, - Olive = 0x808000, - OliveDrab = 0x6B8E23, - Orange = 0xFFA500, - OrangeRed = 0xFF4500, - Orchid = 0xDA70D6, - PaleGoldenrod = 0xEEE8AA, - PaleGreen = 0x98FB98, - PaleTurquoise = 0xAFEEEE, - PaleVioletRed = 0xDB7093, - PapayaWhip = 0xFFEFD5, - PeachPuff = 0xFFDAB9, - Peru = 0xCD853F, - Pink = 0xFFC0CB, - Plum = 0xDDA0DD, - PowderBlue = 0xB0E0E6, - Purple = 0x800080, - Red = 0xFF0000, - RosyBrown = 0xBC8F8F, - RoyalBlue = 0x4169E1, - SaddleBrown = 0x8B4513, - Salmon = 0xFA8072, - SandyBrown = 0xF4A460, - SeaGreen = 0x2E8B57, - SeaShell = 0xFFF5EE, - Sienna = 0xA0522D, - Silver = 0xC0C0C0, - SkyBlue = 0x87CEEB, - SlateBlue = 0x6A5ACD, - SlateGray = 0x708090, - Snow = 0xFFFAFA, - SpringGreen = 0x00FF7F, - SteelBlue = 0x4682B4, - Tan = 0xD2B48C, - Teal = 0x008080, - Thistle = 0xD8BFD8, - Tomato = 0xFF6347, - Turquoise = 0x40E0D0, - Violet = 0xEE82EE, - Wheat = 0xF5DEB3, - White = 0xFFFFFF, - WhiteSmoke = 0xF5F5F5, - Yellow = 0xFFFF00, - YellowGreen = 0x9ACD32, - }; - + AliceBlue = 0xF0F8FF, + AntiqueWhite = 0xFAEBD7, + Aqua = 0x00FFFF, + Aquamarine = 0x7FFFD4, + Azure = 0xF0FFFF, + Beige = 0xF5F5DC, + Bisque = 0xFFE4C4, + Black = 0x000000, + BlanchedAlmond = 0xFFEBCD, + Blue = 0x0000FF, + BlueViolet = 0x8A2BE2, + Brown = 0xA52A2A, + BurlyWood = 0xDEB887, + CadetBlue = 0x5F9EA0, + Chartreuse = 0x7FFF00, + Chocolate = 0xD2691E, + Coral = 0xFF7F50, + CornflowerBlue = 0x6495ED, + Cornsilk = 0xFFF8DC, + Crimson = 0xDC143C, + Cyan = 0x00FFFF, + DarkBlue = 0x00008B, + DarkCyan = 0x008B8B, + DarkGoldenrod = 0xB8860B, + DarkGray = 0xA9A9A9, + DarkGreen = 0x006400, + DarkKhaki = 0xBDB76B, + DarkMagenta = 0x8B008B, + DarkOliveGreen = 0x556B2F, + DarkOrange = 0xFF8C00, + DarkOrchid = 0x9932CC, + DarkRed = 0x8B0000, + DarkSalmon = 0xE9967A, + DarkSeaGreen = 0x8FBC8F, + DarkSlateBlue = 0x483D8B, + DarkSlateGray = 0x2F4F4F, + DarkTurquoise = 0x00CED1, + DarkViolet = 0x9400D3, + DeepPink = 0xFF1493, + DeepSkyBlue = 0x00BFFF, + DimGray = 0x696969, + DodgerBlue = 0x1E90FF, + Firebrick = 0xB22222, + FloralWhite = 0xFFFAF0, + ForestGreen = 0x228B22, + Fuchsia = 0xFF00FF, + Gainsboro = 0xDCDCDC, + GhostWhite = 0xF8F8FF, + Gold = 0xFFD700, + Goldenrod = 0xDAA520, + Gray = 0x808080, + Green = 0x008000, + GreenYellow = 0xADFF2F, + Honeydew = 0xF0FFF0, + HotPink = 0xFF69B4, + IndianRed = 0xCD5C5C, + Indigo = 0x4B0082, + Ivory = 0xFFFFF0, + Khaki = 0xF0E68C, + Lavender = 0xE6E6FA, + LavenderBlush = 0xFFF0F5, + LawnGreen = 0x7CFC00, + LemonChiffon = 0xFFFACD, + LightBlue = 0xADD8E6, + LightCoral = 0xF08080, + LightCyan = 0xE0FFFF, + LightGoldenrodYellow = 0xFAFAD2, + LightGreen = 0x90EE90, + LightGray = 0xD3D3D3, + LightPink = 0xFFB6C1, + LightSalmon = 0xFFA07A, + LightSeaGreen = 0x20B2AA, + LightSkyBlue = 0x87CEFA, + LightSlateGray = 0x778899, + LightSteelBlue = 0xB0C4DE, + LightYellow = 0xFFFFE0, + Lime = 0x00FF00, + LimeGreen = 0x32CD32, + Linen = 0xFAF0E6, + Magenta = 0xFF00FF, + Maroon = 0x800000, + MediumAquamarine = 0x66CDAA, + MediumBlue = 0x0000CD, + MediumOrchid = 0xBA55D3, + MediumPurple = 0x9370DB, + MediumSeaGreen = 0x3CB371, + MediumSlateBlue = 0x7B68EE, + MediumSpringGreen = 0x00FA9A, + MediumTurquoise = 0x48D1CC, + MediumVioletRed = 0xC71585, + MidnightBlue = 0x191970, + MintCream = 0xF5FFFA, + MistyRose = 0xFFE4E1, + Moccasin = 0xFFE4B5, + NavajoWhite = 0xFFDEAD, + Navy = 0x000080, + OldLace = 0xFDF5E6, + Olive = 0x808000, + OliveDrab = 0x6B8E23, + Orange = 0xFFA500, + OrangeRed = 0xFF4500, + Orchid = 0xDA70D6, + PaleGoldenrod = 0xEEE8AA, + PaleGreen = 0x98FB98, + PaleTurquoise = 0xAFEEEE, + PaleVioletRed = 0xDB7093, + PapayaWhip = 0xFFEFD5, + PeachPuff = 0xFFDAB9, + Peru = 0xCD853F, + Pink = 0xFFC0CB, + Plum = 0xDDA0DD, + PowderBlue = 0xB0E0E6, + Purple = 0x800080, + Red = 0xFF0000, + RosyBrown = 0xBC8F8F, + RoyalBlue = 0x4169E1, + SaddleBrown = 0x8B4513, + Salmon = 0xFA8072, + SandyBrown = 0xF4A460, + SeaGreen = 0x2E8B57, + SeaShell = 0xFFF5EE, + Sienna = 0xA0522D, + Silver = 0xC0C0C0, + SkyBlue = 0x87CEEB, + SlateBlue = 0x6A5ACD, + SlateGray = 0x708090, + Snow = 0xFFFAFA, + SpringGreen = 0x00FF7F, + SteelBlue = 0x4682B4, + Tan = 0xD2B48C, + Teal = 0x008080, + Thistle = 0xD8BFD8, + Tomato = 0xFF6347, + Turquoise = 0x40E0D0, + Violet = 0xEE82EE, + Wheat = 0xF5DEB3, + White = 0xFFFFFF, + WhiteSmoke = 0xF5F5F5, + Yellow = 0xFFFF00, + YellowGreen = 0x9ACD32, }; } \ No newline at end of file diff --git a/Easy2D/emacros.h b/Easy2D/emacros.h index 8d86d282..8bc0845f 100644 --- a/Easy2D/emacros.h +++ b/Easy2D/emacros.h @@ -26,6 +26,13 @@ #include #include #include +#include +#include +#include +#include +#pragma comment(lib, "d2d1.lib") +#pragma comment(lib, "dwrite.lib") +#pragma comment(lib, "windowscodecs.lib") #ifndef ASSERT_IF diff --git a/Easy2D/emsg.h b/Easy2D/emsg.h index 59cbee0b..166a979e 100644 --- a/Easy2D/emsg.h +++ b/Easy2D/emsg.h @@ -30,10 +30,10 @@ public: }; // »ñÈ¡Êó±êºá×ø±ê - static DWORD getX(); + static DWORD getPosX(); // »ñÈ¡Êó±ê×Ý×ø±ê - static DWORD getY(); + static DWORD getPosY(); // »ñÈ¡Êó±ê×ø±ê static EPoint getPos(); @@ -67,7 +67,7 @@ protected: // °´¼üÏûÏ¢ -class EKeyMsg +class EKeyboardMsg { friend EMsgManager; @@ -147,7 +147,7 @@ public: EListener(); EListener( - EString name + const EString &name ); // »ñÈ¡¼àÌýÆ÷״̬ @@ -162,12 +162,6 @@ public: // Í£Ö¹¼àÌý void stop(); - // ½øÈëµÈ´ý״̬ - void wait(); - - // »½ÐÑ - void notify(); - // »ñÈ¡¼àÌýÆ÷Ãû³Æ EString getName() const; @@ -179,7 +173,7 @@ public: // ÉèÖüàÌýÆ÷Ãû³Æ void setName( - EString name + const EString &name ); // °ó¶¨¼àÌýÆ÷µ½³¡¾° @@ -192,6 +186,16 @@ public: ENode * pParentNode ) = 0; +protected: + // ½øÈëµÈ´ý״̬ + void _wait(); + + // »½ÐÑ + void _notify(); + + // Ö´ÐмàÌýÆ÷»Øµ÷º¯Êý + virtual void _runCallback() = 0; + protected: EString m_sName; bool m_bRunning; @@ -205,11 +209,13 @@ protected: class EMouseListener : public EListener { + friend EMsgManager; + public: EMouseListener(); EMouseListener( - EString name + const EString &name ); EMouseListener( @@ -217,13 +223,10 @@ public: ); EMouseListener( - EString name, + const EString &name, const MOUSE_LISTENER_CALLBACK &callback ); - // Ö´ÐмàÌýÆ÷»Øµ÷º¯Êý - virtual void runCallback(); - // ÉèÖüàÌýÆ÷»Øµ÷º¯Êý void setCallback( const MOUSE_LISTENER_CALLBACK &callback @@ -240,7 +243,11 @@ public: ) override; protected: - MOUSE_LISTENER_CALLBACK m_callback; + // Ö´ÐмàÌýÆ÷»Øµ÷º¯Êý + virtual void _runCallback() override; + +protected: + MOUSE_LISTENER_CALLBACK m_Callback; }; @@ -252,7 +259,7 @@ public: EMousePressListener(); EMousePressListener( - EString name + const EString &name ); EMousePressListener( @@ -260,20 +267,21 @@ public: ); EMousePressListener( - EString name, + const EString &name, const MOUSE_PRESS_LISTENER_CALLBACK &callback ); - // Ö´ÐмàÌýÆ÷»Øµ÷º¯Êý - virtual void runCallback() override; - // ÉèÖüàÌýÆ÷»Øµ÷º¯Êý void setCallback( const MOUSE_PRESS_LISTENER_CALLBACK &callback ); protected: - MOUSE_PRESS_LISTENER_CALLBACK m_callback; + // Ö´ÐмàÌýÆ÷»Øµ÷º¯Êý + virtual void _runCallback() override; + +protected: + MOUSE_PRESS_LISTENER_CALLBACK m_Callback; }; @@ -285,7 +293,7 @@ public: EMouseClickListener(); EMouseClickListener( - EString name + const EString &name ); EMouseClickListener( @@ -293,21 +301,22 @@ public: ); EMouseClickListener( - EString name, + const EString &name, const MOUSE_CLICK_LISTENER_CALLBACK &callback ); - // Ö´ÐмàÌýÆ÷»Øµ÷º¯Êý - virtual void runCallback() override; - // ÉèÖüàÌýÆ÷»Øµ÷º¯Êý void setCallback( const MOUSE_CLICK_LISTENER_CALLBACK &callback ); +protected: + // Ö´ÐмàÌýÆ÷»Øµ÷º¯Êý + virtual void _runCallback() override; + protected: bool m_bPressed; - MOUSE_CLICK_LISTENER_CALLBACK m_callback; + MOUSE_CLICK_LISTENER_CALLBACK m_Callback; }; @@ -319,7 +328,7 @@ public: EMouseDoubleClickListener(); EMouseDoubleClickListener( - EString name + const EString &name ); EMouseDoubleClickListener( @@ -327,21 +336,22 @@ public: ); EMouseDoubleClickListener( - EString name, + const EString &name, const MOUSE_DBLCLK_LISTENER_CALLBACK &callback ); - // Ö´ÐмàÌýÆ÷»Øµ÷º¯Êý - virtual void runCallback() override; - // ÉèÖüàÌýÆ÷»Øµ÷º¯Êý void setCallback( const MOUSE_DBLCLK_LISTENER_CALLBACK &callback ); +protected: + // Ö´ÐмàÌýÆ÷»Øµ÷º¯Êý + virtual void _runCallback() override; + protected: bool m_bPressed; - MOUSE_DBLCLK_LISTENER_CALLBACK m_callback; + MOUSE_DBLCLK_LISTENER_CALLBACK m_Callback; }; @@ -353,7 +363,7 @@ public: EMouseDragListener(); EMouseDragListener( - EString name + const EString &name ); EMouseDragListener( @@ -361,21 +371,22 @@ public: ); EMouseDragListener( - EString name, + const EString &name, const MOUSE_DRAG_LISTENER_CALLBACK &callback ); - // Ö´ÐмàÌýÆ÷»Øµ÷º¯Êý - virtual void runCallback() override; - // ÉèÖüàÌýÆ÷»Øµ÷º¯Êý void setCallback( const MOUSE_DRAG_LISTENER_CALLBACK &callback ); +protected: + // Ö´ÐмàÌýÆ÷»Øµ÷º¯Êý + virtual void _runCallback() override; + protected: EPoint m_Begin; - MOUSE_DRAG_LISTENER_CALLBACK m_callback; + MOUSE_DRAG_LISTENER_CALLBACK m_Callback; }; @@ -383,11 +394,13 @@ protected: class EKeyboardListener : public EListener { + friend EMsgManager; + public: EKeyboardListener(); EKeyboardListener( - EString name + const EString &name ); EKeyboardListener( @@ -395,13 +408,10 @@ public: ); EKeyboardListener( - EString name, + const EString &name, const KEY_LISTENER_CALLBACK &callback ); - // Ö´ÐмàÌýÆ÷»Øµ÷º¯Êý - virtual void runCallback(); - // ÉèÖüàÌýÆ÷»Øµ÷º¯Êý void setCallback( const KEY_LISTENER_CALLBACK &callback @@ -418,32 +428,39 @@ public: ) override; protected: - KEY_LISTENER_CALLBACK m_callback; + // Ö´ÐмàÌýÆ÷»Øµ÷º¯Êý + virtual void _runCallback() override; + +protected: + KEY_LISTENER_CALLBACK m_Callback; }; // °´¼ü°´ÏÂÏûÏ¢¼àÌý -class EKeyPressListener : +class EKeyboardPressListener : public EKeyboardListener { + friend EMsgManager; + public: - EKeyPressListener(); + EKeyboardPressListener(); - EKeyPressListener( - EString name + EKeyboardPressListener( + const EString &name ); - EKeyPressListener( + EKeyboardPressListener( const KEY_LISTENER_CALLBACK &callback ); - EKeyPressListener( - EString name, + EKeyboardPressListener( + const EString &name, const KEY_LISTENER_CALLBACK &callback ); +protected: // Ö´ÐмàÌýÆ÷»Øµ÷º¯Êý - virtual void runCallback() override; + virtual void _runCallback() override; }; @@ -451,122 +468,192 @@ public: class EMsgManager { friend EApp; + friend EScene; + friend ENode; public: // °ó¶¨Êó±êÏûÏ¢¼àÌýÆ÷µ½³¡¾° - static void bindListenerWith( + static void bindListener( EMouseListener * listener, EScene * pParentScene ); - // °ó¶¨Êó±êÏûÏ¢¼àÌýÆ÷µ½³¡¾° - static void bindListenerWith( - EKeyboardListener * listener, - EScene * pParentScene - ); - - // °ó¶¨°´¼üÏûÏ¢¼àÌýÆ÷µ½½Úµã - static void bindListenerWith( + // °ó¶¨Êó±êÏûÏ¢¼àÌýÆ÷µ½½Úµã + static void bindListener( EMouseListener * listener, ENode * pParentNode ); - // °ó¶¨°´¼üÏûÏ¢¼àÌýÆ÷µ½½Úµã - static void bindListenerWith( - EKeyboardListener * listener, + // Æô¶¯¾ßÓÐÏàͬÃû³ÆµÄÊó±êÏûÏ¢¼àÌýÆ÷ + static void startMouseListeners( + const EString &name + ); + + // Í£Ö¹¾ßÓÐÏàͬÃû³ÆµÄÊó±êÏûÏ¢¼àÌýÆ÷ + static void stopMouseListeners( + const EString &name + ); + + // ɾ³ý¾ßÓÐÏàͬÃû³ÆµÄÊó±êÏûÏ¢¼àÌýÆ÷ + static void delMouseListeners( + const EString &name + ); + + // Æô¶¯°ó¶¨ÔÚ³¡¾°¼°Æä×Ó½ÚµãÉϵÄËùÓÐÊó±êÏûÏ¢¼àÌýÆ÷ + static void startAllMouseListenersBindedWith( + EScene * pParentScene + ); + + // Í£Ö¹°ó¶¨ÔÚ³¡¾°¼°Æä×Ó½ÚµãÉϵÄËùÓÐÊó±êÏûÏ¢¼àÌýÆ÷ + static void stopAllMouseListenersBindedWith( + EScene * pParentScene + ); + + // Çå³ý°ó¶¨ÔÚ³¡¾°¼°Æä×Ó½ÚµãÉϵÄËùÓÐÊó±êÏûÏ¢¼àÌýÆ÷ + static void clearAllMouseListenersBindedWith( + EScene * pParentScene + ); + + // Æô¶¯°ó¶¨ÔÚ½ÚµãÉϵÄËùÓÐÊó±êÏûÏ¢¼àÌýÆ÷ + static void startAllMouseListenersBindedWith( ENode * pParentNode ); - // Æô¶¯¾ßÓÐÏàͬÃû³ÆµÄ¼àÌýÆ÷ - static void startListener( - EString name + // Í£Ö¹°ó¶¨ÔÚ½ÚµãÉϵÄËùÓÐÊó±êÏûÏ¢¼àÌýÆ÷ + static void stopAllMouseListenersBindedWith( + ENode * pParentNode ); - // Í£Ö¹¾ßÓÐÏàͬÃû³ÆµÄ¼àÌýÆ÷ - static void stopListener( - EString name - ); - - // ɾ³ý¾ßÓÐÏàͬÃû³ÆµÄ¼àÌýÆ÷ - static void delListener( - EString name + // Çå³ý°ó¶¨ÔÚ½ÚµãÉϵÄËùÓÐÊó±êÏûÏ¢¼àÌýÆ÷ + static void clearAllMouseListenersBindedWith( + ENode * pParentNode ); // Æô¶¯ËùÓÐÊó±êÏûÏ¢¼àÌýÆ÷ - static void startAllMouseListener(); + static void startAllMouseListeners(); // Í£Ö¹ËùÓÐÊó±êÏûÏ¢¼àÌýÆ÷ - static void stopAllMouseListener(); + static void stopAllMouseListeners(); // Çå³ýËùÓÐÊó±êÏûÏ¢¼àÌýÆ÷ static void clearAllMouseListeners(); + // °ó¶¨°´¼üÏûÏ¢¼àÌýÆ÷µ½³¡¾° + static void bindListener( + EKeyboardListener * listener, + EScene * pParentScene + ); + + // °ó¶¨°´¼üÏûÏ¢¼àÌýÆ÷µ½½Úµã + static void bindListener( + EKeyboardListener * listener, + ENode * pParentNode + ); + + // Æô¶¯Ãû³ÆÏàͬµÄ°´¼üÏûÏ¢¼àÌýÆ÷ + static void startKeyboardListeners( + const EString &name + ); + + // Í£Ö¹Ãû³ÆÏàͬµÄ°´¼üÏûÏ¢¼àÌýÆ÷ + static void stopKeyboardListeners( + const EString &name + ); + + // ɾ³ýÃû³ÆÏàͬµÄ°´¼üÏûÏ¢¼àÌýÆ÷ + static void delKeyboardListeners( + const EString &name + ); + + // Æô¶¯°ó¶¨ÔÚ³¡¾°¼°Æä×Ó½ÚµãÉϵÄËùÓа´¼üÏûÏ¢¼àÌýÆ÷ + static void startAllKeyboardListenersBindedWith( + EScene * pParentScene + ); + + // Í£Ö¹°ó¶¨ÔÚ³¡¾°¼°Æä×Ó½ÚµãÉϵÄËùÓа´¼üÏûÏ¢¼àÌýÆ÷ + static void stopAllKeyboardListenersBindedWith( + EScene * pParentScene + ); + + // Çå³ý°ó¶¨ÔÚ³¡¾°¼°Æä×Ó½ÚµãÉϵÄËùÓа´¼üÏûÏ¢¼àÌýÆ÷ + static void clearAllKeyboardListenersBindedWith( + EScene * pParentScene + ); + + // Æô¶¯°ó¶¨ÔÚ½ÚµãÉϵÄËùÓа´¼üÏûÏ¢¼àÌýÆ÷ + static void startAllKeyboardListenersBindedWith( + ENode * pParentNode + ); + + // Í£Ö¹°ó¶¨ÔÚ½ÚµãÉϵÄËùÓа´¼üÏûÏ¢¼àÌýÆ÷ + static void stopAllKeyboardListenersBindedWith( + ENode * pParentNode + ); + + // Çå³ý°ó¶¨ÔÚ½ÚµãÉϵÄËùÓа´¼üÏûÏ¢¼àÌýÆ÷ + static void clearAllKeyboardListenersBindedWith( + ENode * pParentNode + ); + // Æô¶¯ËùÓа´¼üÏûÏ¢¼àÌýÆ÷ - static void startAllKeyboardListener(); + static void startAllKeyboardListeners(); // Í£Ö¹ËùÓа´¼üÏûÏ¢¼àÌýÆ÷ - static void stopAllKeyboardListener(); + static void stopAllKeyboardListeners(); // Çå³ýËùÓа´¼üÏûÏ¢¼àÌýÆ÷ static void clearAllKeyboardListeners(); - // Æô¶¯°ó¶¨ÔÚ³¡¾°ÉϵÄËùÓÐÊó±êÏûÏ¢¼àÌýÆ÷ - static void startAllMouseListenersBindWithScene( +private: + // ¹ÒÆð°ó¶¨ÔÚ³¡¾°ÉϵÄËùÓÐÊó±êÏûÏ¢¼àÌýÆ÷ + static void _waitAllMouseListenersBindedWith( EScene * pParentScene ); // ÖØÆô°ó¶¨ÔÚ³¡¾°ÉϵÄËùÓÐÊó±êÏûÏ¢¼àÌýÆ÷ - static void stopAllMouseListenersBindWithScene( + static void _notifyAllMouseListenersBindedWith( EScene * pParentScene ); - // Æô¶¯°ó¶¨ÔÚ³¡¾°ÉϵÄËùÓа´¼üÏûÏ¢¼àÌýÆ÷ - static void startAllKeyboardListenersBindWithScene( - EScene * pParentScene - ); - - // ÖØÆô°ó¶¨ÔÚ³¡¾°ÉϵÄËùÓа´¼üÏûÏ¢¼àÌýÆ÷ - static void stopAllKeyboardListenersBindWithScene( - EScene * pParentScene - ); - - // ¹ÒÆð°ó¶¨ÔÚ³¡¾°ÉϵÄËùÓмàÌýÆ÷ - static void waitAllListenersBindWithScene( - EScene * pParentScene - ); - - // ÖØÆô°ó¶¨ÔÚ³¡¾°ÉϵÄËùÓмàÌýÆ÷ - static void notifyAllListenersBindWithScene( - EScene * pParentScene - ); - - // Çå¿Õ°ó¶¨ÔÚ³¡¾°ÉϵÄËùÓмàÌýÆ÷ - static void clearAllListenersBindWithScene( - EScene * pParentScene - ); - - // ¹ÒÆð°ó¶¨ÔÚ½ÚµãÉϵÄËùÓмàÌýÆ÷ - static void waitAllListenersBindWithNode( + // ¹ÒÆð°ó¶¨ÔÚ½ÚµãÉϵÄËùÓÐÊó±êÏûÏ¢¼àÌýÆ÷ + static void _waitAllMouseListenersBindedWith( ENode * pParentNode ); - // ÖØÆô°ó¶¨ÔÚ½ÚµãÉϵÄËùÓмàÌýÆ÷ - static void notifyAllListenersBindWithNode( + // ÖØÆô°ó¶¨ÔÚ½ÚµãÉϵÄËùÓÐÊó±êÏûÏ¢¼àÌýÆ÷ + static void _notifyAllMouseListenersBindedWith( ENode * pParentNode ); - // Çå¿Õ°ó¶¨ÔÚ½ÚµãÉϵÄËùÓмàÌýÆ÷ - static void clearAllListenersBindWithNode( + + // ¹ÒÆð°ó¶¨ÔÚ³¡¾°¼°Æä×Ó½ÚµãÉϵÄËùÓа´¼ü¼àÌýÆ÷ + static void _waitAllKeyboardListenersBindedWith( + EScene * pParentScene + ); + + // ÖØÆô°ó¶¨ÔÚ³¡¾°¼°Æä×Ó½ÚµãÉϵÄËùÓа´¼ü¼àÌýÆ÷ + static void _notifyAllKeyboardListenersBindedWith( + EScene * pParentScene + ); + + // ¹ÒÆð°ó¶¨ÔÚ½ÚµãÉϵÄËùÓа´¼ü¼àÌýÆ÷ + static void _waitAllKeyboardListenersBindedWith( ENode * pParentNode ); -private: + // ÖØÆô°ó¶¨ÔÚ½ÚµãÉϵÄËùÓа´¼ü¼àÌýÆ÷ + static void _notifyAllKeyboardListenersBindedWith( + ENode * pParentNode + ); + + // Êó±êÏûÏ¢³ÌÐò static void MouseProc( UINT message, WPARAM wParam, LPARAM lParam ); + // °´¼üÏûÏ¢³ÌÐò static void KeyboardProc( UINT message, WPARAM wParam, diff --git a/Easy2D/enodes.h b/Easy2D/enodes.h index 40483a3e..0ea52c96 100644 --- a/Easy2D/enodes.h +++ b/Easy2D/enodes.h @@ -14,22 +14,34 @@ public: ENode(); ENode( - EString name + const EString & name ); virtual ~ENode(); - // ½ÚµãÊÇ·ñÏÔʾ + // ÖØÐ´Õâ¸öº¯Êý£¬Ëü½«ÔÚ½Úµã½øÈ볡¾°Ê±×Ô¶¯Ö´ÐÐ + virtual void onEnter(); + + // ÖØÐ´Õâ¸öº¯Êý£¬Ëü½«ÔÚ½ÚµãÀ뿪³¡¾°Ê±×Ô¶¯Ö´ÐÐ + virtual void onExit(); + + // »ñÈ¡½ÚµãÏÔʾ״̬ virtual bool isVisiable() const; + // »ñÈ¡½ÚµãÃû³Æ + virtual EString getName() const; + // »ñÈ¡½Úµã»æÍ¼Ë³Ðò virtual int getOrder() const; // »ñÈ¡½Úµãºá×ø±ê - virtual float getX() const; + virtual float getPosX() const; // »ñÈ¡½Úµã×Ý×ø±ê - virtual float getY() const; + virtual float getPosY() const; + + // »ñÈ¡½Úµã×ø±ê + virtual EPoint getPos() const; // »ñÈ¡½Úµã¿í¶È virtual float getWidth() const; @@ -37,6 +49,18 @@ public: // »ñÈ¡½Úµã¸ß¶È virtual float getHeight() const; + // »ñÈ¡½Úµã¿í¶È£¨²»¿¼ÂÇËõ·Å£© + virtual float getRealWidth() const; + + // »ñÈ¡½Úµã¸ß¶È£¨²»¿¼ÂÇËõ·Å£© + virtual float getRealHeight() const; + + // »ñÈ¡½Úµã´óС£¨²»¿¼ÂÇËõ·Å£© + virtual ESize getRealSize() const; + + // »ñÈ¡½Úµã´óС + virtual ESize getSize() const; + // »ñÈ¡½ÚµãºáÏòËõ·Å±ÈÀý virtual float getScaleX() const; @@ -62,20 +86,14 @@ public: virtual EScene * getParentScene() const; // »ñÈ¡ËùÓÐ×Ó½Úµã - virtual std::vector &getChildren(); + virtual EVector &getChildren(); // »ñÈ¡×Ó½ÚµãÊýÁ¿ virtual size_t getChildrenCount() const; // ¸ù¾ÝÃû×Ö»ñÈ¡×Ó½Úµã virtual ENode * getChild( - EString name - ) const; - - // ¸ù¾ÝÃû×Ö»ñÈ¡×Ó½Úµã - static ENode * getChild( - EString name, - const std::vector &children + const EString & name ); // ÉèÖýڵãÊÇ·ñÏÔʾ @@ -85,19 +103,24 @@ public: // ÉèÖýڵãÃû³Æ virtual void setName( - EString name + const EString & name ); // ÉèÖýڵãºá×ø±ê - virtual void setX( + virtual void setPosX( float x ); // ÉèÖýڵã×Ý×ø±ê - virtual void setY( + virtual void setPosY( float y ); + // ÉèÖýڵã×ø±ê + virtual void setPos( + const EPoint & point + ); + // ÉèÖýڵã×ø±ê virtual void setPos( float x, @@ -110,6 +133,11 @@ public: float y ); + // ÒÆ¶¯½Úµã + virtual void move( + const EVec & v + ); + // ÉèÖýڵã¿í¶È virtual void setWidth( float width @@ -120,6 +148,11 @@ public: float height ); + // ÉèÖýڵã´óС + virtual void setSize( + const ESize & size + ); + // ÉèÖýڵã´óС virtual void setSize( float width, @@ -207,11 +240,6 @@ public: float anchorY ); - // ÉèÖýڵãËùÔÚ³¡¾° - virtual void setParentScene( - EScene * scene - ); - // ÉèÖø¸½Úµã virtual void setParent( ENode* parent @@ -229,14 +257,19 @@ public: ); // ÒÆ³ý×Ó½Úµã - virtual void removeChild( + virtual bool removeChild( ENode * child, bool release = false ); // ÒÆ³ý×Ó½Úµã virtual void removeChild( - EString childName, + const EString & childName, + bool release = false + ); + + // ÒÆ³ýËùÓнڵã + virtual void clearAllChildren( bool release = false ); @@ -247,9 +280,23 @@ protected: // äÖȾ½Úµã virtual void _onRender(); + // ½Úµã±»Ìí¼Óµ½³¡¾°Ê±µÄÖ´ÐгÌÐò + virtual void _onEnter(); + + // ½Úµã´Ó³¡¾°ÖÐÏûʧʱµÄÖ´ÐгÌÐò + virtual void _onExit(); + + // ½ÚµãÇå³ýʱµÄÖ´ÐгÌÐò + virtual void _onClear(); + // ×Ó½ÚµãÅÅÐò void _sortChildren(); + // ÉèÖýڵãËùÔÚ³¡¾° + virtual void _setParentScene( + EScene * scene + ); + // Ö»¿¼ÂÇ×ÔÉí½øÐжþά¾ØÕó±ä»» void _updateTransformToReal(); @@ -268,10 +315,8 @@ protected: protected: EString m_sName; size_t m_nHashName; - float m_fPosX; - float m_fPosY; - float m_fWidth; - float m_fHeight; + EPoint m_Pos; + ESize m_Size; float m_fScaleX; float m_fScaleY; float m_fRotation; @@ -281,14 +326,15 @@ protected: float m_fRealOpacity; float m_fAnchorX; float m_fAnchorY; - D2D1::Matrix3x2F m_Matri; int m_nOrder; bool m_bVisiable; + bool m_bDisplayedInScene; bool m_bSortChildrenNeeded; bool m_bTransformChildrenNeeded; EScene * m_pParentScene; ENode * m_pParent; - std::vector m_vChildren; + D2D1::Matrix3x2F m_Matri; + EVector m_vChildren; }; @@ -298,15 +344,15 @@ class ERectangle : public: ERectangle(); - EColor::Enum getColor() const; + EColor getColor() const; - void setColor(EColor::Enum color); + void setColor(EColor color); protected: virtual void _onRender() override; protected: - EColor::Enum m_Color; + EColor m_Color; }; @@ -314,17 +360,47 @@ class ESprite : public ENode { public: + // ´´½¨Ò»¸ö¿Õ¾«Áé ESprite(); - ESprite(EString imageFileName); + // ´ÓÎļþͼƬ´´½¨¾«Áé + ESprite( + const EString & imageFileName + ); - ESprite(EString resourceName, EString resourceType); + // ´Ó×ÊԴͼƬ´´½¨¾«Áé + ESprite( + const EString & resourceName, + const EString & resourceType + ); + + // ´ÓÎļþ¼ÓÔØÍ¼Æ¬ + void setImage( + const EString & fileName + ); - void setImage(EString fileName); + // ´Ó×ÊÔ´¼ÓÔØÍ¼Æ¬ + void setImage( + const EString & resourceName, + const EString & resourceType + ); - void setImage(EString resourceName, EString resourceType); + // Ô¤¼ÓÔØ×ÊÔ´ + static bool preloadImage( + const EString & fileName + ); + + // Ô¤¼ÓÔØ×ÊÔ´ + static bool preloadImage( + const EString & resourceName, + const EString & resourceType + ); + + // Çå¿ÕͼƬ»º´æ + static void clearCache(); protected: + // äÖȾ¾«Áé virtual void _onRender() override; protected: diff --git a/Easy2D/etools.h b/Easy2D/etools.h index 4e150748..eff1e6c1 100644 --- a/Easy2D/etools.h +++ b/Easy2D/etools.h @@ -1,9 +1,12 @@ #pragma once #include "ebase.h" +#include namespace e2d { +class ETimerManager; + // ¶ÔÏó¹ÜÀíÆ÷ class EObjectManager { @@ -31,14 +34,187 @@ private: class ETimer : public EObject { + friend ETimerManager; +public: + ETimer(); + + ETimer( + const EString &name /* ¶¨Ê±Æ÷Ãû³Æ */ + ); + + ETimer( + const TIMER_CALLBACK &callback, /* ¶¨Ê±Æ÷»Øµ÷º¯Êý */ + LONGLONG delay = 20LL /* ʱ¼ä¼ä¸ô */ + ); + + ETimer( + const EString &name, /* ¶¨Ê±Æ÷Ãû³Æ */ + const TIMER_CALLBACK &callback, /* ¶¨Ê±Æ÷»Øµ÷º¯Êý */ + LONGLONG delay = 20LL /* ʱ¼ä¼ä¸ô */ + ); + + // »ñÈ¡¶¨Ê±Æ÷״̬ + bool isRunning() const; + + // »ñÈ¡¶¨Ê±Æ÷¹ÒÆð״̬ + bool isWaiting() const; + + // Æô¶¯¼àÌý + void start(); + + // Í£Ö¹¼àÌý + void stop(); + + // »ñÈ¡¶¨Ê±Æ÷Ãû³Æ + EString getName() const; + + // »ñÈ¡¶¨Ê±Æ÷ËùÔÚ³¡¾° + EScene * getParentScene() const; + + // »ñÈ¡¶¨Ê±Æ÷ËùÔÚ½Úµã + ENode * getParentNode() const; + + // ÉèÖö¨Ê±Æ÷Ãû³Æ + void setName( + const EString &name + ); + + // ÉèÖö¨Ê±Æ÷Ö´Ðмä¸ô + void setInterval( + LONGLONG interval + ); + + // °ó¶¨¶¨Ê±Æ÷µ½³¡¾° + virtual void bindWith( + EScene * pParentScene + ); + + // °ó¶¨¶¨Ê±Æ÷µ½½Úµã + virtual void bindWith( + ENode * pParentNode + ); + +protected: + // ½øÈëµÈ´ý״̬ + void _wait(); + + // »½ÐÑ + void _notify(); + + // Ö´Ðлص÷º¯Êý + virtual void _runCallback(); + +protected: + EString m_sName; + bool m_bRunning; + bool m_bWaiting; + int m_nRunTimes; + EScene * m_pParentScene; + ENode * m_pParentNode; + TIMER_CALLBACK m_Callback; + LONGLONG m_nInterval; + std::chrono::steady_clock::time_point m_tLast; }; // ¶¨Ê±Æ÷¹ÜÀíÆ÷ class ETimerManager { + friend EApp; + friend EScene; + friend ENode; +public: + // °ó¶¨¶¨Ê±Æ÷µ½³¡¾° + static void bindTimer( + ETimer * timer, + EScene * pParentScene + ); + + // °ó¶¨¶¨Ê±Æ÷µ½½Úµã + static void bindTimer( + ETimer * timer, + ENode * pParentNode + ); + + // Æô¶¯¾ßÓÐÏàͬÃû³ÆµÄ¶¨Ê±Æ÷ + static void startTimers( + const EString &name + ); + + // Í£Ö¹¾ßÓÐÏàͬÃû³ÆµÄ¶¨Ê±Æ÷ + static void stopTimers( + const EString &name + ); + + // ɾ³ý¾ßÓÐÏàͬÃû³ÆµÄ¶¨Ê±Æ÷ + static void delTimers( + const EString &name + ); + + // Æô¶¯°ó¶¨ÔÚ³¡¾°¼°Æä×Ó½ÚµãÉϵÄËùÓж¨Ê±Æ÷ + static void startAllTimersBindedWith( + EScene * pParentScene + ); + + // Í£Ö¹°ó¶¨ÔÚ³¡¾°¼°Æä×Ó½ÚµãÉϵÄËùÓж¨Ê±Æ÷ + static void stopAllTimersBindedWith( + EScene * pParentScene + ); + + // Çå¿Õ°ó¶¨ÔÚ³¡¾°¼°Æä×Ó½ÚµãÉϵÄËùÓж¨Ê±Æ÷ + static void clearAllTimersBindedWith( + EScene * pParentScene + ); + + // Æô¶¯°ó¶¨ÔÚ½ÚµãÉϵÄËùÓж¨Ê±Æ÷ + static void startAllTimersBindedWith( + ENode * pParentNode + ); + + // Í£Ö¹°ó¶¨ÔÚ½ÚµãÉϵÄËùÓж¨Ê±Æ÷ + static void stopAllTimersBindedWith( + ENode * pParentNode + ); + + // Çå¿Õ°ó¶¨ÔÚ½ÚµãÉϵÄËùÓж¨Ê±Æ÷ + static void clearAllTimersBindedWith( + ENode * pParentNode + ); + + // Æô¶¯ËùÓж¨Ê±Æ÷ + static void startAllTimers(); + + // Í£Ö¹ËùÓж¨Ê±Æ÷ + static void stopAllTimers(); + + // Çå³ýËùÓж¨Ê±Æ÷ + static void clearAllTimers(); + +private: + // ¹ÒÆð°ó¶¨ÔÚ³¡¾°¼°Æä×Ó½ÚµãÉϵÄËùÓж¨Ê±Æ÷ + static void _waitAllTimersBindedWith( + EScene * pParentScene + ); + + // ÖØÆô°ó¶¨ÔÚ³¡¾°¼°Æä×Ó½ÚµãÉϵÄËùÓж¨Ê±Æ÷ + static void _notifyAllTimersBindedWith( + EScene * pParentScene + ); + + // ¹ÒÆð°ó¶¨ÔÚ½ÚµãÉϵÄËùÓж¨Ê±Æ÷ + static void _waitAllTimersBindedWith( + ENode * pParentNode + ); + + // ÖØÆô°ó¶¨ÔÚ½ÚµãÉϵÄËùÓж¨Ê±Æ÷ + static void _notifyAllTimersBindedWith( + ENode * pParentNode + ); + + // ¶¨Ê±Æ÷Ö´ÐгÌÐò + static void TimerProc(); }; } \ No newline at end of file