File: ModelPreview.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 (215 lines) | stat: -rw-r--r-- 4,834 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
208
209
210
211
212
213
214
215
#include "ModelPreview.h"
#include "../GLWidget.h"

#include "ifilter.h"
#include "imodelcache.h"
#include "i18n.h"
#include "ieclass.h"
#include "math/AABB.h"
#include "modelskin.h"
#include "entitylib.h"
#include "scenelib.h"
#include "scene/Node.h"
#include "scene/BasicRootNode.h"
#include "wxutil/dialog/MessageBox.h"
#include "string/convert.h"
#include "fmt/format.h"

namespace wxutil
{

namespace
{
	constexpr const char* const FUNC_STATIC_CLASS = "func_static";
}

ModelPreview::ModelPreview(wxWindow* parent) :
    EntityPreview(parent)
{}

ModelPreview::~ModelPreview()
{
    _skinDeclChangedConn.disconnect();
}

const std::string& ModelPreview::getModel() const
{
    return _model;
}

const std::string& ModelPreview::getSkin() const
{
    return _skin;
}

void ModelPreview::setModel(const std::string& model)
{
    // Remember the name and mark the scene as "not ready"
    _model = model;
    queueSceneUpdate();

    if (!_model.empty())
    {
        // Reset time if the model has changed
        if (_model != _lastModel)
        {
            // Reset preview time
            stopPlayback();
        }

        // Redraw
        queueDraw();
    }
    else
    {
        stopPlayback();
    }
}

void ModelPreview::setSkin(const std::string& skin) {

    _skin = skin;
    _skinDeclChangedConn.disconnect();
    queueSceneUpdate();

    // Redraw
    queueDraw();
}

void ModelPreview::setupSceneGraph()
{
    EntityPreview::setupSceneGraph();

    try
    {
        // Add a hidden func_static as preview entity
        auto entity = GlobalEntityModule().createEntity(
            GlobalEntityClassManager().findClass(FUNC_STATIC_CLASS));

        setEntity(entity);

        entity->enable(scene::Node::eHidden);
        entity->getEntity().setKeyValue("model", "-");
    }
    catch (std::runtime_error&)
    {
        Messagebox::ShowError(fmt::format(
            _("Unable to setup the preview,\ncould not find the entity class '{0}'"),
            FUNC_STATIC_CLASS));
    }
}

void ModelPreview::prepareScene()
{
    EntityPreview::prepareScene();

    // If the model name is empty, release the model
    if (_model.empty())
    {
        if (_modelNode)
        {
            getEntity()->removeChildNode(_modelNode);
        }

        _modelNode.reset();

        // Emit the signal carrying an empty pointer
        _modelLoadedSignal.emit(model::ModelNodePtr());
        return;
    }

    if (_modelNode)
    {
        getEntity()->removeChildNode(_modelNode);
    }

    // Check if the model key is pointing to a def
    auto modelDef = GlobalEntityClassManager().findModel(_model);

    _modelNode = GlobalModelCache().getModelNode(modelDef ? modelDef->getMesh() : _model);

    if (_modelNode)
    {
        getEntity()->addChildNode(_modelNode);

        // Apply the skin
        applySkin();

        // Apply the idle pose if possible
        if (modelDef)
        {
            // Set the default skin to the one defined in the modelDef
            auto skinned = std::dynamic_pointer_cast<SkinnedModel>(_modelNode);

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

            scene::applyIdlePose(_modelNode, modelDef);
        }

        setupInitialViewPosition();

        _lastModel = _model;

        // Done loading, emit the signal
        _modelLoadedSignal.emit(Node_getModel(_modelNode));
    }
}

void ModelPreview::setupInitialViewPosition()
{
    if (_lastModel != _model)
    {
        // Reset the model rotation
        resetModelRotation();

        // Reset the default view, facing down to the model from diagonally above the bounding box
        double distance = getSceneBounds().getRadius() * _defaultCamDistanceFactor;

        setViewOrigin(getSceneBounds().getOrigin() + Vector3(1, 1, 1) * distance);
        setViewAngles(Vector3(34, 135, 0));
    }
}

AABB ModelPreview::getSceneBounds()
{
    if (!_modelNode)
    {
        return EntityPreview::getSceneBounds();
    }

    return _modelNode->localAABB();
}

sigc::signal<void, const model::ModelNodePtr&>& ModelPreview::signal_ModelLoaded()
{
    return _modelLoadedSignal;
}

void ModelPreview::applySkin()
{
    if (auto model = Node_getModel(_modelNode); model)
    {
        auto skin = GlobalModelSkinCache().findSkin(_skin);

        if (skin)
        {
            _skinDeclChangedConn.disconnect();
            _skinDeclChangedConn = skin->signal_DeclarationChanged().connect(
                sigc::mem_fun(*this, &ModelPreview::onSkinDeclarationChanged));
        }

        model->getIModel().applySkin(skin);
    }
}

void ModelPreview::onSkinDeclarationChanged()
{
    applySkin();
    queueDraw();
}

} // namespace ui