DART  6.6.2
DefaultEventHandler.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2018, The DART development contributors
3  * All rights reserved.
4  *
5  * The list of contributors can be found at:
6  * https://github.com/dartsim/dart/blob/master/LICENSE
7  *
8  * This file is provided under the following "BSD-style" License:
9  * Redistribution and use in source and binary forms, with or
10  * without modification, are permitted provided that the following
11  * conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef DART_GUI_OSG_DEFAULTEVENTHANDLER_HPP_
34 #define DART_GUI_OSG_DEFAULTEVENTHANDLER_HPP_
35 
36 #include <vector>
37 #include <array>
38 #include <memory>
39 
40 #include <Eigen/Core>
41 
42 #include <osgGA/GUIEventHandler>
43 
44 #include "dart/common/Subject.hpp"
45 #include "dart/common/Observer.hpp"
46 
47 namespace dart {
48 
49 namespace dynamics {
50 class Shape;
51 class ShapeFrame;
52 class Entity;
53 } // dynamics
54 
55 namespace gui {
56 namespace osg {
57 
58 struct PickInfo
59 {
61  std::shared_ptr<dart::dynamics::Shape> shape;
62  Eigen::Vector3d position;
63  Eigen::Vector3d normal;
64 };
65 
66 class Viewer;
67 
69 
73 
75 };
76 
78 
83 
84 };
85 
87 
92 
94 };
95 
96 class MouseEventHandler;
97 
98 class DefaultEventHandler : public ::osgGA::GUIEventHandler,
99  public virtual dart::common::Subject,
100  public virtual dart::common::Observer
101 {
102 public:
103 
105  explicit DefaultEventHandler(Viewer* _viewer);
106 
108  virtual ~DefaultEventHandler();
109 
112 
114  int getModKeyMask() const;
115 
117  double getWindowCursorX() const;
118 
120  double getWindowCursorY() const;
121 
133  Eigen::Vector3d getDeltaCursor(const Eigen::Vector3d& _fromPosition,
134  ConstraintType _constraint=UNCONSTRAINED,
135  const Eigen::Vector3d& _constraintVector =
136  Eigen::Vector3d::UnitZ()) const;
137 
141  void getNearAndFarPointUnderCursor(Eigen::Vector3d& near,
142  Eigen::Vector3d& far,
143  double distance=1.0) const;
144 
146  const std::vector<PickInfo>& getButtonPicks(MouseButton button,
147  MouseButtonEvent event) const;
148 
151  const std::vector<PickInfo>& getMovePicks() const;
152 
155 
157  void suppressMovePicks();
158 
161 
163  void activateMovePicks();
164 
167  void pick(std::vector<PickInfo>& infoVector,
168  const ::osgGA::GUIEventAdapter& ea);
169 
175 
178  const std::set<MouseEventHandler*>& getMouseEventHandlers() const;
179 
181  bool handle(const ::osgGA::GUIEventAdapter& ea,
182  ::osgGA::GUIActionAdapter&) override;
183 
184 protected:
185 
188 
190  void eventPick(const ::osgGA::GUIEventAdapter& ea);
191 
193  void clearButtonEvents();
194 
196  const dart::common::Subject* _subject) override;
197 
200 
202  std::set<MouseEventHandler*> mMouseEventHandlers;
203 
206 
209 
211  std::vector<PickInfo> mMovePicks;
212 
215 
217  std::vector<PickInfo> mTempPicks;
218 
221 
224  Eigen::Vector2d mLastCursorPosition;
225 
228 
229 };
230 
231 } // namespace osg
232 } // namespace gui
233 } // namespace dart
234 
235 #endif // DART_GUI_OSG_DEFAULTEVENTHANDLER_HPP_
The Observer class should be inherited by any class that wants to respond in a customized way to the ...
Definition: Observer.hpp:52
The Subject class is a base class for any object that wants to report when it gets destroyed.
Definition: Subject.hpp:58
Definition: ShapeFrame.hpp:164
Definition: DefaultEventHandler.hpp:101
std::vector< PickInfo > mMovePicks
The objects that were under the cursor during the last move.
Definition: DefaultEventHandler.hpp:211
int getModKeyMask() const
Returns the last modkey mask.
Definition: DefaultEventHandler.cpp:81
void clearButtonEvents()
Clear out the current button events.
Definition: DefaultEventHandler.cpp:426
double getWindowCursorX() const
Get the last x value of the cursor in Window coordinates.
Definition: DefaultEventHandler.cpp:87
void activateMovePicks()
Activate pick detection for mouse movements (on by default)
Definition: DefaultEventHandler.cpp:208
void pick(std::vector< PickInfo > &infoVector, const ::osgGA::GUIEventAdapter &ea)
Detect picks TODO(MXG): Consider putting this functionality in a more accessible place.
Definition: DefaultEventHandler.cpp:214
std::set< MouseEventHandler * > mMouseEventHandlers
Set of MouseEventHandlers that are tied to this DefaultEventHandler.
Definition: DefaultEventHandler.hpp:202
void triggerMouseEventHandlers()
Calls update on all MouseEventHandlers.
Definition: DefaultEventHandler.cpp:378
bool mSuppressMovePicks
Suppress pick detection for moves.
Definition: DefaultEventHandler.hpp:214
void suppressButtonPicks(MouseButton button, MouseButtonEvent event)
Suppress pick detection for the specified button event.
Definition: DefaultEventHandler.cpp:182
const std::vector< PickInfo > & getButtonPicks(MouseButton button, MouseButtonEvent event) const
Get the most recent picks for the specified button and event type.
Definition: DefaultEventHandler.cpp:166
bool handle(const ::osgGA::GUIEventAdapter &ea, ::osgGA::GUIActionAdapter &) override
Handle incoming user input.
Definition: DefaultEventHandler.cpp:304
DefaultEventHandler(Viewer *_viewer)
Constructor takes in a pointer to a viewer.
Definition: DefaultEventHandler.cpp:52
void suppressMovePicks()
Suppress pick detection for mouse movements.
Definition: DefaultEventHandler.cpp:192
std::vector< PickInfo > mTempPicks
Cache for pick data.
Definition: DefaultEventHandler.hpp:217
void activateButtonPicks(MouseButton button, MouseButtonEvent event)
Activate pick detection for the specified button event (on by default)
Definition: DefaultEventHandler.cpp:198
Eigen::Vector3d getDeltaCursor(const Eigen::Vector3d &_fromPosition, ConstraintType _constraint=UNCONSTRAINED, const Eigen::Vector3d &_constraintVector=Eigen::Vector3d::UnitZ()) const
Get the change change in the cursor position with respect to some previous location in the world (_fr...
Definition: DefaultEventHandler.cpp:99
MouseButtonEvent getButtonEvent(MouseButton button) const
Returns the last event performed by a mouse button.
Definition: DefaultEventHandler.cpp:75
Eigen::Vector2d mLastCursorPosition
X/Y values of the cursor (in the window coordinates) during the last mouse event.
Definition: DefaultEventHandler.hpp:224
void getNearAndFarPointUnderCursor(Eigen::Vector3d &near, Eigen::Vector3d &far, double distance=1.0) const
Get two points that are under the current cursor position.
Definition: DefaultEventHandler.cpp:147
void eventPick(const ::osgGA::GUIEventAdapter &ea)
Gather current picks and assign them to the latest event.
Definition: DefaultEventHandler.cpp:387
void handleDestructionNotification(const dart::common::Subject *_subject) override
Called by receiveDestructionNotification().
Definition: DefaultEventHandler.cpp:433
void addMouseEventHandler(MouseEventHandler *handler)
Add a MouseEventHandler that will get invoked whenever a mouse event occurs.
Definition: DefaultEventHandler.cpp:243
virtual ~DefaultEventHandler()
Destructor.
Definition: DefaultEventHandler.cpp:69
const std::vector< PickInfo > & getMovePicks() const
Get the most recent picks for a mouse movement (click-and-drag actions do not qualify as movements)
Definition: DefaultEventHandler.cpp:176
std::vector< PickInfo > mButtonPicks[NUM_MOUSE_BUTTONS][BUTTON_NOTHING]
The objects that were under the cursor during the last button event.
Definition: DefaultEventHandler.hpp:205
bool mSuppressButtonPicks[NUM_MOUSE_BUTTONS][BUTTON_NOTHING]
Suppress pick detection.
Definition: DefaultEventHandler.hpp:208
double getWindowCursorY() const
Get the last y value of the cursor in Window coordinates.
Definition: DefaultEventHandler.cpp:93
MouseButtonEvent mLastButtonEvent[NUM_MOUSE_BUTTONS]
The last mouse event that was registered by the event handler.
Definition: DefaultEventHandler.hpp:220
Viewer * mViewer
dart::gui::osg::Viewer that this event handler is tied to
Definition: DefaultEventHandler.hpp:199
const std::set< MouseEventHandler * > & getMouseEventHandlers() const
Get the list of MouseEventHandlers that are currently held by this DefaultEventHandler.
Definition: DefaultEventHandler.cpp:252
int mLastModKeyMask
Storage for the last modkey mask.
Definition: DefaultEventHandler.hpp:227
Definition: MouseEventHandler.hpp:49
Definition: Viewer.hpp:116
MouseButton
Definition: DefaultEventHandler.hpp:68
@ MIDDLE_MOUSE
Definition: DefaultEventHandler.hpp:72
@ RIGHT_MOUSE
Definition: DefaultEventHandler.hpp:71
@ NUM_MOUSE_BUTTONS
Definition: DefaultEventHandler.hpp:74
@ LEFT_MOUSE
Definition: DefaultEventHandler.hpp:70
ConstraintType
Definition: DefaultEventHandler.hpp:86
@ PLANE_CONSTRAINT
Definition: DefaultEventHandler.hpp:90
@ CUSTOM_CONSTRAINT
Definition: DefaultEventHandler.hpp:91
@ UNCONSTRAINED
Definition: DefaultEventHandler.hpp:88
@ LINE_CONSTRAINT
Definition: DefaultEventHandler.hpp:89
@ NUM_CONSTRAINT_TYPES
Definition: DefaultEventHandler.hpp:93
MouseButtonEvent
Definition: DefaultEventHandler.hpp:77
@ BUTTON_DRAG
Definition: DefaultEventHandler.hpp:80
@ BUTTON_RELEASE
Definition: DefaultEventHandler.hpp:81
@ BUTTON_PUSH
Definition: DefaultEventHandler.hpp:79
@ BUTTON_NOTHING
Definition: DefaultEventHandler.hpp:82
Definition: BulletCollisionDetector.cpp:63
Definition: DefaultEventHandler.hpp:59
dart::dynamics::ShapeFrame * frame
Definition: DefaultEventHandler.hpp:60
Eigen::Vector3d normal
Definition: DefaultEventHandler.hpp:63
std::shared_ptr< dart::dynamics::Shape > shape
Definition: DefaultEventHandler.hpp:61
Eigen::Vector3d position
Definition: DefaultEventHandler.hpp:62