vsg: include/vsg/app/Trackball.h Source File

vsg 1.1.13 VulkanSceneGraph library

Loading...
Searching...
No Matches
Trackball.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2019 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#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
23namespace 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
33 dvec2 ndc(const PointerEvent& event);
34
36 dvec3 tbc(const PointerEvent& event);
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
vsg::ButtonPressEvent
ButtonPressEvent represents a button press event.
Definition PointerEvent.h:55
vsg::ButtonReleaseEvent
ButtonReleaseEvent represents a button release event.
Definition PointerEvent.h:72
vsg::FocusInEvent
FocusInEvent represents a window acquiring focus event.
Definition WindowEvent.h:98
vsg::FocusOutEvent
FocusOutEvent represents a window losing focus event.
Definition WindowEvent.h:109
vsg::FrameEvent
Definition ApplicationEvent.h:37
vsg::KeyPressEvent
KeyPressEvent represents a key press event.
Definition KeyEvent.h:309
vsg::KeyReleaseEvent
KeyReleaseEvent represents a key release event.
Definition KeyEvent.h:320
vsg::MoveEvent
MoveEvent represents a pointer move event.
Definition PointerEvent.h:89
vsg::PointerEvent
PointerEvent is a base class for mouse pointer events.
Definition PointerEvent.h:34
vsg::ScrollWheelEvent
ScrollWheelEvent represents a scroll wheel event.
Definition ScrollWheelEvent.h:22
vsg::TouchDownEvent
TouchDownEvent represents a touch down event.
Definition TouchEvent.h:45
vsg::TouchMoveEvent
TouchMoveEvent represents a touch move event.
Definition TouchEvent.h:67
vsg::TouchUpEvent
TouchUpEvent represents a touch up event.
Definition TouchEvent.h:56
vsg::Trackball
Trackball is an event handler that provides mouse and touch controlled 3d trackball camera view manip...
Definition Trackball.h:28
vsg::Trackball::ndc
dvec2 ndc(const PointerEvent &event)
compute non dimensional window coordinate (-1,1) from event coords
vsg::Trackball::touchMappedToButtonMask
ButtonMask touchMappedToButtonMask
Button mask value used used for touch events.
Definition Trackball.h:132
vsg::Trackball::supportsThrow
bool supportsThrow
Toggle on/off whether the view should continue moving when the mouse buttons are released while the m...
Definition Trackball.h:138
vsg::Trackball::moveForwardKey
KeySymbol moveForwardKey
Key that moves the view forward.
Definition Trackball.h:105
vsg::Trackball::addWindow
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
vsg::Trackball::addKeyViewpoint
void addKeyViewpoint(KeySymbol key, ref_ptr< LookAt > lookAt, double duration=1.0)
add Key to Viewpoint binding using a LookAt to define the viewpoint
vsg::Trackball::keyViewpointMap
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
vsg::Trackball::rollRightKey
KeySymbol rollRightKey
Key that rolls the view clockwise/right.
Definition Trackball.h:102
vsg::Trackball::rotateButtonMask
ButtonMask rotateButtonMask
Button mask value used to enable panning of the view, defaults to left mouse button.
Definition Trackball.h:123
vsg::Trackball::moveLeftKey
KeySymbol moveLeftKey
Key that moves the view left.
Definition Trackball.h:111
vsg::Trackball::zoomScale
double zoomScale
Scale for controlling how rapidly the view zooms in/out. Positive value zooms in when mouse moves dow...
Definition Trackball.h:135
vsg::Trackball::addKeyViewpoint
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....
vsg::Trackball::pitchUpKey
KeySymbol pitchUpKey
Key that pitches up the view around the eye point.
Definition Trackball.h:93
vsg::Trackball::setViewpoint
void setViewpoint(ref_ptr< LookAt > lookAt, double duration=1.0)
vsg::Trackball::windowOffsets
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
vsg::Trackball::moveBackwardKey
KeySymbol moveBackwardKey
Key that moves the view backwards.
Definition Trackball.h:108
vsg::Trackball::zoomButtonMask
ButtonMask zoomButtonMask
Button mask value used to enable zooming of the view, defaults to right mouse button.
Definition Trackball.h:129
vsg::Trackball::rollLeftKey
KeySymbol rollLeftKey
Key that rools the view anti-clockwise/left.
Definition Trackball.h:99
vsg::Trackball::turnRightKey
KeySymbol turnRightKey
Key that turns the view right around the eye points.
Definition Trackball.h:90
vsg::Trackball::moveUpKey
KeySymbol moveUpKey
Key that moves the view upward.
Definition Trackball.h:117
vsg::Trackball::panButtonMask
ButtonMask panButtonMask
Button mask value used to enable panning of the view, defaults to middle mouse button.
Definition Trackball.h:126
vsg::Trackball::moveRightKey
KeySymbol moveRightKey
Key that moves the view right.
Definition Trackball.h:114
vsg::Trackball::moveDownKey
KeySymbol moveDownKey
Key that moves the view downward.
Definition Trackball.h:120
vsg::Trackball::tbc
dvec3 tbc(const PointerEvent &event)
compute trackball coordinate from event coords
vsg::Trackball::pitchDownKey
KeySymbol pitchDownKey
Key that pitches down the view around the eye point.
Definition Trackball.h:96
vsg::Trackball::turnLeftKey
KeySymbol turnLeftKey
Key that turns the view left around the eye points.
Definition Trackball.h:87
vsg::WindowEvent
WindowEvent is the base class for events related to a window.
Definition WindowEvent.h:24
vsg::ref_ptr
Definition ref_ptr.h:22
vsg::Trackball::Viewpoint
Definition Trackball.h:78