MyGUI 3.4.1
MyGUI_Button.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
8#include "MyGUI_Button.h"
10#include "MyGUI_ImageBox.h"
11#include "MyGUI_InputManager.h"
12
13namespace MyGUI
14{
15
17 mIsMousePressed(false),
18 mIsKeyFocus(false),
19 mIsMouseFocus(false),
20 mStateSelected(false),
21 mImage(nullptr),
22 mModeImage(false)
23 {
24 }
25
27 {
29
31 assignWidget(mImage, "Image");
32 }
33
35 {
36 mImage = nullptr;
37
39 }
40
42 {
43 _setKeyFocus(true);
44
46 }
47
49 {
50 _setKeyFocus(false);
51
53 }
54
56 {
57 _setMouseFocus(true);
58
60 }
61
63 {
64 _setMouseFocus(false);
65
67 }
68
69 void Button::onMouseButtonPressed(int _left, int _top, MouseButton _id)
70 {
71 if (_id == MouseButton::Left)
72 {
73 mIsMousePressed = true;
74 updateButtonState();
75 }
76
77 Base::onMouseButtonPressed(_left, _top, _id);
78 }
79
80 void Button::onMouseButtonReleased(int _left, int _top, MouseButton _id)
81 {
82 if (_id == MouseButton::Left)
83 {
84 mIsMousePressed = false;
85 updateButtonState();
86 }
87
88 Base::onMouseButtonReleased(_left, _top, _id);
89 }
90
91 void Button::updateButtonState()
92 {
93 if (mStateSelected)
94 {
96 {
97 if (!_setState("disabled_checked"))
98 _setState("disabled");
99 }
100 else if (mIsMousePressed)
101 {
102 if (!_setState("pushed_checked"))
103 _setState("pushed");
104 }
105 else if (mIsMouseFocus || mIsKeyFocus)
106 {
107 if (!_setState("highlighted_checked"))
108 _setState("pushed");
109 }
110 else
111 {
112 if (!_setState("normal_checked"))
113 _setState("pushed");
114 }
115 }
116 else
117 {
118 if (!getInheritedEnabled())
119 _setState("disabled");
120 else if (mIsMousePressed)
121 _setState("pushed");
122 else if (mIsMouseFocus || mIsKeyFocus)
123 _setState("highlighted");
124 else
125 _setState("normal");
126 }
127 }
128
129 void Button::setStateSelected(bool _check)
130 {
131 if (mStateSelected == _check)
132 return;
133
134 mStateSelected = _check;
135 updateButtonState();
136 }
137
138 void Button::_setKeyFocus(bool _focus)
139 {
140 mIsKeyFocus = _focus;
141 updateButtonState();
142 }
143
144 void Button::_setMouseFocus(bool _focus)
145 {
146 mIsMouseFocus = _focus;
147 updateButtonState();
148 }
149
151 {
152 updateButtonState();
153 if (!getInheritedEnabled())
154 {
155 mIsKeyFocus = false;
156 mIsMouseFocus = false;
157 }
158 }
159
160 void Button::setModeImage(bool _value)
161 {
162 mModeImage = _value;
163 updateButtonState();
164 }
165
166 bool Button::_setState(const std::string& _value)
167 {
168 if (mModeImage)
169 {
170 if (mImage)
171 mImage->setItemName(_value);
172
173 _setWidgetState(_value);
174 return true;
175 }
176
177 return _setWidgetState(_value);
178 }
179
180 void Button::setImageResource(const std::string& _name)
181 {
182 if (mImage)
183 mImage->setItemResource(_name);
184 updateButtonState();
185 }
186
187 void Button::setImageGroup(const std::string& _name)
188 {
189 if (mImage)
190 mImage->setItemGroup(_name);
191 updateButtonState();
192 }
193
194 void Button::setImageName(const std::string& _name)
195 {
196 if (mImage)
197 mImage->setItemName(_name);
198 }
199
200 void Button::setPropertyOverride(const std::string& _key, const std::string& _value)
201 {
203 if (_key == "StateSelected")
204 setStateSelected(utility::parseValue<bool>(_value));
205
207 else if (_key == "ModeImage")
208 setModeImage(utility::parseValue<bool>(_value));
209
211 else if (_key == "ImageResource")
212 setImageResource(_value);
213
215 else if (_key == "ImageGroup")
216 setImageGroup(_value);
217
219 else if (_key == "ImageName")
220 setImageName(_value);
221
222 else
223 {
224 Base::setPropertyOverride(_key, _value);
225 return;
226 }
227
228 eventChangeProperty(this, _key, _value);
229 }
230
232 {
233 return mStateSelected;
234 }
235
237 {
238 return mModeImage;
239 }
240
242 {
243 return mImage;
244 }
245
246} // namespace MyGUI
void setImageGroup(const std::string &_name)
void setImageName(const std::string &_name)
void onMouseSetFocus(Widget *_old) override
void _setMouseFocus(bool _focus)
void onMouseButtonReleased(int _left, int _top, MouseButton _id) override
void onKeyLostFocus(Widget *_new) override
void onMouseButtonPressed(int _left, int _top, MouseButton _id) override
void setStateSelected(bool _value)
Set button selected state.
void onKeySetFocus(Widget *_old) override
void setPropertyOverride(const std::string &_key, const std::string &_value) override
bool getStateSelected() const
Get buton selected.
void setImageResource(const std::string &_name)
bool getModeImage() const
ImageBox * _getImageBox() const
void shutdownOverride() override
void baseUpdateEnable() override
void initialiseOverride() override
void setModeImage(bool _value)
void onMouseLostFocus(Widget *_new) override
bool _setState(const std::string &_value)
void _setKeyFocus(bool _focus)
widget description should be here.
void setItemName(const std::string &_value)
bool setItemResource(const std::string &_name)
void setItemGroup(const std::string &_value)
widget description should be here.
Definition: MyGUI_Widget.h:37
bool getInheritedEnabled() const
EventHandle_WidgetStringString eventChangeProperty
Definition: MyGUI_Widget.h:267
bool _setWidgetState(const std::string &_value)
void assignWidget(T *&_widget, const std::string &_name)
Definition: MyGUI_Widget.h:335