1 /*!
2 	@file
3 	@author		Albert Semenov
4 	@date		07/2012
5 */
6 
7 #include "Precompiled.h"
8 #include "FontTextureController.h"
9 #include "FactoryManager.h"
10 #include "DataSelectorManager.h"
11 #include "DataManager.h"
12 #include "PropertyUtility.h"
13 #include "ScopeManager.h"
14 #include "DataUtility.h"
15 #include "CommandManager.h"
16 #include <MyGUI_ResourceTrueTypeFont.h>
17 #include "FontExportSerializer.h"
18 
19 namespace tools
20 {
21 
FACTORY_ITEM_ATTRIBUTE(FontTextureController)22 	FACTORY_ITEM_ATTRIBUTE(FontTextureController)
23 
24 	FontTextureController::FontTextureController() :
25 		mControl(nullptr),
26 		mParentData(nullptr),
27 		mActivated(false)
28 	{
29 	}
30 
setTarget(Control * _control)31 	void FontTextureController::setTarget(Control* _control)
32 	{
33 		mControl = _control->findControl<ScopeTextureControl>();
34 	}
35 
activate()36 	void FontTextureController::activate()
37 	{
38 		mParentTypeName = "Root";
39 		mScopeName = "Font";
40 
41 		ScopeManager::getInstance().eventChangeScope.connect(this, &FontTextureController::notifyChangeScope);
42 		notifyChangeScope(ScopeManager::getInstance().getCurrentScope());
43 
44 		CommandManager::getInstance().getEvent("Command_GenerateFont")->connect(this, &FontTextureController::commandGenerateFont);
45 	}
46 
deactivate()47 	void FontTextureController::deactivate()
48 	{
49 		ScopeManager::getInstance().eventChangeScope.disconnect(this);
50 		CommandManager::getInstance().getEvent("Command_GenerateFont")->disconnect(this);
51 	}
52 
notifyChangeDataSelector(DataPtr _data,bool _changeOnlySelection)53 	void FontTextureController::notifyChangeDataSelector(DataPtr _data, bool _changeOnlySelection)
54 	{
55 		mParentData = _data;
56 		if (mParentData != nullptr && mParentData->getType()->getName() != mParentTypeName)
57 			mParentData = nullptr;
58 
59 		std::string texture;
60 		PropertyPtr property = PropertyUtility::getPropertyByName("Font", "FontName");
61 		if (property != nullptr)
62 		{
63 			texture = property->getValue();
64 		}
65 
66 		updateTexture(texture);
67 	}
68 
notifyChangeProperty(PropertyPtr _sender)69 	void FontTextureController::notifyChangeProperty(PropertyPtr _sender)
70 	{
71 		if (!mActivated || !PropertyUtility::isDataSelected(_sender->getOwner()))
72 			return;
73 
74 		if (_sender->getOwner()->getType()->getName() == "Font")
75 		{
76 			if (_sender->getType()->getName() == "FontName")
77 				updateTexture(_sender->getValue());
78 		}
79 	}
80 
notifyChangeScope(const std::string & _scope)81 	void FontTextureController::notifyChangeScope(const std::string& _scope)
82 	{
83 		if (mControl == nullptr)
84 			return;
85 
86 		if (_scope == mScopeName)
87 		{
88 			if (!mActivated)
89 			{
90 				mControl->clearAll();
91 
92 				DataSelectorManager::getInstance().getEvent(mParentTypeName)->connect(this, &FontTextureController::notifyChangeDataSelector);
93 				mParentData = DataUtility::getSelectedDataByType(mParentTypeName);
94 				notifyChangeDataSelector(mParentData, false);
95 
96 				mControl->getRoot()->setUserString("CurrentScopeController", mScopeName);
97 
98 				mActivated = true;
99 			}
100 		}
101 		else
102 		{
103 			if (mActivated)
104 			{
105 				DataSelectorManager::getInstance().getEvent(mParentTypeName)->disconnect(this);
106 				mParentData = nullptr;
107 
108 				// мы еще владельцы контрола сбрасываем его
109 				std::string value = mControl->getRoot()->getUserString("CurrentScopeController");
110 				if (value == mScopeName)
111 				{
112 					mControl->getRoot()->setUserString("CurrentScopeController", "");
113 					notifyChangeDataSelector(mParentData, false);
114 
115 					mControl->clearAll();
116 				}
117 
118 				mActivated = false;
119 			}
120 		}
121 	}
122 
updateTexture(const std::string & _value)123 	void FontTextureController::updateTexture(const std::string& _value)
124 	{
125 		MyGUI::IResource* resource = MyGUI::ResourceManager::getInstance().findByName(_value);
126 		MyGUI::ResourceTrueTypeFont* font = resource != nullptr ? resource->castType<MyGUI::ResourceTrueTypeFont>(false) : nullptr;
127 
128 		MyGUI::ITexture* texture = font != nullptr ? font->getTextureFont() : nullptr;
129 		std::string value = texture != nullptr ? texture->getName() : "";
130 
131 		mControl->setTextureValue(value);
132 		mControl->resetTextureRegion();
133 	}
134 
commandGenerateFont(const MyGUI::UString & _commandName,bool & _result)135 	void FontTextureController::commandGenerateFont(const MyGUI::UString& _commandName, bool& _result)
136 	{
137 		if (mParentData != nullptr)
138 		{
139 			DataPtr font = (mParentData != nullptr) ? mParentData->getChildSelected() : nullptr;
140 			if (font != nullptr)
141 			{
142 				FontExportSerializer::generateFont(font);
143 				notifyChangeDataSelector(mParentData, false);
144 				updateResultPropery(font);
145 
146 				CommandManager::getInstance().executeCommand("Command_OnGenerateFont");
147 			}
148 		}
149 	}
150 
updateResultPropery(DataPtr _data)151 	void FontTextureController::updateResultPropery(DataPtr _data)
152 	{
153 		MyGUI::IResource* resource = MyGUI::ResourceManager::getInstance().findByName(_data->getPropertyValue("FontName"));
154 		MyGUI::ResourceTrueTypeFont* font = resource != nullptr ? resource->castType<MyGUI::ResourceTrueTypeFont>(false) : nullptr;
155 		MyGUI::ITexture* texture = font != nullptr ? font->getTextureFont() : nullptr;
156 
157 		if (texture != nullptr)
158 			_data->setPropertyValue("TextureSizeResult", MyGUI::utility::toString(texture->getWidth(), " ", texture->getHeight()));
159 		else
160 			_data->setPropertyValue("TextureSizeResult", "0 0");
161 
162 		if (font != nullptr)
163 			_data->setPropertyValue("FontHeightPix", font->getDefaultHeight());
164 		else
165 			_data->setPropertyValue("FontHeightPix", "0");
166 	}
167 
168 }
169