summaryrefslogtreecommitdiffstats
path: root/src/uipparser/Qt3DSUIPParserActionHelper.cpp
blob: ab6d751d639203d6a428c40d33d9a6e2a160576f (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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/****************************************************************************
**
** 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$
** 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$
**
****************************************************************************/

#include "RuntimePrefix.h"
#ifdef EA_PLATFORM_WINDOWS
#pragma warning(disable : 4396) // specializer warning nonsense
#endif

//==============================================================================
//	Includes
//==============================================================================
#include "Qt3DSUIPParserActionHelper.h"
#include "Qt3DSUIPParserObjectRefHelper.h"
#include "Qt3DSIPresentation.h"
#include "Qt3DSCommandEventTypes.h"

#include "Qt3DSDMPrefix.h"
#include "Qt3DSDMXML.h"
#include "Qt3DSDMWStrOpsImpl.h"
#include "Qt3DSMetadata.h"
#include "Qt3DSEulerAngles.h"
#include "Qt3DSLogicSystem.h"
#include "Qt3DSParametersSystem.h"

using namespace qt3dsdm;

//==============================================================================
//	Namespace
//==============================================================================
namespace Q3DStudio {

//==============================================================================
/**
 * Constructor
 */
CUIPParserActionHelper::CUIPParserActionHelper(CUIPParserImpl *inUIPParser,
                                               CUIPParserObjectRefHelper *inObjRefHelper,
                                               IRuntimeMetaData &inMetaData)
    : m_UIPParser(inUIPParser)
    , m_ObjRefHelper(inObjRefHelper)
    , m_MetaData(inMetaData)
{
}

//==============================================================================
/**
 * Destructor
 */
CUIPParserActionHelper::~CUIPParserActionHelper()
{
}

void CUIPParserActionHelper::CacheAction(qt3dsdm::IDOMReader &inReader,
                                         const char8_t *inActionOwnerId)
{
    if (IsTrivial(inActionOwnerId))
        return;

    IDOMReader::Scope __childScope(inReader);

    const char *theRef;
    if (inReader.Att("ref", theRef)) {
        // Ignore "ref" because we are only interested in new actions
        return;
    }

    // Parse the action
    SActionInfo theAction;
    inReader.Att("id", theAction.m_Id);
    theAction.m_Owner = inActionOwnerId;

    const char *tempStr;
    if (inReader.Att("triggerObject", tempStr))
        theAction.m_TriggerObject = m_UIPParser->ParseObjectRefId(tempStr, inActionOwnerId);
    inReader.Att("event", theAction.m_Event);
    if (inReader.Att("targetObject", tempStr))
        theAction.m_TargetObject = m_UIPParser->ParseObjectRefId(tempStr, inActionOwnerId);
    if (inReader.Att("handler", tempStr)) {
        theAction.m_Handler = tempStr;
        CacheHandlerArguments(inReader, theAction);
    }

    // Only proceeds if we can find the elements
    if (theAction.m_TargetObject.empty() || theAction.m_TriggerObject.empty()
        || theAction.m_Owner.empty()) {
        qCWarning(qt3ds::INVALID_OPERATION) << "Action invalid, id: " << theAction.m_Id.c_str();
        return;
    }

    m_ActionMap[theAction.m_Id] = theAction;

    // Build up logic tables to be processed by BuildAction later
    // There are 2 tables involved:
    // 1. The Listener-To-Events tables - multimap of each listener to all events it is listening to
    // 2. The Listener-Event-To-Actions table - multimap of each listener-event pair to all actions
    // it triggers
    // note: The struct for Action is a pair of EventAction node and its owner
    AddListenerEventPair(theAction.m_TriggerObject, theAction.m_Event);

    SListenerEventNamePair theListenerEventPair(theAction.m_TriggerObject, theAction.m_Event);
    TEventActionOwnerPair theLogicOwnerPair(theAction.m_Id, theAction.m_Owner);
    m_ListenerEventActionsMap.insert(eastl::make_pair(theListenerEventPair, theLogicOwnerPair));
}

void CUIPParserActionHelper::AddListenerEventPair(eastl::string inListener,
                                                  const eastl::string &inEventName)
{
    // Check for dupes
    TListenerEventsNameMap::iterator theStartItr = m_ListenerEventsNameMap.lower_bound(inListener);
    TListenerEventsNameMap::iterator theEndItr = m_ListenerEventsNameMap.upper_bound(inListener);
    for (; theStartItr != theEndItr; ++theStartItr) {
        if (theStartItr->second == inEventName)
            return;
    }

    m_ListenerEventsNameMap.insert(eastl::make_pair(inListener, inEventName));
}

void CUIPParserActionHelper::CacheHandlerArguments(qt3dsdm::IDOMReader &inReader,
                                                   SActionInfo &inAction)
{
    IDOMReader::Scope __handlerArgScope(inReader);
    for (bool success = inReader.MoveToFirstChild(); success;
         success = inReader.MoveToNextSibling()) {
        SHandlerArgumentInfo theArg;
        inReader.Att("name", theArg.m_Name);
        inReader.Att("value", theArg.m_Value);
        inAction.m_HandlerArgs.push_back(theArg);
    }
    if (inAction.m_Handler == "Set Property") {
        SHandlerArgumentInfo theHandlerArg1 = inAction.m_HandlerArgs[0];
        SElementData *targetData = m_UIPParser->GetElementData(inAction.m_TargetObject.c_str());
        if (targetData && !theHandlerArg1.m_Value.empty()) {
            m_UIPParser->m_ParseElementManager.MarkAttributeAsReferenced(
                *targetData, theHandlerArg1.m_Value.c_str());
        }
    }
}

SElement *CUIPParserActionHelper::GetElement(const eastl::string &inElementName)
{
    return m_UIPParser->GetElement(inElementName.c_str());
}

void CUIPParserActionHelper::GetActionSectionCount(
    CUIPParserImpl::SActionSectionCount &outActionCount)
{
    // There are 2 tables involved:
    // 1. The Listener-To-Events tables - multimap of each listener to all events it is listening to
    // 2. The Listener-Event-To-Actions table - multimap of each listener-event pair to all actions
    // it triggers
    // note: The struct for Action is a pair of EventAction node and its owner

    // Get the count to reserve
    // Iterate through each listener
    for (TListenerEventsNameMap::iterator theListenerItr = m_ListenerEventsNameMap.begin();
         theListenerItr != m_ListenerEventsNameMap.end();) {
        ++outActionCount.m_TriggerElementCount;

        INT32 theEventCount =
            static_cast<INT32>(m_ListenerEventsNameMap.count(theListenerItr->first));
        for (INT32 theEventCounter = 0; theEventCounter < theEventCount;
             ++theEventCounter, ++theListenerItr) {
            ++outActionCount.m_TriggerEventCount;

            // iterate through each listener-event pair
            SListenerEventNamePair thePair(theListenerItr->first, theListenerItr->second);
            TListenerEventActionsMap::iterator theActionsItr =
                m_ListenerEventActionsMap.lower_bound(thePair);
            INT32 theActionCount = static_cast<INT32>(m_ListenerEventActionsMap.count(thePair));
            for (INT32 theActionCounter = 0; theActionCounter < theActionCount;
                 ++theActionCounter) {
                eastl::string theActionId = theActionsItr->second.first;
                outActionCount.m_ActionCount +=
                    GetActionCount(theActionId, outActionCount.m_StringAttrCount,
                                   outActionCount.m_CustomParamCount);
                ++theActionsItr;
            }
        }
    }
}

void CUIPParserActionHelper::BuildActions(IPresentation &inPresentation)
{
    // Now we start building
    // Iterate through each listener
    for (TListenerEventsNameMap::iterator theListenerItr = m_ListenerEventsNameMap.begin();
         theListenerItr != m_ListenerEventsNameMap.end();) {
        TElement *theElement = GetElement(theListenerItr->first);

        INT32 theEventCount =
            static_cast<INT32>(m_ListenerEventsNameMap.count(theListenerItr->first));
        for (INT32 theEventCounter = 0; theEventCounter < theEventCount;
             ++theEventCounter, ++theListenerItr) {
            UINT32 theEventName =
                CHash::HashEventCommand(theListenerItr->second.c_str()); // UINT32: HashedName

            // iterate through each listener-event pair
            SListenerEventNamePair thePair(theListenerItr->first, theListenerItr->second);
            TListenerEventActionsMap::iterator theActionsItr =
                m_ListenerEventActionsMap.lower_bound(thePair);
            INT32 theActionCount = static_cast<INT32>(m_ListenerEventActionsMap.count(thePair));
            for (INT32 theActionCounter = 0; theActionCounter < theActionCount;
                 ++theActionCounter) {
                eastl::string theActionId = theActionsItr->second.first;
                if (theElement)
                    BuildAction(*theElement, theEventName, inPresentation, theActionId);
                ++theActionsItr;
            }
        }
    }
}

union UTypeConverter {
    SAttributeKey m_Key;
    INT32 m_IntData;
};

struct SActionAdder
{
    ILogicSystem &m_LogicSystem;
    TElement &m_EventElement;
    UINT32 m_EventName;
    TElement *m_Owner;
    TElement *m_Target;

    SActionAdder(ILogicSystem &inLogicSystem, TElement &inEventElement, UINT32 inEventName,
                 TElement *inOwner, TElement *inTarget)
        : m_LogicSystem(inLogicSystem)
        , m_EventElement(inEventElement)
        , m_EventName(inEventName)
        , m_Owner(inOwner)
        , m_Target(inTarget)
    {
    }

    INT32 AddAction(TEventCommandHash inCommand, UINT32 inArg1 = 0, UINT32 inArg2 = 0)
    {
        UVariant arg1Var;
        UVariant arg2Var;
        arg1Var.m_INT32 = (INT32)inArg1;
        arg2Var.m_INT32 = (INT32)inArg2;
        return m_LogicSystem.AddAction(m_EventElement, m_EventName, m_Target, m_Owner, inCommand,
                                       arg1Var, arg2Var, true);
    }
};

void CUIPParserActionHelper::BuildAction(TElement &inElement, UINT32 inEventName,
                                         IPresentation &inPresentation,
                                         const eastl::string &inActionId)
{
    ILogicSystem &theLogicBuilder = inPresentation.GetLogicSystem();
    IParametersSystem &theParamSystem = inPresentation.GetParametersSystem();
    TActionMap::iterator theIter = m_ActionMap.find(inActionId);
    if (theIter == m_ActionMap.end()) {
        Q3DStudio_ASSERT(false);
        qCWarning(qt3ds::INVALID_OPERATION) << "Can't find action, id: " << inActionId.c_str();
        return;
    }

    SActionInfo theAction = theIter->second;
    INT32 theAddActionIndex = -1; // the index of newly added action. If there are more than 1
                                  // actions, the index points to the firstly added action.
    SActionAdder theAdder(theLogicBuilder, inElement, inEventName, GetElement(theAction.m_Owner),
                          GetElement(theAction.m_TargetObject));

    // The list of applicable handler is loaded from metadata xml file (if it is non-custom handler)
    bool theIsCustomHandler = IsCustomHandler(theAction);

    if (theIsCustomHandler) {
        // Argument 1 - param table start index
        INT32 paramGroup = theParamSystem.CreateParameterGroup();

        // First param is the handler name we want to invoke for the custom action.
        ExportCustomActionParameters(inPresentation, theParamSystem, paramGroup,
                                     theAction.m_Handler.c_str(), ERuntimeDataModelDataTypeString,
                                     ERuntimeAdditionalMetaDataTypeNone);

        int theHandlerArgumentCount = theAction.m_HandlerArgs.size();
        ERuntimeDataModelDataType theType;
        ERuntimeAdditionalMetaDataType theAdditionalType;
        // Actual handler parameters to pass in.
        for (int theIndex = 0; theIndex < theHandlerArgumentCount; ++theIndex) {
            GetHandlerArgumentType(theAction, theIndex, theType, theAdditionalType);
            ExportCustomActionParameters(inPresentation, theParamSystem, paramGroup,
                                         theAction.m_HandlerArgs[theIndex].m_Value.c_str(), theType,
                                         theAdditionalType);
        }
        theAddActionIndex = theAdder.AddAction(COMMAND_CUSTOMACTION, (UINT32)paramGroup);
    } else if (theAction.m_Handler == "Set Property") // Plain set action
    {
        // ArgumentOne - HashPropertyName
        // ArgumentTwo - Value
        SHandlerArgumentInfo theHandlerArg1 = theAction.m_HandlerArgs[0];
        SHandlerArgumentInfo theHandlerArg2 = theAction.m_HandlerArgs[1];

        eastl::string thePropertyName = theHandlerArg1.m_Value;
        SElementData *theTargetObject =
            m_UIPParser->GetElementData(theAction.m_TargetObject.c_str());
        if (theTargetObject) {
            SElementPropertyInfo *theProperty = m_UIPParser->m_ParseElementManager.FindProperty(
                *theTargetObject, thePropertyName.c_str());
            if (theProperty) {
                TPropertyDescAndValueList theProperties;
                ERuntimeDataModelDataType theType = theProperty->m_DataType;
                ERuntimeAdditionalMetaDataType theAdditionalType = theProperty->m_AdditionalType;

                m_UIPParser->GetAttributeList(inPresentation, theProperties, theType,
                                              theAdditionalType, thePropertyName.c_str(),
                                              theHandlerArg2.m_Value.c_str(),
                                              theProperty->m_PropertyNames);

                UINT32 theActionCount = theProperties.size();
                Q3DStudio_ASSERT(theActionCount > 0);
                for (UINT32 theActionIndex = 0; theActionIndex < theActionCount; ++theActionIndex) {
                    INT32 actionId = theAdder.AddAction(
                        COMMAND_SETPROPERTY, theProperties[theActionIndex].first.GetNameHash(),
                        theProperties[theActionIndex].second.m_INT32);
                    if (theActionIndex == 0)
                        theAddActionIndex = actionId;
                }
            }
        }
    } else if (theAction.m_Handler == "Fire Event") // Fire the selected event on the element
    {
        theAddActionIndex = theAdder.AddAction(
            COMMAND_FIREEVENT, CHash::HashEventCommand(theAction.m_HandlerArgs[0].m_Value.c_str()));
    } else if (theAction.m_Handler == "Go to Slide") // Switch slides
    {
        theAddActionIndex = theAdder.AddAction(
            COMMAND_GOTOSLIDENAME, CHash::HashString(theAction.m_HandlerArgs[0].m_Value.c_str()));
    } else if (theAction.m_Handler == "Next Slide") // Switch slides
    {
        theAddActionIndex = theAdder.AddAction(COMMAND_GOTONEXTSLIDE);
    } else if (theAction.m_Handler == "Previous Slide") // Switch slides
    {
        theAddActionIndex = theAdder.AddAction(COMMAND_GOTOPREVIOUSSLIDE);
    } else if (theAction.m_Handler == "Preceding Slide") // Switch slides
    {
        theAddActionIndex = theAdder.AddAction(COMMAND_BACKSLIDE);
    } else if (theAction.m_Handler == "Play") // Play
    {
        theAddActionIndex = theAdder.AddAction(COMMAND_PLAY);
    } else if (theAction.m_Handler == "Pause") // Pause
    {
        theAddActionIndex = theAdder.AddAction(COMMAND_PAUSE);
    } else if (theAction.m_Handler == "Go to Time") // Goto Time
    {
        float theTime = 0;
        const char *theValue = theAction.m_HandlerArgs[0].m_Value.c_str();
        if (!IsTrivial(theValue))
            WStrOps<float>().StrTo(theValue, theTime);

        theAddActionIndex =
            theAdder.AddAction(COMMAND_GOTOTIME, static_cast<UINT32>(theTime * 1000.0f));

        bool thePause = false;
        theValue = theAction.m_HandlerArgs[1].m_Value.c_str();
        if (!IsTrivial(theValue))
            WStrOps<bool>().StrTo(theValue, thePause);
        theAdder.AddAction(thePause ? COMMAND_PAUSE : COMMAND_PLAY);
    } else if (theAction.m_Handler == "Emit Signal") // Emit a signal
    {
        qt3ds::foundation::CStringHandle theStringHandle;
        if (!theAction.m_HandlerArgs[0].m_Value.empty()) {
            const char *theValue = theAction.m_HandlerArgs[0].m_Value.c_str();
            theStringHandle = inPresentation.GetStringTable().GetHandle(theValue);
        }
        theAddActionIndex = theAdder.AddAction(COMMAND_EMITSIGNAL, theStringHandle.handle());
    } else {
        Q3DStudio_ASSERT(false); // what sort of crazy action is this?
    }

    if (theAddActionIndex > -1)
        m_ActionIdentifierMap[theAction.m_Id] = theAddActionIndex;
}

INT32 CUIPParserActionHelper::GetActionCount(const eastl::string &inActionId)
{
    INT32 theStringAttrCount = 0;
    INT32 theCustomParamCount = 0;
    return GetActionCount(inActionId, theStringAttrCount, theCustomParamCount);
}

INT32 CUIPParserActionHelper::GetActionCount(const eastl::string &inActionId,
                                             INT32 &outStringAttrCount, INT32 &outCustomParamCount)
{
    INT32 theActionCount = 0;

    TActionMap::iterator theIter = m_ActionMap.find(inActionId);
    if (theIter == m_ActionMap.end()) {
        Q3DStudio_ASSERT(false);
        qCWarning(qt3ds::INVALID_OPERATION) << "Can't find action, id: " << inActionId.c_str();
        return theActionCount;
    }

    SActionInfo theAction = theIter->second;

    // The list of applicable handler is loaded from metadata xml file (if it is non-custom handler)
    bool theIsCustomHandler = IsCustomHandler(theAction);

    if (theIsCustomHandler) {
        theActionCount = 1;

        // First param is the handler name we want to invoke for the custom action.
        ++outStringAttrCount;
        ++outCustomParamCount;

        int theHandlerArgumentCount = theAction.m_HandlerArgs.size();
        ERuntimeDataModelDataType theType;
        ERuntimeAdditionalMetaDataType theAdditionalType;
        // Actual handler parameters to pass in.
        for (int theIndex = 0; theIndex < theHandlerArgumentCount; ++theIndex) {
            GetHandlerArgumentType(theAction, theIndex, theType, theAdditionalType);
            GetCustomActionParametersCount(theType, theAdditionalType, outStringAttrCount,
                                           outCustomParamCount);
        }
    }

    else if (theAction.m_Handler == "Set Property") // Plain set action
    {
        SHandlerArgumentInfo theHandlerArg1 = theAction.m_HandlerArgs[0];

        eastl::string thePropertyName = theHandlerArg1.m_Value;
        SElementData *theData = m_UIPParser->GetElementData(theAction.m_TargetObject.c_str());
        if (theData) {
            SElementPropertyInfo *thePropertyInfo =
                m_UIPParser->m_ParseElementManager.GetOrCreateProperty(*theData,
                                                                       thePropertyName.c_str());
            if (thePropertyInfo) {
                theActionCount = thePropertyInfo->m_Arity;
                if (m_UIPParser->IsStringType(thePropertyInfo->m_DataType))
                    ++outStringAttrCount;
            }
        }
    }

    else if (theAction.m_Handler == "Fire Event" // Fire the selected event on the element
             || theAction.m_Handler == "Go to Slide" // Switch slides
             || theAction.m_Handler == "Next Slide" // Switch slides
             || theAction.m_Handler == "Previous Slide" // Switch slides
             || theAction.m_Handler == "Preceding Slide" // Switch slides
             || theAction.m_Handler == "Play" // Play
             || theAction.m_Handler == "Pause" // Pause
             ) {
        theActionCount = 1;
    }

    else if (theAction.m_Handler == "Go to Time") // Goto Time
    {
        theActionCount = 2;
    } else if (theAction.m_Handler == "Play Sound") // Play Sound
    {
        theActionCount = 1;
        ++outStringAttrCount;
    } else if (theAction.m_Handler == "Emit Signal") // Emit Signal
    {
        theActionCount = 1;
        ++outStringAttrCount;
    } else {
        Q3DStudio_ASSERT(false); // what sort of crazy action is this?
    }

    return theActionCount;
}

INT32 CUIPParserActionHelper::GetActionIndex(const eastl::string &inActionId)
{
    TActionIdentifierMap::iterator theFind = m_ActionIdentifierMap.find(inActionId);
    if (theFind != m_ActionIdentifierMap.end())
        return theFind->second;
    Q3DStudio_ASSERT(false);
    return -1;
}

bool CUIPParserActionHelper::IsCustomHandler(const SActionInfo &inAction)
{
    const char *theClass = m_ObjRefHelper->GetClass(inAction.m_TargetObject);
    if (!IsTrivial(theClass))
        return m_MetaData.IsCustomHandler(theClass, inAction.m_Handler.c_str());
    return false;
}

void CUIPParserActionHelper::GetHandlerArgumentType(
    const SActionInfo &inAction, int inArgumentIndex, ERuntimeDataModelDataType &outType,
    ERuntimeAdditionalMetaDataType &outAdditionalType)
{
    const char *theClass = m_ObjRefHelper->GetClass(inAction.m_TargetObject);
    return m_MetaData.GetHandlerArgumentType(theClass, inAction.m_Handler.c_str(),
                                             inAction.m_HandlerArgs[inArgumentIndex].m_Name.c_str(),
                                             outType, outAdditionalType);
}

void CUIPParserActionHelper::GetCustomActionParametersCount(
    ERuntimeDataModelDataType inDataType, ERuntimeAdditionalMetaDataType inAdditionalType,
    INT32 &outStringAttrCount, INT32 &outCustomParamCount)
{
    // This function should match ExportCustomActionParameters
    switch (inDataType) {
    case ERuntimeDataModelDataTypeFloat:
    case ERuntimeDataModelDataTypeLong:
    case ERuntimeDataModelDataTypeBool: {
        outCustomParamCount += 1;
        break;
    }
    case ERuntimeDataModelDataTypeFloat2: {
        outCustomParamCount += 2;
        break;
    }
    case ERuntimeDataModelDataTypeFloat3: {
        if (inAdditionalType == ERuntimeAdditionalMetaDataTypeColor) {
            // Append rgba
            outCustomParamCount += 4;
        } else {
            // Append xyz
            outCustomParamCount += 3;
        }
        break;
    }
    // default to string
    default: {
        outCustomParamCount += 1;
        outStringAttrCount += 1;
    }
    }
}

namespace {

    using qt3ds::foundation::IStringTable;

    struct SCustomParameterParseInfoFloat
    {
        typedef float TParseType;
        static const char8_t *Name() { return "float"; }
        static TParseType Default() { return 0; }
        static Q3DStudio::UVariant ToVariant(const TParseType &inParseType)
        {
            Q3DStudio::UVariant retval;
            retval.m_FLOAT = inParseType;
            return retval;
        }
    };

    struct SCustomParameterParseInfoLong
    {
        typedef QT3DSI32 TParseType;
        static const char8_t *Name() { return "long"; }
        static TParseType Default() { return 0; }
        static Q3DStudio::UVariant ToVariant(const TParseType &inParseType)
        {
            Q3DStudio::UVariant retval;
            retval.m_FLOAT = static_cast<float>(inParseType);
            return retval;
        }
    };

    struct SCustomParameterParseInfoBool
    {
        typedef bool TParseType;
        static const char8_t *Name() { return "bool"; }
        static TParseType Default() { return false; }
        static Q3DStudio::UVariant ToVariant(const TParseType &inParseType)
        {
            Q3DStudio::UVariant retval;
            retval.m_FLOAT = static_cast<float>(inParseType);
            return retval;
        }
    };

    struct SCustomParameterParseInfoFloat2
    {
        typedef SFloat2 TParseType;
        static const char8_t *Name() { return "float2"; }
        static TParseType Default() { return SFloat2(0, 0); }
        static QT3DSU32 NumParameters() { return 2; }
        static Q3DStudio::UVariant ToVariant(QT3DSU32 inIdx, const TParseType &inParseType)
        {
            Q3DStudio::UVariant retval;
            retval.m_FLOAT = static_cast<float>(inParseType[inIdx]);
            return retval;
        }
    };

    struct SCustomParameterParseInfoFloat3
    {
        typedef SFloat3 TParseType;
        static const char8_t *Name() { return "float3"; }
        static const char8_t **Extensions()
        {
            static const char8_t *retval[] = { ".x", ".y", ".z", NULL };
            return retval;
        }
        static TParseType Default() { return SFloat3(0, 0, 0); }
        static QT3DSU32 NumParameters() { return 3; }
        static Q3DStudio::UVariant ToVariant(QT3DSU32 inIdx, const TParseType &inParseType)
        {
            Q3DStudio::UVariant retval;
            retval.m_FLOAT = static_cast<float>(inParseType[inIdx]);
            return retval;
        }
    };

    struct SCustomParameterParseInfoColor
    {
        typedef SFloat3 TParseType;
        static const char8_t *Name() { return "color4"; }
        static TParseType Default() { return SFloat3(0, 0, 0); }
        static QT3DSU32 NumParameters() { return 4; }
        static Q3DStudio::UVariant ToVariant(QT3DSU32 inIdx, const TParseType &inParseType)
        {
            if (inIdx < 3) {
                Q3DStudio::UVariant retval;
                retval.m_FLOAT = static_cast<float>(inParseType[inIdx]) * 255.0f;
                return retval;
            } else {
                Q3DStudio::UVariant retval;
                retval.m_FLOAT = 255.0f;
                return retval;
            }
        }
    };

    template <typename TParseInfo>
    struct ParseParameter
    {
        void operator()(WCharTReader &inReader, const char *inValue, IParametersSystem &inSystem,
                        QT3DSI32 inGroupId)
        {
            typename TParseInfo::TParseType theValue = TParseInfo::Default();
            if (!IsTrivial(inValue))
                inReader.Read(theValue);
            Q3DStudio::UVariant theVarValue = TParseInfo::ToVariant(theValue);
            inSystem.AddParameter(inGroupId, CHash::HashString(TParseInfo::Name()), theVarValue);
        }
    };

    template <typename TParseInfo>
    struct ParseAggregateParameter
    {
        void operator()(WCharTReader &inReader, const char *inValue, IParametersSystem &inSystem,
                        QT3DSI32 inGroupId)
        {
            typename TParseInfo::TParseType theValue = TParseInfo::Default();
            if (!IsTrivial(inValue))
                inReader.ReadRef(NVDataRef<float>(&theValue[0], TParseInfo::NumParameters()));

            for (QT3DSU32 idx = 0, end = TParseInfo::NumParameters(); idx < end; ++idx) {
                Q3DStudio::UVariant theVarValue = TParseInfo::ToVariant(idx, theValue);
                inSystem.AddParameter(inGroupId, CHash::HashString(TParseInfo::Name()),
                                      theVarValue);
            }
        }
    };
}

void CUIPParserActionHelper::ExportCustomActionParameters(
    IPresentation &inPresentation, IParametersSystem &inParamSystem, QT3DSI32 inParamGroupId,
    const char *inValue, ERuntimeDataModelDataType inDataType,
    ERuntimeAdditionalMetaDataType inAdditionalType)
{
    // Create a destructible value
    m_UIPParser->m_ValueBuffer.clear();
    m_UIPParser->m_ValueBuffer.write(inValue, (QT3DSU32)strlen(inValue) + 1);
    // Clear the destination buffer
    m_UIPParser->m_TempBuffer.clear();
    WCharTReader theReader((char8_t *)m_UIPParser->m_ValueBuffer.begin(), m_UIPParser->m_TempBuffer,
                           *m_UIPParser->GetDOMReader().GetStringTable());

    // The function should match CUIPParserActionHelper::GetCustomActionParametersCount
    switch (inDataType) {
    case ERuntimeDataModelDataTypeFloat: {
        ParseParameter<SCustomParameterParseInfoFloat>()(theReader, inValue, inParamSystem,
                                                         inParamGroupId);
        break;
    }
    case ERuntimeDataModelDataTypeLong: {
        ParseParameter<SCustomParameterParseInfoLong>()(theReader, inValue, inParamSystem,
                                                        inParamGroupId);
        break;
    }
    case ERuntimeDataModelDataTypeBool: {
        ParseParameter<SCustomParameterParseInfoBool>()(theReader, inValue, inParamSystem,
                                                        inParamGroupId);
        break;
    }
    case ERuntimeDataModelDataTypeFloat2: {
        ParseAggregateParameter<SCustomParameterParseInfoFloat2>()(theReader, inValue,
                                                                   inParamSystem, inParamGroupId);
        break;
    }
    case ERuntimeDataModelDataTypeFloat3: {
        if (inAdditionalType == ERuntimeAdditionalMetaDataTypeColor) {
            ParseAggregateParameter<SCustomParameterParseInfoColor>()(
                theReader, inValue, inParamSystem, inParamGroupId);
        } else {
            ParseAggregateParameter<SCustomParameterParseInfoFloat3>()(
                theReader, inValue, inParamSystem, inParamGroupId);
        }
        break;
    }
    // default to string
    default: {
        UVariant theValue;
        theValue.m_StringHandle = inPresentation.GetStringTable().GetHandle(inValue);
        inParamSystem.AddParameter(inParamGroupId, CHash::HashString("string"), theValue);
        break;
    }
    }
}
}