vsg  1.1.0
VulkanSceneGraph library
MacOS_Window.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2018 Robert Osfield
6 
7 Permission 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 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE 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_MACOS_MVK
16 
17 #include <vsg/app/Window.h>
18 #include <vsg/ui/KeyEvent.h>
19 
20 @class NSEvent;
21 @class CAMetalLayer;
22 @class vsg_MacOS_NSView;
23 @class vsg_MacOS_NSWindow;
24 
25 #import <Cocoa/Cocoa.h>
26 
27 namespace vsgMacOS
28 {
29  extern vsg::Names getInstanceExtensions();
30  using KeySymbolState = std::pair<vsg::KeySymbol,bool>;
31  using ModifierKeyChanges = std::vector< KeySymbolState >;
32 
34  class KeyboardMap : public vsg::Object
35  {
36  public:
37  KeyboardMap();
38 
39  using kVKKeyCodeToKeySymbolMap = std::map<unsigned short, vsg::KeySymbol>;
40  bool getKeySymbol(NSEvent* anEvent, vsg::KeySymbol& keySymbol, vsg::KeySymbol& modifiedKeySymbol, vsg::KeyModifier& keyModifier);
41  void getModifierKeyChanges(NSEvent* anEvent, ModifierKeyChanges& changes);
42 
43  protected:
44  kVKKeyCodeToKeySymbolMap _vk2vsg;
45  NSEventModifierFlags _lastFlags;
46  };
47 
49  class MacOS_Window : public vsg::Inherit<vsg::Window, MacOS_Window>
50  {
51  public:
52 
54  MacOS_Window() = delete;
55  MacOS_Window(const MacOS_Window&) = delete;
56  MacOS_Window operator = (const MacOS_Window&) = delete;
57 
58  const char* instanceExtensionSurfaceName() const override { return "VK_MVK_macos_surface"; }
59 
60  bool valid() const override { return _window; }
61 
62  bool pollEvents(vsg::UIEvents& events) override;
63 
64  void resize() override;
65 
66  bool handleNSEvent(NSEvent* anEvent);
67 
68  // native objects
69  vsg_MacOS_NSWindow* window() { return _window; };
70  vsg_MacOS_NSView* view() { return _view; };
71  CAMetalLayer* layer() { return _metalLayer; };
72 
73  vsg::clock::time_point getEventTime(double eventTime)
74  {
75  long elapsedmilli = long(double(eventTime - _first_macos_timestamp) * 1000.0f);
76  return _first_macos_time_point + std::chrono::milliseconds(elapsedmilli);
77  }
78 
79  void queueEvent(vsg::UIEvent* anEvent) { bufferedEvents.emplace_back(anEvent); }
80 
81  protected:
82  virtual ~MacOS_Window();
83 
84  void _initSurface() override;
85 
86  vsg_MacOS_NSWindow* _window;
87  vsg_MacOS_NSView* _view;
88  CAMetalLayer* _metalLayer;
89 
90  double _first_macos_timestamp = 0;
91  vsg::clock::time_point _first_macos_time_point;
92 
93  vsg::ref_ptr<KeyboardMap> _keyboard;
94  };
95 
96 } // namespace vsgMacOS
97 
98 EVSG_type_name(vsgMacOS::MacOS_Window);
KeyboardMap maps macOS keyboard events to vsg::KeySymbol.
Definition: MacOS_Window.h:35
MacOS_Window implements macOS specific window creation, event handling and vulkan Surface setup.
Definition: MacOS_Window.h:50
bool pollEvents(vsg::UIEvents &events) override
get the list of events since the last pollEvents() call by splicing bufferEvents with polled windowin...
Definition: Inherit.h:28
Definition: Object.h:42
UIEvent is a base class for user interface events.
Definition: UIEvent.h:28
UIEvents bufferedEvents
events buffered since the last pollEvents.
Definition: Window.h:54