-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathViewerEventHandlers.cpp
More file actions
275 lines (210 loc) · 7.72 KB
/
ViewerEventHandlers.cpp
File metadata and controls
275 lines (210 loc) · 7.72 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// -*-c++-*- osgWidget - Code by: Jeremy Moles (cubicool) 2007-2008
#include <osgWidget/ViewerEventHandlers>
namespace osgWidget {
MouseHandler::MouseHandler(WindowManager* wm):
_wm(wm) {
}
bool MouseHandler::handle(
const osgGA::GUIEventAdapter& gea,
osgGA::GUIActionAdapter& /*gaa*/,
osg::Object* /*obj*/,
osg::NodeVisitor* /*nv*/
) {
osgGA::GUIEventAdapter::EventType ev = gea.getEventType();
MouseAction ma = _isMouseEvent(ev);
if(ma) {
// If we're scrolling, we need to inform the WindowManager of that.
_wm->setScrollingMotion(gea.getScrollingMotion());
// osgWidget assumes origin is bottom left of window so make sure mouse coordinate are increaseing y upwards and are scaled to window size.
float x = (gea.getX()-gea.getXmin())/(gea.getXmax()-gea.getXmin())*static_cast<float>(gea.getWindowWidth());
float y = (gea.getY()-gea.getYmin())/(gea.getYmax()-gea.getYmin())*static_cast<float>(gea.getWindowHeight());
if (gea.getMouseYOrientation()==osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS) y = static_cast<float>(gea.getWindowHeight() - 1) - y;
//OSG_NOTICE<<"MouseHandler(x="<<x<<", y="<<y<<")"<<std::endl;
return (this->*ma)(x, y, gea.getButton());
}
return false;
}
bool MouseHandler::_handleMousePush(float x, float y, int button) {
if(button == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) return _doMouseEvent(
x,
y,
&WindowManager::mousePushedLeft
);
else if(button == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) return _doMouseEvent(
x,
y,
&WindowManager::mousePushedRight
);
else if(button == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON) return _doMouseEvent(
x,
y,
&WindowManager::mousePushedMiddle
);
else if(button == osgGA::GUIEventAdapter::X1_MOUSE_BUTTON) return _doMouseEvent(
x,
y,
&WindowManager::mousePushedX1
);
else if(button == osgGA::GUIEventAdapter::X2_MOUSE_BUTTON) return _doMouseEvent(
x,
y,
&WindowManager::mousePushedX2
);
else return false;
}
bool MouseHandler::_handleMouseRelease(float x, float y, int button) {
if(button == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) return _doMouseEvent(
x,
y,
&WindowManager::mouseReleasedLeft
);
else if(button == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) return _doMouseEvent(
x,
y,
&WindowManager::mouseReleasedRight
);
else if(button == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON) return _doMouseEvent(
x,
y,
&WindowManager::mouseReleasedMiddle
);
else return false;
}
bool MouseHandler::_handleMouseDoubleClick(float /*x*/, float /*y*/, int /*button*/) {
return false;
}
bool MouseHandler::_handleMouseDrag(float x, float y, int /*button*/) {
return _doMouseEvent(x, y, &WindowManager::pointerDrag);
}
bool MouseHandler::_handleMouseMove(float x, float y, int /*button*/) {
return _doMouseEvent(x, y, &WindowManager::pointerMove);
}
bool MouseHandler::_handleMouseScroll(float x, float y, int) {
return _doMouseEvent(x, y, &WindowManager::mouseScroll);
}
MouseHandler::MouseAction MouseHandler::_isMouseEvent(
osgGA::GUIEventAdapter::EventType ev
) const {
if(ev == osgGA::GUIEventAdapter::PUSH) return
&MouseHandler::_handleMousePush
;
else if(ev == osgGA::GUIEventAdapter::RELEASE) return
&MouseHandler::_handleMouseRelease
;
else if(ev == osgGA::GUIEventAdapter::DOUBLECLICK) return
&MouseHandler::_handleMouseDoubleClick
;
else if(ev == osgGA::GUIEventAdapter::DRAG) return
&MouseHandler::_handleMouseDrag
;
else if(ev == osgGA::GUIEventAdapter::MOVE) return
&MouseHandler::_handleMouseMove
;
else if(ev == osgGA::GUIEventAdapter::SCROLL) return
&MouseHandler::_handleMouseScroll
;
else return 0;
}
bool MouseHandler::_doMouseEvent(float x, float y, MouseEvent me) {
bool handled = (_wm.get()->*me)(x, y);
// This is called LAST for things like drag, which needs to calculate a mouse difference.
_wm->setPointerXY(x, y);
return handled;
}
KeyboardHandler::KeyboardHandler(WindowManager* wm):
_wm(wm) {
}
bool KeyboardHandler::handle(
const osgGA::GUIEventAdapter& gea,
osgGA::GUIActionAdapter& /*gaa*/,
osg::Object* /*obj*/,
osg::NodeVisitor* /*nv*/
) {
osgGA::GUIEventAdapter::EventType ev = gea.getEventType();
if(ev!=osgGA::GUIEventAdapter::KEYDOWN && ev!=osgGA::GUIEventAdapter::KEYUP) return false;
int key = gea.getKey();
int keyMask = gea.getModKeyMask();
// -1 is the "key invalid" return code.
if(key == -1) return false;
if(ev == osgGA::GUIEventAdapter::KEYDOWN) return _wm->keyDown(key, keyMask);
// ev == osgGA::GUIEventAdapter::KEYUP
return _wm->keyUp(key, keyMask);
}
ResizeHandler::ResizeHandler(WindowManager* wm, osg::Camera* camera):
_wm (wm),
_camera (camera) {
}
bool ResizeHandler::handle(
const osgGA::GUIEventAdapter& gea,
osgGA::GUIActionAdapter& /*gaa*/,
osg::Object* /*obj*/,
osg::NodeVisitor* /*nv*/
) {
osgGA::GUIEventAdapter::EventType ev = gea.getEventType();
if(ev != osgGA::GUIEventAdapter::RESIZE) return false;
osg::Matrix::value_type w = gea.getWindowWidth();
osg::Matrix::value_type h = gea.getWindowHeight();
if(_camera.valid()) {
_camera->setProjectionMatrix(osg::Matrix::ortho2D(0.0f, w, 0.0f, h));
_wm->setSize(w, h);
}
_wm->setWindowSize(w, h);
_wm->resizeAllWindows();
return true;
}
CameraSwitchHandler::CameraSwitchHandler(WindowManager* wm, osg::Camera* camera):
_wm (wm),
_camera (camera) {
}
bool CameraSwitchHandler::handle(
const osgGA::GUIEventAdapter& gea,
osgGA::GUIActionAdapter& gaa,
osg::Object* /*obj*/,
osg::NodeVisitor* /*nv*/
) {
if(
gea.getEventType() != osgGA::GUIEventAdapter::KEYDOWN ||
gea.getKey() != osgGA::GUIEventAdapter::KEY_F12
) return false;
osgViewer::View* view = dynamic_cast<osgViewer::View*>(&gaa);
if(!view) return false;
osg::Node* oldNode = view->getSceneData();
osg::MatrixTransform* oldTrans = dynamic_cast<osg::MatrixTransform*>(oldNode);
if(!oldTrans) {
// Imagine this is the number of pixels...
double scale = 2000.0f;
double width = _wm->getWidth();
double height = _wm->getHeight();
_oldNode = oldNode;
osg::MatrixTransform* mt = new osg::MatrixTransform();
mt->setMatrix(
osg::Matrix::translate(width / 2.0f, 0.0f, 0.0f) *
osg::Matrix::scale(1.0f, 1.0f, scale) *
osg::Matrix::rotate(osg::DegreesToRadians(45.0f), 0.0f, 1.0f, 0.0f)
);
mt->addChild(_wm.get());
mt->getOrCreateStateSet()->setMode(
GL_LIGHTING,
osg::StateAttribute::PROTECTED | osg::StateAttribute::OFF
);
mt->getOrCreateStateSet()->setMode(
GL_SCISSOR_TEST,
osg::StateAttribute::OVERRIDE | osg::StateAttribute::OFF
);
osgGA::CameraManipulator* mm = view->getCameraManipulator();
// mm->setDistance(3000.0f);
// mm->setMinimumZoomScale(10.0f);
mm->setHomePosition(
// eye
osg::Vec3(width / 2.0f, height, 100.0f),
// center
osg::Vec3(0.0f, 0.0f, -(scale / 2.0f)),
// up
osg::Vec3(0.0f, 1.0f, 0.0f)
);
view->setSceneData(mt);
}
else view->setSceneData(_oldNode.get());
return true;
}
}