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 #include <boost/shared_ptr.hpp>
29 #include <osgViewer/GraphicsWindow>
30 
31 #include "WGraphicsEngineMode.h"
32 
33 /**
34  * Class managing a single graphics context and OSG GraphicsWindow.
35  * \ingroup ge
36  */
38 {
39 public:
40  /**
41  * Default constructor.
42  *
43  * \param wdata the WindowData instance for the widget to use as render widget. NULL on Mac!
44  * \param x X coordinate of widget where to create the context.
45  * \param y Y coordinate of widget where to create the context.
46  * \param width Width of the widget.
47  * \param height Height of the Widget.
48  * \exception WGEInitFailed thrown if initialization of graphics context or graphics window has failed.
49  */
50  WGEGraphicsWindow( osg::ref_ptr<osg::Referenced> wdata, int x, int y, int width, int height );
51 
52  /**
53  * Destructor.
54  */
55  virtual ~WGEGraphicsWindow();
56 
57  /**
58  * Getter for m_GraphicsWindow.
59  *
60  * \return the OSG GraphicsWindow instance.
61  */
62  osg::ref_ptr<osgViewer::GraphicsWindow> getGraphicsWindow();
63 
64  /**
65  * Event types for the keyEvent() handler.
66  */
67  enum KeyEvents
68  {
69  KEYPRESS, KEYRELEASE
70  };
71 
72  /**
73  * Mouse event types for the mouseEvent() handler.
74  */
76  {
77  MOUSEPRESS, MOUSERELEASE, MOUSEDOUBLECLICK, MOUSEMOVE, MOUSESCROLL
78  };
79 
80  /**
81  * Updates size information.
82  *
83  * \param width new width.
84  * \param height new height.
85  */
86  virtual void resize( int width, int height );
87 
88  /**
89  * Initiates a close event for this viewer. It destroys the graphics context and invalidates the viewer.
90  * This should be called whenever a QT Widget closes to also free its OSG Viewer resources.
91  */
92  virtual void close();
93 
94  /**
95  * Handles key events (if forwarded to this Viewer instance).
96  *
97  * \param key the key code.
98  * \param eventType the type of event.
99  */
100  virtual void keyEvent( KeyEvents eventType, int key );
101 
102  /**
103  * Handles mouse events forwarded from widget.
104  *
105  * \param eventType the event type.
106  * \param x x coordinate of event.
107  * \param y y coordinate of event.
108  * \param button mouse button.
109  */
110  virtual void mouseEvent( MouseEvents eventType, int x, int y, int button );
111 
112  /**
113  * Check if the windows is open.
114  *
115  * \return false if the window is not open anymore.
116  */
117  virtual bool isClosed() const;
118 
119  /**
120  * Set closed state.
121  *
122  * \param closed true if widget should be marked as closed.
123  *
124  */
125  virtual void setClosed( bool closed = true );
126 protected:
127  /**
128  * OpenSceneGraph render window.
129  */
130  osg::ref_ptr<osgViewer::GraphicsWindow> m_GraphicsWindow;
131 
132 #ifdef WGEMODE_MULTITHREADED
133  /**
134  * Creates a new OpenGL context in the calling thread. Needs to be called before any other OpenGL operation.
135  *
136  * \param x X coordinate of widget where to create the context.
137  * \param y Y coordinate of widget where to create the context.
138  * \param width Width of the widget.
139  * \param height Height of the Widget.
140  */
141  void createContext( int x, int y, int width, int height );
142 
143  /**
144  * OpenSceneGraph render context.
145  */
146  osg::ref_ptr<osg::GraphicsContext> m_GraphicsContext;
147 
148  /**
149  * Widget window data.
150  */
151  osg::ref_ptr<osg::Referenced> m_WindowData;
152 #endif
153 private:
154  /**
155  * Mark the window opened or closed.
156  */
157  bool m_closed;
158 };
159 
160 #endif // WGEGRAPHICSWINDOW_H
161 
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.