MyGUI_LayerManager.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_LayerManager.h"
#include "MyGUI_LayerItem.h"
#include "MyGUI_WidgetManager.h"
#include "MyGUI_RenderManager.h"
#include "MyGUI_Widget.h"
#include "MyGUI_FactoryManager.h"
#include "MyGUI_SharedLayer.h"
#include "MyGUI_OverlappedLayer.h"
namespace MyGUI
{
MYGUI_SINGLETON_DEFINITION(LayerManager);
LayerManager::LayerManager() :
mCategoryName("Layer"),
mSingletonHolder(this)
{
}
void LayerManager::initialise()
{
MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
WidgetManager::getInstance().registerUnlinker(this);
ResourceManager::getInstance().registerLoadXmlDelegate(mCategoryName) = newDelegate(this, &LayerManager::_load);
FactoryManager::getInstance().registerFactory<SharedLayer>(mCategoryName);
FactoryManager::getInstance().registerFactory<OverlappedLayer>(mCategoryName);
MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
mIsInitialise = true;
}
void LayerManager::shutdown()
{
MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
FactoryManager::getInstance().unregisterFactory<SharedLayer>(mCategoryName);
FactoryManager::getInstance().unregisterFactory<OverlappedLayer>(mCategoryName);
// удаляем все хранители слоев
clear();
WidgetManager::getInstance().unregisterUnlinker(this);
ResourceManager::getInstance().unregisterLoadXmlDelegate(mCategoryName);
MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
mIsInitialise = false;
}
void LayerManager::clear()
{
for (auto& layerNode : mLayerNodes)
{
destroy(layerNode);
}
mLayerNodes.clear();
}
void LayerManager::_load(xml::ElementPtr _node, std::string_view _file, Version _version)
{
VectorLayer layers;
// берем детей и крутимся, основной цикл
xml::ElementEnumerator layer = _node->getElementEnumerator();
while (layer.next(mCategoryName))
{
std::string name;
if (!layer->findAttribute("name", name))
{
MYGUI_LOG(Warning, "Attribute 'name' not found (file : " << _file << ")");
continue;
}
for (const auto& item : layers)
{
MYGUI_ASSERT(item->getName() != name, "Layer '" << name << "' already exist (file : " << _file << ")");
}
std::string_view type = layer->findAttribute("type");
if (type.empty() && _version <= Version(1, 0))
{
bool overlapped = utility::parseBool(layer->findAttribute("overlapped"));
type = overlapped ? "OverlappedLayer" : "SharedLayer";
}
ILayer* item = _createLayerObject(type);
item->deserialization(layer.current(), _version);
layers.push_back(item);
}
// теперь мержим новые и старые слои
merge(layers);
}
ILayer* LayerManager::_createLayerObject(std::string_view _type)
{
IObject* object = FactoryManager::getInstance().createObject(mCategoryName, _type);
MYGUI_ASSERT(object != nullptr, "factory '" << _type << "' is not found");
return object->castType<ILayer>();
}
void LayerManager::_unlinkWidget(Widget* _widget)
{
detachFromLayer(_widget);
}
// поправить на виджет и проверять на рутовость
void LayerManager::attachToLayerNode(std::string_view _name, Widget* _item)
{
MYGUI_ASSERT(nullptr != _item, "pointer must be valid");
MYGUI_ASSERT(_item->isRootWidget(), "attached widget must be root");
// сначала отсоединяем
_item->detachFromLayer();
// а теперь аттачим
for (auto& layerNode : mLayerNodes)
{
if (_name == layerNode->getName())
{
ILayerNode* node = layerNode->createChildItemNode();
node->attachLayerItem(_item);
return;
}
}
MYGUI_LOG(Error, "Layer '" << _name << "' is not found");
//MYGUI_EXCEPT("Layer '" << _name << "' is not found");
}
void LayerManager::detachFromLayer(Widget* _item)
{
MYGUI_ASSERT(nullptr != _item, "pointer must be valid");
_item->detachFromLayer();
}
void LayerManager::upLayerItem(Widget* _item)
{
MYGUI_ASSERT(nullptr != _item, "pointer must be valid");
_item->upLayerItem();
}
bool LayerManager::isExist(std::string_view _name) const
{
return getByName(_name, false) != nullptr;
}
void LayerManager::merge(VectorLayer& _layers)
{
for (auto& layerNode : mLayerNodes)
{
if (layerNode == nullptr)
continue;
bool find = false;
const std::string& name = layerNode->getName();
for (auto& layer : _layers)
{
if (name == layer->getName())
{
// заменяем новый слой, на уже существующий
delete layer;
layer = layerNode;
layerNode = nullptr;
find = true;
break;
}
}
if (!find)
{
destroy(layerNode);
layerNode = nullptr;
}
}
// теперь в основной
mLayerNodes = _layers;
}
ILayer* LayerManager::createLayerAt(std::string_view _name, std::string_view _type, size_t _index)
{
MYGUI_ASSERT_RANGE(_index, mLayerNodes.size() + 1, "LayerManager::getLayer");
ILayer* item = _createLayerObject(_type);
item->setName(_name);
mLayerNodes.insert(mLayerNodes.begin() + _index, item);
return item;
}
void LayerManager::destroy(ILayer* _layer)
{
MYGUI_LOG(Info, "destroy layer '" << _layer->getName() << "'");
delete _layer;
}
Widget* LayerManager::getWidgetFromPoint(int _left, int _top) const
{
VectorLayer::const_reverse_iterator iter = mLayerNodes.rbegin();
while (iter != mLayerNodes.rend())
{
ILayerItem* item = (*iter)->getLayerItemByPoint(_left, _top);
if (item != nullptr)
return static_cast<Widget*>(item);
++iter;
}
return nullptr;
}
void LayerManager::renderToTarget(IRenderTarget* _target, bool _update)
{
for (auto& layerNode : mLayerNodes)
{
layerNode->renderToTarget(_target, _update);
}
}
ILayer* LayerManager::getByName(std::string_view _name, bool _throw) const
{
for (const auto& layerNode : mLayerNodes)
{
if (_name == layerNode->getName())
return layerNode;
}
MYGUI_ASSERT(!_throw, "Layer '" << _name << "' not found");
return nullptr;
}
LayerManager::EnumeratorLayer LayerManager::getEnumerator() const
{
return EnumeratorLayer(mLayerNodes);
}
void LayerManager::resizeView(const IntSize& _viewSize)
{
for (const auto& node : mLayerNodes)
node->resizeView(_viewSize);
}
size_t LayerManager::getLayerCount() const
{
return mLayerNodes.size();
}
ILayer* LayerManager::getLayer(size_t _index) const
{
MYGUI_ASSERT_RANGE(_index, mLayerNodes.size(), "LayerManager::getLayer");
return mLayerNodes[_index];
}
const std::string& LayerManager::getCategoryName() const
{
return mCategoryName;
}
} // namespace MyGUI
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition MyGUI_Diagnostic.h:31
MYGUI_ASSERT_RANGE
#define MYGUI_ASSERT_RANGE(index, size, owner)
Definition MyGUI_Diagnostic.h:44
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition MyGUI_Diagnostic.h:17
MyGUI_FactoryManager.h
MyGUI_LayerItem.h
MyGUI_LayerManager.h
MyGUI_OverlappedLayer.h
MyGUI_Precompiled.h
MyGUI_RenderManager.h
MyGUI_SharedLayer.h
MYGUI_SINGLETON_DEFINITION
#define MYGUI_SINGLETON_DEFINITION(ClassName)
Definition MyGUI_Singleton.h:126
MyGUI_Widget.h
MyGUI_WidgetManager.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::FactoryManager::createObject
IObject * createObject(std::string_view _category, std::string_view _type)
Definition MyGUI_FactoryManager.cpp:80
MyGUI::ILayer
Definition MyGUI_ILayer.h:22
MyGUI::ILayer::getName
const std::string & getName() const
Definition MyGUI_ILayer.h:28
MyGUI::ILayer::setName
void setName(std::string_view _name)
Definition MyGUI_ILayer.h:33
MyGUI::ILayerItem
Definition MyGUI_ILayerItem.h:20
MyGUI::ILayerItem::getLayerItemByPoint
virtual ILayerItem * getLayerItemByPoint(int _left, int _top) const =0
MyGUI::ILayerNode
Definition MyGUI_ILayerNode.h:29
MyGUI::ILayerNode::attachLayerItem
virtual void attachLayerItem(ILayerItem *_item)=0
MyGUI::ILayerNode::createChildItemNode
virtual ILayerNode * createChildItemNode()=0
MyGUI::IRenderTarget
Definition MyGUI_IRenderTarget.h:21
MyGUI::LayerItem::detachFromLayer
void detachFromLayer() override
Definition MyGUI_LayerItem.cpp:96
MyGUI::LayerItem::upLayerItem
void upLayerItem() override
Definition MyGUI_LayerItem.cpp:120
MyGUI::LayerManager
Definition MyGUI_LayerManager.h:23
MyGUI::LayerManager::LayerManager
LayerManager()
Definition MyGUI_LayerManager.cpp:23
MyGUI::LayerManager::detachFromLayer
void detachFromLayer(Widget *_item)
Definition MyGUI_LayerManager.cpp:145
MyGUI::LayerManager::upLayerItem
void upLayerItem(Widget *_item)
Definition MyGUI_LayerManager.cpp:151
MyGUI::LayerManager::getLayer
ILayer * getLayer(size_t _index) const
Definition MyGUI_LayerManager.cpp:259
MyGUI::LayerManager::getCategoryName
const std::string & getCategoryName() const
Definition MyGUI_LayerManager.cpp:265
MyGUI::LayerManager::getClassTypeName
static std::string_view getClassTypeName()
MyGUI::LayerManager::getEnumerator
EnumeratorLayer getEnumerator() const
Definition MyGUI_LayerManager.cpp:243
MyGUI::LayerManager::getLayerCount
size_t getLayerCount() const
Definition MyGUI_LayerManager.cpp:254
MyGUI::LayerManager::createLayerAt
ILayer * createLayerAt(std::string_view _name, std::string_view _type, size_t _index)
Definition MyGUI_LayerManager.cpp:193
MyGUI::LayerManager::renderToTarget
void renderToTarget(IRenderTarget *_target, bool _update)
Definition MyGUI_LayerManager.cpp:224
MyGUI::LayerManager::shutdown
void shutdown()
Definition MyGUI_LayerManager.cpp:44
MyGUI::LayerManager::VectorLayer
std::vector< ILayer * > VectorLayer
Definition MyGUI_LayerManager.h:27
MyGUI::LayerManager::getByName
ILayer * getByName(std::string_view _name, bool _throw=true) const
Definition MyGUI_LayerManager.cpp:232
MyGUI::LayerManager::resizeView
void resizeView(const IntSize &_viewSize)
Definition MyGUI_LayerManager.cpp:248
MyGUI::LayerManager::attachToLayerNode
void attachToLayerNode(std::string_view _name, Widget *_item)
Definition MyGUI_LayerManager.cpp:122
MyGUI::LayerManager::getWidgetFromPoint
Widget * getWidgetFromPoint(int _left, int _top) const
Definition MyGUI_LayerManager.cpp:211
MyGUI::LayerManager::initialise
void initialise()
Definition MyGUI_LayerManager.cpp:29
MyGUI::LayerManager::EnumeratorLayer
Enumerator< VectorLayer > EnumeratorLayer
Definition MyGUI_LayerManager.h:28
MyGUI::LayerManager::isExist
bool isExist(std::string_view _name) const
Definition MyGUI_LayerManager.cpp:157
MyGUI::OverlappedLayer
Definition MyGUI_OverlappedLayer.h:18
MyGUI::ResourceManager::unregisterLoadXmlDelegate
void unregisterLoadXmlDelegate(std::string_view _key)
Definition MyGUI_ResourceManager.cpp:127
MyGUI::ResourceManager::registerLoadXmlDelegate
LoadXmlDelegate & registerLoadXmlDelegate(std::string_view _key)
Definition MyGUI_ResourceManager.cpp:120
MyGUI::ResourceManager::getInstance
static ResourceManager & getInstance()
MyGUI::SharedLayer
Definition MyGUI_SharedLayer.h:19
MyGUI::Version
Definition MyGUI_Version.h:18
MyGUI::Widget
widget description should be here.
Definition MyGUI_Widget.h:40
MyGUI::Widget::isRootWidget
bool isRootWidget() const
Definition MyGUI_Widget.cpp:1347
MyGUI::WidgetManager::unregisterUnlinker
void unregisterUnlinker(IUnlinkWidget *_unlink)
Definition MyGUI_WidgetManager.cpp:144
MyGUI::WidgetManager::getInstance
static WidgetManager & getInstance()
MyGUI::WidgetManager::registerUnlinker
void registerUnlinker(IUnlinkWidget *_unlink)
Definition MyGUI_WidgetManager.cpp:138
MyGUI::utility::parseBool
bool parseBool(std::string_view _value)
Definition MyGUI_StringUtility.h:127
MyGUI::xml::ElementPtr
Element * ElementPtr
Definition MyGUI_XmlDocument.h:112
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::IntSize
types::TSize< int > IntSize
Definition MyGUI_Types.h:30
MyGUI::newDelegate
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
Definition MyGUI_Delegate.h:100