OpenWalnut  1.3.1
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 protected:
113  /**
114  * OpenSceneGraph render window.
115  */
116  osg::ref_ptr<osgViewer::GraphicsWindow> m_GraphicsWindow;
117 
118 #ifdef WGEMODE_MULTITHREADED
119  /**
120  * Creates a new OpenGL context in the calling thread. Needs to be called before any other OpenGL operation.
121  *
122  * \param x X coordinate of widget where to create the context.
123  * \param y Y coordinate of widget where to create the context.
124  * \param width Width of the widget.
125  * \param height Height of the Widget.
126  */
127  void createContext( int x, int y, int width, int height );
128 
129  /**
130  * OpenSceneGraph render context.
131  */
132  osg::ref_ptr<osg::GraphicsContext> m_GraphicsContext;
133 
134  /**
135  * Widget window data.
136  */
137  osg::ref_ptr<osg::Referenced> m_WindowData;
138 #endif
139 private:
140 };
141 
142 #endif // WGEGRAPHICSWINDOW_H
143