os/graphics/m3g/m3gcore11/src/m3g_animationcontroller.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: AnimationController implementation
    15 *
    16 */
    17 
    18 
    19 /*!
    20  * \internal
    21  * \file
    22  * \brief AnimationController implementation
    23  */
    24 
    25 #ifndef M3G_CORE_INCLUDE
    26 #   error included by m3g_core.c; do not compile separately.
    27 #endif
    28 
    29 #include "m3g_animationcontroller.h"
    30 #include "m3g_memory.h"
    31 
    32 
    33 /*----------------------------------------------------------------------
    34  * Internal functions
    35  *--------------------------------------------------------------------*/
    36 
    37 /*!
    38  * \internal
    39  * \brief Destroys this AnimationController object.
    40  *
    41  * \param obj AnimationController object
    42  */
    43 static void m3gDestroyAnimationController(Object *obj)
    44 {
    45     AnimationController *animController = (AnimationController *) obj;
    46     M3G_VALIDATE_OBJECT(animController);
    47 
    48     m3gDestroyObject(&animController->object);
    49 }
    50 
    51 /*!
    52  * \internal
    53  * \brief Check if controller is active.
    54  *
    55  * \param controller AnimationController object
    56  * \param worldTime current world time
    57  * \retval M3G_TRUE controller active
    58  * \retval M3G_FALSE controller not active
    59  */
    60 static M3Gbool m3gIsActive(const AnimationController *controller,
    61                         M3Gint worldTime)
    62 {
    63     if (controller->activationTime == controller->deactivationTime) {
    64         return M3G_TRUE;
    65     }
    66     return (worldTime >= controller->activationTime &&
    67             worldTime < controller->deactivationTime);
    68 }
    69 
    70 /*!
    71  * \internal
    72  * \brief Gets time to controller activation.
    73  *
    74  * \param controller AnimationController object
    75  * \param worldTime current world time
    76  * \return time to controller activation
    77  */
    78 static M3Gint m3gTimeToActivation(const AnimationController *controller,
    79                                M3Gint worldTime)
    80 {
    81     if (worldTime < controller->activationTime) {
    82         return (controller->activationTime - worldTime);
    83     }
    84     else if (worldTime < controller->deactivationTime) {
    85         return 0;
    86     }
    87     return 0x7FFFFFFF;
    88 }
    89 
    90 /*!
    91  * \internal
    92  * \brief Gets time to controller deactivation.
    93  *
    94  * \param controller AnimationController object
    95  * \param worldTime current world time
    96  * \return time to controller deactivation
    97  */
    98 static M3Gint m3gTimeToDeactivation(const AnimationController *controller,
    99                                  M3Gint worldTime)
   100 {
   101     if (worldTime < controller->deactivationTime) {
   102         return (controller->deactivationTime - worldTime);
   103     }
   104     return 0x7FFFFFFF;
   105 }
   106 
   107 /*!
   108  * \internal
   109  * \brief Overloaded Object3D method.
   110  *
   111  * \param originalObj original AnimationController object
   112  * \param cloneObj pointer to cloned AnimationController object
   113  * \param pairs array for all object-duplicate pairs
   114  * \param numPairs number of pairs
   115  */
   116 static M3Gbool m3gAnimationControllerDuplicate(const Object *originalObj,
   117                                                Object **cloneObj,
   118                                                Object **pairs,
   119                                                M3Gint *numPairs)
   120 {
   121     const AnimationController *original = (AnimationController *)originalObj;
   122     AnimationController *clone =
   123         m3gCreateAnimationController(originalObj->interface);
   124     *cloneObj = (Object *)clone;
   125     if (*cloneObj == NULL) {
   126         return M3G_FALSE;
   127     }
   128 
   129     if (m3gObjectDuplicate(originalObj, cloneObj, pairs, numPairs)) {
   130         clone->activationTime = original->activationTime;
   131         clone->deactivationTime = original->deactivationTime;
   132         clone->weight = original->weight;
   133         clone->speed = original->speed;
   134         clone->refWorldTime = original->refWorldTime;
   135         clone->refSequenceTime = original->refSequenceTime;
   136         return M3G_TRUE;
   137     }
   138     else {
   139         return M3G_FALSE;
   140     }
   141 }
   142 
   143 /*----------------------------------------------------------------------
   144  * Virtual function table
   145  *--------------------------------------------------------------------*/
   146 
   147 static const ObjectVFTable m3gvf_AnimationController = {
   148     m3gObjectApplyAnimation,
   149     m3gObjectIsCompatible,
   150     m3gObjectUpdateProperty,
   151     m3gObjectDoGetReferences,
   152     m3gObjectFindID,
   153     m3gAnimationControllerDuplicate,
   154     m3gDestroyAnimationController
   155 };
   156 
   157 
   158 /*----------------------------------------------------------------------
   159  * Public API functions
   160  *--------------------------------------------------------------------*/
   161 
   162 /*!
   163  * \brief Creates a new AnimationController with default values
   164  *
   165  * \param hInterface    M3G interface
   166  * \retval AnimationController new AnimationController object
   167  * \retval NULL AnimationController creating failed
   168  */
   169 M3G_API M3GAnimationController m3gCreateAnimationController(
   170     M3GInterface hInterface)
   171 {
   172     Interface *m3g = (Interface *) hInterface;
   173     M3G_VALIDATE_INTERFACE(m3g);
   174     {
   175         AnimationController *controller = m3gAllocZ(m3g, sizeof(AnimationController));
   176 
   177         if (controller != NULL) {
   178             m3gInitObject(&controller->object, m3g,
   179                           M3G_CLASS_ANIMATION_CONTROLLER);
   180             controller->weight = 1;
   181             controller->speed = 1.0f;
   182         }
   183 
   184         return (M3GAnimationController) controller;
   185     }
   186 }
   187 
   188 /*!
   189  * \brief Set active interval.
   190  *
   191  * \param hController AnimationController object
   192  * \param worldTimeMin active interval start
   193  * \param worldTimeMax active interval end
   194  */
   195 M3G_API void m3gSetActiveInterval(M3GAnimationController hController,
   196                                   M3Gint worldTimeMin,
   197                                   M3Gint worldTimeMax)
   198 {
   199     AnimationController *controller = (AnimationController *) hController;
   200     M3G_VALIDATE_OBJECT(controller);
   201 
   202     if (worldTimeMin > worldTimeMax) {
   203         m3gRaiseError(M3G_INTERFACE(controller), M3G_INVALID_VALUE);
   204         return;
   205     }
   206     controller->activationTime   = worldTimeMin;
   207     controller->deactivationTime = worldTimeMax;
   208 }
   209 
   210 /*!
   211  * \brief Get active interval start.
   212  *
   213  * \param hController AnimationController object
   214  * \return active interval start
   215  */
   216 M3G_API M3Gint m3gGetActiveIntervalStart(M3GAnimationController hController)
   217 {
   218     AnimationController *controller = (AnimationController *) hController;
   219     M3G_VALIDATE_OBJECT(controller);
   220 
   221     return controller->activationTime;
   222 }
   223 
   224 /*!
   225  * \brief Get active interval end.
   226  *
   227  * \param hController AnimationController object
   228  * \return active interval end
   229  */
   230 M3G_API M3Gint m3gGetActiveIntervalEnd(M3GAnimationController hController)
   231 {
   232     AnimationController *controller = (AnimationController *) hController;
   233     M3G_VALIDATE_OBJECT(controller);
   234 
   235     return controller->deactivationTime;
   236 }
   237 
   238 /*!
   239  * \brief Set speed.
   240  *
   241  * \param hController   AnimationController object
   242  * \param factor        speed factor
   243  * \param worldTime     reference world time
   244  */
   245 M3G_API void m3gSetSpeed(M3GAnimationController hController,
   246                          M3Gfloat factor, M3Gint worldTime)
   247 {
   248     AnimationController *controller = (AnimationController *) hController;
   249     M3G_VALIDATE_OBJECT(controller);
   250 
   251     controller->refSequenceTime = m3gGetPosition(controller, worldTime);
   252     controller->refWorldTime = worldTime;
   253     controller->speed = factor;
   254 }
   255 
   256 /*!
   257  * \brief Get speed.
   258  *
   259  * \param hController   AnimationController object
   260  * \return              speed factor
   261  */
   262 M3G_API M3Gfloat m3gGetSpeed(M3GAnimationController hController)
   263 {
   264     AnimationController *controller = (AnimationController *) hController;
   265     M3G_VALIDATE_OBJECT(controller);
   266 
   267     return controller->speed;
   268 }
   269 
   270 /*!
   271  * \brief Set position.
   272  *
   273  * \param hController   AnimationController object
   274  * \param sequenceTime  sequence time
   275  * \param worldTime     world time
   276  */
   277 M3G_API void m3gSetPosition(M3GAnimationController hController,
   278                             M3Gfloat sequenceTime, M3Gint worldTime)
   279 {
   280     AnimationController *controller = (AnimationController *) hController;
   281     M3G_VALIDATE_OBJECT(controller);
   282 
   283     controller->refWorldTime = worldTime;
   284     controller->refSequenceTime = sequenceTime;
   285 }
   286 
   287 /*!
   288  * \brief Get position.
   289  *
   290  * \param hController   AnimationController object
   291  * \param worldTime     current world time
   292  * \retrun              position
   293  */
   294 M3G_API M3Gfloat m3gGetPosition(M3GAnimationController hController, M3Gint worldTime)
   295 {
   296     AnimationController *controller = (AnimationController *) hController;
   297     M3G_VALIDATE_OBJECT(controller);
   298 
   299     return m3gAdd(controller->refSequenceTime,
   300                   m3gMul(controller->speed, (M3Gfloat) worldTime - controller->refWorldTime));
   301 }
   302 
   303 
   304 /*!
   305  * \brief Set controller weight.
   306  *
   307  * \param hController   AnimationController object
   308  * \param weight        controller weight
   309  */
   310 M3G_API void m3gSetWeight(M3GAnimationController hController, M3Gfloat weight)
   311 {
   312     AnimationController *controller = (AnimationController *) hController;
   313     M3G_VALIDATE_OBJECT(controller);
   314 
   315     if (weight < 0) {
   316         m3gRaiseError(M3G_INTERFACE(controller), M3G_INVALID_VALUE);
   317         return;
   318     }
   319     controller->weight = weight;
   320 }
   321 
   322 /*!
   323  * \brief Get controller weight.
   324  *
   325  * \param hController   AnimationController object
   326  * \return              controller weight
   327  */
   328 M3G_API M3Gfloat m3gGetWeight(M3GAnimationController hController)
   329 {
   330     AnimationController *controller = (AnimationController *) hController;
   331     M3G_VALIDATE_OBJECT(controller);
   332 
   333     return controller->weight;
   334 }
   335 
   336 /*!
   337  * \brief Get reference world time.
   338  *
   339  * \param hController   AnimationController object
   340  * \return              reference world time
   341  */
   342 M3G_API M3Gint m3gGetRefWorldTime(M3GAnimationController hController)
   343 {
   344     AnimationController *controller = (AnimationController *) hController;
   345     M3G_VALIDATE_OBJECT(controller);
   346 
   347     return controller->refWorldTime;
   348 }