MyGUI: MyGUI_Button.cpp Source File

MyGUI 3.2.2

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"
9 #include "MyGUI_ResourceSkin.h"
10 #include "MyGUI_ImageBox.h"
11 #include "MyGUI_InputManager.h"
12
13 namespace MyGUI
14 {
15
16 Button::Button() :
17 mIsMousePressed(false),
18 mIsMouseFocus(false),
19 mStateSelected(false),
20 mImage(nullptr),
21 mModeImage(false)
22 {
23 }
24
25 void Button::initialiseOverride()
26 {
27 Base::initialiseOverride();
28
30 assignWidget(mImage, "Image");
31 }
32
33 void Button::shutdownOverride()
34 {
35 mImage = nullptr;
36
37 Base::shutdownOverride();
38 }
39
40 void Button::onMouseSetFocus(Widget* _old)
41 {
42 _setMouseFocus(true);
43
44 Base::onMouseSetFocus(_old);
45 }
46
47 void Button::onMouseLostFocus(Widget* _new)
48 {
49 _setMouseFocus(false);
50
51 Base::onMouseLostFocus(_new);
52 }
53
54 void Button::onMouseButtonPressed(int _left, int _top, MouseButton _id)
55 {
56 if (_id == MouseButton::Left)
57 {
58 mIsMousePressed = true;
59 updateButtonState();
60 }
61
62 Base::onMouseButtonPressed(_left, _top, _id);
63 }
64
65 void Button::onMouseButtonReleased(int _left, int _top, MouseButton _id)
66 {
67 if (_id == MouseButton::Left)
68 {
69 mIsMousePressed = false;
70 updateButtonState();
71 }
72
73 Base::onMouseButtonReleased(_left, _top, _id);
74 }
75
76 void Button::updateButtonState()
77 {
78 if (mStateSelected)
79 {
80 if (!getInheritedEnabled())
81 {
82 if (!_setState("disabled_checked"))
83 _setState("disabled");
84 }
85 else if (mIsMousePressed)
86 {
87 if (!_setState("pushed_checked"))
88 _setState("pushed");
89 }
90 else if (mIsMouseFocus)
91 {
92 if (!_setState("highlighted_checked"))
93 _setState("pushed");
94 }
95 else
96 _setState("normal_checked");
97 }
98 else
99 {
100 if (!getInheritedEnabled())
101 _setState("disabled");
102 else if (mIsMousePressed)
103 _setState("pushed");
104 else if (mIsMouseFocus)
105 _setState("highlighted");
106 else
107 _setState("normal");
108 }
109 }
110
111 void Button::setStateSelected(bool _check)
112 {
113 if (mStateSelected == _check)
114 return;
115
116 mStateSelected = _check;
117 updateButtonState();
118 }
119
120 void Button::_setMouseFocus(bool _focus)
121 {
122 mIsMouseFocus = _focus;
123 updateButtonState();
124 }
125
126 void Button::baseUpdateEnable()
127 {
128 updateButtonState();
129 if (!getInheritedEnabled())
130 {
131 mIsMouseFocus = false;
132 }
133 }
134
135 void Button::setModeImage(bool _value)
136 {
137 mModeImage = _value;
138 updateButtonState();
139 }
140
141 bool Button::_setState(const std::string& _value)
142 {
143 if (mModeImage)
144 {
145 if (mImage)
146 mImage->setItemName(_value);
147
148 _setWidgetState(_value);
149 return true;
150 }
151
152 return _setWidgetState(_value);
153 }
154
155 void Button::setImageResource(const std::string& _name)
156 {
157 if (mImage)
158 mImage->setItemResource(_name);
159 updateButtonState();
160 }
161
162 void Button::setImageGroup(const std::string& _name)
163 {
164 if (mImage)
165 mImage->setItemGroup(_name);
166 updateButtonState();
167 }
168
169 void Button::setImageName(const std::string& _name)
170 {
171 if (mImage)
172 mImage->setItemName(_name);
173 }
174
175 void Button::setPropertyOverride(const std::string& _key, const std::string& _value)
176 {
178 if (_key == "StateSelected")
179 setStateSelected(utility::parseValue<bool>(_value));
180
182 else if (_key == "ModeImage")
183 setModeImage(utility::parseValue<bool>(_value));
184
186 else if (_key == "ImageResource")
187 setImageResource(_value);
188
190 else if (_key == "ImageGroup")
191 setImageGroup(_value);
192
194 else if (_key == "ImageName")
195 setImageName(_value);
196
197 else
198 {
199 Base::setPropertyOverride(_key, _value);
200 return;
201 }
202
203 eventChangeProperty(this, _key, _value);
204 }
205
206 bool Button::getStateSelected() const
207 {
208 return mStateSelected;
209 }
210
211 bool Button::getModeImage() const
212 {
213 return mModeImage;
214 }
215
216 ImageBox* Button::_getImageBox()
217 {
218 return mImage;
219 }
220
221 } // namespace MyGUI
MyGUI::Widget::getInheritedEnabled
bool getInheritedEnabled() const
Definition: MyGUI_Widget.cpp:1330
MyGUI::Button::setModeImage
void setModeImage(bool _value)
Definition: MyGUI_Button.cpp:135
MyGUI::Button::onMouseButtonPressed
virtual void onMouseButtonPressed(int _left, int _top, MouseButton _id)
Definition: MyGUI_Button.cpp:54
MyGUI::Button::_setMouseFocus
void _setMouseFocus(bool _focus)
Definition: MyGUI_Button.cpp:120
MyGUI::ImageBox
ImageBox properties. Skin childs. ImageBox widget description should be here.
Definition: MyGUI_ImageBox.h:21
MyGUI::Button::Button
Button()
Definition: MyGUI_Button.cpp:16
MyGUI::Button::_getImageBox
ImageBox * _getImageBox()
Definition: MyGUI_Button.cpp:216
MyGUI::Widget::eventChangeProperty
EventHandle_WidgetStringString eventChangeProperty
Definition: MyGUI_Widget.h:266
MyGUI::Widget
Widget properties. Skin childs. Widget widget description should be here.
Definition: MyGUI_Widget.h:29
MyGUI::Button::setImageResource
void setImageResource(const std::string &_name)
Definition: MyGUI_Button.cpp:155
MyGUI::Button::setStateSelected
void setStateSelected(bool _value)
Set button selected state.
Definition: MyGUI_Button.cpp:111
MyGUI::Button::_setState
bool _setState(const std::string &_value)
Definition: MyGUI_Button.cpp:141
MyGUI_Precompiled.h
MyGUI::Button::setPropertyOverride
virtual void setPropertyOverride(const std::string &_key, const std::string &_value)
Definition: MyGUI_Button.cpp:175
MyGUI::MouseButton
Definition: MyGUI_MouseButton.h:15
MyGUI::ImageBox::setItemResource
bool setItemResource(const std::string &_name)
Definition: MyGUI_ImageBox.cpp:345
MyGUI_ImageBox.h
MyGUI_InputManager.h
MyGUI::ImageBox::setItemName
void setItemName(const std::string &_value)
Definition: MyGUI_ImageBox.cpp:405
MyGUI::Button::shutdownOverride
virtual void shutdownOverride()
Definition: MyGUI_Button.cpp:33
MyGUI::Button::baseUpdateEnable
virtual void baseUpdateEnable()
Definition: MyGUI_Button.cpp:126
MyGUI_Button.h
MyGUI::Button::setImageName
void setImageName(const std::string &_name)
Definition: MyGUI_Button.cpp:169
MyGUI::Button::onMouseSetFocus
virtual void onMouseSetFocus(Widget *_old)
Definition: MyGUI_Button.cpp:40
MyGUI::Button::onMouseLostFocus
virtual void onMouseLostFocus(Widget *_new)
Definition: MyGUI_Button.cpp:47
MyGUI::Button::setImageGroup
void setImageGroup(const std::string &_name)
Definition: MyGUI_Button.cpp:162
nullptr
#define nullptr
Definition: MyGUI_Prerequest.h:29
MyGUI::Widget::assignWidget
void assignWidget(T *&_widget, const std::string &_name)
Definition: MyGUI_Widget.h:328
MyGUI::Button::getModeImage
bool getModeImage() const
Definition: MyGUI_Button.cpp:211
MyGUI::Button::initialiseOverride
virtual void initialiseOverride()
Definition: MyGUI_Button.cpp:25
MyGUI::MouseButton::Left
Definition: MyGUI_MouseButton.h:21
MyGUI::Button::onMouseButtonReleased
virtual void onMouseButtonReleased(int _left, int _top, MouseButton _id)
Definition: MyGUI_Button.cpp:65
MyGUI::ImageBox::setItemGroup
void setItemGroup(const std::string &_value)
Definition: MyGUI_ImageBox.cpp:395
MyGUI::Widget::_setWidgetState
bool _setWidgetState(const std::string &_value)
Definition: MyGUI_Widget.cpp:374
MyGUI
Definition: MyGUI_ActionController.h:14
MyGUI::Button::getStateSelected
bool getStateSelected() const
Get buton selected.
Definition: MyGUI_Button.cpp:206
MyGUI_ResourceSkin.h