VEvent.cpp

Engine/source/Verve/Core/VEvent.cpp

More...

Public Functions

Detailed Description

Public Functions

IMPLEMENT_CONOBJECT(VEvent )

  1
  2//-----------------------------------------------------------------------------
  3// Verve
  4// Copyright (C) 2014 - Violent Tulip
  5//
  6// Permission is hereby granted, free of charge, to any person obtaining a copy
  7// of this software and associated documentation files (the "Software"), to
  8// deal in the Software without restriction, including without limitation the
  9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 10// sell copies of the Software, and to permit persons to whom the Software is
 11// furnished to do so, subject to the following conditions:
 12//
 13// The above copyright notice and this permission notice shall be included in
 14// all copies or substantial portions of the Software.
 15//
 16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 22// IN THE SOFTWARE.
 23//-----------------------------------------------------------------------------
 24#include "Verve/Core/VEvent.h"
 25#include "Verve/Core/VGroup.h"
 26#include "Verve/Core/VTrack.h"
 27
 28#include "console/consoleTypes.h"
 29#include "math/mMathFn.h"
 30
 31//-----------------------------------------------------------------------------
 32IMPLEMENT_CONOBJECT( VEvent );
 33//-----------------------------------------------------------------------------
 34
 35VEvent::VEvent( void ) : 
 36        mIsPlaying( false ),
 37        mTriggered( false ),
 38        mTriggerTime( 0 ),
 39        mDuration( 0 )
 40{
 41    setLabel( "DefaultEvent" );
 42}
 43
 44void VEvent::initPersistFields( void )
 45{
 46    Parent::initPersistFields();
 47
 48    addProtectedField( "TriggerTime", TypeS32, Offset( mTriggerTime, VEvent ), &setTriggerTime, &defaultProtectedGetFn, "The time that this event is triggered." );
 49    addProtectedField( "Duration",    TypeS32, Offset( mDuration,    VEvent ), &setDuration,    &defaultProtectedGetFn, "The total duration that this event plays for." );
 50}
 51
 52//-----------------------------------------------------------------------------
 53//
 54// Controller Methods.
 55//
 56//-----------------------------------------------------------------------------
 57
 58//-----------------------------------------------------------------------------
 59// 
 60// VEvent::onControllerReset( pTime, pForward );
 61// 
 62// Reset the status of the event. If the given time is between the event's
 63// start and finish times, then the isPlaying flag will be true. This means
 64// that the event is free to be triggered upon playback.
 65// 
 66//-----------------------------------------------------------------------------
 67void VEvent::onControllerReset( const S32 &pTime, const bool &pForward )
 68{
 69    // Reset Status.
 70    mIsPlaying = ( pTime > mTriggerTime && pTime < ( mTriggerTime + mDuration ) );
 71    mTriggered = false;
 72}
 73
 74//-----------------------------------------------------------------------------
 75// 
 76// VEvent::onControllerUpdate( pTime, pDelta )
 77// 
 78// Integrate is only called when this event is the Next Event for the parent
 79// track. For each track, there is only ever *one* event being integrated - the
 80// event that needs to be triggered next.
 81//
 82// If the event has a duration greater than 0, then this event will continue to
 83// integrate until its time is up, or the controller finishes playing
 84// (whichever happens first).
 85// 
 86// If a value of true is returned, then this event will continue to integrate
 87// until a value of false is returned to the parent track. When this happens,
 88// this event ceases to be the track's Next Event and will not continue
 89// updating.
 90// 
 91//-----------------------------------------------------------------------------
 92bool VEvent::onControllerUpdate( const S32 &pTime, const S32 &pDelta )
 93{
 94    if ( !isEnabled() )
 95    {
 96        return false;
 97    }
 98
 99    const S32  newTime    = ( pTime + pDelta );
100    const S32 &startTime  = getStartTime();
101    const S32 &finishTime = getFinishTime();
102
103    if ( !mIsPlaying || !mTriggered )
104    {
105        if ( !mIsPlaying )
106        {
107            if ( ( pDelta > 0 && newTime < startTime )
108                 || ( pDelta < 0 && newTime > startTime ) )
109            {
110                // Not Time to Trigger.
111                return true;
112            }
113
114            if ( ( pDelta > 0 && pTime > startTime )
115                 || ( pDelta < 0 && pTime < startTime ) )
116            {
117                //AssertFatal( false, "VEvent::onControllerUpdate() - Event has been skipped." );
118                return false;
119            }
120        }
121
122        if ( !mTriggered )
123        {
124            // Play and Trigger.
125            mIsPlaying = ( mDuration > 0 );
126            mTriggered = true;
127
128            // Callback.
129            onTrigger( pTime, pDelta );
130
131            if ( mDuration == 0 )
132            {
133                // Stop Integrating.
134                return false;
135            }
136
137            // Return Here.
138            // Note: If Duration is non-zero this event will continue to update
139            //       so that VEvent:: onUpdate is processed for the full event
140            //       duration.
141            return ( mDuration != 0 );
142        }
143    }
144
145    // Complete?
146    const bool isComplete = ( ( pDelta > 0 && newTime > finishTime )
147                               || ( pDelta < 0 && newTime < finishTime ) );
148
149    if ( !isComplete )
150    {
151        // Callback.
152        onUpdate( pTime, pDelta );
153    }
154    else
155    {
156        // Complete.
157        mIsPlaying = false;
158
159        // Callback.
160        onComplete( pTime, pDelta );
161    }
162
163    // Continue?
164    return !isComplete;
165}
166
167//-----------------------------------------------------------------------------
168//
169// Callback Methods.
170//
171//-----------------------------------------------------------------------------
172
173//-----------------------------------------------------------------------------
174// 
175// VEvent::onTrigger( pTime, pDelta );
176// 
177// This method is called when an event is due to be triggered. This method is
178// meant to be overloaded by derived classes.
179// 
180// For examples of what an event might do, please refer to some of the included
181// events with Verve.
182// 
183//-----------------------------------------------------------------------------
184void VEvent::onTrigger( const S32 &pTime, const S32 &pDelta )
185{
186    // Void.
187}
188
189//-----------------------------------------------------------------------------
190// 
191// VEvent::onUpdate( pTime, pDelta );
192// 
193// This method is called each tick once an event has been triggered and ceases
194// to be called when it is completed. This method is meant to be overloaded by
195// derived classes.
196// 
197//-----------------------------------------------------------------------------
198void VEvent::onUpdate( const S32 &pTime, const S32 &pDelta )
199{
200    // Void.
201}
202
203//-----------------------------------------------------------------------------
204// 
205// VEvent::onComplete( pTime, pDelta );
206// 
207// This method is called once an event has finished being updated. It is not
208// called on events that have a duration of 0. This method is meant to be
209// overloaded by derived classes.
210// 
211//-----------------------------------------------------------------------------
212void VEvent::onComplete( const S32 &pTime, const S32 &pDelta )
213{
214    // Void.
215}
216
217//-----------------------------------------------------------------------------
218//
219// Property Methods.
220//
221//-----------------------------------------------------------------------------
222
223//-----------------------------------------------------------------------------
224// 
225// VEvent::getGroup();
226// 
227// Returns the parent group.
228// 
229//-----------------------------------------------------------------------------
230VGroup *VEvent::getGroup( void )
231{
232    VTrack *track = getTrack();
233    if ( track )
234    {
235        return track->getGroup();
236    }
237
238    return NULL;
239}
240
241//-----------------------------------------------------------------------------
242// 
243// VEvent::getTrack();
244// 
245// Returns the parent track.
246// 
247//-----------------------------------------------------------------------------
248VTrack *VEvent::getTrack( void )
249{
250    return dynamic_cast<VTrack*>( mParentNode );
251}
252
253//-----------------------------------------------------------------------------
254// 
255// VEvent::getNextEvent();
256// 
257// Returns the next event.
258// 
259//-----------------------------------------------------------------------------
260VEvent *VEvent::getNextEvent( void )
261{
262    if ( !isControllerPlayingForward() )
263    {
264        return dynamic_cast<VEvent*>( mSiblingPrevNode );
265    }
266
267    return dynamic_cast<VEvent*>( mSiblingNextNode );
268}
269
270//-----------------------------------------------------------------------------
271// 
272// VEvent::getPreviousEvent();
273// 
274// Returns the previous event.
275// 
276//-----------------------------------------------------------------------------
277VEvent *VEvent::getPreviousEvent( void )
278{
279    if ( !isControllerPlayingForward() )
280    {
281        return dynamic_cast<VEvent*>( mSiblingNextNode );
282    }
283
284    return dynamic_cast<VEvent*>( mSiblingPrevNode );
285}
286
287//-----------------------------------------------------------------------------
288// 
289// VEvent::getStartTime();
290// 
291// Returns the time, in milliseconds, that the event is due to trigger.
292// 
293//-----------------------------------------------------------------------------
294S32 VEvent::getStartTime( void )
295{
296    return ( mTriggerTime + ( !isControllerPlayingForward() * mDuration ) );
297}
298
299//-----------------------------------------------------------------------------
300// 
301// VEvent::getFinishTime();
302// 
303// Returns the time, in milliseconds, that the event will cease updating.
304// 
305//-----------------------------------------------------------------------------
306S32 VEvent::getFinishTime( void )
307{
308    return ( mTriggerTime + ( isControllerPlayingForward() * mDuration ) );
309}
310
311//-----------------------------------------------------------------------------
312// 
313// VEvent::setTriggerTime( pTime );
314// 
315// Apply the given trigger time to the object.
316// 
317// If the project was built using the VT_EDITOR preprocessor argument, then
318// the validity of the passed value is verified. It also cannot be changed
319// while the controller is playing.
320// 
321//-----------------------------------------------------------------------------
322void VEvent::setTriggerTime( const S32 &pTime )
323{
324#ifdef VT_EDITOR
325
326    VTrack *track = getTrack();
327    if ( !track )
328    {
329        // Apply Time.
330        mTriggerTime = pTime;
331
332        return;
333    }
334
335    if ( track->isControllerPlaying() )
336    {
337        // Don't Change While Playing.
338        return;
339    }
340
341    /*
342    // Check For Overlap.
343    for ( ITreeNode *node = mChildNode; node != NULL; node = node->mSiblingNextNode )
344    {
345        VEvent *event = ( VEvent* )node;
346        if ( event == this )
347        {
348            // Skip.
349            continue;
350        }
351
352        const U32 startTime  = getStartTime();
353        const U32 finishTime = getFinishTime();
354
355        if ( ( pTime > startTime && pTime < finishTime )
356             || ( ( pTime + mDuration ) > startTime && ( pTime + mDuration ) < finishTime )
357             || ( pTime < startTime && ( pTime + mDuration ) > finishTime ) )
358        {
359            // Overlap!
360            return;
361        }
362    }
363    */
364
365    // Apply Time.
366    mTriggerTime = mClamp( pTime, 0, getControllerDuration() );
367
368    // Sort Events.
369    track->sort();
370
371    // Reset Track.
372    track->onControllerReset( getControllerTime(), isControllerPlayingForward() );
373
374#else
375
376    // Apply Time.
377    mTriggerTime = pTime;
378
379#endif
380}
381
382//-----------------------------------------------------------------------------
383// 
384// VEvent::setDuration( pDuration );
385// 
386// Apply the given duration time to the object.
387// 
388// If the project was built using the VT_EDITOR preprocessor argument, then
389// the validity of the passed value is verified. It also cannot be changed
390// while the controller is playing.
391// 
392//-----------------------------------------------------------------------------
393void VEvent::setDuration( const S32 &pDuration )
394{
395#ifdef VT_EDITOR
396
397    if ( isControllerPlaying() )
398    {
399        // Don't Change While Playing.
400        return;
401    }
402
403#endif
404
405    // Apply Duration.
406    mDuration = pDuration;
407}
408