1
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
3
* This application is open source and may be redistributed and/or modified
4
* freely and without restriction, both in commericial and non commericial applications,
5
* as long as this copyright notice is maintained.
7
* This application is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
#include <osgDB/ReadFile>
13
#include <osgUtil/Optimizer>
14
#include <osgViewer/Viewer>
15
#include <osg/CoordinateSystemNode>
16
#include <osgGA/GUIEventAdapter>
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>
28
class CEGUIDrawable : public osg::Drawable
34
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
35
CEGUIDrawable(const CEGUIDrawable& drawable,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
36
Drawable(drawable,copyop) {}
38
META_Object(osg,CEGUIDrawable);
40
void loadScheme(const std::string& scheme);
41
void loadFont(const std::string& font);
42
void loadLayout(const std::string& layout);
44
void drawImplementation(osg::RenderInfo& renderInfo) const;
48
virtual ~CEGUIDrawable();
50
unsigned int _activeContextID;
55
struct CEGUIEventCallback : public osgGA::GUIEventHandler
57
CEGUIEventCallback() {}
59
/** do customized Event code. */
60
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object* obj, osg::NodeVisitor* nv)
62
osgGA::EventVisitor* ev = dynamic_cast<osgGA::EventVisitor*>(nv);
63
CEGUIDrawable* cd = dynamic_cast<CEGUIDrawable*>(obj);
65
if (!ev || !cd) return false;
67
switch(ea.getEventType())
69
case(osgGA::GUIEventAdapter::DRAG):
70
case(osgGA::GUIEventAdapter::MOVE):
71
CEGUI::System::getSingleton().injectMousePosition(ea.getX(),ea.getY());
73
case(osgGA::GUIEventAdapter::PUSH):
75
CEGUI::System::getSingleton().injectMousePosition(ea.getX(), ea.getY());
77
if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) // left
78
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton);
80
else if (ea.getButton() == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON) // middle
81
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton);
83
else if (ea.getButton() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) // right
84
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton);
88
case(osgGA::GUIEventAdapter::RELEASE):
90
CEGUI::System::getSingleton().injectMousePosition(ea.getX(), ea.getY());
92
if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) // left
93
CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::LeftButton);
95
else if (ea.getButton() == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON) // middle
96
CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::MiddleButton);
98
else if (ea.getButton() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) // right
99
CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::RightButton);
103
case(osgGA::GUIEventAdapter::DOUBLECLICK):
105
// do we need to do something special here to handle double click??? Will just assume button down for now.
106
CEGUI::System::getSingleton().injectMousePosition(ea.getX(), ea.getY());
108
if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) // left
109
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton);
111
else if (ea.getButton() == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON) // middle
112
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton);
114
else if (ea.getButton() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) // right
115
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton);
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() ) );
123
case(osgGA::GUIEventAdapter::KEYUP):
124
CEGUI::System::getSingleton().injectKeyUp( static_cast<CEGUI::uint>(ea.getKey()) );
134
CEGUIDrawable::CEGUIDrawable()
136
setSupportsDisplayList(false);
138
setEventCallback(new CEGUIEventCallback());
140
new CEGUI::System( new CEGUI::OpenGLRenderer(0) );
142
_activeContextID = 0;
145
CEGUIDrawable::~CEGUIDrawable()
150
void CEGUIDrawable::loadScheme(const std::string& scheme)
154
CEGUI::SchemeManager::getSingleton().loadScheme(scheme.c_str());
156
catch (CEGUI::Exception e)
158
std::cout<<"CEGUIDrawable::loadScheme Error: "<<e.getMessage()<<std::endl;
162
void CEGUIDrawable::loadFont(const std::string& font)
166
CEGUI::FontManager::getSingleton().createFont(font.c_str());
168
catch (CEGUI::Exception e)
170
std::cout<<"CEGUIDrawable::loadFont Error: "<<e.getMessage()<<std::endl;
174
void CEGUIDrawable::loadLayout(const std::string& layout)
178
CEGUI::Window* myRoot = CEGUI::WindowManager::getSingleton().loadWindowLayout(layout.c_str());
179
CEGUI::System::getSingleton().setGUISheet(myRoot);
181
catch (CEGUI::Exception e)
183
std::cout<<"CEGUIDrawable::loadLayout error: "<<e.getMessage()<<std::endl;
188
void CEGUIDrawable::drawImplementation(osg::RenderInfo& renderInfo) const
190
osg::State& state = renderInfo.getState();
192
if (state.getContextID()!=_activeContextID) return;
194
glPushAttrib(GL_ALL_ATTRIB_BITS);
196
state.disableAllVertexArrays();
198
CEGUI::System::getSingleton().renderGUI();
202
state.checkGLErrors("CEGUIDrawable::drawImplementation");
205
int main( int argc, char **argv )
208
// use an ArgumentParser object to manage the program arguments.
209
osg::ArgumentParser arguments(&argc,argv);
212
// construct the viewer.
213
osgViewer::Viewer viewer;
216
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
217
osg::ref_ptr<CEGUIDrawable> cd = new CEGUIDrawable();
218
geode->addDrawable(cd.get());
221
while(arguments.read("--scheme",scheme))
223
cd->loadScheme(scheme);
227
while(arguments.read("--font",font))
233
while(arguments.read("--layout",layout))
235
cd->loadLayout(layout);
238
osg::Timer_t start_tick = osg::Timer::instance()->tick();
240
// read the scene from the list of file specified command line args.
241
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
243
// if no model has been successfully loaded report failure.
246
std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
250
osg::ref_ptr<osg::Group> group = new osg::Group;
251
group->addChild(loadedModel.get());
253
group->addChild(geode.get());
256
// any option left unread are converted into errors to write out later.
257
arguments.reportRemainingOptionsAsUnrecognized();
259
// report any errors if they have occurred when parsing the program arguments.
260
if (arguments.errors())
262
arguments.writeErrorMessages(std::cout);
265
osg::Timer_t end_tick = osg::Timer::instance()->tick();
267
std::cout << "Time to load = "<<osg::Timer::instance()->delta_s(start_tick,end_tick)<<std::endl;
270
// optimize the scene graph, remove redundant nodes and state etc.
271
osgUtil::Optimizer optimizer;
272
optimizer.optimize(loadedModel.get());
274
// pass the loaded scene graph to the viewer.
275
viewer.setSceneData(group.get());