MyGUI  3.4.1
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"
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 
21 
22 namespace MyGUI
23 {
24 
26 
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 
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);
48 
49  ResourceManager::getInstance().registerLoadXmlDelegate(mXmlPointerTagName) = newDelegate(this, &PointerManager::_load);
50 
51  std::string resourceCategory = ResourceManager::getInstance().getCategoryName();
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 
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();
77 
78  // удаляем все виджеты
79  _destroyAllChildWidget();
80 
81  mWidgetOwner = nullptr;
82 
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 
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  {
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 
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())
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  // и сами удалим, так как его больше в списке нет
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  {
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 
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
#define MYGUI_ASSERT(exp, dest)
#define MYGUI_LOG(level, text)
void unregisterFactory(const std::string &_category, const std::string &_type)
void registerFactory(const std::string &_category, const std::string &_type, Delegate::IDelegate *_delegate)
static FactoryManager & getInstance()
static Gui & getInstance()
EventHandle_FrameEventDelegate eventFrameStart
Definition: MyGUI_Gui.h:150
Type * castType(bool _throw=true)
Definition: MyGUI_IObject.h:18
virtual void setImage(ImageBox *_image)=0
virtual void setPosition(ImageBox *_image, const IntPoint &_point)=0
widget description should be here.
static const std::string & getClassTypeName()
const IntPoint & getMousePosition() const
delegates::CMultiDelegate1< Widget * > eventChangeMouseFocus
static InputManager & getInstance()
static LayerManager & getInstance()
void attachToLayerNode(const std::string &_name, Widget *_item)
void loadOldPointerFormat(xml::ElementPtr _node, const std::string &_file, Version _version, const std::string &_tag)
delegates::CMultiDelegate1< const std::string & > eventChangeMousePointer
const std::string & getDefaultPointer() const
void setVisible(bool _visible)
const std::string & getLayerName() const
void setPointer(const std::string &_name)
static const char * getClassTypeName()
void setLayerName(const std::string &_value)
IPointer * getByName(const std::string &_name) const
void setDefaultPointer(const std::string &_value)
const std::string & getCategoryName() const
IResource * getByName(const std::string &_name, bool _throw=true) const
void unregisterLoadXmlDelegate(const std::string &_key)
static ResourceManager & getInstance()
LoadXmlDelegate & registerLoadXmlDelegate(const std::string &_key)
widget description should be here.
Definition: MyGUI_Widget.h:37
bool getInheritedEnabled() const
virtual void setVisible(bool _value)
virtual void setAlign(Align _value)
const std::string & getPointer() const
void unregisterUnlinker(IUnlinkWidget *_unlink)
static WidgetManager & getInstance()
Widget * createWidget(WidgetStyle _style, const std::string &_type, const std::string &_skin, const IntCoord &_coord, Widget *_parent, ICroppedRectangle *_cropeedParent, const std::string &_name)
void _deleteWidget(Widget *_widget)
void registerUnlinker(IUnlinkWidget *_unlink)
ElementEnumerator getElementEnumerator()
const std::string & getName() const
bool findAttribute(const std::string &_name, std::string &_value)
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
types::TCoord< int > IntCoord
Definition: MyGUI_Types.h:35
MYGUI_SINGLETON_DEFINITION(ClipboardManager)