////mem probs without this next one
#include <CEGUI/elements/CEGUIPushButton.h>
#include <CEGUI/elements/CEGUICombobox.h>
#include <CEGUI/elements/CEGUIListbox.h>
#include <CEGUI/elements/CEGUIListboxTextItem.h>
#include <cegui/elements/CEGUIPopupMenu.h>
#include <OgreNoMemoryMacros.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 "OgreCEGUIRenderer.h"
#include "OgreCEGUIResourceProvider.h"
//regular mem handler
#include <OgreMemoryMacros.h>
#include "ExampleApplication.h"
CEGUI::MouseButton convertOgreButtonToCegui(int buttonID)
{
switch (buttonID)
{
case MouseEvent::BUTTON0_MASK:
return CEGUI::LeftButton;
case MouseEvent::BUTTON1_MASK:
return CEGUI::RightButton;
case MouseEvent::BUTTON2_MASK:
return CEGUI::MiddleButton;
case MouseEvent::BUTTON3_MASK:
return CEGUI::X1Button;
default:
return CEGUI::LeftButton;
}
}
class GuiFrameListener : public ExampleFrameListener, public MouseMotionListener, public MouseListener
{
private:
CEGUI::Renderer* mGUIRenderer;
bool mShutdownRequested;
public:
// NB using buffered input
GuiFrameListener(RenderWindow* win, Camera* cam, CEGUI::Renderer* renderer)
: ExampleFrameListener(win, cam, true, true),
mGUIRenderer(renderer),
mShutdownRequested(false)
{
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);
}
void mouseMoved (MouseEvent *e)
{
CEGUI::System::getSingleton().injectMouseMove(
e->getRelX() * mGUIRenderer->getWidth(),
e->getRelY() * mGUIRenderer->getHeight());
e->consume();
}
void mouseDragged (MouseEvent *e)
{
mouseMoved(e);
}
void mousePressed (MouseEvent *e)
{
CEGUI::System::getSingleton().injectMouseButtonDown(
convertOgreButtonToCegui(e->getButtonID()));
e->consume();
}
void mouseReleased (MouseEvent *e)
{
CEGUI::System::getSingleton().injectMouseButtonUp(
convertOgreButtonToCegui(e->getButtonID()));
e->consume();
}
void mouseClicked(MouseEvent* e) {}
void mouseEntered(MouseEvent* e) {}
void mouseExited(MouseEvent* e) {}
void keyPressed(KeyEvent* e)
{
if(e->getKey() == KC_ESCAPE)
{
mShutdownRequested = true;
e->consume();
return;
}
CEGUI::System::getSingleton().injectKeyDown(e->getKey());
CEGUI::System::getSingleton().injectChar(e->getKeyChar());
e->consume();
}
void keyReleased(KeyEvent* e)
{
CEGUI::System::getSingleton().injectKeyUp(e->getKey());
e->consume();
}
void keyClicked(KeyEvent* e)
{
// Do nothing
e->consume();
}
};
class TutorialApplication : public ExampleApplication
{
private:
CEGUI::OgreCEGUIRenderer* mGUIRenderer;
CEGUI::System* mGUISystem;
CEGUI::Window* mEditorGuiSheet;
bool mButtonQuitBuild;
CEGUI::PopupMenu* popupMenu;
public:
TutorialApplication()
: mGUIRenderer(0),
mGUISystem(0),
mEditorGuiSheet(0),mButtonQuitBuild(false),popupMenu(0)
{
}
~TutorialApplication()
{
if(mEditorGuiSheet)
{
CEGUI::WindowManager::getSingleton().destroyWindow(mEditorGuiSheet);
}
if(popupMenu)
{
CEGUI::WindowManager::getSingleton().destroyWindow(popupMenu);
}
if(mGUISystem)
{
delete mGUISystem;
mGUISystem = 0;
}
if(mGUIRenderer)
{
delete mGUIRenderer;
mGUIRenderer = 0;
}
}
void setupEventHandlers(void)
{
CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
wmgr.getWindow((CEGUI::utf8*)"MyButton")->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&TutorialApplication::handlePopMenu, this));
}
bool handlePopMenu(const CEGUI::EventArgs& e)
{
if(!popupMenu)
{ CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
CEGUI::ItemEntry* menuItemOne=(CEGUI::ItemEntry*)wmgr.createWindow("TaharezLook/MenuItem",(CEGUI::utf8*)"MenuItemOne");
menuItemOne->setText((CEGUI::utf8*)"飞机");
CEGUI::ItemEntry* menuItemTwo=(CEGUI::ItemEntry*)wmgr.createWindow("TaharezLook/MenuItem",(CEGUI::utf8*)"MenuItemTwo");
menuItemTwo->setText((CEGUI::utf8*)"汽车");
CEGUI::ItemEntry* menuItemThree=(CEGUI::ItemEntry*)wmgr.createWindow("TaharezLook/MenuItem",(CEGUI::utf8*)"MenuItemThree");
menuItemThree->setText((CEGUI::utf8*)"轮船");
CEGUI::ItemEntry* menuItemFour=(CEGUI::ItemEntry*)wmgr.createWindow("TaharezLook/MenuItem",(CEGUI::utf8*)"MenuItemFour");
menuItemFour->setText((CEGUI::utf8*)"自行车");
CEGUI::ItemEntry* menuItemSubItemOne=(CEGUI::ItemEntry*)wmgr.createWindow("TaharezLook/MenuItem",(CEGUI::utf8*)"MenuItemFive");
menuItemSubItemOne->setText((CEGUI::utf8*)"红色");
CEGUI::ItemEntry* menuItemSubItemTwo=(CEGUI::ItemEntry*)wmgr.createWindow("TaharezLook/MenuItem",(CEGUI::utf8*)"MenuItemSix");
menuItemSubItemTwo->setText((CEGUI::utf8*)"绿色");
CEGUI::PopupMenu* subPopupMenu=(CEGUI::PopupMenu*)CEGUI::WindowManager::getSingleton().createWindow("TaharezLook/PopupMenu", (CEGUI::utf8*)"SubPopMenu");
subPopupMenu->setAlwaysOnTop(true);
subPopupMenu->addItem(menuItemSubItemOne);
subPopupMenu->addItem(menuItemSubItemTwo);
menuItemSubItemOne->setAlwaysOnTop(true);
menuItemSubItemTwo->setAlwaysOnTop(true);
menuItemSubItemTwo->subscribeEvent(CEGUI::ItemEntry::EventMouseClick,CEGUI::Event::Subscriber(&TutorialApplication::handleQuit, this));
popupMenu=(CEGUI::PopupMenu*)CEGUI::WindowManager::getSingleton().createWindow("TaharezLook/PopupMenu", (CEGUI::utf8*)"PopMenu");
popupMenu->setText("Option");
popupMenu->setAlwaysOnTop(true);
popupMenu->addItem(menuItemOne);
popupMenu->addItem(menuItemTwo);
popupMenu->addItem(menuItemThree);
popupMenu->addItem(menuItemFour);
menuItemOne->setAlwaysOnTop(true);
menuItemTwo->setAlwaysOnTop(true);
menuItemThree->setAlwaysOnTop(true);
menuItemFour->setAlwaysOnTop(true);
menuItemTwo->addChildWindow(subPopupMenu);
popupMenu->show();
popupMenu->setPosition(CEGUI::Point(0.65,0.45));
//subPopupMenu->show();
//popupMenu->openPopupMenu();
//CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
wmgr.getWindow((CEGUI::utf8*)"Root")->addChildWindow(popupMenu);
return true;
}
//将editbox中的内容送到listbox
CEGUI::Window* myEditBox = CEGUI::WindowManager::getSingleton().getWindow("MyEditbox");
CEGUI::ListboxTextItem *listboxitem =
new CEGUI::ListboxTextItem (myEditBox->getText());
listboxitem->setSelectionBrushImage("TaharezLook", "ListboxSelectionBrush");
//listboxitem->setSelected(mList->getItemCount() == 0);
//listboxitem->setSelectionColours(mPreview->getImageColours());
CEGUI::Listbox*myListBox=(CEGUI::Listbox*)CEGUI::WindowManager::getSingleton().getWindow("MyListBox");
myListBox->addItem(listboxitem);
return true;
}
bool handleQuit(const CEGUI::EventArgs& e)
{
if(!mButtonQuitBuild)
{
/*static_cast<GuiFrameListener*>(mFrameListener)->requestShutdown();*/
CEGUI::PushButton* quitButton = (CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().createWindow("TaharezLook/Button", (CEGUI::utf8*)"Quit");
CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
wmgr.getWindow((CEGUI::utf8*)"MyFrameWindow")->addChildWindow(quitButton);
//mEditorGuiSheet->addChildWindow(quitButton);
quitButton->setPosition(CEGUI::Point(0.65f, 0.25f));
quitButton->setSize(CEGUI::Size(0.3f, 0.06f));
quitButton->setText((CEGUI::utf8*)"退出");
mButtonQuitBuild=true;
return true;
}
return true;
}
protected:
void createScene(void)
{
// Set ambient light
mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
// Set up 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);
CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme");
mGUISystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");
CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseMoveCursor");
mGUISystem->setDefaultFont((CEGUI::utf8*)"SimHei-12");
//////////////////////////////////////////////////////////////////////////
mEditorGuiSheet = CEGUI::WindowManager::getSingleton().loadWindowLayout((CEGUI::utf8*)"My2.xml");
mGUISystem->setGUISheet(mEditorGuiSheet);
//CEGUI::PushButton* quitButton = (CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().getWindow((CEGUI::utf8*)"MyButton");
//////////////////////////////////////////////////////////////////////////
CEGUI::Combobox* objectComboBox = (CEGUI::Combobox*)CEGUI::WindowManager::getSingleton().getWindow("MyComboBox");
CEGUI::ListboxTextItem* item = new CEGUI::ListboxTextItem((CEGUI::utf8*)"飞机", 0);
objectComboBox->addItem(item);
item = new CEGUI::ListboxTextItem((CEGUI::utf8*)"汽车", 1);
objectComboBox->addItem(item);
item = new CEGUI::ListboxTextItem((CEGUI::utf8*)"轮船", 2);
objectComboBox->addItem(item);
item = new CEGUI::ListboxTextItem((CEGUI::utf8*)"StaticText", 3);
objectComboBox->addItem(item);
item = new CEGUI::ListboxTextItem((CEGUI::utf8*)"StaticImage", 4);
objectComboBox->addItem(item);
item = new CEGUI::ListboxTextItem((CEGUI::utf8*)"Render to Texture", 5);
objectComboBox->addItem(item);
//////////////////////////////////////////////////////////////////////////
setupEventHandlers();
}
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"
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
int main(int argc, char **argv)
#endif
{
// Create application object
TutorialApplication 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
fprintf(stderr, "An exception has occured: %s\n",
e.getFullDescription().c_str());
#endif
}
return 0;
}
posted on 2006-10-19 17:16
清源游民 阅读(2995)
评论(1) 编辑 收藏 引用