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

OgreButtonGuiElement.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-2002 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 "OgreButtonGuiElement.h"
00027 #include "OgreGuiManager.h"
00028 #include "OgreStringConverter.h"
00029 
00030 namespace Ogre {
00031 
00032     //-----------------------------------------------------------------------
00033     String ButtonGuiElement::msTypeName = "Button";
00034     ButtonGuiElement::CmdButtonDownMaterial ButtonGuiElement::msCmdButtonDownMaterial;
00035     ButtonGuiElement::CmdButtonUpMaterial ButtonGuiElement::msCmdButtonUpMaterial;
00036     ButtonGuiElement::CmdButtonHiliteDownMaterial ButtonGuiElement::msCmdButtonHiliteDownMaterial;
00037     ButtonGuiElement::CmdButtonHiliteUpMaterial ButtonGuiElement::msCmdButtonHiliteUpMaterial;
00038     ButtonGuiElement::CmdButtonDisabledMaterial ButtonGuiElement::msCmdButtonDisabledMaterial;
00039     ButtonGuiElement::CmdCaptionColour ButtonGuiElement::msCmdCaptionColour;
00040     ButtonGuiElement::CmdCaptionDisabledColour ButtonGuiElement::msCmdCaptionDisabledColour;
00041     ButtonGuiElement::CmdButtonCaption ButtonGuiElement::msCmdButtonCaption;
00042 
00043     //-----------------------------------------------------------------------
00044     ButtonGuiElement::ButtonGuiElement(const String& name) :
00045         PanelGuiElement(name),
00046         GuiPressable(name)
00047     {
00048         mButtonDown = false;
00049         mChildrenProcessEvents = false;
00050 
00051         mDownMaterialName = "";
00052         mUpMaterialName = "";
00053         mHiliteDownMaterialName = "";
00054         mHiliteUpMaterialName = "";
00055         mDisabledMaterialName = "";
00056 
00057         mSetCaptionColor = false;
00058         mSetCaptionDisabledColor = false;
00059 
00060         mInsideObject = 0;
00061 
00062         if (createParamDictionary("ButtonGuiElement"))
00063         {
00064             addBaseParameters();
00065         }
00066         setSource(this);
00067     }
00068 
00069     //-----------------------------------------------------------------------
00070     void ButtonGuiElement::processEvent(InputEvent* e) 
00071     {
00072         PanelGuiElement::processEvent(e);
00073 
00074         updateMaterials();
00075     }
00076 
00077     //-----------------------------------------------------------------------
00078     void ButtonGuiElement::updateMaterials(bool init) 
00079     {
00080         bool buttonStatus;
00081 
00082         // did the button's status change between last time
00083         buttonStatus = (mButtonDown != (isPressed() && isMouseWithin()));
00084 
00085         mButtonDown = (isPressed() && isMouseWithin());
00086 
00087         String materialName;
00088 
00089         if (!isEnabled())
00090         {
00091             if (mInsideObject)
00092             {
00093                 if (mSetCaptionDisabledColor)
00094                     mInsideObject->setColour(mCaptionDisabledColour);
00095                 else if (mSetCaptionColor)
00096                     mInsideObject->setColour(mCaptionColour);
00097             }
00098 
00099             if (mDisabledMaterialName.empty())
00100                 materialName = mDisabledMaterialName;
00101             else
00102                 materialName = mUpMaterialName;
00103         }
00104         else
00105         {
00106             if (mSetCaptionColor && mInsideObject)
00107                 mInsideObject->setColour(mCaptionColour);
00108 
00109             if (mMouseWithin)
00110             {
00111                 if (mButtonDown)
00112                     materialName = mHiliteDownMaterialName;
00113                 else
00114                     materialName = mHiliteUpMaterialName;
00115 
00116                 if (materialName.empty())
00117                 {
00118                     if (mButtonDown)
00119                         materialName = mDownMaterialName;
00120                     else
00121                         materialName = mUpMaterialName;
00122                 }
00123             }
00124             else
00125             {
00126                 if (mButtonDown)
00127                     materialName = mDownMaterialName;
00128                 else
00129                     materialName = mUpMaterialName;
00130             }
00131         }
00132 
00133         PanelGuiElement::setMaterialName(materialName);
00134 
00135         if (buttonStatus && !init)
00136         {
00137             ChildIterator it = getChildIterator();
00138             while (it.hasMoreElements())
00139             {
00140                 if (mMetricsMode == GMM_PIXELS)
00141                     changeChild(it.getNext(), ((mButtonDown)?1:-1));
00142                 else
00143                     changeChild(it.getNext(), ((mButtonDown)?0.003:-0.003));
00144             }
00145         }
00146     }
00147 
00148     //-----------------------------------------------------------------------
00149     void ButtonGuiElement::changeChild(GuiElement* e, Real add)
00150     {
00151         e->setLeft(e->getLeft() + add);
00152         e->setTop(e->getTop() + add);
00153 
00154         e->setWidth(e->getWidth() - 2 * add);
00155         e->setHeight(e->getHeight() - 2 * add);
00156     }
00157 
00158     //---------------------------------------------------------------------
00159     void ButtonGuiElement::addBaseParameters(void)
00160     {
00161         PanelGuiElement::addBaseParameters();
00162         ParamDictionary* dict = getParamDictionary();
00163 
00164         dict->addParameter(ParameterDef("button_down_material", 
00165             "The material to use when the button is down."
00166             , PT_STRING),
00167             &ButtonGuiElement::msCmdButtonDownMaterial);
00168 
00169         dict->addParameter(ParameterDef("button_up_material", 
00170             "The material to use when the button is up."
00171             , PT_STRING),
00172             &ButtonGuiElement::msCmdButtonUpMaterial);
00173 
00174         dict->addParameter(ParameterDef("button_hilite_down_material", 
00175             "The material to use when the button is highlighted and down."
00176             , PT_STRING),
00177             &ButtonGuiElement::msCmdButtonHiliteDownMaterial);
00178 
00179         dict->addParameter(ParameterDef("button_hilite_up_material", 
00180             "The material to use when the button is highlighted and up."
00181             , PT_STRING),
00182             &ButtonGuiElement::msCmdButtonHiliteUpMaterial);
00183 
00184         dict->addParameter(ParameterDef("button_disabled_material", 
00185             "The material to use when the button is disabled."
00186             , PT_STRING),
00187             &ButtonGuiElement::msCmdButtonDisabledMaterial);
00188 
00189         dict->addParameter(ParameterDef("caption_colour", 
00190             "Sets the caption's font colour."
00191             , PT_STRING),
00192             &msCmdCaptionColour);
00193 
00194         dict->addParameter(ParameterDef("caption_disabled_colour", 
00195             "Sets the caption's font colour when the button is disabled."
00196             , PT_STRING),
00197             &msCmdCaptionDisabledColour);
00198 
00199         dict->addParameter(ParameterDef("caption", 
00200             "The text in the middle of the button."
00201             , PT_STRING),
00202             &ButtonGuiElement::msCmdButtonCaption);
00203     }
00204 
00205     //-----------------------------------------------------------------------
00206     void ButtonGuiElement::setDownMaterialName(const String& name)
00207     {
00208         mDownMaterialName = name;
00209     }
00210     //-----------------------------------------------------------------------
00211     void ButtonGuiElement::setUpMaterialName(const String& name)
00212     {
00213         mUpMaterialName = name;
00214         setPressed(false);
00215         updateMaterials(true);
00216     }
00217 
00218     //-----------------------------------------------------------------------
00219     void ButtonGuiElement::setHiliteDownMaterialName(const String& name)
00220     {
00221         mHiliteDownMaterialName = name;
00222     }
00223     //-----------------------------------------------------------------------
00224     void ButtonGuiElement::setHiliteUpMaterialName(const String& name)
00225     {
00226         mHiliteUpMaterialName = name;
00227     }
00228     //-----------------------------------------------------------------------
00229     void ButtonGuiElement::setDisabledMaterialName(const String& name)
00230     {
00231         mDisabledMaterialName = name;
00232     }
00233     //---------------------------------------------------------------------
00234     void ButtonGuiElement::setCaptionColour(const ColourValue& col)
00235     {
00236         mCaptionColour = col;
00237         mSetCaptionColor = true;
00238         updateMaterials();
00239     }
00240     //---------------------------------------------------------------------
00241     void ButtonGuiElement::setCaptionDisabledColour(const ColourValue& col)
00242     {
00243         mCaptionDisabledColour = col;
00244         mSetCaptionDisabledColor = true;
00245         updateMaterials();
00246     }
00247 
00248     //-----------------------------------------------------------------------
00249     void ButtonGuiElement::setButtonCaption(const String& templateName, const String& name)
00250     {
00251         if (mInsideObject)
00252         {
00253             removeChild(mInsideObject->getName());
00254             GuiManager::getSingleton().destroyGuiElement(mInsideObject);
00255             mInsideObject = NULL;
00256         }
00257         if (name == "")
00258         {
00259             return;
00260         }
00261 
00262         mInsideObject = 
00263             GuiManager::getSingleton().createGuiElementFromTemplate(templateName, "", mName + "/caption");
00264 
00265         // change left/top etc to relative
00266 //      mInsideObject->setLeft(mInsideObject->getLeft()*mWidth);
00267 //      mInsideObject->setWidth(mInsideObject->getWidth()*mWidth);
00268 //      mInsideObject->setTop(mInsideObject->getTop()*mHeight);
00269 //      mInsideObject->setWidth(mInsideObject->getHeight()*mHeight);
00270         mInsideObject->setCaption(name);
00271 
00272         // do not make this cloneable, otherwise there will be 2 copies of it when it is cloned,
00273         // one copy when the children are copied, and another copy when setButtonCaption is set.
00274         mInsideObject->setCloneable(false);
00275 
00276         addChild(mInsideObject);
00277 
00278         // to make sure everything is right (colour)
00279         updateMaterials();
00280     }
00281     //-----------------------------------------------------------------------
00282     String ButtonGuiElement::getButtonCaption() const
00283     {
00284         return (mInsideObject)?mInsideObject->getCaption():String("");
00285     }
00286 
00287     //---------------------------------------------------------------------
00288     const String& ButtonGuiElement::getDownMaterialName(void) const
00289     {
00290         return mDownMaterialName;
00291     }
00292     //---------------------------------------------------------------------
00293     const String& ButtonGuiElement::getUpMaterialName(void) const
00294     {
00295         return mUpMaterialName;
00296     }
00297 
00298     //---------------------------------------------------------------------
00299     const String& ButtonGuiElement::getHiliteDownMaterialName(void) const
00300     {
00301         return mHiliteDownMaterialName;
00302     }
00303     //---------------------------------------------------------------------
00304     const String& ButtonGuiElement::getHiliteUpMaterialName(void) const
00305     {
00306         return mHiliteUpMaterialName;
00307     }
00308     //---------------------------------------------------------------------
00309     const String& ButtonGuiElement::getDisabledMaterialName(void) const
00310     {
00311         return mDisabledMaterialName;
00312     }
00313     //---------------------------------------------------------------------
00314     ColourValue ButtonGuiElement::getCaptionColour(void) const
00315     {
00316         return mCaptionColour;
00317     }
00318     //---------------------------------------------------------------------
00319     ColourValue ButtonGuiElement::getCaptionDisabledColour(void) const
00320     {
00321         return mCaptionDisabledColour;
00322     }
00323 
00324     //---------------------------------------------------------------------
00325     //---------------------------------------------------------------------
00326     // Command objects
00327     //---------------------------------------------------------------------
00328 
00329     //-----------------------------------------------------------------------
00330     String ButtonGuiElement::CmdButtonDownMaterial::doGet(const void* target) const
00331     {
00332         // No need right now..
00333         return static_cast<const ButtonGuiElement*>(target)->getDownMaterialName();
00334     }
00335     //-----------------------------------------------------------------------
00336     void ButtonGuiElement::CmdButtonDownMaterial::doSet(void* target, const String& val)
00337     {
00338         std::vector<String> vec = StringUtil::split(val);
00339 
00340         static_cast<ButtonGuiElement*>(target)->setDownMaterialName(val);
00341     }
00342     //-----------------------------------------------------------------------
00343     String ButtonGuiElement::CmdButtonUpMaterial::doGet(const void* target) const
00344     {
00345         // No need right now..
00346         return static_cast<const ButtonGuiElement*>(target)->getUpMaterialName();
00347     }
00348     //-----------------------------------------------------------------------
00349     void ButtonGuiElement::CmdButtonUpMaterial::doSet(void* target, const String& val)
00350     {
00351         std::vector<String> vec = StringUtil::split(val);
00352 
00353         static_cast<ButtonGuiElement*>(target)->setUpMaterialName(val);
00354     }
00355     //-----------------------------------------------------------------------
00356     String ButtonGuiElement::CmdButtonHiliteDownMaterial::doGet(const void* target) const
00357     {
00358         // No need right now..
00359         return static_cast<const ButtonGuiElement*>(target)->getHiliteDownMaterialName();
00360     }
00361     //-----------------------------------------------------------------------
00362     void ButtonGuiElement::CmdButtonHiliteDownMaterial::doSet(void* target, const String& val)
00363     {
00364         std::vector<String> vec = StringUtil::split(val);
00365 
00366         static_cast<ButtonGuiElement*>(target)->setHiliteDownMaterialName(val);
00367     }
00368     //-----------------------------------------------------------------------
00369     String ButtonGuiElement::CmdButtonHiliteUpMaterial::doGet(const void* target) const
00370     {
00371         // No need right now..
00372         return static_cast<const ButtonGuiElement*>(target)->getHiliteUpMaterialName();
00373     }
00374     //-----------------------------------------------------------------------
00375     void ButtonGuiElement::CmdButtonHiliteUpMaterial::doSet(void* target, const String& val)
00376     {
00377         std::vector<String> vec = StringUtil::split(val);
00378 
00379         static_cast<ButtonGuiElement*>(target)->setHiliteUpMaterialName(val);
00380     }
00381     //-----------------------------------------------------------------------
00382     String ButtonGuiElement::CmdButtonDisabledMaterial::doGet(const void* target) const
00383     {
00384         // No need right now..
00385         return static_cast<const ButtonGuiElement*>(target)->getDisabledMaterialName();
00386     }
00387     //-----------------------------------------------------------------------
00388     void ButtonGuiElement::CmdButtonDisabledMaterial::doSet(void* target, const String& val)
00389     {
00390         std::vector<String> vec = StringUtil::split(val);
00391 
00392         static_cast<ButtonGuiElement*>(target)->setDisabledMaterialName(val);
00393     }
00394     //-----------------------------------------------------------------------
00395     String ButtonGuiElement::CmdCaptionColour::doGet(const void* target) const
00396     {
00397         return StringConverter::toString(static_cast<const ButtonGuiElement*>(target)->getCaptionColour());
00398     }
00399     //-----------------------------------------------------------------------
00400     void ButtonGuiElement::CmdCaptionColour::doSet(void* target, const String& val)
00401     {
00402         static_cast<ButtonGuiElement*>(target)->setCaptionColour(StringConverter::parseColourValue(val));
00403     }
00404     //-----------------------------------------------------------------------
00405     String ButtonGuiElement::CmdCaptionDisabledColour::doGet(const void* target) const
00406     {
00407         return StringConverter::toString(static_cast<const ButtonGuiElement*>(target)->getCaptionDisabledColour());
00408     }
00409     //-----------------------------------------------------------------------
00410     void ButtonGuiElement::CmdCaptionDisabledColour::doSet(void* target, const String& val)
00411     {
00412         static_cast<ButtonGuiElement*>(target)->setCaptionDisabledColour(StringConverter::parseColourValue(val));
00413     }
00414     //-----------------------------------------------------------------------
00415     String ButtonGuiElement::CmdButtonCaption::doGet(const void* target) const
00416     {
00417         // No need right now..
00418         return static_cast<const ButtonGuiElement*>(target)->getButtonCaption();
00419     }
00420     //-----------------------------------------------------------------------
00421     void ButtonGuiElement::CmdButtonCaption::doSet(void* target, const String& val)
00422     {
00423         std::vector<String> vec = StringUtil::split(val, "\t\n ", 1);
00424 
00425 
00426         if (vec.size() < 2)
00427         {
00428             static_cast<ButtonGuiElement*>(target)->setButtonCaption(val, String(""));
00429         }
00430         else
00431         {
00432             static_cast<ButtonGuiElement*>(target)->setButtonCaption(vec[0], vec[1]);
00433         }
00434     }
00435     //---------------------------------------------------------------------
00436     const String& ButtonGuiElement::getTypeName(void) const
00437     {
00438         return msTypeName;
00439     }
00440 
00441 }
00442 

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