1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
46 #include "OSGConfig.h"
47 #include "OSGBaseFunctions.h"
48 #include "OSGImageFileType.h"
49 #include "OSGSolidBackground.h"
50 #include "OSGViewport.h"
51 #include "OSGDrawable.h"
52 #include "OSGTransform.h"
54 #include "OSGNavigationManager.h"
56 #if defined(OSG_WIN32_ICL) && !defined(OSG_CHECK_FIELDSETARG)
57 #pragma warning (disable : 383)
63 /***************************************************************************\
65 \***************************************************************************/
67 /*! \class OSG::NavigationManager
68 \ingroup GrpSystemWindow
70 The NavigationManager manages a navigator that is needed to do a
71 basic navigation within a subscene of a scenegraph (specified by the
72 beacon). It also takes care of creating the necessary additional OpenSG
73 objects, like a viewport for the window (if not already present).
76 /*! \var NavigationManager::MouseLeft
77 The constant for the left mouse button.
80 /*! \var NavigationManager::MouseMiddle
81 The constant for the middle mouse button.
84 /*! \var NavigationManager::MouseRight
85 The constant for the right mouse button.
88 /*! \var NavigationManager::MouseUp
89 The constant for the mouse wheel up (away from the hand) motion.
92 /*! \var NavigationManager::MouseDown
93 The constant for the mouse wheel down (towards the hand) motion.
96 /*! \var NavigationManager::_win
97 The managed window. Has to be set by the user.
100 /*! \var NavigationManager::_navigator
101 The navigator for viewer manipulation.
104 /*! \var NavigationManager::_lastx
105 The x position of the last mouse event, needed by the navigator.
108 /*! \var NavigationManager::_lasty
109 The y position of the last mouse event, needed by the navigator.
112 /*! \var NavigationManager::_mousebuttons
113 Storage for the actively pressed mouse buttons. Needed to do the right
114 thing for mouse motions, as mouseMove() doesn't get them.
117 /*-------------------------------------------------------------------------*/
123 NavigationManager::NavigationManager(void) :
127 _lastx (TypeTraits
<Int16
>::getMax()),
128 _lasty (TypeTraits
<Int16
>::getMax()),
131 _navigator
.setMode(Navigator::TRACKBALL
);
135 /*! pseudo constructor. Standard interface for OpenSG object creation.
138 NavigationManager::ObjTransitPtr
NavigationManager::create(void)
140 return ObjTransitPtr(new NavigationManager
);
146 NavigationManager::~NavigationManager(void)
152 /*-------------------------------------------------------------------------*/
155 /*! get the window to be used for display
157 Window
*NavigationManager::getWindow(void)
162 /*! get the navigator
164 Navigator
*NavigationManager::getNavigator(void)
169 /*-------------------------------------------------------------------------*/
172 /*! set the window to be used for display
174 void NavigationManager::setWindow(Window
*win
)
178 FWARNING(("NavigationManager::setWindow: new window is NULL, "
185 if(_win
->getMFPort()->size() > 0 && _win
->getPort(0) != NULL
)
186 _navigator
.setViewport(_win
->getPort(0));
189 /*! Sets the navigator's camera transformation
190 If newBeacon is null, a dummy beacon will be added instead.
192 void NavigationManager::setBeacon(Node
* const newBeacon
)
194 if(newBeacon
== NULL
)
196 setNavigationMode(Navigator::NONE
);
198 TransformNodeRefPtr xnode
= TransformNodeRefPtr::create();
200 _navigator
.setCameraTransformation(xnode
);
204 _navigator
.setCameraTransformation(newBeacon
);
208 /*! Sets the navigation mode
210 void NavigationManager::setNavigationMode (Navigator::Mode new_mode
)
212 Matrix m
= _navigator
.getMatrix();
214 _navigator
.setMode(new_mode
);
218 /*-------------------------------------------------------------------------*/
219 /* Interaction handling */
223 void NavigationManager::resize(UInt16 width
, UInt16 height
)
225 _win
->resize(width
, height
);
230 void NavigationManager::mouseMove(Int16 x
, Int16 y
)
233 _navigator
.moveTo(x
,y
);
239 /*! call when a mouse button is pressed. button is the number of the pressed
240 button, starting at 0, ordered from left to right. A wheel should be
241 mapped to buttons 3 and 4. The position is in pixel, starting at the
245 void NavigationManager::mouseButtonPress(UInt16 button
, Int16 x
, Int16 y
)
250 _navigator
.buttonPress(Navigator::LEFT_MOUSE
, x
, y
);
254 _navigator
.buttonPress(Navigator::MIDDLE_MOUSE
, x
, y
);
258 _navigator
.buttonPress(Navigator::RIGHT_MOUSE
, x
, y
);
262 _navigator
.buttonPress(Navigator::UP_MOUSE
, x
, y
);
266 _navigator
.buttonPress(Navigator::DOWN_MOUSE
, x
, y
);
270 updateMouseButtonPress(button
, x
, y
);
274 /*! call when a mouse button is released. button is the number of the pressed
275 button, starting at 0, ordered from left to right. A wheel should be
276 mapped to buttons 3 and 4. The position is in pixel, starting at the
279 void NavigationManager::mouseButtonRelease(UInt16 button
, Int16 x
, Int16 y
)
284 _navigator
.buttonRelease(Navigator::LEFT_MOUSE
, x
, y
);
288 _navigator
.buttonRelease(Navigator::MIDDLE_MOUSE
, x
, y
);
292 _navigator
.buttonRelease(Navigator::RIGHT_MOUSE
, x
, y
);
296 _navigator
.buttonRelease(Navigator::UP_MOUSE
, x
, y
);
300 _navigator
.buttonRelease(Navigator::DOWN_MOUSE
, x
, y
);
304 updateMouseButtonRelease(button
, x
, y
);
307 void NavigationManager::key(UChar8 key
, Int16 x
, Int16 y
)
312 _navigator
.keyPress(Navigator::LEFT
, x
, y
);
316 _navigator
.keyPress(Navigator::RIGHT
, x
, y
);
320 _navigator
.keyPress(Navigator::LEFTROT
, x
, y
);
324 _navigator
.keyPress(Navigator::RIGHTROT
, x
, y
);
328 _navigator
.keyPress(Navigator::FORWARDS
, x
, y
);
332 _navigator
.keyPress(Navigator::BACKWARDS
, x
, y
);
337 /*-------------------------------------------------------------------------*/
340 /*! Draw the next frame, update if needed.
342 void NavigationManager::idle(void)
344 _navigator
.idle(_mousebuttons
, _lastx
, _lasty
);
347 /*! Update data needed for rendering.
349 void NavigationManager::update(void)
351 // Check necessary stuff
354 FWARNING(("NavigationManager::update: window not set, "
360 if(_win
->getMFPort()->size() == 0)
362 SolidBackgroundUnrecPtr bg
= SolidBackground::create();
364 bg
->setColor(Color3f(0.2f
, 0.2f
, 0.2f
));
366 ViewportUnrecPtr vp
= Viewport::create();
368 vp
->setSize (0,0, 1,1);
369 vp
->setBackground(bg
);
372 _navigator
.setViewport(_win
->getPort(0));
375 _navigator
.updateCameraTransformation();
378 /*! Draw the next frame, update if needed.
380 void NavigationManager::redraw(void)
385 /*-------------------------------------------------------------------------*/
388 /*! called internally by mouseButtonRelease() to keep track of the
389 current status of _mousebuttons, _lastx and _lasty
391 void NavigationManager::updateMouseButtonPress(UInt16 button
,
395 _mousebuttons
|= 1 << button
;
402 /*! called internally by mouseButtonRelease() to keep track of the
403 current status of _mousebuttons, _lastx and _lasty
405 void NavigationManager::updateMouseButtonRelease(UInt16 button
,
409 _mousebuttons
&= ~(1 << button
);
415 /*------------------------------ access -----------------------------------*/
417 /*---------------------------- properties ---------------------------------*/
419 /*-------------------------- your_category---------------------------------*/
421 /*-------------------------- assignment -----------------------------------*/
423 /*-------------------------- comparison -----------------------------------*/
428 bool NavigationManager::operator < (const NavigationManager
&other
) const
430 return this < &other
;