-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathEventHandler.cpp
executable file
·220 lines (194 loc) · 7.61 KB
/
EventHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "EventHandler.h"
#include <assert.h>
#include <iostream>
#include <osgViewer/View>
#include <osg/Camera>
#include <osg/ref_ptr>
#include <osg/Plane>
#include "LineIntersector.h"
#include "SVMData.h"
EventHandler::EventHandler()
: osgGA::GUIEventHandler()
, m_selection(0)
, m_mode(MOUSE_IDLE)
{
}
bool EventHandler::handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
{
bool handled = true;
switch(m_mode){
case MOUSE_HOVER_WIRE:
case MOUSE_DRAG_WIRE:
this->doHoverWire(ea, aa);
break;
case MOUSE_HOVER_POINT:
this->doHoverPoint(ea, aa);
break;
case MOUSE_DRAG_POINT:
this->doDragPoint(ea, aa);
break;
case MOUSE_IDLE:
default:
this->doIdleMouse(ea, aa);
handled = false;
break;
}
return handled;
}
void EventHandler::doIdleMouse(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
if (ea.getEventType() != osgGA::GUIEventAdapter::MOVE)
return;
bool isModeSame = true;
LineIntersector::Intersection intersection;
std::tie(isModeSame, intersection) = this->setMouseState<LineIntersector::Intersection, LineIntersector>(ea, aa, MOUSE_IDLE);
this->updateWireGeometry(intersection);
}
void EventHandler::doHoverWire(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
if (ea.getEventType() != osgGA::GUIEventAdapter::MOVE)
return;
bool isModeSame = true;
LineIntersector::Intersection intersectionLine;
std::tie(isModeSame, intersectionLine) = this->setMouseState<LineIntersector::Intersection, LineIntersector>(ea, aa, MOUSE_IDLE);
this->updateWireGeometry(intersectionLine);
if (!isModeSame) return;
PointIntersector::Intersection intersectionPoint;
std::tie(isModeSame, intersectionPoint) =
this->setMouseState<PointIntersector::Intersection, PointIntersector>(ea, aa, MOUSE_HOVER_WIRE);
this->updatePointGeometry(intersectionPoint);
}
void EventHandler::doHoverPoint(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
{
if ( !(ea.getEventType() == osgGA::GUIEventAdapter::MOVE ||
(ea.getEventType() == osgGA::GUIEventAdapter::DRAG && ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)) )
return;
bool isModeSame = true;
PointIntersector::Intersection intersectionPoint;
std::tie(isModeSame, intersectionPoint) =
this->setMouseState<PointIntersector::Intersection, PointIntersector>(ea, aa, MOUSE_HOVER_WIRE);
this->updatePointGeometry(intersectionPoint);
}
void EventHandler::doDragPoint(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
{
if (!( (ea.getEventType() == osgGA::GUIEventAdapter::PUSH && ea.getButtonMask()== osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
|| (ea.getEventType() == osgGA::GUIEventAdapter::DRAG && ea.getButtonMask()== osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
|| (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE && ea.getButton()==osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
))
return;
/* obtain new location of the dragging point and edit the selection */
osg::ref_ptr<VirtualPlaneIntersector> vpi = new VirtualPlaneIntersector(m_selection.get());
VirtualPlaneIntersector::Intersection intersection = vpi->getIntersection(ea, aa);
this->updateDragPointGeometry(intersection, ea);
}
DraggableWire *EventHandler::getDraggableWire(const osgUtil::LineSegmentIntersector::Intersection &result)
{
if (!result.drawable.get()) return NULL;
osg::Group* group = result.drawable->getParent(0);
if (!group) return NULL;
osg::Group* parent = group->getParent(0);
return parent? dynamic_cast<DraggableWire*>(parent) : NULL;
}
int EventHandler::getSelectedPoint(const osgUtil::LineSegmentIntersector::Intersection &result)
{
if (!result.drawable.get()) return -1;
return result.primitiveIndex;
}
EDIT_MODE EventHandler::getModeFromName(const osgUtil::LineSegmentIntersector::Intersection &result, EDIT_MODE mode_default) const
{
std::string name = result.drawable->getName();
if (name == "Wire")
return MOUSE_HOVER_WIRE;
else if (name == "Point")
return MOUSE_HOVER_POINT;
return mode_default;
}
EDIT_MODE EventHandler::getModeFromEvent(EDIT_MODE mode, const osgGA::GUIEventAdapter &ea)
{
return static_cast<EDIT_MODE>((ea.getEventType() == osgGA::GUIEventAdapter::DRAG)?
(mode | EDIT_MODE::DRAG_MASK) : mode);
}
void EventHandler::updateWireGeometry(const osgUtil::LineSegmentIntersector::Intersection &intersection)
{
if (intersection.drawable.get()){
if (!m_selection.get()){
DraggableWire* wire = this->getDraggableWire(intersection);
if (!wire) return;
wire->select();
m_selection = wire;
}
}
else {
if (m_selection.get()){
m_selection->unselect();
m_selection = 0;
}
}
}
void EventHandler::updatePointGeometry(const osgUtil::LineSegmentIntersector::Intersection &intersection)
{
if (intersection.drawable.get()){
// pick a point
if (!m_selection.get()){
DraggableWire* wire = this->getDraggableWire(intersection);
if (!wire) return;
wire->select();
}
if (!m_selection->getGeode()->containsDrawable(intersection.drawable.get())) return;
int index = this->getSelectedPoint(intersection);
if (! (m_mode & EDIT_MODE::DRAG_MASK))
m_selection->pick(index);
else
m_selection->drag();
}
else {
// unpick the point
if (m_selection.get()){
m_selection->unpick();
}
}
}
void EventHandler::updateDragPointGeometry(const std::tuple<double, double, bool> &intersection, const osgGA::GUIEventAdapter &ea)
{
if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE || !std::get<2>(intersection)){
m_selection->dragStop();
m_mode = MOUSE_HOVER_POINT;
}
else {
m_selection->editPick(std::get<0>(intersection), std::get<1>(intersection));
}
}
template <typename TypeIntersection, typename TypeIntersector>
bool EventHandler::getIntersection(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, TypeIntersection& resultIntersection)
{
osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
if (!viewer) return false;
osg::ref_ptr<TypeIntersector> intersector = new TypeIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
osgUtil::IntersectionVisitor iv(intersector);
osg::Camera* cam = viewer->getCamera();
if (!cam) return false;
cam->accept(iv);
if (!intersector->containsIntersections()) return false;
resultIntersection = intersector->getFirstIntersection();
return true;
}
template <typename TResult, typename TIntersector>
std::tuple<bool, TResult> EventHandler::setMouseState(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, EDIT_MODE modeDefault)
{
bool isModeSame = true;
TResult intersection;
bool inter_occured = this->getIntersection<TResult, TIntersector>(ea,aa, intersection);
/* if mouse is hovering over certain drawable, set the corresponding mode */
if (inter_occured){
EDIT_MODE mode = this->getModeFromName(intersection, modeDefault);
mode = this->getModeFromEvent(mode, ea);
isModeSame = (mode == m_mode);
m_mode = mode;
}
/* if not, or if the mouse left the drawable area, make sure it is in entity select mode */
else{
isModeSame = m_mode == modeDefault;
m_mode = modeDefault;
}
return std::make_tuple(isModeSame, intersection);
}