vsg 1.1.3
VulkanSceneGraph library
Loading...
Searching...
No Matches
Win32_Window.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
13</editor-fold> */
14
15#define VK_USE_PLATFORM_WIN32_KHR
16
17#ifndef NOMINMAX
18# define NOMINMAX
19#endif
20
21#include <vsg/app/Window.h>
22#include <vsg/ui/KeyEvent.h>
23#include <vsg/ui/PointerEvent.h>
24
25#include <windows.h>
26#include <windowsx.h>
27
28#include <vulkan/vulkan_win32.h>
29
30namespace vsgWin32
31{
32 class VSG_DECLSPEC KeyboardMap : public vsg::Object
33 {
34 public:
36
37 using VirtualKeyToKeySymbolMap = std::map<uint16_t, vsg::KeySymbol>;
38
39 bool getKeySymbol(WPARAM wParam, LPARAM lParam, vsg::KeySymbol& keySymbol, vsg::KeySymbol& modifiedKeySymbol, vsg::KeyModifier& keyModifier)
40 {
41 uint16_t modifierMask = 0;
42 uint32_t virtualKey = ::MapVirtualKeyEx((lParam >> 16) & 0xff, MAPVK_VSC_TO_VK_EX, ::GetKeyboardLayout(0));
43 auto itr = _vk2vsg.find(virtualKey);
44
45 if (itr == _vk2vsg.end())
46 {
47 // What ever the code was in lParam should translate to a Virtual Key that we know of in _vk2vsg
48 // If we cannot find it, we simply return.
49 return false;
50 }
51
52 // This is the base-key that was pressed. (i.e., the VSG enum of the physical key pressed).
53 keySymbol = itr->second;
54
55 // Look for any modifiers that may be active.
56 BYTE keyState[256];
57 if (virtualKey == 0 || !::GetKeyboardState(keyState))
58 {
59 // if virtualKey was undefined or we could not get the keyboard state, simply return.
60 return false;
61 }
62
63 // If any of the specific left/right modifier keys are active
64 // add the side-independent vsg modifier to the modifier Mask
65 switch (virtualKey)
66 {
67 case VK_LSHIFT:
68 case VK_RSHIFT:
69 modifierMask |= vsg::KeyModifier::MODKEY_Shift;
70 break;
71
72 case VK_LCONTROL:
73 case VK_RCONTROL:
74 modifierMask |= vsg::KeyModifier::MODKEY_Control;
75 break;
76
77 case VK_LMENU:
78 case VK_RMENU:
79 modifierMask |= vsg::KeyModifier::MODKEY_Alt;
80 break;
81
82 default:
83 virtualKey = static_cast<int>(wParam);
84 break;
85 }
86
87 // Check if caps lock or numlock is toggled.
88 if (keyState[VK_CAPITAL] & 0x01) modifierMask |= vsg::KeyModifier::MODKEY_CapsLock;
89 if (keyState[VK_NUMLOCK] & 0x01) modifierMask |= vsg::KeyModifier::MODKEY_NumLock;
90
91 // Check if the modifier keys are down (these are non-toggle keys, so the high-order bit is relevant!)
92 // again, vsg only has a side-independent modifier
93 if (keyState[VK_LSHIFT] & 0x80) modifierMask |= vsg::KeyModifier::MODKEY_Shift;
94 if (keyState[VK_RSHIFT] & 0x80) modifierMask |= vsg::KeyModifier::MODKEY_Shift;
95 if (keyState[VK_LCONTROL] & 0x80) modifierMask |= vsg::KeyModifier::MODKEY_Control;
96 if (keyState[VK_RCONTROL] & 0x80) modifierMask |= vsg::KeyModifier::MODKEY_Control;
97 if (keyState[VK_LMENU] & 0x80) modifierMask |= vsg::KeyModifier::MODKEY_Alt;
98 if (keyState[VK_RMENU] & 0x80) modifierMask |= vsg::KeyModifier::MODKEY_Alt;
99
100 // This is the final keyModifier
101 keyModifier = static_cast<vsg::KeyModifier>(modifierMask);
102
103 // The actual keystroke is what we get after the ::ToAscii call
104 char asciiKey[2];
105 int32_t numChars = ::ToAscii(static_cast<UINT>(wParam), (lParam >> 16) & 0xff, keyState, reinterpret_cast<WORD*>(asciiKey), 0);
106 if (numChars == 1)
107 {
108 // it is indeed an ascii character. 0-127
109 modifiedKeySymbol = static_cast<vsg::KeySymbol>(asciiKey[0]);
110 }
111 else
112 {
113 // otherwise treat the modifiedKeySymbol as the same as the keySymbol.
114 modifiedKeySymbol = keySymbol;
115 }
116
117 return true;
118 }
119
120 protected:
121 VirtualKeyToKeySymbolMap _vk2vsg;
122 };
123
124 inline vsg::ButtonMask getButtonMask(WPARAM wParam)
125 {
126 auto mask = (wParam & MK_LBUTTON ? vsg::ButtonMask::BUTTON_MASK_1 : 0) | (wParam & MK_MBUTTON ? vsg::ButtonMask::BUTTON_MASK_2 : 0) | (wParam & MK_RBUTTON ? vsg::ButtonMask::BUTTON_MASK_3 : 0) |
127 (wParam & MK_XBUTTON1 ? vsg::ButtonMask::BUTTON_MASK_4 : 0) | (wParam & MK_XBUTTON2 ? vsg::ButtonMask::BUTTON_MASK_5 : 0);
128 return static_cast<vsg::ButtonMask>(mask);
129 }
130
131 inline uint32_t getButtonDownEventDetail(UINT buttonMsg, WORD wParamHi)
132 {
133 switch (buttonMsg)
134 {
135 case WM_LBUTTONDOWN: return 1;
136 case WM_MBUTTONDOWN: return 2;
137 case WM_RBUTTONDOWN: return 3;
138 case WM_XBUTTONDOWN:
139 if (wParamHi == XBUTTON1)
140 return 4;
141 else if (wParamHi == XBUTTON2)
142 return 5;
143 else
144 return 0;
145 default:
146 return 0;
147 }
148 }
149
150 inline uint32_t getButtonUpEventDetail(UINT buttonMsg, WORD wParamHi)
151 {
152 switch (buttonMsg)
153 {
154 case WM_LBUTTONUP: return 1;
155 case WM_MBUTTONUP: return 2;
156 case WM_RBUTTONUP: return 3;
157 case WM_XBUTTONUP:
158 if (wParamHi == XBUTTON1)
159 return 4;
160 else if (wParamHi == XBUTTON2)
161 return 5;
162 else
163 return 0;
164 default:
165 return 0;
166 }
167 }
168
170 class VSG_DECLSPEC Win32_Window : public vsg::Inherit<vsg::Window, Win32_Window>
171 {
172 public:
174 Win32_Window() = delete;
175 Win32_Window(const Win32_Window&) = delete;
176 Win32_Window operator=(const Win32_Window&) = delete;
177
178 const char* instanceExtensionSurfaceName() const override { return VK_KHR_WIN32_SURFACE_EXTENSION_NAME; }
179
180 bool valid() const override { return _window; }
181
182 bool visible() const override;
183
184 void releaseWindow() override;
185
186 bool pollEvents(vsg::UIEvents& events) override;
187
188 void resize() override;
189
190 operator HWND() const { return _window; }
191
193 virtual bool handleWin32Messages(UINT msg, WPARAM wParam, LPARAM lParam);
194
195 protected:
196 virtual ~Win32_Window();
197
198 void _initSurface() override;
199
200 HWND _window;
201 bool _windowMapped = false;
202
204 };
205
206} // namespace vsgWin32
207
208EVSG_type_name(vsgWin32::Win32_Window);
Definition Inherit.h:28
Definition Object.h:60
Definition ref_ptr.h:22
Definition Win32_Window.h:33
Win32_Window implements Win32 specific window creation, event handling and vulkan Surface setup.
Definition Win32_Window.h:171
bool pollEvents(vsg::UIEvents &events) override
get the list of events since the last pollEvents() call by splicing bufferEvents with polled windowin...
virtual bool handleWin32Messages(UINT msg, WPARAM wParam, LPARAM lParam)
handle Win32 event messages, return true if handled.
void releaseWindow() override