MyGUI 3.2.0

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files
- Examples

- File List
- Globals

MyGUI_Button.cpp
Go to the documentation of this file.
1
6 /*
7 This file is part of MyGUI.
8
9 MyGUI is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 MyGUI is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include "MyGUI_Precompiled.h"
23 #include "MyGUI_Button.h"
24 #include "MyGUI_ResourceSkin.h"
25 #include "MyGUI_ImageBox.h"
26 #include "MyGUI_InputManager.h"
27
28 namespace MyGUI
29 {
30
31 Button::Button() :
32 mIsMousePressed(false),
33 mIsMouseFocus(false),
34 mStateSelected(false),
35 mImage(nullptr),
36 mModeImage(false)
37 {
38 }
39
40 void Button::initialiseOverride()
41 {
42 Base::initialiseOverride();
43
44 assignWidget(mImage, "Image");
45 }
46
47 void Button::shutdownOverride()
48 {
49 mImage = nullptr;
50
51 Base::shutdownOverride();
52 }
53
54 void Button::onMouseSetFocus(Widget* _old)
55 {
56 _setMouseFocus(true);
57
58 Base::onMouseSetFocus(_old);
59 }
60
61 void Button::onMouseLostFocus(Widget* _new)
62 {
63 _setMouseFocus(false);
64
65 Base::onMouseLostFocus(_new);
66 }
67
68 void Button::onMouseButtonPressed(int _left, int _top, MouseButton _id)
69 {
70 if (_id == MouseButton::Left)
71 {
72 mIsMousePressed = true;
73 updateButtonState();
74 }
75
76 Base::onMouseButtonPressed(_left, _top, _id);
77 }
78
79 void Button::onMouseButtonReleased(int _left, int _top, MouseButton _id)
80 {
81 if (_id == MouseButton::Left)
82 {
83 mIsMousePressed = false;
84 updateButtonState();
85 }
86
87 Base::onMouseButtonReleased(_left, _top, _id);
88 }
89
90 void Button::updateButtonState()
91 {
92 if (mStateSelected)
93 {
94 if (!getEnabled())
95 {
96 if (!_setState("disabled_checked"))
97 _setState("disabled");
98 }
99 else if (mIsMousePressed)
100 {
101 if (!_setState("pushed_checked"))
102 _setState("pushed");
103 }
104 else if (mIsMouseFocus)
105 {
106 if (!_setState("highlighted_checked"))
107 _setState("pushed");
108 }
109 else
110 _setState("normal_checked");
111 }
112 else
113 {
114 if (!getEnabled())
115 _setState("disabled");
116 else if (mIsMousePressed)
117 _setState("pushed");
118 else if (mIsMouseFocus)
119 _setState("highlighted");
120 else
121 _setState("normal");
122 }
123 }
124
125 void Button::setStateSelected(bool _check)
126 {
127 if (mStateSelected == _check)
128 return;
129
130 mStateSelected = _check;
131 updateButtonState();
132 }
133
134 void Button::_setMouseFocus(bool _focus)
135 {
136 mIsMouseFocus = _focus;
137 updateButtonState();
138 }
139
140 void Button::baseUpdateEnable()
141 {
142 updateButtonState();
143 if (!getEnabled())
144 {
145 mIsMouseFocus = false;
146 }
147 }
148
149 void Button::setModeImage(bool _value)
150 {
151 mModeImage = _value;
152 updateButtonState();
153 }
154
155 bool Button::_setState(const std::string& _value)
156 {
157 if (mModeImage)
158 {
159 if (mImage)
160 mImage->setItemName(_value);
161
162 _setWidgetState(_value);
163 return true;
164 }
165
166 return _setWidgetState(_value);
167 }
168
169 void Button::setImageResource(const std::string& _name)
170 {
171 if (mImage)
172 mImage->setItemResource(_name);
173 updateButtonState();
174 }
175
176 void Button::setImageGroup(const std::string& _name)
177 {
178 if (mImage)
179 mImage->setItemGroup(_name);
180 updateButtonState();
181 }
182
183 void Button::setImageName(const std::string& _name)
184 {
185 if (mImage)
186 mImage->setItemName(_name);
187 }
188
189 void Button::setPropertyOverride(const std::string& _key, const std::string& _value)
190 {
192 if (_key == "StateSelected")
193 setStateSelected(utility::parseValue<bool>(_value));
194 else if (_key == "ModeImage")
195 setModeImage(utility::parseValue<bool>(_value));
196 else if (_key == "ImageResource")
197 setImageResource(_value);
198 else if (_key == "ImageGroup")
199 setImageGroup(_value);
200 else if (_key == "ImageName")
201 setImageName(_value);
202 else
203 {
204 Base::setPropertyOverride(_key, _value);
205 return;
206 }
207 eventChangeProperty(this, _key, _value);
208 }
209
210 bool Button::getStateSelected() const
211 {
212 return mStateSelected;
213 }
214
215 bool Button::getModeImage() const
216 {
217 return mModeImage;
218 }
219
220 ImageBox* Button::_getImageBox()
221 {
222 return mImage;
223 }
224
225 } // namespace MyGUI
MyGUI::Widget
Definition: MyGUI_Widget.h:41
MyGUI::Button::setPropertyOverride
virtual void setPropertyOverride(const std::string &_key, const std::string &_value)
Definition: MyGUI_Button.cpp:189
MyGUI::Button::onMouseButtonPressed
virtual void onMouseButtonPressed(int _left, int _top, MouseButton _id)
Definition: MyGUI_Button.cpp:68
MyGUI::ImageBox
Definition: MyGUI_ImageBox.h:33
MyGUI_Precompiled.h
MyGUI::Button::setImageGroup
void setImageGroup(const std::string &_name)
Definition: MyGUI_Button.cpp:176
MyGUI::Button::onMouseButtonReleased
virtual void onMouseButtonReleased(int _left, int _top, MouseButton _id)
Definition: MyGUI_Button.cpp:79
MyGUI::Button::getStateSelected
bool getStateSelected() const
Get buton selected.
Definition: MyGUI_Button.cpp:210
MyGUI::Button::onMouseLostFocus
virtual void onMouseLostFocus(Widget *_new)
Definition: MyGUI_Button.cpp:61
MyGUI::Button::baseUpdateEnable
virtual void baseUpdateEnable()
Definition: MyGUI_Button.cpp:140
MyGUI::Widget::assignWidget
void assignWidget(T *&_widget, const std::string &_name)
Definition: MyGUI_Widget.h:324
MyGUI::Button::_setMouseFocus
void _setMouseFocus(bool _focus)
Definition: MyGUI_Button.cpp:134
MyGUI_Button.h
nullptr
#define nullptr
Definition: MyGUI_Prerequest.h:47
MyGUI::Button::_getImageBox
ImageBox * _getImageBox()
Definition: MyGUI_Button.cpp:220
MyGUI::MouseButton
Definition: MyGUI_MouseButton.h:30
MyGUI::Button::_setState
bool _setState(const std::string &_value)
Definition: MyGUI_Button.cpp:155
MyGUI::Widget::_setWidgetState
bool _setWidgetState(const std::string &_value)
Definition: MyGUI_Widget.cpp:389
MyGUI::Widget::eventChangeProperty
EventHandle_WidgetStringString eventChangeProperty
Definition: MyGUI_Widget.h:271
MyGUI::Button::shutdownOverride
virtual void shutdownOverride()
Definition: MyGUI_Button.cpp:47
MyGUI::ImageBox::setItemName
void setItemName(const std::string &_value)
Definition: MyGUI_ImageBox.cpp:426
MyGUI::Button::setModeImage
void setModeImage(bool _value)
Definition: MyGUI_Button.cpp:149
MyGUI::Widget::getEnabled
bool getEnabled() const
Definition: MyGUI_Widget.cpp:1255
MyGUI_InputManager.h
MyGUI_ResourceSkin.h
MyGUI::Button::onMouseSetFocus
virtual void onMouseSetFocus(Widget *_old)
Definition: MyGUI_Button.cpp:54
MyGUI::ImageBox::setItemResource
bool setItemResource(const std::string &_name)
Definition: MyGUI_ImageBox.cpp:366
MyGUI::Button::initialiseOverride
virtual void initialiseOverride()
Definition: MyGUI_Button.cpp:40
MyGUI::ImageBox::setItemGroup
void setItemGroup(const std::string &_value)
Definition: MyGUI_ImageBox.cpp:416
MyGUI::MouseButton::Left
Definition: MyGUI_MouseButton.h:36
MyGUI::Button::setImageName
void setImageName(const std::string &_name)
Definition: MyGUI_Button.cpp:183
MyGUI::Button::setImageResource
void setImageResource(const std::string &_name)
Definition: MyGUI_Button.cpp:169
MyGUI_ImageBox.h
MyGUI::Button::getModeImage
bool getModeImage() const
Definition: MyGUI_Button.cpp:215
MyGUI::Button::Button
Button()
Definition: MyGUI_Button.cpp:31
MyGUI::Button::setStateSelected
void setStateSelected(bool _value)
Set button selected state.
Definition: MyGUI_Button.cpp:125