summaryrefslogtreecommitdiffstats
path: root/src/runtime/Qt3DSApplicationValues.h
blob: 80e8f73f8916ce9f3e368e7ffecee2429d178fbc (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/****************************************************************************
**
** Copyright (C) 2013 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#pragma once
#include "Qt3DSApplication.h"
#include "foundation/Qt3DSDiscriminatedUnion.h"
#include "foundation/StringTable.h"

namespace qt3ds {

class Qt3DSAssetVisitor
{
public:
    virtual void visit(const char *path) = 0;
    virtual void visit(const char *type, const char *id, const char *src, const char *args) = 0;
};

namespace runtime {

    using qt3ds::foundation::CRegisteredString;

    struct AssetValueTypes
    {
        enum Enum {
            NoAssetValue = 0,
            Presentation,
            SCXML,
            RenderPlugin,
            Behavior,
            QmlPresentation,
        };
    };
    struct SAssetBase
    {
        CRegisteredString m_Id;
        CRegisteredString m_Src;
        CRegisteredString m_Args;
        SAssetBase(CRegisteredString inId = CRegisteredString(),
                   CRegisteredString inSrc = CRegisteredString(),
                   CRegisteredString inArgs = CRegisteredString())
            : m_Id(inId)
            , m_Src(inSrc)
            , m_Args(inArgs)
        {
        }
        bool operator==(const SAssetBase &inOther) const
        {
            return m_Id == inOther.m_Id && m_Src == inOther.m_Src
                    && m_Args == inOther.m_Args;
        }
        template <typename TRemapper>
        void Remap(TRemapper &item)
        {
            item.Remap(m_Id);
            item.Remap(m_Src);
            item.Remap(m_Args);
        }
        virtual const char *Type() const
        {
            return nullptr;
        }
    };

    struct SPresentationAsset : public SAssetBase
    {
        Q3DStudio::CPresentation *m_Presentation;
        bool m_Active;

        SPresentationAsset(CRegisteredString inId = CRegisteredString(),
                           CRegisteredString inRelPath = CRegisteredString(),
                           Q3DStudio::CPresentation *inPresentation = NULL)
            : SAssetBase(inId, inRelPath, CRegisteredString())
            , m_Presentation(inPresentation)
            , m_Active(true)
        {
        }

        bool operator==(const SPresentationAsset &inOther) const
        {
            return SAssetBase::operator==(inOther) && m_Presentation == inOther.m_Presentation
                && m_Active == inOther.m_Active;
        }

        template <typename TRemapper>
        void Remap(TRemapper &item)
        {
            m_Presentation = NULL;
            SAssetBase::Remap(item);
        }
        const char *Type() const override
        {
            return "presentation";
        }
    };

    struct SSCXMLAsset : public SAssetBase
    {
        CRegisteredString m_Datamodel;
        SSCXMLAsset(CRegisteredString inId = CRegisteredString(),
                    CRegisteredString inRelPath = CRegisteredString(),
                    CRegisteredString inDatamodel = CRegisteredString())
            : SAssetBase(inId, inRelPath, CRegisteredString())
            , m_Datamodel(inDatamodel)
        {
        }
        bool operator==(const SSCXMLAsset &inOther) const
        {
            return SAssetBase::operator==(inOther) && m_Datamodel == inOther.m_Datamodel;
        }
        template <typename TRemapper>
        void Remap(TRemapper &item)
        {
            SAssetBase::Remap(item);
            item.Remap(m_Datamodel);
        }
        const char *Type() const override
        {
            return "scxml";
        }
    };

    struct SRenderPluginAsset : public SAssetBase
    {
        SRenderPluginAsset(CRegisteredString inId = CRegisteredString(),
                           CRegisteredString inRelPath = CRegisteredString(),
                           CRegisteredString inArgs = CRegisteredString())
            : SAssetBase(inId, inRelPath, inArgs)
        {
        }
        const char *Type() const override
        {
            return "renderplugin";
        }
    };

    struct SQmlPresentationAsset : public SAssetBase
    {
        SQmlPresentationAsset(CRegisteredString inId = CRegisteredString(),
                           CRegisteredString inRelPath = CRegisteredString(),
                           CRegisteredString inArgs = CRegisteredString())
            : SAssetBase(inId, inRelPath, inArgs)
        {
        }
        const char *Type() const override
        {
            return "presentation-qml";
        }
    };

    struct SBehaviorAsset : public SAssetBase
    {
        Q3DStudio::INT32 m_Handle;
        SBehaviorAsset(CRegisteredString inId = CRegisteredString(),
                       CRegisteredString inRelPath = CRegisteredString(),
                       Q3DStudio::INT32 inHandle = 0)
            : SAssetBase(inId, inRelPath, CRegisteredString())
            , m_Handle(inHandle)
        {
        }
        bool operator==(const SBehaviorAsset &inOther) const
        {
            return SAssetBase::operator==(inOther) && m_Handle == inOther.m_Handle;
        }
        template <typename TRemapper>
        void Remap(TRemapper &item)
        {
            m_Handle = -1;
            SAssetBase::Remap(item);
        }
        const char *Type() const override
        {
            return "behaviour";
        }
    };
}
}

namespace qt3ds {
namespace foundation {
    template <>
    struct DestructTraits<qt3ds::runtime::SPresentationAsset>
    {
        void destruct(qt3ds::runtime::SPresentationAsset &) {}
    };
    template <>
    struct DestructTraits<qt3ds::runtime::SSCXMLAsset>
    {
        void destruct(qt3ds::runtime::SSCXMLAsset &) {}
    };
    template <>
    struct DestructTraits<qt3ds::runtime::SRenderPluginAsset>
    {
        void destruct(qt3ds::runtime::SRenderPluginAsset &) {}
    };
    template <>
    struct DestructTraits<qt3ds::runtime::SBehaviorAsset>
    {
        void destruct(qt3ds::runtime::SBehaviorAsset &) {}
    };
}
}

namespace qt3ds {
namespace runtime {

    // Force compile error if unsupported datatype requested
    template <typename TDataType>
    struct SAssetValueTypeMap
    {
    };

    template <>
    struct SAssetValueTypeMap<SPresentationAsset>
    {
        static AssetValueTypes::Enum GetType() { return AssetValueTypes::Presentation; }
    };
    template <>
    struct SAssetValueTypeMap<SSCXMLAsset>
    {
        static AssetValueTypes::Enum GetType() { return AssetValueTypes::SCXML; }
    };
    template <>
    struct SAssetValueTypeMap<SRenderPluginAsset>
    {
        static AssetValueTypes::Enum GetType() { return AssetValueTypes::RenderPlugin; }
    };
    template <>
    struct SAssetValueTypeMap<SQmlPresentationAsset>
    {
        static AssetValueTypes::Enum GetType() { return AssetValueTypes::QmlPresentation; }
    };
    template <>
    struct SAssetValueTypeMap<SBehaviorAsset>
    {
        static AssetValueTypes::Enum GetType() { return AssetValueTypes::Behavior; }
    };

    struct SAssetValueUnionTraits
    {
        typedef AssetValueTypes::Enum TIdType;
        enum {
            TBufferSize = sizeof(SPresentationAsset),
        };

        static TIdType getNoDataId() { return AssetValueTypes::NoAssetValue; }

        template <typename TDataType>
        static TIdType getType()
        {
            return SAssetValueTypeMap<TDataType>().GetType();
        }

        template <typename TRetType, typename TVisitorType>
        static TRetType visit(char *inData, TIdType inType, TVisitorType inVisitor)
        {
            switch (inType) {
            case AssetValueTypes::Presentation:
                return inVisitor(*NVUnionCast<SPresentationAsset *>(inData));
            case AssetValueTypes::SCXML:
                return inVisitor(*NVUnionCast<SSCXMLAsset *>(inData));
            case AssetValueTypes::RenderPlugin:
                return inVisitor(*NVUnionCast<SRenderPluginAsset *>(inData));
            case AssetValueTypes::Behavior:
                return inVisitor(*NVUnionCast<SBehaviorAsset *>(inData));
            case AssetValueTypes::QmlPresentation:
                return inVisitor(*NVUnionCast<SQmlPresentationAsset*>(inData));
            default:
                QT3DS_ASSERT(false);
            case AssetValueTypes::NoAssetValue:
                return inVisitor();
            }
        }

        template <typename TRetType, typename TVisitorType>
        static TRetType visit(const char *inData, TIdType inType, TVisitorType inVisitor)
        {
            switch (inType) {
            case AssetValueTypes::Presentation:
                return inVisitor(*NVUnionCast<const SPresentationAsset *>(inData));
            case AssetValueTypes::SCXML:
                return inVisitor(*NVUnionCast<const SSCXMLAsset *>(inData));
            case AssetValueTypes::RenderPlugin:
                return inVisitor(*NVUnionCast<const SRenderPluginAsset *>(inData));
            case AssetValueTypes::Behavior:
                return inVisitor(*NVUnionCast<const SBehaviorAsset *>(inData));
            case AssetValueTypes::QmlPresentation:
                return inVisitor(*NVUnionCast<const SQmlPresentationAsset *>(inData));
            default:
                QT3DS_ASSERT(false);
            case AssetValueTypes::NoAssetValue:
                return inVisitor();
            }
        }
    };

    typedef qt3ds::foundation::
        DiscriminatedUnion<qt3ds::foundation::
                               DiscriminatedUnionGenericBase<SAssetValueUnionTraits,
                                                             SAssetValueUnionTraits::TBufferSize>,
                           SAssetValueUnionTraits::TBufferSize>
            TAssetValueUnionType;

    struct SAssetValue : public TAssetValueUnionType
    {
        SAssetValue() {}

        SAssetValue(const SAssetValue &inOther)
            : TAssetValueUnionType(static_cast<const TAssetValueUnionType &>(inOther))
        {
        }

        template <typename TDataType>
        SAssetValue(const TDataType &inDt)
            : TAssetValueUnionType(inDt)
        {
        }

        SAssetValue &operator=(const SAssetValue &inOther)
        {
            TAssetValueUnionType::operator=(inOther);
            return *this;
        }

        bool operator==(const SAssetValue &inOther) const
        {
            return TAssetValueUnionType::operator==(inOther);
        }
        bool operator!=(const SAssetValue &inOther) const
        {
            return TAssetValueUnionType::operator!=(inOther);
        }

        bool empty() const { return getType() == AssetValueTypes::NoAssetValue; }

        CRegisteredString GetSource()
        {
            if (empty())
                return CRegisteredString();
            return reinterpret_cast<SAssetBase *>(m_Data)->m_Src;
        }
    };
}
}