OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2016 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "Window_win.h" | |
9 | |
10 #include <tchar.h> | |
11 #include <windows.h> | |
12 #include <windowsx.h> | |
13 | |
14 #include "SkUtils.h" | |
15 #include "VulkanTestContext_win.h" | |
16 | |
17 namespace sk_app { | |
18 | |
19 Window* Window::CreateNativeWindow(void* platformData) { | |
20 HINSTANCE hInstance = (HINSTANCE)platformData; | |
21 | |
22 Window_win* window = new Window_win(); | |
23 if (!window->init(hInstance)) { | |
24 delete window; | |
25 return nullptr; | |
26 } | |
27 | |
28 return window; | |
29 } | |
30 | |
31 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); | |
32 | |
33 bool Window_win::init(HINSTANCE hInstance) { | |
34 fHInstance = hInstance ? hInstance : GetModuleHandle(nullptr); | |
35 | |
36 WNDCLASSEX wcex; | |
37 // The main window class name | |
38 static const TCHAR gSZWindowClass[] = _T("SkiaApp"); | |
39 | |
40 wcex.cbSize = sizeof(WNDCLASSEX); | |
41 | |
42 wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; | |
43 wcex.lpfnWndProc = WndProc; | |
44 wcex.cbClsExtra = 0; | |
45 wcex.cbWndExtra = 0; | |
46 wcex.hInstance = fHInstance; | |
47 wcex.hIcon = LoadIcon(fHInstance, (LPCTSTR)IDI_WINLOGO); | |
48 wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);; | |
49 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); | |
50 wcex.lpszMenuName = nullptr; | |
51 wcex.lpszClassName = gSZWindowClass; | |
52 wcex.hIconSm = LoadIcon(fHInstance, (LPCTSTR)IDI_WINLOGO);; | |
53 | |
54 if (!RegisterClassEx(&wcex)) { | |
55 return false; | |
56 } | |
57 | |
58 /* | |
59 if (fullscreen) | |
60 { | |
61 DEVMODE dmScreenSettings; | |
62 // If full screen set the screen to maximum size of the users desktop an
d 32bit. | |
63 memset(&dmScreenSettings, 0, sizeof(dmScreenSettings)); | |
64 dmScreenSettings.dmSize = sizeof(dmScreenSettings); | |
65 dmScreenSettings.dmPelsWidth = (unsigned long)width; | |
66 dmScreenSettings.dmPelsHeight = (unsigned long)height; | |
67 dmScreenSettings.dmBitsPerPel = 32; | |
68 dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT
; | |
69 | |
70 // Change the display settings to full screen. | |
71 ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN); | |
72 | |
73 // Set the position of the window to the top left corner. | |
74 posX = posY = 0; | |
75 } | |
76 */ | |
77 // gIsFullscreen = fullscreen; | |
78 | |
79 fHWnd = CreateWindow(gSZWindowClass, nullptr, WS_OVERLAPPEDWINDOW, | |
80 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, f
HInstance, nullptr); | |
81 if (!fHWnd) | |
82 { | |
83 return false; | |
84 } | |
85 | |
86 SetWindowLongPtr(fHWnd, GWLP_USERDATA, (LONG_PTR)this); | |
87 | |
88 return true; | |
89 } | |
90 | |
91 static Window::Key get_key(WPARAM vk) { | |
92 static const struct { | |
93 WPARAM fVK; | |
94 Window::Key fKey; | |
95 } gPair[] = { | |
96 { VK_BACK, Window::kBack_Key }, | |
97 { VK_CLEAR, Window::kBack_Key }, | |
98 { VK_RETURN, Window::kOK_Key }, | |
99 { VK_UP, Window::kUp_Key }, | |
100 { VK_DOWN, Window::kDown_Key }, | |
101 { VK_LEFT, Window::kLeft_Key }, | |
102 { VK_RIGHT, Window::kRight_Key } | |
103 }; | |
104 for (size_t i = 0; i < SK_ARRAY_COUNT(gPair); i++) { | |
105 if (gPair[i].fVK == vk) { | |
106 return gPair[i].fKey; | |
107 } | |
108 } | |
109 return Window::kNONE_Key; | |
110 } | |
111 | |
112 static uint32_t get_modifiers(UINT message, WPARAM wParam, LPARAM lParam) { | |
113 uint32_t modifiers = 0; | |
114 | |
115 switch (message) { | |
116 case WM_UNICHAR: | |
117 case WM_CHAR: | |
118 if (0 == (lParam & (1 << 30))) { | |
119 modifiers |= Window::kFirstPress_ModifierKey; | |
120 } | |
121 if (lParam & (1 << 29)) { | |
122 modifiers |= Window::kOption_ModifierKey; | |
123 } | |
124 break; | |
125 | |
126 case WM_KEYDOWN: | |
127 case WM_SYSKEYDOWN: | |
128 if (0 == (lParam & (1 << 30))) { | |
129 modifiers |= Window::kFirstPress_ModifierKey; | |
130 } | |
131 if (lParam & (1 << 29)) { | |
132 modifiers |= Window::kOption_ModifierKey; | |
133 } | |
134 break; | |
135 | |
136 case WM_KEYUP: | |
137 case WM_SYSKEYUP: | |
138 if (lParam & (1 << 29)) { | |
139 modifiers |= Window::kOption_ModifierKey; | |
140 } | |
141 break; | |
142 | |
143 case WM_LBUTTONDOWN: | |
144 case WM_LBUTTONUP: | |
145 case WM_MOUSEMOVE: | |
146 if (wParam & MK_CONTROL) { | |
147 modifiers |= Window::kControl_ModifierKey; | |
148 } | |
149 if (wParam & MK_SHIFT) { | |
150 modifiers |= Window::kShift_ModifierKey; | |
151 } | |
152 } | |
153 | |
154 return modifiers; | |
155 } | |
156 | |
157 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) | |
158 { | |
159 PAINTSTRUCT ps; | |
160 HDC hdc; | |
161 | |
162 Window_win* window = (Window_win*) GetWindowLongPtr(hWnd, GWLP_USERDATA); | |
163 | |
164 bool eventHandled = false; | |
165 | |
166 switch (message) { | |
167 case WM_PAINT: | |
168 hdc = BeginPaint(hWnd, &ps); | |
169 window->onPaint(); | |
170 EndPaint(hWnd, &ps); | |
171 eventHandled = true; | |
172 break; | |
173 | |
174 case WM_CLOSE: | |
175 case WM_DESTROY: | |
176 PostQuitMessage(0); | |
177 eventHandled = true; | |
178 break; | |
179 | |
180 case WM_ACTIVATE: | |
181 // disable/enable rendering here, depending on wParam != WA_INACTIVE | |
182 break; | |
183 | |
184 case WM_SIZE: | |
185 window->onResize(LOWORD(lParam), HIWORD(lParam)); | |
186 eventHandled = true; | |
187 break; | |
188 | |
189 case WM_UNICHAR: | |
190 eventHandled = window->onChar((SkUnichar)wParam, | |
191 get_modifiers(message, wParam, lParam)
); | |
192 break; | |
193 | |
194 case WM_CHAR: { | |
195 const uint16_t* c = reinterpret_cast<uint16_t*>(&wParam); | |
196 eventHandled = window->onChar(SkUTF16_NextUnichar(&c), | |
197 get_modifiers(message, wParam, lParam)
); | |
198 } break; | |
199 | |
200 case WM_KEYDOWN: | |
201 case WM_SYSKEYDOWN: | |
202 eventHandled = window->onKey(get_key(wParam), Window::kDown_InputSta
te, | |
203 get_modifiers(message, wParam, lParam))
; | |
204 break; | |
205 | |
206 case WM_KEYUP: | |
207 case WM_SYSKEYUP: | |
208 eventHandled = window->onKey(get_key(wParam), Window::kUp_InputState
, | |
209 get_modifiers(message, wParam, lParam))
; | |
210 break; | |
211 | |
212 case WM_LBUTTONDOWN: | |
213 case WM_LBUTTONUP: { | |
214 int xPos = GET_X_LPARAM(lParam); | |
215 int yPos = GET_Y_LPARAM(lParam); | |
216 | |
217 //if (!gIsFullscreen) | |
218 //{ | |
219 // RECT rc = { 0, 0, 640, 480 }; | |
220 // AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE); | |
221 // xPos -= rc.left; | |
222 // yPos -= rc.top; | |
223 //} | |
224 | |
225 Window::InputState istate = ((wParam & MK_LBUTTON) != 0) ? Window::k
Down_InputState | |
226 : Window::k
Up_InputState; | |
227 | |
228 eventHandled = window->onMouse(xPos, yPos, istate, | |
229 get_modifiers(message, wParam, lPara
m)); | |
230 } break; | |
231 | |
232 case WM_MOUSEMOVE: | |
233 // only track if left button is down | |
234 if ((wParam & MK_LBUTTON) != 0) { | |
235 int xPos = GET_X_LPARAM(lParam); | |
236 int yPos = GET_Y_LPARAM(lParam); | |
237 | |
238 //if (!gIsFullscreen) | |
239 //{ | |
240 // RECT rc = { 0, 0, 640, 480 }; | |
241 // AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE); | |
242 // xPos -= rc.left; | |
243 // yPos -= rc.top; | |
244 //} | |
245 | |
246 eventHandled = window->onMouse(xPos, yPos, Window::kMove_InputSt
ate, | |
247 get_modifiers(message, wParam, lP
aram)); | |
248 } | |
249 break; | |
250 | |
251 default: | |
252 return DefWindowProc(hWnd, message, wParam, lParam); | |
253 } | |
254 | |
255 return eventHandled ? 0 : 1; | |
256 } | |
257 | |
258 void Window_win::setTitle(const char* title) { | |
259 SetWindowTextA(fHWnd, title); | |
260 } | |
261 | |
262 void Window_win::show() { | |
263 ShowWindow(fHWnd, SW_SHOW); | |
264 } | |
265 | |
266 | |
267 bool Window_win::attach(BackEndType attachType, int msaaSampleCount) { | |
268 if (kVulkan_BackendType != attachType) { | |
269 return false; | |
270 } | |
271 | |
272 ContextPlatformData_win platformData; | |
273 platformData.fHInstance = fHInstance; | |
274 platformData.fHWnd = fHWnd; | |
275 | |
276 fTestContext = VulkanTestContext::Create((void*)&platformData, msaaSampleCou
nt); | |
277 | |
278 return (SkToBool(fTestContext)); | |
279 } | |
280 | |
281 void Window_win::inval() { | |
282 InvalidateRect(fHWnd, nullptr, false); | |
283 } | |
284 | |
285 } // namespace sk_app | |
OLD | NEW |