dart
DefaultEventHandler.hpp
1 /*
2  * Copyright (c) 2011-2021, 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 <array>
37 #include <memory>
38 #include <vector>
39 
40 #include <Eigen/Core>
41 
42 #include <osgGA/GUIEventHandler>
43 
44 #include "dart/common/ClassWithVirtualBase.hpp"
45 #include "dart/common/Observer.hpp"
46 #include "dart/common/Subject.hpp"
47 
48 namespace dart {
49 
50 namespace dynamics {
51 class Shape;
52 class ShapeFrame;
53 class Entity;
54 } // namespace dynamics
55 
56 namespace gui {
57 namespace osg {
58 
59 struct PickInfo
60 {
62  std::shared_ptr<dart::dynamics::Shape> shape;
63  Eigen::Vector3d position;
64  Eigen::Vector3d normal;
65 };
66 
67 class Viewer;
68 
69 enum MouseButton
70 {
71 
72  LEFT_MOUSE = 0,
73  RIGHT_MOUSE,
74  MIDDLE_MOUSE,
75 
76  NUM_MOUSE_BUTTONS
77 };
78 
79 enum MouseButtonEvent
80 {
81 
82  BUTTON_PUSH = 0,
83  BUTTON_DRAG,
84  BUTTON_RELEASE,
85  BUTTON_NOTHING
86 
87 };
88 
89 enum ConstraintType
90 {
91 
92  UNCONSTRAINED = 0,
93  LINE_CONSTRAINT,
94  PLANE_CONSTRAINT,
95  CUSTOM_CONSTRAINT,
96 
97  NUM_CONSTRAINT_TYPES
98 };
99 
100 class MouseEventHandler;
101 
102 DART_DECLARE_CLASS_WITH_VIRTUAL_BASE_BEGIN
103 class DefaultEventHandler : public ::osgGA::GUIEventHandler,
104  public virtual dart::common::Subject,
105  public virtual dart::common::Observer
106 {
107 public:
109  explicit DefaultEventHandler(Viewer* _viewer);
110 
112  virtual ~DefaultEventHandler();
113 
115  MouseButtonEvent getButtonEvent(MouseButton button) const;
116 
118  int getModKeyMask() const;
119 
121  double getWindowCursorX() const;
122 
124  double getWindowCursorY() const;
125 
137  Eigen::Vector3d getDeltaCursor(
138  const Eigen::Vector3d& _fromPosition,
139  ConstraintType _constraint = UNCONSTRAINED,
140  const Eigen::Vector3d& _constraintVector
141  = Eigen::Vector3d::UnitZ()) const;
142 
146  void getNearAndFarPointUnderCursor(
147  Eigen::Vector3d& near, Eigen::Vector3d& far, double distance = 1.0) const;
148 
150  const std::vector<PickInfo>& getButtonPicks(
151  MouseButton button, MouseButtonEvent event) const;
152 
155  const std::vector<PickInfo>& getMovePicks() const;
156 
158  void suppressButtonPicks(MouseButton button, MouseButtonEvent event);
159 
161  void suppressMovePicks();
162 
164  void activateButtonPicks(MouseButton button, MouseButtonEvent event);
165 
167  void activateMovePicks();
168 
171  void pick(
172  std::vector<PickInfo>& infoVector, const ::osgGA::GUIEventAdapter& ea);
173 
178  void addMouseEventHandler(MouseEventHandler* handler);
179 
182  const std::set<MouseEventHandler*>& getMouseEventHandlers() const;
183 
185  bool handle(
186  const ::osgGA::GUIEventAdapter& ea, ::osgGA::GUIActionAdapter&) override;
187 
188 protected:
190  void triggerMouseEventHandlers();
191 
193  void eventPick(const ::osgGA::GUIEventAdapter& ea);
194 
196  void clearButtonEvents();
197 
198  void handleDestructionNotification(
199  const dart::common::Subject* _subject) override;
200 
203 
205  std::set<MouseEventHandler*> mMouseEventHandlers;
206 
208  std::vector<PickInfo> mButtonPicks[NUM_MOUSE_BUTTONS][BUTTON_NOTHING];
209 
211  bool mSuppressButtonPicks[NUM_MOUSE_BUTTONS][BUTTON_NOTHING];
212 
214  std::vector<PickInfo> mMovePicks;
215 
218 
220  std::vector<PickInfo> mTempPicks;
221 
223  MouseButtonEvent mLastButtonEvent[NUM_MOUSE_BUTTONS];
224 
227  Eigen::Vector2d mLastCursorPosition;
228 
231 };
232 DART_DECLARE_CLASS_WITH_VIRTUAL_BASE_END
233 
234 } // namespace osg
235 } // namespace gui
236 } // namespace dart
237 
238 #endif // DART_GUI_OSG_DEFAULTEVENTHANDLER_HPP_
std::set< MouseEventHandler * > mMouseEventHandlers
Set of MouseEventHandlers that are tied to this DefaultEventHandler.
Definition: DefaultEventHandler.hpp:205
The Subject class is a base class for any object that wants to report when it gets destroyed...
Definition: Subject.hpp:57
Viewer * mViewer
dart::gui::osg::Viewer that this event handler is tied to
Definition: DefaultEventHandler.hpp:202
Eigen::Vector2d mLastCursorPosition
X/Y values of the cursor (in the window coordinates) during the last mouse event. ...
Definition: DefaultEventHandler.hpp:227
Definition: DefaultEventHandler.hpp:59
int mLastModKeyMask
Storage for the last modkey mask.
Definition: DefaultEventHandler.hpp:230
Definition: Aspect.cpp:40
bool mSuppressMovePicks
Suppress pick detection for moves.
Definition: DefaultEventHandler.hpp:217
std::vector< PickInfo > mMovePicks
The objects that were under the cursor during the last move.
Definition: DefaultEventHandler.hpp:214
Definition: MouseEventHandler.hpp:49
Definition: ShapeFrame.hpp:189
Definition: Viewer.hpp:111
Definition: DefaultEventHandler.hpp:103
The Observer class should be inherited by any class that wants to respond in a customized way to the ...
Definition: Observer.hpp:51
std::vector< PickInfo > mTempPicks
Cache for pick data.
Definition: DefaultEventHandler.hpp:220