File: ModelKey.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (207 lines) | stat: -rw-r--r-- 5,341 bytes parent folder | download | duplicates (2)
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
#include "ModelKey.h"

#include <functional>

#include "entitylib.h"
#include "imodelcache.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;

    attachModelNodeKeepingSkin();
}

// 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)
	attachModelNodeKeepingSkin();
}

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)
    {
        // Set the default skin to the one defined in the modelDef
        auto skinned = std::dynamic_pointer_cast<SkinnedModel>(_model.node);

        if (skinned && !modelDef->getSkin().empty())
        {
            skinned->setDefaultSkin(modelDef->getSkin());
            skinned->skinChanged(std::string()); // trigger a remap
        }

        scene::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()
{
    attachModelNodeKeepingSkin();
}

void ModelKey::attachModelNodeKeepingSkin()
{
    if (_model.node)
    {
        attachModelNode();

        // If we have an explicit skin value, set it on the new model
        if (auto skinned = std::dynamic_pointer_cast<SkinnedModel>(_model.node); skinned)
        {
            skinned->skinChanged(_model.explicitSkin);
        }
    }
    else
    {
        // No existing model, just attach it
        attachModelNode();
    }
}

void ModelKey::skinChanged(const std::string& value)
{
    // Store this value locally
    _model.explicitSkin = value;

    // Check if we have a skinnable model
    if (auto skinned = std::dynamic_pointer_cast<SkinnedModel>(_model.node); skinned)
    {
        skinned->skinChanged(_model.explicitSkin);
    }
}

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;
}