Magic_Game/core/Base/Window.cpp

537 lines
10 KiB
C++
Raw Normal View History

2018-04-21 21:24:46 +08:00
#include "..\e2dbase.h"
#include "..\e2dmanager.h"
2018-08-13 23:47:10 +08:00
#include "..\e2dnode.h"
2018-01-30 16:45:38 +08:00
#include <imm.h>
#pragma comment (lib ,"imm32.lib")
2018-08-02 00:27:45 +08:00
#define WINDOW_STYLE WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_THICKFRAME
#define REGISTER_CLASS L"Easy2DApp"
2018-01-30 16:45:38 +08:00
2018-08-28 00:06:10 +08:00
e2d::Window * e2d::Window::_instance = nullptr;
e2d::Window * e2d::Window::getInstance()
{
if (!_instance)
_instance = new (std::nothrow) Window;
return _instance;
}
void e2d::Window::destroyInstance()
{
if (_instance)
{
delete _instance;
_instance = nullptr;
}
}
e2d::Window::Window()
: _hWnd(nullptr)
2018-08-28 00:06:10 +08:00
, _width(640)
, _height(480)
, _title(L"Easy2D Game")
, _iconID(0)
2018-07-28 22:22:58 +08:00
, _dpi(0.f)
{
::CoInitialize(nullptr);
2018-08-19 15:11:20 +08:00
// <20><>ȡϵͳ DPI
_dpi = static_cast<float>(::GetDpiForSystem());
2018-08-15 00:06:03 +08:00
}
e2d::Window::~Window()
{
// <20>رտ<D8B1><D5BF><EFBFBD>̨
if (::GetConsoleWindow())
::FreeConsole();
// <20>رմ<D8B1><D5B4><EFBFBD>
if (_hWnd)
::DestroyWindow(_hWnd);
::CoUninitialize();
2018-08-15 00:06:03 +08:00
}
bool e2d::Window::createMutex(const String & mutex)
{
if (mutex.isEmpty())
2018-08-15 00:06:03 +08:00
return false;
HANDLE hMutex = ::CreateMutex(nullptr, TRUE, LPCWSTR(L"Easy2DApp-" + mutex));
if (hMutex == nullptr)
{
WARN("CreateMutex Failed!");
return false;
}
2018-08-15 00:06:03 +08:00
else if (::GetLastError() == ERROR_ALREADY_EXISTS)
{
// <20>رս<D8B1><D5BD>̻<EFBFBD><CCBB><EFBFBD><EFBFBD><EFBFBD>
::CloseHandle(hMutex);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>
if (!this->_title.isEmpty())
2018-08-15 00:06:03 +08:00
{
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ھ<EFBFBD><DABE><EFBFBD>
HWND hProgramWnd = ::FindWindow(REGISTER_CLASS, (LPCTSTR)_title);
if (hProgramWnd)
{
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ״̬
WINDOWPLACEMENT wpm;
::GetWindowPlacement(hProgramWnd, &wpm);
// <20><><EFBFBD><EFBFBD><EFBFBD>еij<D0B5><C4B3>򴰿ڻ<F2B4B0BF>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
wpm.showCmd = SW_SHOW;
::SetWindowPlacement(hProgramWnd, &wpm);
::SetWindowPos(hProgramWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
}
}
return false;
}
return true;
2018-01-30 16:45:38 +08:00
}
2018-08-19 15:11:20 +08:00
e2d::Rect e2d::Window::_locate(int width, int height)
2018-08-02 00:27:45 +08:00
{
Rect result;
2018-08-19 15:11:20 +08:00
RECT wRECT = { 0, 0, LONG(ceil(width * _dpi / 96.f)), LONG(ceil(height * _dpi / 96.f)) };
2018-08-02 00:27:45 +08:00
int maxWidth = ::GetSystemMetrics(SM_CXSCREEN);
int maxHeight = ::GetSystemMetrics(SM_CYSCREEN);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵĴ<CAB5><C4B4>ڴ<EFBFBD>С
::AdjustWindowRectEx(&wRECT, WINDOW_STYLE, FALSE, NULL);
width = static_cast<int>(wRECT.right - wRECT.left);
height = static_cast<int>(wRECT.bottom - wRECT.top);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4>ڴ<EFBFBD>С<EFBFBD>ȷֱ<C8B7><D6B1>ʴ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
WARN_IF(maxWidth < width || maxHeight < height, "The window is larger than screen!");
width = std::min(width, maxWidth);
height = std::min(height, maxHeight);
float x = float((maxWidth - width) / 2), y = float((maxHeight - height) / 2);
return std::move(Rect(x, y, float(width), float(height)));
}
void e2d::Window::poll()
2018-01-30 16:45:38 +08:00
{
while (::PeekMessage(&_msg, nullptr, 0, 0, PM_REMOVE))
2018-01-30 16:45:38 +08:00
{
::TranslateMessage(&_msg);
::DispatchMessage(&_msg);
2018-01-30 16:45:38 +08:00
}
}
int e2d::Window::getWidth() const
2018-01-30 16:45:38 +08:00
{
2018-08-15 00:06:03 +08:00
return _width;
2018-01-30 16:45:38 +08:00
}
int e2d::Window::getHeight() const
2018-01-30 16:45:38 +08:00
{
2018-08-15 00:06:03 +08:00
return _height;
2018-01-30 16:45:38 +08:00
}
e2d::Size e2d::Window::getSize() const
2018-01-30 16:45:38 +08:00
{
2018-08-15 00:06:03 +08:00
return Size(float(_width), float(_height));
}
float e2d::Window::getDpi() const
2018-07-28 22:22:58 +08:00
{
return _dpi;
}
e2d::String e2d::Window::getTitle() const
{
return _title;
2018-01-30 16:45:38 +08:00
}
2018-08-28 00:06:10 +08:00
HWND e2d::Window::getHWnd()
2018-01-30 16:45:38 +08:00
{
2018-08-28 00:06:10 +08:00
if (!_hWnd)
{
WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpszClassName = REGISTER_CLASS;
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = Window::WndProc;
wcex.hIcon = nullptr;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = sizeof(LONG_PTR);
wcex.hInstance = HINST_THISCOMPONENT;
wcex.hbrBackground = nullptr;
wcex.lpszMenuName = nullptr;
wcex.hCursor = ::LoadCursor(nullptr, IDC_ARROW);
if (_iconID != 0)
{
wcex.hIcon = (HICON)::LoadImage(
HINST_THISCOMPONENT,
MAKEINTRESOURCE(_iconID),
IMAGE_ICON,
0,
0,
LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE
);
}
// ע<><EFBFBD><E1B4B0><EFBFBD><EFBFBD>
RegisterClassEx(&wcex);
// <20><><EFBFBD><EFBFBD>ڴ<EFBFBD>С
Rect clientRect = _locate(_width, _height);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
_hWnd = ::CreateWindowEx(
NULL,
REGISTER_CLASS,
(LPCTSTR)_title,
WINDOW_STYLE,
int(clientRect.origin.x),
int(clientRect.origin.y),
int(clientRect.size.width),
int(clientRect.size.height),
nullptr,
nullptr,
HINST_THISCOMPONENT,
this
);
if (_hWnd)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
setTypewritingEnabled(false);
// <20><><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>̨<EFBFBD>رհ<D8B1>ť
HWND consoleHWnd = ::GetConsoleWindow();
if (consoleHWnd)
{
HMENU hmenu = ::GetSystemMenu(consoleHWnd, FALSE);
::RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND);
}
}
else
{
::UnregisterClass(REGISTER_CLASS, HINST_THISCOMPONENT);
throw SystemException("Create window failed");
}
}
return _hWnd;
2018-01-30 16:45:38 +08:00
}
void e2d::Window::setSize(int width, int height)
2018-01-30 16:45:38 +08:00
{
2018-08-15 00:06:03 +08:00
if (_width == width && _height == height)
return;
2018-08-15 00:06:03 +08:00
_width = width;
_height = height;
2018-07-03 23:39:00 +08:00
if (_hWnd)
{
2018-08-19 15:11:20 +08:00
Rect wRect = _locate(width, height);
2018-08-02 00:27:45 +08:00
::MoveWindow(
_hWnd,
int(wRect.origin.x),
int(wRect.origin.y),
int(wRect.size.width),
int(wRect.size.height),
TRUE
);
2018-07-03 23:39:00 +08:00
}
2018-01-30 16:45:38 +08:00
}
void e2d::Window::setTitle(const String& title)
2018-01-30 16:45:38 +08:00
{
2018-08-02 00:27:45 +08:00
_title = title;
2018-07-03 23:39:00 +08:00
if (_hWnd)
{
::SetWindowText(_hWnd, (LPCWSTR)title);
}
2018-01-30 16:45:38 +08:00
}
void e2d::Window::setIcon(int iconID)
{
2018-07-03 23:39:00 +08:00
this->_iconID = iconID;
if (_hWnd)
{
HICON hIcon = (HICON)::LoadImage(HINST_THISCOMPONENT, MAKEINTRESOURCE(iconID), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
// <20><><EFBFBD>ô<EFBFBD><C3B4>ڵ<EFBFBD>ͼ<EFBFBD><CDBC>
::SendMessage(_hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
::SendMessage(_hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
}
}
void e2d::Window::setCursor(Cursor cursor)
{
LPCWSTR pCursorName = nullptr;
switch (cursor)
{
2018-05-24 20:37:34 +08:00
case Cursor::Normal:
pCursorName = IDC_ARROW;
break;
2018-05-24 20:37:34 +08:00
case Cursor::Hand:
pCursorName = IDC_HAND;
break;
2018-05-24 20:37:34 +08:00
case Cursor::No:
pCursorName = IDC_NO;
break;
2018-05-24 20:37:34 +08:00
case Cursor::Wait:
pCursorName = IDC_WAIT;
break;
2018-05-24 20:37:34 +08:00
case Cursor::ArrowWait:
pCursorName = IDC_APPSTARTING;
break;
default:
break;
}
HCURSOR hCursor = ::LoadCursor(nullptr, pCursorName);
::SetCursor(hCursor);
}
void e2d::Window::setConsoleEnabled(bool enabled)
2018-01-30 16:45:38 +08:00
{
// <20><><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD><D1B4>ڵĿ<DAB5><C4BF><EFBFBD>̨<EFBFBD><CCA8><EFBFBD><EFBFBD>
HWND hwnd = ::GetConsoleWindow();
// <20>رտ<D8B1><D5BF><EFBFBD>̨
2018-07-13 01:33:53 +08:00
if (enabled)
2018-01-30 16:45:38 +08:00
{
if (hwnd)
{
::ShowWindow(hwnd, SW_SHOWNORMAL);
}
else
{
// <20><>ʾһ<CABE><D2BB><EFBFBD>¿<EFBFBD><C2BF><EFBFBD>̨
if (::AllocConsole())
{
hwnd = ::GetConsoleWindow();
// <20>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
FILE * stdoutStream, * stdinStream, * stderrStream;
2018-08-19 15:11:20 +08:00
freopen_s(&stdoutStream, "conout$", "w+t", stdout);
freopen_s(&stdinStream, "conin$", "r+t", stdin);
freopen_s(&stderrStream, "conout$", "w+t", stderr);
2018-01-30 16:45:38 +08:00
// <20><><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>̨<EFBFBD>رհ<D8B1>ť
HMENU hmenu = ::GetSystemMenu(hwnd, FALSE);
::RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND);
}
}
}
else
{
if (hwnd)
{
::ShowWindow(hwnd, SW_HIDE);
}
}
}
void e2d::Window::setTypewritingEnabled(bool enabled)
2018-01-30 16:45:38 +08:00
{
static HIMC hImc = nullptr;
2018-07-04 17:00:21 +08:00
if (enabled)
2018-01-30 16:45:38 +08:00
{
if (hImc != nullptr)
{
2018-08-28 00:06:10 +08:00
::ImmAssociateContext(getHWnd(), hImc);
2018-01-30 16:45:38 +08:00
hImc = nullptr;
}
}
else
{
if (hImc == nullptr)
{
2018-08-28 00:06:10 +08:00
hImc = ::ImmAssociateContext(getHWnd(), nullptr);
2018-01-30 16:45:38 +08:00
}
}
}
bool e2d::Window::popup(const String & text, const String & title, Popup style, bool hasCancel)
{
2018-07-17 00:35:27 +08:00
UINT type = 0;
switch (style)
{
case e2d::Window::Popup::Information:
2018-07-17 00:35:27 +08:00
type = MB_ICONINFORMATION;
break;
case e2d::Window::Popup::Warning:
2018-07-17 00:35:27 +08:00
type = MB_ICONWARNING;
break;
case e2d::Window::Popup::Error:
2018-07-17 00:35:27 +08:00
type = MB_ICONERROR;
break;
default:
break;
}
2018-07-17 00:35:27 +08:00
if (hasCancel)
{
type |= MB_OKCANCEL;
}
Game::getInstance()->pause();
2018-07-17 00:35:27 +08:00
int ret = ::MessageBox(_hWnd, (LPCWSTR)text, (LPCWSTR)title, type);
Game::getInstance()->resume();
2018-07-17 00:35:27 +08:00
return ret == IDOK;
}
2018-01-30 16:45:38 +08:00
2018-08-15 00:06:03 +08:00
LRESULT e2d::Window::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2018-01-30 16:45:38 +08:00
{
LRESULT result = 0;
2018-08-15 00:06:03 +08:00
if (uMsg == WM_CREATE)
2018-01-30 16:45:38 +08:00
{
2018-08-15 00:06:03 +08:00
LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
Window *window = (Window *)pcs->lpCreateParams;
2018-08-15 00:06:03 +08:00
::SetWindowLongPtrW(
hWnd,
GWLP_USERDATA,
PtrToUlong(window)
);
2018-08-15 00:06:03 +08:00
result = 1;
}
2018-08-15 00:06:03 +08:00
else
2018-01-30 16:45:38 +08:00
{
2018-08-15 00:06:03 +08:00
bool hasHandled = false;
Window *window = reinterpret_cast<Window *>(
static_cast<LONG_PTR>(
::GetWindowLongPtrW(hWnd, GWLP_USERDATA)
)
);
switch (uMsg)
{
2018-08-15 00:06:03 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
case WM_LBUTTONUP:
case WM_LBUTTONDOWN:
case WM_LBUTTONDBLCLK:
case WM_MBUTTONUP:
case WM_MBUTTONDOWN:
case WM_MBUTTONDBLCLK:
case WM_RBUTTONUP:
case WM_RBUTTONDOWN:
case WM_RBUTTONDBLCLK:
case WM_MOUSEMOVE:
case WM_MOUSEWHEEL:
{
auto game = Game::getInstance();
if (game->isTransitioning())
2018-08-15 23:30:23 +08:00
break;
if (game->getCurrentScene())
2018-08-15 23:30:23 +08:00
{
game->getCurrentScene()->dispatch(MouseEvent(hWnd, uMsg, wParam, lParam, window->_dpi), false);
2018-08-15 23:30:23 +08:00
}
}
2018-08-15 00:06:03 +08:00
result = 0;
hasHandled = true;
break;
2018-08-15 00:06:03 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
case WM_KEYDOWN:
case WM_KEYUP:
{
auto game = Game::getInstance();
if (game->isTransitioning())
2018-08-15 23:30:23 +08:00
break;
if (game->getCurrentScene())
2018-08-15 23:30:23 +08:00
{
game->getCurrentScene()->dispatch(KeyEvent(hWnd, uMsg, wParam, lParam), false);
2018-08-15 23:30:23 +08:00
}
}
2018-08-15 00:06:03 +08:00
result = 0;
hasHandled = true;
break;
2018-08-15 00:06:03 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>С<EFBFBD><EFBFBD><E4BBAF>Ϣ
case WM_SIZE:
{
UINT width = LOWORD(lParam);
UINT height = HIWORD(lParam);
2018-01-30 16:45:38 +08:00
2018-08-15 00:06:03 +08:00
if (wParam == SIZE_RESTORED)
{
window->_width = static_cast<int>(width * 96.f / window->_dpi);
window->_height = static_cast<int>(height * 96.f / window->_dpi);
}
2018-01-30 16:45:38 +08:00
2018-08-15 00:06:03 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD>һ<EFBFBD><D2BB> WM_SIZE <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ
// Ŀ<><C4BF><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܻ<EFBFBD><DCBB><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ժ<EFBFBD><D4BA><EFBFBD><EFBFBD>п<EFBFBD><D0BF>ܵ<EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ε<EFBFBD><CEB5><EFBFBD> EndDraw ʱ<><CAB1><EFBFBD><EFBFBD>
2018-08-28 00:06:10 +08:00
auto pRT = Renderer::getInstance()->getRenderTarget();
2018-08-15 00:06:03 +08:00
if (pRT)
{
pRT->Resize(D2D1::SizeU(width, height));
}
}
break;
2018-01-30 16:45:38 +08:00
2018-08-15 00:06:03 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD><E4BBAF>Ϣ
case WM_SETTEXT:
2018-01-30 16:45:38 +08:00
{
2018-08-15 00:06:03 +08:00
window->_title = (const wchar_t*)lParam;
2018-01-30 16:45:38 +08:00
}
2018-08-15 00:06:03 +08:00
break;
2018-01-30 16:45:38 +08:00
2018-08-15 00:06:03 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>ʱ仯<CAB1><E4BBAF>Ϣ
case WM_DISPLAYCHANGE:
{
// <20>ػ<EFBFBD><D8BB>ͻ<EFBFBD><CDBB><EFBFBD>
InvalidateRect(hWnd, nullptr, FALSE);
}
result = 0;
hasHandled = true;
break;
2018-01-30 16:45:38 +08:00
2018-08-15 00:06:03 +08:00
// <20>ػ洰<D8BB><E6B4B0>
case WM_PAINT:
{
Game::getInstance()->drawScene();
2018-08-15 00:06:03 +08:00
ValidateRect(hWnd, nullptr);
}
result = 0;
hasHandled = true;
break;
2018-01-30 16:45:38 +08:00
2018-08-15 00:06:03 +08:00
// <20><><EFBFBD>ڹر<DAB9><D8B1><EFBFBD>Ϣ
case WM_CLOSE:
{
auto game = Game::getInstance();
auto currScene = game->getCurrentScene();
2018-08-15 23:30:23 +08:00
if (!currScene || currScene->onCloseWindow())
2018-08-15 00:06:03 +08:00
{
2018-08-15 23:30:23 +08:00
game->quit();
2018-08-15 00:06:03 +08:00
}
}
result = 0;
hasHandled = true;
break;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
case WM_DESTROY:
{
PostQuitMessage(0);
}
result = 1;
hasHandled = true;
break;
2018-01-30 16:45:38 +08:00
2018-08-15 00:06:03 +08:00
}
if (!hasHandled)
{
result = DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
2018-01-30 16:45:38 +08:00
return result;
}