• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

MyGUI_Button.cpp

Go to the documentation of this file.
00001 
00007 /*
00008     This file is part of MyGUI.
00009 
00010     MyGUI is free software: you can redistribute it and/or modify
00011     it under the terms of the GNU Lesser General Public License as published by
00012     the Free Software Foundation, either version 3 of the License, or
00013     (at your option) any later version.
00014 
00015     MyGUI is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU Lesser General Public License for more details.
00019 
00020     You should have received a copy of the GNU Lesser General Public License
00021     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00022 */
00023 #include "MyGUI_Precompiled.h"
00024 #include "MyGUI_Button.h"
00025 #include "MyGUI_ResourceSkin.h"
00026 #include "MyGUI_StaticImage.h"
00027 #include "MyGUI_InputManager.h"
00028 
00029 namespace MyGUI
00030 {
00031 
00032     Button::Button() :
00033         mIsMousePressed(false),
00034         mIsMouseFocus(false),
00035         mIsStateCheck(false),
00036         mImage(nullptr)
00037     {
00038     }
00039 
00040     void Button::_initialise(WidgetStyle _style, const IntCoord& _coord, Align _align, ResourceSkin* _info, WidgetPtr _parent, ICroppedRectangle * _croppedParent, IWidgetCreator * _creator, const std::string& _name)
00041     {
00042         Base::_initialise(_style, _coord, _align, _info, _parent, _croppedParent, _creator, _name);
00043 
00044         initialiseWidgetSkin(_info);
00045     }
00046 
00047     Button::~Button()
00048     {
00049         shutdownWidgetSkin();
00050     }
00051 
00052     void Button::baseChangeWidgetSkin(ResourceSkin* _info)
00053     {
00054         shutdownWidgetSkin();
00055         Base::baseChangeWidgetSkin(_info);
00056         initialiseWidgetSkin(_info);
00057     }
00058 
00059     void Button::initialiseWidgetSkin(ResourceSkin* _info)
00060     {
00061         // парсим свойства
00062         const MapString& properties = _info->getProperties();
00063         if (!properties.empty())
00064         {
00065             MapString::const_iterator iter = properties.find("ButtonPressed");
00066             if (iter != properties.end()) setButtonPressed(utility::parseBool(iter->second));
00067             iter = properties.find("StateCheck");
00068             if (iter != properties.end()) setStateCheck(utility::parseBool(iter->second));
00069         }
00070 
00071         for (VectorWidgetPtr::iterator iter=mWidgetChildSkin.begin(); iter!=mWidgetChildSkin.end(); ++iter)
00072         {
00073             if (*(*iter)->_getInternalData<std::string>() == "Image")
00074             {
00075                 MYGUI_DEBUG_ASSERT( ! mImage, "widget already assigned");
00076                 mImage = (*iter)->castType<StaticImage>();
00077             }
00078         }
00079     }
00080 
00081     void Button::shutdownWidgetSkin()
00082     {
00083         mImage = nullptr;
00084     }
00085 
00086     void Button::onMouseSetFocus(WidgetPtr _old)
00087     {
00088         _setMouseFocus(true);
00089 
00090         Base::onMouseSetFocus(_old);
00091     }
00092 
00093     void Button::onMouseLostFocus(WidgetPtr _new)
00094     {
00095         _setMouseFocus(false);
00096 
00097         Base::onMouseLostFocus(_new);
00098     }
00099 
00100     void Button::onMouseButtonPressed(int _left, int _top, MouseButton _id)
00101     {
00102         if (_id == MouseButton::Left)
00103         {
00104             mIsMousePressed = true;
00105             updateButtonState();
00106         }
00107 
00108         Base::onMouseButtonPressed(_left, _top, _id);
00109     }
00110 
00111     void Button::onMouseButtonReleased(int _left, int _top, MouseButton _id)
00112     {
00113         if (_id == MouseButton::Left)
00114         {
00115             mIsMousePressed = false;
00116             updateButtonState();
00117         }
00118 
00119         Base::onMouseButtonReleased(_left, _top, _id);
00120     }
00121 
00122     void Button::setImageIndex(size_t _index)
00123     {
00124         if (mImage) mImage->setImageIndex(_index);
00125     }
00126 
00127     size_t Button::getImageIndex()
00128     {
00129         if (mImage) return mImage->getImageIndex();
00130         return ITEM_NONE;
00131     }
00132 
00133     void Button::updateButtonState()
00134     {
00135         if (mIsStateCheck)
00136         {
00137             if (!mEnabled) { if (!setState("disabled_checked")) setState("disabled"); }
00138             else if (mIsMousePressed) { if (!setState("pushed_checked")) setState("pushed"); }
00139             else if (mIsMouseFocus) { if (!setState("highlighted_checked")) setState("pushed"); }
00140             else setState("normal_checked");
00141         }
00142         else
00143         {
00144             if (!mEnabled) setState("disabled");
00145             else if (mIsMousePressed) setState("pushed");
00146             else if (mIsMouseFocus) setState("highlighted");
00147             else setState("normal");
00148         }
00149     }
00150 
00151     void Button::setStateCheck(bool _check)
00152     {
00153         if (mIsStateCheck == _check) return;
00154         mIsStateCheck = _check;
00155         updateButtonState();
00156     }
00157 
00158     void Button::_setMouseFocus(bool _focus)
00159     {
00160         mIsMouseFocus = _focus;
00161         updateButtonState();
00162     }
00163 
00164     void Button::setProperty(const std::string& _key, const std::string& _value)
00165     {
00167         if (_key == "Button_Pressed") setButtonPressed(utility::parseValue<bool>(_value));
00168         else Base::setProperty(_key, _value);
00169     }
00170 
00171     void Button::baseUpdateEnable()
00172     {
00173         updateButtonState();
00174         if (!mEnabled)
00175         {
00176             mIsMouseFocus = false;
00177         }
00178     }
00179 
00180 } // namespace MyGUI

Generated on Sun Jan 30 2011 for MyGUI by  doxygen 1.7.1