File dialog demo code Demo Gui2 for Linux.cpp
A Common File Dialog window for CEGUI - demo (Linux)
Welcome to the new Ogre Wiki!
If you haven't done so already, be sure to visit the Wiki Portal to read about how the wiki works. Especially the Ogre Wiki Overview page.
If you haven't done so already, be sure to visit the Wiki Portal to read about how the wiki works. Especially the Ogre Wiki Overview page.
Demo_Gui2.cpp
/***************************************************************************** Written by spookyboo ported to Linux by ancestral (granturkoATgmail.com) *****************************************************************************/ #include "ExampleApplication.h" #include <CEGUI/CEGUIImageset.h> #include <CEGUI/CEGUISystem.h> #include <CEGUI/CEGUILogger.h> #include <CEGUI/CEGUISchemeManager.h> #include <CEGUI/CEGUIWindowManager.h> #include <CEGUI/CEGUIWindow.h> #include <CEGUI/elements/CEGUICombobox.h> #include <CEGUI/elements/CEGUIListbox.h> #include <CEGUI/elements/CEGUIListboxTextItem.h> #include <CEGUI/elements/CEGUIPushButton.h> #include <CEGUI/elements/CEGUIScrollbar.h> // this doesn't exist... //#include <CEGUI/elements/CEGUIStaticImage.h> #include "OgreCEGUIRenderer.h" #include "OgreCEGUIResourceProvider.h" #include <CEGUI/elements/CEGUIFrameWindow.h> #include "CEGUICommonFileDialog.h" #include <OgreRenderQueueListener.h> CEGUI::MouseButton convertOgreButtonToCegui(int buttonID) { switch (buttonID) { case 0: return CEGUI::LeftButton; case 1: return CEGUI::RightButton; case 2: return CEGUI::MiddleButton; case 3: return CEGUI::X1Button; default: return CEGUI::LeftButton; } } //MouseMotion... class GuiFrameListener : public ExampleFrameListener, public OIS::MouseListener, public OIS::KeyListener { private: CEGUI::Renderer* mGUIRenderer; bool mShutdownRequested; public: // NB using buffered input, this is the only change GuiFrameListener(RenderWindow* win, Camera* cam, CEGUI::Renderer* renderer) : ExampleFrameListener(win, cam, true, true, true), mGUIRenderer(renderer), mShutdownRequested(false) { mMouse->setEventCallback(this); mKeyboard->setEventCallback(this); //mEventProcessor->addMouseMotionListener(this); //mEventProcessor->addMouseListener(this); //mEventProcessor->addKeyListener(this); } // Tell the frame listener to exit at the end of the next frame void requestShutdown(void) { mShutdownRequested = true; } bool frameEnded(const FrameEvent& evt) { if (mShutdownRequested) return false; else return ExampleFrameListener::frameEnded(evt); } bool mouseMoved (const OIS::MouseEvent &e) { CEGUI::System::getSingleton().injectMouseMove(e.state.X.rel, e.state.Y.rel); //e->getRelX() * mGUIRenderer->getWidth(), //e->getRelY() * mGUIRenderer->getHeight()); //e->consume(); return true; } //void mouseDragged (OIS::MouseEvent *e) //{ // mouseMoved(e); //} bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id) { CEGUI::System::getSingleton().injectMouseButtonDown(convertOgreButtonToCegui(id)); return true; //convertOgreButtonToCegui(e->getButtonID())); //e->consume(); } bool mouseReleased (const OIS::MouseEvent &arg, OIS::MouseButtonID id) { CEGUI::System::getSingleton().injectMouseButtonUp(convertOgreButtonToCegui(id)); return true; // convertOgreButtonToCegui(e->getButtonID())); // e->consume(); } //void mouseClicked(OIS::MouseEvent* e) {} //void mouseEntered(OIS::MouseEvent* e) {} //void mouseExited(OIS::MouseEvent* e) {} bool keyPressed(const OIS::KeyEvent &arg) { if( arg.key == OIS::KC_ESCAPE ) mShutdownRequested = true; CEGUI::System::getSingleton().injectKeyDown( arg.key ); CEGUI::System::getSingleton().injectChar( arg.text ); return true; } bool keyReleased(const OIS::KeyEvent &arg) { CEGUI::System::getSingleton().injectKeyUp(arg.key); return true; } void keyClicked(OIS::KeyEvent* e) { // Do nothing //e->consume(); } }; class GuiApplication : public ExampleApplication { private: CEGUI::Window* mEditorGuiSheet; CEGUI::OgreCEGUIRenderer* mGUIRenderer; CEGUI::System* mGUISystem; CEGUI::Listbox* _mlstListbox; CEGUI::Window* mainWindow; public: GuiApplication() : mGUIRenderer(0), mGUISystem(0), mEditorGuiSheet(0){} ~GuiApplication() { _mlstListbox->resetList(); if (mEditorGuiSheet) CEGUI::WindowManager::getSingleton().destroyWindow(mEditorGuiSheet); if(mGUISystem) { delete mGUISystem; mGUISystem = 0; } if(mGUIRenderer) { delete mGUIRenderer; mGUIRenderer = 0; } } protected: // Just override the mandatory create scene method void createScene(void) { // Set ambient light mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5)); // Create a light Light* l = mSceneMgr->createLight("MainLight"); l->setPosition(20,80,50); // setup GUI system mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr); mGUISystem = new CEGUI::System(mGUIRenderer); CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::Informative); // load scheme and set up defaults - ORIGINAL CEGUI::Scheme *skeme = CEGUI::SchemeManager::getSingleton().loadScheme("TaharezLookSkin.scheme"); mGUISystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow"); mGUISystem->setDefaultFont((CEGUI::utf8*)"BlueHighway-12"); mEditorGuiSheet = CEGUI::WindowManager::getSingleton().loadWindowLayout((CEGUI::utf8*)"ogregui2.layout"); mGUISystem->setGUISheet(mEditorGuiSheet); CEGUI::Window* _mBtnLoad = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow((CEGUI::utf8*)"Root/Load/LoadButton"); CEGUI::Window* _mBtnSave = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow((CEGUI::utf8*)"Root/Save/SaveButton"); _mlstListbox = (CEGUI::Listbox*)CEGUI::WindowManager::getSingleton().getWindow((CEGUI::utf8*)"Root/List/Listbox"); /* CEGUI::PushButton* _mBtnLoad = (CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().getWindow((CEGUI::utf8*)"Root/Load/LoadButton"); CEGUI::PushButton* _mBtnSave = (CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().getWindow((CEGUI::utf8*)"Root/Save/SaveButton"); _mlstListbox = (CEGUI::Listbox*)CEGUI::WindowManager::getSingleton().getWindow((CEGUI::utf8*)"Root/List/Listbox"); */ _mBtnLoad->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&GuiApplication::handleLoad, this)); _mBtnSave->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&GuiApplication::handleSave, this)); /**************************************************************************************************** Step 1: Add the common file dialog window to the main window Calling CommonFileDialog::getSingleton() instantiates the common file dialog *****************************************************************************************************/ mainWindow = CEGUI::WindowManager::getSingleton().getWindow((CEGUI::utf8*)"Root"); mainWindow->addChildWindow(CEGUI::CommonFileDialog::getSingleton().getWindow()); /**************************************************************************************************** Step 2: Register the function that must be called in case the common file dialog has been closed by: - the Ok button - the Cancel button - the Window close button ('X') Use the FrameWindow::EventHidden event *****************************************************************************************************/ CEGUI::CommonFileDialog::getSingleton().getWindow()->subscribeEvent (CEGUI::FrameWindow::EventHidden, CEGUI::Event::Subscriber(&GuiApplication::handleFileDialogOutput, this)); } bool handleLoad(const CEGUI::EventArgs& e) { /**************************************************************************************************** Step 3a: Call the file dialog with the input data *****************************************************************************************************/ CEGUI::CommonFileDialogInput input; input.setHandle (1); // Set the handle to determine the originator input.setFilter ("Archive (*.zip)|*.zip|All files|*.*|"); // Extensions input.setOpenFileDialog (true); // Load mode input.setDefaultExtension ("*.*"); // Zip is the first one to display CEGUI::CommonFileDialog::getSingleton().openDialog(input); // Open the common file dialog return true; } bool handleSave(const CEGUI::EventArgs& e) { /**************************************************************************************************** Step 3b: Call the file dialog with the input data *****************************************************************************************************/ CEGUI::CommonFileDialogInput input; input.setHandle (2); // Set the handle to determine the originator input.setFilter ("JPEG (*.jpg)|*.jpg|Portable Network Graphics (*.png)|*.png|Truevision Targa (*.tga)|*.tga|"); input.setOpenFileDialog (false); // Save mode input.setDefaultExtension ("*.png"); // .png is the preferred extension input.setFileName ("test.png"); // Set default filename CEGUI::CommonFileDialog::getSingleton().openDialog(input); // Open the common file dialog return true; } bool handleFileDialogOutput(const CEGUI::EventArgs& e) { /**************************************************************************************************** Step 4: Handle the returned output *****************************************************************************************************/ CEGUI::CommonFileDialogOutput result = CEGUI::CommonFileDialog::getSingleton().getResult(); switch (result.getHandle()) { case 1: _mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Load button pressed")); break; case 2: _mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Save button pressed")); break; } switch (result.getAction()) { case CEGUI::ACTION_OK: _mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Returned with ACTION_OK")); break; case CEGUI::ACTION_CANCEL: _mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Returned with ACTION_CANCEL")); break; case CEGUI::ACTION_WINDOW_CLOSED: _mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Returned with ACTION_WINDOW_CLOSED")); break; } _mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Drive: " + result.getDrive())); _mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Full qualified name: " + result.getFullQualifiedFileName())); _mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Absolute path: " + result.getAbsolutePath())); _mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Relative path: " + result.getRelativePath())); _mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Filename: " + result.getFileName())); String fileExists = result.isFileExisting()? "TRUE" : "FALSE"; _mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)fileExists.c_str())); _mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"-------------------------------------------------------------------------------")); return true; } // Create new frame listener void createFrameListener(void) { mFrameListener = new GuiFrameListener(mWindow, mCamera, mGUIRenderer); mRoot->addFrameListener(mFrameListener); } }; #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 #define WIN32_LEAN_AND_MEAN #include "windows.h" #endif #ifdef __cplusplus extern "C" { #endif #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT ) #else int main(int argc, char **argv) #endif { // Create application object GuiApplication app; try { app.go(); } catch( Exception& e ) { #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL); #else std::cerr << "An exception has occured: " << e.getFullDescription(); #endif } return 0; } #ifdef __cplusplus } #endif
Contributors to this page: jacmoe
and
Spacegaier
.
Page last modified on Saturday 02 of January, 2010 21:20:21 UTC by jacmoe
.
The content on this page is licensed under the terms of the Creative Commons Attribution-ShareAlike License.
As an exception, any source code contributed within the content is released into the Public Domain.
Sidebar
Search box
Online users
37
online users

