~ubuntu-branches/ubuntu/vivid/mygui/vivid

« back to all changes in this revision

Viewing changes to Tools/LayoutEditor/EditorWidgets.cpp

  • Committer: Package Import Robot
  • Author(s): Scott Howard, Bret Curtis, Scott Howard
  • Date: 2014-09-18 17:57:48 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20140918175748-dd8va78mvpw1jbes
Tags: 3.2.1-1
[ Bret Curtis ]
* Updated license for majority of files from LGPL to Expat (MIT)

[ Scott Howard ]
* New upstream release
* Updated patch to add build option for system GLEW libraries
* All patches accepted upstream except shared_libraries.patch
* Bumped SONAME due to dropped symbols, updated *.symbols and package
  names
* Updated license of debian/* to Expat with permission of all authors
* Don't install Doxygen autogenerated md5 and map files (thanks
  lintian)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "Precompiled.h"
2
 
#include "EditorWidgets.h"
3
 
#include "Common.h"
4
 
#include "WidgetTypes.h"
5
 
#include "GroupMessage.h"
6
 
#include "BackwardCompatibilityManager.h"
7
 
#include "WidgetSelectorManager.h"
8
 
#include "SettingsManager.h"
9
 
 
10
 
template <> tools::EditorWidgets* MyGUI::Singleton<tools::EditorWidgets>::msInstance = nullptr;
11
 
template <> const char* MyGUI::Singleton<tools::EditorWidgets>::mClassTypeName("EditorWidgets");
12
 
 
13
 
namespace tools
14
 
{
15
 
        const std::string LogSection = "LayoutEditor";
16
 
 
17
 
        EditorWidgets::EditorWidgets() :
18
 
                mWidgetsChanged(false)
19
 
        {
20
 
        }
21
 
 
22
 
        EditorWidgets::~EditorWidgets()
23
 
        {
24
 
                destroyAllSectors();
25
 
        }
26
 
 
27
 
        void EditorWidgets::initialise()
28
 
        {
29
 
                mWidgetsChanged = true;
30
 
 
31
 
                MyGUI::ResourceManager::getInstance().registerLoadXmlDelegate("IgnoreParameters") = MyGUI::newDelegate(this, &EditorWidgets::loadIgnoreParameters);
32
 
                MyGUI::ResourceManager::getInstance().registerLoadXmlDelegate("SkinReplace") = MyGUI::newDelegate(this, &EditorWidgets::loadSkinReplace);
33
 
 
34
 
                MyGUI::Gui::getInstance().eventFrameStart += MyGUI::newDelegate(this, &EditorWidgets::notifyFrameStarted);
35
 
                MyGUI::WidgetManager::getInstance().registerUnlinker(this);
36
 
        }
37
 
 
38
 
        void EditorWidgets::shutdown()
39
 
        {
40
 
                MyGUI::WidgetManager::getInstance().unregisterUnlinker(this);
41
 
                MyGUI::Gui::getInstance().eventFrameStart -= MyGUI::newDelegate(this, &EditorWidgets::notifyFrameStarted);
42
 
 
43
 
                destroyAllWidgets();
44
 
                destroyAllSectors();
45
 
        }
46
 
 
47
 
        void EditorWidgets::destroyAllWidgets()
48
 
        {
49
 
                for (std::vector<WidgetContainer*>::iterator iter = mWidgets.begin(); iter != mWidgets.end(); ++iter)
50
 
                        delete *iter;
51
 
                mWidgets.clear();
52
 
        }
53
 
 
54
 
        bool EditorWidgets::load(const MyGUI::UString& _fileName)
55
 
        {
56
 
                size_t index = _fileName.find("|");
57
 
                if (index != MyGUI::UString::npos)
58
 
                {
59
 
                        MyGUI::UString fileName = _fileName.substr(0, index);
60
 
                        MyGUI::UString itemIndex = _fileName.substr(index + 1);
61
 
 
62
 
                        return loadFromProject(fileName, MyGUI::utility::parseValue<size_t>(itemIndex));
63
 
                }
64
 
 
65
 
                mCurrentFileName = _fileName;
66
 
                mCurrentItemName.clear();
67
 
 
68
 
                MyGUI::xml::Document doc;
69
 
                if (!doc.open(_fileName))
70
 
                {
71
 
                        MYGUI_LOGGING(LogSection, Error, getClassTypeName() << " : " << doc.getLastError());
72
 
                        return false;
73
 
                }
74
 
 
75
 
                MyGUI::xml::ElementPtr root = doc.getRoot();
76
 
                if ((nullptr == root) || (root->getName() != "MyGUI"))
77
 
                {
78
 
                        MYGUI_LOGGING(LogSection, Error, getClassTypeName() << " : '" << _fileName << "', tag 'MyGUI' not found");
79
 
                        return false;
80
 
                }
81
 
 
82
 
                if (root->findAttribute("type") == "Layout")
83
 
                {
84
 
                        loadWidgetsFromXmlNode(root);
85
 
                }
86
 
                else
87
 
                {
88
 
                        return false;
89
 
                }
90
 
 
91
 
                mWidgetsChanged = true;
92
 
                return true;
93
 
        }
94
 
 
95
 
        bool EditorWidgets::loadFromProject(const MyGUI::UString& _fileName, size_t _index)
96
 
        {
97
 
                mCurrentFileName = _fileName;
98
 
 
99
 
                MyGUI::xml::Document doc;
100
 
                if (!doc.open(_fileName))
101
 
                {
102
 
                        MYGUI_LOGGING(LogSection, Error, getClassTypeName() << " : " << doc.getLastError());
103
 
                        return false;
104
 
                }
105
 
 
106
 
                MyGUI::xml::ElementPtr root = doc.getRoot();
107
 
                if ((nullptr == root) || (root->getName() != "MyGUI"))
108
 
                {
109
 
                        MYGUI_LOGGING(LogSection, Error, getClassTypeName() << " : '" << _fileName << "', tag 'MyGUI' not found");
110
 
                        return false;
111
 
                }
112
 
 
113
 
                if (root->findAttribute("type") == "Resource")
114
 
                {
115
 
                        // берем детей и крутимся
116
 
                        MyGUI::xml::ElementEnumerator element = root->getElementEnumerator();
117
 
                        while (element.next("Resource"))
118
 
                        {
119
 
                                if (element->findAttribute("type") == "ResourceLayout")
120
 
                                {
121
 
                                        if (_index == 0)
122
 
                                        {
123
 
                                                mCurrentItemName = element->findAttribute("name");
124
 
 
125
 
                                                loadWidgetsFromXmlNode(element.current());
126
 
 
127
 
                                                break;
128
 
                                        }
129
 
                                        else
130
 
                                        {
131
 
                                                _index --;
132
 
                                        }
133
 
                                }
134
 
                        }
135
 
                }
136
 
                else
137
 
                {
138
 
                        return false;
139
 
                }
140
 
 
141
 
                mWidgetsChanged = true;
142
 
                return true;
143
 
        }
144
 
 
145
 
        bool EditorWidgets::save(const MyGUI::UString& _fileName)
146
 
        {
147
 
                size_t index = _fileName.find("|");
148
 
                if (index != MyGUI::UString::npos)
149
 
                {
150
 
                        MyGUI::UString fileName = _fileName.substr(0, index);
151
 
                        MyGUI::UString itemIndex = _fileName.substr(index + 1);
152
 
 
153
 
                        return saveToProject(fileName, MyGUI::utility::parseValue<size_t>(itemIndex));
154
 
                }
155
 
 
156
 
                mCurrentFileName = _fileName;
157
 
                mCurrentItemName.clear();
158
 
 
159
 
                MyGUI::xml::Document doc;
160
 
                doc.createDeclaration();
161
 
                MyGUI::xml::ElementPtr root = doc.createRoot("MyGUI");
162
 
                root->addAttribute("type", "Layout");
163
 
 
164
 
                saveWidgetsToXmlNode(root, true);
165
 
 
166
 
                if (!doc.save(_fileName))
167
 
                {
168
 
                        MYGUI_LOGGING(LogSection, Error, getClassTypeName() << " : " << doc.getLastError());
169
 
                        return false;
170
 
                }
171
 
 
172
 
                return true;
173
 
        }
174
 
 
175
 
        bool EditorWidgets::saveToProject(const MyGUI::UString& _fileName, size_t _index)
176
 
        {
177
 
                mCurrentFileName = _fileName;
178
 
 
179
 
                MyGUI::xml::Document doc;
180
 
                if (!doc.open(_fileName))
181
 
                {
182
 
                        MYGUI_LOGGING(LogSection, Error, getClassTypeName() << " : " << doc.getLastError());
183
 
                        return false;
184
 
                }
185
 
 
186
 
                MyGUI::xml::ElementPtr root = doc.getRoot();
187
 
                if ((nullptr == root) || (root->getName() != "MyGUI"))
188
 
                {
189
 
                        MYGUI_LOGGING(LogSection, Error, getClassTypeName() << " : '" << _fileName << "', tag 'MyGUI' not found");
190
 
                        return false;
191
 
                }
192
 
 
193
 
                if (root->findAttribute("type") == "Resource")
194
 
                {
195
 
                        // берем детей и крутимся
196
 
                        MyGUI::xml::ElementEnumerator element = root->getElementEnumerator();
197
 
                        while (element.next("Resource"))
198
 
                        {
199
 
                                if (element->findAttribute("type") == "ResourceLayout")
200
 
                                {
201
 
                                        if (_index == 0)
202
 
                                        {
203
 
                                                mCurrentItemName = element->findAttribute("name");
204
 
 
205
 
                                                element->clear();
206
 
                                                element->addAttribute("type", "ResourceLayout");
207
 
                                                element->addAttribute("name", mCurrentItemName);
208
 
 
209
 
                                                saveWidgetsToXmlNode(element.current(), true);
210
 
 
211
 
                                                if (!doc.save(_fileName))
212
 
                                                {
213
 
                                                        MYGUI_LOGGING(LogSection, Error, getClassTypeName() << " : " << doc.getLastError());
214
 
                                                        return false;
215
 
                                                }
216
 
 
217
 
                                                return true;
218
 
                                        }
219
 
                                        else
220
 
                                        {
221
 
                                                _index --;
222
 
                                        }
223
 
                                }
224
 
                        }
225
 
                        return false;
226
 
                }
227
 
 
228
 
                return false;
229
 
        }
230
 
 
231
 
        void EditorWidgets::loadxmlDocument(MyGUI::xml::Document* doc, bool _testMode)
232
 
        {
233
 
                MyGUI::xml::ElementPtr root = doc->getRoot();
234
 
 
235
 
                std::string type;
236
 
                if (root->findAttribute("type", type))
237
 
                {
238
 
                        if (type == "Layout")
239
 
                        {
240
 
                                loadWidgetsFromXmlNode(root, _testMode);
241
 
                        }
242
 
                }
243
 
                mWidgetsChanged = true;
244
 
        }
245
 
 
246
 
        MyGUI::xml::Document* EditorWidgets::savexmlDocument()
247
 
        {
248
 
                MyGUI::xml::Document* doc = new MyGUI::xml::Document();
249
 
 
250
 
                doc->createDeclaration();
251
 
                MyGUI::xml::ElementPtr root = doc->createRoot("MyGUI");
252
 
                root->addAttribute("type", "Layout");
253
 
 
254
 
                saveWidgetsToXmlNode(root);
255
 
 
256
 
                return doc;
257
 
        }
258
 
 
259
 
        void EditorWidgets::add(WidgetContainer* _container)
260
 
        {
261
 
                if (nullptr == _container->getWidget()->getParent())
262
 
                {
263
 
                        mWidgets.push_back(_container);
264
 
                }
265
 
                else
266
 
                {
267
 
                        MyGUI::Widget* parent = _container->getWidget()->getParent();
268
 
                        WidgetContainer* containerParent = find(parent);
269
 
                        while (nullptr == containerParent)
270
 
                        {
271
 
                                parent = parent->getParent();
272
 
                                if (parent == nullptr)
273
 
                                        return;
274
 
                                containerParent = find(parent);
275
 
                        }
276
 
                        containerParent->childContainers.push_back(_container);
277
 
                }
278
 
                mWidgetsChanged = true;
279
 
        }
280
 
 
281
 
        void EditorWidgets::remove(MyGUI::Widget* _widget)
282
 
        {
283
 
                remove(find(_widget));
284
 
                mWidgetsChanged = true;
285
 
        }
286
 
 
287
 
        void EditorWidgets::remove(WidgetContainer* _container)
288
 
        {
289
 
                if (nullptr != _container)
290
 
                {
291
 
                        std::vector<WidgetContainer*>::reverse_iterator iter;
292
 
                        while (_container->childContainers.empty() == false)
293
 
                        {
294
 
                                iter = _container->childContainers.rbegin();
295
 
                                remove(*iter);
296
 
                        }
297
 
 
298
 
                        if (nullptr == _container->getWidget()->getParent())
299
 
                        {
300
 
                                mWidgets.erase(std::find(mWidgets.begin(), mWidgets.end(), _container));
301
 
                        }
302
 
                        else
303
 
                        {
304
 
                                MyGUI::Widget* parent = _container->getWidget()->getParent();
305
 
                                WidgetContainer* containerParent = find(parent);
306
 
                                while (nullptr == containerParent)
307
 
                                {
308
 
                                        parent = parent->getParent();
309
 
                                        if (parent == nullptr)
310
 
                                        {
311
 
                                                // такого не должно быть, или утечка памяти,
312
 
                                                // так как удалять нельзя им пользуется код вызывающий данную функцию
313
 
                                                return;
314
 
                                        }
315
 
                                        containerParent = find(parent);
316
 
                                }
317
 
 
318
 
                                containerParent->childContainers.erase(std::find(containerParent->childContainers.begin(), containerParent->childContainers.end(), _container));
319
 
                        }
320
 
 
321
 
                        MyGUI::Gui::getInstance().destroyWidget(_container->getWidget());
322
 
 
323
 
                        delete _container;
324
 
                }
325
 
 
326
 
                mWidgetsChanged = true;
327
 
        }
328
 
 
329
 
        bool EditorWidgets::unbind(WidgetContainer* _container)
330
 
        {
331
 
                bool result = false;
332
 
 
333
 
                if (nullptr != _container)
334
 
                {
335
 
                        std::vector<WidgetContainer*>::reverse_iterator iter;
336
 
                        while (_container->childContainers.empty() == false)
337
 
                        {
338
 
                                iter = _container->childContainers.rbegin();
339
 
                                if (unbind(*iter))
340
 
                                        result = true;
341
 
                        }
342
 
 
343
 
                        if (nullptr == _container->getWidget()->getParent())
344
 
                        {
345
 
                                mWidgets.erase(std::find(mWidgets.begin(), mWidgets.end(), _container));
346
 
                        }
347
 
                        else
348
 
                        {
349
 
                                MyGUI::Widget* parent = _container->getWidget()->getParent();
350
 
                                WidgetContainer* containerParent = find(parent);
351
 
                                while (nullptr == containerParent)
352
 
                                {
353
 
                                        parent = parent->getParent();
354
 
                                        if (parent == nullptr)
355
 
                                        {
356
 
                                                // такого не должно быть, или утечка памяти,
357
 
                                                // так как удалять нельзя им пользуется код вызывающий данную функцию
358
 
                                                return result;
359
 
                                        }
360
 
                                        containerParent = find(parent);
361
 
                                }
362
 
 
363
 
                                containerParent->childContainers.erase(std::find(containerParent->childContainers.begin(), containerParent->childContainers.end(), _container));
364
 
                        }
365
 
 
366
 
                        delete _container;
367
 
                        result = true;
368
 
                }
369
 
 
370
 
                mWidgetsChanged = true;
371
 
                return result;
372
 
        }
373
 
 
374
 
        void EditorWidgets::clear()
375
 
        {
376
 
                mCurrentFileName.clear();
377
 
                mCurrentItemName.clear();
378
 
 
379
 
                while (!mWidgets.empty())
380
 
                {
381
 
                        remove(mWidgets[mWidgets.size()-1]);
382
 
                }
383
 
 
384
 
                destroyAllSectors();
385
 
        }
386
 
 
387
 
        WidgetContainer* EditorWidgets::find(MyGUI::Widget* _widget)
388
 
        {
389
 
                return _find(_widget, "", mWidgets);
390
 
        }
391
 
 
392
 
        WidgetContainer* EditorWidgets::find(const std::string& _name)
393
 
        {
394
 
                return _find(nullptr, _name, mWidgets);
395
 
        }
396
 
 
397
 
        WidgetContainer* EditorWidgets::_find(MyGUI::Widget* _widget, const std::string& _name, std::vector<WidgetContainer*> _widgets)
398
 
        {
399
 
                for (std::vector<WidgetContainer*>::iterator iter = _widgets.begin(); iter != _widgets.end(); ++iter)
400
 
                {
401
 
                        if (((*iter)->getWidget() == _widget) || ((_name.empty() == false) && ((*iter)->getName() == _name)))
402
 
                        {
403
 
                                return *iter;
404
 
                        }
405
 
                        WidgetContainer* retContainer = _find(_widget, _name, (*iter)->childContainers);
406
 
                        if (retContainer)
407
 
                                return retContainer;
408
 
                }
409
 
                return nullptr;
410
 
        }
411
 
 
412
 
        void EditorWidgets::parseWidget(MyGUI::xml::ElementEnumerator& _widget, MyGUI::Widget* _parent, bool _testMode)
413
 
        {
414
 
                WidgetContainer* container = new WidgetContainer();
415
 
                // парсим атрибуты виджета
416
 
                MyGUI::IntCoord coord;
417
 
                MyGUI::Align align = MyGUI::Align::Default;
418
 
                MyGUI::WidgetStyle widgetStyle = MyGUI::WidgetStyle::Child;
419
 
                std::string position;
420
 
 
421
 
                container->setName(_widget->findAttribute("name"));
422
 
                container->setType(_widget->findAttribute("type"));
423
 
                container->setSkin(_widget->findAttribute("skin"));
424
 
                container->setLayerName(_widget->findAttribute("layer"));
425
 
                std::string tmp;
426
 
                if (_widget->findAttribute("style", tmp))
427
 
                {
428
 
                        container->setStyle(tmp);
429
 
                        widgetStyle = MyGUI::WidgetStyle::parse(tmp);
430
 
                }
431
 
                if (_widget->findAttribute("align", tmp))
432
 
                {
433
 
                        container->setAlign(tmp);
434
 
                        align = MyGUI::Align::parse(tmp);
435
 
                }
436
 
                if (_widget->findAttribute("position", position))
437
 
                        coord = MyGUI::IntCoord::parse(position);
438
 
                if (_widget->findAttribute("position_real", position))
439
 
                {
440
 
                        container->setRelativeMode(true);
441
 
                        SettingsSector* sector = SettingsManager::getInstance().getSector("Workspace");
442
 
                        MyGUI::IntSize size = _testMode ? MyGUI::RenderManager::getInstance().getViewSize() : sector->getPropertyValue<MyGUI::IntSize>("TextureSize");
443
 
                        coord = MyGUI::CoordConverter::convertFromRelative(MyGUI::FloatCoord::parse(position), _parent == nullptr ? size : _parent->getClientCoord().size());
444
 
                }
445
 
 
446
 
                // проверяем скин на присутствие
447
 
                std::string skin = container->getSkin();
448
 
                bool exist = isSkinExist(container->getSkin());
449
 
                if (!exist && !container->getSkin().empty())
450
 
                {
451
 
                        skin = WidgetTypes::getInstance().findWidgetStyle(container->getType())->default_skin;
452
 
 
453
 
                        std::string skin_string;
454
 
                        if (skin.empty())
455
 
                                skin_string = "empty skin";
456
 
                        else
457
 
                                skin_string = "'" + skin + "'";
458
 
 
459
 
                        // FIXME : not translated string
460
 
                        std::string mess = MyGUI::utility::toString("'", container->getSkin(), "' skin not found , temporary changed to ", skin_string);
461
 
                        GroupMessage::getInstance().addMessage(mess, MyGUI::LogLevel::Error);
462
 
                }
463
 
 
464
 
                if (!_testMode)
465
 
                        skin = getSkinReplace(skin);
466
 
 
467
 
                std::string layer = DEFAULT_EDITOR_LAYER;
468
 
                if (_testMode)
469
 
                {
470
 
                        // use widget's layer if possible
471
 
                        if (MyGUI::LayerManager::getInstance().isExist(container->getLayerName()))
472
 
                                layer = container->getLayerName();
473
 
                        else
474
 
                                layer = DEFAULT_TEST_MODE_LAYER;
475
 
                }
476
 
                std::string widgetType = MyGUI::FactoryManager::getInstance().isFactoryExist("Widget", container->getType()) ?
477
 
                        container->getType() : MyGUI::Widget::getClassTypeName();
478
 
 
479
 
                if (nullptr == _parent)
480
 
                {
481
 
                        container->setWidget(MyGUI::Gui::getInstance().createWidgetT(widgetType, skin, coord, align, layer));
482
 
                }
483
 
                else
484
 
                {
485
 
                        container->setWidget(_parent->createWidgetT(widgetStyle, widgetType, skin, coord, align, layer));
486
 
                }
487
 
 
488
 
                add(container);
489
 
 
490
 
                // берем детей и крутимся
491
 
                MyGUI::xml::ElementEnumerator widget = _widget->getElementEnumerator();
492
 
                while (widget.next())
493
 
                {
494
 
                        std::string key, value, type;
495
 
 
496
 
                        if (widget->getName() == "Widget")
497
 
                        {
498
 
                                parseWidget(widget, container->getWidget(), _testMode);
499
 
                        }
500
 
                        else if (widget->getName() == "Property")
501
 
                        {
502
 
                                // парсим атрибуты
503
 
                                if (!widget->findAttribute("key", key))
504
 
                                        continue;
505
 
                                if (!widget->findAttribute("value", value))
506
 
                                        continue;
507
 
 
508
 
                                // конвертим проперти в текущий вариант версии
509
 
                                key = MyGUI::BackwardCompatibility::getPropertyRename(key);
510
 
                                size_t indexSeparator = key.find('_');
511
 
                                if (indexSeparator != std::string::npos)
512
 
                                        key = key.substr(indexSeparator + 1);
513
 
 
514
 
                                // и пытаемся парсить свойство
515
 
                                if (tryToApplyProperty(container->getWidget(), key, value, _testMode) == false)
516
 
                                        continue;
517
 
 
518
 
                                container->setProperty(key, value, false);
519
 
                        }
520
 
                        else if (widget->getName() == "UserString")
521
 
                        {
522
 
                                // парсим атрибуты
523
 
                                if (!widget->findAttribute("key", key))
524
 
                                        continue;
525
 
                                if (!widget->findAttribute("value", value))
526
 
                                        continue;
527
 
                                //container->mUserString.insert(MyGUI::PairString(key, value));
528
 
                                container->setUserData(key, value);
529
 
                        }
530
 
                        else if (widget->getName() == "Controller")
531
 
                        {
532
 
                                // парсим атрибуты
533
 
                                if (!widget->findAttribute("type", type))
534
 
                                        continue;
535
 
                                ControllerInfo* controllerInfo = new ControllerInfo();
536
 
                                controllerInfo->mType = type;
537
 
 
538
 
                                MyGUI::ControllerItem* item = nullptr;
539
 
                                if (_testMode)
540
 
                                {
541
 
                                        item = MyGUI::ControllerManager::getInstance().createItem(type);
542
 
                                }
543
 
 
544
 
                                MyGUI::xml::ElementEnumerator prop = widget->getElementEnumerator();
545
 
                                while (prop.next("Property"))
546
 
                                {
547
 
                                        if (!prop->findAttribute("key", key))
548
 
                                                continue;
549
 
                                        if (!prop->findAttribute("value", value))
550
 
                                                continue;
551
 
 
552
 
                                        controllerInfo->mProperty[key] = value;
553
 
                                        if (item)
554
 
                                        {
555
 
                                                item->setProperty(key, value);
556
 
                                        }
557
 
                                }
558
 
                                if (item)
559
 
                                {
560
 
                                        MyGUI::ControllerManager::getInstance().addItem(container->getWidget(), item);
561
 
                                }
562
 
 
563
 
                                container->mController.push_back(controllerInfo);
564
 
                        }
565
 
                };
566
 
        }
567
 
 
568
 
        bool EditorWidgets::tryToApplyProperty(MyGUI::Widget* _widget, const std::string& _key, const std::string& _value, bool _testMode)
569
 
        {
570
 
                WidgetContainer* container = EditorWidgets::getInstance().find(_widget);
571
 
                if (container->existUserData("LE_TargetWidgetType"))
572
 
                        return true;
573
 
 
574
 
                try
575
 
                {
576
 
                        if (_key == "Image_Texture")
577
 
                        {
578
 
                                if (!MyGUI::DataManager::getInstance().isDataExist(_value))
579
 
                                {
580
 
                                        GroupMessage::getInstance().addMessage("No such " + _key + ": '" + _value + "'. This value will be saved.", MyGUI::LogLevel::Warning);
581
 
                                        return true;
582
 
                                }
583
 
                        }
584
 
 
585
 
                        if (_testMode || std::find(mIgnoreParameters.begin(), mIgnoreParameters.end(), _key) == mIgnoreParameters.end())
586
 
                        {
587
 
                                _widget->setProperty(_key, _value);
588
 
                        }
589
 
                }
590
 
                catch (...)
591
 
                {
592
 
                        GroupMessage::getInstance().addMessage("Can't apply '" + _key + "'property.", MyGUI::LogLevel::Error);
593
 
                        return false;
594
 
                }
595
 
                return true;
596
 
        }
597
 
 
598
 
        void EditorWidgets::serialiseWidget(WidgetContainer* _container, MyGUI::xml::ElementPtr _node, bool _compatibility)
599
 
        {
600
 
                MyGUI::xml::ElementPtr node = _node->createChild("Widget");
601
 
 
602
 
                node->addAttribute("type", _container->getType());
603
 
                node->addAttribute("skin", _container->getSkin());
604
 
 
605
 
                if (!_container->getRelativeMode())
606
 
                        node->addAttribute("position", _container->position());
607
 
                else
608
 
                        node->addAttribute("position_real", _container->position(false));
609
 
 
610
 
                if (!_container->getAlign().empty())
611
 
                        node->addAttribute("align", _container->getAlign());
612
 
 
613
 
                if (!_container->getStyle().empty())
614
 
                        node->addAttribute("style", _container->getStyle());
615
 
 
616
 
                if ("" != _container->getLayerName())
617
 
                        node->addAttribute("layer", _container->getLayerName());
618
 
 
619
 
                if (!_container->getName().empty())
620
 
                        node->addAttribute("name", _container->getName());
621
 
 
622
 
                WidgetContainer::PropertyEnumerator propertyItem = _container->getPropertyEnumerator();
623
 
                while (propertyItem.next())
624
 
                {
625
 
                        BackwardCompatibilityManager::getInstance().serialiseProperty(node, _container->getType(), propertyItem.current(), _compatibility);
626
 
                }
627
 
 
628
 
                WidgetContainer::UserDataEnumerator userData = _container->getUserDataEnumerator();
629
 
                while (userData.next())
630
 
                {
631
 
                        MyGUI::xml::ElementPtr nodeProp = node->createChild("UserString");
632
 
                        nodeProp->addAttribute("key", userData.current().first);
633
 
                        nodeProp->addAttribute("value", userData.current().second);
634
 
                }
635
 
 
636
 
                for (std::vector<ControllerInfo*>::iterator iter = _container->mController.begin(); iter != _container->mController.end(); ++iter)
637
 
                {
638
 
                        MyGUI::xml::ElementPtr nodeController = node->createChild("Controller");
639
 
                        nodeController->addAttribute("type", (*iter)->mType);
640
 
                        for (MyGUI::MapString::iterator iterProp = (*iter)->mProperty.begin(); iterProp != (*iter)->mProperty.end(); ++iterProp)
641
 
                        {
642
 
                                MyGUI::xml::ElementPtr nodeProp = nodeController->createChild("Property");
643
 
                                nodeProp->addAttribute("key", iterProp->first);
644
 
                                nodeProp->addAttribute("value", iterProp->second);
645
 
                        }
646
 
                }
647
 
 
648
 
                for (std::vector<WidgetContainer*>::iterator iter = _container->childContainers.begin(); iter != _container->childContainers.end(); ++iter)
649
 
                {
650
 
                        serialiseWidget(*iter, node, _compatibility);
651
 
                }
652
 
        }
653
 
 
654
 
        void EditorWidgets::loadIgnoreParameters(MyGUI::xml::ElementPtr _node, const std::string& _file, MyGUI::Version _version)
655
 
        {
656
 
                MyGUI::xml::ElementEnumerator parameter = _node->getElementEnumerator();
657
 
                while (parameter.next("Parameter"))
658
 
                {
659
 
                        std::string name = parameter->findAttribute("key");
660
 
                        mIgnoreParameters.push_back(name);
661
 
                }
662
 
        }
663
 
 
664
 
        void EditorWidgets::notifyFrameStarted(float _time)
665
 
        {
666
 
                if (mWidgetsChanged)
667
 
                {
668
 
                        mWidgetsChanged = false;
669
 
                        eventChangeWidgets();
670
 
                }
671
 
        }
672
 
 
673
 
        void EditorWidgets::invalidateWidgets()
674
 
        {
675
 
                mWidgetsChanged = true;
676
 
        }
677
 
 
678
 
        void EditorWidgets::loadSector(MyGUI::xml::ElementPtr _sectorNode)
679
 
        {
680
 
                SettingsSector* sector = new SettingsSector();
681
 
                sector->deserialization(_sectorNode, MyGUI::Version());
682
 
 
683
 
                mSettings.push_back(sector);
684
 
        }
685
 
 
686
 
        void EditorWidgets::saveSectors(MyGUI::xml::ElementPtr _rootNode)
687
 
        {
688
 
                for (VectorSettingsSector::iterator item = mSettings.begin(); item != mSettings.end(); ++item)
689
 
                        (*item)->serialization(_rootNode, MyGUI::Version());
690
 
        }
691
 
 
692
 
        void EditorWidgets::destroyAllSectors()
693
 
        {
694
 
                for (VectorSettingsSector::iterator item = mSettings.begin(); item != mSettings.end(); ++item)
695
 
                        delete (*item);
696
 
                mSettings.clear();
697
 
        }
698
 
 
699
 
        SettingsSector* EditorWidgets::getSector(const MyGUI::UString& _sectorName)
700
 
        {
701
 
                for (VectorSettingsSector::iterator item = mSettings.begin(); item != mSettings.end(); ++item)
702
 
                {
703
 
                        if ((*item)->getName() == _sectorName)
704
 
                                return (*item);
705
 
                }
706
 
 
707
 
                SettingsSector* sector = new SettingsSector();
708
 
                sector->setName(_sectorName);
709
 
 
710
 
                mSettings.push_back(sector);
711
 
                return sector;
712
 
        }
713
 
 
714
 
        EnumeratorWidgetContainer EditorWidgets::getWidgets()
715
 
        {
716
 
                return EnumeratorWidgetContainer(mWidgets);
717
 
        }
718
 
 
719
 
        std::string EditorWidgets::getSkinReplace(const std::string& _skinName)
720
 
        {
721
 
                MapString::iterator item = mSkinReplaces.find(_skinName);
722
 
                if (item != mSkinReplaces.end())
723
 
                        return (*item).second;
724
 
                return _skinName;
725
 
        }
726
 
 
727
 
        void EditorWidgets::loadSkinReplace(MyGUI::xml::ElementPtr _node, const std::string& _file, MyGUI::Version _version)
728
 
        {
729
 
                MyGUI::xml::ElementEnumerator node = _node->getElementEnumerator();
730
 
                while (node.next("Skin"))
731
 
                        mSkinReplaces[node->findAttribute("key")] = node->getContent();
732
 
        }
733
 
 
734
 
        bool EditorWidgets::isSkinExist(const std::string& _skinName)
735
 
        {
736
 
                return _skinName == "Default" ||
737
 
                        MyGUI::SkinManager::getInstance().isExist(_skinName) ||
738
 
                        (MyGUI::LayoutManager::getInstance().isExist(_skinName) && checkTemplate(_skinName));
739
 
        }
740
 
 
741
 
        bool EditorWidgets::checkTemplate(const std::string& _skinName)
742
 
        {
743
 
                MyGUI::ResourceLayout* templateInfo = MyGUI::LayoutManager::getInstance().getByName(_skinName, false);
744
 
                if (templateInfo != nullptr)
745
 
                {
746
 
                        const MyGUI::VectorWidgetInfo& data = templateInfo->getLayoutData();
747
 
                        for (MyGUI::VectorWidgetInfo::const_iterator container = data.begin(); container != data.end(); ++container)
748
 
                        {
749
 
                                if (container->name == "Root")
750
 
                                        return true;
751
 
                        }
752
 
                }
753
 
 
754
 
                return false;
755
 
        }
756
 
 
757
 
        const MyGUI::UString& EditorWidgets::getCurrentFileName() const
758
 
        {
759
 
                return mCurrentFileName;
760
 
        }
761
 
 
762
 
        const MyGUI::UString& EditorWidgets::getCurrentItemName() const
763
 
        {
764
 
                return mCurrentItemName;
765
 
        }
766
 
 
767
 
        void EditorWidgets::_unlinkWidget(MyGUI::Widget* _widget)
768
 
        {
769
 
                WidgetContainer* container = find(_widget);
770
 
                if (container != nullptr)
771
 
                {
772
 
                        bool result = unbind(container);
773
 
                        mWidgetsChanged = true;
774
 
 
775
 
                        if (result)
776
 
                                WidgetSelectorManager::getInstance().setSelectedWidget(nullptr);
777
 
                }
778
 
        }
779
 
 
780
 
        void EditorWidgets::loadWidgetsFromXmlNode(MyGUI::xml::ElementPtr _root, bool _testMode)
781
 
        {
782
 
                // берем детей и крутимся
783
 
                MyGUI::xml::ElementEnumerator element = _root->getElementEnumerator();
784
 
                while (element.next())
785
 
                {
786
 
                        if (element->getName() == "Widget")
787
 
                                parseWidget(element, nullptr, _testMode);
788
 
                        else
789
 
                                loadSector(element.current());
790
 
                }
791
 
        }
792
 
 
793
 
        void EditorWidgets::saveWidgetsToXmlNode(MyGUI::xml::ElementPtr _root, bool _compatibility)
794
 
        {
795
 
                _root->addAttribute("version", BackwardCompatibilityManager::getInstancePtr()->getCurrentVersion());
796
 
 
797
 
                for (std::vector<WidgetContainer*>::iterator iter = mWidgets.begin(); iter != mWidgets.end(); ++iter)
798
 
                {
799
 
                        // в корень только сирот
800
 
                        if (nullptr == (*iter)->getWidget()->getParent())
801
 
                                serialiseWidget(*iter, _root, _compatibility);
802
 
                }
803
 
 
804
 
                saveSectors(_root);
805
 
        }
806
 
 
807
 
        void EditorWidgets::onSetWidgetCoord(MyGUI::Widget* _widget, const MyGUI::IntCoord& _value, const std::string& _owner)
808
 
        {
809
 
                eventChangeWidgetCoord(_widget, _value, _owner);
810
 
        }
811
 
 
812
 
} // namespace tools
 
1
#include "Precompiled.h"
 
2
#include "EditorWidgets.h"
 
3
#include "Common.h"
 
4
#include "WidgetTypes.h"
 
5
#include "GroupMessage.h"
 
6
#include "BackwardCompatibilityManager.h"
 
7
#include "WidgetSelectorManager.h"
 
8
#include "SettingsManager.h"
 
9
 
 
10
template <> tools::EditorWidgets* MyGUI::Singleton<tools::EditorWidgets>::msInstance = nullptr;
 
11
template <> const char* MyGUI::Singleton<tools::EditorWidgets>::mClassTypeName = "EditorWidgets";
 
12
 
 
13
namespace tools
 
14
{
 
15
 
 
16
        const std::string LogSection = "LayoutEditor";
 
17
        const std::string CodeGeneratorSettingsNodeName = "CodeGeneratorSettings";
 
18
 
 
19
        EditorWidgets::EditorWidgets() :
 
20
                mWidgetsChanged(false)
 
21
        {
 
22
        }
 
23
 
 
24
        EditorWidgets::~EditorWidgets()
 
25
        {
 
26
        }
 
27
 
 
28
        void EditorWidgets::initialise()
 
29
        {
 
30
                mWidgetsChanged = true;
 
31
 
 
32
                MyGUI::ResourceManager::getInstance().registerLoadXmlDelegate("IgnoreParameters") = MyGUI::newDelegate(this, &EditorWidgets::loadIgnoreParameters);
 
33
                MyGUI::ResourceManager::getInstance().registerLoadXmlDelegate("SkinReplace") = MyGUI::newDelegate(this, &EditorWidgets::loadSkinReplace);
 
34
 
 
35
                MyGUI::Gui::getInstance().eventFrameStart += MyGUI::newDelegate(this, &EditorWidgets::notifyFrameStarted);
 
36
                MyGUI::WidgetManager::getInstance().registerUnlinker(this);
 
37
        }
 
38
 
 
39
        void EditorWidgets::shutdown()
 
40
        {
 
41
                MyGUI::WidgetManager::getInstance().unregisterUnlinker(this);
 
42
                MyGUI::Gui::getInstance().eventFrameStart -= MyGUI::newDelegate(this, &EditorWidgets::notifyFrameStarted);
 
43
 
 
44
                destroyAllWidgets();
 
45
 
 
46
                mCodeGeneratorSettings.clear();
 
47
        }
 
48
 
 
49
        void EditorWidgets::destroyAllWidgets()
 
50
        {
 
51
                for (std::vector<WidgetContainer*>::iterator iter = mWidgets.begin(); iter != mWidgets.end(); ++iter)
 
52
                        delete *iter;
 
53
                mWidgets.clear();
 
54
        }
 
55
 
 
56
        bool EditorWidgets::load(const MyGUI::UString& _fileName)
 
57
        {
 
58
                size_t index = _fileName.find("|");
 
59
                if (index != MyGUI::UString::npos)
 
60
                {
 
61
                        MyGUI::UString fileName = _fileName.substr(0, index);
 
62
                        MyGUI::UString itemIndex = _fileName.substr(index + 1);
 
63
 
 
64
                        return loadFromProject(fileName, MyGUI::utility::parseValue<size_t>(itemIndex));
 
65
                }
 
66
 
 
67
                mCurrentFileName = _fileName;
 
68
                mCurrentItemName.clear();
 
69
 
 
70
                MyGUI::xml::Document doc;
 
71
                if (!doc.open(_fileName))
 
72
                {
 
73
                        MYGUI_LOGGING(LogSection, Error, getClassTypeName() << " : " << doc.getLastError());
 
74
                        return false;
 
75
                }
 
76
 
 
77
                MyGUI::xml::ElementPtr root = doc.getRoot();
 
78
                if ((nullptr == root) || (root->getName() != "MyGUI"))
 
79
                {
 
80
                        MYGUI_LOGGING(LogSection, Error, getClassTypeName() << " : '" << _fileName << "', tag 'MyGUI' not found");
 
81
                        return false;
 
82
                }
 
83
 
 
84
                if (root->findAttribute("type") == "Layout")
 
85
                {
 
86
                        loadWidgetsFromXmlNode(root);
 
87
                }
 
88
                else
 
89
                {
 
90
                        return false;
 
91
                }
 
92
 
 
93
                mWidgetsChanged = true;
 
94
                return true;
 
95
        }
 
96
 
 
97
        bool EditorWidgets::loadFromProject(const MyGUI::UString& _fileName, size_t _index)
 
98
        {
 
99
                mCurrentFileName = _fileName;
 
100
 
 
101
                MyGUI::xml::Document doc;
 
102
                if (!doc.open(_fileName))
 
103
                {
 
104
                        MYGUI_LOGGING(LogSection, Error, getClassTypeName() << " : " << doc.getLastError());
 
105
                        return false;
 
106
                }
 
107
 
 
108
                MyGUI::xml::ElementPtr root = doc.getRoot();
 
109
                if ((nullptr == root) || (root->getName() != "MyGUI"))
 
110
                {
 
111
                        MYGUI_LOGGING(LogSection, Error, getClassTypeName() << " : '" << _fileName << "', tag 'MyGUI' not found");
 
112
                        return false;
 
113
                }
 
114
 
 
115
                if (root->findAttribute("type") == "Resource")
 
116
                {
 
117
                        // берем детей и крутимся
 
118
                        MyGUI::xml::ElementEnumerator element = root->getElementEnumerator();
 
119
                        while (element.next("Resource"))
 
120
                        {
 
121
                                if (element->findAttribute("type") == "ResourceLayout")
 
122
                                {
 
123
                                        if (_index == 0)
 
124
                                        {
 
125
                                                mCurrentItemName = element->findAttribute("name");
 
126
 
 
127
                                                loadWidgetsFromXmlNode(element.current());
 
128
 
 
129
                                                break;
 
130
                                        }
 
131
                                        else
 
132
                                        {
 
133
                                                _index --;
 
134
                                        }
 
135
                                }
 
136
                        }
 
137
                }
 
138
                else
 
139
                {
 
140
                        return false;
 
141
                }
 
142
 
 
143
                mWidgetsChanged = true;
 
144
                return true;
 
145
        }
 
146
 
 
147
        bool EditorWidgets::save(const MyGUI::UString& _fileName)
 
148
        {
 
149
                size_t index = _fileName.find("|");
 
150
                if (index != MyGUI::UString::npos)
 
151
                {
 
152
                        MyGUI::UString fileName = _fileName.substr(0, index);
 
153
                        MyGUI::UString itemIndex = _fileName.substr(index + 1);
 
154
 
 
155
                        return saveToProject(fileName, MyGUI::utility::parseValue<size_t>(itemIndex));
 
156
                }
 
157
 
 
158
                mCurrentFileName = _fileName;
 
159
                mCurrentItemName.clear();
 
160
 
 
161
                MyGUI::xml::Document doc;
 
162
                doc.createDeclaration();
 
163
                MyGUI::xml::ElementPtr root = doc.createRoot("MyGUI");
 
164
                root->addAttribute("type", "Layout");
 
165
 
 
166
                saveWidgetsToXmlNode(root, true);
 
167
 
 
168
                if (!doc.save(_fileName))
 
169
                {
 
170
                        MYGUI_LOGGING(LogSection, Error, getClassTypeName() << " : " << doc.getLastError());
 
171
                        return false;
 
172
                }
 
173
 
 
174
                return true;
 
175
        }
 
176
 
 
177
        bool EditorWidgets::saveToProject(const MyGUI::UString& _fileName, size_t _index)
 
178
        {
 
179
                mCurrentFileName = _fileName;
 
180
 
 
181
                MyGUI::xml::Document doc;
 
182
                if (!doc.open(_fileName))
 
183
                {
 
184
                        MYGUI_LOGGING(LogSection, Error, getClassTypeName() << " : " << doc.getLastError());
 
185
                        return false;
 
186
                }
 
187
 
 
188
                MyGUI::xml::ElementPtr root = doc.getRoot();
 
189
                if ((nullptr == root) || (root->getName() != "MyGUI"))
 
190
                {
 
191
                        MYGUI_LOGGING(LogSection, Error, getClassTypeName() << " : '" << _fileName << "', tag 'MyGUI' not found");
 
192
                        return false;
 
193
                }
 
194
 
 
195
                if (root->findAttribute("type") == "Resource")
 
196
                {
 
197
                        // берем детей и крутимся
 
198
                        MyGUI::xml::ElementEnumerator element = root->getElementEnumerator();
 
199
                        while (element.next("Resource"))
 
200
                        {
 
201
                                if (element->findAttribute("type") == "ResourceLayout")
 
202
                                {
 
203
                                        if (_index == 0)
 
204
                                        {
 
205
                                                mCurrentItemName = element->findAttribute("name");
 
206
 
 
207
                                                element->clear();
 
208
                                                element->addAttribute("type", "ResourceLayout");
 
209
                                                element->addAttribute("name", mCurrentItemName);
 
210
 
 
211
                                                saveWidgetsToXmlNode(element.current(), true);
 
212
 
 
213
                                                if (!doc.save(_fileName))
 
214
                                                {
 
215
                                                        MYGUI_LOGGING(LogSection, Error, getClassTypeName() << " : " << doc.getLastError());
 
216
                                                        return false;
 
217
                                                }
 
218
 
 
219
                                                return true;
 
220
                                        }
 
221
                                        else
 
222
                                        {
 
223
                                                _index --;
 
224
                                        }
 
225
                                }
 
226
                        }
 
227
                        return false;
 
228
                }
 
229
 
 
230
                return false;
 
231
        }
 
232
 
 
233
        void EditorWidgets::loadxmlDocument(MyGUI::xml::Document* doc, bool _testMode)
 
234
        {
 
235
                MyGUI::xml::ElementPtr root = doc->getRoot();
 
236
 
 
237
                std::string type;
 
238
                if (root->findAttribute("type", type))
 
239
                {
 
240
                        if (type == "Layout")
 
241
                        {
 
242
                                loadWidgetsFromXmlNode(root, _testMode);
 
243
                        }
 
244
                }
 
245
                mWidgetsChanged = true;
 
246
        }
 
247
 
 
248
        MyGUI::xml::Document* EditorWidgets::savexmlDocument()
 
249
        {
 
250
                MyGUI::xml::Document* doc = new MyGUI::xml::Document();
 
251
 
 
252
                doc->createDeclaration();
 
253
                MyGUI::xml::ElementPtr root = doc->createRoot("MyGUI");
 
254
                root->addAttribute("type", "Layout");
 
255
 
 
256
                saveWidgetsToXmlNode(root);
 
257
 
 
258
                return doc;
 
259
        }
 
260
 
 
261
        void EditorWidgets::add(WidgetContainer* _container)
 
262
        {
 
263
                if (nullptr == _container->getWidget()->getParent())
 
264
                {
 
265
                        mWidgets.push_back(_container);
 
266
                }
 
267
                else
 
268
                {
 
269
                        MyGUI::Widget* parent = _container->getWidget()->getParent();
 
270
                        WidgetContainer* containerParent = find(parent);
 
271
                        while (nullptr == containerParent)
 
272
                        {
 
273
                                parent = parent->getParent();
 
274
                                if (parent == nullptr)
 
275
                                        return;
 
276
                                containerParent = find(parent);
 
277
                        }
 
278
                        containerParent->childContainers.push_back(_container);
 
279
                }
 
280
                mWidgetsChanged = true;
 
281
        }
 
282
 
 
283
        void EditorWidgets::remove(MyGUI::Widget* _widget)
 
284
        {
 
285
                remove(find(_widget));
 
286
                mWidgetsChanged = true;
 
287
        }
 
288
 
 
289
        void EditorWidgets::remove(WidgetContainer* _container)
 
290
        {
 
291
                if (nullptr != _container)
 
292
                {
 
293
                        std::vector<WidgetContainer*>::reverse_iterator iter;
 
294
                        while (_container->childContainers.empty() == false)
 
295
                        {
 
296
                                iter = _container->childContainers.rbegin();
 
297
                                remove(*iter);
 
298
                        }
 
299
 
 
300
                        if (nullptr == _container->getWidget()->getParent())
 
301
                        {
 
302
                                mWidgets.erase(std::find(mWidgets.begin(), mWidgets.end(), _container));
 
303
                        }
 
304
                        else
 
305
                        {
 
306
                                MyGUI::Widget* parent = _container->getWidget()->getParent();
 
307
                                WidgetContainer* containerParent = find(parent);
 
308
                                while (nullptr == containerParent)
 
309
                                {
 
310
                                        parent = parent->getParent();
 
311
                                        if (parent == nullptr)
 
312
                                        {
 
313
                                                // такого не должно быть, или утечка памяти,
 
314
                                                // так как удалять нельзя им пользуется код вызывающий данную функцию
 
315
                                                return;
 
316
                                        }
 
317
                                        containerParent = find(parent);
 
318
                                }
 
319
 
 
320
                                containerParent->childContainers.erase(std::find(containerParent->childContainers.begin(), containerParent->childContainers.end(), _container));
 
321
                        }
 
322
 
 
323
                        MyGUI::Gui::getInstance().destroyWidget(_container->getWidget());
 
324
 
 
325
                        delete _container;
 
326
                }
 
327
 
 
328
                mWidgetsChanged = true;
 
329
        }
 
330
 
 
331
        bool EditorWidgets::unbind(WidgetContainer* _container)
 
332
        {
 
333
                bool result = false;
 
334
 
 
335
                if (nullptr != _container)
 
336
                {
 
337
                        std::vector<WidgetContainer*>::reverse_iterator iter;
 
338
                        while (_container->childContainers.empty() == false)
 
339
                        {
 
340
                                iter = _container->childContainers.rbegin();
 
341
                                if (unbind(*iter))
 
342
                                        result = true;
 
343
                        }
 
344
 
 
345
                        if (nullptr == _container->getWidget()->getParent())
 
346
                        {
 
347
                                mWidgets.erase(std::find(mWidgets.begin(), mWidgets.end(), _container));
 
348
                        }
 
349
                        else
 
350
                        {
 
351
                                MyGUI::Widget* parent = _container->getWidget()->getParent();
 
352
                                WidgetContainer* containerParent = find(parent);
 
353
                                while (nullptr == containerParent)
 
354
                                {
 
355
                                        parent = parent->getParent();
 
356
                                        if (parent == nullptr)
 
357
                                        {
 
358
                                                // такого не должно быть, или утечка памяти,
 
359
                                                // так как удалять нельзя им пользуется код вызывающий данную функцию
 
360
                                                return result;
 
361
                                        }
 
362
                                        containerParent = find(parent);
 
363
                                }
 
364
 
 
365
                                containerParent->childContainers.erase(std::find(containerParent->childContainers.begin(), containerParent->childContainers.end(), _container));
 
366
                        }
 
367
 
 
368
                        delete _container;
 
369
                        result = true;
 
370
                }
 
371
 
 
372
                mWidgetsChanged = true;
 
373
                return result;
 
374
        }
 
375
 
 
376
        void EditorWidgets::clear()
 
377
        {
 
378
                mCurrentFileName.clear();
 
379
                mCurrentItemName.clear();
 
380
 
 
381
                while (!mWidgets.empty())
 
382
                {
 
383
                        remove(mWidgets[mWidgets.size()-1]);
 
384
                }
 
385
 
 
386
                mCodeGeneratorSettings.clear();
 
387
        }
 
388
 
 
389
        WidgetContainer* EditorWidgets::find(MyGUI::Widget* _widget)
 
390
        {
 
391
                return _find(_widget, "", mWidgets);
 
392
        }
 
393
 
 
394
        WidgetContainer* EditorWidgets::find(const std::string& _name)
 
395
        {
 
396
                return _find(nullptr, _name, mWidgets);
 
397
        }
 
398
 
 
399
        WidgetContainer* EditorWidgets::_find(MyGUI::Widget* _widget, const std::string& _name, std::vector<WidgetContainer*> _widgets)
 
400
        {
 
401
                for (std::vector<WidgetContainer*>::iterator iter = _widgets.begin(); iter != _widgets.end(); ++iter)
 
402
                {
 
403
                        if (((*iter)->getWidget() == _widget) || ((_name.empty() == false) && ((*iter)->getName() == _name)))
 
404
                        {
 
405
                                return *iter;
 
406
                        }
 
407
                        WidgetContainer* retContainer = _find(_widget, _name, (*iter)->childContainers);
 
408
                        if (retContainer)
 
409
                                return retContainer;
 
410
                }
 
411
                return nullptr;
 
412
        }
 
413
 
 
414
        void EditorWidgets::parseWidget(MyGUI::xml::ElementEnumerator& _widget, MyGUI::Widget* _parent, bool _testMode)
 
415
        {
 
416
                WidgetContainer* container = new WidgetContainer();
 
417
                // парсим атрибуты виджета
 
418
                MyGUI::IntCoord coord;
 
419
                MyGUI::Align align = MyGUI::Align::Default;
 
420
                MyGUI::WidgetStyle widgetStyle = MyGUI::WidgetStyle::Child;
 
421
                std::string position;
 
422
 
 
423
                container->setName(_widget->findAttribute("name"));
 
424
                container->setType(_widget->findAttribute("type"));
 
425
                container->setSkin(_widget->findAttribute("skin"));
 
426
                container->setLayerName(_widget->findAttribute("layer"));
 
427
                std::string tmp;
 
428
                if (_widget->findAttribute("style", tmp))
 
429
                {
 
430
                        container->setStyle(tmp);
 
431
                        widgetStyle = MyGUI::WidgetStyle::parse(tmp);
 
432
                }
 
433
                if (_widget->findAttribute("align", tmp))
 
434
                {
 
435
                        container->setAlign(tmp);
 
436
                        align = MyGUI::Align::parse(tmp);
 
437
                }
 
438
                if (_widget->findAttribute("position", position))
 
439
                        coord = MyGUI::IntCoord::parse(position);
 
440
                if (_widget->findAttribute("position_real", position))
 
441
                {
 
442
                        container->setRelativeMode(true);
 
443
                        MyGUI::IntSize textureSize = SettingsManager::getInstance().getValue<MyGUI::IntSize>("Settings/WorkspaceTextureSize");
 
444
                        MyGUI::IntSize size = _testMode ? MyGUI::RenderManager::getInstance().getViewSize() : textureSize;
 
445
                        coord = MyGUI::CoordConverter::convertFromRelative(MyGUI::FloatCoord::parse(position), _parent == nullptr ? size : _parent->getClientCoord().size());
 
446
                }
 
447
 
 
448
                // проверяем скин на присутствие
 
449
                std::string skin = container->getSkin();
 
450
                bool exist = isSkinExist(container->getSkin());
 
451
                if (!exist && !container->getSkin().empty())
 
452
                {
 
453
                        skin = WidgetTypes::getInstance().findWidgetStyle(container->getType())->default_skin;
 
454
 
 
455
                        std::string skin_string;
 
456
                        if (skin.empty())
 
457
                                skin_string = "empty skin";
 
458
                        else
 
459
                                skin_string = "'" + skin + "'";
 
460
 
 
461
                        // FIXME : not translated string
 
462
                        std::string mess = MyGUI::utility::toString("'", container->getSkin(), "' skin not found , temporary changed to ", skin_string);
 
463
                        GroupMessage::getInstance().addMessage(mess, MyGUI::LogLevel::Error);
 
464
                }
 
465
 
 
466
                if (!_testMode)
 
467
                        skin = getSkinReplace(skin);
 
468
 
 
469
                std::string layer = DEFAULT_EDITOR_LAYER;
 
470
                if (_testMode)
 
471
                {
 
472
                        // use widget's layer if possible
 
473
                        if (MyGUI::LayerManager::getInstance().isExist(container->getLayerName()))
 
474
                                layer = container->getLayerName();
 
475
                        else
 
476
                                layer = DEFAULT_TEST_MODE_LAYER;
 
477
                }
 
478
                std::string widgetType = MyGUI::FactoryManager::getInstance().isFactoryExist("Widget", container->getType()) ?
 
479
                        container->getType() : MyGUI::Widget::getClassTypeName();
 
480
 
 
481
                if (nullptr == _parent)
 
482
                {
 
483
                        container->setWidget(MyGUI::Gui::getInstance().createWidgetT(widgetType, skin, coord, align, layer));
 
484
                }
 
485
                else
 
486
                {
 
487
                        container->setWidget(_parent->createWidgetT(widgetStyle, widgetType, skin, coord, align, layer));
 
488
                }
 
489
 
 
490
                add(container);
 
491
 
 
492
                // берем детей и крутимся
 
493
                MyGUI::xml::ElementEnumerator widget = _widget->getElementEnumerator();
 
494
                while (widget.next())
 
495
                {
 
496
                        std::string key, value, type;
 
497
 
 
498
                        if (widget->getName() == "Widget")
 
499
                        {
 
500
                                parseWidget(widget, container->getWidget(), _testMode);
 
501
                        }
 
502
                        else if (widget->getName() == "Property")
 
503
                        {
 
504
                                // парсим атрибуты
 
505
                                if (!widget->findAttribute("key", key))
 
506
                                        continue;
 
507
                                if (!widget->findAttribute("value", value))
 
508
                                        continue;
 
509
 
 
510
                                // конвертим проперти в текущий вариант версии
 
511
                                key = MyGUI::BackwardCompatibility::getPropertyRename(key);
 
512
                                size_t indexSeparator = key.find('_');
 
513
                                if (indexSeparator != std::string::npos)
 
514
                                        key = key.substr(indexSeparator + 1);
 
515
 
 
516
                                // и пытаемся парсить свойство
 
517
                                if (tryToApplyProperty(container->getWidget(), key, value, _testMode) == false)
 
518
                                        continue;
 
519
 
 
520
                                container->setProperty(key, value, false);
 
521
                        }
 
522
                        else if (widget->getName() == "UserString")
 
523
                        {
 
524
                                // парсим атрибуты
 
525
                                if (!widget->findAttribute("key", key))
 
526
                                        continue;
 
527
                                if (!widget->findAttribute("value", value))
 
528
                                        continue;
 
529
                                //container->mUserString.insert(MyGUI::PairString(key, value));
 
530
                                container->setUserData(key, value);
 
531
                        }
 
532
                        else if (widget->getName() == "Controller")
 
533
                        {
 
534
                                // парсим атрибуты
 
535
                                if (!widget->findAttribute("type", type))
 
536
                                        continue;
 
537
                                ControllerInfo* controllerInfo = new ControllerInfo();
 
538
                                controllerInfo->mType = type;
 
539
 
 
540
                                MyGUI::ControllerItem* item = nullptr;
 
541
                                if (_testMode)
 
542
                                {
 
543
                                        item = MyGUI::ControllerManager::getInstance().createItem(type);
 
544
                                }
 
545
 
 
546
                                MyGUI::xml::ElementEnumerator prop = widget->getElementEnumerator();
 
547
                                while (prop.next("Property"))
 
548
                                {
 
549
                                        if (!prop->findAttribute("key", key))
 
550
                                                continue;
 
551
                                        if (!prop->findAttribute("value", value))
 
552
                                                continue;
 
553
 
 
554
                                        controllerInfo->mProperty[key] = value;
 
555
                                        if (item)
 
556
                                        {
 
557
                                                item->setProperty(key, value);
 
558
                                        }
 
559
                                }
 
560
                                if (item)
 
561
                                {
 
562
                                        MyGUI::ControllerManager::getInstance().addItem(container->getWidget(), item);
 
563
                                }
 
564
 
 
565
                                container->mController.push_back(controllerInfo);
 
566
                        }
 
567
                };
 
568
        }
 
569
 
 
570
        bool EditorWidgets::tryToApplyProperty(MyGUI::Widget* _widget, const std::string& _key, const std::string& _value, bool _testMode)
 
571
        {
 
572
                WidgetContainer* container = EditorWidgets::getInstance().find(_widget);
 
573
                if (container->existUserData("LE_TargetWidgetType"))
 
574
                        return true;
 
575
 
 
576
                try
 
577
                {
 
578
                        if (_key == "Image_Texture")
 
579
                        {
 
580
                                if (!MyGUI::DataManager::getInstance().isDataExist(_value))
 
581
                                {
 
582
                                        GroupMessage::getInstance().addMessage("No such " + _key + ": '" + _value + "'. This value will be saved.", MyGUI::LogLevel::Warning);
 
583
                                        return true;
 
584
                                }
 
585
                        }
 
586
 
 
587
                        if (_testMode || std::find(mIgnoreParameters.begin(), mIgnoreParameters.end(), _key) == mIgnoreParameters.end())
 
588
                        {
 
589
                                _widget->setProperty(_key, _value);
 
590
                        }
 
591
                }
 
592
                catch (...)
 
593
                {
 
594
                        GroupMessage::getInstance().addMessage("Can't apply '" + _key + "'property.", MyGUI::LogLevel::Error);
 
595
                        return false;
 
596
                }
 
597
                return true;
 
598
        }
 
599
 
 
600
        void EditorWidgets::serialiseWidget(WidgetContainer* _container, MyGUI::xml::ElementPtr _node, bool _compatibility)
 
601
        {
 
602
                MyGUI::xml::ElementPtr node = _node->createChild("Widget");
 
603
 
 
604
                node->addAttribute("type", _container->getType());
 
605
                node->addAttribute("skin", _container->getSkin());
 
606
 
 
607
                if (!_container->getRelativeMode())
 
608
                        node->addAttribute("position", _container->position());
 
609
                else
 
610
                        node->addAttribute("position_real", _container->position(false));
 
611
 
 
612
                if (!_container->getAlign().empty())
 
613
                        node->addAttribute("align", _container->getAlign());
 
614
 
 
615
                if (!_container->getStyle().empty())
 
616
                        node->addAttribute("style", _container->getStyle());
 
617
 
 
618
                if (!_container->getLayerName().empty())
 
619
                        node->addAttribute("layer", _container->getLayerName());
 
620
 
 
621
                if (!_container->getName().empty())
 
622
                        node->addAttribute("name", _container->getName());
 
623
 
 
624
                WidgetContainer::PropertyEnumerator propertyItem = _container->getPropertyEnumerator();
 
625
                while (propertyItem.next())
 
626
                {
 
627
                        BackwardCompatibilityManager::getInstance().serialiseProperty(node, _container->getType(), propertyItem.current(), _compatibility);
 
628
                }
 
629
 
 
630
                WidgetContainer::UserDataEnumerator userData = _container->getUserDataEnumerator();
 
631
                while (userData.next())
 
632
                {
 
633
                        MyGUI::xml::ElementPtr nodeProp = node->createChild("UserString");
 
634
                        nodeProp->addAttribute("key", userData.current().first);
 
635
                        nodeProp->addAttribute("value", userData.current().second);
 
636
                }
 
637
 
 
638
                for (std::vector<ControllerInfo*>::iterator iter = _container->mController.begin(); iter != _container->mController.end(); ++iter)
 
639
                {
 
640
                        MyGUI::xml::ElementPtr nodeController = node->createChild("Controller");
 
641
                        nodeController->addAttribute("type", (*iter)->mType);
 
642
                        for (MyGUI::MapString::iterator iterProp = (*iter)->mProperty.begin(); iterProp != (*iter)->mProperty.end(); ++iterProp)
 
643
                        {
 
644
                                MyGUI::xml::ElementPtr nodeProp = nodeController->createChild("Property");
 
645
                                nodeProp->addAttribute("key", iterProp->first);
 
646
                                nodeProp->addAttribute("value", iterProp->second);
 
647
                        }
 
648
                }
 
649
 
 
650
                for (std::vector<WidgetContainer*>::iterator iter = _container->childContainers.begin(); iter != _container->childContainers.end(); ++iter)
 
651
                {
 
652
                        serialiseWidget(*iter, node, _compatibility);
 
653
                }
 
654
        }
 
655
 
 
656
        void EditorWidgets::loadIgnoreParameters(MyGUI::xml::ElementPtr _node, const std::string& _file, MyGUI::Version _version)
 
657
        {
 
658
                MyGUI::xml::ElementEnumerator parameter = _node->getElementEnumerator();
 
659
                while (parameter.next("Parameter"))
 
660
                {
 
661
                        std::string name = parameter->findAttribute("key");
 
662
                        mIgnoreParameters.push_back(name);
 
663
                }
 
664
        }
 
665
 
 
666
        void EditorWidgets::notifyFrameStarted(float _time)
 
667
        {
 
668
                if (mWidgetsChanged)
 
669
                {
 
670
                        mWidgetsChanged = false;
 
671
                        eventChangeWidgets();
 
672
                }
 
673
        }
 
674
 
 
675
        void EditorWidgets::invalidateWidgets()
 
676
        {
 
677
                mWidgetsChanged = true;
 
678
        }
 
679
 
 
680
        EnumeratorWidgetContainer EditorWidgets::getWidgets()
 
681
        {
 
682
                return EnumeratorWidgetContainer(mWidgets);
 
683
        }
 
684
 
 
685
        std::string EditorWidgets::getSkinReplace(const std::string& _skinName)
 
686
        {
 
687
                MyGUI::MapString::iterator item = mSkinReplaces.find(_skinName);
 
688
                if (item != mSkinReplaces.end())
 
689
                        return (*item).second;
 
690
                return _skinName;
 
691
        }
 
692
 
 
693
        void EditorWidgets::loadSkinReplace(MyGUI::xml::ElementPtr _node, const std::string& _file, MyGUI::Version _version)
 
694
        {
 
695
                MyGUI::xml::ElementEnumerator node = _node->getElementEnumerator();
 
696
                while (node.next("Skin"))
 
697
                        mSkinReplaces[node->findAttribute("key")] = node->getContent();
 
698
        }
 
699
 
 
700
        bool EditorWidgets::isSkinExist(const std::string& _skinName)
 
701
        {
 
702
                return _skinName == "Default" ||
 
703
                        MyGUI::SkinManager::getInstance().isExist(_skinName) ||
 
704
                        (MyGUI::LayoutManager::getInstance().isExist(_skinName) && checkTemplate(_skinName));
 
705
        }
 
706
 
 
707
        bool EditorWidgets::checkTemplate(const std::string& _skinName)
 
708
        {
 
709
                MyGUI::ResourceLayout* templateInfo = MyGUI::LayoutManager::getInstance().getByName(_skinName, false);
 
710
                if (templateInfo != nullptr)
 
711
                {
 
712
                        const MyGUI::VectorWidgetInfo& data = templateInfo->getLayoutData();
 
713
                        for (MyGUI::VectorWidgetInfo::const_iterator container = data.begin(); container != data.end(); ++container)
 
714
                        {
 
715
                                if (container->name == "Root")
 
716
                                        return true;
 
717
                        }
 
718
                }
 
719
 
 
720
                return false;
 
721
        }
 
722
 
 
723
        const MyGUI::UString& EditorWidgets::getCurrentFileName() const
 
724
        {
 
725
                return mCurrentFileName;
 
726
        }
 
727
 
 
728
        const MyGUI::UString& EditorWidgets::getCurrentItemName() const
 
729
        {
 
730
                return mCurrentItemName;
 
731
        }
 
732
 
 
733
        void EditorWidgets::_unlinkWidget(MyGUI::Widget* _widget)
 
734
        {
 
735
                WidgetContainer* container = find(_widget);
 
736
                if (container != nullptr)
 
737
                {
 
738
                        bool result = unbind(container);
 
739
                        mWidgetsChanged = true;
 
740
 
 
741
                        if (result)
 
742
                                WidgetSelectorManager::getInstance().setSelectedWidget(nullptr);
 
743
                }
 
744
        }
 
745
 
 
746
        MyGUI::MapString& EditorWidgets::getCodeGeneratorSettings()
 
747
        {
 
748
                return mCodeGeneratorSettings;
 
749
        }
 
750
 
 
751
        void EditorWidgets::loadWidgetsFromXmlNode(MyGUI::xml::ElementPtr _root, bool _testMode)
 
752
        {
 
753
                // берем детей и крутимся
 
754
                MyGUI::xml::ElementEnumerator element = _root->getElementEnumerator();
 
755
                while (element.next())
 
756
                {
 
757
                        if (element->getName() == "Widget")
 
758
                                parseWidget(element, nullptr, _testMode);
 
759
                        else if (element->getName() == CodeGeneratorSettingsNodeName)
 
760
                                loadCodeGeneratorSettings(element.current());
 
761
                }
 
762
        }
 
763
 
 
764
        void EditorWidgets::saveWidgetsToXmlNode(MyGUI::xml::ElementPtr _root, bool _compatibility)
 
765
        {
 
766
                _root->addAttribute("version", BackwardCompatibilityManager::getInstancePtr()->getCurrentVersion());
 
767
 
 
768
                for (std::vector<WidgetContainer*>::iterator iter = mWidgets.begin(); iter != mWidgets.end(); ++iter)
 
769
                {
 
770
                        // в корень только сирот
 
771
                        if (nullptr == (*iter)->getWidget()->getParent())
 
772
                                serialiseWidget(*iter, _root, _compatibility);
 
773
                }
 
774
 
 
775
                saveCodeGeneratorSettings(_root);
 
776
        }
 
777
 
 
778
        void EditorWidgets::loadCodeGeneratorSettings(MyGUI::xml::ElementPtr _sectorNode)
 
779
        {
 
780
                MyGUI::xml::ElementEnumerator widget = _sectorNode->getElementEnumerator();
 
781
                while (widget.next())
 
782
                {
 
783
                        std::string key, value;
 
784
 
 
785
                        if (widget->getName() == "Property")
 
786
                        {
 
787
                                // парсим атрибуты
 
788
                                if (!widget->findAttribute("key", key))
 
789
                                        continue;
 
790
                                if (!widget->findAttribute("value", value))
 
791
                                        continue;
 
792
 
 
793
                                mCodeGeneratorSettings[key] = value;
 
794
                        }
 
795
                }
 
796
        }
 
797
 
 
798
        void EditorWidgets::saveCodeGeneratorSettings(MyGUI::xml::ElementPtr _rootNode)
 
799
        {
 
800
                MyGUI::xml::ElementPtr node = _rootNode->createChild(CodeGeneratorSettingsNodeName);
 
801
 
 
802
                for (MyGUI::MapString::const_iterator iter = mCodeGeneratorSettings.begin(); iter != mCodeGeneratorSettings.end(); ++iter)
 
803
                {
 
804
                        MyGUI::xml::ElementPtr nodeProp = node->createChild("Property");
 
805
                        nodeProp->addAttribute("key", iter->first);
 
806
                        nodeProp->addAttribute("value", iter->second);
 
807
                }
 
808
        }
 
809
 
 
810
        void EditorWidgets::onSetWidgetCoord(MyGUI::Widget* _widget, const MyGUI::IntCoord& _value, const std::string& _owner)
 
811
        {
 
812
                eventChangeWidgetCoord(_widget, _value, _owner);
 
813
        }
 
814
 
 
815
}