MyGUI 3.0.1
|
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 mModeImage(false) 00038 { 00039 } 00040 00041 void Button::_initialise(WidgetStyle _style, const IntCoord& _coord, Align _align, ResourceSkin* _info, Widget* _parent, ICroppedRectangle * _croppedParent, IWidgetCreator * _creator, const std::string& _name) 00042 { 00043 Base::_initialise(_style, _coord, _align, _info, _parent, _croppedParent, _creator, _name); 00044 00045 initialiseWidgetSkin(_info); 00046 } 00047 00048 Button::~Button() 00049 { 00050 shutdownWidgetSkin(); 00051 } 00052 00053 void Button::baseChangeWidgetSkin(ResourceSkin* _info) 00054 { 00055 shutdownWidgetSkin(); 00056 Base::baseChangeWidgetSkin(_info); 00057 initialiseWidgetSkin(_info); 00058 } 00059 00060 void Button::initialiseWidgetSkin(ResourceSkin* _info) 00061 { 00062 // парсим свойства 00063 const MapString& properties = _info->getProperties(); 00064 if (!properties.empty()) 00065 { 00066 MapString::const_iterator iter = properties.find("ButtonPressed"); 00067 if (iter != properties.end()) setButtonPressed(utility::parseValue<bool>(iter->second)); 00068 iter = properties.find("StateCheck"); 00069 if (iter != properties.end()) setStateCheck(utility::parseValue<bool>(iter->second)); 00070 iter = properties.find("ModeImage"); 00071 if (iter != properties.end()) setModeImage(utility::parseValue<bool>(iter->second)); 00072 } 00073 00074 for (VectorWidgetPtr::iterator iter=mWidgetChildSkin.begin(); iter!=mWidgetChildSkin.end(); ++iter) 00075 { 00076 if (*(*iter)->_getInternalData<std::string>() == "Image") 00077 { 00078 MYGUI_DEBUG_ASSERT( ! mImage, "widget already assigned"); 00079 mImage = (*iter)->castType<StaticImage>(); 00080 } 00081 } 00082 } 00083 00084 void Button::shutdownWidgetSkin() 00085 { 00086 mImage = nullptr; 00087 } 00088 00089 void Button::onMouseSetFocus(Widget* _old) 00090 { 00091 _setMouseFocus(true); 00092 00093 Base::onMouseSetFocus(_old); 00094 } 00095 00096 void Button::onMouseLostFocus(Widget* _new) 00097 { 00098 _setMouseFocus(false); 00099 00100 Base::onMouseLostFocus(_new); 00101 } 00102 00103 void Button::onMouseButtonPressed(int _left, int _top, MouseButton _id) 00104 { 00105 if (_id == MouseButton::Left) 00106 { 00107 mIsMousePressed = true; 00108 updateButtonState(); 00109 } 00110 00111 Base::onMouseButtonPressed(_left, _top, _id); 00112 } 00113 00114 void Button::onMouseButtonReleased(int _left, int _top, MouseButton _id) 00115 { 00116 if (_id == MouseButton::Left) 00117 { 00118 mIsMousePressed = false; 00119 updateButtonState(); 00120 } 00121 00122 Base::onMouseButtonReleased(_left, _top, _id); 00123 } 00124 00125 void Button::setImageIndex(size_t _index) 00126 { 00127 if (mImage) mImage->setImageIndex(_index); 00128 } 00129 00130 size_t Button::getImageIndex() 00131 { 00132 if (mImage) return mImage->getImageIndex(); 00133 return ITEM_NONE; 00134 } 00135 00136 void Button::updateButtonState() 00137 { 00138 if (mIsStateCheck) 00139 { 00140 if (!mEnabled) { if (!_setState("disabled_checked")) _setState("disabled"); } 00141 else if (mIsMousePressed) { if (!_setState("pushed_checked")) _setState("pushed"); } 00142 else if (mIsMouseFocus) { if (!_setState("highlighted_checked")) _setState("pushed"); } 00143 else _setState("normal_checked"); 00144 } 00145 else 00146 { 00147 if (!mEnabled) _setState("disabled"); 00148 else if (mIsMousePressed) _setState("pushed"); 00149 else if (mIsMouseFocus) _setState("highlighted"); 00150 else _setState("normal"); 00151 } 00152 } 00153 00154 void Button::setStateCheck(bool _check) 00155 { 00156 if (mIsStateCheck == _check) return; 00157 mIsStateCheck = _check; 00158 updateButtonState(); 00159 } 00160 00161 void Button::_setMouseFocus(bool _focus) 00162 { 00163 mIsMouseFocus = _focus; 00164 updateButtonState(); 00165 } 00166 00167 void Button::setProperty(const std::string& _key, const std::string& _value) 00168 { 00170 if (_key == "Button_Pressed") setButtonPressed(utility::parseValue<bool>(_value)); 00171 else if (_key == "Button_ModeImage") setModeImage(utility::parseValue<bool>(_value)); 00172 else if (_key == "Button_ImageResource") setImageResource(_value); 00173 else 00174 { 00175 Base::setProperty(_key, _value); 00176 return; 00177 } 00178 eventChangeProperty(this, _key, _value); 00179 } 00180 00181 void Button::baseUpdateEnable() 00182 { 00183 updateButtonState(); 00184 if (!mEnabled) 00185 { 00186 mIsMouseFocus = false; 00187 } 00188 } 00189 00190 void Button::setModeImage(bool _value) 00191 { 00192 mModeImage = _value; 00193 updateButtonState(); 00194 } 00195 00196 bool Button::_setState(const std::string& _value) 00197 { 00198 if (mModeImage) 00199 { 00200 if (mImage) 00201 mImage->setItemName(_value); 00202 00203 setState(_value); 00204 return true; 00205 } 00206 00207 return setState(_value); 00208 } 00209 00210 void Button::setImageResource(const std::string& _name) 00211 { 00212 if (mImage) 00213 mImage->setItemResource(_name); 00214 updateButtonState(); 00215 } 00216 00217 } // namespace MyGUI