root/OpenSceneGraph/trunk/src/osgWidget/ViewerEventHandlers.cpp @ 13376

Revision 13376, 7.4 kB (checked in by robert, 4 hours ago)

From Jannik Heller, "I noticed the rotation in the OrbitManipulator? depends on the framerate. To reproduce this issue, start the osganimate example, rotate the model with the left mouse button, then let go of the mouse button while still moving. You will notice that with V-Sync enabled, the model rotates slower.

The OrbitManipulator? calculates a scale to counteract the framerate dependency, but it turns out this scale wasn't used for the rotation yet."

  • Property svn:eol-style set to native
Line 
1// -*-c++-*- osgWidget - Code by: Jeremy Moles (cubicool) 2007-2008
2
3#include <osgWidget/ViewerEventHandlers>
4
5namespace osgWidget {
6
7MouseHandler::MouseHandler(WindowManager* wm):
8_wm(wm) {
9}
10
11bool MouseHandler::handle(
12    const osgGA::GUIEventAdapter& gea,
13    osgGA::GUIActionAdapter&      gaa,
14    osg::Object*                  obj,
15    osg::NodeVisitor*             nv
16) {
17    osgGA::GUIEventAdapter::EventType ev = gea.getEventType();
18    MouseAction                       ma = _isMouseEvent(ev);
19
20    if(ma) {
21        // If we're scrolling, we need to inform the WindowManager of that.
22        _wm->setScrollingMotion(gea.getScrollingMotion());
23
24        // osgWidget assumes origin is bottom left of window so make sure mouse coordinate are increaseing y upwards and are scaled to window size.
25        float x = (gea.getX()-gea.getXmin())/(gea.getXmax()-gea.getXmin())*static_cast<float>(gea.getWindowWidth());
26        float y = (gea.getY()-gea.getYmin())/(gea.getYmax()-gea.getYmin())*static_cast<float>(gea.getWindowHeight());
27        if (gea.getMouseYOrientation()==osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS) y = static_cast<float>(gea.getWindowHeight())-y;
28
29        //OSG_NOTICE<<"MouseHandler(x="<<x<<", y="<<y<<")"<<std::endl;
30
31        return (this->*ma)(x, y, gea.getButton());
32    }
33
34    return false;
35}
36
37bool MouseHandler::_handleMousePush(float x, float y, int button) {
38    if(button == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) return _doMouseEvent(
39        x,
40        y,
41        &WindowManager::mousePushedLeft
42    );
43
44    else if(button == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) return _doMouseEvent(
45        x,
46        y,
47        &WindowManager::mousePushedRight
48    );
49
50    else if(button == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON) return _doMouseEvent(
51        x,
52        y,
53        &WindowManager::mousePushedMiddle
54    );
55
56    else return false;
57}
58
59bool MouseHandler::_handleMouseRelease(float x, float y, int button) {
60    if(button == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) return _doMouseEvent(
61        x,
62        y,
63        &WindowManager::mouseReleasedLeft
64    );
65
66    else if(button == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) return _doMouseEvent(
67        x,
68        y,
69        &WindowManager::mouseReleasedRight
70    );
71
72    else if(button == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON) return _doMouseEvent(
73        x,
74        y,
75        &WindowManager::mouseReleasedMiddle
76    );
77
78    else return false;
79}
80
81bool MouseHandler::_handleMouseDoubleClick(float x, float y, int button) {
82    return false;
83}
84
85bool MouseHandler::_handleMouseDrag(float x, float y, int button) {
86    return _doMouseEvent(x, y, &WindowManager::pointerDrag);
87}
88
89bool MouseHandler::_handleMouseMove(float x, float y, int button) {
90    return _doMouseEvent(x, y, &WindowManager::pointerMove);
91}
92
93bool MouseHandler::_handleMouseScroll(float x, float y, int) {
94    return _doMouseEvent(x, y, &WindowManager::mouseScroll);
95}
96
97MouseHandler::MouseAction MouseHandler::_isMouseEvent(
98    osgGA::GUIEventAdapter::EventType ev
99) const {
100    if(ev == osgGA::GUIEventAdapter::PUSH) return
101        &MouseHandler::_handleMousePush
102    ;
103
104    else if(ev == osgGA::GUIEventAdapter::RELEASE) return
105        &MouseHandler::_handleMouseRelease
106    ;
107
108    else if(ev == osgGA::GUIEventAdapter::DOUBLECLICK) return
109        &MouseHandler::_handleMouseDoubleClick
110    ;
111
112    else if(ev == osgGA::GUIEventAdapter::DRAG) return
113        &MouseHandler::_handleMouseDrag
114    ;
115
116    else if(ev == osgGA::GUIEventAdapter::MOVE) return
117        &MouseHandler::_handleMouseMove
118    ;
119
120    else if(ev == osgGA::GUIEventAdapter::SCROLL) return
121        &MouseHandler::_handleMouseScroll
122    ;
123
124    else return 0;
125}
126
127bool MouseHandler::_doMouseEvent(float x, float y, MouseEvent me) {
128    bool handled = (_wm.get()->*me)(x, y);
129
130    // This is called LAST for things like drag, which needs to calculate a mouse difference.
131    _wm->setPointerXY(x, y);
132
133    return handled;
134}
135
136KeyboardHandler::KeyboardHandler(WindowManager* wm):
137_wm(wm) {
138}
139
140bool KeyboardHandler::handle(
141    const osgGA::GUIEventAdapter& gea,
142    osgGA::GUIActionAdapter&      gaa,
143    osg::Object*                  obj,
144    osg::NodeVisitor*             nv
145) {
146    osgGA::GUIEventAdapter::EventType ev = gea.getEventType();
147
148    if(
149        ev != osgGA::GUIEventAdapter::KEYDOWN &&
150        ev != osgGA::GUIEventAdapter::KEYUP
151    ) return false;
152
153    int key     = gea.getKey();
154    int keyMask = gea.getModKeyMask();
155
156    // -1 is the "key invalid" return code.
157    if(key == -1) return false;
158
159    if(ev == osgGA::GUIEventAdapter::KEYDOWN) return _wm->keyDown(key, keyMask);
160
161    else if(ev == osgGA::GUIEventAdapter::KEYUP) return _wm->keyUp(key, keyMask);
162
163    return false;
164}
165
166ResizeHandler::ResizeHandler(WindowManager* wm, osg::Camera* camera):
167_wm     (wm),
168_camera (camera) {
169}
170
171bool ResizeHandler::handle(
172    const osgGA::GUIEventAdapter& gea,
173    osgGA::GUIActionAdapter&      gaa,
174    osg::Object*                  obj,
175    osg::NodeVisitor*             nv
176) {
177    osgGA::GUIEventAdapter::EventType ev = gea.getEventType();
178
179    if(ev != osgGA::GUIEventAdapter::RESIZE) return false;
180
181    osg::Matrix::value_type w = gea.getWindowWidth();
182    osg::Matrix::value_type h = gea.getWindowHeight();
183
184    if(_camera.valid()) {
185        _camera->setProjectionMatrix(osg::Matrix::ortho2D(0.0f, w, 0.0f, h));
186
187        _wm->setSize(w, h);
188    }
189
190    _wm->setWindowSize(w, h);
191    _wm->resizeAllWindows();
192
193    return true;
194}
195
196CameraSwitchHandler::CameraSwitchHandler(WindowManager* wm, osg::Camera* camera):
197_wm     (wm),
198_camera (camera) {
199}
200
201bool CameraSwitchHandler::handle(
202    const osgGA::GUIEventAdapter& gea,
203    osgGA::GUIActionAdapter&      gaa,
204    osg::Object*                  obj,
205    osg::NodeVisitor*             nv
206) {
207    if(
208        gea.getEventType() != osgGA::GUIEventAdapter::KEYDOWN ||
209        gea.getKey()       != osgGA::GUIEventAdapter::KEY_F12
210    ) return false;
211
212    osgViewer::View* view = dynamic_cast<osgViewer::View*>(&gaa);
213
214    if(!view) return false;
215
216    osg::Node* oldNode = view->getSceneData();
217
218    osg::MatrixTransform* oldTrans = dynamic_cast<osg::MatrixTransform*>(oldNode);
219
220    if(!oldTrans) {
221        // Imagine this is the number of pixels...
222        double scale  = 2000.0f;
223        double width  = _wm->getWidth();
224        double height = _wm->getHeight();
225
226        _oldNode = oldNode;
227
228        osg::MatrixTransform* mt = new osg::MatrixTransform();
229
230        mt->setMatrix(
231            osg::Matrix::translate(width / 2.0f, 0.0f, 0.0f) *
232            osg::Matrix::scale(1.0f, 1.0f, scale) *
233            osg::Matrix::rotate(osg::DegreesToRadians(45.0f), 0.0f, 1.0f, 0.0f)
234        );
235
236        mt->addChild(_wm.get());
237        mt->getOrCreateStateSet()->setMode(
238            GL_LIGHTING,
239            osg::StateAttribute::PROTECTED | osg::StateAttribute::OFF
240        );
241        mt->getOrCreateStateSet()->setMode(
242            GL_SCISSOR_TEST,
243            osg::StateAttribute::OVERRIDE | osg::StateAttribute::OFF
244        );
245
246        osgGA::CameraManipulator* mm = view->getCameraManipulator();
247
248        // mm->setDistance(3000.0f);
249        // mm->setMinimumZoomScale(10.0f);
250        mm->setHomePosition(
251            // eye
252            osg::Vec3(width / 2.0f, height, 100.0f),
253            // center
254            osg::Vec3(0.0f, 0.0f, -(scale / 2.0f)),
255            // up
256            osg::Vec3(0.0f, 1.0f, 0.0f)
257        );
258
259        view->setSceneData(mt);
260    }
261
262    else view->setSceneData(_oldNode.get());
263
264    return true;
265}
266
267}
Note: See TracBrowser for help on using the browser.