Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgrePopupMenuGuiElement.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2003 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 
00026 #include "OgrePopupMenuGuiElement.h"    
00027 #include "OgreStringConverter.h"    
00028 #include "OgreGuiManager.h" 
00029 #include "OgreStringResource.h" 
00030 #include "OgreException.h"  
00031 #include "OgreMouseEvent.h" 
00032 #include <algorithm>
00033 
00034 namespace Ogre {
00035 
00036     String PopupMenuGuiElement::msTypeName = "PopupMenu";
00037     PopupMenuGuiElement::CmdItemTemplate PopupMenuGuiElement::msCmdItemTemplate;
00038     PopupMenuGuiElement::CmdVSpacing PopupMenuGuiElement::msCmdVSpacing;
00039     PopupMenuGuiElement::CmdHSpacing PopupMenuGuiElement::msCmdHSpacing;
00040     PopupMenuGuiElement::CmdItemPanelMaterial PopupMenuGuiElement::msCmdItemPanelMaterial;
00041     PopupMenuGuiElement::CmdItemPanelMaterialSelected PopupMenuGuiElement::msCmdItemPanelMaterialSelected;
00042 
00043     PopupMenuGuiElement::PopupMenuGuiElement(const String& name) :
00044         PanelGuiElement(name),
00045         GuiPressable(name)
00046     {
00047         if (createParamDictionary("PopupMenuGuiElement"))
00048         {
00049             addBaseParameters();
00050         }
00051         mChildrenProcessEvents = false;
00052 
00053         mSelectedElement = 0;
00054         mVSpacing = 0;
00055         mHSpacing = 0;
00056         mPixelVSpacing = 0;
00057         mPixelHSpacing = 0;
00058         mItemPanelMaterial = "";
00059         mItemPanelMaterialSelected = "";
00060 
00061         mSelectedMenuItem = new StringResource("");
00062         mSeparatormenuItem = new StringResource("-----------------------");
00063         setSource(this);
00064         addMouseMotionListener(this);
00065     }
00066     //---------------------------------------------------------------------
00067     PopupMenuGuiElement::~PopupMenuGuiElement()
00068     {
00069         for (ResourceList::iterator i = mResourceList.begin(); i != mResourceList.end(); ++i)
00070             delete *i;
00071     }
00072 
00073     //---------------------------------------------------------------------
00074     void PopupMenuGuiElement::addBaseParameters(void)
00075     {
00076         PanelGuiElement::addBaseParameters();
00077         ParamDictionary* dict = getParamDictionary();
00078 
00079         dict->addParameter(ParameterDef("item_template", 
00080             "The template of List Item objects."
00081             , PT_STRING),
00082             &PopupMenuGuiElement::msCmdItemTemplate);
00083 
00084         dict->addParameter(ParameterDef("v_spacing", 
00085             "The vertical spacing of the elements"
00086             , PT_STRING),
00087             &PopupMenuGuiElement::msCmdVSpacing);
00088         dict->addParameter(ParameterDef("h_spacing", 
00089             "The horizontal spacing of the elements from the edge of the list"
00090             , PT_STRING),
00091             &PopupMenuGuiElement::msCmdHSpacing);
00092         dict->addParameter(ParameterDef("item_material", 
00093             "The material of the item panel"
00094             , PT_STRING),
00095             &PopupMenuGuiElement::msCmdItemPanelMaterial);
00096         dict->addParameter(ParameterDef("item_material_selected", 
00097             "The material of the item panel when it is selected"
00098             , PT_STRING),
00099             &PopupMenuGuiElement::msCmdItemPanelMaterialSelected);
00100     }
00101     //---------------------------------------------------------------------
00102     // Command objects
00103     //---------------------------------------------------------------------
00104 
00105     //-----------------------------------------------------------------------
00106     String PopupMenuGuiElement::CmdItemTemplate::doGet(const void* target) const
00107     {
00108         return static_cast<const PopupMenuGuiElement*>(target)->getItemTemplateName();
00109     }
00110     void PopupMenuGuiElement::CmdItemTemplate::doSet(void* target, const String& val)
00111     {
00112         std::vector<String> vec = StringUtil::split(val);
00113 
00114         static_cast<PopupMenuGuiElement*>(target)->setItemTemplateName(val);
00115     }
00116     //-----------------------------------------------------------------------
00117     String PopupMenuGuiElement::CmdVSpacing::doGet(const void* target) const
00118     {
00119         return static_cast<const PopupMenuGuiElement*>(target)->getVSpacing();
00120     }
00121     void PopupMenuGuiElement::CmdVSpacing::doSet(void* target, const String& val)
00122     {
00123         std::vector<String> vec = StringUtil::split(val);
00124 
00125         static_cast<PopupMenuGuiElement*>(target)->setVSpacing(val);
00126     }
00127     //-----------------------------------------------------------------------
00128     String PopupMenuGuiElement::CmdHSpacing::doGet(const void* target) const
00129     {
00130         return static_cast<const PopupMenuGuiElement*>(target)->getHSpacing();
00131     }
00132     void PopupMenuGuiElement::CmdHSpacing::doSet(void* target, const String& val)
00133     {
00134         std::vector<String> vec = StringUtil::split(val);
00135 
00136         static_cast<PopupMenuGuiElement*>(target)->setHSpacing(val);
00137     }
00138     //-----------------------------------------------------------------------
00139     String PopupMenuGuiElement::CmdItemPanelMaterialSelected::doGet(const void* target) const
00140     {
00141         return static_cast<const PopupMenuGuiElement*>(target)->getItemPanelMaterialSelected();
00142     }
00143     void PopupMenuGuiElement::CmdItemPanelMaterialSelected::doSet(void* target, const String& val)
00144     {
00145         static_cast<PopupMenuGuiElement*>(target)->setItemPanelMaterialSelected(val);
00146     }
00147     //-----------------------------------------------------------------------
00148     String PopupMenuGuiElement::CmdItemPanelMaterial::doGet(const void* target) const
00149     {
00150         return static_cast<const PopupMenuGuiElement*>(target)->getItemPanelMaterial();
00151     }
00152     void PopupMenuGuiElement::CmdItemPanelMaterial::doSet(void* target, const String& val)
00153     {
00154         static_cast<PopupMenuGuiElement*>(target)->setItemPanelMaterial(val);
00155     }
00156     //-----------------------------------------------------------------------
00157     String PopupMenuGuiElement::getItemTemplateName() const
00158     {
00159         return mItemTemplateName;
00160     }
00161     void PopupMenuGuiElement::setItemTemplateName(const String& val)
00162     {
00163         mItemTemplateName = val;
00164     }
00165     //-----------------------------------------------------------------------
00166 
00167     void PopupMenuGuiElement::setHSpacing(const String& val)
00168     {
00169         mHSpacing = StringConverter::parseReal(val);
00170     }
00171     String PopupMenuGuiElement::getHSpacing() const
00172     {
00173         return  StringConverter::toString(mHSpacing);
00174     }
00175 
00176     //-----------------------------------------------------------------------
00177 
00178     void PopupMenuGuiElement::setVSpacing(const String& val)
00179     {
00180         mVSpacing = StringConverter::parseReal(val);
00181     }
00182     String PopupMenuGuiElement::getVSpacing() const
00183     {
00184         return  StringConverter::toString(mVSpacing);
00185     }
00186     //-----------------------------------------------------------------------
00187     String PopupMenuGuiElement::getItemPanelMaterial() const
00188     {
00189         return mItemPanelMaterial;
00190     }
00191     void PopupMenuGuiElement::setItemPanelMaterial(const String& val)
00192     {
00193         mItemPanelMaterial = val;
00194     }
00195     //-----------------------------------------------------------------------
00196     String PopupMenuGuiElement::getItemPanelMaterialSelected() const
00197     {
00198         return mItemPanelMaterialSelected;
00199 
00200     }
00201     void PopupMenuGuiElement::setItemPanelMaterialSelected(const String& val)
00202     {
00203         mItemPanelMaterialSelected = val;
00204     }
00205     //-----------------------------------------------------------------------
00206 
00207     void PopupMenuGuiElement::addMenuItem(Resource* r)
00208     {
00209         GuiElement* mInsideObject = 
00210             GuiManager::getSingleton().createGuiElementFromTemplate(mItemTemplateName, "", getListItemName(r));
00211 
00212         // create a back panel for the item
00213 
00214         GuiContainer* pBackPanel = static_cast<GuiContainer*>
00215             (GuiManager::getSingleton().createGuiElement("Panel",getListItemPanelName(r)));
00216 
00217         pBackPanel->setLeft(0);
00218         pBackPanel->setWidth(getWidth());
00219         pBackPanel->setHeight(mInsideObject->getHeight());
00220         addChild(pBackPanel);
00221 
00222         mInsideObject->setCaption(r->getName());
00223         mInsideObject->setLeft(mVSpacing);
00224         mInsideObject->setTop(0);
00225         mInsideObject->setWidth(pBackPanel->getWidth()-mVSpacing);
00226 
00227         pBackPanel->addChild((GuiContainer*)mInsideObject);
00228 
00229         setSelectedItem(mInsideObject,false);
00230     }
00231 
00232     void PopupMenuGuiElement::removeMenuItem(Resource* r)
00233     {
00234         GuiContainer* backPanel = static_cast<GuiContainer*> (getChild(getListItemPanelName(r)));
00235 
00236         backPanel->removeChild(getListItemName(r));
00237         removeChild(getListItemPanelName(r));
00238 
00239         GuiManager::getSingleton().destroyGuiElement(getListItemName(r));
00240         GuiManager::getSingleton().destroyGuiElement(getListItemPanelName(r));
00241     }
00242     //-----------------------------------------------------------------------
00243 
00244     void PopupMenuGuiElement::addListItem(Resource* r)
00245     {
00246         mResourceList.push_back(r);
00247 
00248         if (mResourceList.size() == 1)
00249         {
00250             addMenuItem(r);
00251             mSelectedString = r->getName();
00252             layoutItems();
00253         }
00254     }
00255 
00256     void PopupMenuGuiElement::removeListItem(Resource* r)
00257     {
00258         bool bFound = false;
00259         ResourceList::iterator i;
00260 
00261         for (i = mResourceList.begin(); i != mResourceList.end(); ++i)
00262         {
00263             if (*i == r)
00264             {
00265                 delete *i;
00266                 mResourceList.erase(i);
00267                 bFound = true;
00268                 break;
00269             }
00270         }
00271 
00272         if (!bFound)
00273         {
00274             Except(Exception::ERR_ITEM_NOT_FOUND, "Cannot find Resource " + r->getName() + 
00275                 " to remove from list.", "PopupMenuGuiElement::removeListItem");
00276         }
00277     }
00278 
00279     String PopupMenuGuiElement::getListItemName(Resource* r)
00280     {
00281         return mName + "/" + r->getName();
00282     }
00283 
00284     String PopupMenuGuiElement::getListItemPanelName(Resource* r)
00285     {
00286         return getListItemName(r) + "/" + "BackPanel";
00287     }
00288 
00289     void PopupMenuGuiElement::layoutItems()
00290     {
00291         Real currentTop = 0;    //mVSpacing;
00292         ChildIterator it = getChildIterator();
00293         while (it.hasMoreElements())
00294         {
00295             GuiElement* currentElement = it.getNext();
00296 
00297             currentElement->setTop(currentTop);
00298             currentTop += currentElement->getHeight() + mVSpacing;
00299         }
00300         this->setHeight(currentTop - mVSpacing);
00301 
00302         _update();
00303     }
00304 
00305     void PopupMenuGuiElement::mouseDragged(MouseEvent* e) {}
00306 
00307     void PopupMenuGuiElement::mouseMoved(MouseEvent* e) 
00308     {
00309         MouseEvent* me = static_cast<MouseEvent*>(e);
00310         GuiElement* newSelect = GuiContainer::findElementAt(me->getX(), me->getY());
00311 
00312         if (newSelect == this || !isPressed())  // in case there are fringe pixels not in the list
00313         {
00314             newSelect = NULL;
00315         }
00316 
00317         if (newSelect != mSelectedElement)
00318         {
00319             if (mSelectedElement)
00320             {
00321                 setSelectedItem(mSelectedElement, false);
00322             }
00323 
00324             if (newSelect)
00325             {
00326                 setSelectedItem(newSelect, true);
00327             }
00328 
00329             mSelectedElement = newSelect;
00330         }
00331     }
00332 
00333     void PopupMenuGuiElement::mouseExited(void) 
00334     {
00335         if (mSelectedElement)
00336         {
00337             setSelectedItem(mSelectedElement, false);
00338             mSelectedElement = NULL;
00339         }
00340     }
00341 
00342     void PopupMenuGuiElement::mousePressed(void)
00343     {
00344         ResourceList::iterator i;
00345 
00346         for (i = mResourceList.begin(); i != mResourceList.end(); ++i)
00347         {
00348             if (mSelectedString == (*i)->getName())
00349             {
00350                 removeMenuItem(*i);
00351                 break;
00352             }
00353         }
00354 
00355         for (i = mResourceList.begin(); i != mResourceList.end(); ++i)
00356         {
00357             addMenuItem(*i);
00358         }
00359 
00360         layoutItems();
00361     }
00362 
00363     void PopupMenuGuiElement::mouseReleased(void) 
00364     {
00365         ResourceList::iterator i;
00366         bool selectedItem=false;
00367 
00368         if (mSelectedElement)
00369         {
00370             setActionCommand(mSelectedElement->getName());
00371             setSelectedItem(mSelectedElement, false);
00372             mSelectedString = mSelectedElement->getCaption();
00373             mSelectedElement = NULL;
00374             selectedItem = true;
00375         }
00376 
00377         for (i = mResourceList.begin(); i != mResourceList.end(); ++i)
00378         {
00379             if (mSelectedString != (*i)->getName())
00380             {
00381                 removeMenuItem(*i);
00382             }
00383         }
00384 
00385         layoutItems();
00386 
00387         if (selectedItem)
00388         {
00389             fireActionPerformed();
00390         }
00391     }
00392 
00393     Resource* PopupMenuGuiElement::getSelectedItem()
00394     {
00395         Resource* selectedResource = NULL;
00396         return selectedResource;
00397     }
00398 
00399     void PopupMenuGuiElement::setSelectedItem(Resource* r, bool on)
00400     {
00401         // do later
00402     }
00403     void PopupMenuGuiElement::setSelectedItem(Resource* r)
00404     {
00405         // do later
00406     }
00407 
00408     void PopupMenuGuiElement::setSelectedItem(GuiElement* item)
00409     {
00410         // do later
00411     }
00412 
00413     void PopupMenuGuiElement::setSelectedItem(GuiElement* item, bool on)
00414     {
00415         if (on)
00416         {
00417             item->getParent()->setMaterialName(mItemPanelMaterialSelected);
00418         }
00419         else
00420         {
00421             if (mItemPanelMaterial == "")
00422             {
00423                 // default to the list material
00424                 item->getParent()->setMaterialName(mMaterialName);
00425             }
00426             else
00427             {
00428                 item->getParent()->setMaterialName(mItemPanelMaterial);
00429             }
00430         }
00431     }
00432     ResourceListConstIterator PopupMenuGuiElement::getConstIterator() const
00433     {
00434         return ResourceListConstIterator(mResourceList.begin());
00435     }
00436 
00437     ResourceListConstIterator PopupMenuGuiElement::getConstEndIterator() const
00438     {
00439         return ResourceListConstIterator(mResourceList.end());
00440     }
00441 
00442     Resource* PopupMenuGuiElement::popFront()
00443     {
00444         Resource* r = mResourceList.front();
00445         mResourceList.pop_front();
00446 
00447         return r;
00448     }
00449     
00450     size_t PopupMenuGuiElement::getListSize() const
00451     {
00452         return mResourceList.size();
00453     }
00454 
00455 }

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:39 2004