/
PropertyEditorFactory.cpp
144 lines (114 loc) · 4.08 KB
/
PropertyEditorFactory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "PropertyEditorFactory.h"
#include "itextstream.h"
#include "Vector3PropertyEditor.h"
#include "BooleanPropertyEditor.h"
#include "EntityPropertyEditor.h"
#include "ColourPropertyEditor.h"
#include "TexturePropertyEditor.h"
#include "SkinPropertyEditor.h"
#include "SoundPropertyEditor.h"
#include "FloatPropertyEditor.h"
#include "ModelPropertyEditor.h"
#include "ClassnamePropertyEditor.h"
#include "AnglePropertyEditor.h"
#include <regex>
#include "wxutil/Bitmap.h"
namespace ui
{
PropertyEditorFactory::PropertyEditorFactory()
{
registerBuiltinTypes();
}
PropertyEditorFactory::~PropertyEditorFactory()
{
_customEditors.clear();
_peMap.clear();
_dialogs.clear();
}
void PropertyEditorFactory::registerBuiltinTypes()
{
_peMap["vector3"] = Vector3PropertyEditor::CreateNew;
_peMap["bool"] = BooleanPropertyEditor::CreateNew;
_peMap["entity"] = EntityPropertyEditor::CreateNew;
_peMap["colour"] = ColourPropertyEditor::CreateNew;
_peMap["color"] = ColourPropertyEditor::CreateNew;
_peMap["texture"] = TexturePropertyEditor::CreateNew;
_peMap["mat"] = TexturePropertyEditor::CreateNew;
_peMap["skin"] = SkinPropertyEditor::CreateNew;
_peMap["sound"] = SoundPropertyEditor::CreateNew;
_peMap["float"] = FloatPropertyEditor::CreateNew;
_peMap["model"] = ModelPropertyEditor::CreateNew;
_peMap["classname"] = ClassnamePropertyEditor::CreateNew;
_peMap["angle"] = AnglePropertyEditor::CreateNew;
_dialogs["skin"] = []() { return std::make_shared<SkinChooserDialogWrapper>(); };
}
void PropertyEditorFactory::registerPropertyEditor(const std::string& key, const IPropertyEditor::CreationFunc& creator)
{
auto result = _customEditors.emplace(key, creator);
if (!result.second)
{
rWarning() << "Could not register property editor for key " << key
<< ", it is already associated." << std::endl;;
}
}
void PropertyEditorFactory::unregisterPropertyEditor(const std::string& key)
{
auto found = _customEditors.find(key);
if (found != _customEditors.end())
{
_customEditors.erase(found);
return;
}
rWarning() << "Cannot unregister property editor for key " << key << std::endl;
}
IPropertyEditor::Ptr PropertyEditorFactory::create(wxWindow* parent, const std::string& className,
IEntitySelection& entities, const std::string& key, const std::string& options)
{
// greebo: First, search the custom editors for a match
for (const auto& pair : _customEditors)
{
if (pair.first.empty()) continue; // skip empty keys
// Try to match the entity key against the regex (i->first)
std::regex expr(pair.first);
std::smatch matches;
if (!std::regex_match(key, matches, expr)) continue;
// We have a match, invoke the creation function
return pair.second(parent, entities, key, options);
}
// No custom editor found, search for the named property editor type
auto iter = _peMap.find(className);
// If the type is not found, return NULL otherwise create a new instance of
// the associated derived type.
if (iter == _peMap.end())
{
return {};
}
return iter->second(parent, entities, key, options);
}
void PropertyEditorFactory::registerPropertyEditorDialog(const std::string& key, const IPropertyEditorDialog::CreationFunc& create)
{
auto result = _dialogs.emplace(key, create);
if (!result.second)
{
rWarning() << "Could not register property editor dialog for key " << key
<< ", it is already associated." << std::endl;;
}
}
IPropertyEditorDialog::Ptr PropertyEditorFactory::createDialog(const std::string& key)
{
auto dialog = _dialogs.find(key);
return dialog != _dialogs.end() ? dialog->second() : IPropertyEditorDialog::Ptr();
}
void PropertyEditorFactory::unregisterPropertyEditorDialog(const std::string& key)
{
_dialogs.erase(key);
}
wxBitmap PropertyEditorFactory::getBitmapFor(const std::string& type)
{
// Sanity check
if (type.empty()) return wxNullBitmap;
std::string iconName = "icon_" + type + ".png";
auto candidate = wxutil::GetLocalBitmap(iconName);
return candidate.IsOk() ? candidate : wxutil::GetLocalBitmap("empty.png");
}
}