OpenWalnut  1.4.0
WGEGraphicsWindow.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WGEGRAPHICSWINDOW_H
26 #define WGEGRAPHICSWINDOW_H
27 
28 #ifndef Q_MOC_RUN
29 #include <boost/shared_ptr.hpp>
30 #endif
31 #include <osgViewer/GraphicsWindow>
32 
33 #include "WGraphicsEngineMode.h"
34 
35 /**
36  * Class managing a single graphics context and OSG GraphicsWindow.
37  * \ingroup ge
38  */
40 {
41 public:
42  /**
43  * Default constructor.
44  *
45  * \param wdata the WindowData instance for the widget to use as render widget. NULL on Mac!
46  * \param x X coordinate of widget where to create the context.
47  * \param y Y coordinate of widget where to create the context.
48  * \param width Width of the widget.
49  * \param height Height of the Widget.
50  * \exception WGEInitFailed thrown if initialization of graphics context or graphics window has failed.
51  */
52  WGEGraphicsWindow( osg::ref_ptr<osg::Referenced> wdata, int x, int y, int width, int height );
53 
54  /**
55  * Destructor.
56  */
57  virtual ~WGEGraphicsWindow();
58 
59  /**
60  * Getter for m_GraphicsWindow.
61  *
62  * \return the OSG GraphicsWindow instance.
63  */
64  osg::ref_ptr<osgViewer::GraphicsWindow> getGraphicsWindow();
65 
66  /**
67  * Event types for the keyEvent() handler.
68  */
69  enum KeyEvents
70  {
71  KEYPRESS, KEYRELEASE
72  };
73 
74  /**
75  * Mouse event types for the mouseEvent() handler.
76  */
78  {
79  MOUSEPRESS, MOUSERELEASE, MOUSEDOUBLECLICK, MOUSEMOVE, MOUSESCROLL
80  };
81 
82  /**
83  * Updates size information.
84  *
85  * \param width new width.
86  * \param height new height.
87  */
88  virtual void resize( int width, int height );
89 
90  /**
91  * Initiates a close event for this viewer. It destroys the graphics context and invalidates the viewer.
92  * This should be called whenever a QT Widget closes to also free its OSG Viewer resources.
93  */
94  virtual void close();
95 
96  /**
97  * Handles key events (if forwarded to this Viewer instance).
98  *
99  * \param key the key code.
100  * \param eventType the type of event.
101  */
102  virtual void keyEvent( KeyEvents eventType, int key );
103 
104  /**
105  * Handles mouse events forwarded from widget.
106  *
107  * \param eventType the event type.
108  * \param x x coordinate of event.
109  * \param y y coordinate of event.
110  * \param button mouse button.
111  */
112  virtual void mouseEvent( MouseEvents eventType, int x, int y, int button );
113 
114  /**
115  * Check if the windows is open.
116  *
117  * \return false if the window is not open anymore.
118  */
119  virtual bool isClosed() const;
120 
121  /**
122  * Set closed state.
123  *
124  * \param closed true if widget should be marked as closed.
125  *
126  */
127  virtual void setClosed( bool closed = true );
128 protected:
129  /**
130  * OpenSceneGraph render window.
131  */
132  osg::ref_ptr<osgViewer::GraphicsWindow> m_GraphicsWindow;
133 
134 #ifdef WGEMODE_MULTITHREADED
135  /**
136  * Creates a new OpenGL context in the calling thread. Needs to be called before any other OpenGL operation.
137  *
138  * \param x X coordinate of widget where to create the context.
139  * \param y Y coordinate of widget where to create the context.
140  * \param width Width of the widget.
141  * \param height Height of the Widget.
142  */
143  void createContext( int x, int y, int width, int height );
144 
145  /**
146  * OpenSceneGraph render context.
147  */
148  osg::ref_ptr<osg::GraphicsContext> m_GraphicsContext;
149 
150  /**
151  * Widget window data.
152  */
153  osg::ref_ptr<osg::Referenced> m_WindowData;
154 #endif
155 private:
156  /**
157  * Mark the window opened or closed.
158  */
159  bool m_closed;
160 };
161 
162 #endif // WGEGRAPHICSWINDOW_H
163 
osg::ref_ptr< osgViewer::GraphicsWindow > m_GraphicsWindow
OpenSceneGraph render window.
KeyEvents
Event types for the keyEvent() handler.
virtual bool isClosed() const
Check if the windows is open.
virtual ~WGEGraphicsWindow()
Destructor.
virtual void close()
Initiates a close event for this viewer.
osg::ref_ptr< osg::GraphicsContext > m_GraphicsContext
OpenSceneGraph render context.
virtual void mouseEvent(MouseEvents eventType, int x, int y, int button)
Handles mouse events forwarded from widget.
MouseEvents
Mouse event types for the mouseEvent() handler.
WGEGraphicsWindow(osg::ref_ptr< osg::Referenced > wdata, int x, int y, int width, int height)
Default constructor.
Class managing a single graphics context and OSG GraphicsWindow.
virtual void keyEvent(KeyEvents eventType, int key)
Handles key events (if forwarded to this Viewer instance).
virtual void setClosed(bool closed=true)
Set closed state.
osg::ref_ptr< osg::Referenced > m_WindowData
Widget window data.
virtual void resize(int width, int height)
Updates size information.
bool m_closed
Mark the window opened or closed.
osg::ref_ptr< osgViewer::GraphicsWindow > getGraphicsWindow()
Getter for m_GraphicsWindow.
void createContext(int x, int y, int width, int height)
Creates a new OpenGL context in the calling thread.