1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
#include <osgDB/ReadFile> |
13 |
#include <osgUtil/Optimizer> |
14 |
#include <osgViewer/Viewer> |
15 |
#include <osg/CoordinateSystemNode> |
16 |
#include <osgGA/GUIEventAdapter> |
17 |
|
18 |
#include <CEGUISystem.h> |
19 |
#include <RendererModules/OpenGLGUIRenderer/openglrenderer.h> |
20 |
#include <CEGUIScriptModule.h> |
21 |
#include <CEGUIFontManager.h> |
22 |
#include <CEGUISchemeManager.h> |
23 |
#include <CEGUIWindowManager.h> |
24 |
#include <CEGUIExceptions.h> |
25 |
|
26 |
#include <iostream> |
27 |
|
28 |
class CEGUIDrawable : public osg::Drawable |
29 |
{ |
30 |
public: |
31 |
|
32 |
CEGUIDrawable(); |
33 |
|
34 |
|
35 |
CEGUIDrawable(const CEGUIDrawable& drawable,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): |
36 |
Drawable(drawable,copyop) {} |
37 |
|
38 |
META_Object(osg,CEGUIDrawable); |
39 |
|
40 |
void loadScheme(const std::string& scheme); |
41 |
void loadFont(const std::string& font); |
42 |
void loadLayout(const std::string& layout); |
43 |
|
44 |
void drawImplementation(osg::RenderInfo& renderInfo) const; |
45 |
|
46 |
protected: |
47 |
|
48 |
virtual ~CEGUIDrawable(); |
49 |
|
50 |
unsigned int _activeContextID; |
51 |
|
52 |
}; |
53 |
|
54 |
|
55 |
struct CEGUIEventCallback : public osgGA::GUIEventHandler |
56 |
{ |
57 |
CEGUIEventCallback() {} |
58 |
|
59 |
|
60 |
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object* obj, osg::NodeVisitor* nv) |
61 |
{ |
62 |
osgGA::EventVisitor* ev = dynamic_cast<osgGA::EventVisitor*>(nv); |
63 |
CEGUIDrawable* cd = dynamic_cast<CEGUIDrawable*>(obj); |
64 |
|
65 |
if (!ev || !cd) return false; |
66 |
|
67 |
switch(ea.getEventType()) |
68 |
{ |
69 |
case(osgGA::GUIEventAdapter::DRAG): |
70 |
case(osgGA::GUIEventAdapter::MOVE): |
71 |
CEGUI::System::getSingleton().injectMousePosition(ea.getX(),ea.getY()); |
72 |
return true; |
73 |
case(osgGA::GUIEventAdapter::PUSH): |
74 |
{ |
75 |
CEGUI::System::getSingleton().injectMousePosition(ea.getX(), ea.getY()); |
76 |
|
77 |
if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) |
78 |
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton); |
79 |
|
80 |
else if (ea.getButton() == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON) |
81 |
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton); |
82 |
|
83 |
else if (ea.getButton() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) |
84 |
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton); |
85 |
|
86 |
return true; |
87 |
} |
88 |
case(osgGA::GUIEventAdapter::RELEASE): |
89 |
{ |
90 |
CEGUI::System::getSingleton().injectMousePosition(ea.getX(), ea.getY()); |
91 |
|
92 |
if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) |
93 |
CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::LeftButton); |
94 |
|
95 |
else if (ea.getButton() == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON) |
96 |
CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::MiddleButton); |
97 |
|
98 |
else if (ea.getButton() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) |
99 |
CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::RightButton); |
100 |
|
101 |
return true; |
102 |
} |
103 |
case(osgGA::GUIEventAdapter::DOUBLECLICK): |
104 |
{ |
105 |
|
106 |
CEGUI::System::getSingleton().injectMousePosition(ea.getX(), ea.getY()); |
107 |
|
108 |
if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) |
109 |
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton); |
110 |
|
111 |
else if (ea.getButton() == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON) |
112 |
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton); |
113 |
|
114 |
else if (ea.getButton() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) |
115 |
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton); |
116 |
|
117 |
return true; |
118 |
} |
119 |
case(osgGA::GUIEventAdapter::KEYDOWN): |
120 |
CEGUI::System::getSingleton().injectKeyDown( static_cast<CEGUI::uint>(ea.getKey()) ); |
121 |
CEGUI::System::getSingleton().injectChar( static_cast<CEGUI::utf32>( ea.getKey() ) ); |
122 |
return true; |
123 |
case(osgGA::GUIEventAdapter::KEYUP): |
124 |
CEGUI::System::getSingleton().injectKeyUp( static_cast<CEGUI::uint>(ea.getKey()) ); |
125 |
return true; |
126 |
default: |
127 |
break; |
128 |
} |
129 |
|
130 |
return false; |
131 |
} |
132 |
}; |
133 |
|
134 |
CEGUIDrawable::CEGUIDrawable() |
135 |
{ |
136 |
setSupportsDisplayList(false); |
137 |
|
138 |
setEventCallback(new CEGUIEventCallback()); |
139 |
|
140 |
new CEGUI::System( new CEGUI::OpenGLRenderer(0) ); |
141 |
|
142 |
_activeContextID = 0; |
143 |
} |
144 |
|
145 |
CEGUIDrawable::~CEGUIDrawable() |
146 |
{ |
147 |
|
148 |
} |
149 |
|
150 |
void CEGUIDrawable::loadScheme(const std::string& scheme) |
151 |
{ |
152 |
try |
153 |
{ |
154 |
CEGUI::SchemeManager::getSingleton().loadScheme(scheme.c_str()); |
155 |
} |
156 |
catch (CEGUI::Exception e) |
157 |
{ |
158 |
std::cout<<"CEGUIDrawable::loadScheme Error: "<<e.getMessage()<<std::endl; |
159 |
} |
160 |
} |
161 |
|
162 |
void CEGUIDrawable::loadFont(const std::string& font) |
163 |
{ |
164 |
try |
165 |
{ |
166 |
CEGUI::FontManager::getSingleton().createFont(font.c_str()); |
167 |
} |
168 |
catch (CEGUI::Exception e) |
169 |
{ |
170 |
std::cout<<"CEGUIDrawable::loadFont Error: "<<e.getMessage()<<std::endl; |
171 |
} |
172 |
} |
173 |
|
174 |
void CEGUIDrawable::loadLayout(const std::string& layout) |
175 |
{ |
176 |
try |
177 |
{ |
178 |
CEGUI::Window* myRoot = CEGUI::WindowManager::getSingleton().loadWindowLayout(layout.c_str()); |
179 |
CEGUI::System::getSingleton().setGUISheet(myRoot); |
180 |
} |
181 |
catch (CEGUI::Exception e) |
182 |
{ |
183 |
std::cout<<"CEGUIDrawable::loadLayout error: "<<e.getMessage()<<std::endl; |
184 |
} |
185 |
|
186 |
} |
187 |
|
188 |
void CEGUIDrawable::drawImplementation(osg::RenderInfo& renderInfo) const |
189 |
{ |
190 |
osg::State& state = renderInfo.getState(); |
191 |
|
192 |
if (state.getContextID()!=_activeContextID) return; |
193 |
|
194 |
glPushAttrib(GL_ALL_ATTRIB_BITS); |
195 |
|
196 |
state.disableAllVertexArrays(); |
197 |
|
198 |
CEGUI::System::getSingleton().renderGUI(); |
199 |
|
200 |
glPopAttrib(); |
201 |
|
202 |
state.checkGLErrors("CEGUIDrawable::drawImplementation"); |
203 |
} |
204 |
|
205 |
int main( int argc, char **argv ) |
206 |
{ |
207 |
|
208 |
|
209 |
osg::ArgumentParser arguments(&argc,argv); |
210 |
|
211 |
|
212 |
|
213 |
osgViewer::Viewer viewer; |
214 |
|
215 |
|
216 |
osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
217 |
osg::ref_ptr<CEGUIDrawable> cd = new CEGUIDrawable(); |
218 |
geode->addDrawable(cd.get()); |
219 |
|
220 |
std::string scheme; |
221 |
while(arguments.read("--scheme",scheme)) |
222 |
{ |
223 |
cd->loadScheme(scheme); |
224 |
} |
225 |
|
226 |
std::string font; |
227 |
while(arguments.read("--font",font)) |
228 |
{ |
229 |
cd->loadFont(font); |
230 |
} |
231 |
|
232 |
std::string layout; |
233 |
while(arguments.read("--layout",layout)) |
234 |
{ |
235 |
cd->loadLayout(layout); |
236 |
} |
237 |
|
238 |
osg::Timer_t start_tick = osg::Timer::instance()->tick(); |
239 |
|
240 |
|
241 |
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
242 |
|
243 |
|
244 |
if (!loadedModel) |
245 |
{ |
246 |
std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; |
247 |
return 1; |
248 |
} |
249 |
|
250 |
osg::ref_ptr<osg::Group> group = new osg::Group; |
251 |
group->addChild(loadedModel.get()); |
252 |
|
253 |
group->addChild(geode.get()); |
254 |
|
255 |
|
256 |
|
257 |
arguments.reportRemainingOptionsAsUnrecognized(); |
258 |
|
259 |
|
260 |
if (arguments.errors()) |
261 |
{ |
262 |
arguments.writeErrorMessages(std::cout); |
263 |
} |
264 |
|
265 |
osg::Timer_t end_tick = osg::Timer::instance()->tick(); |
266 |
|
267 |
std::cout << "Time to load = "<<osg::Timer::instance()->delta_s(start_tick,end_tick)<<std::endl; |
268 |
|
269 |
|
270 |
|
271 |
osgUtil::Optimizer optimizer; |
272 |
optimizer.optimize(loadedModel.get()); |
273 |
|
274 |
|
275 |
viewer.setSceneData(group.get()); |
276 |
|
277 |
|
278 |
return viewer.run(); |
279 |
} |