MyGUI: MyGUI_PointerManager.cpp Source File

MyGUI 3.4.2

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

- File List
- Globals

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
22namespace MyGUI
23{
24
25 MYGUI_SINGLETON_DEFINITION(PointerManager);
26
27 PointerManager::PointerManager() :
28 mVisible(false),
29 mWidgetOwner(nullptr),
30 mMousePointer(nullptr),
31 mPointer(nullptr),
32 mIsInitialise(false),
33 mXmlPointerTagName("Pointer"),
34 mXmlPropertyTagName("Property"),
35 mXmlDefaultPointerValue("Default"),
36 mSingletonHolder(this)
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::_destroyAllChildWidget()
179 {
180 WidgetManager& manager = WidgetManager::getInstance();
181 while (!mWidgetChild.empty())
182 {
183 // сразу себя отписывем, иначе вложенной удаление убивает все
184 Widget* widget = mWidgetChild.back();
185 mWidgetChild.pop_back();
186
187 // отписываем от всех
188 manager.unlinkFromUnlinkers(widget);
189
190 // и сами удалим, так как его больше в списке нет
191 WidgetManager::getInstance()._deleteWidget(widget);
192 }
193 }
194
195 void PointerManager::setDefaultPointer(const std::string& _value)
196 {
197 Update();
198
199 mDefaultName = _value;
200 setPointer(mDefaultName, nullptr);
201 }
202
203 void PointerManager::setLayerName(const std::string& _value)
204 {
205 Update();
206
207 mLayerName = _value;
208 if (LayerManager::getInstance().isExist(_value))
209 LayerManager::getInstance().attachToLayerNode(mLayerName, mMousePointer);
210 }
211
212 void PointerManager::Update()
213 {
214 if (mMousePointer == nullptr)
215 mMousePointer = static_cast<ImageBox*>(baseCreateWidget(WidgetStyle::Overlapped, ImageBox::getClassTypeName(), mSkinName, IntCoord(), Align::Default, "", ""));
216 }
217
218 IPointer* PointerManager::getByName(const std::string& _name) const
219 {
220 IResource* result = nullptr;
221 if (!_name.empty() && _name != mXmlDefaultPointerValue)
222 result = ResourceManager::getInstance().getByName(_name, false);
223
224 if (result == nullptr)
225 result = ResourceManager::getInstance().getByName(mDefaultName, false);
226
227 return result ? result->castType<IPointer>(false) : nullptr;
228 }
229
230 void PointerManager::notifyChangeMouseFocus(Widget* _widget)
231 {
232 std::string pointer = (_widget == nullptr || !_widget->getInheritedEnabled()) ? "" : _widget->getPointer();
233 if (pointer != mCurrentMousePointer)
234 {
235 mCurrentMousePointer = pointer;
236 if (mCurrentMousePointer.empty())
237 {
238 resetToDefaultPointer();
239 eventChangeMousePointer(mDefaultName);
240 }
241 else
242 {
243 setPointer(mCurrentMousePointer, _widget);
244 eventChangeMousePointer(mCurrentMousePointer);
245 }
246 }
247 }
248
249 void PointerManager::setPointer(const std::string& _name)
250 {
251 setPointer(_name, nullptr);
252 }
253
254 bool PointerManager::isVisible() const
255 {
256 return mVisible;
257 }
258
259 const std::string& PointerManager::getDefaultPointer() const
260 {
261 return mDefaultName;
262 }
263
264 const std::string& PointerManager::getLayerName() const
265 {
266 return mLayerName;
267 }
268
269} // namespace MyGUI
MyGUI_CoordConverter.h
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition MyGUI_Diagnostic.h:29
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition MyGUI_Diagnostic.h:17
MyGUI_FactoryManager.h
MyGUI_Gui.h
MyGUI_InputManager.h
MyGUI_LayerManager.h
MyGUI_PointerManager.h
MyGUI_Precompiled.h
MyGUI_ResourceImageSetPointer.h
MyGUI_ResourceManager.h
MyGUI_ResourceManualPointer.h
MYGUI_SINGLETON_DEFINITION
#define MYGUI_SINGLETON_DEFINITION(ClassName)
Definition MyGUI_Singleton.h:123
MyGUI_Widget.h
MyGUI_WidgetManager.h
MyGUI_XmlDocument.h
MyGUI::FactoryManager::unregisterFactory
void unregisterFactory(const std::string &_category, const std::string &_type)
Definition MyGUI_FactoryManager.cpp:46
MyGUI::FactoryManager::getInstance
static FactoryManager & getInstance()
MyGUI::FactoryManager::registerFactory
void registerFactory(const std::string &_category, const std::string &_type, Delegate::IDelegate *_delegate)
Definition MyGUI_FactoryManager.cpp:40
MyGUI::Gui::getInstance
static Gui & getInstance()
Definition MyGUI_Gui.cpp:34
MyGUI::Gui::eventFrameStart
EventHandle_FrameEventDelegate eventFrameStart
Definition MyGUI_Gui.h:150
MyGUI::IObject::castType
Type * castType(bool _throw=true)
Definition MyGUI_IObject.h:18
MyGUI::IPointer
Definition MyGUI_IPointer.h:18
MyGUI::IPointer::setImage
virtual void setImage(ImageBox *_image)=0
MyGUI::IPointer::setPosition
virtual void setPosition(ImageBox *_image, const IntPoint &_point)=0
MyGUI::IResource
Definition MyGUI_IResource.h:25
MyGUI::ImageBox
widget description should be here.
Definition MyGUI_ImageBox.h:24
MyGUI::ImageBox::getClassTypeName
static const std::string & getClassTypeName()
Definition MyGUI_ImageBox.h:25
MyGUI::InputManager::getMousePosition
const IntPoint & getMousePosition() const
Definition MyGUI_InputManager.cpp:666
MyGUI::InputManager::eventChangeMouseFocus
delegates::CMultiDelegate1< Widget * > eventChangeMouseFocus
Definition MyGUI_InputManager.h:123
MyGUI::InputManager::getInstance
static InputManager & getInstance()
MyGUI::LayerManager::getInstance
static LayerManager & getInstance()
MyGUI::LayerManager::attachToLayerNode
void attachToLayerNode(const std::string &_name, Widget *_item)
Definition MyGUI_LayerManager.cpp:124
MyGUI::MemberObsolete< PointerManager >::loadOldPointerFormat
void loadOldPointerFormat(xml::ElementPtr _node, const std::string &_file, Version _version, const std::string &_tag)
Definition MyGUI_BackwardCompatibility.cpp:906
MyGUI::PointerManager
Definition MyGUI_PointerManager.h:23
MyGUI::PointerManager::PointerManager
PointerManager()
Definition MyGUI_PointerManager.cpp:27
MyGUI::PointerManager::resetToDefaultPointer
void resetToDefaultPointer()
Definition MyGUI_PointerManager.cpp:158
MyGUI::PointerManager::eventChangeMousePointer
delegates::CMultiDelegate1< const std::string & > eventChangeMousePointer
Definition MyGUI_PointerManager.h:61
MyGUI::PointerManager::isVisible
bool isVisible() const
Definition MyGUI_PointerManager.cpp:254
MyGUI::PointerManager::getDefaultPointer
const std::string & getDefaultPointer() const
Definition MyGUI_PointerManager.cpp:259
MyGUI::PointerManager::setVisible
void setVisible(bool _visible)
Definition MyGUI_PointerManager.cpp:125
MyGUI::PointerManager::getLayerName
const std::string & getLayerName() const
Definition MyGUI_PointerManager.cpp:264
MyGUI::PointerManager::setPointer
void setPointer(const std::string &_name)
Definition MyGUI_PointerManager.cpp:249
MyGUI::PointerManager::shutdown
void shutdown()
Definition MyGUI_PointerManager.cpp:66
MyGUI::PointerManager::getClassTypeName
static const char * getClassTypeName()
Definition MyGUI_PointerManager.cpp:25
MyGUI::PointerManager::setLayerName
void setLayerName(const std::string &_value)
Definition MyGUI_PointerManager.cpp:203
MyGUI::PointerManager::getByName
IPointer * getByName(const std::string &_name) const
Definition MyGUI_PointerManager.cpp:218
MyGUI::PointerManager::setDefaultPointer
void setDefaultPointer(const std::string &_value)
Definition MyGUI_PointerManager.cpp:195
MyGUI::PointerManager::initialise
void initialise()
Definition MyGUI_PointerManager.cpp:40
MyGUI::ResourceImageSetPointer
Definition MyGUI_ResourceImageSetPointer.h:19
MyGUI::ResourceManager::getCategoryName
const std::string & getCategoryName() const
Definition MyGUI_ResourceManager.cpp:275
MyGUI::ResourceManager::getByName
IResource * getByName(const std::string &_name, bool _throw=true) const
Definition MyGUI_ResourceManager.cpp:235
MyGUI::ResourceManager::unregisterLoadXmlDelegate
void unregisterLoadXmlDelegate(const std::string &_key)
Definition MyGUI_ResourceManager.cpp:126
MyGUI::ResourceManager::getInstance
static ResourceManager & getInstance()
MyGUI::ResourceManager::registerLoadXmlDelegate
LoadXmlDelegate & registerLoadXmlDelegate(const std::string &_key)
Definition MyGUI_ResourceManager.cpp:119
MyGUI::ResourceManualPointer
Definition MyGUI_ResourceManualPointer.h:18
MyGUI::Version
Definition MyGUI_Version.h:18
MyGUI::Widget
widget description should be here.
Definition MyGUI_Widget.h:37
MyGUI::Widget::getInheritedEnabled
bool getInheritedEnabled() const
Definition MyGUI_Widget.cpp:1335
MyGUI::Widget::setVisible
virtual void setVisible(bool _value)
Definition MyGUI_Widget.cpp:959
MyGUI::Widget::setAlign
virtual void setAlign(Align _value)
Definition MyGUI_Widget.cpp:731
MyGUI::WidgetInput::getPointer
const std::string & getPointer() const
Definition MyGUI_WidgetInput.cpp:147
MyGUI::WidgetManager::unregisterUnlinker
void unregisterUnlinker(IUnlinkWidget *_unlink)
Definition MyGUI_WidgetManager.cpp:139
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::WidgetManager::getInstance
static WidgetManager & getInstance()
MyGUI::WidgetManager::_deleteWidget
void _deleteWidget(Widget *_widget)
Definition MyGUI_WidgetManager.cpp:169
MyGUI::WidgetManager::registerUnlinker
void registerUnlinker(IUnlinkWidget *_unlink)
Definition MyGUI_WidgetManager.cpp:133
MyGUI::xml::ElementEnumerator
Definition MyGUI_XmlDocument.h:115
MyGUI::xml::ElementEnumerator::next
bool next()
Definition MyGUI_XmlDocument.cpp:100
MyGUI::xml::Element
Definition MyGUI_XmlDocument.h:159
MyGUI::xml::Element::getElementEnumerator
ElementEnumerator getElementEnumerator()
Definition MyGUI_XmlDocument.cpp:352
MyGUI::xml::Element::getName
const std::string & getName() const
Definition MyGUI_XmlDocument.cpp:332
MyGUI::xml::Element::findAttribute
bool findAttribute(const std::string &_name, std::string &_value)
Definition MyGUI_XmlDocument.cpp:246
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::IntCoord
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:36
MyGUI::newDelegate
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
Definition MyGUI_Delegate.h:99
MyGUI::Align
Definition MyGUI_Align.h:20
MyGUI::Align::Default
@ Default
Definition MyGUI_Align.h:36
MyGUI::WidgetStyle
Definition MyGUI_WidgetStyle.h:19
MyGUI::WidgetStyle::Overlapped
@ Overlapped
Definition MyGUI_WidgetStyle.h:24
MyGUI::types::TCoord< int >