/
ModelKey.cpp
226 lines (175 loc) · 5.52 KB
/
ModelKey.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "ModelKey.h"
#include <functional>
#include "imodelcache.h"
#include "imd5model.h"
#include "ifilter.h"
#include "modelskin.h"
#include "string/replace.h"
#include "scenelib.h"
ModelKey::ModelKey(scene::INode& parentNode) :
_parentNode(parentNode),
_active(true),
_undo(_model, std::bind(&ModelKey::importState, this, std::placeholders::_1))
{}
const scene::INodePtr& ModelKey::getNode() const
{
return _model.node;
}
void ModelKey::destroy()
{
detachModelNode();
_model.node.reset();
_model.path.clear();
_active = false;
}
void ModelKey::refreshModel()
{
if (!_model.node) return;
attachModelNodeKeepinSkin();
}
// Update the contained model from the provided keyvalues
void ModelKey::modelChanged(const std::string& value)
{
if (!_active) return; // deactivated during parent node destruction
// Sanitise the keyvalue - must use forward slashes
auto newModelName = string::replace_all_copy(value, "\\", "/");
if (newModelName == _model.path)
{
return; // new name is the same as we have now
}
_undo.save();
// Now store the new modelpath
_model.path = newModelName;
// Call the attach routine, keeping the skin (#4142)
attachModelNodeKeepinSkin();
}
void ModelKey::attachModelNode()
{
// Remove the old model node first (this also clears the pointer)
detachModelNode();
// If the "model" spawnarg is empty, there's nothing to attach
if (_model.path.empty()) return;
// The actual model path to request the model from the file cache
std::string actualModelPath(_model.path);
// Check if the model key is pointing to a def
auto modelDef = GlobalEntityClassManager().findModel(_model.path);
if (modelDef)
{
// We have a valid modelDef, use the mesh defined there
actualModelPath = modelDef->getMesh();
// Start watching the modelDef for changes
subscribeToModelDef(modelDef);
}
// We have a non-empty model key, send the request to
// the model cache to acquire a new child node
_model.node = GlobalModelCache().getModelNode(actualModelPath);
// The model loader should not return NULL, but a sanity check is always ok
if (!_model.node) return;
// Add the model node as child of the entity node
_parentNode.addChildNode(_model.node);
// Assign the model node to the same layers as the parent entity
_model.node->assignToLayers(_parentNode.getLayers());
// Inherit the parent node's visibility. This should do the trick to resolve #2709
// but is not as heavy on performance as letting the Filtersystem check the whole subgraph
// Copy the visibility flags from the parent node (#4141 and #5134)
scene::assignVisibilityFlagsFromNode(*_model.node, _parentNode);
// Assign idle pose to modelDef meshes
if (modelDef)
{
applyIdlePose(_model.node, modelDef);
}
// Mark the transform of this model as changed, it must re-evaluate itself
_model.node->transformChanged();
}
void ModelKey::detachModelNode()
{
unsubscribeFromModelDef();
if (!_model.node) return; // nothing to do
_parentNode.removeChildNode(_model.node);
_model.node.reset();
}
void ModelKey::onModelDefChanged()
{
attachModelNodeKeepinSkin();
}
void ModelKey::attachModelNodeKeepinSkin()
{
if (_model.node)
{
// Check if we have a skinnable model and remember the skin
SkinnedModelPtr skinned = std::dynamic_pointer_cast<SkinnedModel>(_model.node);
std::string skin = skinned ? skinned->getSkin() : "";
attachModelNode();
// Reset the skin to the previous value if we have a model
skinned = std::dynamic_pointer_cast<SkinnedModel>(_model.node);
if (skinned)
{
skinned->skinChanged(skin);
}
}
else
{
// No existing model, just attach it
attachModelNode();
}
}
void ModelKey::skinChanged(const std::string& value)
{
// Check if we have a skinnable model
auto skinned = std::dynamic_pointer_cast<SkinnedModel>(_model.node);
if (skinned)
{
skinned->skinChanged(value);
}
}
void ModelKey::connectUndoSystem(IUndoSystem& undoSystem)
{
_undo.connectUndoSystem(undoSystem);
}
void ModelKey::disconnectUndoSystem(IUndoSystem& undoSystem)
{
_undo.disconnectUndoSystem(undoSystem);
}
void ModelKey::importState(const ModelNodeAndPath& data)
{
_model.path = data.path;
_model.node = data.node;
_model.modelDefMonitored = data.modelDefMonitored;
if (_model.modelDefMonitored)
{
unsubscribeFromModelDef();
if (auto modelDef = GlobalEntityClassManager().findModel(_model.path); modelDef)
{
subscribeToModelDef(modelDef);
}
}
}
void ModelKey::subscribeToModelDef(const IModelDef::Ptr& modelDef)
{
// Monitor this modelDef for potential mesh changes
_modelDefChanged = modelDef->signal_DeclarationChanged().connect(
sigc::mem_fun(this, &ModelKey::onModelDefChanged)
);
_model.modelDefMonitored = true;
}
void ModelKey::unsubscribeFromModelDef()
{
_modelDefChanged.disconnect();
_model.modelDefMonitored = false;
}
void ModelKey::applyIdlePose(const scene::INodePtr& node, const IModelDef::Ptr& modelDef)
{
auto modelNode = Node_getModel(node);
if (!modelNode) return;
// Set the animation to play
auto md5model = dynamic_cast<md5::IMD5Model*>(&(modelNode->getIModel()));
// Look up the "idle" anim if there is one
auto found = modelDef->getAnim("idle");
if (found.empty()) return;
// Load the anim
if (auto anim = GlobalAnimationCache().getAnim(found))
{
md5model->setAnim(anim);
md5model->updateAnim(0);
}
}