summaryrefslogtreecommitdiffstats
path: root/src/dm/systems/cores/SimpleSlideCore.cpp
blob: 87eadb9fc06db1eb0ca22284aca3e4ac87f26f8a (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/****************************************************************************
**
** Copyright (C) 1993-2009 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-EXCEPT$
** 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 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** 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$
**
****************************************************************************/
#include "Qt3DSDMPrefix.h"
#include "SimpleSlideCore.h"

using namespace std;
#ifdef _WIN32
#pragma warning(disable : 4503)
#endif

namespace qt3dsdm {

Qt3DSDMSlideHandle CSimpleSlideCore::CreateSlide(Qt3DSDMInstanceHandle inInstance)
{
    int nextId = GetNextId();
    return CreateSlideWithHandle(nextId, inInstance);
}

Qt3DSDMInstanceHandle CSimpleSlideCore::GetSlideInstance(Qt3DSDMSlideHandle inSlide) const
{
    return GetSlideNF(inSlide, m_Objects)->m_Instance;
}

inline bool SlideInstanceMatches(const THandleObjectPair &inPair, int inInstance)
{
    if (inPair.second->GetType() == CHandleObject::EHandleObjectTypeSSlide
        && static_cast<const SSlide *>(inPair.second.get())->m_Instance == inInstance)
        return true;
    return false;
}

Qt3DSDMSlideHandle CSimpleSlideCore::GetSlideByInstance(Qt3DSDMInstanceHandle inInstance) const
{
    THandleObjectMap::const_iterator theSlide =
        find_if(m_Objects.begin(), m_Objects.end(),
                std::bind(SlideInstanceMatches,
                          std::placeholders::_1, inInstance.GetHandleValue()));
    if (theSlide != m_Objects.end())
        return theSlide->first;
    throw SlideNotFound(L"");
}

void RecurseDeleteSlide(Qt3DSDMSlideHandle inSlide, THandleObjectMap &inObjects,
                        TInstanceHandleList &outInstances)
{
    SSlide *theSlide = CSimpleSlideCore::GetSlideNF(inSlide, inObjects);
    do_all(theSlide->m_Children,
           std::bind(RecurseDeleteSlide, std::placeholders::_1,
                     std::ref(inObjects), std::ref(outInstances)));
    outInstances.push_back(theSlide->m_Instance);
    CHandleBase::EraseHandle(inSlide, inObjects);
}

void CSimpleSlideCore::DeleteSlide(Qt3DSDMSlideHandle inSlide, TInstanceHandleList &outInstances)
{
    SSlide *theSlide = GetSlideNF(inSlide, m_Objects);
    if (theSlide->m_Parent) {
        SSlide *theParent = GetSlideNF(theSlide->m_Parent, m_Objects);
        erase_if(theParent->m_Children, std::bind(equal_to<int>(), theSlide->m_Handle,
                                                  std::placeholders::_1));
    }
    RecurseDeleteSlide(inSlide, m_Objects, outInstances);
}

template <typename TDataType, typename TVectorType>
inline void MaybeAddObject(const THandleObjectPair &inPair, vector<TVectorType> &outVectorItems)
{
    if (inPair.second->GetType() == TDataType::s_Type)
        outVectorItems.push_back(inPair.first);
}

void CSimpleSlideCore::GetSlides(TSlideHandleList &outSlides) const
{
    do_all(m_Objects,
           std::bind(MaybeAddObject<SSlide, Qt3DSDMSlideHandle>,
                     std::placeholders::_1, std::ref(outSlides)));
}

float CSimpleSlideCore::GetSlideTime(Qt3DSDMSlideHandle inSlide) const
{
    return GetSlideNF(inSlide, m_Objects)->m_Time;
}

void CSimpleSlideCore::SetSlideTime(Qt3DSDMSlideHandle inSlide, float inNewTime)
{
    GetSlideNF(inSlide, m_Objects)->m_Time = inNewTime;
}

void CSimpleSlideCore::DeriveSlide(Qt3DSDMSlideHandle inSlide, Qt3DSDMSlideHandle inParent,
                                   int inIndex)
{
    int oldParent = GetSlideNF(inSlide, m_Objects)->m_Parent;
    if (oldParent)
        erase_if(GetSlideNF(oldParent, m_Objects)->m_Children,
                 std::bind(equal_to<int>(), inSlide, std::placeholders::_1));
    if (inParent.Valid()) {
        SSlide *theParent = GetSlideNF(inParent, m_Objects);
        if (exists(theParent->m_Children, std::bind(equal_to<int>(), inSlide,
                                                    std::placeholders::_1)))
            throw SlideDerivationError(L"Already derived");
        if (inIndex < 0 || inIndex >= (int)theParent->m_Children.size())
            inIndex = (int)theParent->m_Children.size();
        theParent->m_Children.insert(theParent->m_Children.begin() + inIndex,
                                     inSlide.GetHandleValue());
    }
    GetSlideNF(inSlide, m_Objects)->m_Parent = inParent;
}

Qt3DSDMSlideHandle CSimpleSlideCore::GetParentSlide(Qt3DSDMSlideHandle inSlide) const
{
    return GetSlideNF(inSlide, m_Objects)->m_Parent;
}

void CSimpleSlideCore::GetChildSlides(Qt3DSDMSlideHandle inSlide,
                                      TSlideHandleList &outChildren) const
{
    transformv_all(GetSlideNF(inSlide, m_Objects)->m_Children, outChildren);
}

int CSimpleSlideCore::GetChildIndex(Qt3DSDMSlideHandle inParent, Qt3DSDMSlideHandle inChild) const
{
    const SSlide *theSlide = GetSlideNF(inParent, m_Objects);
    size_t dist = distance(theSlide->m_Children.begin(),
                           find_if<TIntList::const_iterator>(
                               theSlide->m_Children, std::bind(equal_to<int>(), inChild,
                                                               std::placeholders::_1)));
    if (dist == theSlide->m_Children.size())
        throw SlideChildNotFoundError(L"");
    return (int)dist;
}

bool CSimpleSlideCore::GetInstancePropertyValue(Qt3DSDMSlideHandle inSlide,
                                                Qt3DSDMInstanceHandle inHandle,
                                                Qt3DSDMPropertyHandle inProperty,
                                                SValue &outValue) const
{
    const SSlide *theSlide = GetSlideNF(inSlide, m_Objects);
    SInternValue *theValue = theSlide->GetInstancePropertyValue(inHandle, inProperty);
    if (theValue) {
        outValue = theValue->GetValue();
        return true;
    }
    if (theSlide->m_Parent)
        return GetInstancePropertyValue(theSlide->m_Parent, inHandle, inProperty, outValue);
    return false;
}

std::pair<SSlide *, SInternValue *> CSimpleSlideCore::ResolveSetInstancePropertyValue(
    Qt3DSDMSlideHandle inSlide, Qt3DSDMInstanceHandle inHandle, Qt3DSDMPropertyHandle inProperty)
{
    SSlide *theSlide = CSimpleSlideCore::GetSlideNF(inSlide, m_Objects);
    SInternValue *theValue = theSlide->GetInstancePropertyValue(inHandle, inProperty);
    // If we have the value already *or* or parent is not a valid slide, then return now
    if (theValue || theSlide->m_Parent == 0)
        return std::make_pair(theSlide, theValue);
    // Else give our parent a chance.
    return ResolveSetInstancePropertyValue(theSlide->m_Parent, inHandle, inProperty);
}

void CSimpleSlideCore::SetInstancePropertyValue(Qt3DSDMSlideHandle inSlide,
                                                Qt3DSDMInstanceHandle inHandle,
                                                Qt3DSDMPropertyHandle inProperty,
                                                const SValue &inValue)
{
    std::pair<SSlide *, SInternValue *> theTarget(
        ResolveSetInstancePropertyValue(inSlide, inHandle, inProperty));
    SInternValue theValue(inValue, GetStringTable());
    if (theTarget.second)
        *theTarget.second = theValue;
    else
        theTarget.first->SetInstancePropertyValue(inHandle, inProperty, theValue);
}

void CSimpleSlideCore::ForceSetInstancePropertyValue(Qt3DSDMSlideHandle inSlide,
                                                     Qt3DSDMInstanceHandle inHandle,
                                                     Qt3DSDMPropertyHandle inProperty,
                                                     const SValue &inValue)
{
    CSimpleSlideCore::ForceSetPropertyValue(GetStringTable(), m_Objects, inSlide, inHandle,
                                            inProperty, inValue);
}

void CSimpleSlideCore::forceSetInstancePropertyValueOnAllSlides(Qt3DSDMInstanceHandle inInstance,
                                                                Qt3DSDMPropertyHandle inProperty,
                                                                const SValue &inValue)
{
    for (auto &it : m_Objects) {
        if (it.second->GetType() == SSlide::s_Type) {
            Qt3DSDMSlideHandle slide(it.first);
            ForceSetInstancePropertyValue(slide, inInstance, inProperty, inValue);
        }
    }
}

bool CSimpleSlideCore::GetSpecificInstancePropertyValue(Qt3DSDMSlideHandle inSlide,
                                                        Qt3DSDMInstanceHandle inInstance,
                                                        Qt3DSDMPropertyHandle inProperty,
                                                        SValue &outValue) const
{
    const SSlide *theSlide = GetSlideNF(inSlide, m_Objects);
    SInternValue *theValue = theSlide->GetInstancePropertyValue(inInstance, inProperty);
    if (theValue) {
        outValue = theValue->GetValue();
        return true;
    }
    return false;
}

void CSimpleSlideCore::GetSpecificInstancePropertyValues(Qt3DSDMSlideHandle inSlide,
                                                         Qt3DSDMInstanceHandle inInstance,
                                                         TPropertyHandleValuePairList &outValues)
{
    const SSlide *theSlide = GetSlideNF(inSlide, m_Objects);
    theSlide->GetSpecificInstancePropertyValues(inInstance, outValues);
}

void CSimpleSlideCore::GetSlidePropertyEntries(Qt3DSDMSlideHandle inSlide,
                                               TSlideEntryList &outEntries) const
{
    const SSlide *theSlide = GetSlideNF(inSlide, m_Objects);
    theSlide->ToSlideEntryList(outEntries);
}

bool CSimpleSlideCore::ContainsProperty(Qt3DSDMSlideHandle inSlide, Qt3DSDMInstanceHandle inHandle,
                                        Qt3DSDMPropertyHandle inProperty) const
{
    const SSlide *theSlide = GetSlideNF(inSlide, m_Objects);
    return theSlide->GetInstancePropertyValue(inHandle, inProperty) != NULL;
}

Qt3DSDMSlideHandle CSimpleSlideCore::CreateSlideWithHandle(int inHandle,
                                                          Qt3DSDMInstanceHandle inInstance)
{
    if (HandleValid(inHandle))
        throw HandleExists(L"");
    m_Objects.insert(make_pair(inHandle, (THandleObjectPtr) new SSlide(inHandle, inInstance)));
    return inHandle;
}

void CSimpleSlideCore::GetSlideProperties(Qt3DSDMSlideHandle inSlide,
                                          TSlideEntryList &outProperties) const
{
    outProperties.clear();
    GetSlidePropertyEntries(inSlide, outProperties);
}

bool CSimpleSlideCore::IsSlide(Qt3DSDMSlideHandle inSlide) const
{
    return m_Objects.find(inSlide) != m_Objects.end();
}

void CSimpleSlideCore::ForceSetPropertyValue(IStringTable &inStringTable,
                                             THandleObjectMap &inObjects, Qt3DSDMSlideHandle inSlide,
                                             Qt3DSDMInstanceHandle inHandle,
                                             Qt3DSDMPropertyHandle inProperty, const SValue &inValue)
{
    CSimpleSlideCore::GetSlideNF(inSlide, inObjects)
        ->SetInstancePropertyValue(inHandle, inProperty, SInternValue(inValue, inStringTable));
}

void CSimpleSlideCore::PushPropertyValueToChildren(Qt3DSDMSlideHandle inParent,
                                                   Qt3DSDMInstanceHandle inHandle,
                                                   Qt3DSDMPropertyHandle inProperty,
                                                   const SValue &inValue)
{
    ForceSetPropertyValue(GetStringTable(), m_Objects, inParent, inHandle, inProperty, inValue);
    do_all(CSimpleSlideCore::GetSlideNF(inParent, m_Objects)->m_Children,
           std::bind(ForceSetPropertyValue, std::ref(GetStringTable()), std::ref(m_Objects),
                       std::placeholders::_1, inHandle, inProperty, inValue));
}

inline void AddIntersectingEntry(TSlideEntryList &outEntries, Qt3DSDMInstanceHandle inst,
                                 Qt3DSDMPropertyHandle prop, const SInternValue &inValue)
{
    outEntries.push_back(TSlideEntry(inst, prop, inValue.GetValue()));
}

void CSimpleSlideCore::GetIntersectingProperties(Qt3DSDMSlideHandle inSlide1,
                                                 Qt3DSDMSlideHandle inSlide2,
                                                 TSlideEntryList &outEntries) const
{
    const SSlide *theSlide1 = GetSlideNF(inSlide1, m_Objects);
    const SSlide *theSlide2 = GetSlideNF(inSlide2, m_Objects);
    theSlide1->IntersectProperties(
        *theSlide2, std::bind(AddIntersectingEntry, std::ref(outEntries),
                              std::placeholders::_1, std::placeholders::_2,
                              std::placeholders::_3));
}

void CSimpleSlideCore::PushIntersectingProperties(Qt3DSDMSlideHandle inSlide1,
                                                  Qt3DSDMSlideHandle inSlide2,
                                                  Qt3DSDMSlideHandle inDestination)
{
    const SSlide *theSlide1 = GetSlideNF(inSlide1, m_Objects);
    const SSlide *theSlide2 = GetSlideNF(inSlide2, m_Objects);
    SSlide *theDest = GetSlideNF(inDestination, m_Objects);
    theSlide1->IntersectProperties(
        *theSlide2, std::bind(&SSlide::SetInstancePropertyValue, theDest,
                              std::placeholders::_1, std::placeholders::_2,
                              std::placeholders::_3));
}

void CSimpleSlideCore::ClearPropertyValue(THandleObjectMap &inObjects, Qt3DSDMSlideHandle inSlide,
                                          Qt3DSDMInstanceHandle inInstance,
                                          Qt3DSDMPropertyHandle inProperty)
{
    CSimpleSlideCore::GetSlideNF(inSlide, inObjects)
        ->RemoveInstancePropertyValue(inInstance, inProperty);
}

inline void DoForEachSlide(std::pair<int, THandleObjectPtr> inObject,
                           std::function<void(SSlide *)> inFunction)
{
    inFunction((SSlide *)inObject.second.get());
}

void CSimpleSlideCore::ForEachSlide(std::function<void(SSlide *)> inFunction)
{
    do_all(m_Objects, std::bind(DoForEachSlide, std::placeholders::_1, inFunction));
}

void LookupSlideAndDoSomething(Qt3DSDMSlideHandle inSlide, THandleObjectMap &inObjects,
                               std::function<void(SSlide *)> inFunction)
{
    inFunction(CSimpleSlideCore::GetSlideNF(inSlide, inObjects));
}

void CSimpleSlideCore::ForEachChild(Qt3DSDMSlideHandle inParent,
                                    std::function<void(SSlide *)> inFunction)
{
    do_all(GetSlideNF(inParent, m_Objects)->m_Children,
           std::bind(LookupSlideAndDoSomething, std::placeholders::_1, m_Objects, inFunction));
}

bool InstanceMatches(Qt3DSDMInstanceHandle inTarget, Qt3DSDMInstanceHandle inHandle,
                     Qt3DSDMPropertyHandle)
{
    return inTarget == inHandle;
}

bool PropertyMatches(Qt3DSDMPropertyHandle inTarget, Qt3DSDMInstanceHandle,
                     Qt3DSDMPropertyHandle inProp)
{
    return inTarget == inProp;
}

bool InstancePropertyMatchesVector(const TInstanceHandleList &inInstances,
                                   const TPropertyHandleList &inProperties,
                                   Qt3DSDMInstanceHandle slideInst, Qt3DSDMPropertyHandle slideProp)
{
    return std::find(inInstances.begin(), inInstances.end(), slideInst) != inInstances.end()
        && std::find(inProperties.begin(), inProperties.end(), slideProp) != inProperties.end();
}

bool InstancePropertyMatches(const Qt3DSDMInstanceHandle inInstance,
                             const Qt3DSDMPropertyHandle inProperty, Qt3DSDMInstanceHandle slideInst,
                             Qt3DSDMPropertyHandle slideProp)
{
    return inInstance == slideInst && inProperty == slideProp;
}

void CSimpleSlideCore::DeleteAllInstanceEntries(Qt3DSDMInstanceHandle inHandle)
{
    std::function<bool(Qt3DSDMInstanceHandle, Qt3DSDMPropertyHandle)> predicate(
        std::bind(InstanceMatches, inHandle, std::placeholders::_1, std::placeholders::_2));
    ForEachSlide(std::bind(&SSlide::ClearPropertiesIf, std::placeholders::_1, predicate));
}

void CSimpleSlideCore::DeleteAllPropertyEntries(Qt3DSDMPropertyHandle inHandle)
{
    std::function<bool(Qt3DSDMInstanceHandle, Qt3DSDMPropertyHandle)> predicate(
        std::bind(PropertyMatches, inHandle, std::placeholders::_1, std::placeholders::_2));
    ForEachSlide(std::bind(&SSlide::ClearPropertiesIf, std::placeholders::_1, predicate));
}

void CSimpleSlideCore::DeleteAllInstancePropertyEntries(const TInstanceHandleList &inInstances,
                                                        const TPropertyHandleList &inProperties)
{
    std::function<bool(Qt3DSDMInstanceHandle, Qt3DSDMPropertyHandle)> predicate(
        std::bind(InstancePropertyMatchesVector, inInstances, inProperties,
                  std::placeholders::_1, std::placeholders::_2));
    ForEachSlide(std::bind(&SSlide::ClearPropertiesIf, std::placeholders::_1, predicate));
}

void CSimpleSlideCore::ClearChildrenPropertyValues(Qt3DSDMSlideHandle inParent,
                                                   Qt3DSDMInstanceHandle inHandle,
                                                   Qt3DSDMPropertyHandle inProperty)
{
    std::function<bool(Qt3DSDMInstanceHandle, Qt3DSDMPropertyHandle)> predicate(
        std::bind(InstancePropertyMatches, inHandle, inProperty,
                  std::placeholders::_1, std::placeholders::_2));
    ForEachChild(inParent, std::bind(&SSlide::ClearPropertiesIf, std::placeholders::_1, predicate));
}
}