c++ snap code /* ----------------------------------------------------------------------------- This source file is part of OGRE ----- ...
c++ snap code
/* ----------------------------------------------------------------------------- This source file is part of OGRE ----------------------------------------------------------------------------- */ #ifndef __SdkTrays_H__ #define __SdkTrays_H__ #include "Ogre.h" #include "OgreFontManager.h" #include "OgreBorderPanelOverlayElement.h" #include "OgreTextAreaOverlayElement.h" #include <math.h> #if OGRE_COMPILER == OGRE_COMPILER_MSVC // TODO - remove this # pragma warning (disable : 4244) #endif namespace OgreBites { enum TrayLocation // enumerator values for widget tray anchoring locations { TL_TOPLEFT, TL_TOP, TL_TOPRIGHT, TL_LEFT, TL_CENTER, TL_RIGHT, TL_BOTTOMLEFT, TL_BOTTOM, TL_BOTTOMRIGHT, TL_NONE }; enum ButtonState // enumerator values for button states { BS_UP, BS_OVER, BS_DOWN }; // forward widget class declarations class Widget; class Button; class SelectMenu; class Label; class Slider; class CheckBox; /*============================================================================= | Listener class for responding to tray events. =============================================================================*/ /*============================================================================= | Abstract base class for all widgets. =============================================================================*/ class Widget { public: Widget() { mTrayLoc = TL_NONE; mElement = 0; mListener = 0; } virtual ~Widget() {} void cleanup() { if (mElement) nukeOverlayElement(mElement); mElement = 0; } /*----------------------------------------------------------------------------- | Static utility method to recursively delete an overlay element plus | all of its children from the system. -----------------------------------------------------------------------------*/ static void nukeOverlayElement(Ogre::OverlayElement* element) { Ogre::OverlayContainer* container = dynamic_cast<Ogre::OverlayContainer*>(element); if (container) { std::vector<Ogre::OverlayElement*> toDelete; Ogre::OverlayContainer::ChildIterator children = container->getChildIterator(); while (children.hasMoreElements()) { toDelete.push_back(children.getNext()); } for (unsigned int i = 0; i < toDelete.size(); i++) { nukeOverlayElement(toDelete[i]); } } if (element) { Ogre::OverlayContainer* parent = element->getParent(); if (parent) parent->removeChild(element->getName()); Ogre::OverlayManager::getSingleton().destroyOverlayElement(element); } } /*----------------------------------------------------------------------------- | Static utility method to check if the cursor is over an overlay element. -----------------------------------------------------------------------------*/ static bool isCursorOver(Ogre::OverlayElement* element, const Ogre::Vector2& cursorPos, Ogre::Real voidBorder = 0) { Ogre::OverlayManager& om = Ogre::OverlayManager::getSingleton(); Ogre::Real l = element->_getDerivedLeft() * om.getViewportWidth(); Ogre::Real t = element->_getDerivedTop() * om.getViewportHeight(); Ogre::Real r = l + element->getWidth(); Ogre::Real b = t + element->getHeight(); return (cursorPos.x >= l + voidBorder && cursorPos.x <= r - voidBorder && cursorPos.y >= t + voidBorder && cursorPos.y <= b - voidBorder); } /*----------------------------------------------------------------------------- | Static utility method used to get the cursor's offset from the center | of an overlay element in pixels. -----------------------------------------------------------------------------*/ static Ogre::Vector2 cursorOffset(Ogre::OverlayElement* element, const Ogre::Vector2& cursorPos) { Ogre::OverlayManager& om = Ogre::OverlayManager::getSingleton(); return Ogre::Vector2(cursorPos.x - (element->_getDerivedLeft() * om.getViewportWidth() + element->getWidth() / 2), cursorPos.y - (element->_getDerivedTop() * om.getViewportHeight() + element->getHeight() / 2)); } void resourceGroupLoadStarted(const Ogre::String& groupName, size_t resourceCount) { mLoadInc = mGroupLoadProportion / resourceCount; mLoadBar->setCaption("Loading..."); #if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE mWindow->update(); #endif } void resourceLoadStarted(const Ogre::ResourcePtr& resource) { mLoadBar->setComment(resource->getName()); #if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE mWindow->update(); #endif } void resourceLoadEnded() { mLoadBar->setProgress(mLoadBar->getProgress() + mLoadInc); #if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE mWindow->update(); #endif } void worldGeometryStageStarted(const Ogre::String& description) { mLoadBar->setComment(description); #if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE mWindow->update(); #endif } void worldGeometryStageEnded() { mLoadBar->setProgress(mLoadBar->getProgress() + mLoadInc); #if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE mWindow->update(); #endif } void resourceGroupLoadEnded(const Ogre::String& groupName) {} /*----------------------------------------------------------------------------- | Toggles visibility of advanced statistics. -----------------------------------------------------------------------------*/ void labelHit(Label* label) { if (mStatsPanel->getOverlayElement()->isVisible()) { mStatsPanel->getOverlayElement()->hide(); mFpsLabel->getOverlayElement()->setWidth(150); removeWidgetFromTray(mStatsPanel); } else { mStatsPanel->getOverlayElement()->show(); mFpsLabel->getOverlayElement()->setWidth(180); moveWidgetToTray(mStatsPanel, mFpsLabel->getTrayLocation(), locateWidgetInTray(mFpsLabel) + 1); } } /*----------------------------------------------------------------------------- | Destroys dialog widgets, notifies listener, and ends high priority session. -----------------------------------------------------------------------------*/ void buttonHit(Button* button) { if (mListener) { if (button == mOk) mListener->okDialogClosed(mDialog->getText()); else mListener->yesNoDialogClosed(mDialog->getText(), button == mYes); } closeDialog(); } /*----------------------------------------------------------------------------- | Processes mouse button down events. Returns true if the event was | consumed and should not be passed on to other handlers. -----------------------------------------------------------------------------*/ #if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE bool injectMouseDown(const OIS::MultiTouchEvent& evt) #else bool injectMouseDown(const OIS::MouseEvent& evt, OIS::MouseButtonID id) #endif { #if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE // only process left button when stuff is visible if (!mCursorLayer->isVisible() || id != OIS::MB_Left) return false; #else // only process touches when stuff is visible if (!mCursorLayer->isVisible()) return false; #endif Ogre::Vector2 cursorPos(mCursor->getLeft(), mCursor->getTop()); mTrayDrag = false; if (mExpandedMenu) // only check top priority widget until it passes on { mExpandedMenu->_cursorPressed(cursorPos); if (!mExpandedMenu->isExpanded()) setExpandedMenu(0); return true; } if (mDialog) // only check top priority widget until it passes on { mDialog->_cursorPressed(cursorPos); if (mOk) mOk->_cursorPressed(cursorPos); else { mYes->_cursorPressed(cursorPos); mNo->_cursorPressed(cursorPos); } return true; } for (unsigned int i = 0; i < 9; i++) // check if mouse is over a non-null tray { if (mTrays[i]->isVisible() && Widget::isCursorOver(mTrays[i], cursorPos, 2)) { mTrayDrag = true; // initiate a drag that originates in a tray break; } } for (unsigned int i = 0; i < mWidgets[9].size(); i++) // check if mouse is over a non-null tray's widgets { if (mWidgets[9][i]->getOverlayElement()->isVisible() && Widget::isCursorOver(mWidgets[9][i]->getOverlayElement(), cursorPos)) { mTrayDrag = true; // initiate a drag that originates in a tray break; } } if (!mTrayDrag) return false; // don't process if mouse press is not in tray for (unsigned int i = 0; i < 10; i++) { if (!mTrays[i]->isVisible()) continue; for (unsigned int j = 0; j < mWidgets[i].size(); j++) { Widget* w = mWidgets[i][j]; if (!w->getOverlayElement()->isVisible()) continue; w->_cursorPressed(cursorPos); // send event to widget SelectMenu* m = dynamic_cast<SelectMenu*>(w); if (m && m->isExpanded()) // a menu has begun a top priority session { setExpandedMenu(m); return true; } } } return true; // a tray click is not to be handled by another party } /*----------------------------------------------------------------------------- | Processes mouse button up events. Returns true if the event was | consumed and should not be passed on to other handlers. -----------------------------------------------------------------------------*/ #if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE bool injectMouseUp(const OIS::MultiTouchEvent& evt) #else bool injectMouseUp(const OIS::MouseEvent& evt, OIS::MouseButtonID id) #endif { #if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE // only process left button when stuff is visible if (!mCursorLayer->isVisible() || id != OIS::MB_Left) return false; #else // only process touches when stuff is visible if (!mCursorLayer->isVisible()) return false; #endif Ogre::Vector2 cursorPos(mCursor->getLeft(), mCursor->getTop()); if (mExpandedMenu) // only check top priority widget until it passes on { mExpandedMenu->_cursorReleased(cursorPos); return true; } if (mDialog) // only check top priority widget until it passes on { mDialog->_cursorReleased(cursorPos); if (mOk) mOk->_cursorReleased(cursorPos); else { mYes->_cursorReleased(cursorPos); // very important to check if second button still exists, because first button could've closed the popup if (mNo) mNo->_cursorReleased(cursorPos); } return true; } if (!mTrayDrag) return false; // this click did not originate in a tray, so don't process Widget* w; for (unsigned int i = 0; i < 10; i++) { if (!mTrays[i]->isVisible()) continue; for (unsigned int j = 0; j < mWidgets[i].size(); j++) { w = mWidgets[i][j]; if (!w->getOverlayElement()->isVisible()) continue; w->_cursorReleased(cursorPos); // send event to widget } } mTrayDrag = false; // stop this drag return true; // this click did originate in this tray, so don't pass it on } /*----------------------------------------------------------------------------- | Updates cursor position. Returns true if the event was | consumed and should not be passed on to other handlers. -----------------------------------------------------------------------------*/ #if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE bool injectMouseMove(const OIS::MultiTouchEvent& evt) #else bool injectMouseMove(const OIS::MouseEvent& evt) #endif { if (!mCursorLayer->isVisible()) return false; // don't process if cursor layer is invisible Ogre::Vector2 cursorPos(evt.state.X.abs, evt.state.Y.abs); mCursor->setPosition(cursorPos.x, cursorPos.y); if (mExpandedMenu) // only check top priority widget until it passes on { mExpandedMenu->_cursorMoved(cursorPos); return true; } if (mDialog) // only check top priority widget until it passes on { mDialog->_cursorMoved(cursorPos); if (mOk) mOk->_cursorMoved(cursorPos); else { mYes->_cursorMoved(cursorPos); mNo->_cursorMoved(cursorPos); } return true; } Widget* w; for (unsigned int i = 0; i < 10; i++) { if (!mTrays[i]->isVisible()) continue; for (unsigned int j = 0; j < mWidgets[i].size(); j++) { w = mWidgets[i][j]; if (!w->getOverlayElement()->isVisible()) continue; w->_cursorMoved(cursorPos); // send event to widget } } if (mTrayDrag) return true; // don't pass this event on if we're in the middle of a drag return false; } protected: /*----------------------------------------------------------------------------- | Internal method to prioritise / deprioritise expanded menus. -----------------------------------------------------------------------------*/ void setExpandedMenu(SelectMenu* m) { if (!mExpandedMenu && m) { Ogre::OverlayContainer* c = (Ogre::OverlayContainer*)m->getOverlayElement(); Ogre::OverlayContainer* eb = (Ogre::OverlayContainer*)c->getChild(m->getName() + "/MenuExpandedBox"); eb->_update(); eb->setPosition ((unsigned int)(eb->_getDerivedLeft() * Ogre::OverlayManager::getSingleton().getViewportWidth()), (unsigned int)(eb->_getDerivedTop() * Ogre::OverlayManager::getSingleton().getViewportHeight())); c->removeChild(eb->getName()); mPriorityLayer->add2D(eb); } else if(mExpandedMenu && !m) { Ogre::OverlayContainer* eb = mPriorityLayer->getChild(mExpandedMenu->getName() + "/MenuExpandedBox"); mPriorityLayer->remove2D(eb); ((Ogre::OverlayContainer*)mExpandedMenu->getOverlayElement())->addChild(eb); } mExpandedMenu = m; } Ogre::String mName; // name of this tray system Ogre::RenderWindow* mWindow; // render window #if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE OIS::MultiTouch* mMouse; // multitouch device #else OIS::Mouse* mMouse; // mouse device #endif Ogre::Overlay* mBackdropLayer; // backdrop layer Ogre::Overlay* mTraysLayer; // widget layer Ogre::Overlay* mPriorityLayer; // top priority layer Ogre::Overlay* mCursorLayer; // cursor layer Ogre::OverlayContainer* mBackdrop; // backdrop Ogre::OverlayContainer* mTrays[10]; // widget trays WidgetList mWidgets[10]; // widgets WidgetList mWidgetDeathRow; // widget queue for deletion Ogre::OverlayContainer* mCursor; // cursor SdkTrayListener* mListener; // tray listener Ogre::Real mWidgetPadding; // widget padding Ogre::Real mWidgetSpacing; // widget spacing Ogre::Real mTrayPadding; // tray padding bool mTrayDrag; // a mouse press was initiated on a tray SelectMenu* mExpandedMenu; // top priority expanded menu widget TextBox* mDialog; // top priority dialog widget Ogre::OverlayContainer* mDialogShade; // top priority dialog shade Button* mOk; // top priority OK button Button* mYes; // top priority Yes button Button* mNo; // top priority No button bool mCursorWasVisible; // cursor state before showing dialog Label* mFpsLabel; // FPS label ParamsPanel* mStatsPanel; // frame stats panel DecorWidget* mLogo; // logo ProgressBar* mLoadBar; // loading bar Ogre::Real mGroupInitProportion; // proportion of load job assigned to initialising one resource group Ogre::Real mGroupLoadProportion; // proportion of load job assigned to loading one resource group Ogre::Real mLoadInc; // loading increment Ogre::GuiHorizontalAlignment mTrayWidgetAlign[10]; // tray widget alignments }; } #endif
原标题:Ogre SdkTrays UI c++ code override with c# (.net2.0)
关键词:.NET
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。