This commit is contained in:
Nomango 2019-10-12 08:54:10 +08:00
parent 3f9817a20d
commit 63cb724347
4 changed files with 47 additions and 38 deletions

View File

@ -23,6 +23,8 @@
namespace kiwano namespace kiwano
{ {
// ʼþÀàÐÍ
typedef int EventType;
// 鼠标事件 // 鼠标事件
struct MouseEvent struct MouseEvent
@ -36,7 +38,7 @@ namespace kiwano
{ {
struct // Events::MouseDown | Events::MouseUp | Events::MouseClick struct // Events::MouseDown | Events::MouseUp | Events::MouseClick
{ {
int button; MouseButton::Value button;
}; };
struct // Events::MouseWheel struct // Events::MouseWheel
@ -45,7 +47,7 @@ namespace kiwano
}; };
}; };
static bool Check(int type); static bool Check(EventType type);
}; };
// 键盘事件 // 键盘事件
@ -56,7 +58,7 @@ namespace kiwano
{ {
struct // Events::KeyDown | Events::KeyUp struct // Events::KeyDown | Events::KeyUp
{ {
int code; // enum KeyCode KeyCode::Value code;
}; };
struct // Events::Char struct // Events::Char
@ -65,7 +67,7 @@ namespace kiwano
}; };
}; };
static bool Check(int type); static bool Check(EventType type);
}; };
// 窗口事件 // 窗口事件
@ -96,7 +98,7 @@ namespace kiwano
}; };
}; };
static bool Check(int type); static bool Check(EventType type);
}; };
// 自定义事件 // 自定义事件
@ -110,7 +112,7 @@ namespace kiwano
// 事件 // 事件
struct KGE_API Event struct KGE_API Event
{ {
enum Type : int enum Type : EventType
{ {
First, First,
@ -155,23 +157,23 @@ namespace kiwano
CustomEvent custom; CustomEvent custom;
}; };
Event(int type = Type::First) : type(type), target(nullptr) {} Event(EventType type = Type::First) : type(type), target(nullptr) {}
}; };
// Check-functions // Check-functions
inline bool MouseEvent::Check(int type) inline bool MouseEvent::Check(EventType type)
{ {
return type > Event::MouseFirst && type < Event::MouseLast; return type > Event::MouseFirst && type < Event::MouseLast;
} }
inline bool KeyboardEvent::Check(int type) inline bool KeyboardEvent::Check(EventType type)
{ {
return type > Event::KeyFirst && type < Event::KeyLast; return type > Event::KeyFirst && type < Event::KeyLast;
} }
inline bool WindowEvent::Check(int type) inline bool WindowEvent::Check(EventType type)
{ {
return type > Event::WindowFirst && type < Event::WindowLast; return type > Event::WindowFirst && type < Event::WindowLast;
} }

View File

@ -196,6 +196,17 @@ namespace kiwano
} }
} }
void Window::PollEvents()
{
static MSG msg = {};
if (::GetMessageW(&msg, nullptr, 0, 0))
{
::TranslateMessage(&msg);
::DispatchMessageW(&msg);
}
}
String Window::GetTitle() const String Window::GetTitle() const
{ {
if (handle_) if (handle_)

View File

@ -98,6 +98,8 @@ namespace kiwano
void Prepare(); void Prepare();
void PollEvents();
HWND GetHandle() const; HWND GetHandle() const;
DWORD GetWindowStyle() const; DWORD GetWindowStyle() const;

View File

@ -116,19 +116,12 @@ namespace kiwano
{ {
KGE_ASSERT(inited_ && "Calling Application::Run before Application::Init"); KGE_ASSERT(inited_ && "Calling Application::Run before Application::Init");
HWND hwnd = Window::GetInstance()->GetHandle();
if (hwnd)
{
end_ = false; end_ = false;
Window::GetInstance()->Prepare();
MSG msg = {}; Window::GetInstance()->Prepare();
while (::GetMessageW(&msg, nullptr, 0, 0) && !end_) while (!end_)
{ {
::TranslateMessage(&msg); Window::GetInstance()->PollEvents();
::DispatchMessageW(&msg);
}
} }
} }
@ -444,6 +437,8 @@ namespace kiwano
if (!app->OnClosing()) if (!app->OnClosing())
{ {
Event evt(Event::WindowClosed);
app->DispatchEvent(evt);
return 0; return 0;
} }
} }
@ -453,8 +448,7 @@ namespace kiwano
{ {
KGE_LOG(L"Window was destroyed"); KGE_LOG(L"Window was destroyed");
Event evt(Event::WindowClosed); app->Quit();
app->DispatchEvent(evt);
app->OnDestroy(); app->OnDestroy();
::PostQuitMessage(0); ::PostQuitMessage(0);