新增ETimer和ETimerManager

This commit is contained in:
Nomango 2017-10-17 21:22:25 +08:00
parent 05bcd762e0
commit 2422ec258b
32 changed files with 2251 additions and 1121 deletions

View File

@ -23,7 +23,7 @@
<ProjectGuid>{70931955-FE2D-4A50-93C6-6955A730B0FE}</ProjectGuid> <ProjectGuid>{70931955-FE2D-4A50-93C6-6955A730B0FE}</ProjectGuid>
<Keyword>Win32Proj</Keyword> <Keyword>Win32Proj</Keyword>
<RootNamespace>ConsoleDemo</RootNamespace> <RootNamespace>ConsoleDemo</RootNamespace>
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion> <WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">

View File

@ -23,23 +23,23 @@ int main()
} }
});*/ });*/
auto listener = new EKeyPressListener([=] { auto listener = new EKeyboardPressListener([=] {
if (EKeyMsg::isCapitalLockOn()) if (EKeyboardMsg::isCapitalLockOn())
{ {
if (EKeyMsg::getVal() == EKeyMsg::KEY::LEFT) if (EKeyboardMsg::getVal() == EKeyboardMsg::KEY::LEFT)
{ {
node->move(-3, 0); node->move(-3, 0);
} }
if (EKeyMsg::getVal() == EKeyMsg::KEY::RIGHT) if (EKeyboardMsg::getVal() == EKeyboardMsg::KEY::RIGHT)
{ {
node->move(3, 0); node->move(3, 0);
} }
} }
}); });
listener->bindWithNode(node); listener->bindWith(node);
EMsgManager::bindListenerWithScene(listener, scene); EMsgManager::bindListener(listener, scene);
app.enterScene(scene); app.enterScene(scene);

4
Demo/Demo.vcxproj.user Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>

View File

@ -14,34 +14,20 @@ int WINAPI WinMain(
{ {
auto scene = new EScene(); 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"); auto sprite = new ESprite(L"test.png");
sprite->setAnchor(0.5f, 0.5f); 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"); auto sprite2 = new ESprite(L"test.png");
sprite2->setPos(30, 0);
sprite2->setScale(0.5); sprite2->setScale(0.5);
sprite2->setRotation(45); sprite2->setRotation(45);
sprite->addChild(sprite2); sprite->addChild(sprite2);
auto mlistener = new EMouseClickListener([=](EPoint p) { auto mlistener = new EMouseDoubleClickListener([=](EPoint p) {
sprite->setRotation(sprite->getRotation() + 10); //sprite->setRotation(sprite->getRotation() + 10);
EApp::setWindowSize(640, 480);
}); });
mlistener->bindWith(sprite); mlistener->bindWith(sprite);

View File

@ -2,29 +2,41 @@
#include "..\Win\winbase.h" #include "..\Win\winbase.h"
#include "..\emsg.h" #include "..\emsg.h"
#include "..\etools.h" #include "..\etools.h"
#include "..\enodes.h"
#include <stack> #include <stack>
#include <chrono>
#include <thread> #include <thread>
using namespace std::chrono; #include <imm.h>
#pragma comment (lib ,"imm32.lib")
using namespace std::this_thread; using namespace std::this_thread;
using namespace std::chrono;
// 唯一实例指针
e2d::EApp * s_pInstance = nullptr; e2d::EApp * s_pInstance = nullptr;
// 场景栈
std::stack<e2d::EScene*> s_SceneStack; std::stack<e2d::EScene*> s_SceneStack;
e2d::EApp::EApp() e2d::EApp::EApp()
: m_bRunning(false) : m_bRunning(false)
, nAnimationInterval(17LL)
, m_ClearColor(EColor::Black) , m_ClearColor(EColor::Black)
, m_pCurrentScene(nullptr) , m_pCurrentScene(nullptr)
, m_pNextScene(nullptr) , m_pNextScene(nullptr)
{ {
ASSERT(s_pInstance == nullptr, "EApp instance already exists!");
s_pInstance = this; // 保存实例对象 s_pInstance = this; // 保存实例对象
CoInitialize(NULL);
} }
e2d::EApp::~EApp() e2d::EApp::~EApp()
{ {
SafeReleaseInterface(&GetFactory()); // 释放资源
SafeReleaseInterface(&GetRenderTarget()); SafeReleaseInterface(&GetRenderTarget());
SafeReleaseInterface(&GetFactory());
CoUninitialize();
} }
e2d::EApp * e2d::EApp::get() e2d::EApp * e2d::EApp::get()
@ -33,58 +45,34 @@ e2d::EApp * e2d::EApp::get()
return s_pInstance; // 获取 EApp 的唯一实例 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; HRESULT hr;
CoInitialize(NULL);
// 关闭控制台 // 显示或关闭控制台
if (bShowConsole) EApp::showConsole(showConsole);
{
// 查找是否已经存在控制台
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();
}
// 初始化 device-indpendent 资源 // 创建设备无关资源
// 比如 Direct2D factory.
hr = _createDeviceIndependentResources(); hr = _createDeviceIndependentResources();
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// 注册窗口类 // 注册窗口类
WNDCLASSEX wcex = { sizeof(WNDCLASSEX) }; WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = EApp::WndProc; wcex.lpfnWndProc = EApp::WndProc;
wcex.cbClsExtra = 0; wcex.cbClsExtra = 0;
wcex.cbWndExtra = sizeof(LONG_PTR); wcex.cbWndExtra = sizeof(LONG_PTR);
wcex.hInstance = HINST_THISCOMPONENT; wcex.hInstance = HINST_THISCOMPONENT;
wcex.hbrBackground = NULL; wcex.hbrBackground = (HBRUSH)(GetStockObject(BLACK_BRUSH));
wcex.lpszMenuName = NULL; wcex.lpszMenuName = NULL;
wcex.hCursor = LoadCursor(NULL, IDI_APPLICATION); wcex.hCursor = LoadCursor(NULL, IDI_APPLICATION);
wcex.lpszClassName = L"E2DApp"; wcex.lpszClassName = L"Easy2DApp";
RegisterClassEx(&wcex); RegisterClassEx(&wcex);
@ -97,17 +85,31 @@ bool e2d::EApp::init(e2d::EString title, UINT32 width, UINT32 height, bool bShow
// to create its own windows. // to create its own windows.
GetFactory()->GetDesktopDpi(&dpiX, &dpiY); GetFactory()->GetDesktopDpi(&dpiX, &dpiY);
m_sTitle = title; width = static_cast<UINT>(ceil(width * dpiX / 96.f));
height = static_cast<UINT>(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( GetHWnd() = CreateWindow(
L"E2DApp", L"Easy2DApp",
m_sTitle.c_str(), m_sTitle.c_str(),
WS_OVERLAPPEDWINDOW | CS_DBLCLKS, windowStyle,
CW_USEDEFAULT, rtWindow.left,
CW_USEDEFAULT, rtWindow.top,
static_cast<UINT>(ceil(width * dpiX / 96.f)), rtWindow.right - rtWindow.left,
static_cast<UINT>(ceil(height * dpiY / 96.f)), rtWindow.bottom - rtWindow.top,
NULL, NULL,
NULL, NULL,
HINST_THISCOMPONENT, 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; 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 else
{ {
// 获取屏幕分辨率 UnregisterClass(L"E2DApp", HINST_THISCOMPONENT);
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
);
} }
} }
@ -148,12 +137,59 @@ bool e2d::EApp::init(e2d::EString title, UINT32 width, UINT32 height, bool bShow
return SUCCEEDED(hr); 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() void e2d::EApp::run()
{ {
ASSERT(m_pNextScene != nullptr, "Next scene NULL pointer exception."); ASSERT(GetHWnd() != nullptr, "Cannot find Game Window.");
// 进入第一个场景 // 进入第一个场景
_enterNextScene(); _enterNextScene();
ASSERT(m_pCurrentScene != nullptr, "Current scene NULL pointer exception.");
// 显示窗口 // 显示窗口
ShowWindow(GetHWnd(), SW_SHOWNORMAL); ShowWindow(GetHWnd(), SW_SHOWNORMAL);
UpdateWindow(GetHWnd()); UpdateWindow(GetHWnd());
@ -163,56 +199,59 @@ void e2d::EApp::run()
MSG msg; MSG msg;
while (m_bRunning) while (m_bRunning)
{
if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{ {
// 处理窗口消息 // 处理窗口消息
if (msg.message == WM_QUIT) while (PeekMessage(&msg, GetHWnd(), 0, 0, PM_REMOVE))
{ {
m_bRunning = false;
break;
}
TranslateMessage(&msg); TranslateMessage(&msg);
DispatchMessage(&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() 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 nInterval = 0LL;
// 挂起时长 // 挂起时长
static LONGLONG nWaitMS = 0L; static LONGLONG nWaitMS = 0L;
// 刷新计时 // 刷新计时
nNow = steady_clock::now(); static steady_clock::time_point tLast = steady_clock::now();
GetNow() = steady_clock::now();
// 计算时间间隔 // 计算时间间隔
nInterval = duration_cast<milliseconds>(nNow - nLast).count(); nInterval = GetInterval(tLast);
// 判断间隔时间是否足够 // 判断间隔时间是否足够
if (nInterval >= nAnimationInterval) if (nInterval >= nAnimationInterval)
{ {
// 记录当前时间 // 记录当前时间
nLast = nNow; tLast = GetNow();
// 执行游戏控制 // 游戏控制流程
_onControl(); _onControl();
// 刷新游戏画面 // 刷新游戏画面
_onRender(); if (!_onRender())
{
MessageBox(GetHWnd(), L"Game Render Failed!", L"Error", MB_OK);
this->quit();
}
} }
else else
{ {
@ -221,7 +260,7 @@ void e2d::EApp::_mainLoop()
// 挂起线程,释放 CPU 占用 // 挂起线程,释放 CPU 占用
if (nWaitMS > 1LL) if (nWaitMS > 1LL)
{ {
std::this_thread::sleep_for(milliseconds(nWaitMS)); sleep_for(milliseconds(nWaitMS));
} }
} }
} }
@ -237,7 +276,7 @@ void e2d::EApp::_onControl()
// 断言当前场景非空 // 断言当前场景非空
ASSERT(m_pCurrentScene != nullptr, "Current scene NULL pointer exception."); ASSERT(m_pCurrentScene != nullptr, "Current scene NULL pointer exception.");
//Timer::__exec(); // 定时器执行程序 ETimerManager::TimerProc(); // 定时器执行程序
//ActionManager::__exec(); // 动作管理器执行程序 //ActionManager::__exec(); // 动作管理器执行程序
EObjectManager::__flush(); // 刷新内存池 EObjectManager::__flush(); // 刷新内存池
} }
@ -245,7 +284,7 @@ void e2d::EApp::_onControl()
// This method discards device-specific // This method discards device-specific
// resources if the Direct3D device dissapears during execution and // resources if the Direct3D device dissapears during execution and
// recreates the resources the next time it's invoked. // recreates the resources the next time it's invoked.
void e2d::EApp::_onRender() bool e2d::EApp::_onRender()
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
@ -254,12 +293,14 @@ void e2d::EApp::_onRender()
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
GetRenderTarget()->BeginDraw(); GetRenderTarget()->BeginDraw();
// 使用背景色清空屏幕
GetRenderTarget()->Clear(D2D1::ColorF(m_ClearColor)); GetRenderTarget()->Clear(D2D1::ColorF(m_ClearColor));
// 绘制当前场景
m_pCurrentScene->_onRender(); // 绘制当前场景 m_pCurrentScene->_onRender();
hr = GetRenderTarget()->EndDraw(); hr = GetRenderTarget()->EndDraw();
// 刷新界面
UpdateWindow(GetHWnd());
} }
if (hr == D2DERR_RECREATE_TARGET) if (hr == D2DERR_RECREATE_TARGET)
@ -268,13 +309,10 @@ void e2d::EApp::_onRender()
_discardDeviceResources(); _discardDeviceResources();
} }
if (FAILED(hr)) return SUCCEEDED(hr);
{
exit(EXIT_FAILURE);
}
} }
void e2d::EApp::setWindowSize(int width, int height) void e2d::EApp::setWindowSize(UINT32 width, UINT32 height)
{ {
// 获取屏幕分辨率 // 获取屏幕分辨率
int screenWidth = GetSystemMetrics(SM_CXSCREEN); 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); MoveWindow(GetHWnd(), (screenWidth - width) / 2, (screenHeight - height) / 2, width, height, TRUE);
} }
void e2d::EApp::setWindowSize(e2d::ESize size) void e2d::EApp::setWindowTitle(const EString &title)
{
setWindowSize(size.cx, size.cy);
}
void e2d::EApp::setWindowTitle(e2d::EString title)
{ {
// 设置窗口标题 // 设置窗口标题
SetWindowText(GetHWnd(), title.c_str()); SetWindowText(GetHWnd(), title.c_str());
@ -310,11 +343,6 @@ e2d::EString e2d::EApp::getTitle()
return get()->m_sTitle; return get()->m_sTitle;
} }
e2d::ESize e2d::EApp::getSize()
{
return e2d::ESize(GetRenderTarget()->GetPixelSize().width, GetRenderTarget()->GetPixelSize().height);
}
UINT32 e2d::EApp::getWidth() UINT32 e2d::EApp::getWidth()
{ {
return GetRenderTarget()->GetPixelSize().width; return GetRenderTarget()->GetPixelSize().width;
@ -338,6 +366,7 @@ void e2d::EApp::enterScene(EScene * scene, bool save /* = true */)
void e2d::EApp::backScene() void e2d::EApp::backScene()
{ {
ASSERT(s_SceneStack.size(), "Scene stack now is empty!");
// 从栈顶取出场景指针,作为下一场景 // 从栈顶取出场景指针,作为下一场景
get()->m_pNextScene = s_SceneStack.top(); get()->m_pNextScene = s_SceneStack.top();
s_SceneStack.pop(); s_SceneStack.pop();
@ -364,36 +393,64 @@ e2d::EScene * e2d::EApp::getCurrentScene()
return get()->m_pCurrentScene; return get()->m_pCurrentScene;
} }
void e2d::EApp::setAppName(e2d::EString appname) void e2d::EApp::setAppName(const EString &appname)
{ {
s_pInstance->m_sAppName = appname; s_pInstance->m_sAppName = appname;
} }
e2d::EString e2d::EApp::getAppName() e2d::EString e2d::EApp::getAppName()
{ {
if (s_pInstance->m_sAppName.empty())
s_pInstance->m_sAppName = s_pInstance->m_sTitle;
return s_pInstance->m_sAppName; return s_pInstance->m_sAppName;
} }
void e2d::EApp::setBkColor(EColor::Enum color) void e2d::EApp::setBkColor(EColor color)
{ {
get()->m_ClearColor = 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); ShowWindow(GetHWnd(), SW_HIDE);
} }
void e2d::EApp::show() void e2d::EApp::showWindow()
{ {
ShowWindow(GetHWnd(), SW_NORMAL); ShowWindow(GetHWnd(), SW_SHOWNORMAL);
} }
void e2d::EApp::free() void e2d::EApp::free()
{ {
// 释放场景内存 // 释放场景内存
SafeDelete(&get()->m_pCurrentScene); SafeDelete(&m_pCurrentScene);
SafeDelete(&get()->m_pNextScene); SafeDelete(&m_pNextScene);
// 清空场景栈 // 清空场景栈
while (s_SceneStack.size()) while (s_SceneStack.size())
{ {
@ -401,8 +458,10 @@ void e2d::EApp::free()
SafeDelete(&temp); SafeDelete(&temp);
s_SceneStack.pop(); s_SceneStack.pop();
} }
// 删除图片缓存
ESprite::clearCache();
// 删除所有定时器、监听器和动画 // 删除所有定时器、监听器和动画
//Timer::clearAllTimers(); ETimerManager::clearAllTimers();
EMsgManager::clearAllKeyboardListeners(); EMsgManager::clearAllKeyboardListeners();
EMsgManager::clearAllMouseListeners(); EMsgManager::clearAllMouseListeners();
//ActionManager::clearAllActions(); //ActionManager::clearAllActions();
@ -486,7 +545,7 @@ void e2d::EApp::_discardDeviceResources()
// If the application receives a WM_SIZE message, this method // If the application receives a WM_SIZE message, this method
// re2d::ESizes the render target appropriately. // re2d::ESizes the render target appropriately.
void e2d::EApp::_onResize(UINT width, UINT height) void e2d::EApp::_onResize(UINT32 width, UINT32 height)
{ {
if (GetRenderTarget()) if (GetRenderTarget())
{ {
@ -504,9 +563,10 @@ LRESULT e2d::EApp::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam
if (message == WM_CREATE) if (message == WM_CREATE)
{ {
// 获取发送 WM_CREATE 消息的 EApp 实例对象指针
LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam; LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
e2d::EApp *pEApp = (e2d::EApp *)pcs->lpCreateParams; e2d::EApp *pEApp = (e2d::EApp *)pcs->lpCreateParams;
// 保存 EApp 指针到 GWLP_USERDATA 字段
::SetWindowLongPtrW( ::SetWindowLongPtrW(
hWnd, hWnd,
GWLP_USERDATA, GWLP_USERDATA,
@ -517,6 +577,7 @@ LRESULT e2d::EApp::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam
} }
else else
{ {
// 从 GWLP_USERDATA 字段取出 EApp 指针
e2d::EApp *pEApp = reinterpret_cast<e2d::EApp *>(static_cast<LONG_PTR>( e2d::EApp *pEApp = reinterpret_cast<e2d::EApp *>(static_cast<LONG_PTR>(
::GetWindowLongPtrW( ::GetWindowLongPtrW(
hWnd, hWnd,
@ -529,17 +590,7 @@ LRESULT e2d::EApp::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam
{ {
switch (message) 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_LBUTTONUP:
case WM_LBUTTONDOWN: case WM_LBUTTONDOWN:
case WM_LBUTTONDBLCLK: case WM_LBUTTONDBLCLK:
@ -555,16 +606,20 @@ LRESULT e2d::EApp::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam
EMsgManager::MouseProc(message, wParam, lParam); EMsgManager::MouseProc(message, wParam, lParam);
} }
result = 0; result = 0;
wasHandled = true;
break; break;
// 处理按键消息
case WM_KEYDOWN: case WM_KEYDOWN:
case WM_KEYUP: case WM_KEYUP:
{ {
EMsgManager::KeyboardProc(message, wParam, lParam); EMsgManager::KeyboardProc(message, wParam, lParam);
} }
result = 0; result = 0;
wasHandled = true;
break; break;
// 处理窗口大小变化消息
case WM_SIZE: case WM_SIZE:
{ {
UINT width = LOWORD(lParam); UINT width = LOWORD(lParam);
@ -575,14 +630,17 @@ LRESULT e2d::EApp::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam
wasHandled = true; wasHandled = true;
break; break;
// 处理分辨率变化消息
case WM_DISPLAYCHANGE: case WM_DISPLAYCHANGE:
{ {
// 重绘客户区
InvalidateRect(hWnd, NULL, FALSE); InvalidateRect(hWnd, NULL, FALSE);
} }
result = 0; result = 0;
wasHandled = true; wasHandled = true;
break; break;
// 重绘窗口
case WM_PAINT: case WM_PAINT:
{ {
pEApp->_onRender(); pEApp->_onRender();
@ -592,12 +650,24 @@ LRESULT e2d::EApp::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam
wasHandled = true; wasHandled = true;
break; break;
// 窗口关闭消息
case WM_CLOSE:
{
if (pEApp->onExit())
{
DestroyWindow(hWnd);
}
}
result = 1;
wasHandled = true;
break;
// 窗口被销毁
case WM_DESTROY: case WM_DESTROY:
{ {
if (GetConsoleWindow()) // 退出程序
{ pEApp->quit();
FreeConsole(); // 发送退出消息
}
PostQuitMessage(0); PostQuitMessage(0);
} }
result = 1; result = 1;

View File

@ -1,17 +1,21 @@
#include "..\ebase.h" #include "..\ebase.h"
#include "..\enodes.h" #include "..\enodes.h"
#include "..\emsg.h" #include "..\emsg.h"
#include "..\etools.h"
#include <algorithm> #include <algorithm>
e2d::EScene::EScene() e2d::EScene::EScene()
: m_bWillSave(true) : m_bWillSave(true)
, m_bSortNeeded(false) , m_bSortNeeded(false)
, m_Root(new ENode())
{ {
m_Root->_onEnter();
m_Root->_setParentScene(this);
} }
e2d::EScene::~EScene() e2d::EScene::~EScene()
{ {
clearAllChildren(); m_Root->autoRelease();
} }
void e2d::EScene::onEnter() void e2d::EScene::onEnter()
@ -24,39 +28,15 @@ void e2d::EScene::onExit()
void e2d::EScene::_onRender() void e2d::EScene::_onRender()
{ {
this->_sortChildren(); m_Root->_callOn();
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();
}
);
}
} }
void e2d::EScene::_onEnter() void e2d::EScene::_onEnter()
{ {
// 启用场景上的所有定时器、监听器和动画 // 启用场景上的所有定时器、监听器和动画
//Timer::notifyAllSceneTimers(m_pNextScene); ETimerManager::_notifyAllTimersBindedWith(this);
EMsgManager::notifyAllListenersBindWithScene(this); EMsgManager::_notifyAllMouseListenersBindedWith(this);
EMsgManager::_notifyAllKeyboardListenersBindedWith(this);
//ActionManager::notifyAllSceneActions(m_pNextScene); //ActionManager::notifyAllSceneActions(m_pNextScene);
} }
@ -64,94 +44,61 @@ void e2d::EScene::_onExit()
{ {
if (m_bWillSave) if (m_bWillSave)
{ {
//Timer::waitAllSceneTimers(m_pCurrentScene); ETimerManager::_waitAllTimersBindedWith(this);
EMsgManager::waitAllListenersBindWithScene(this); EMsgManager::_waitAllMouseListenersBindedWith(this);
EMsgManager::_waitAllKeyboardListenersBindedWith(this);
//ActionManager::waitAllSceneActions(m_pCurrentScene); //ActionManager::waitAllSceneActions(m_pCurrentScene);
} }
else else
{ {
//Timer::clearAllSceneTimers(m_pCurrentScene); ETimerManager::clearAllTimersBindedWith(this);
EMsgManager::clearAllListenersBindWithScene(this); EMsgManager::clearAllMouseListenersBindedWith(this);
EMsgManager::clearAllKeyboardListenersBindedWith(this);
//ActionManager::stopAllSceneActions(m_pCurrentScene); //ActionManager::stopAllSceneActions(m_pCurrentScene);
} }
} }
void e2d::EScene::add(ENode * child, int order /* = 0 */) void e2d::EScene::add(ENode * child, int order /* = 0 */)
{ {
ASSERT(child != nullptr, "Scene::add NULL pointer exception."); m_Root->addChild(child, order);
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;
}
} }
bool e2d::EScene::remove(ENode * child, bool autoRelease /* = true */) bool e2d::EScene::remove(ENode * child, bool release /* = false */)
{ {
if (child == nullptr) return false; return m_Root->removeChild(child, release);
// 寻找是否有相同节点
std::vector<ENode*>::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;
} }
std::vector<e2d::ENode*>& 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::ENode*>& e2d::EScene::getChildren()
{
return m_Root->m_vChildren;
} }
size_t e2d::EScene::getChildrenCount() const 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() void e2d::EScene::clearAllChildren()
{ {
// 所有节点的引用计数减一 m_Root->clearAllChildren();
for (auto child : m_vChildren)
{
child->autoRelease();
child->release();
}
// 清空储存节点的容器
m_vChildren.clear();
} }
void e2d::EScene::bindListener(EMouseListener * listener) void e2d::EScene::bindListener(EMouseListener * listener)
{ {
EMsgManager::bindListenerWith(listener, this); EMsgManager::bindListener(listener, this);
} }
void e2d::EScene::bindListener(EKeyboardListener * listener) void e2d::EScene::bindListener(EKeyboardListener * listener)
{ {
EMsgManager::bindListenerWith(listener, this); EMsgManager::bindListener(listener, this);
} }

104
Easy2D/ETimer.cpp Normal file
View File

@ -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++;
}

View File

@ -195,9 +195,10 @@
<ClCompile Include="Base\EApp.cpp" /> <ClCompile Include="Base\EApp.cpp" />
<ClCompile Include="Base\EObject.cpp" /> <ClCompile Include="Base\EObject.cpp" />
<ClCompile Include="Base\EScene.cpp" /> <ClCompile Include="Base\EScene.cpp" />
<ClCompile Include="ETimer.cpp" />
<ClCompile Include="Msg\EMsgManager.cpp" /> <ClCompile Include="Msg\EMsgManager.cpp" />
<ClCompile Include="Msg\Listener\EKeyboardListener.cpp" /> <ClCompile Include="Msg\Listener\EKeyboardListener.cpp" />
<ClCompile Include="Msg\Listener\EKeyPressListener.cpp" /> <ClCompile Include="Msg\Listener\EKeyboardPressListener.cpp" />
<ClCompile Include="Msg\Listener\EListener.cpp" /> <ClCompile Include="Msg\Listener\EListener.cpp" />
<ClCompile Include="Msg\Listener\EMouseClickListener.cpp" /> <ClCompile Include="Msg\Listener\EMouseClickListener.cpp" />
<ClCompile Include="Msg\Listener\EMouseDoubleClickListener.cpp" /> <ClCompile Include="Msg\Listener\EMouseDoubleClickListener.cpp" />

View File

@ -54,12 +54,6 @@
<ClCompile Include="Tool\ETimerManager.cpp"> <ClCompile Include="Tool\ETimerManager.cpp">
<Filter>Tool</Filter> <Filter>Tool</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Msg\Listener\EKeyboardListener.cpp">
<Filter>Msg\Listener</Filter>
</ClCompile>
<ClCompile Include="Msg\Listener\EKeyPressListener.cpp">
<Filter>Msg\Listener</Filter>
</ClCompile>
<ClCompile Include="Node\ERectangle.cpp"> <ClCompile Include="Node\ERectangle.cpp">
<Filter>Node</Filter> <Filter>Node</Filter>
</ClCompile> </ClCompile>
@ -75,6 +69,15 @@
<ClCompile Include="Msg\Listener\EMouseDragListener.cpp"> <ClCompile Include="Msg\Listener\EMouseDragListener.cpp">
<Filter>Msg\Listener</Filter> <Filter>Msg\Listener</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="ETimer.cpp">
<Filter>Tool</Filter>
</ClCompile>
<ClCompile Include="Msg\Listener\EKeyboardListener.cpp">
<Filter>Msg\Listener</Filter>
</ClCompile>
<ClCompile Include="Msg\Listener\EKeyboardPressListener.cpp">
<Filter>Msg\Listener</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Win\winbase.h"> <ClInclude Include="Win\winbase.h">

View File

@ -1,86 +1,86 @@
#include "..\emsg.h" #include "..\emsg.h"
#include "..\enodes.h"
#include "..\Win\winbase.h" #include "..\Win\winbase.h"
#include <vector>
// 鼠标消息 // 鼠标消息
e2d::EMouseMsg mouseMsg; e2d::EMouseMsg s_MouseMsg;
// 按键消息 // 按键消息
e2d::EKeyMsg keyMsg; e2d::EKeyboardMsg s_KeyboardMsg;
// 鼠标消息监听器 // 鼠标消息监听器
std::vector<e2d::EMouseListener*> s_vMouseListeners; e2d::EVector<e2d::EMouseListener*> s_vMouseListeners;
// 按键消息监听器 // 按键消息监听器
std::vector<e2d::EKeyboardListener*> s_vKeyboardListeners; e2d::EVector<e2d::EKeyboardListener*> 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() 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() 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() 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() 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() 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() 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() 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() 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) if (::GetAsyncKeyState((int)key) & 0x8000)
{ {
@ -89,7 +89,7 @@ bool e2d::EKeyMsg::isKeyDown(KEY key)
return false; return false;
} }
bool e2d::EKeyMsg::isCapitalLockOn() bool e2d::EKeyboardMsg::isCapitalLockOn()
{ {
if (::GetKeyState(VK_CAPITAL) & 0x0001) if (::GetKeyState(VK_CAPITAL) & 0x0001)
{ {
@ -98,7 +98,7 @@ bool e2d::EKeyMsg::isCapitalLockOn()
return false; return false;
} }
bool e2d::EKeyMsg::isNumpadLockOn() bool e2d::EKeyboardMsg::isNumpadLockOn()
{ {
if (::GetKeyState(VK_NUMLOCK) & 0x0001) if (::GetKeyState(VK_NUMLOCK) & 0x0001)
{ {
@ -107,7 +107,7 @@ bool e2d::EKeyMsg::isNumpadLockOn()
return false; return false;
} }
bool e2d::EKeyMsg::isScrollLockOn() bool e2d::EKeyboardMsg::isScrollLockOn()
{ {
if (::GetKeyState(VK_SCROLL) & 0x0001) if (::GetKeyState(VK_SCROLL) & 0x0001)
{ {
@ -121,15 +121,15 @@ bool e2d::EKeyMsg::isScrollLockOn()
void e2d::EMsgManager::MouseProc(UINT message, WPARAM wParam, LPARAM lParam) void e2d::EMsgManager::MouseProc(UINT message, WPARAM wParam, LPARAM lParam)
{ {
// 保存鼠标消息 // 保存鼠标消息
mouseMsg.m_nMsg = message; s_MouseMsg.m_nMsg = message;
mouseMsg.m_wParam = wParam; s_MouseMsg.m_wParam = wParam;
mouseMsg.m_lParam = lParam; s_MouseMsg.m_lParam = lParam;
// 执行鼠标消息监听函数 // 执行鼠标消息监听函数
for (auto mlistener : s_vMouseListeners) for (auto mlistener : s_vMouseListeners)
{ {
if (mlistener->isRunning()) 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) void e2d::EMsgManager::KeyboardProc(UINT message, WPARAM wParam, LPARAM lParam)
{ {
// 保存按键消息 // 保存按键消息
keyMsg.m_nMsg = message; s_KeyboardMsg.m_nMsg = message;
keyMsg.m_wParam = wParam; s_KeyboardMsg.m_wParam = wParam;
keyMsg.m_lParam = lParam; s_KeyboardMsg.m_lParam = lParam;
// 执行按键消息监听函数 // 执行按键消息监听函数
for (auto klistener : s_vKeyboardListeners) for (auto klistener : s_vKeyboardListeners)
{ {
if (klistener->isRunning()) 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(listener == nullptr, "EMouseListener NULL pointer exception!");
WARN_IF(pParentScene == nullptr, "Bind EMouseListener with a NULL EScene pointer!"); 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(listener == nullptr, "EKeyboardListener NULL pointer exception!");
WARN_IF(pParentScene == nullptr, "Bind EKeyboardListener with a NULL EScene pointer!"); 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(listener == nullptr, "EMouseListener NULL pointer exception!");
WARN_IF(pParentNode == nullptr, "Bind EMouseListener with a NULL ENode pointer!"); 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(listener == nullptr, "EKeyboardListener NULL pointer exception!");
WARN_IF(pParentNode == nullptr, "Bind EKeyboardListener with a NULL ENode pointer!"); 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) for (auto l : s_vMouseListeners)
{ {
if (l->getName() == name) if (l->getName() == name)
@ -216,19 +231,10 @@ void e2d::EMsgManager::startListener(EString name)
l->start(); 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) for (auto l : s_vMouseListeners)
{ {
if (l->getName() == name) if (l->getName() == name)
@ -236,20 +242,12 @@ void e2d::EMsgManager::stopListener(EString name)
l->stop(); 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<EMouseListener*>::iterator mIter; EVector<EMouseListener*>::iterator mIter;
for (mIter = s_vMouseListeners.begin(); mIter != s_vMouseListeners.end();) for (mIter = s_vMouseListeners.begin(); mIter != s_vMouseListeners.end();)
{ {
if ((*mIter)->getName() == name) if ((*mIter)->getName() == name)
@ -263,8 +261,36 @@ void e2d::EMsgManager::delListener(EString name)
mIter++; 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<EKeyboardListener*>::iterator kIter; EVector<EKeyboardListener*>::iterator kIter;
for (kIter = s_vKeyboardListeners.begin(); kIter != s_vKeyboardListeners.end();) for (kIter = s_vKeyboardListeners.begin(); kIter != s_vKeyboardListeners.end();)
{ {
if ((*kIter)->getName() == name) 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) for (auto l : s_vMouseListeners)
{ {
if (!l->isWaiting()) if (l->getParentScene() == pParentScene)
{ {
l->start(); 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) for (auto l : s_vMouseListeners)
{ {
if (!l->isWaiting()) if (l->getParentScene() == pParentScene)
{ {
l->stop(); 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() void e2d::EMsgManager::clearAllMouseListeners()
{ {
for (auto l : s_vMouseListeners) EMsgManager::clearAllMouseListenersBindedWith(EApp::getCurrentScene());
{
l->autoRelease();
l->release();
}
s_vMouseListeners.clear();
} }
void e2d::EMsgManager::startAllKeyboardListener() void e2d::EMsgManager::startAllKeyboardListeners()
{ {
for (auto l : s_vKeyboardListeners) EMsgManager::startAllKeyboardListenersBindedWith(EApp::getCurrentScene());
{
if (!l->isWaiting())
{
l->start();
}
}
} }
void e2d::EMsgManager::stopAllKeyboardListener() void e2d::EMsgManager::stopAllKeyboardListeners()
{ {
for (auto l : s_vKeyboardListeners) EMsgManager::stopAllKeyboardListenersBindedWith(EApp::getCurrentScene());
{
if (!l->isWaiting())
{
l->stop();
}
}
} }
void e2d::EMsgManager::clearAllKeyboardListeners() void e2d::EMsgManager::clearAllKeyboardListeners()
{ {
for (auto l : s_vKeyboardListeners) EMsgManager::clearAllKeyboardListenersBindedWith(EApp::getCurrentScene());
{
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<EMouseListener*>::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<EKeyboardListener*>::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<EMouseListener*>::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<EKeyboardListener*>::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++;
}
}
}

View File

@ -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();
}
}

View File

@ -5,7 +5,7 @@ e2d::EKeyboardListener::EKeyboardListener()
{ {
} }
e2d::EKeyboardListener::EKeyboardListener(EString name) e2d::EKeyboardListener::EKeyboardListener(const EString & name)
: EListener(name) : EListener(name)
{ {
} }
@ -13,23 +13,23 @@ e2d::EKeyboardListener::EKeyboardListener(EString name)
e2d::EKeyboardListener::EKeyboardListener(const KEY_LISTENER_CALLBACK & callback) e2d::EKeyboardListener::EKeyboardListener(const KEY_LISTENER_CALLBACK & callback)
: EListener() : 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) : 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) void e2d::EKeyboardListener::setCallback(const KEY_LISTENER_CALLBACK & callback)
{ {
m_callback = callback; m_Callback = callback;
} }
void e2d::EKeyboardListener::bindWith(EScene * pParentScene) void e2d::EKeyboardListener::bindWith(EScene * pParentScene)
@ -38,7 +38,7 @@ void e2d::EKeyboardListener::bindWith(EScene * pParentScene)
if (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) if (pParentNode != nullptr && m_pParentScene == nullptr)
{ {
EMsgManager::bindListenerWith(this, pParentNode); EMsgManager::bindListener(this, pParentNode);
} }
} }

View File

@ -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();
}
}

View File

@ -3,13 +3,12 @@
e2d::EListener::EListener() e2d::EListener::EListener()
: m_bRunning(false) : m_bRunning(false)
, m_bWaiting(false) , m_bWaiting(false)
, m_sName(L"")
, m_pParentScene(nullptr) , m_pParentScene(nullptr)
, m_pParentNode(nullptr) , m_pParentNode(nullptr)
{ {
} }
e2d::EListener::EListener(EString name) e2d::EListener::EListener(const EString & name)
: EListener() : EListener()
{ {
m_sName = name; m_sName = name;
@ -35,12 +34,12 @@ void e2d::EListener::stop()
m_bRunning = false; m_bRunning = false;
} }
void e2d::EListener::wait() void e2d::EListener::_wait()
{ {
m_bWaiting = true; m_bWaiting = true;
} }
void e2d::EListener::notify() void e2d::EListener::_notify()
{ {
m_bWaiting = false; m_bWaiting = false;
} }
@ -60,7 +59,7 @@ e2d::ENode * e2d::EListener::getParentNode() const
return m_pParentNode; return m_pParentNode;
} }
void e2d::EListener::setName(EString name) void e2d::EListener::setName(const EString & name)
{ {
m_sName = name; m_sName = name;
} }

View File

@ -6,7 +6,7 @@ e2d::EMouseClickListener::EMouseClickListener()
{ {
} }
e2d::EMouseClickListener::EMouseClickListener(EString name) e2d::EMouseClickListener::EMouseClickListener(const EString & name)
: EMouseListener(name) : EMouseListener(name)
, m_bPressed(false) , m_bPressed(false)
{ {
@ -14,19 +14,19 @@ e2d::EMouseClickListener::EMouseClickListener(EString name)
e2d::EMouseClickListener::EMouseClickListener(const MOUSE_CLICK_LISTENER_CALLBACK & callback) e2d::EMouseClickListener::EMouseClickListener(const MOUSE_CLICK_LISTENER_CALLBACK & callback)
: EMouseListener() : EMouseListener()
, m_callback(callback) , m_Callback(callback)
, m_bPressed(false) , 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) : EMouseListener(name)
, m_callback(callback) , m_Callback(callback)
, m_bPressed(false) , m_bPressed(false)
{ {
} }
void e2d::EMouseClickListener::runCallback() void e2d::EMouseClickListener::_runCallback()
{ {
if (EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DOWN || if (EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DOWN ||
EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DBLCLK) EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DBLCLK)
@ -35,12 +35,12 @@ void e2d::EMouseClickListener::runCallback()
} }
else if (m_bPressed && EMouseMsg::getMsg() == EMouseMsg::LBUTTON_UP) else if (m_bPressed && EMouseMsg::getMsg() == EMouseMsg::LBUTTON_UP)
{ {
m_callback(EMouseMsg::getPos()); m_Callback(EMouseMsg::getPos());
m_bPressed = false; m_bPressed = false;
} }
} }
void e2d::EMouseClickListener::setCallback(const MOUSE_CLICK_LISTENER_CALLBACK & callback) void e2d::EMouseClickListener::setCallback(const MOUSE_CLICK_LISTENER_CALLBACK & callback)
{ {
m_callback = callback; m_Callback = callback;
} }

View File

@ -6,7 +6,7 @@ e2d::EMouseDoubleClickListener::EMouseDoubleClickListener()
{ {
} }
e2d::EMouseDoubleClickListener::EMouseDoubleClickListener(EString name) e2d::EMouseDoubleClickListener::EMouseDoubleClickListener(const EString & name)
: EMouseListener(name) : EMouseListener(name)
, m_bPressed(false) , m_bPressed(false)
{ {
@ -14,32 +14,36 @@ e2d::EMouseDoubleClickListener::EMouseDoubleClickListener(EString name)
e2d::EMouseDoubleClickListener::EMouseDoubleClickListener(const MOUSE_DBLCLK_LISTENER_CALLBACK & callback) e2d::EMouseDoubleClickListener::EMouseDoubleClickListener(const MOUSE_DBLCLK_LISTENER_CALLBACK & callback)
: EMouseListener() : EMouseListener()
, m_callback(callback) , m_Callback(callback)
, m_bPressed(false) , 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) : EMouseListener(name)
, m_callback(callback) , m_Callback(callback)
, m_bPressed(false) , 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; m_bPressed = true;
} }
else if (m_bPressed && EMouseMsg::getMsg() == EMouseMsg::LBUTTON_UP) else if (m_bPressed && EMouseMsg::getMsg() == EMouseMsg::LBUTTON_UP)
{ {
m_callback(EMouseMsg::getPos()); m_Callback(EMouseMsg::getPos());
m_bPressed = false; m_bPressed = false;
} }
} }
void e2d::EMouseDoubleClickListener::setCallback(const MOUSE_DBLCLK_LISTENER_CALLBACK & callback) void e2d::EMouseDoubleClickListener::setCallback(const MOUSE_DBLCLK_LISTENER_CALLBACK & callback)
{ {
m_callback = callback; m_Callback = callback;
} }

View File

@ -5,24 +5,24 @@ e2d::EMouseDragListener::EMouseDragListener()
{ {
} }
e2d::EMouseDragListener::EMouseDragListener(EString name) e2d::EMouseDragListener::EMouseDragListener(const EString & name)
: EMouseListener(name) : EMouseListener(name)
{ {
} }
e2d::EMouseDragListener::EMouseDragListener(const MOUSE_DRAG_LISTENER_CALLBACK & callback) e2d::EMouseDragListener::EMouseDragListener(const MOUSE_DRAG_LISTENER_CALLBACK & callback)
: EMouseListener() : 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) : EMouseListener(name)
, m_callback(callback) , m_Callback(callback)
{ {
} }
void e2d::EMouseDragListener::runCallback() void e2d::EMouseDragListener::_runCallback()
{ {
if (EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DOWN || if (EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DOWN ||
EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DBLCLK) EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DBLCLK)
@ -31,11 +31,11 @@ void e2d::EMouseDragListener::runCallback()
} }
else if (EMouseMsg::isLButtonDown() && EMouseMsg::getMsg() == EMouseMsg::MOVE) 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) void e2d::EMouseDragListener::setCallback(const MOUSE_DRAG_LISTENER_CALLBACK & callback)
{ {
m_callback = callback; m_Callback = callback;
} }

View File

@ -5,7 +5,7 @@ e2d::EMouseListener::EMouseListener()
{ {
} }
e2d::EMouseListener::EMouseListener(EString name) e2d::EMouseListener::EMouseListener(const EString & name)
: EListener(name) : EListener(name)
{ {
} }
@ -13,23 +13,23 @@ e2d::EMouseListener::EMouseListener(EString name)
e2d::EMouseListener::EMouseListener(const MOUSE_LISTENER_CALLBACK & callback) e2d::EMouseListener::EMouseListener(const MOUSE_LISTENER_CALLBACK & callback)
: EListener() : 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) : 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) void e2d::EMouseListener::setCallback(const MOUSE_LISTENER_CALLBACK & callback)
{ {
m_callback = callback; m_Callback = callback;
} }
void e2d::EMouseListener::bindWith(EScene * pParentScene) void e2d::EMouseListener::bindWith(EScene * pParentScene)
@ -38,7 +38,7 @@ void e2d::EMouseListener::bindWith(EScene * pParentScene)
if (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) if (pParentNode != nullptr && m_pParentScene == nullptr)
{ {
EMsgManager::bindListenerWith(this, pParentNode); EMsgManager::bindListener(this, pParentNode);
} }
} }

View File

@ -5,33 +5,33 @@ e2d::EMousePressListener::EMousePressListener()
{ {
} }
e2d::EMousePressListener::EMousePressListener(EString name) e2d::EMousePressListener::EMousePressListener(const EString & name)
: EMouseListener(name) : EMouseListener(name)
{ {
} }
e2d::EMousePressListener::EMousePressListener(const MOUSE_PRESS_LISTENER_CALLBACK & callback) e2d::EMousePressListener::EMousePressListener(const MOUSE_PRESS_LISTENER_CALLBACK & callback)
: EMouseListener() : 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) : EMouseListener(name)
, m_callback(callback) , m_Callback(callback)
{ {
} }
void e2d::EMousePressListener::runCallback() void e2d::EMousePressListener::_runCallback()
{ {
if (EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DOWN || if (EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DOWN ||
EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DBLCLK) EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DBLCLK)
{ {
m_callback(EMouseMsg::getPos()); m_Callback(EMouseMsg::getPos());
} }
} }
void e2d::EMousePressListener::setCallback(const MOUSE_PRESS_LISTENER_CALLBACK & callback) void e2d::EMousePressListener::setCallback(const MOUSE_PRESS_LISTENER_CALLBACK & callback)
{ {
m_callback = callback; m_Callback = callback;
} }

View File

@ -1,13 +1,11 @@
#include "..\enodes.h" #include "..\enodes.h"
#include "..\emsg.h"
#include "..\etools.h"
#include "..\Win\winbase.h" #include "..\Win\winbase.h"
#include <algorithm> #include <algorithm>
e2d::ENode::ENode() e2d::ENode::ENode()
: m_nOrder(0) : m_nOrder(0)
, m_fPosX(0)
, m_fPosY(0)
, m_fWidth(0)
, m_fHeight(0)
, m_fScaleX(1.0f) , m_fScaleX(1.0f)
, m_fScaleY(1.0f) , m_fScaleY(1.0f)
, m_fRotation(0) , m_fRotation(0)
@ -19,6 +17,7 @@ e2d::ENode::ENode()
, m_fAnchorY(0) , m_fAnchorY(0)
, m_Matri(D2D1::Matrix3x2F::Identity()) , m_Matri(D2D1::Matrix3x2F::Identity())
, m_bVisiable(true) , m_bVisiable(true)
, m_bDisplayedInScene(false)
, m_pParent(nullptr) , m_pParent(nullptr)
, m_pParentScene(nullptr) , m_pParentScene(nullptr)
, m_nHashName(0) , m_nHashName(0)
@ -27,7 +26,7 @@ e2d::ENode::ENode()
{ {
} }
e2d::ENode::ENode(EString name) e2d::ENode::ENode(const EString & name)
: ENode() : ENode()
{ {
setName(name); setName(name);
@ -37,6 +36,14 @@ e2d::ENode::~ENode()
{ {
} }
void e2d::ENode::onEnter()
{
}
void e2d::ENode::onExit()
{
}
void e2d::ENode::_callOn() void e2d::ENode::_callOn()
{ {
if (!m_bVisiable) 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() void e2d::ENode::_sortChildren()
{ {
if (m_bSortChildrenNeeded) if (m_bSortChildrenNeeded)
@ -110,26 +164,26 @@ void e2d::ENode::_updateTransformToReal()
{ {
// 计算锚点坐标 // 计算锚点坐标
D2D1_POINT_2F anchorPos = D2D1::Point2F( D2D1_POINT_2F anchorPos = D2D1::Point2F(
m_fWidth * m_fAnchorX, m_Size.width * m_fAnchorX,
m_fHeight * m_fAnchorY m_Size.height * m_fAnchorY
); );
// 计算左上角坐标 // 计算左上角坐标
D2D1_POINT_2F upperLeftCorner = D2D1::Point2F( D2D1_POINT_2F upperLeftCorner = D2D1::Point2F(
m_fPosX - m_fWidth * m_fAnchorX, m_Pos.x - m_Size.width * m_fAnchorX,
m_fPosY - m_fHeight * m_fAnchorY m_Pos.y - m_Size.height * m_fAnchorY
); );
// 二维矩形变换 // 二维矩形变换
m_Matri = D2D1::Matrix3x2F::Scale( m_Matri = D2D1::Matrix3x2F::Scale(
m_fScaleX, m_fScaleX,
m_fScaleY, m_fScaleY,
anchorPos anchorPos
) * D2D1::Matrix3x2F::Rotation(
m_fRotation,
anchorPos
) * D2D1::Matrix3x2F::Skew( ) * D2D1::Matrix3x2F::Skew(
m_fSkewAngleX, m_fSkewAngleX,
m_fSkewAngleY, m_fSkewAngleY,
anchorPos anchorPos
) * D2D1::Matrix3x2F::Rotation(
m_fRotation,
anchorPos
) * D2D1::Matrix3x2F::Translation( ) * D2D1::Matrix3x2F::Translation(
upperLeftCorner.x, upperLeftCorner.x,
upperLeftCorner.y upperLeftCorner.y
@ -178,24 +232,54 @@ bool e2d::ENode::isVisiable() const
return m_bVisiable; 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 float e2d::ENode::getWidth() const
{ {
return m_fWidth; return m_Size.width * m_fScaleX;
} }
float e2d::ENode::getHeight() const 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 float e2d::ENode::getScaleX() const
@ -238,48 +322,63 @@ void e2d::ENode::setOrder(int order)
m_nOrder = 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) 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; return;
m_fPosX = x; m_Pos.x = x;
m_fPosY = y; m_Pos.y = y;
m_bTransformChildrenNeeded = true; m_bTransformChildrenNeeded = true;
} }
void e2d::ENode::move(float x, float y) 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) void e2d::ENode::setWidth(float width)
{ {
this->setSize(width, m_fHeight); this->setSize(width, m_Size.height);
} }
void e2d::ENode::setHeight(float 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) 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; return;
m_fWidth = width; m_Size.width = width;
m_fHeight = height; m_Size.height = height;
m_bTransformChildrenNeeded = true; m_bTransformChildrenNeeded = true;
} }
@ -382,7 +481,7 @@ void e2d::ENode::setParent(ENode * parent)
void e2d::ENode::addChild(ENode * child, int order /* = 0 */) void e2d::ENode::addChild(ENode * child, int order /* = 0 */)
{ {
WARN_IF(child == nullptr, "ENode::addChild NULL pointer exception."); 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) if (child)
{ {
@ -401,6 +500,16 @@ void e2d::ENode::addChild(ENode * child, int order /* = 0 */)
_updateOpacity(child); _updateOpacity(child);
if (this->m_pParentScene)
{
child->_setParentScene(this->m_pParentScene);
}
if (this->m_bDisplayedInScene)
{
child->_onEnter();
}
m_bSortChildrenNeeded = true; m_bSortChildrenNeeded = true;
} }
} }
@ -415,7 +524,7 @@ e2d::EScene * e2d::ENode::getParentScene() const
return m_pParentScene; return m_pParentScene;
} }
std::vector<e2d::ENode*>& e2d::ENode::getChildren() e2d::EVector<e2d::ENode*>& e2d::ENode::getChildren()
{ {
return m_vChildren; return m_vChildren;
} }
@ -425,19 +534,14 @@ size_t e2d::ENode::getChildrenCount() const
return m_vChildren.size(); return m_vChildren.size();
} }
e2d::ENode * e2d::ENode::getChild(EString name) const e2d::ENode * e2d::ENode::getChild(const EString & name)
{
return ENode::getChild(name, this->m_vChildren);
}
e2d::ENode * e2d::ENode::getChild(EString name, const std::vector<ENode*> &children)
{ {
WARN_IF(name.empty(), "Invalid ENode name."); WARN_IF(name.empty(), "Invalid ENode name.");
std::hash<EString> h; std::hash<EString> h;
size_t hash = h(name); size_t hash = h(name);
for (const auto& child : children) for (const auto& child : m_vChildren)
{ {
// 不同的名称可能会有相同的 Hash 值,但是先比较 Hash 可以提升搜索速度 // 不同的名称可能会有相同的 Hash 值,但是先比较 Hash 可以提升搜索速度
if (child->m_nHashName == hash && child->m_sName == name) if (child->m_nHashName == hash && child->m_sName == name)
@ -446,14 +550,6 @@ e2d::ENode * e2d::ENode::getChild(EString name, const std::vector<ENode*> &child
return nullptr; return nullptr;
} }
void e2d::ENode::setParentScene(EScene * scene)
{
if (m_pParentScene)
{
m_pParentScene = scene;
}
}
void e2d::ENode::removeFromParent(bool release /* = false */) void e2d::ENode::removeFromParent(bool release /* = false */)
{ {
if (m_pParent) 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."); WARN_IF(child == nullptr, "ENode::removeChild NULL pointer exception.");
if (m_vChildren.empty()) if (m_vChildren.empty())
{ {
return; return false;
} }
if (child) if (child)
@ -480,16 +576,22 @@ void e2d::ENode::removeChild(ENode * child, bool release /* = false */)
{ {
m_vChildren.erase(m_vChildren.begin() + i); m_vChildren.erase(m_vChildren.begin() + i);
child->m_pParent = nullptr; child->m_pParent = nullptr;
if (child->m_pParentScene)
{
child->_setParentScene(nullptr);
}
child->_onExit();
child->release(); child->release();
if (release) if (release)
child->autoRelease(); 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."); WARN_IF(childName.empty(), "Invalid ENode name.");
@ -498,8 +600,10 @@ void e2d::ENode::removeChild(EString childName, bool release /* = false */)
return; return;
} }
// 计算名称 Hash 值
std::hash<EString> h; std::hash<EString> h;
size_t hash = h(childName); size_t hash = h(childName);
size_t size = m_vChildren.size(); size_t size = m_vChildren.size();
for (size_t i = 0; i < size; i++) 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); m_vChildren.erase(m_vChildren.begin() + i);
child->m_pParent = nullptr; child->m_pParent = nullptr;
if (child->m_pParentScene)
{
child->_setParentScene(nullptr);
}
child->_onExit();
child->release(); child->release();
if (release) if (release)
child->autoRelease(); 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) void e2d::ENode::setVisiable(bool value)
{ {
m_bVisiable = 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."); WARN_IF(name.empty(), "Invalid ENode name.");
@ -534,3 +667,12 @@ void e2d::ENode::setName(EString name)
m_nHashName = h(name); m_nHashName = h(name);
} }
} }
void e2d::ENode::_setParentScene(EScene * scene)
{
m_pParentScene = scene;
for (const auto &child : m_vChildren)
{
child->_setParentScene(scene);
}
}

View File

@ -6,19 +6,19 @@ e2d::ERectangle::ERectangle()
{ {
} }
e2d::EColor::Enum e2d::ERectangle::getColor() const e2d::EColor e2d::ERectangle::getColor() const
{ {
return m_Color; return m_Color;
} }
void e2d::ERectangle::setColor(EColor::Enum color) void e2d::ERectangle::setColor(EColor color)
{ {
m_Color = color; m_Color = color;
} }
void e2d::ERectangle::_onRender() 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)); GetSolidColorBrush()->SetColor(D2D1::ColorF(m_Color, m_fDisplayOpacity));
GetRenderTarget()->FillRectangle(&rectangle, GetSolidColorBrush()); GetRenderTarget()->FillRectangle(&rectangle, GetSolidColorBrush());
} }

View File

@ -1,33 +1,49 @@
#include "..\enodes.h" #include "..\enodes.h"
#include <map> #include <map>
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<size_t, ID2D1Bitmap*> s_mBitmapsFromFile; static std::map<size_t, ID2D1Bitmap*> s_mBitmapsFromFile;
//static std::map<size_t, size_t, ID2D1Bitmap*> s_mBitmapsFromResource; static std::map<ResKey, ID2D1Bitmap*> s_mBitmapsFromResource;
static ID2D1Bitmap * GetBitmapFromFile(e2d::EString fileName); static ID2D1Bitmap * GetBitmapFromFile(const e2d::EString & fileName);
static ID2D1Bitmap * GetBitmapFromResource(e2d::EString resourceName, e2d::EString resourceType); static ID2D1Bitmap * GetBitmapFromResource(const e2d::EString & resourceName, const e2d::EString & resourceType);
static bool _loadBitmapFromFile(e2d::EString fileName);
static bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceType);
e2d::ESprite::ESprite() e2d::ESprite::ESprite()
{ {
} }
e2d::ESprite::ESprite(EString imageFileName) e2d::ESprite::ESprite(const EString & imageFileName)
: ESprite() : ESprite()
{ {
setImage(imageFileName); setImage(imageFileName);
} }
e2d::ESprite::ESprite(EString resourceName, EString resourceType) e2d::ESprite::ESprite(const EString & resourceName, const EString & resourceType)
: ESprite() : ESprite()
{ {
setImage(resourceName, resourceType); 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."); 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); 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."); WARN_IF(resourceName.empty() || resourceType.empty(), "ESprite cannot load bitmap from NULL resource.");
@ -75,17 +91,22 @@ void e2d::ESprite::_onRender()
// Draw bitmap // Draw bitmap
GetRenderTarget()->DrawBitmap( GetRenderTarget()->DrawBitmap(
pBitmap, pBitmap,
D2D1::RectF(0, 0, m_fWidth, m_fHeight), D2D1::RectF(0, 0, getRealWidth(), getRealHeight()),
m_fDisplayOpacity m_fDisplayOpacity
); );
} }
} }
bool e2d::ESprite::preloadImage(const EString & fileName)
bool _loadBitmapFromFile(e2d::EString fileName)
{ {
std::hash<e2d::EString> h;
size_t hash = h(fileName);
if (s_mBitmapsFromFile.find(hash) != s_mBitmapsFromFile.end())
{
return true;
}
HRESULT hr = S_OK; HRESULT hr = S_OK;
IWICBitmapDecoder *pDecoder = nullptr; IWICBitmapDecoder *pDecoder = nullptr;
@ -94,6 +115,7 @@ bool _loadBitmapFromFile(e2d::EString fileName)
IWICFormatConverter *pConverter = nullptr; IWICFormatConverter *pConverter = nullptr;
ID2D1Bitmap *pBitmap = nullptr; ID2D1Bitmap *pBitmap = nullptr;
// 创建解码器
hr = GetImagingFactory()->CreateDecoderFromFilename( hr = GetImagingFactory()->CreateDecoderFromFilename(
fileName.c_str(), fileName.c_str(),
NULL, NULL,
@ -104,18 +126,19 @@ bool _loadBitmapFromFile(e2d::EString fileName)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// Create the initial frame. // 创建初始化框架
hr = pDecoder->GetFrame(0, &pSource); hr = pDecoder->GetFrame(0, &pSource);
} }
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// Convert the image format to 32bppPBGRA // 创建图片格式转换器
// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED). // (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
hr = GetImagingFactory()->CreateFormatConverter(&pConverter); hr = GetImagingFactory()->CreateFormatConverter(&pConverter);
} }
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// 图片格式转换成 32bbpPBGRA
hr = pConverter->Initialize( hr = pConverter->Initialize(
pSource, pSource,
GUID_WICPixelFormat32bppPBGRA, GUID_WICPixelFormat32bppPBGRA,
@ -127,7 +150,7 @@ bool _loadBitmapFromFile(e2d::EString fileName)
} }
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// Create a Direct2D bitmap from the WIC bitmap. // 从 WIC 位图创建一个 Direct2D 位图
hr = GetRenderTarget()->CreateBitmapFromWicBitmap( hr = GetRenderTarget()->CreateBitmapFromWicBitmap(
pConverter, pConverter,
NULL, NULL,
@ -136,6 +159,7 @@ bool _loadBitmapFromFile(e2d::EString fileName)
} }
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// 保存图片指针和图片的 Hash 名
std::hash<e2d::EString> h; std::hash<e2d::EString> h;
size_t hash = h(fileName); size_t hash = h(fileName);
@ -146,6 +170,7 @@ bool _loadBitmapFromFile(e2d::EString fileName)
); );
} }
// 释放相关资源
SafeReleaseInterface(&pDecoder); SafeReleaseInterface(&pDecoder);
SafeReleaseInterface(&pSource); SafeReleaseInterface(&pSource);
SafeReleaseInterface(&pStream); SafeReleaseInterface(&pStream);
@ -154,8 +179,19 @@ bool _loadBitmapFromFile(e2d::EString fileName)
return SUCCEEDED(hr); return SUCCEEDED(hr);
} }
bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceType) bool e2d::ESprite::preloadImage(const EString & resourceName, const EString & resourceType)
{ {
std::hash<e2d::EString> 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; HRESULT hr = S_OK;
IWICBitmapDecoder *pDecoder = nullptr; IWICBitmapDecoder *pDecoder = nullptr;
@ -170,29 +206,29 @@ bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceTyp
void *pImageFile = nullptr; void *pImageFile = nullptr;
DWORD imageFileSize = 0; 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; hr = imageResHandle ? S_OK : E_FAIL;
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// Load the resource. // 加载资源
imageResDataHandle = LoadResource(HINST_THISCOMPONENT, imageResHandle); imageResDataHandle = ::LoadResource(HINST_THISCOMPONENT, imageResHandle);
hr = imageResDataHandle ? S_OK : E_FAIL; hr = imageResDataHandle ? S_OK : E_FAIL;
} }
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// Lock it to get a system memory pointer. // 获取文件指针,并锁定资源
pImageFile = LockResource(imageResDataHandle); pImageFile = ::LockResource(imageResDataHandle);
hr = pImageFile ? S_OK : E_FAIL; hr = pImageFile ? S_OK : E_FAIL;
} }
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// Calculate the size. // 计算大小
imageFileSize = SizeofResource(HINST_THISCOMPONENT, imageResHandle); imageFileSize = SizeofResource(HINST_THISCOMPONENT, imageResHandle);
hr = imageFileSize ? S_OK : E_FAIL; hr = imageFileSize ? S_OK : E_FAIL;
@ -200,13 +236,13 @@ bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceTyp
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// Create a WIC stream to map onto the memory. // 创建 WIC 流
hr = GetImagingFactory()->CreateStream(&pStream); hr = GetImagingFactory()->CreateStream(&pStream);
} }
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// Initialize the stream with the memory pointer and size. // 初始化流
hr = pStream->InitializeFromMemory( hr = pStream->InitializeFromMemory(
reinterpret_cast<BYTE*>(pImageFile), reinterpret_cast<BYTE*>(pImageFile),
imageFileSize imageFileSize
@ -215,7 +251,7 @@ bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceTyp
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// Create a decoder for the stream. // 创建流的解码器
hr = GetImagingFactory()->CreateDecoderFromStream( hr = GetImagingFactory()->CreateDecoderFromStream(
pStream, pStream,
NULL, NULL,
@ -226,19 +262,20 @@ bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceTyp
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// Create the initial frame. // 创建初始化框架
hr = pDecoder->GetFrame(0, &pSource); hr = pDecoder->GetFrame(0, &pSource);
} }
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// Convert the image format to 32bppPBGRA // 创建图片格式转换器
// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED). // (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
hr = GetImagingFactory()->CreateFormatConverter(&pConverter); hr = GetImagingFactory()->CreateFormatConverter(&pConverter);
} }
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// 图片格式转换成 32bppPBGRA
hr = pConverter->Initialize( hr = pConverter->Initialize(
pSource, pSource,
GUID_WICPixelFormat32bppPBGRA, GUID_WICPixelFormat32bppPBGRA,
@ -251,7 +288,7 @@ bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceTyp
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
//create a Direct2D bitmap from the WIC bitmap. // 从 WIC 位图创建一个 Direct2D 位图
hr = GetRenderTarget()->CreateBitmapFromWicBitmap( hr = GetRenderTarget()->CreateBitmapFromWicBitmap(
pConverter, pConverter,
NULL, NULL,
@ -259,6 +296,22 @@ bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceTyp
); );
} }
if (SUCCEEDED(hr))
{
std::hash<e2d::EString> h;
ResKey key;
key.resNameHash = h(resourceName);
key.resTypeHash = h(resourceType);
s_mBitmapsFromResource.insert(
std::map<ResKey, ID2D1Bitmap*>::value_type(
key,
pBitmap)
);
}
// 释放相关资源
SafeReleaseInterface(&pDecoder); SafeReleaseInterface(&pDecoder);
SafeReleaseInterface(&pSource); SafeReleaseInterface(&pSource);
SafeReleaseInterface(&pStream); SafeReleaseInterface(&pStream);
@ -268,22 +321,47 @@ bool _loadBitmapFromResource(e2d::EString resourceName, e2d::EString resourceTyp
return SUCCEEDED(hr); 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<e2d::EString> h; std::hash<e2d::EString> h;
size_t hash = h(fileName); size_t hash = h(fileName);
if (s_mBitmapsFromFile.find(hash) == s_mBitmapsFromFile.end())
{
if (!_loadBitmapFromFile(fileName))
{
return nullptr;
}
}
return s_mBitmapsFromFile.at(hash); return s_mBitmapsFromFile.at(hash);
} }
ID2D1Bitmap * GetBitmapFromResource(e2d::EString resourceName, e2d::EString resourceType) ID2D1Bitmap * GetBitmapFromResource(const e2d::EString & resourceName, const e2d::EString & resourceType)
{
if (!e2d::ESprite::preloadImage(resourceName, resourceType))
{ {
return nullptr; return nullptr;
} }
ResKey key;
std::hash<e2d::EString> h;
key.resNameHash = h(resourceName);
key.resTypeHash = h(resourceType);
return s_mBitmapsFromResource.at(key);
}

View File

@ -1,5 +1,4 @@
#include "..\etools.h" #include "..\etools.h"
#include <vector>
// EObjectManager 释放池的实现机制: // EObjectManager 释放池的实现机制:
/// EObject 类中的引用计数m_nRefCount保证了指针的使用安全 /// EObject 类中的引用计数m_nRefCount保证了指针的使用安全
@ -9,7 +8,7 @@
/// 让其自动释放 /// 让其自动释放
// 释放池容器 // 释放池容器
static std::vector<e2d::EObject*> s_vPool; static e2d::EVector<e2d::EObject*> s_vPool;
// 标志释放池执行状态 // 标志释放池执行状态
static bool s_bNotifyed = false; static bool s_bNotifyed = false;
@ -19,11 +18,11 @@ void e2d::EObjectManager::__flush()
s_bNotifyed = false; s_bNotifyed = false;
// 创建迭代器 // 创建迭代器
static std::vector<e2d::EObject*>::iterator iter; static EVector<e2d::EObject*>::iterator iter;
// 循环遍历容器中的所有对象 // 循环遍历容器中的所有对象
for (iter = s_vPool.begin(); iter != s_vPool.end();) 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, 释放该对象 // 若对象的引用的计数为 0, 释放该对象
delete (*iter); delete (*iter);

View File

@ -1 +1,264 @@
#include "..\etools.h" #include "..\etools.h"
#include "..\enodes.h"
#include "..\Win\winbase.h"
static e2d::EVector<e2d::ETimer*> 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<ETimer*>::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();
}
}
}

View File

@ -1,43 +1,46 @@
#include "winbase.h" #include "winbase.h"
using namespace std::chrono;
HWND hwnd = nullptr;
ID2D1Factory * pDirect2dFactory = nullptr; HWND s_HWnd = nullptr;
ID2D1HwndRenderTarget * pRenderTarget = nullptr; ID2D1Factory * s_pDirect2dFactory = nullptr;
ID2D1SolidColorBrush * m_pSolidBrush = nullptr; ID2D1HwndRenderTarget * s_pRenderTarget = nullptr;
IWICImagingFactory * pIWICFactory = nullptr; ID2D1SolidColorBrush * s_pSolidBrush = nullptr;
IWICImagingFactory * s_pIWICFactory = nullptr;
steady_clock::time_point s_tNow;
HWND &GetHWnd() HWND &GetHWnd()
{ {
return hwnd; return s_HWnd;
} }
ID2D1Factory * &GetFactory() ID2D1Factory * &GetFactory()
{ {
return pDirect2dFactory; return s_pDirect2dFactory;
} }
IWICImagingFactory * &GetImagingFactory() IWICImagingFactory * &GetImagingFactory()
{ {
if (!pIWICFactory) if (!s_pIWICFactory)
{ {
CoCreateInstance( CoCreateInstance(
CLSID_WICImagingFactory, CLSID_WICImagingFactory,
NULL, NULL,
CLSCTX_INPROC_SERVER, CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory, IID_IWICImagingFactory,
reinterpret_cast<void **>(&pIWICFactory) reinterpret_cast<void **>(&s_pIWICFactory)
); );
} }
return pIWICFactory; return s_pIWICFactory;
} }
ID2D1HwndRenderTarget * &GetRenderTarget() ID2D1HwndRenderTarget * &GetRenderTarget()
{ {
if (!pRenderTarget) if (!s_pRenderTarget)
{ {
RECT rc; RECT rc;
GetClientRect(hwnd, &rc); GetClientRect(s_HWnd, &rc);
D2D1_SIZE_U size = D2D1::SizeU( D2D1_SIZE_U size = D2D1::SizeU(
rc.right - rc.left, rc.right - rc.left,
@ -46,23 +49,32 @@ ID2D1HwndRenderTarget * &GetRenderTarget()
// Create a Direct2D render target. // Create a Direct2D render target.
HRESULT hr; HRESULT hr;
hr = pDirect2dFactory->CreateHwndRenderTarget( hr = s_pDirect2dFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(), D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(hwnd, size), D2D1::HwndRenderTargetProperties(s_HWnd, size),
&pRenderTarget &s_pRenderTarget
); );
ASSERT(SUCCEEDED(hr), "Create Render Target Failed!"); ASSERT(SUCCEEDED(hr), "Create Render Target Failed!");
} }
return pRenderTarget; return s_pRenderTarget;
} }
ID2D1SolidColorBrush * &GetSolidColorBrush() 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<milliseconds>(s_tNow - tLast).count();
}

View File

@ -1,12 +1,6 @@
#pragma once #pragma once
#include "..\emacros.h" #include "..\emacros.h"
#include <d2d1.h> #include <chrono>
#include <d2d1helper.h>
#include <dwrite.h>
#include <wincodec.h>
#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "dwrite.lib")
#pragma comment(lib, "windowscodecs.lib")
#ifndef HINST_THISCOMPONENT #ifndef HINST_THISCOMPONENT
@ -25,6 +19,11 @@ ID2D1SolidColorBrush * &GetSolidColorBrush();
IWICImagingFactory * &GetImagingFactory(); IWICImagingFactory * &GetImagingFactory();
std::chrono::steady_clock::time_point &GetNow();
long long GetInterval(std::chrono::steady_clock::time_point tLast);
template<class Interface> template<class Interface>
inline void SafeReleaseInterface( inline void SafeReleaseInterface(
Interface **ppInterfaceToRelease Interface **ppInterfaceToRelease

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include "emacros.h" #include "emacros.h"
#include "ecommon.h" #include "ecommon.h"
#include <vector>
// Base Classes // Base Classes
@ -22,48 +21,58 @@ public:
~EApp(); ~EApp();
// 获取程序实例 // 初始化游戏界面
static EApp * get();
// Register the window class and call methods for instantiating drawing resources
bool init( bool init(
e2d::EString title, const EString &title, /* 窗口标题 */
e2d::ESize size, UINT32 width, /* 窗口宽度 */
bool bShowConsole = false UINT32 height, /* 窗口高度 */
bool showConsole = false/* 是否显示控制台 */
); );
// Register the window class and call methods for instantiating drawing resources // 初始化游戏界面
bool init( bool init(
e2d::EString title, const EString &title, /* 窗口标题 */
UINT32 width, UINT32 width, /* 窗口宽度 */
UINT32 height, UINT32 height, /* 窗口高度 */
bool bShowConsole = false int windowStyle, /* 窗口样式 */
bool showConsole = false/* 是否显示控制台 */
); );
// 启动程序 // 启动程序
void run(); void run();
// 修改窗口大小 // 预设画面帧数
static void setWindowSize( void setFPS(
int width, UINT32 fps
int height );
// 退出程序时的执行程序
virtual bool onExit();
// 释放所有内存资源
void free();
// 获取程序实例
static EApp * get();
// 显示或隐藏控制台(默认隐藏)
static void showConsole(
bool show
); );
// 修改窗口大小 // 修改窗口大小
static void setWindowSize( static void setWindowSize(
e2d::ESize size UINT32 width,
UINT32 height
); );
// 设置窗口标题 // 设置窗口标题
static void setWindowTitle( static void setWindowTitle(
e2d::EString title const EString &title
); );
// 获取窗口标题 // 获取窗口标题
static e2d::EString getTitle(); static EString getTitle();
// 获取窗口大小
static e2d::ESize getSize();
// 获取窗口宽度 // 获取窗口宽度
static UINT32 getWidth(); static UINT32 getWidth();
@ -74,7 +83,7 @@ public:
// 切换场景 // 切换场景
static void enterScene( static void enterScene(
EScene * scene, EScene * scene,
bool save = true bool saveCurrentScene = true
); );
// 返回上一场景 // 返回上一场景
@ -87,26 +96,31 @@ public:
static EScene * getCurrentScene(); static EScene * getCurrentScene();
// 获取 AppName // 获取 AppName
static e2d::EString getAppName(); static EString getAppName();
// 设置 AppName // 设置 AppName
static void setAppName( static void setAppName(
e2d::EString appname const EString &appname
); );
// 修改窗口背景色 // 修改窗口背景色
static void setBkColor( 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(); static void quit();
@ -115,31 +129,34 @@ public:
static void end(); static void end();
protected: protected:
// Initialize device-independent resources. // 创建设备无关资源
HRESULT _createDeviceIndependentResources(); HRESULT _createDeviceIndependentResources();
// Initialize device-dependent resources. // 创建设备相关资源
HRESULT _createDeviceResources(); HRESULT _createDeviceResources();
// Release device-dependent resource. // 释放设备相关资源
void _discardDeviceResources(); void _discardDeviceResources();
// 游戏主循环
void _mainLoop(); void _mainLoop();
// 游戏控制流程
void _onControl(); void _onControl();
// Draw content. // 渲染游戏画面
void _onRender(); bool _onRender();
// 进入下一场景
void _enterNextScene(); void _enterNextScene();
// ReSize the render target. // 重定 render target 大小
void _onResize( void _onResize(
UINT width, UINT32 width,
UINT height UINT32 height
); );
// The windows procedure. // 窗口程序
static LRESULT CALLBACK WndProc( static LRESULT CALLBACK WndProc(
HWND hWnd, HWND hWnd,
UINT message, UINT message,
@ -151,7 +168,8 @@ protected:
bool m_bRunning; bool m_bRunning;
EString m_sTitle; EString m_sTitle;
EString m_sAppName; EString m_sAppName;
EColor::Enum m_ClearColor; EColor m_ClearColor;
LONGLONG nAnimationInterval;
EScene * m_pCurrentScene; EScene * m_pCurrentScene;
EScene * m_pNextScene; EScene * m_pNextScene;
@ -173,28 +191,34 @@ public:
// 重写这个函数,它将在离开这个场景时自动执行 // 重写这个函数,它将在离开这个场景时自动执行
virtual void onExit(); virtual void onExit();
// 添加子成员到场景 // 添加子节点到场景
void add( void add(
e2d::ENode * child, ENode * child,
int zOrder = 0 int zOrder = 0
); );
// 删除子成员 // 删除子节点
bool remove( bool remove(
e2d::ENode * child, ENode * child,
bool autoRelease = true bool release = false
);
// 根据名称删除子节点
void remove(
const EString &childName,
bool release = false
); );
// 获取所有子节点 // 获取所有子节点
std::vector<e2d::ENode*> &getChildren(); EVector<e2d::ENode*> &getChildren();
// 获取子节点数量 // 获取子节点数量
size_t getChildrenCount() const; size_t getChildrenCount() const;
// 根据名称获取子节点 // 根据名称获取子节点
ENode * getChild( ENode * getChild(
EString childName const EString &childName
) const; );
// 清空所有子成员 // 清空所有子成员
void clearAllChildren(); void clearAllChildren();
@ -209,9 +233,6 @@ protected:
// 渲染场景画面 // 渲染场景画面
void _onRender(); void _onRender();
// 子节点排序
void _sortChildren();
// 进入场景时需调用该函数 // 进入场景时需调用该函数
virtual void _onEnter(); virtual void _onEnter();
@ -221,7 +242,7 @@ protected:
protected: protected:
bool m_bSortNeeded; bool m_bSortNeeded;
bool m_bWillSave; bool m_bWillSave;
std::vector<e2d::ENode*> m_vChildren; ENode * const m_Root;
}; };

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <Windows.h> #include <Windows.h>
#include <string> #include <string>
#include <atltypes.h> #include <vector>
#include <functional> #include <functional>
namespace e2d namespace e2d
@ -9,18 +9,46 @@ namespace e2d
typedef std::wstring EString; typedef std::wstring EString;
template<typename T>
using EVector = std::vector<T>;
typedef CSize ESize; struct EPoint
typedef CPoint EPoint;
typedef EPoint EVector;
typedef CRect ERect;
typedef struct
{ {
FLOAT width; EPoint()
FLOAT height; {
} ESize_F; 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<void()> KEY_LISTENER_CALLBACK; typedef std::function<void()> KEY_LISTENER_CALLBACK;
@ -30,12 +58,11 @@ typedef MOUSE_CLICK_LISTENER_CALLBACK MOUSE_PRESS_LISTENER_CALLBACK;
typedef MOUSE_CLICK_LISTENER_CALLBACK MOUSE_DBLCLK_LISTENER_CALLBACK; typedef MOUSE_CLICK_LISTENER_CALLBACK MOUSE_DBLCLK_LISTENER_CALLBACK;
typedef std::function<void(EPoint begin, EPoint end)> MOUSE_DRAG_LISTENER_CALLBACK; typedef std::function<void(EPoint begin, EPoint end)> MOUSE_DRAG_LISTENER_CALLBACK;
typedef std::function<void(int)> TIMER_CALLBACK;
class EColor
{
public:
enum Enum
enum EColor
{ {
AliceBlue = 0xF0F8FF, AliceBlue = 0xF0F8FF,
AntiqueWhite = 0xFAEBD7, AntiqueWhite = 0xFAEBD7,
@ -179,6 +206,4 @@ public:
YellowGreen = 0x9ACD32, YellowGreen = 0x9ACD32,
}; };
};
} }

View File

@ -26,6 +26,13 @@
#include <wchar.h> #include <wchar.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <d2d1.h>
#include <d2d1helper.h>
#include <dwrite.h>
#include <wincodec.h>
#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "dwrite.lib")
#pragma comment(lib, "windowscodecs.lib")
#ifndef ASSERT_IF #ifndef ASSERT_IF

View File

@ -30,10 +30,10 @@ public:
}; };
// 获取鼠标横坐标 // 获取鼠标横坐标
static DWORD getX(); static DWORD getPosX();
// 获取鼠标纵坐标 // 获取鼠标纵坐标
static DWORD getY(); static DWORD getPosY();
// 获取鼠标坐标 // 获取鼠标坐标
static EPoint getPos(); static EPoint getPos();
@ -67,7 +67,7 @@ protected:
// 按键消息 // 按键消息
class EKeyMsg class EKeyboardMsg
{ {
friend EMsgManager; friend EMsgManager;
@ -147,7 +147,7 @@ public:
EListener(); EListener();
EListener( EListener(
EString name const EString &name
); );
// 获取监听器状态 // 获取监听器状态
@ -162,12 +162,6 @@ public:
// 停止监听 // 停止监听
void stop(); void stop();
// 进入等待状态
void wait();
// 唤醒
void notify();
// 获取监听器名称 // 获取监听器名称
EString getName() const; EString getName() const;
@ -179,7 +173,7 @@ public:
// 设置监听器名称 // 设置监听器名称
void setName( void setName(
EString name const EString &name
); );
// 绑定监听器到场景 // 绑定监听器到场景
@ -192,6 +186,16 @@ public:
ENode * pParentNode ENode * pParentNode
) = 0; ) = 0;
protected:
// 进入等待状态
void _wait();
// 唤醒
void _notify();
// 执行监听器回调函数
virtual void _runCallback() = 0;
protected: protected:
EString m_sName; EString m_sName;
bool m_bRunning; bool m_bRunning;
@ -205,11 +209,13 @@ protected:
class EMouseListener : class EMouseListener :
public EListener public EListener
{ {
friend EMsgManager;
public: public:
EMouseListener(); EMouseListener();
EMouseListener( EMouseListener(
EString name const EString &name
); );
EMouseListener( EMouseListener(
@ -217,13 +223,10 @@ public:
); );
EMouseListener( EMouseListener(
EString name, const EString &name,
const MOUSE_LISTENER_CALLBACK &callback const MOUSE_LISTENER_CALLBACK &callback
); );
// 执行监听器回调函数
virtual void runCallback();
// 设置监听器回调函数 // 设置监听器回调函数
void setCallback( void setCallback(
const MOUSE_LISTENER_CALLBACK &callback const MOUSE_LISTENER_CALLBACK &callback
@ -240,7 +243,11 @@ public:
) override; ) override;
protected: protected:
MOUSE_LISTENER_CALLBACK m_callback; // 执行监听器回调函数
virtual void _runCallback() override;
protected:
MOUSE_LISTENER_CALLBACK m_Callback;
}; };
@ -252,7 +259,7 @@ public:
EMousePressListener(); EMousePressListener();
EMousePressListener( EMousePressListener(
EString name const EString &name
); );
EMousePressListener( EMousePressListener(
@ -260,20 +267,21 @@ public:
); );
EMousePressListener( EMousePressListener(
EString name, const EString &name,
const MOUSE_PRESS_LISTENER_CALLBACK &callback const MOUSE_PRESS_LISTENER_CALLBACK &callback
); );
// 执行监听器回调函数
virtual void runCallback() override;
// 设置监听器回调函数 // 设置监听器回调函数
void setCallback( void setCallback(
const MOUSE_PRESS_LISTENER_CALLBACK &callback const MOUSE_PRESS_LISTENER_CALLBACK &callback
); );
protected: 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();
EMouseClickListener( EMouseClickListener(
EString name const EString &name
); );
EMouseClickListener( EMouseClickListener(
@ -293,21 +301,22 @@ public:
); );
EMouseClickListener( EMouseClickListener(
EString name, const EString &name,
const MOUSE_CLICK_LISTENER_CALLBACK &callback const MOUSE_CLICK_LISTENER_CALLBACK &callback
); );
// 执行监听器回调函数
virtual void runCallback() override;
// 设置监听器回调函数 // 设置监听器回调函数
void setCallback( void setCallback(
const MOUSE_CLICK_LISTENER_CALLBACK &callback const MOUSE_CLICK_LISTENER_CALLBACK &callback
); );
protected:
// 执行监听器回调函数
virtual void _runCallback() override;
protected: protected:
bool m_bPressed; bool m_bPressed;
MOUSE_CLICK_LISTENER_CALLBACK m_callback; MOUSE_CLICK_LISTENER_CALLBACK m_Callback;
}; };
@ -319,7 +328,7 @@ public:
EMouseDoubleClickListener(); EMouseDoubleClickListener();
EMouseDoubleClickListener( EMouseDoubleClickListener(
EString name const EString &name
); );
EMouseDoubleClickListener( EMouseDoubleClickListener(
@ -327,21 +336,22 @@ public:
); );
EMouseDoubleClickListener( EMouseDoubleClickListener(
EString name, const EString &name,
const MOUSE_DBLCLK_LISTENER_CALLBACK &callback const MOUSE_DBLCLK_LISTENER_CALLBACK &callback
); );
// 执行监听器回调函数
virtual void runCallback() override;
// 设置监听器回调函数 // 设置监听器回调函数
void setCallback( void setCallback(
const MOUSE_DBLCLK_LISTENER_CALLBACK &callback const MOUSE_DBLCLK_LISTENER_CALLBACK &callback
); );
protected:
// 执行监听器回调函数
virtual void _runCallback() override;
protected: protected:
bool m_bPressed; bool m_bPressed;
MOUSE_DBLCLK_LISTENER_CALLBACK m_callback; MOUSE_DBLCLK_LISTENER_CALLBACK m_Callback;
}; };
@ -353,7 +363,7 @@ public:
EMouseDragListener(); EMouseDragListener();
EMouseDragListener( EMouseDragListener(
EString name const EString &name
); );
EMouseDragListener( EMouseDragListener(
@ -361,21 +371,22 @@ public:
); );
EMouseDragListener( EMouseDragListener(
EString name, const EString &name,
const MOUSE_DRAG_LISTENER_CALLBACK &callback const MOUSE_DRAG_LISTENER_CALLBACK &callback
); );
// 执行监听器回调函数
virtual void runCallback() override;
// 设置监听器回调函数 // 设置监听器回调函数
void setCallback( void setCallback(
const MOUSE_DRAG_LISTENER_CALLBACK &callback const MOUSE_DRAG_LISTENER_CALLBACK &callback
); );
protected:
// 执行监听器回调函数
virtual void _runCallback() override;
protected: protected:
EPoint m_Begin; EPoint m_Begin;
MOUSE_DRAG_LISTENER_CALLBACK m_callback; MOUSE_DRAG_LISTENER_CALLBACK m_Callback;
}; };
@ -383,11 +394,13 @@ protected:
class EKeyboardListener : class EKeyboardListener :
public EListener public EListener
{ {
friend EMsgManager;
public: public:
EKeyboardListener(); EKeyboardListener();
EKeyboardListener( EKeyboardListener(
EString name const EString &name
); );
EKeyboardListener( EKeyboardListener(
@ -395,13 +408,10 @@ public:
); );
EKeyboardListener( EKeyboardListener(
EString name, const EString &name,
const KEY_LISTENER_CALLBACK &callback const KEY_LISTENER_CALLBACK &callback
); );
// 执行监听器回调函数
virtual void runCallback();
// 设置监听器回调函数 // 设置监听器回调函数
void setCallback( void setCallback(
const KEY_LISTENER_CALLBACK &callback const KEY_LISTENER_CALLBACK &callback
@ -418,32 +428,39 @@ public:
) override; ) override;
protected: protected:
KEY_LISTENER_CALLBACK m_callback; // 执行监听器回调函数
virtual void _runCallback() override;
protected:
KEY_LISTENER_CALLBACK m_Callback;
}; };
// 按键按下消息监听 // 按键按下消息监听
class EKeyPressListener : class EKeyboardPressListener :
public EKeyboardListener public EKeyboardListener
{ {
friend EMsgManager;
public: public:
EKeyPressListener(); EKeyboardPressListener();
EKeyPressListener( EKeyboardPressListener(
EString name const EString &name
); );
EKeyPressListener( EKeyboardPressListener(
const KEY_LISTENER_CALLBACK &callback const KEY_LISTENER_CALLBACK &callback
); );
EKeyPressListener( EKeyboardPressListener(
EString name, const EString &name,
const KEY_LISTENER_CALLBACK &callback const KEY_LISTENER_CALLBACK &callback
); );
protected:
// 执行监听器回调函数 // 执行监听器回调函数
virtual void runCallback() override; virtual void _runCallback() override;
}; };
@ -451,122 +468,192 @@ public:
class EMsgManager class EMsgManager
{ {
friend EApp; friend EApp;
friend EScene;
friend ENode;
public: public:
// 绑定鼠标消息监听器到场景 // 绑定鼠标消息监听器到场景
static void bindListenerWith( static void bindListener(
EMouseListener * listener, EMouseListener * listener,
EScene * pParentScene EScene * pParentScene
); );
// 绑定鼠标消息监听器到场景 // 绑定鼠标消息监听器到节点
static void bindListenerWith( static void bindListener(
EKeyboardListener * listener,
EScene * pParentScene
);
// 绑定按键消息监听器到节点
static void bindListenerWith(
EMouseListener * listener, EMouseListener * listener,
ENode * pParentNode ENode * pParentNode
); );
// 绑定按键消息监听器到节点 // 启动具有相同名称的鼠标消息监听器
static void bindListenerWith( static void startMouseListeners(
EKeyboardListener * listener, 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 ENode * pParentNode
); );
// 启动具有相同名称的监听器 // 停止绑定在节点上的所有鼠标消息监听器
static void startListener( static void stopAllMouseListenersBindedWith(
EString name ENode * pParentNode
); );
// 停止具有相同名称的监听器 // 清除绑定在节点上的所有鼠标消息监听器
static void stopListener( static void clearAllMouseListenersBindedWith(
EString name ENode * pParentNode
);
// 删除具有相同名称的监听器
static void delListener(
EString name
); );
// 启动所有鼠标消息监听器 // 启动所有鼠标消息监听器
static void startAllMouseListener(); static void startAllMouseListeners();
// 停止所有鼠标消息监听器 // 停止所有鼠标消息监听器
static void stopAllMouseListener(); static void stopAllMouseListeners();
// 清除所有鼠标消息监听器 // 清除所有鼠标消息监听器
static void clearAllMouseListeners(); 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 clearAllKeyboardListeners();
// 启动绑定在场景上的所有鼠标消息监听器 private:
static void startAllMouseListenersBindWithScene( // 挂起绑定在场景上的所有鼠标消息监听器
static void _waitAllMouseListenersBindedWith(
EScene * pParentScene EScene * pParentScene
); );
// 重启绑定在场景上的所有鼠标消息监听器 // 重启绑定在场景上的所有鼠标消息监听器
static void stopAllMouseListenersBindWithScene( static void _notifyAllMouseListenersBindedWith(
EScene * pParentScene EScene * pParentScene
); );
// 启动绑定在场景上的所有按键消息监听器 // 挂起绑定在节点上的所有鼠标消息监听器
static void startAllKeyboardListenersBindWithScene( static void _waitAllMouseListenersBindedWith(
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(
ENode * pParentNode ENode * pParentNode
); );
// 重启绑定在节点上的所有监听器 // 重启绑定在节点上的所有鼠标消息监听器
static void notifyAllListenersBindWithNode( static void _notifyAllMouseListenersBindedWith(
ENode * pParentNode ENode * pParentNode
); );
// 清空绑定在节点上的所有监听器
static void clearAllListenersBindWithNode( // 挂起绑定在场景及其子节点上的所有按键监听器
static void _waitAllKeyboardListenersBindedWith(
EScene * pParentScene
);
// 重启绑定在场景及其子节点上的所有按键监听器
static void _notifyAllKeyboardListenersBindedWith(
EScene * pParentScene
);
// 挂起绑定在节点上的所有按键监听器
static void _waitAllKeyboardListenersBindedWith(
ENode * pParentNode ENode * pParentNode
); );
private: // 重启绑定在节点上的所有按键监听器
static void _notifyAllKeyboardListenersBindedWith(
ENode * pParentNode
);
// 鼠标消息程序
static void MouseProc( static void MouseProc(
UINT message, UINT message,
WPARAM wParam, WPARAM wParam,
LPARAM lParam LPARAM lParam
); );
// 按键消息程序
static void KeyboardProc( static void KeyboardProc(
UINT message, UINT message,
WPARAM wParam, WPARAM wParam,

View File

@ -14,22 +14,34 @@ public:
ENode(); ENode();
ENode( ENode(
EString name const EString & name
); );
virtual ~ENode(); virtual ~ENode();
// 节点是否显示 // 重写这个函数,它将在节点进入场景时自动执行
virtual void onEnter();
// 重写这个函数,它将在节点离开场景时自动执行
virtual void onExit();
// 获取节点显示状态
virtual bool isVisiable() const; virtual bool isVisiable() const;
// 获取节点名称
virtual EString getName() const;
// 获取节点绘图顺序 // 获取节点绘图顺序
virtual int getOrder() 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; virtual float getWidth() const;
@ -37,6 +49,18 @@ public:
// 获取节点高度 // 获取节点高度
virtual float getHeight() const; virtual float getHeight() const;
// 获取节点宽度(不考虑缩放)
virtual float getRealWidth() const;
// 获取节点高度(不考虑缩放)
virtual float getRealHeight() const;
// 获取节点大小(不考虑缩放)
virtual ESize getRealSize() const;
// 获取节点大小
virtual ESize getSize() const;
// 获取节点横向缩放比例 // 获取节点横向缩放比例
virtual float getScaleX() const; virtual float getScaleX() const;
@ -62,20 +86,14 @@ public:
virtual EScene * getParentScene() const; virtual EScene * getParentScene() const;
// 获取所有子节点 // 获取所有子节点
virtual std::vector<ENode*> &getChildren(); virtual EVector<ENode*> &getChildren();
// 获取子节点数量 // 获取子节点数量
virtual size_t getChildrenCount() const; virtual size_t getChildrenCount() const;
// 根据名字获取子节点 // 根据名字获取子节点
virtual ENode * getChild( virtual ENode * getChild(
EString name const EString & name
) const;
// 根据名字获取子节点
static ENode * getChild(
EString name,
const std::vector<ENode*> &children
); );
// 设置节点是否显示 // 设置节点是否显示
@ -85,19 +103,24 @@ public:
// 设置节点名称 // 设置节点名称
virtual void setName( virtual void setName(
EString name const EString & name
); );
// 设置节点横坐标 // 设置节点横坐标
virtual void setX( virtual void setPosX(
float x float x
); );
// 设置节点纵坐标 // 设置节点纵坐标
virtual void setY( virtual void setPosY(
float y float y
); );
// 设置节点坐标
virtual void setPos(
const EPoint & point
);
// 设置节点坐标 // 设置节点坐标
virtual void setPos( virtual void setPos(
float x, float x,
@ -110,6 +133,11 @@ public:
float y float y
); );
// 移动节点
virtual void move(
const EVec & v
);
// 设置节点宽度 // 设置节点宽度
virtual void setWidth( virtual void setWidth(
float width float width
@ -120,6 +148,11 @@ public:
float height float height
); );
// 设置节点大小
virtual void setSize(
const ESize & size
);
// 设置节点大小 // 设置节点大小
virtual void setSize( virtual void setSize(
float width, float width,
@ -207,11 +240,6 @@ public:
float anchorY float anchorY
); );
// 设置节点所在场景
virtual void setParentScene(
EScene * scene
);
// 设置父节点 // 设置父节点
virtual void setParent( virtual void setParent(
ENode* parent ENode* parent
@ -229,14 +257,19 @@ public:
); );
// 移除子节点 // 移除子节点
virtual void removeChild( virtual bool removeChild(
ENode * child, ENode * child,
bool release = false bool release = false
); );
// 移除子节点 // 移除子节点
virtual void removeChild( virtual void removeChild(
EString childName, const EString & childName,
bool release = false
);
// 移除所有节点
virtual void clearAllChildren(
bool release = false bool release = false
); );
@ -247,9 +280,23 @@ protected:
// 渲染节点 // 渲染节点
virtual void _onRender(); virtual void _onRender();
// 节点被添加到场景时的执行程序
virtual void _onEnter();
// 节点从场景中消失时的执行程序
virtual void _onExit();
// 节点清除时的执行程序
virtual void _onClear();
// 子节点排序 // 子节点排序
void _sortChildren(); void _sortChildren();
// 设置节点所在场景
virtual void _setParentScene(
EScene * scene
);
// 只考虑自身进行二维矩阵变换 // 只考虑自身进行二维矩阵变换
void _updateTransformToReal(); void _updateTransformToReal();
@ -268,10 +315,8 @@ protected:
protected: protected:
EString m_sName; EString m_sName;
size_t m_nHashName; size_t m_nHashName;
float m_fPosX; EPoint m_Pos;
float m_fPosY; ESize m_Size;
float m_fWidth;
float m_fHeight;
float m_fScaleX; float m_fScaleX;
float m_fScaleY; float m_fScaleY;
float m_fRotation; float m_fRotation;
@ -281,14 +326,15 @@ protected:
float m_fRealOpacity; float m_fRealOpacity;
float m_fAnchorX; float m_fAnchorX;
float m_fAnchorY; float m_fAnchorY;
D2D1::Matrix3x2F m_Matri;
int m_nOrder; int m_nOrder;
bool m_bVisiable; bool m_bVisiable;
bool m_bDisplayedInScene;
bool m_bSortChildrenNeeded; bool m_bSortChildrenNeeded;
bool m_bTransformChildrenNeeded; bool m_bTransformChildrenNeeded;
EScene * m_pParentScene; EScene * m_pParentScene;
ENode * m_pParent; ENode * m_pParent;
std::vector<ENode*> m_vChildren; D2D1::Matrix3x2F m_Matri;
EVector<ENode*> m_vChildren;
}; };
@ -298,15 +344,15 @@ class ERectangle :
public: public:
ERectangle(); ERectangle();
EColor::Enum getColor() const; EColor getColor() const;
void setColor(EColor::Enum color); void setColor(EColor color);
protected: protected:
virtual void _onRender() override; virtual void _onRender() override;
protected: protected:
EColor::Enum m_Color; EColor m_Color;
}; };
@ -314,17 +360,47 @@ class ESprite :
public ENode public ENode
{ {
public: public:
// 创建一个空精灵
ESprite(); ESprite();
ESprite(EString imageFileName); // 从文件图片创建精灵
ESprite(
const EString & imageFileName
);
ESprite(EString resourceName, EString resourceType); // 从资源图片创建精灵
ESprite(
const EString & resourceName,
const EString & resourceType
);
void setImage(EString fileName); // 从文件加载图片
void setImage(
const EString & fileName
);
void setImage(EString resourceName, EString resourceType); // 从资源加载图片
void setImage(
const EString & resourceName,
const EString & resourceType
);
// 预加载资源
static bool preloadImage(
const EString & fileName
);
// 预加载资源
static bool preloadImage(
const EString & resourceName,
const EString & resourceType
);
// 清空图片缓存
static void clearCache();
protected: protected:
// 渲染精灵
virtual void _onRender() override; virtual void _onRender() override;
protected: protected:

View File

@ -1,9 +1,12 @@
#pragma once #pragma once
#include "ebase.h" #include "ebase.h"
#include <chrono>
namespace e2d namespace e2d
{ {
class ETimerManager;
// 对象管理器 // 对象管理器
class EObjectManager class EObjectManager
{ {
@ -31,14 +34,187 @@ private:
class ETimer : class ETimer :
public EObject 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 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();
}; };
} }