fixed: stay with old cmake qt link setup (policy CMP0020)
[opensg.git] / Source / System / Window / Utilities / OSGNavigationManager.cpp
blob0c9a4f1ef0ecd2e51e003b01bc89e0f49e87971f
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
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. *
18 * *
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. *
23 * *
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. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
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)
58 #endif
61 OSG_USING_NAMESPACE
63 /***************************************************************************\
64 * Description *
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 /*-------------------------------------------------------------------------*/
118 /* Constructors */
120 /*! Constructor
123 NavigationManager::NavigationManager(void) :
124 Inherited ( ),
125 _win (NULL ),
126 _navigator ( ),
127 _lastx (TypeTraits<Int16>::getMax()),
128 _lasty (TypeTraits<Int16>::getMax()),
129 _mousebuttons(0 )
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);
143 /*! Destructor
146 NavigationManager::~NavigationManager(void)
148 _win = NULL;
152 /*-------------------------------------------------------------------------*/
153 /* Get */
155 /*! get the window to be used for display
157 Window *NavigationManager::getWindow(void)
159 return _win;
162 /*! get the navigator
164 Navigator *NavigationManager::getNavigator(void)
166 return &_navigator;
169 /*-------------------------------------------------------------------------*/
170 /* Set */
172 /*! set the window to be used for display
174 void NavigationManager::setWindow(Window *win)
176 if(win == NULL)
178 FWARNING(("NavigationManager::setWindow: new window is NULL, "
179 "ignoring!\n"));
180 return;
183 _win = win;
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);
202 else
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);
215 _navigator.set (m );
218 /*-------------------------------------------------------------------------*/
219 /* Interaction handling */
221 /*! resize
223 void NavigationManager::resize(UInt16 width, UInt16 height)
225 _win->resize(width, height);
228 /*! motion
230 void NavigationManager::mouseMove(Int16 x, Int16 y)
232 if(_mousebuttons)
233 _navigator.moveTo(x,y);
235 _lastx = x;
236 _lasty = 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
242 upper left corner.
245 void NavigationManager::mouseButtonPress(UInt16 button, Int16 x, Int16 y)
247 switch (button)
249 case MouseLeft:
250 _navigator.buttonPress(Navigator::LEFT_MOUSE, x, y);
251 break;
253 case MouseMiddle:
254 _navigator.buttonPress(Navigator::MIDDLE_MOUSE, x, y);
255 break;
257 case MouseRight:
258 _navigator.buttonPress(Navigator::RIGHT_MOUSE, x, y);
259 break;
261 case MouseUp:
262 _navigator.buttonPress(Navigator::UP_MOUSE, x, y);
263 break;
265 case MouseDown:
266 _navigator.buttonPress(Navigator::DOWN_MOUSE, x, y);
267 break;
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
277 upper left corner.
279 void NavigationManager::mouseButtonRelease(UInt16 button, Int16 x, Int16 y)
281 switch (button)
283 case MouseLeft:
284 _navigator.buttonRelease(Navigator::LEFT_MOUSE, x, y);
285 break;
287 case MouseMiddle:
288 _navigator.buttonRelease(Navigator::MIDDLE_MOUSE, x, y);
289 break;
291 case MouseRight:
292 _navigator.buttonRelease(Navigator::RIGHT_MOUSE, x, y);
293 break;
295 case MouseUp:
296 _navigator.buttonRelease(Navigator::UP_MOUSE, x, y);
297 break;
299 case MouseDown:
300 _navigator.buttonRelease(Navigator::DOWN_MOUSE, x, y);
301 break;
304 updateMouseButtonRelease(button, x, y);
307 void NavigationManager::key(UChar8 key, Int16 x, Int16 y)
309 switch ( key )
311 case 'j':
312 _navigator.keyPress(Navigator::LEFT, x, y);
313 break;
315 case 'g':
316 _navigator.keyPress(Navigator::RIGHT, x, y);
317 break;
319 case 'u':
320 _navigator.keyPress(Navigator::LEFTROT, x, y);
321 break;
323 case 't':
324 _navigator.keyPress(Navigator::RIGHTROT, x, y);
325 break;
327 case 'y':
328 _navigator.keyPress(Navigator::FORWARDS, x, y);
329 break;
331 case 'h':
332 _navigator.keyPress(Navigator::BACKWARDS, x, y);
333 break;
337 /*-------------------------------------------------------------------------*/
338 /* Actions */
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
352 if(_win == NULL)
354 FWARNING(("NavigationManager::update: window not set, "
355 "ignoring!\n"));
356 return;
359 // need a viewport?
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);
371 _win->addPort(vp);
372 _navigator.setViewport(_win->getPort(0));
375 _navigator.updateCameraTransformation();
378 /*! Draw the next frame, update if needed.
380 void NavigationManager::redraw(void)
382 update();
385 /*-------------------------------------------------------------------------*/
386 /* Updates */
388 /*! called internally by mouseButtonRelease() to keep track of the
389 current status of _mousebuttons, _lastx and _lasty
391 void NavigationManager::updateMouseButtonPress(UInt16 button,
392 Int16 x,
393 Int16 y )
395 _mousebuttons |= 1 << button;
397 _lastx = x;
398 _lasty = y;
402 /*! called internally by mouseButtonRelease() to keep track of the
403 current status of _mousebuttons, _lastx and _lasty
405 void NavigationManager::updateMouseButtonRelease(UInt16 button,
406 Int16 x,
407 Int16 y )
409 _mousebuttons &= ~(1 << button);
411 _lastx = x;
412 _lasty = y;
415 /*------------------------------ access -----------------------------------*/
417 /*---------------------------- properties ---------------------------------*/
419 /*-------------------------- your_category---------------------------------*/
421 /*-------------------------- assignment -----------------------------------*/
423 /*-------------------------- comparison -----------------------------------*/
425 /*! less
428 bool NavigationManager::operator < (const NavigationManager &other) const
430 return this < &other;