-
Notifications
You must be signed in to change notification settings - Fork 3
/
MessageSource.cpp
45 lines (40 loc) · 986 Bytes
/
MessageSource.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "MessageSource.h"
int MessageSource::MessageLoop()
{
BOOL result = { 0 };
MSG msg = { 0 };
while ((result = GetMessage(&msg, nullptr, 0, 0)) > 0 && msg.message != WM_QUIT)
{
if (msg.message == WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
PublishMessage(WindowMessage(msg.hwnd, msg.message, msg.wParam, msg.lParam));
}
if (msg.message == WM_QUIT)
{
PublishMessage(WindowMessage(msg.hwnd, msg.message, msg.wParam, msg.lParam));
}
return (int)msg.wParam;
}
void MessageSource::Subscribe(IMessageReceiver* messageReceiver)
{
m_messageReceiver.push_back(messageReceiver);
}
void MessageSource::Subscribe(std::function<void(WindowMessage)> messageHandler)
{
m_lambdas.push_back(messageHandler);
}
void MessageSource::PublishMessage(WindowMessage windowMessage)
{
for (auto receiver : m_messageReceiver)
{
receiver->HandleWindowMessage(windowMessage);
}
for (auto receiver : m_lambdas)
{
receiver(windowMessage);
}
}