-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowContainer.cpp
More file actions
147 lines (118 loc) · 3.11 KB
/
WindowContainer.cpp
File metadata and controls
147 lines (118 loc) · 3.11 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "WindowContainer.h"
WindowContainer::WindowContainer() {
// For
static bool rawInputIsInitialized = false;
if (!rawInputIsInitialized) {
RAWINPUTDEVICE rid = { 0 };
rid.usUsagePage = 0x01;
rid.usUsage = 0x02;
if (!RegisterRawInputDevices(&rid, 1, sizeof(rid))) {
Logger::Log("Failed to register raw input device.");
exit(-1);
}
rawInputIsInitialized = true;
}
}
LRESULT WindowContainer::WindowProc(HWND handle, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CHAR: {
unsigned char keycode = static_cast<unsigned char>(wParam);
if (keyboard.IsCharsAutoRepeat()) {
keyboard.OnChar(keycode);
}
else { // Since this is when auto repeat is off
// Check if it is a auto repeat key.
const bool wasPressed = lParam & 0x40000000;
if (!wasPressed) {
keyboard.OnChar(keycode);
}
}
return 0;
}
case WM_KEYDOWN: {
unsigned char keycode = static_cast<unsigned char>(wParam);
if (keyboard.IsKeysAutoRepeat()) {
keyboard.OnKeyPressed(keycode);
}
else {
const bool wasPressed = lParam & 0x40000000;
if (!wasPressed) {
keyboard.OnChar(keycode);
}
}
return 0;
}
case WM_KEYUP: {
unsigned char keycode = static_cast<unsigned char>(wParam);
keyboard.OnKeyReleased(keycode);
return 0;
}
case WM_MOUSEMOVE: {
int x = LOWORD(lParam);
int y = HIWORD(lParam);
mouse.OnMouseMove(x, y);
return 0;
}
case WM_LBUTTONDOWN: {
int x = LOWORD(lParam);
int y = HIWORD(lParam);
mouse.OnLeftPressed(x, y);
return 0;
}
case WM_LBUTTONUP: {
int x = LOWORD(lParam);
int y = HIWORD(lParam);
mouse.OnLeftReleased(x, y);
return 0;
}
case WM_RBUTTONDOWN: {
int x = LOWORD(lParam);
int y = HIWORD(lParam);
mouse.OnRightPressed(x, y);
return 0;
}
case WM_RBUTTONUP: {
int x = LOWORD(lParam);
int y = HIWORD(lParam);
mouse.OnRightReleased(x, y);
return 0;
}
case WM_MBUTTONDOWN: {
int x = LOWORD(lParam);
int y = HIWORD(lParam);
mouse.OnMiddlePressed(x, y);
return 0;
}
case WM_MBUTTONUP: {
int x = LOWORD(lParam);
int y = HIWORD(lParam);
mouse.OnMiddleReleased(x, y);
return 0;
}
case WM_MOUSEWHEEL: {
int x = LOWORD(lParam);
int y = HIWORD(lParam);
if (GET_WHEEL_DELTA_WPARAM(wParam) > 0) // Scrolling up
mouse.OnWheelUp(x, y);
else if (GET_WHEEL_DELTA_WPARAM(wParam) < 0) // Scrolling down
mouse.OnWheelDown(x, y);
} break;
case WM_INPUT: { // For RAW mouse movement
// 1.) Register raw input devices(the mouse)
UINT dataSize;
GetRawInputData(reinterpret_cast<HRAWINPUT>(lParam), RID_INPUT, 0, &dataSize, sizeof(RAWINPUTHEADER));
if (dataSize > 0) {
std::unique_ptr<BYTE[]> rawData = std::make_unique<BYTE[]>(dataSize);
if (GetRawInputData(reinterpret_cast<HRAWINPUT>(lParam), RID_INPUT, rawData.get(), &dataSize, sizeof(RAWINPUTHEADER)) == dataSize) {
RAWINPUT* raw = reinterpret_cast<RAWINPUT*>(rawData.get());
if (raw->header.dwType == RIM_TYPEMOUSE) {
mouse.OnMouseMoveRaw(raw->data.mouse.lLastX, raw->data.mouse.lLastY);
}
}
}
return DefWindowProc(handle, msg, wParam, lParam);
}
default:
return DefWindowProc(handle, msg, wParam, lParam);
}
}