MyGUI: MyGUI_PointerManager.cpp Source File

MyGUI 3.2.2

MyGUI_PointerManager.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_PointerManager.h"
9 #include "MyGUI_ResourceManager.h"
10 #include "MyGUI_LayerManager.h"
11 #include "MyGUI_CoordConverter.h"
12 #include "MyGUI_WidgetManager.h"
13 #include "MyGUI_XmlDocument.h"
14 #include "MyGUI_Widget.h"
15 #include "MyGUI_FactoryManager.h"
16 #include "MyGUI_InputManager.h"
17 #include "MyGUI_Gui.h"
18
19 #include "MyGUI_ResourceManualPointer.h"
20 #include "MyGUI_ResourceImageSetPointer.h"
21
22 namespace MyGUI
23 {
24
25 template <> PointerManager* Singleton<PointerManager>::msInstance = nullptr;
26 template <> const char* Singleton<PointerManager>::mClassTypeName = "PointerManager";
27
28 PointerManager::PointerManager() :
29 mVisible(false),
30 mWidgetOwner(nullptr),
31 mMousePointer(nullptr),
32 mPointer(nullptr),
33 mIsInitialise(false),
34 mXmlPointerTagName("Pointer"),
35 mXmlPropertyTagName("Property"),
36 mXmlDefaultPointerValue("Default")
37 {
38 }
39
40 void PointerManager::initialise()
41 {
42 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
43 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
44
45 Gui::getInstance().eventFrameStart += newDelegate(this, &PointerManager::notifyFrameStart);
46 InputManager::getInstance().eventChangeMouseFocus += newDelegate(this, &PointerManager::notifyChangeMouseFocus);
47 WidgetManager::getInstance().registerUnlinker(this);
48
49 ResourceManager::getInstance().registerLoadXmlDelegate(mXmlPointerTagName) = newDelegate(this, &PointerManager::_load);
50
51 std::string resourceCategory = ResourceManager::getInstance().getCategoryName();
52 FactoryManager::getInstance().registerFactory<ResourceManualPointer>(resourceCategory);
53 FactoryManager::getInstance().registerFactory<ResourceImageSetPointer>(resourceCategory);
54
55 mPointer = nullptr;
56 mMousePointer = nullptr;
57 mWidgetOwner = nullptr;
58 mVisible = true;
59
60 mSkinName = "ImageBox";
61
62 MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
63 mIsInitialise = true;
64 }
65
66 void PointerManager::shutdown()
67 {
68 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
69 MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
70
71 InputManager::getInstance().eventChangeMouseFocus -= newDelegate(this, &PointerManager::notifyChangeMouseFocus);
72 Gui::getInstance().eventFrameStart -= newDelegate(this, &PointerManager::notifyFrameStart);
73
74 std::string resourceCategory = ResourceManager::getInstance().getCategoryName();
75 FactoryManager::getInstance().unregisterFactory<ResourceManualPointer>(resourceCategory);
76 FactoryManager::getInstance().unregisterFactory<ResourceImageSetPointer>(resourceCategory);
77
78 // удаляем все виджеты
79 _destroyAllChildWidget();
80
81 mWidgetOwner = nullptr;
82
83 WidgetManager::getInstance().unregisterUnlinker(this);
84 ResourceManager::getInstance().unregisterLoadXmlDelegate(mXmlPointerTagName);
85
86 MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
87 mIsInitialise = false;
88 }
89
90 void PointerManager::_load(xml::ElementPtr _node, const std::string& _file, Version _version)
91 {
92 #ifndef MYGUI_DONT_USE_OBSOLETE
93 loadOldPointerFormat(_node, _file, _version, mXmlPointerTagName);
94 #endif // MYGUI_DONT_USE_OBSOLETE
95
96 xml::ElementEnumerator node = _node->getElementEnumerator();
97 while (node.next())
98 {
99 if (node->getName() == mXmlPropertyTagName)
100 {
101 const std::string& key = node->findAttribute("key");
102 const std::string& value = node->findAttribute("value");
103 if (key == "Default")
104 setDefaultPointer(value);
105 else if (key == "Layer")
106 setLayerName(value);
107 else if (key == "Skin")
108 mSkinName = value;
109 }
110 }
111 }
112
113 void PointerManager::notifyFrameStart(float _time)
114 {
115 mPoint = InputManager::getInstance().getMousePosition();
116 if (mOldPoint != mPoint)
117 {
118 mOldPoint = mPoint;
119
120 if (nullptr != mMousePointer && mPointer != nullptr)
121 mPointer->setPosition(mMousePointer, mPoint);
122 }
123 }
124
125 void PointerManager::setVisible(bool _visible)
126 {
127 if (nullptr != mMousePointer) mMousePointer->setVisible(_visible);
128 mVisible = _visible;
129 }
130
131 void PointerManager::setPointer(const std::string& _name, Widget* _owner)
132 {
133 if (nullptr == mMousePointer)
134 return;
135
136 IResource* result = getByName(_name);
137 if (result == nullptr)
138 {
139 mPointer = nullptr;
140 mMousePointer->setVisible(false);
141 return;
142 }
143
144 mMousePointer->setVisible(mVisible);
145 mPointer = result->castType<IPointer>();
146 mPointer->setImage(mMousePointer);
147 mPointer->setPosition(mMousePointer, mPoint);
148
149 mWidgetOwner = _owner;
150 }
151
152 void PointerManager::_unlinkWidget(Widget* _widget)
153 {
154 if (_widget == mWidgetOwner) setPointer(mDefaultName, nullptr);
155 else if (_widget == mMousePointer) mMousePointer = nullptr;
156 }
157
158 void PointerManager::resetToDefaultPointer()
159 {
160 setPointer(mDefaultName, nullptr);
161 }
162
163 // создает виджет
164 Widget* PointerManager::baseCreateWidget(WidgetStyle _style, const std::string& _type, const std::string& _skin, const IntCoord& _coord, Align _align, const std::string& _layer, const std::string& _name)
165 {
166 Widget* widget = WidgetManager::getInstance().createWidget(_style, _type, _skin, _coord, /*_align, */nullptr, nullptr, /*this, */_name);
167 mWidgetChild.push_back(widget);
168
169 widget->setAlign(_align);
170
171 // присоединяем виджет с уровню
172 if (!_layer.empty())
173 LayerManager::getInstance().attachToLayerNode(_layer, widget);
174 return widget;
175 }
176
177 // удяляет неудачника
178 void PointerManager::_destroyChildWidget(Widget* _widget)
179 {
180 MYGUI_ASSERT(nullptr != _widget, "invalid widget pointer");
181
182 VectorWidgetPtr::iterator iter = std::find(mWidgetChild.begin(), mWidgetChild.end(), _widget);
183 if (iter != mWidgetChild.end())
184 {
185 // сохраняем указатель
186 MyGUI::Widget* widget = *iter;
187
188 // удаляем из списка
189 mWidgetChild.erase(iter);
190
191 // отписываем от всех
192 WidgetManager::getInstance().unlinkFromUnlinkers(_widget);
193
194 // непосредственное удаление
195 WidgetManager::getInstance()._deleteWidget(widget);
196 }
197 else
198 {
199 MYGUI_EXCEPT("Widget '" << _widget->getName() << "' not found");
200 }
201 }
202
203 // удаляет всех детей
204 void PointerManager::_destroyAllChildWidget()
205 {
206 WidgetManager& manager = WidgetManager::getInstance();
207 while (!mWidgetChild.empty())
208 {
209 // сразу себя отписывем, иначе вложенной удаление убивает все
210 Widget* widget = mWidgetChild.back();
211 mWidgetChild.pop_back();
212
213 // отписываем от всех
214 manager.unlinkFromUnlinkers(widget);
215
216 // и сами удалим, так как его больше в списке нет
217 WidgetManager::getInstance()._deleteWidget(widget);
218 }
219 }
220
221 void PointerManager::setDefaultPointer(const std::string& _value)
222 {
223 Update();
224
225 mDefaultName = _value;
226 setPointer(mDefaultName, nullptr);
227 }
228
229 void PointerManager::setLayerName(const std::string& _value)
230 {
231 Update();
232
233 mLayerName = _value;
234 if (LayerManager::getInstance().isExist(_value))
235 LayerManager::getInstance().attachToLayerNode(mLayerName, mMousePointer);
236 }
237
238 void PointerManager::Update()
239 {
240 if (mMousePointer == nullptr)
241 mMousePointer = static_cast<ImageBox*>(baseCreateWidget(WidgetStyle::Overlapped, ImageBox::getClassTypeName(), mSkinName, IntCoord(), Align::Default, "", ""));
242 }
243
244 IPointer* PointerManager::getByName(const std::string& _name) const
245 {
246 IResource* result = nullptr;
247 if (!_name.empty() && _name != mXmlDefaultPointerValue)
248 result = ResourceManager::getInstance().getByName(_name, false);
249
250 if (result == nullptr)
251 result = ResourceManager::getInstance().getByName(mDefaultName, false);
252
253 return result ? result->castType<IPointer>(false) : nullptr;
254 }
255
256 void PointerManager::notifyChangeMouseFocus(Widget* _widget)
257 {
258 std::string pointer = (_widget == nullptr || !_widget->getInheritedEnabled()) ? "" : _widget->getPointer();
259 if (pointer != mCurrentMousePointer)
260 {
261 mCurrentMousePointer = pointer;
262 if (mCurrentMousePointer.empty())
263 {
264 resetToDefaultPointer();
265 eventChangeMousePointer(mDefaultName);
266 }
267 else
268 {
269 setPointer(mCurrentMousePointer, _widget);
270 eventChangeMousePointer(mCurrentMousePointer);
271 }
272 }
273 }
274
275 void PointerManager::setPointer(const std::string& _name)
276 {
277 setPointer(_name, nullptr);
278 }
279
280 bool PointerManager::isVisible() const
281 {
282 return mVisible;
283 }
284
285 const std::string& PointerManager::getDefaultPointer() const
286 {
287 return mDefaultName;
288 }
289
290 const std::string& PointerManager::getLayerName() const
291 {
292 return mLayerName;
293 }
294
295 } // namespace MyGUI
MyGUI::Singleton< Gui >::getInstance
static Gui & getInstance()
Definition: MyGUI_Singleton.h:38
MyGUI::ResourceManager::getCategoryName
const std::string & getCategoryName() const
Definition: MyGUI_ResourceManager.cpp:275
MyGUI::Gui::eventFrameStart
EventHandle_FrameEventDelegate eventFrameStart
Definition: MyGUI_Gui.h:150
MyGUI::FactoryManager::unregisterFactory
void unregisterFactory(const std::string &_category, const std::string &_type)
Definition: MyGUI_FactoryManager.cpp:46
MyGUI_Gui.h
MyGUI::Widget::getInheritedEnabled
bool getInheritedEnabled() const
Definition: MyGUI_Widget.cpp:1330
MyGUI::ImageBox::getClassTypeName
static const std::string & getClassTypeName()
Definition: MyGUI_ImageBox.h:25
MyGUI::IntCoord
types::TCoord< int > IntCoord
Definition: MyGUI_Types.h:35
MyGUI::WidgetManager::_deleteWidget
void _deleteWidget(Widget *_widget)
Definition: MyGUI_WidgetManager.cpp:169
MyGUI::IPointer
Definition: MyGUI_IPointer.h:16
MyGUI_XmlDocument.h
MyGUI_Widget.h
MyGUI::ResourceManager::registerLoadXmlDelegate
LoadXmlDelegate & registerLoadXmlDelegate(const std::string &_key)
Definition: MyGUI_ResourceManager.cpp:119
MyGUI_ResourceManager.h
MyGUI::PointerManager::PointerManager
PointerManager()
Definition: MyGUI_PointerManager.cpp:28
MyGUI::ResourceManager::unregisterLoadXmlDelegate
void unregisterLoadXmlDelegate(const std::string &_key)
Definition: MyGUI_ResourceManager.cpp:126
MyGUI::WidgetManager::registerUnlinker
void registerUnlinker(IUnlinkWidget *_unlink)
Definition: MyGUI_WidgetManager.cpp:133
MyGUI::xml::Element::getName
const std::string & getName() const
Definition: MyGUI_XmlDocument.cpp:332
MyGUI_PointerManager.h
MyGUI::Singleton< PointerManager >::getClassTypeName
static const char * getClassTypeName()
Definition: MyGUI_Singleton.h:49
MyGUI::WidgetManager::createWidget
Widget * createWidget(WidgetStyle _style, const std::string &_type, const std::string &_skin, const IntCoord &_coord, Widget *_parent, ICroppedRectangle *_cropeedParent, const std::string &_name)
Definition: MyGUI_WidgetManager.cpp:104
MyGUI::FactoryManager::registerFactory
void registerFactory(const std::string &_category, const std::string &_type, Delegate::IDelegate *_delegate)
Definition: MyGUI_FactoryManager.cpp:40
MyGUI::ResourceManualPointer
Definition: MyGUI_ResourceManualPointer.h:16
MyGUI::Widget
Widget properties. Skin childs. Widget widget description should be here.
Definition: MyGUI_Widget.h:29
MyGUI::Widget::setAlign
virtual void setAlign(Align _value)
Definition: MyGUI_Widget.cpp:734
MyGUI::Align
Definition: MyGUI_Align.h:19
MyGUI_CoordConverter.h
MyGUI::xml::Element
Definition: MyGUI_XmlDocument.h:158
MyGUI::xml::Element::findAttribute
bool findAttribute(const std::string &_name, std::string &_value)
Definition: MyGUI_XmlDocument.cpp:246
MyGUI_Precompiled.h
MyGUI::IPointer::setImage
virtual void setImage(ImageBox *_image)=0
MyGUI_ResourceImageSetPointer.h
MyGUI::Align::Default
Definition: MyGUI_Align.h:36
MyGUI_ResourceManualPointer.h
MyGUI::PointerManager::getByName
IPointer * getByName(const std::string &_name) const
Definition: MyGUI_PointerManager.cpp:244
MyGUI::InputManager::getMousePosition
const IntPoint & getMousePosition() const
Definition: MyGUI_InputManager.cpp:647
MyGUI::WidgetInput::getPointer
const std::string & getPointer() const
Definition: MyGUI_WidgetInput.cpp:151
MyGUI::WidgetManager::unregisterUnlinker
void unregisterUnlinker(IUnlinkWidget *_unlink)
Definition: MyGUI_WidgetManager.cpp:139
MyGUI::PointerManager::setLayerName
void setLayerName(const std::string &_value)
Definition: MyGUI_PointerManager.cpp:229
MyGUI::Version
Definition: MyGUI_Version.h:17
MyGUI_InputManager.h
MYGUI_EXCEPT
#define MYGUI_EXCEPT(dest)
Definition: MyGUI_Diagnostic.h:33
MyGUI::Widget::setVisible
virtual void setVisible(bool _value)
Definition: MyGUI_Widget.cpp:965
MyGUI::PointerManager::shutdown
void shutdown()
Definition: MyGUI_PointerManager.cpp:66
MyGUI::InputManager::eventChangeMouseFocus
delegates::CMultiDelegate1< Widget * > eventChangeMouseFocus
Definition: MyGUI_InputManager.h:119
MyGUI_LayerManager.h
MyGUI::xml::ElementEnumerator
Definition: MyGUI_XmlDocument.h:114
MyGUI::WidgetStyle::Overlapped
Definition: MyGUI_WidgetStyle.h:22
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition: MyGUI_Diagnostic.h:42
MyGUI::IObject::castType
Type * castType(bool _throw=true)
Definition: MyGUI_IObject.h:18
MyGUI_WidgetManager.h
MyGUI::LayerManager::attachToLayerNode
void attachToLayerNode(const std::string &_name, Widget *_item)
Definition: MyGUI_LayerManager.cpp:119
MyGUI::ResourceImageSetPointer
Definition: MyGUI_ResourceImageSetPointer.h:17
MyGUI::MemberObsolete< PointerManager >::loadOldPointerFormat
void loadOldPointerFormat(xml::ElementPtr _node, const std::string &_file, Version _version, const std::string &_tag)
Definition: MyGUI_BackwardCompatibility.cpp:902
MyGUI::ResourceManager::getByName
IResource * getByName(const std::string &_name, bool _throw=true) const
Definition: MyGUI_ResourceManager.cpp:235
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition: MyGUI_Diagnostic.h:22
MyGUI::PointerManager::isVisible
bool isVisible() const
Definition: MyGUI_PointerManager.cpp:280
nullptr
#define nullptr
Definition: MyGUI_Prerequest.h:29
MyGUI::WidgetStyle
Definition: MyGUI_WidgetStyle.h:16
MyGUI::PointerManager::getDefaultPointer
const std::string & getDefaultPointer() const
Definition: MyGUI_PointerManager.cpp:285
MyGUI::PointerManager::setDefaultPointer
void setDefaultPointer(const std::string &_value)
Definition: MyGUI_PointerManager.cpp:221
MyGUI::PointerManager::eventChangeMousePointer
delegates::CMultiDelegate1< const std::string & > eventChangeMousePointer
Definition: MyGUI_PointerManager.h:61
MyGUI::IPointer::setPosition
virtual void setPosition(ImageBox *_image, const IntPoint &_point)=0
MyGUI::PointerManager::setVisible
void setVisible(bool _visible)
Definition: MyGUI_PointerManager.cpp:125
MyGUI::PointerManager::setPointer
void setPointer(const std::string &_name)
Definition: MyGUI_PointerManager.cpp:275
MyGUI::IResource
Definition: MyGUI_IResource.h:23
MyGUI::types::TCoord
Definition: MyGUI_TCoord.h:20
MyGUI::PointerManager::resetToDefaultPointer
void resetToDefaultPointer()
Definition: MyGUI_PointerManager.cpp:158
MyGUI::WidgetManager::unlinkFromUnlinkers
void unlinkFromUnlinkers(Widget *_widget)
Definition: MyGUI_WidgetManager.cpp:146
MyGUI::xml::Element::getElementEnumerator
ElementEnumerator getElementEnumerator()
Definition: MyGUI_XmlDocument.cpp:352
MyGUI
Definition: MyGUI_ActionController.h:14
MyGUI::xml::ElementEnumerator::next
bool next()
Definition: MyGUI_XmlDocument.cpp:100
newDelegate
MYGUI_TEMPLATE MYGUI_TEMPLATE_PARAMS delegates::IDelegateMYGUI_SUFFIX MYGUI_TEMPLATE_ARGS * newDelegate(void(*_func)(MYGUI_PARAMS))
Definition: MyGUI_DelegateImplement.h:117
MyGUI::PointerManager::initialise
void initialise()
Definition: MyGUI_PointerManager.cpp:40
MyGUI_FactoryManager.h
MyGUI::PointerManager::getLayerName
const std::string & getLayerName() const
Definition: MyGUI_PointerManager.cpp:290