vsg  1.0.4
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/ApplicationEvent.h>
19 #include <vsg/ui/KeyEvent.h>
20 #include <vsg/ui/PointerEvent.h>
21 #include <vsg/ui/ScrollWheelEvent.h>
22 #include <vsg/ui/TouchEvent.h>
23 
24 namespace vsg
25 {
26 
28  class VSG_DECLSPEC Trackball : public Inherit<Visitor, Trackball>
29  {
30  public:
31  explicit Trackball(ref_ptr<Camera> camera, ref_ptr<EllipsoidModel> ellipsoidModel = {});
32 
35 
38 
39  void apply(KeyPressEvent& keyPress) override;
40  void apply(ButtonPressEvent& buttonPress) override;
41  void apply(ButtonReleaseEvent& buttonRelease) override;
42  void apply(MoveEvent& moveEvent) override;
43  void apply(ScrollWheelEvent& scrollWheel) override;
44  void apply(TouchDownEvent& touchDown) override;
45  void apply(TouchUpEvent& touchUp) override;
46  void apply(TouchMoveEvent& touchMove) override;
47  void apply(FrameEvent& frame) override;
48 
49  virtual void rotate(double angle, const dvec3& axis);
50  virtual void zoom(double ratio);
51  virtual void pan(const dvec2& delta);
52 
53  std::pair<int32_t, int32_t> cameraRenderAreaCoordinates(const PointerEvent& pointerEvent) const;
54  bool withinRenderArea(const PointerEvent& pointerEvent) const;
55 
56  void clampToGlobe();
57 
59  std::map<observer_ptr<Window>, ivec2> windowOffsets;
60 
62  void addWindow(ref_ptr<Window> window, const ivec2& offset = {});
63 
65  void addKeyViewpoint(KeySymbol key, ref_ptr<LookAt> lookAt, double duration = 1.0);
66 
68  void addKeyViewpoint(KeySymbol key, double latitude, double longitude, double altitude, double duration = 1.0);
69 
72  void setViewpoint(ref_ptr<LookAt> lookAt, double duration = 1.0);
73 
74  struct Viewpoint
75  {
76  ref_ptr<LookAt> lookAt;
77  double duration = 0.0;
78  };
79 
81  std::map<KeySymbol, Viewpoint> keyViewpointMap;
82 
84  ButtonMask rotateButtonMask = BUTTON_MASK_1;
85 
87  ButtonMask panButtonMask = BUTTON_MASK_2;
88 
90  ButtonMask zoomButtonMask = BUTTON_MASK_3;
91 
93  double zoomScale = 1.0;
94 
96  bool supportsThrow = true;
97 
98  protected:
99  ref_ptr<Camera> _camera;
100  ref_ptr<LookAt> _lookAt;
101  ref_ptr<EllipsoidModel> _ellipsoidModel;
102 
103  bool _hasFocus = false;
104  bool _lastPointerEventWithinRenderArea = false;
105 
106  enum UpdateMode
107  {
108  INACTIVE = 0,
109  ROTATE,
110  PAN,
111  ZOOM
112  };
113  UpdateMode _updateMode = INACTIVE;
114  double _zoomPreviousRatio = 0.0;
115  dvec2 _pan;
116  double _rotateAngle = 0.0;
117  dvec3 _rotateAxis;
118 
119  time_point _previousTime;
120  ref_ptr<PointerEvent> _previousPointerEvent;
121  double _previousDelta = 0.0;
122  double _prevZoomTouchDistance = 0.0;
123  bool _thrown = false;
124 
125  time_point _startTime;
126  ref_ptr<LookAt> _startLookAt;
127  ref_ptr<LookAt> _endLookAt;
128  std::map<uint32_t, ref_ptr<TouchEvent>> _previousTouches;
129 
130  double _animationDuration = 0.0;
131  };
132  VSG_type_name(vsg::Trackball);
133 
134 } // namespace vsg
ButtonPressEvent represent a button press event.
Definition: PointerEvent.h:54
ButtonReleaseEvent represent a button release event.
Definition: PointerEvent.h:71
Definition: ApplicationEvent.h:37
Definition: Inherit.h:28
KeyPressEvent represents a key press event.
Definition: KeyEvent.h:308
MoveEvent represent a button move event.
Definition: PointerEvent.h:88
PointerEvent is a base class for mouse pointer events.
Definition: PointerEvent.h:33
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:29
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:81
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:59
dvec3 tbc(PointerEvent &event)
compute trackball coordinate from event coords
Definition: ref_ptr.h:22
Definition: Trackball.h:75