MyGUI_PointerManager.cpp
Go to the documentation of this file.
/*
* This source file is part of MyGUI. For the latest info, see http://mygui.info/
* Distributed under the MIT License
* (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
*/
#include "MyGUI_Precompiled.h"
#include "MyGUI_PointerManager.h"
#include "MyGUI_ResourceManager.h"
#include "MyGUI_LayerManager.h"
#include "MyGUI_CoordConverter.h"
#include "MyGUI_WidgetManager.h"
#include "MyGUI_XmlDocument.h"
#include "MyGUI_Widget.h"
#include "MyGUI_FactoryManager.h"
#include "MyGUI_InputManager.h"
#include "MyGUI_Gui.h"
#include "MyGUI_ResourceManualPointer.h"
#include "MyGUI_ResourceImageSetPointer.h"
namespace MyGUI
{
MYGUI_SINGLETON_DEFINITION(PointerManager);
PointerManager::PointerManager() :
mXmlPointerTagName("Pointer"),
mXmlPropertyTagName("Property"),
mXmlDefaultPointerValue("Default"),
mSingletonHolder(this)
{
}
void PointerManager::initialise()
{
MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
Gui::getInstance().eventFrameStart += newDelegate(this, &PointerManager::notifyFrameStart);
InputManager::getInstance().eventChangeMouseFocus += newDelegate(this, &PointerManager::notifyChangeMouseFocus);
WidgetManager::getInstance().registerUnlinker(this);
ResourceManager::getInstance().registerLoadXmlDelegate(mXmlPointerTagName) =
newDelegate(this, &PointerManager::_load);
const std::string& resourceCategory = ResourceManager::getInstance().getCategoryName();
FactoryManager::getInstance().registerFactory<ResourceManualPointer>(resourceCategory);
FactoryManager::getInstance().registerFactory<ResourceImageSetPointer>(resourceCategory);
mPointer = nullptr;
mMousePointer = nullptr;
mWidgetOwner = nullptr;
mVisible = true;
mSkinName = "ImageBox";
MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
mIsInitialise = true;
}
void PointerManager::shutdown()
{
MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
InputManager::getInstance().eventChangeMouseFocus -= newDelegate(this, &PointerManager::notifyChangeMouseFocus);
Gui::getInstance().eventFrameStart -= newDelegate(this, &PointerManager::notifyFrameStart);
const std::string& resourceCategory = ResourceManager::getInstance().getCategoryName();
FactoryManager::getInstance().unregisterFactory<ResourceManualPointer>(resourceCategory);
FactoryManager::getInstance().unregisterFactory<ResourceImageSetPointer>(resourceCategory);
// удаляем все виджеты
_destroyAllChildWidget();
mWidgetOwner = nullptr;
WidgetManager::getInstance().unregisterUnlinker(this);
ResourceManager::getInstance().unregisterLoadXmlDelegate(mXmlPointerTagName);
MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
mIsInitialise = false;
}
void PointerManager::_load(xml::ElementPtr _node, std::string_view _file, Version _version)
{
#ifndef MYGUI_DONT_USE_OBSOLETE
loadOldPointerFormat(_node, _file, _version, mXmlPointerTagName);
#endif // MYGUI_DONT_USE_OBSOLETE
xml::ElementEnumerator node = _node->getElementEnumerator();
while (node.next())
{
if (node->getName() == mXmlPropertyTagName)
{
std::string_view key = node->findAttribute("key");
std::string_view value = node->findAttribute("value");
if (key == "Default")
setDefaultPointer(value);
else if (key == "Layer")
setLayerName(value);
else if (key == "Skin")
mSkinName = value;
}
}
}
void PointerManager::notifyFrameStart(float _time)
{
mPoint = InputManager::getInstance().getMousePosition();
if (mOldPoint != mPoint)
{
mOldPoint = mPoint;
if (nullptr != mMousePointer && mPointer != nullptr)
mPointer->setPosition(mMousePointer, mPoint);
}
}
void PointerManager::setVisible(bool _visible)
{
if (nullptr != mMousePointer)
mMousePointer->setVisible(_visible);
mVisible = _visible;
}
void PointerManager::setPointer(std::string_view _name, Widget* _owner)
{
if (nullptr == mMousePointer)
return;
IResource* result = getByName(_name);
if (result == nullptr)
{
mPointer = nullptr;
mMousePointer->setVisible(false);
return;
}
mMousePointer->setVisible(mVisible);
mPointer = result->castType<IPointer>();
mPointer->setImage(mMousePointer);
mPointer->setPosition(mMousePointer, mPoint);
mWidgetOwner = _owner;
}
void PointerManager::_unlinkWidget(Widget* _widget)
{
if (_widget == mWidgetOwner)
setPointer(mDefaultName, nullptr);
else if (_widget == mMousePointer)
mMousePointer = nullptr;
}
void PointerManager::resetToDefaultPointer()
{
setPointer(mDefaultName, nullptr);
}
// создает виджет
Widget* PointerManager::baseCreateWidget(
WidgetStyle _style,
std::string_view _type,
std::string_view _skin,
const IntCoord& _coord,
Align _align,
std::string_view _layer,
std::string_view _name)
{
Widget* widget =
WidgetManager::getInstance()
.createWidget(_style, _type, _skin, _coord, /*_align, */ nullptr, nullptr, /*this, */ _name);
mWidgetChild.push_back(widget);
widget->setAlign(_align);
// присоединяем виджет с уровню
if (!_layer.empty())
LayerManager::getInstance().attachToLayerNode(_layer, widget);
return widget;
}
// удаляет всех детей
void PointerManager::_destroyAllChildWidget()
{
WidgetManager& manager = WidgetManager::getInstance();
while (!mWidgetChild.empty())
{
// сразу себя отписывем, иначе вложенной удаление убивает все
Widget* widget = mWidgetChild.back();
mWidgetChild.pop_back();
// отписываем от всех
manager.unlinkFromUnlinkers(widget);
// и сами удалим, так как его больше в списке нет
WidgetManager::getInstance()._deleteWidget(widget);
}
}
void PointerManager::setDefaultPointer(std::string_view _value)
{
Update();
mDefaultName = _value;
setPointer(mDefaultName, nullptr);
}
void PointerManager::setLayerName(std::string_view _value)
{
Update();
mLayerName = _value;
if (LayerManager::getInstance().isExist(_value))
LayerManager::getInstance().attachToLayerNode(mLayerName, mMousePointer);
}
void PointerManager::Update()
{
if (mMousePointer == nullptr)
mMousePointer = static_cast<ImageBox*>(baseCreateWidget(
WidgetStyle::Overlapped,
ImageBox::getClassTypeName(),
mSkinName,
IntCoord(),
Align::Default,
std::string_view{},
std::string_view{}));
}
IPointer* PointerManager::getByName(std::string_view _name) const
{
IResource* result = nullptr;
if (!_name.empty() && _name != mXmlDefaultPointerValue)
result = ResourceManager::getInstance().getByName(_name, false);
if (result == nullptr)
result = ResourceManager::getInstance().getByName(mDefaultName, false);
return result ? result->castType<IPointer>(false) : nullptr;
}
void PointerManager::notifyChangeMouseFocus(Widget* _widget)
{
std::string_view pointer;
if (_widget != nullptr && _widget->getInheritedEnabled())
pointer = _widget->getPointer();
if (pointer != mCurrentMousePointer)
{
mCurrentMousePointer = pointer;
if (mCurrentMousePointer.empty())
{
resetToDefaultPointer();
eventChangeMousePointer(mDefaultName);
}
else
{
setPointer(mCurrentMousePointer, _widget);
eventChangeMousePointer(mCurrentMousePointer);
}
}
}
void PointerManager::setPointer(std::string_view _name)
{
setPointer(_name, nullptr);
}
bool PointerManager::isVisible() const
{
return mVisible;
}
const std::string& PointerManager::getDefaultPointer() const
{
return mDefaultName;
}
const std::string& PointerManager::getLayerName() const
{
return mLayerName;
}
} // 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