vsg  1.1.0
VulkanSceneGraph library
CloseHandler.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 #include <vsg/app/Viewer.h>
16 #include <vsg/ui/KeyEvent.h>
17 
18 namespace vsg
19 {
20 
22  class CloseHandler : public Inherit<Visitor, CloseHandler>
23  {
24  public:
25  explicit CloseHandler(Viewer* viewer) :
26  _viewer(viewer) {}
27 
28  KeySymbol closeKey = KEY_Escape; // KEY_Undefined
29 
30  virtual void close()
31  {
32  // take a ref_ptr<> of the observer_ptr<> to be able to safely access it
33  ref_ptr<Viewer> viewer = _viewer;
34  if (viewer) viewer->close();
35  }
36 
37  void apply(KeyPressEvent& keyPress) override
38  {
39  if (closeKey != KEY_Undefined && keyPress.keyBase == closeKey) close();
40  }
41 
42  void apply(CloseWindowEvent&) override
43  {
44  close();
45  }
46 
47  void apply(TerminateEvent&) override
48  {
49  close();
50  }
51 
52  protected:
53  // use observer_ptr<> to avoid circular reference
54  observer_ptr<Viewer> _viewer;
55  };
56  VSG_type_name(vsg::CloseHandler);
57 
58 } // namespace vsg
CloseHandler event handler used to respond to close events generated by a Window.
Definition: CloseHandler.h:23
CloseWindowEvent represents a window close event.
Definition: WindowEvent.h:87
Definition: Inherit.h:28
KeyPressEvent represents a key press event.
Definition: KeyEvent.h:309
TerminateEvent represents an application termination event.
Definition: ApplicationEvent.h:24
Definition: Viewer.h:31
Definition: observer_ptr.h:24
Definition: ref_ptr.h:22