MyGUI  3.0.1
MyGUI_Button.cpp
Go to the documentation of this file.
1 
7 /*
8  This file is part of MyGUI.
9 
10  MyGUI is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MyGUI is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #include "MyGUI_Precompiled.h"
24 #include "MyGUI_Button.h"
25 #include "MyGUI_ResourceSkin.h"
26 #include "MyGUI_StaticImage.h"
27 #include "MyGUI_InputManager.h"
28 
29 namespace MyGUI
30 {
31 
33  mIsMousePressed(false),
34  mIsMouseFocus(false),
35  mIsStateCheck(false),
36  mImage(nullptr),
37  mModeImage(false)
38  {
39  }
40 
41  void Button::_initialise(WidgetStyle _style, const IntCoord& _coord, Align _align, ResourceSkin* _info, Widget* _parent, ICroppedRectangle * _croppedParent, IWidgetCreator * _creator, const std::string& _name)
42  {
43  Base::_initialise(_style, _coord, _align, _info, _parent, _croppedParent, _creator, _name);
44 
45  initialiseWidgetSkin(_info);
46  }
47 
49  {
50  shutdownWidgetSkin();
51  }
52 
54  {
55  shutdownWidgetSkin();
57  initialiseWidgetSkin(_info);
58  }
59 
60  void Button::initialiseWidgetSkin(ResourceSkin* _info)
61  {
62  // парсим свойства
63  const MapString& properties = _info->getProperties();
64  if (!properties.empty())
65  {
66  MapString::const_iterator iter = properties.find("ButtonPressed");
67  if (iter != properties.end()) setButtonPressed(utility::parseValue<bool>(iter->second));
68  iter = properties.find("StateCheck");
69  if (iter != properties.end()) setStateCheck(utility::parseValue<bool>(iter->second));
70  iter = properties.find("ModeImage");
71  if (iter != properties.end()) setModeImage(utility::parseValue<bool>(iter->second));
72  }
73 
74  for (VectorWidgetPtr::iterator iter=mWidgetChildSkin.begin(); iter!=mWidgetChildSkin.end(); ++iter)
75  {
76  if (*(*iter)->_getInternalData<std::string>() == "Image")
77  {
78  MYGUI_DEBUG_ASSERT( ! mImage, "widget already assigned");
79  mImage = (*iter)->castType<StaticImage>();
80  }
81  }
82  }
83 
84  void Button::shutdownWidgetSkin()
85  {
86  mImage = nullptr;
87  }
88 
90  {
91  _setMouseFocus(true);
92 
94  }
95 
97  {
98  _setMouseFocus(false);
99 
101  }
102 
103  void Button::onMouseButtonPressed(int _left, int _top, MouseButton _id)
104  {
105  if (_id == MouseButton::Left)
106  {
107  mIsMousePressed = true;
108  updateButtonState();
109  }
110 
111  Base::onMouseButtonPressed(_left, _top, _id);
112  }
113 
114  void Button::onMouseButtonReleased(int _left, int _top, MouseButton _id)
115  {
116  if (_id == MouseButton::Left)
117  {
118  mIsMousePressed = false;
119  updateButtonState();
120  }
121 
122  Base::onMouseButtonReleased(_left, _top, _id);
123  }
124 
125  void Button::setImageIndex(size_t _index)
126  {
127  if (mImage) mImage->setImageIndex(_index);
128  }
129 
131  {
132  if (mImage) return mImage->getImageIndex();
133  return ITEM_NONE;
134  }
135 
136  void Button::updateButtonState()
137  {
138  if (mIsStateCheck)
139  {
140  if (!mEnabled) { if (!_setState("disabled_checked")) _setState("disabled"); }
141  else if (mIsMousePressed) { if (!_setState("pushed_checked")) _setState("pushed"); }
142  else if (mIsMouseFocus) { if (!_setState("highlighted_checked")) _setState("pushed"); }
143  else _setState("normal_checked");
144  }
145  else
146  {
147  if (!mEnabled) _setState("disabled");
148  else if (mIsMousePressed) _setState("pushed");
149  else if (mIsMouseFocus) _setState("highlighted");
150  else _setState("normal");
151  }
152  }
153 
154  void Button::setStateCheck(bool _check)
155  {
156  if (mIsStateCheck == _check) return;
157  mIsStateCheck = _check;
158  updateButtonState();
159  }
160 
161  void Button::_setMouseFocus(bool _focus)
162  {
163  mIsMouseFocus = _focus;
164  updateButtonState();
165  }
166 
167  void Button::setProperty(const std::string& _key, const std::string& _value)
168  {
170  if (_key == "Button_Pressed") setButtonPressed(utility::parseValue<bool>(_value));
171  else if (_key == "Button_ModeImage") setModeImage(utility::parseValue<bool>(_value));
172  else if (_key == "Button_ImageResource") setImageResource(_value);
173  else
174  {
175  Base::setProperty(_key, _value);
176  return;
177  }
178  eventChangeProperty(this, _key, _value);
179  }
180 
182  {
183  updateButtonState();
184  if (!mEnabled)
185  {
186  mIsMouseFocus = false;
187  }
188  }
189 
190  void Button::setModeImage(bool _value)
191  {
192  mModeImage = _value;
193  updateButtonState();
194  }
195 
196  bool Button::_setState(const std::string& _value)
197  {
198  if (mModeImage)
199  {
200  if (mImage)
201  mImage->setItemName(_value);
202 
203  setState(_value);
204  return true;
205  }
206 
207  return setState(_value);
208  }
209 
210  void Button::setImageResource(const std::string& _name)
211  {
212  if (mImage)
213  mImage->setItemResource(_name);
214  updateButtonState();
215  }
216 
217 } // namespace MyGUI