|
Dernière modification
|
02-07-2007 12:43:51
|
vrag
|
alors je t'explique. Je me suis servi du tuto 9 d'irrlicht sur le meshviewer et je l'ai modifié en fonction de mes besoins. J'ai aussi rajouter deux ou trois icones mais si tu les a pas c'est pas trop grave je pense (si tu les veux je te les envois). J'ai pas tout relié encore (loin de là) donc ya que le menu outil qui marche. Les checkbox sont dans l'outil eau ( outil-> terrain-> eau) voila mon code #include <irrlicht.h> #include <iostream>
using namespace irr; using namespace gui;
#pragma comment(lib, "Irrlicht.lib")
IrrlichtDevice *Device = 0; core::stringc StartUpModelFile; core::stringw MessageText; core::stringw Caption; scene::IAnimatedMeshSceneNode* Model = 0; scene::ISceneNode* SkyBox = 0;
scene::ICameraSceneNode* Camera[2] = { 0, 0};
IGUICheckBox* cbCarre; IGUICheckBox* cbRond; IGUICheckBox* cbLosange;
/* toggles between various cameras */ void setActiveCamera ( scene::ICameraSceneNode* newActive ) { if ( 0 == Device ) return;
scene::ICameraSceneNode* active = Device->getSceneManager()->getActiveCamera ();
newActive->setInputReceiverEnabled ( true ); Device->getSceneManager()->setActiveCamera ( newActive ); }
/* The three following functions do several stuff used by the mesh viewer. The first function showAboutText() simply displays a messagebox with a caption and a message text. The texts will be stored in the MessageText and Caption variables at startup. */ void showAboutText() { // create modal message box with the text // loaded from the xml file. Device->getGUIEnvironment()->addMessageBox( Caption.c_str(), MessageText.c_str()); }
/* The second function loadModel() loads a model and displays it using an addAnimatedMeshSceneNode and the scene manager. Nothing difficult. It also displays a short message box, if the model could not be loaded. */ void loadModel(const c8* fn) { // modify the name if it a .pk3 file
core::stringc filename ( fn );
core::stringc extension; core::getFileNameExtension ( extension, filename ); extension.make_lower();
// if a texture is loaded apply it to the current model.. if ( extension == ".jpg" || extension == ".png" || extension == ".tga" || extension == ".pcx" || extension == ".psd" || extension == ".bmp" ) { video::ITexture * texture = Device->getVideoDriver()->getTexture( filename.c_str() ); if ( texture && Model ) { // always reload texture Device->getVideoDriver()->removeTexture ( texture ); texture = Device->getVideoDriver()->getTexture( filename.c_str() );
Model->setMaterialTexture ( 0, texture ); } return; }
// if a archive is loaded add it to the FileSystems.. if ( extension == ".pk3" || extension == ".zip" ) { Device->getFileSystem()->addZipFileArchive( filename.c_str () ); return; }
// load a model into the engine
if (Model) Model->remove();
Model = 0;
scene::IAnimatedMesh* m = Device->getSceneManager()->getMesh( filename.c_str() );
if (!m) { // model could not be loaded
if (StartUpModelFile != filename) Device->getGUIEnvironment()->addMessageBox( Caption.c_str(), L"The model could not be loaded. " \\ L"Maybe it is not a supported file format."); return; }
// set default material properties
Model = Device->getSceneManager()->addAnimatedMeshSceneNode(m); Model->setMaterialFlag(video::EMF_LIGHTING, false); // Model->setMaterialFlag(video::EMF_BACK_FACE_CULLING, false); Model->setDebugDataVisible(scene::EDS_OFF); Model->setAnimationSpeed(30); }
/* Finally, the third function creates a toolbox window. In this simple mesh viewer, this toolbox only contains a tab control with three edit boxes for changing the scale of the displayed model. */
void createToolBoxEau() { // remove tool box if already there IGUIEnvironment* env = Device->getGUIEnvironment(); IGUIElement* root = env->getRootGUIElement(); IGUIElement* e = root->getElementFromId(5000, true); if (e) e->remove();
// create the toolbox window IGUIWindow* wnd = env->addWindow(core::rect<s32>(600,25,800,480), false, L"Eau", 0, 5000);
// create tab control and tabs IGUITabControl* tab = env->addTabControl( core::rect<s32>(2,20,800-602,480-7), wnd, true, true);
IGUITab* t1 = tab->addTab(L"Eau");
// add senseless checkbox env->addStaticText(L"Format", core::rect<s32>(10,30,100,50), true, false, t1); cbCarre = env->addCheckBox(true, core::rect<s32>(10,55,200,75), t1, -1, L"Carre"); cbRond = env->addCheckBox(true, core::rect<s32>(10,80,200,100), t1, -1, L"Rond"); cbLosange = env->addCheckBox(true, core::rect<s32>(10,105,200,125), t1, -1, L"Losange");
env->addStaticText(L"Taille", core::rect<s32>(10,150,150,175), true, false, t1); IGUIScrollBar* scrollbar = env->addScrollBar(true, core::rect<s32>(10,170,150,185), t1, 104); scrollbar->setMax(16);
}
void createToolBoxCiel() { //Atmosphère + nuage // remove tool box if already there IGUIEnvironment* env = Device->getGUIEnvironment(); IGUIElement* root = env->getRootGUIElement(); IGUIElement* a = root->getElementFromId(5100, true); if (a) a->remove();
// create the toolbox window IGUIWindow* wnd = env->addWindow(core::rect<s32>(600,25,800,480), false, L"Ciel", 0, 5000);
IGUITabControl* tab = env->addTabControl( core::rect<s32>(2,20,800-602,480-7), wnd, true, true);
IGUITab* t1 = tab->addTab(L"Atmosphere"); IGUITab* t2 = tab->addTab(L"Nuage"); }
void createToolBoxRelief() { //Altitude + sol + relief // remove tool box if already there IGUIEnvironment* env = Device->getGUIEnvironment(); IGUIElement* root = env->getRootGUIElement(); IGUIElement* b = root->getElementFromId(5200, true); if (b) b->remove();
// create the toolbox window IGUIWindow* wnd = env->addWindow(core::rect<s32>(600,25,800,480), false, L"Relief", 0, 5000);
IGUITabControl* tab = env->addTabControl( core::rect<s32>(2,20,800-602,480-7), wnd, true, true);
IGUITab* t1 = tab->addTab(L"Altitude"); IGUITab* t2 = tab->addTab(L"Sol"); IGUITab* t3 = tab->addTab(L"Relief"); }
void createToolBoxForet() { // remove tool box if already there IGUIEnvironment* env = Device->getGUIEnvironment(); IGUIElement* root = env->getRootGUIElement(); IGUIElement* c = root->getElementFromId(5300, true); if (c) c->remove();
// create the toolbox window IGUIWindow* wnd = env->addWindow(core::rect<s32>(600,25,800,480), false, L"Foret", 0, 5000);
IGUITabControl* tab = env->addTabControl( core::rect<s32>(2,20,800-602,480-7), wnd, true, true);
IGUITab* t1 = tab->addTab(L"Foret"); }
void createToolBoxInsertion() { // remove tool box if already there IGUIEnvironment* env = Device->getGUIEnvironment(); IGUIElement* root = env->getRootGUIElement(); IGUIElement* d = root->getElementFromId(5400, true); if (d) d->remove();
// create the toolbox window IGUIWindow* wnd = env->addWindow(core::rect<s32>(600,25,800,480), false, L"Insertion", 0, 5000);
IGUITabControl* tab = env->addTabControl( core::rect<s32>(2,20,800-602,480-7), wnd, true, true);
IGUITab* t1 = tab->addTab(L"Insertion"); }
/* To get all the events sent by the GUI Elements, we need to create an event receiver. This one is really simple. If an event occurs, it checks the id of the caller and the event type, and starts an action based on these values. For example, if a menu item with id 100 was selected, if opens a file-open-dialog. */ class MyEventReceiver : public IEventReceiver { public: virtual bool OnEvent(SEvent event) { // Escape swaps Camera Input if (event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.Key == irr::KEY_ESCAPE && event.KeyInput.PressedDown == false) { if ( Device ) { scene::ICameraSceneNode * camera = Device->getSceneManager()->getActiveCamera (); if ( camera ) { camera->setInputReceiverEnabled ( !camera->isInputReceiverEnabled() ); } return true; } }
if (event.EventType == EET_GUI_EVENT) { s32 id = event.GUIEvent.Caller->getID(); IGUIEnvironment* env = Device->getGUIEnvironment();
switch(event.GUIEvent.EventType) { case EGET_MENU_ITEM_SELECTED: { // a menu item was clicked
IGUIContextMenu* menu = (IGUIContextMenu*)event.GUIEvent.Caller; s32 id = menu->getItemCommandId(menu->getSelectedItem());
switch(id) {
case 100: // File -> Open Model env->addFileOpenDialog(L"Please select a model file to open"); break;
case 101: // File -> Set Model Archive env->addFileOpenDialog(L"Please select your game archive/directory"); break;
case 200: // File -> Quit Device->closeDevice(); break;
case 300: // View -> Skybox SkyBox->setVisible(!SkyBox->isVisible()); break;
case 400: // View -> Debug Information if (Model) Model->setDebugDataVisible(Model->isDebugDataVisible() ? scene::EDS_OFF : scene::EDS_FULL); break;
case 401: createToolBoxEau(); break;
case 402: createToolBoxRelief(); break;
case 403: createToolBoxCiel(); break;
case 410: createToolBoxForet(); break;
case 420: createToolBoxInsertion(); break;
case 500: // Help->About showAboutText(); break;
case 610: // View -> Material -> Solid if (Model) Model->setMaterialType(video::EMT_SOLID); break;
case 620: // View -> Material -> Transparent if (Model) Model->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR); break;
case 630: // View -> Material -> Reflection if (Model) Model->setMaterialType(video::EMT_SPHERE_MAP); break;
case 1000: setActiveCamera ( Camera[0] ); break;
case 1100: setActiveCamera ( Camera[1] ); break;
} break; }
case EGET_FILE_SELECTED: { // load the model file, selected in the file open dialog IGUIFileOpenDialog* dialog = (IGUIFileOpenDialog*)event.GUIEvent.Caller; loadModel(core::stringc(dialog->getFilename()).c_str()); }
case EGET_SCROLL_BAR_CHANGED:
// control skin transparency /*if (id == 104) { s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos(); for (s32 i=0; i<irr::gui::EGDC_COUNT ; ++i) { video::SColor col = env->getSkin()->getColor((EGUI_DEFAULT_COLOR)i); col.setAlpha(pos); env->getSkin()->setColor((EGUI_DEFAULT_COLOR)i, col); } } break;*/
case EGET_BUTTON_CLICKED:
switch(id) { case 1101: { // set scale gui::IGUIElement* root = env->getRootGUIElement(); core::vector3df scale; core::stringc s;
s = root->getElementFromId(901, true)->getText(); scale.X = (f32)atof(s.c_str()); s = root->getElementFromId(902, true)->getText(); scale.Y = (f32)atof(s.c_str()); s = root->getElementFromId(903, true)->getText(); scale.Z = (f32)atof(s.c_str());
if (Model) Model->setScale(scale); } break; case 1102: env->addFileOpenDialog(L"Please select a model file to open"); break; case 1103: showAboutText(); break; case 1104: createToolBoxEau(); break; case 1105: createToolBoxRelief(); break; case 1106: createToolBoxCiel(); break; case 1107: createToolBoxForet(); break; case 1108: createToolBoxInsertion(); break; }
break; } }
return false; } };
/* Most of the hard work is done. We only need to create the Irrlicht Engine device and all the buttons, menus and toolbars. We start up the engine as usual, using createDevice(). To make our application catch events, we set our eventreceiver as parameter. The #ifdef WIN32 preprocessor commands are not necesarry, but I included them to make the tutorial use DirectX on Windows and OpenGL on all other platforms like Linux. As you can see, there is also a unusual call to IrrlichtDevice::setResizeAble(). This makes the render window resizeable, which is quite useful for a mesh viewer. */ int main() { // ask user for driver
video::E_DRIVER_TYPE driverType = video::EDT_OPENGL;
// create device and exit if creation failed
MyEventReceiver receiver; Device = createDevice(driverType, core::dimension2d<s32>(800, 600), 16, false, false, false, &receiver);
if (Device == 0) return 1; // could not create selected driver.
Device->setResizeAble(true);
Device->setWindowCaption(L"AoL_M");
video::IVideoDriver* driver = Device->getVideoDriver(); IGUIEnvironment* env = Device->getGUIEnvironment(); scene::ISceneManager* smgr = Device->getSceneManager();
driver->setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);
smgr->addLightSceneNode(); smgr->addLightSceneNode(0, core::vector3df(50,-50,100), video::SColorf(1.0f,1.0f,1.0f),20000); // add our media directory as "search path" Device->getFileSystem()->addFolderFileArchive ( "../../media/" );
/* The next step is to read the configuration file. It is stored in the xml format and looks a little bit like this:
<?xml version="1.0"?> <config> <startUpModel file="some filename" /> <messageText caption="Irrlicht Engine Mesh Viewer"> Hello! </messageText> </config>
We need the data stored in there to be written into the global variables StartUpModelFile, MessageText and Caption. This is now done using the Irrlicht Engine integrated XML parser: */
// read configuration from xml file
io::IXMLReader* xml = Device->getFileSystem()->createXMLReader( "config.xml");
while(xml && xml->read()) { switch(xml->getNodeType()) { case io::EXN_TEXT: // in this xml file, the only text which occurs is the messageText MessageText = xml->getNodeData(); break; case io::EXN_ELEMENT: { if (core::stringw("startUpModel") == xml->getNodeName()) StartUpModelFile = xml->getAttributeValue(L"file"); else if (core::stringw("messageText") == xml->getNodeName()) Caption = xml->getAttributeValue(L"caption"); } break; } }
if (xml) xml->drop(); // don't forget to delete the xml reader
/* That wasn't difficult. Now we'll set a nicer font and create the Menu. It is possible to create submenus for every menu item. The call menu->addItem(L"File", -1, true, true); for example adds a new menu Item with the name "File" and the id -1. The following parameter says that the menu item should be enabled, and the last one says, that there should be a submenu. The submenu can now be accessed with menu->getSubMenu(0), because the "File" entry is the menu item with index 0. */
// set a nicer font
IGUISkin* skin = env->getSkin(); IGUIFont* font = env->getFont("fonthaettenschweiler.bmp"); if (font) skin->setFont(font);
// create menu gui::IGUIContextMenu* menu = env->addMenu(); menu->addItem(L"Fichier", -1, true, true); menu->addItem(L"Edition", -1, true, true); menu->addItem(L"Outils", -1, true, true); menu->addItem(L"Vues", -1, true, true); menu->addItem(L"Aide", -1, true, true);
gui::IGUIContextMenu* submenu; submenu = menu->getSubMenu(0); submenu->addItem(L"Nouveau", 100); submenu->addItem(L"Ouvrir", 101); submenu->addItem(L"Enregistrer", 102); submenu->addItem(L"Enregistrer sous", 103); submenu->addItem(L"Revenir a la derniere sauvegarde", 104); submenu->addItem(L"Explorer les fichiers temporaires", 105); submenu->addSeparator(); submenu->addItem(L"Quitter", 200);
submenu = menu->getSubMenu(1); submenu->addItem(L"Annuler", 300); submenu->addItem(L"Repeter", 310); submenu->addItem(L"Copier", 320 ); submenu->addItem(L"Coller", 330); submenu->addItem(L"Couper", 340); submenu->addItem(L"Generer", 350); submenu->addItem(L"Redimensionner", 360); submenu->addItem(L"Dupliquer", 370);
submenu = menu->getSubMenu(2); submenu->addItem(L"Terrain",-1, true, true); submenu->addItem(L"Foret", 410); submenu->addItem(L"Insertion", 420);
submenu = submenu->getSubMenu(0); submenu->addItem(L"Eau", 401); submenu->addItem(L"Relief", 402); submenu->addItem(L"Ciel", 403);
submenu = menu->getSubMenu(3); submenu->addItem(L"Rendu", 500); submenu->addItem(L"Mode", -1, true, true);
submenu = submenu->getSubMenu(1); submenu->addItem(L"Solide", 1000); submenu->addItem(L"Fil de fer", 1010); submenu->addItem(L"Avec texture", 1020);
submenu= menu->getSubMenu(3); submenu->addItem(L"Camera", -1, true, true); submenu->addItem(L"Vue globale", 510);
submenu = submenu->getSubMenu(2); submenu->addItem(L"FPS", 1100); submenu->addItem(L"Normale", 1110);
submenu = menu->getSubMenu(4); submenu->addItem(L"Documentation", 600); submenu->addSeparator(); submenu->addItem(L"A propos", 610);
/* Below the toolbar, we want a toolbar, onto which we can place colored buttons and important looking stuff like a senseless combobox. */
// create toolbar
gui::IGUIToolBar* bar = env->addToolBar();
video::ITexture* image = driver->getTexture("open.png"); bar->addButton(1102, 0, L"Ouvrir",image, 0, false, true);
image = driver->getTexture("eau.png"); bar->addButton(1104, 0, L"Outil eau",image, 0, false, true);
image = driver->getTexture("relief.png"); bar->addButton(1105, 0, L"Outil Relief",image, 0, false, true);
image = driver->getTexture("ciel.png"); bar->addButton(1106, 0, L"Outil Ciel",image, 0, false, true);
image = driver->getTexture("arbre.png"); bar->addButton(1107, 0, L"Outil Arbre",image, 0, false, true);
image = driver->getTexture("insertion.png"); bar->addButton(1108, 0, L"Outil Insertion",image, 0, false, true);
image = driver->getTexture("help.png"); bar->addButton(1109, 0, L"Aide", image, 0, false, true);
/* To make the editor look a little bit better, we disable transparent gui elements, and add a Irrlicht Engine logo. In addition, a text, which will show the current frame per second value is created, and the window caption changed. */
// disable alpha
for (s32 i=0; i<gui::EGDC_COUNT ; ++i) { video::SColor col = env->getSkin()->getColor((gui::EGUI_DEFAULT_COLOR)i); col.setAlpha(255); env->getSkin()->setColor((gui::EGUI_DEFAULT_COLOR)i, col); }
/* That's nearly the whole application. We simply show the about message box at start up, and load the first model. To make everything look better, a skybox is created and a user controled camera, to make the application a little bit more interactive. Finally, everything is drawed in a standard drawing loop. */
// add a camera scene node Camera[0] = smgr->addCameraSceneNodeMaya(); Camera[1] = smgr->addCameraSceneNodeFPS();
setActiveCamera ( Camera[0] );
// draw everything
while(Device->run() && driver) { if (Device->isWindowActive()) { driver->beginScene(true, true, video::SColor(150,50,50,50));
if(cbCarre->isChecked()) { cbRond->setChecked(false); cbLosange->setChecked(false); } else if(cbRond->isChecked()) { cbCarre->setChecked(false); cbLosange->setChecked(false); }
else if(cbLosange->isChecked()) { cbCarre->setChecked(false); cbRond->setChecked(false); }
smgr->drawAll(); env->drawAll();
driver->endScene(); } else Device->yield(); }
Device->drop(); return 0; }
|
|
Création du message
|
02-07-2007 12:40:39
|
vrag
|
alors je t'explique. Je me suis servi du tuto 9 d'irrlicht sur le meshviewer et je l'ai modifié en fonction de mes besoins. J'ai aussi rajouter deux ou trois icones mais si tu les a pas c'est pas trop grave je pense (si tu les veux je te les envois). J'ai pas tout relié encore (loin de là) donc ya que le menu outil qui marche. Les checkbox sont dans l'outil eau ( outil-> terrain-> eau) voila mon code #include <irrlicht.h> #include <iostream>
using namespace irr; using namespace gui;
#pragma comment(lib, "Irrlicht.lib")
IrrlichtDevice *Device = 0; core::stringc StartUpModelFile; core::stringw MessageText; core::stringw Caption; scene::IAnimatedMeshSceneNode* Model = 0; scene::ISceneNode* SkyBox = 0;
scene::ICameraSceneNode* Camera[2] = { 0, 0};
IGUICheckBox* cbCarre; IGUICheckBox* cbRond; IGUICheckBox* cbLosange;
/* toggles between various cameras */ void setActiveCamera ( scene::ICameraSceneNode* newActive ) { if ( 0 == Device ) return;
scene::ICameraSceneNode* active = Device->getSceneManager()->getActiveCamera ();
newActive->setInputReceiverEnabled ( true ); Device->getSceneManager()->setActiveCamera ( newActive ); }
/* The three following functions do several stuff used by the mesh viewer. The first function showAboutText() simply displays a messagebox with a caption and a message text. The texts will be stored in the MessageText and Caption variables at startup. */ void showAboutText() { // create modal message box with the text // loaded from the xml file. Device->getGUIEnvironment()->addMessageBox( Caption.c_str(), MessageText.c_str()); }
/* The second function loadModel() loads a model and displays it using an addAnimatedMeshSceneNode and the scene manager. Nothing difficult. It also displays a short message box, if the model could not be loaded. */ void loadModel(const c8* fn) { // modify the name if it a .pk3 file
core::stringc filename ( fn );
core::stringc extension; core::getFileNameExtension ( extension, filename ); extension.make_lower();
// if a texture is loaded apply it to the current model.. if ( extension == ".jpg" || extension == ".png" || extension == ".tga" || extension == ".pcx" || extension == ".psd" || extension == ".bmp" ) { video::ITexture * texture = Device->getVideoDriver()->getTexture( filename.c_str() ); if ( texture && Model ) { // always reload texture Device->getVideoDriver()->removeTexture ( texture ); texture = Device->getVideoDriver()->getTexture( filename.c_str() );
Model->setMaterialTexture ( 0, texture ); } return; }
// if a archive is loaded add it to the FileSystems.. if ( extension == ".pk3" || extension == ".zip" ) { Device->getFileSystem()->addZipFileArchive( filename.c_str () ); return; }
// load a model into the engine
if (Model) Model->remove();
Model = 0;
scene::IAnimatedMesh* m = Device->getSceneManager()->getMesh( filename.c_str() );
if (!m) { // model could not be loaded
if (StartUpModelFile != filename) Device->getGUIEnvironment()->addMessageBox( Caption.c_str(), L"The model could not be loaded. " \\ L"Maybe it is not a supported file format."); return; }
// set default material properties
Model = Device->getSceneManager()->addAnimatedMeshSceneNode(m); Model->setMaterialFlag(video::EMF_LIGHTING, false); // Model->setMaterialFlag(video::EMF_BACK_FACE_CULLING, false); Model->setDebugDataVisible(scene::EDS_OFF); Model->setAnimationSpeed(30); }
/* Finally, the third function creates a toolbox window. In this simple mesh viewer, this toolbox only contains a tab control with three edit boxes for changing the scale of the displayed model. */
void createToolBoxEau() { // remove tool box if already there IGUIEnvironment* env = Device->getGUIEnvironment(); IGUIElement* root = env->getRootGUIElement(); IGUIElement* e = root->getElementFromId(5000, true); if (e) e->remove();
// create the toolbox window IGUIWindow* wnd = env->addWindow(core::rect<s32>(600,25,800,480), false, L"Eau", 0, 5000);
// create tab control and tabs IGUITabControl* tab = env->addTabControl( core::rect<s32>(2,20,800-602,480-7), wnd, true, true);
IGUITab* t1 = tab->addTab(L"Eau");
// add senseless checkbox env->addStaticText(L"Format", core::rect<s32>(10,30,100,50), true, false, t1); cbCarre = env->addCheckBox(true, core::rect<s32>(10,55,200,75), t1, -1, L"Carre"); cbRond = env->addCheckBox(true, core::rect<s32>(10,80,200,100), t1, -1, L"Rond"); cbLosange = env->addCheckBox(true, core::rect<s32>(10,105,200,125), t1, -1, L"Losange");
env->addStaticText(L"Taille", core::rect<s32>(10,150,150,175), true, false, t1); IGUIScrollBar* scrollbar = env->addScrollBar(true, core::rect<s32>(10,170,150,185), t1, 104); scrollbar->setMax(16);
}
void createToolBoxCiel() { //Atmosphère + nuage // remove tool box if already there IGUIEnvironment* env = Device->getGUIEnvironment(); IGUIElement* root = env->getRootGUIElement(); IGUIElement* a = root->getElementFromId(5100, true); if (a) a->remove();
// create the toolbox window IGUIWindow* wnd = env->addWindow(core::rect<s32>(600,25,800,480), false, L"Ciel", 0, 5000);
IGUITabControl* tab = env->addTabControl( core::rect<s32>(2,20,800-602,480-7), wnd, true, true);
IGUITab* t1 = tab->addTab(L"Atmosphere"); IGUITab* t2 = tab->addTab(L"Nuage"); }
void createToolBoxRelief() { //Altitude + sol + relief // remove tool box if already there IGUIEnvironment* env = Device->getGUIEnvironment(); IGUIElement* root = env->getRootGUIElement(); IGUIElement* b = root->getElementFromId(5200, true); if (b) b->remove();
// create the toolbox window IGUIWindow* wnd = env->addWindow(core::rect<s32>(600,25,800,480), false, L"Relief", 0, 5000);
IGUITabControl* tab = env->addTabControl( core::rect<s32>(2,20,800-602,480-7), wnd, true, true);
IGUITab* t1 = tab->addTab(L"Altitude"); IGUITab* t2 = tab->addTab(L"Sol"); IGUITab* t3 = tab->addTab(L"Relief"); }
void createToolBoxForet() { // remove tool box if already there IGUIEnvironment* env = Device->getGUIEnvironment(); IGUIElement* root = env->getRootGUIElement(); IGUIElement* c = root->getElementFromId(5300, true); if (c) c->remove();
// create the toolbox window IGUIWindow* wnd = env->addWindow(core::rect<s32>(600,25,800,480), false, L"Foret", 0, 5000);
IGUITabControl* tab = env->addTabControl( core::rect<s32>(2,20,800-602,480-7), wnd, true, true);
IGUITab* t1 = tab->addTab(L"Foret"); }
void createToolBoxInsertion() { // remove tool box if already there IGUIEnvironment* env = Device->getGUIEnvironment(); IGUIElement* root = env->getRootGUIElement(); IGUIElement* d = root->getElementFromId(5400, true); if (d) d->remove();
// create the toolbox window IGUIWindow* wnd = env->addWindow(core::rect<s32>(600,25,800,480), false, L"Insertion", 0, 5000);
IGUITabControl* tab = env->addTabControl( core::rect<s32>(2,20,800-602,480-7), wnd, true, true);
IGUITab* t1 = tab->addTab(L"Insertion"); }
/* To get all the events sent by the GUI Elements, we need to create an event receiver. This one is really simple. If an event occurs, it checks the id of the caller and the event type, and starts an action based on these values. For example, if a menu item with id 100 was selected, if opens a file-open-dialog. */ class MyEventReceiver : public IEventReceiver { public: virtual bool OnEvent(SEvent event) { // Escape swaps Camera Input if (event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.Key == irr::KEY_ESCAPE && event.KeyInput.PressedDown == false) { if ( Device ) { scene::ICameraSceneNode * camera = Device->getSceneManager()->getActiveCamera (); if ( camera ) { camera->setInputReceiverEnabled ( !camera->isInputReceiverEnabled() ); } return true; } }
if (event.EventType == EET_GUI_EVENT) { s32 id = event.GUIEvent.Caller->getID(); IGUIEnvironment* env = Device->getGUIEnvironment();
switch(event.GUIEvent.EventType) { case EGET_MENU_ITEM_SELECTED: { // a menu item was clicked
IGUIContextMenu* menu = (IGUIContextMenu*)event.GUIEvent.Caller; s32 id = menu->getItemCommandId(menu->getSelectedItem());
switch(id) {
case 100: // File -> Open Model env->addFileOpenDialog(L"Please select a model file to open"); break;
case 101: // File -> Set Model Archive env->addFileOpenDialog(L"Please select your game archive/directory"); break;
case 200: // File -> Quit Device->closeDevice(); break;
case 300: // View -> Skybox SkyBox->setVisible(!SkyBox->isVisible()); break;
case 400: // View -> Debug Information if (Model) Model->setDebugDataVisible(Model->isDebugDataVisible() ? scene::EDS_OFF : scene::EDS_FULL); break;
case 401: createToolBoxEau(); break;
case 402: createToolBoxRelief(); break;
case 403: createToolBoxCiel(); break;
case 410: createToolBoxForet(); break;
case 420: createToolBoxInsertion(); break;
case 500: // Help->About showAboutText(); break;
case 610: // View -> Material -> Solid if (Model) Model->setMaterialType(video::EMT_SOLID); break;
case 620: // View -> Material -> Transparent if (Model) Model->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR); break;
case 630: // View -> Material -> Reflection if (Model) Model->setMaterialType(video::EMT_SPHERE_MAP); break;
case 1000: setActiveCamera ( Camera[0] ); break;
case 1100: setActiveCamera ( Camera[1] ); break;
} break; }
case EGET_FILE_SELECTED: { // load the model file, selected in the file open dialog IGUIFileOpenDialog* dialog = (IGUIFileOpenDialog*)event.GUIEvent.Caller; loadModel(core::stringc(dialog->getFilename()).c_str()); }
case EGET_SCROLL_BAR_CHANGED:
// control skin transparency /*if (id == 104) { s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos(); for (s32 i=0; i<irr::gui::EGDC_COUNT ; ++i) { video::SColor col = env->getSkin()->getColor((EGUI_DEFAULT_COLOR)i); col.setAlpha(pos); env->getSkin()->setColor((EGUI_DEFAULT_COLOR)i, col); } } break;*/
case EGET_BUTTON_CLICKED:
switch(id) { case 1101: { // set scale gui::IGUIElement* root = env->getRootGUIElement(); core::vector3df scale; core::stringc s;
s = root->getElementFromId(901, true)->getText(); scale.X = (f32)atof(s.c_str()); s = root->getElementFromId(902, true)->getText(); scale.Y = (f32)atof(s.c_str()); s = root->getElementFromId(903, true)->getText(); scale.Z = (f32)atof(s.c_str());
if (Model) Model->setScale(scale); } break; case 1102: env->addFileOpenDialog(L"Please select a model file to open"); break; case 1103: showAboutText(); break; case 1104: createToolBoxEau(); break; case 1105: createToolBoxRelief(); break; case 1106: createToolBoxCiel(); break; case 1107: createToolBoxForet(); break; case 1108: createToolBoxInsertion(); break; }
break; } }
return false; } };
/* Most of the hard work is done. We only need to create the Irrlicht Engine device and all the buttons, menus and toolbars. We start up the engine as usual, using createDevice(). To make our application catch events, we set our eventreceiver as parameter. The #ifdef WIN32 preprocessor commands are not necesarry, but I included them to make the tutorial use DirectX on Windows and OpenGL on all other platforms like Linux. As you can see, there is also a unusual call to IrrlichtDevice::setResizeAble(). This makes the render window resizeable, which is quite useful for a mesh viewer. */ int main() { // ask user for driver
video::E_DRIVER_TYPE driverType = video::EDT_OPENGL;
// create device and exit if creation failed
MyEventReceiver receiver; Device = createDevice(driverType, core::dimension2d<s32>(800, 600), 16, false, false, false, &receiver);
if (Device == 0) return 1; // could not create selected driver.
Device->setResizeAble(true);
Device->setWindowCaption(L"AoL_M");
video::IVideoDriver* driver = Device->getVideoDriver(); IGUIEnvironment* env = Device->getGUIEnvironment(); scene::ISceneManager* smgr = Device->getSceneManager();
driver->setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);
smgr->addLightSceneNode(); smgr->addLightSceneNode(0, core::vector3df(50,-50,100), video::SColorf(1.0f,1.0f,1.0f),20000); // add our media directory as "search path" Device->getFileSystem()->addFolderFileArchive ( "../../media/" );
/* The next step is to read the configuration file. It is stored in the xml format and looks a little bit like this:
<?xml version="1.0"?> <config> <startUpModel file="some filename" /> <messageText caption="Irrlicht Engine Mesh Viewer"> Hello! </messageText> </config>
We need the data stored in there to be written into the global variables StartUpModelFile, MessageText and Caption. This is now done using the Irrlicht Engine integrated XML parser: */
// read configuration from xml file
io::IXMLReader* xml = Device->getFileSystem()->createXMLReader( "config.xml");
while(xml && xml->read()) { switch(xml->getNodeType()) { case io::EXN_TEXT: // in this xml file, the only text which occurs is the messageText MessageText = xml->getNodeData(); break; case io::EXN_ELEMENT: { if (core::stringw("startUpModel") == xml->getNodeName()) StartUpModelFile = xml->getAttributeValue(L"file"); else if (core::stringw("messageText") == xml->getNodeName()) Caption = xml->getAttributeValue(L"caption"); } break; } }
if (xml) xml->drop(); // don't forget to delete the xml reader
/* That wasn't difficult. Now we'll set a nicer font and create the Menu. It is possible to create submenus for every menu item. The call menu->addItem(L"File", -1, true, true); for example adds a new menu Item with the name "File" and the id -1. The following parameter says that the menu item should be enabled, and the last one says, that there should be a submenu. The submenu can now be accessed with menu->getSubMenu(0), because the "File" entry is the menu item with index 0. */
// set a nicer font
IGUISkin* skin = env->getSkin(); IGUIFont* font = env->getFont("fonthaettenschweiler.bmp"); if (font) skin->setFont(font);
// create menu gui::IGUIContextMenu* menu = env->addMenu(); menu->addItem(L"Fichier", -1, true, true); menu->addItem(L"Edition", -1, true, true); menu->addItem(L"Outils", -1, true, true); menu->addItem(L"Vues", -1, true, true); menu->addItem(L"Aide", -1, true, true);
gui::IGUIContextMenu* submenu; submenu = menu->getSubMenu(0); submenu->addItem(L"Nouveau", 100); submenu->addItem(L"Ouvrir", 101); submenu->addItem(L"Enregistrer", 102); submenu->addItem(L"Enregistrer sous", 103); submenu->addItem(L"Revenir a la derniere sauvegarde", 104); submenu->addItem(L"Explorer les fichiers temporaires", 105); submenu->addSeparator(); submenu->addItem(L"Quitter", 200);
submenu = menu->getSubMenu(1); submenu->addItem(L"Annuler", 300); submenu->addItem(L"Repeter", 310); submenu->addItem(L"Copier", 320 ); submenu->addItem(L"Coller", 330); submenu->addItem(L"Couper", 340); submenu->addItem(L"Generer", 350); submenu->addItem(L"Redimensionner", 360); submenu->addItem(L"Dupliquer", 370);
submenu = menu->getSubMenu(2); submenu->addItem(L"Terrain",-1, true, true); submenu->addItem(L"Foret", 410); submenu->addItem(L"Insertion", 420);
submenu = submenu->getSubMenu(0); submenu->addItem(L"Eau", 401); submenu->addItem(L"Relief", 402); submenu->addItem(L"Ciel", 403);
submenu = menu->getSubMenu(3); submenu->addItem(L"Rendu", 500); submenu->addItem(L"Mode", -1, true, true);
submenu = submenu->getSubMenu(1); submenu->addItem(L"Solide", 1000); submenu->addItem(L"Fil de fer", 1010); submenu->addItem(L"Avec texture", 1020);
submenu= menu->getSubMenu(3); submenu->addItem(L"Camera", -1, true, true); submenu->addItem(L"Vue globale", 510);
submenu = submenu->getSubMenu(2); submenu->addItem(L"FPS", 1100); submenu->addItem(L"Normale", 1110);
submenu = menu->getSubMenu(4); submenu->addItem(L"Documentation", 600); submenu->addSeparator(); submenu->addItem(L"A propos", 610);
/* Below the toolbar, we want a toolbar, onto which we can place colored buttons and important looking stuff like a senseless combobox. */
// create toolbar
gui::IGUIToolBar* bar = env->addToolBar();
video::ITexture* image = driver->getTexture("open.png"); bar->addButton(1102, 0, L"Ouvrir",image, 0, false, true);
image = driver->getTexture("eau.png"); bar->addButton(1104, 0, L"Outil eau",image, 0, false, true);
image = driver->getTexture("relief.png"); bar->addButton(1105, 0, L"Outil Relief",image, 0, false, true);
image = driver->getTexture("ciel.png"); bar->addButton(1106, 0, L"Outil Ciel",image, 0, false, true);
image = driver->getTexture("arbre.png"); bar->addButton(1107, 0, L"Outil Arbre",image, 0, false, true);
image = driver->getTexture("insertion.png"); bar->addButton(1108, 0, L"Outil Insertion",image, 0, false, true);
image = driver->getTexture("help.png"); bar->addButton(1109, 0, L"Aide", image, 0, false, true);
/* To make the editor look a little bit better, we disable transparent gui elements, and add a Irrlicht Engine logo. In addition, a text, which will show the current frame per second value is created, and the window caption changed. */
// disable alpha
for (s32 i=0; i<gui::EGDC_COUNT ; ++i) { video::SColor col = env->getSkin()->getColor((gui::EGUI_DEFAULT_COLOR)i); col.setAlpha(255); env->getSkin()->setColor((gui::EGUI_DEFAULT_COLOR)i, col); }
/* That's nearly the whole application. We simply show the about message box at start up, and load the first model. To make everything look better, a skybox is created and a user controled camera, to make the application a little bit more interactive. Finally, everything is drawed in a standard drawing loop. */
// add a camera scene node Camera[0] = smgr->addCameraSceneNodeMaya(); Camera[1] = smgr->addCameraSceneNodeFPS();
setActiveCamera ( Camera[0] );
// draw everything
while(Device->run() && driver) { if (Device->isWindowActive()) { driver->beginScene(true, true, video::SColor(150,50,50,50));
if(cbCarre->isChecked()) { cbRond->setChecked(false); cbLosange->setChecked(false); } else if(cbRond->isChecked()) { cbCarre->setChecked(false); cbLosange->setChecked(false); }
else if(cbLosange->isChecked()) { cbCarre->setChecked(false); cbRond->setChecked(false); }
smgr->drawAll(); env->drawAll();
driver->endScene(); } else Device->yield(); }
Device->drop(); return 0; }
|