MyGUI: MyGUI_PointerManager.cpp Source File

MyGUI 3.4.3

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