vsg  1.1.0
VulkanSceneGraph library
Trackball.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2019 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/Camera.h>
16 #include <vsg/app/EllipsoidModel.h>
17 #include <vsg/maths/transform.h>
18 #include <vsg/ui/Keyboard.h>
19 #include <vsg/ui/PointerEvent.h>
20 #include <vsg/ui/ScrollWheelEvent.h>
21 #include <vsg/ui/TouchEvent.h>
22 
23 namespace vsg
24 {
25 
27  class VSG_DECLSPEC Trackball : public Inherit<Visitor, Trackball>
28  {
29  public:
30  explicit Trackball(ref_ptr<Camera> camera, ref_ptr<EllipsoidModel> ellipsoidModel = {});
31 
34 
37 
38  void apply(KeyPressEvent& keyPress) override;
39  void apply(KeyReleaseEvent& keyRelease) override;
40  void apply(FocusInEvent& focusIn) override;
41  void apply(FocusOutEvent& focusOut) override;
42  void apply(ButtonPressEvent& buttonPress) override;
43  void apply(ButtonReleaseEvent& buttonRelease) override;
44  void apply(MoveEvent& moveEvent) override;
45  void apply(ScrollWheelEvent& scrollWheel) override;
46  void apply(TouchDownEvent& touchDown) override;
47  void apply(TouchUpEvent& touchUp) override;
48  void apply(TouchMoveEvent& touchMove) override;
49  void apply(FrameEvent& frame) override;
50 
51  virtual void rotate(double angle, const dvec3& axis);
52  virtual void zoom(double ratio);
53  virtual void pan(const dvec2& delta);
54 
55  std::pair<int32_t, int32_t> cameraRenderAreaCoordinates(const PointerEvent& pointerEvent) const;
56  bool withinRenderArea(const PointerEvent& pointerEvent) const;
57  bool eventRelevant(const WindowEvent& event) const;
58 
59  void clampToGlobe();
60 
62  std::map<observer_ptr<Window>, ivec2> windowOffsets;
63 
65  void addWindow(ref_ptr<Window> window, const ivec2& offset = {});
66 
68  void addKeyViewpoint(KeySymbol key, ref_ptr<LookAt> lookAt, double duration = 1.0);
69 
71  void addKeyViewpoint(KeySymbol key, double latitude, double longitude, double altitude, double duration = 1.0);
72 
75  void setViewpoint(ref_ptr<LookAt> lookAt, double duration = 1.0);
76 
77  struct Viewpoint
78  {
79  ref_ptr<LookAt> lookAt;
80  double duration = 0.0;
81  };
82 
84  std::map<KeySymbol, Viewpoint> keyViewpointMap;
85 
87  KeySymbol turnLeftKey = KEY_a;
88 
90  KeySymbol turnRightKey = KEY_d;
91 
93  KeySymbol pitchUpKey = KEY_w;
94 
96  KeySymbol pitchDownKey = KEY_s;
97 
99  KeySymbol rollLeftKey = KEY_q;
100 
102  KeySymbol rollRightKey = KEY_e;
103 
105  KeySymbol moveForwardKey = KEY_o;
106 
108  KeySymbol moveBackwardKey = KEY_i;
109 
111  KeySymbol moveLeftKey = KEY_Left;
112 
114  KeySymbol moveRightKey = KEY_Right;
115 
117  KeySymbol moveUpKey = KEY_Up;
118 
120  KeySymbol moveDownKey = KEY_Down;
121 
123  ButtonMask rotateButtonMask = BUTTON_MASK_1;
124 
126  ButtonMask panButtonMask = BUTTON_MASK_2;
127 
129  ButtonMask zoomButtonMask = BUTTON_MASK_3;
130 
132  ButtonMask touchMappedToButtonMask = BUTTON_MASK_1;
133 
135  double zoomScale = 1.0;
136 
138  bool supportsThrow = true;
139 
140  protected:
141  ref_ptr<Camera> _camera;
142  ref_ptr<LookAt> _lookAt;
143  ref_ptr<EllipsoidModel> _ellipsoidModel;
144 
145  bool _hasKeyboardFocus = false;
146  bool _hasPointerFocus = false;
147  bool _lastPointerEventWithinRenderArea = false;
148 
149  enum UpdateMode
150  {
151  INACTIVE = 0,
152  ROTATE,
153  PAN,
154  ZOOM
155  };
156  UpdateMode _updateMode = INACTIVE;
157  double _zoomPreviousRatio = 0.0;
158  dvec2 _pan;
159  double _rotateAngle = 0.0;
160  dvec3 _rotateAxis;
161 
162  time_point _previousTime;
163  ref_ptr<PointerEvent> _previousPointerEvent;
164  double _previousDelta = 0.0;
165  double _prevZoomTouchDistance = 0.0;
166  bool _thrown = false;
167 
168  time_point _startTime;
169  ref_ptr<LookAt> _startLookAt;
170  ref_ptr<LookAt> _endLookAt;
171  std::map<uint32_t, ref_ptr<TouchEvent>> _previousTouches;
172 
173  ref_ptr<Keyboard> _keyboard;
174 
175  double _animationDuration = 0.0;
176  };
177  VSG_type_name(vsg::Trackball);
178 
179 } // namespace vsg
ButtonPressEvent represents a button press event.
Definition: PointerEvent.h:55
ButtonReleaseEvent represents a button release event.
Definition: PointerEvent.h:72
FocusInEvent represents a window acquiring focus event.
Definition: WindowEvent.h:98
FocusOutEvent represents a window losing focus event.
Definition: WindowEvent.h:109
Definition: ApplicationEvent.h:37
Definition: Inherit.h:28
KeyPressEvent represents a key press event.
Definition: KeyEvent.h:309
KeyReleaseEvent represents a key release event.
Definition: KeyEvent.h:320
MoveEvent represents a pointer move event.
Definition: PointerEvent.h:89
PointerEvent is a base class for mouse pointer events.
Definition: PointerEvent.h:34
ScrollWheelEvent represents a scroll wheel event.
Definition: ScrollWheelEvent.h:22
TouchDownEvent represents a touch down event.
Definition: TouchEvent.h:45
TouchMoveEvent represents a touch move event.
Definition: TouchEvent.h:67
TouchUpEvent represents a touch up event.
Definition: TouchEvent.h:56
Trackball is an event handler that provides mouse and touch controlled 3d trackball camera view manip...
Definition: Trackball.h:28
dvec2 ndc(PointerEvent &event)
compute non dimensional window coordinate (-1,1) from event coords
void addWindow(ref_ptr< Window > window, const ivec2 &offset={})
add a Window to respond events for, with mouse coordinate offset to treat all associated windows
void addKeyViewpoint(KeySymbol key, ref_ptr< LookAt > lookAt, double duration=1.0)
add Key to Viewpoint binding using a LookAt to define the viewpoint
std::map< KeySymbol, Viewpoint > keyViewpointMap
container that maps key symbol bindings with the Viewpoint that should move the LookAt to when presse...
Definition: Trackball.h:84
void addKeyViewpoint(KeySymbol key, double latitude, double longitude, double altitude, double duration=1.0)
add Key to Viewpoint binding using a latitude, longitude and altitude to define the viewpoint....
void setViewpoint(ref_ptr< LookAt > lookAt, double duration=1.0)
std::map< observer_ptr< Window >, ivec2 > windowOffsets
list of windows that this Trackball should respond to events from, and the points xy offsets to apply
Definition: Trackball.h:62
dvec3 tbc(PointerEvent &event)
compute trackball coordinate from event coords
WindowEvent is the base class for events related to a window.
Definition: WindowEvent.h:24
Definition: ref_ptr.h:22
Definition: Trackball.h:78