OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Window_win.h" | 8 #include "Window_win.h" |
9 | 9 |
10 #include <tchar.h> | 10 #include <tchar.h> |
11 #include <windows.h> | 11 #include <windows.h> |
12 #include <windowsx.h> | 12 #include <windowsx.h> |
13 | 13 |
| 14 #include "SkUtils.h" |
14 #include "VulkanTestContext_win.h" | 15 #include "VulkanTestContext_win.h" |
15 | 16 |
16 Window* Window::CreateNativeWindow(void* platformData) { | 17 Window* Window::CreateNativeWindow(void* platformData) { |
17 HINSTANCE hInstance = (HINSTANCE)platformData; | 18 HINSTANCE hInstance = (HINSTANCE)platformData; |
18 | 19 |
19 Window_win* window = new Window_win(); | 20 Window_win* window = new Window_win(); |
20 if (!window->init(hInstance)) { | 21 if (!window->init(hInstance)) { |
21 delete window; | 22 delete window; |
22 return nullptr; | 23 return nullptr; |
23 } | 24 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 if (!fHWnd) | 79 if (!fHWnd) |
79 { | 80 { |
80 return false; | 81 return false; |
81 } | 82 } |
82 | 83 |
83 SetWindowLongPtr(fHWnd, GWLP_USERDATA, (LONG_PTR)this); | 84 SetWindowLongPtr(fHWnd, GWLP_USERDATA, (LONG_PTR)this); |
84 | 85 |
85 return true; | 86 return true; |
86 } | 87 } |
87 | 88 |
| 89 static Window::Key get_key(WPARAM vk) { |
| 90 static const struct { |
| 91 WPARAM fVK; |
| 92 Window::Key fKey; |
| 93 } gPair[] = { |
| 94 { VK_BACK, Window::kBack_Key }, |
| 95 { VK_CLEAR, Window::kBack_Key }, |
| 96 { VK_RETURN, Window::kOK_Key }, |
| 97 { VK_UP, Window::kUp_Key }, |
| 98 { VK_DOWN, Window::kDown_Key }, |
| 99 { VK_LEFT, Window::kLeft_Key }, |
| 100 { VK_RIGHT, Window::kRight_Key } |
| 101 }; |
| 102 for (size_t i = 0; i < SK_ARRAY_COUNT(gPair); i++) { |
| 103 if (gPair[i].fVK == vk) { |
| 104 return gPair[i].fKey; |
| 105 } |
| 106 } |
| 107 return Window::kNONE_Key; |
| 108 } |
| 109 |
| 110 static uint32_t get_modifiers(UINT message, WPARAM wParam, LPARAM lParam) { |
| 111 uint32_t modifiers = 0; |
| 112 |
| 113 switch (message) { |
| 114 case WM_UNICHAR: |
| 115 case WM_CHAR: |
| 116 if (0 == (lParam & (1 << 30))) { |
| 117 modifiers |= Window::kFirstPress_ModifierKey; |
| 118 } |
| 119 if (lParam & (1 << 29)) { |
| 120 modifiers |= Window::kOption_ModifierKey; |
| 121 } |
| 122 break; |
| 123 |
| 124 case WM_KEYDOWN: |
| 125 case WM_SYSKEYDOWN: |
| 126 if (0 == (lParam & (1 << 30))) { |
| 127 modifiers |= Window::kFirstPress_ModifierKey; |
| 128 } |
| 129 if (lParam & (1 << 29)) { |
| 130 modifiers |= Window::kOption_ModifierKey; |
| 131 } |
| 132 break; |
| 133 |
| 134 case WM_KEYUP: |
| 135 case WM_SYSKEYUP: |
| 136 if (lParam & (1 << 29)) { |
| 137 modifiers |= Window::kOption_ModifierKey; |
| 138 } |
| 139 break; |
| 140 |
| 141 case WM_LBUTTONDOWN: |
| 142 case WM_LBUTTONUP: |
| 143 case WM_MOUSEMOVE: |
| 144 if (wParam & MK_CONTROL) { |
| 145 modifiers |= Window::kControl_ModifierKey; |
| 146 } |
| 147 if (wParam & MK_SHIFT) { |
| 148 modifiers |= Window::kShift_ModifierKey; |
| 149 } |
| 150 } |
| 151 |
| 152 return modifiers; |
| 153 } |
88 | 154 |
89 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) | 155 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) |
90 { | 156 { |
91 PAINTSTRUCT ps; | 157 PAINTSTRUCT ps; |
92 HDC hdc; | 158 HDC hdc; |
93 | 159 |
94 Window_win* window = (Window_win*) GetWindowLongPtr(hWnd, GWLP_USERDATA); | 160 Window_win* window = (Window_win*) GetWindowLongPtr(hWnd, GWLP_USERDATA); |
95 | 161 |
96 switch (message) | 162 bool eventHandled = false; |
97 { | |
98 case WM_PAINT: | |
99 hdc = BeginPaint(hWnd, &ps); | |
100 window->onPaint(); | |
101 EndPaint(hWnd, &ps); | |
102 break; | |
103 | 163 |
104 case WM_CLOSE: | 164 switch (message) { |
105 case WM_DESTROY: | 165 case WM_PAINT: |
106 PostQuitMessage(0); | 166 hdc = BeginPaint(hWnd, &ps); |
107 break; | 167 window->onPaint(); |
| 168 EndPaint(hWnd, &ps); |
| 169 eventHandled = true; |
| 170 break; |
108 | 171 |
109 case WM_ACTIVATE: | 172 case WM_CLOSE: |
110 // disable/enable rendering here, depending on wParam != WA_INACTIVE | 173 case WM_DESTROY: |
111 break; | 174 PostQuitMessage(0); |
| 175 eventHandled = true; |
| 176 break; |
112 | 177 |
113 case WM_SIZE: | 178 case WM_ACTIVATE: |
114 window->onSize(); | 179 // disable/enable rendering here, depending on wParam != WA_INACTIVE |
115 break; | 180 break; |
116 | 181 |
117 case WM_KEYDOWN: | 182 case WM_SIZE: |
118 case WM_SYSKEYDOWN: | 183 window->onResize(LOWORD(lParam), HIWORD(lParam)); |
119 { | 184 eventHandled = true; |
120 DWORD dwMask = (1 << 29); | 185 break; |
121 bool bAltDown = ((lParam & dwMask) != 0); | |
122 UINT theChar = MapVirtualKey((UINT)wParam, 2); | |
123 // Handle Extended ASCII only | |
124 if (theChar < 256) { | |
125 return window->onKeyboard(theChar, true, bAltDown); | |
126 } | |
127 } | |
128 break; | |
129 | 186 |
130 case WM_KEYUP: | 187 case WM_UNICHAR: |
131 case WM_SYSKEYUP: | 188 eventHandled = window->onChar((SkUnichar)wParam, |
132 { | 189 get_modifiers(message, wParam, lParam)
); |
133 DWORD dwMask = (1 << 29); | 190 break; |
134 bool bAltDown = ((lParam & dwMask) != 0); | |
135 UINT theChar = MapVirtualKey((UINT)wParam, 2); | |
136 // Handle Extended ASCII only | |
137 if (theChar < 256) { | |
138 return window->onKeyboard(theChar, false, bAltDown); | |
139 } | |
140 } | |
141 break; | |
142 | 191 |
143 case WM_LBUTTONDOWN: | 192 case WM_CHAR: { |
144 case WM_RBUTTONDOWN: | 193 const uint16_t* c = reinterpret_cast<uint16_t*>(&wParam); |
145 case WM_MBUTTONDOWN: | 194 eventHandled = window->onChar(SkUTF16_NextUnichar(&c), |
146 case WM_LBUTTONUP: | 195 get_modifiers(message, wParam, lParam)
); |
147 case WM_RBUTTONUP: | 196 } break; |
148 case WM_MBUTTONUP: | |
149 { | |
150 bool bLeftDown = ((wParam & MK_LBUTTON) != 0); | |
151 bool bRightDown = ((wParam & MK_RBUTTON) != 0); | |
152 bool bMiddleDown = ((wParam & MK_MBUTTON) != 0); | |
153 | 197 |
| 198 case WM_KEYDOWN: |
| 199 case WM_SYSKEYDOWN: |
| 200 eventHandled = window->onKey(get_key(wParam), Window::kDown_InputSta
te, |
| 201 get_modifiers(message, wParam, lParam))
; |
| 202 break; |
| 203 |
| 204 case WM_KEYUP: |
| 205 case WM_SYSKEYUP: |
| 206 eventHandled = window->onKey(get_key(wParam), Window::kUp_InputState
, |
| 207 get_modifiers(message, wParam, lParam))
; |
| 208 break; |
| 209 |
| 210 case WM_LBUTTONDOWN: |
| 211 case WM_LBUTTONUP: { |
154 int xPos = GET_X_LPARAM(lParam); | 212 int xPos = GET_X_LPARAM(lParam); |
155 int yPos = GET_Y_LPARAM(lParam); | 213 int yPos = GET_Y_LPARAM(lParam); |
| 214 |
156 //if (!gIsFullscreen) | 215 //if (!gIsFullscreen) |
157 //{ | 216 //{ |
158 // RECT rc = { 0, 0, 640, 480 }; | 217 // RECT rc = { 0, 0, 640, 480 }; |
159 // AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE); | 218 // AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE); |
160 // xPos -= rc.left; | 219 // xPos -= rc.left; |
161 // yPos -= rc.top; | 220 // yPos -= rc.top; |
162 //} | 221 //} |
163 | 222 |
164 return window->onMouse(bLeftDown, bRightDown, bMiddleDown, false, fa
lse, 0, xPos, yPos); | 223 Window::InputState istate = ((wParam & MK_LBUTTON) != 0) ? Window::k
Down_InputState |
165 } | 224 : Window::k
Up_InputState; |
166 break; | |
167 | 225 |
168 default: | 226 eventHandled = window->onMouse(xPos, yPos, istate, |
169 return DefWindowProc(hWnd, message, wParam, lParam); | 227 get_modifiers(message, wParam, lPara
m)); |
| 228 } break; |
| 229 |
| 230 case WM_MOUSEMOVE: |
| 231 // only track if left button is down |
| 232 if ((wParam & MK_LBUTTON) != 0) { |
| 233 int xPos = GET_X_LPARAM(lParam); |
| 234 int yPos = GET_Y_LPARAM(lParam); |
| 235 |
| 236 //if (!gIsFullscreen) |
| 237 //{ |
| 238 // RECT rc = { 0, 0, 640, 480 }; |
| 239 // AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE); |
| 240 // xPos -= rc.left; |
| 241 // yPos -= rc.top; |
| 242 //} |
| 243 |
| 244 eventHandled = window->onMouse(xPos, yPos, Window::kMove_InputSt
ate, |
| 245 get_modifiers(message, wParam, lP
aram)); |
| 246 } |
| 247 break; |
| 248 |
| 249 default: |
| 250 return DefWindowProc(hWnd, message, wParam, lParam); |
170 } | 251 } |
171 | 252 |
172 return 0; | 253 return eventHandled ? 0 : 1; |
173 } | |
174 | |
175 bool Window_win::onKeyboard(UINT nChar, bool bKeyDown, bool bAltDown) { | |
176 return fKeyFunc(nChar, bKeyDown, fKeyUserData); | |
177 } | |
178 | |
179 bool Window_win::onMouse(bool bLeftButtonDown, bool bRightButtonDown, bool bMidd
leButtonDown, | |
180 bool bSideButton1Down, bool bSideButton2Down, int nMous
eWheelDelta, | |
181 int xPos, int yPos) { | |
182 return fMouseFunc(xPos, yPos, bLeftButtonDown, fMouseUserData); | |
183 } | 254 } |
184 | 255 |
185 void Window_win::setTitle(const char* title) { | 256 void Window_win::setTitle(const char* title) { |
186 SetWindowTextA(fHWnd, title); | 257 SetWindowTextA(fHWnd, title); |
187 } | 258 } |
188 | 259 |
189 void Window_win::show() { | 260 void Window_win::show() { |
190 ShowWindow(fHWnd, SW_SHOW); | 261 ShowWindow(fHWnd, SW_SHOW); |
191 } | 262 } |
192 | 263 |
193 | 264 |
194 bool Window_win::attach(BackEndTypes attachType, int msaaSampleCount, Attachment
Info*) { | 265 bool Window_win::attach(BackEndTypes attachType, int msaaSampleCount, Attachment
Info*) { |
195 if (kVulkan_BackendType != attachType) { | 266 if (kVulkan_BackendType != attachType) { |
196 return false; | 267 return false; |
197 } | 268 } |
198 | 269 |
199 ContextPlatformData_win platformData; | 270 ContextPlatformData_win platformData; |
200 platformData.fHInstance = fHInstance; | 271 platformData.fHInstance = fHInstance; |
201 platformData.fHWnd = fHWnd; | 272 platformData.fHWnd = fHWnd; |
202 | 273 |
203 fTestContext = VulkanTestContext::Create((void*)&platformData, msaaSampleCou
nt); | 274 fTestContext = VulkanTestContext::Create((void*)&platformData, msaaSampleCou
nt); |
204 | 275 |
205 return (SkToBool(fTestContext)); | 276 return (SkToBool(fTestContext)); |
206 } | 277 } |
OLD | NEW |