os/graphics/m3g/m3gcore11/src/m3g_animationcontroller.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/m3g/m3gcore11/src/m3g_animationcontroller.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,348 @@
     1.4 +/*
     1.5 +* Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: AnimationController implementation
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +/*!
    1.23 + * \internal
    1.24 + * \file
    1.25 + * \brief AnimationController implementation
    1.26 + */
    1.27 +
    1.28 +#ifndef M3G_CORE_INCLUDE
    1.29 +#   error included by m3g_core.c; do not compile separately.
    1.30 +#endif
    1.31 +
    1.32 +#include "m3g_animationcontroller.h"
    1.33 +#include "m3g_memory.h"
    1.34 +
    1.35 +
    1.36 +/*----------------------------------------------------------------------
    1.37 + * Internal functions
    1.38 + *--------------------------------------------------------------------*/
    1.39 +
    1.40 +/*!
    1.41 + * \internal
    1.42 + * \brief Destroys this AnimationController object.
    1.43 + *
    1.44 + * \param obj AnimationController object
    1.45 + */
    1.46 +static void m3gDestroyAnimationController(Object *obj)
    1.47 +{
    1.48 +    AnimationController *animController = (AnimationController *) obj;
    1.49 +    M3G_VALIDATE_OBJECT(animController);
    1.50 +
    1.51 +    m3gDestroyObject(&animController->object);
    1.52 +}
    1.53 +
    1.54 +/*!
    1.55 + * \internal
    1.56 + * \brief Check if controller is active.
    1.57 + *
    1.58 + * \param controller AnimationController object
    1.59 + * \param worldTime current world time
    1.60 + * \retval M3G_TRUE controller active
    1.61 + * \retval M3G_FALSE controller not active
    1.62 + */
    1.63 +static M3Gbool m3gIsActive(const AnimationController *controller,
    1.64 +                        M3Gint worldTime)
    1.65 +{
    1.66 +    if (controller->activationTime == controller->deactivationTime) {
    1.67 +        return M3G_TRUE;
    1.68 +    }
    1.69 +    return (worldTime >= controller->activationTime &&
    1.70 +            worldTime < controller->deactivationTime);
    1.71 +}
    1.72 +
    1.73 +/*!
    1.74 + * \internal
    1.75 + * \brief Gets time to controller activation.
    1.76 + *
    1.77 + * \param controller AnimationController object
    1.78 + * \param worldTime current world time
    1.79 + * \return time to controller activation
    1.80 + */
    1.81 +static M3Gint m3gTimeToActivation(const AnimationController *controller,
    1.82 +                               M3Gint worldTime)
    1.83 +{
    1.84 +    if (worldTime < controller->activationTime) {
    1.85 +        return (controller->activationTime - worldTime);
    1.86 +    }
    1.87 +    else if (worldTime < controller->deactivationTime) {
    1.88 +        return 0;
    1.89 +    }
    1.90 +    return 0x7FFFFFFF;
    1.91 +}
    1.92 +
    1.93 +/*!
    1.94 + * \internal
    1.95 + * \brief Gets time to controller deactivation.
    1.96 + *
    1.97 + * \param controller AnimationController object
    1.98 + * \param worldTime current world time
    1.99 + * \return time to controller deactivation
   1.100 + */
   1.101 +static M3Gint m3gTimeToDeactivation(const AnimationController *controller,
   1.102 +                                 M3Gint worldTime)
   1.103 +{
   1.104 +    if (worldTime < controller->deactivationTime) {
   1.105 +        return (controller->deactivationTime - worldTime);
   1.106 +    }
   1.107 +    return 0x7FFFFFFF;
   1.108 +}
   1.109 +
   1.110 +/*!
   1.111 + * \internal
   1.112 + * \brief Overloaded Object3D method.
   1.113 + *
   1.114 + * \param originalObj original AnimationController object
   1.115 + * \param cloneObj pointer to cloned AnimationController object
   1.116 + * \param pairs array for all object-duplicate pairs
   1.117 + * \param numPairs number of pairs
   1.118 + */
   1.119 +static M3Gbool m3gAnimationControllerDuplicate(const Object *originalObj,
   1.120 +                                               Object **cloneObj,
   1.121 +                                               Object **pairs,
   1.122 +                                               M3Gint *numPairs)
   1.123 +{
   1.124 +    const AnimationController *original = (AnimationController *)originalObj;
   1.125 +    AnimationController *clone =
   1.126 +        m3gCreateAnimationController(originalObj->interface);
   1.127 +    *cloneObj = (Object *)clone;
   1.128 +    if (*cloneObj == NULL) {
   1.129 +        return M3G_FALSE;
   1.130 +    }
   1.131 +
   1.132 +    if (m3gObjectDuplicate(originalObj, cloneObj, pairs, numPairs)) {
   1.133 +        clone->activationTime = original->activationTime;
   1.134 +        clone->deactivationTime = original->deactivationTime;
   1.135 +        clone->weight = original->weight;
   1.136 +        clone->speed = original->speed;
   1.137 +        clone->refWorldTime = original->refWorldTime;
   1.138 +        clone->refSequenceTime = original->refSequenceTime;
   1.139 +        return M3G_TRUE;
   1.140 +    }
   1.141 +    else {
   1.142 +        return M3G_FALSE;
   1.143 +    }
   1.144 +}
   1.145 +
   1.146 +/*----------------------------------------------------------------------
   1.147 + * Virtual function table
   1.148 + *--------------------------------------------------------------------*/
   1.149 +
   1.150 +static const ObjectVFTable m3gvf_AnimationController = {
   1.151 +    m3gObjectApplyAnimation,
   1.152 +    m3gObjectIsCompatible,
   1.153 +    m3gObjectUpdateProperty,
   1.154 +    m3gObjectDoGetReferences,
   1.155 +    m3gObjectFindID,
   1.156 +    m3gAnimationControllerDuplicate,
   1.157 +    m3gDestroyAnimationController
   1.158 +};
   1.159 +
   1.160 +
   1.161 +/*----------------------------------------------------------------------
   1.162 + * Public API functions
   1.163 + *--------------------------------------------------------------------*/
   1.164 +
   1.165 +/*!
   1.166 + * \brief Creates a new AnimationController with default values
   1.167 + *
   1.168 + * \param hInterface    M3G interface
   1.169 + * \retval AnimationController new AnimationController object
   1.170 + * \retval NULL AnimationController creating failed
   1.171 + */
   1.172 +M3G_API M3GAnimationController m3gCreateAnimationController(
   1.173 +    M3GInterface hInterface)
   1.174 +{
   1.175 +    Interface *m3g = (Interface *) hInterface;
   1.176 +    M3G_VALIDATE_INTERFACE(m3g);
   1.177 +    {
   1.178 +        AnimationController *controller = m3gAllocZ(m3g, sizeof(AnimationController));
   1.179 +
   1.180 +        if (controller != NULL) {
   1.181 +            m3gInitObject(&controller->object, m3g,
   1.182 +                          M3G_CLASS_ANIMATION_CONTROLLER);
   1.183 +            controller->weight = 1;
   1.184 +            controller->speed = 1.0f;
   1.185 +        }
   1.186 +
   1.187 +        return (M3GAnimationController) controller;
   1.188 +    }
   1.189 +}
   1.190 +
   1.191 +/*!
   1.192 + * \brief Set active interval.
   1.193 + *
   1.194 + * \param hController AnimationController object
   1.195 + * \param worldTimeMin active interval start
   1.196 + * \param worldTimeMax active interval end
   1.197 + */
   1.198 +M3G_API void m3gSetActiveInterval(M3GAnimationController hController,
   1.199 +                                  M3Gint worldTimeMin,
   1.200 +                                  M3Gint worldTimeMax)
   1.201 +{
   1.202 +    AnimationController *controller = (AnimationController *) hController;
   1.203 +    M3G_VALIDATE_OBJECT(controller);
   1.204 +
   1.205 +    if (worldTimeMin > worldTimeMax) {
   1.206 +        m3gRaiseError(M3G_INTERFACE(controller), M3G_INVALID_VALUE);
   1.207 +        return;
   1.208 +    }
   1.209 +    controller->activationTime   = worldTimeMin;
   1.210 +    controller->deactivationTime = worldTimeMax;
   1.211 +}
   1.212 +
   1.213 +/*!
   1.214 + * \brief Get active interval start.
   1.215 + *
   1.216 + * \param hController AnimationController object
   1.217 + * \return active interval start
   1.218 + */
   1.219 +M3G_API M3Gint m3gGetActiveIntervalStart(M3GAnimationController hController)
   1.220 +{
   1.221 +    AnimationController *controller = (AnimationController *) hController;
   1.222 +    M3G_VALIDATE_OBJECT(controller);
   1.223 +
   1.224 +    return controller->activationTime;
   1.225 +}
   1.226 +
   1.227 +/*!
   1.228 + * \brief Get active interval end.
   1.229 + *
   1.230 + * \param hController AnimationController object
   1.231 + * \return active interval end
   1.232 + */
   1.233 +M3G_API M3Gint m3gGetActiveIntervalEnd(M3GAnimationController hController)
   1.234 +{
   1.235 +    AnimationController *controller = (AnimationController *) hController;
   1.236 +    M3G_VALIDATE_OBJECT(controller);
   1.237 +
   1.238 +    return controller->deactivationTime;
   1.239 +}
   1.240 +
   1.241 +/*!
   1.242 + * \brief Set speed.
   1.243 + *
   1.244 + * \param hController   AnimationController object
   1.245 + * \param factor        speed factor
   1.246 + * \param worldTime     reference world time
   1.247 + */
   1.248 +M3G_API void m3gSetSpeed(M3GAnimationController hController,
   1.249 +                         M3Gfloat factor, M3Gint worldTime)
   1.250 +{
   1.251 +    AnimationController *controller = (AnimationController *) hController;
   1.252 +    M3G_VALIDATE_OBJECT(controller);
   1.253 +
   1.254 +    controller->refSequenceTime = m3gGetPosition(controller, worldTime);
   1.255 +    controller->refWorldTime = worldTime;
   1.256 +    controller->speed = factor;
   1.257 +}
   1.258 +
   1.259 +/*!
   1.260 + * \brief Get speed.
   1.261 + *
   1.262 + * \param hController   AnimationController object
   1.263 + * \return              speed factor
   1.264 + */
   1.265 +M3G_API M3Gfloat m3gGetSpeed(M3GAnimationController hController)
   1.266 +{
   1.267 +    AnimationController *controller = (AnimationController *) hController;
   1.268 +    M3G_VALIDATE_OBJECT(controller);
   1.269 +
   1.270 +    return controller->speed;
   1.271 +}
   1.272 +
   1.273 +/*!
   1.274 + * \brief Set position.
   1.275 + *
   1.276 + * \param hController   AnimationController object
   1.277 + * \param sequenceTime  sequence time
   1.278 + * \param worldTime     world time
   1.279 + */
   1.280 +M3G_API void m3gSetPosition(M3GAnimationController hController,
   1.281 +                            M3Gfloat sequenceTime, M3Gint worldTime)
   1.282 +{
   1.283 +    AnimationController *controller = (AnimationController *) hController;
   1.284 +    M3G_VALIDATE_OBJECT(controller);
   1.285 +
   1.286 +    controller->refWorldTime = worldTime;
   1.287 +    controller->refSequenceTime = sequenceTime;
   1.288 +}
   1.289 +
   1.290 +/*!
   1.291 + * \brief Get position.
   1.292 + *
   1.293 + * \param hController   AnimationController object
   1.294 + * \param worldTime     current world time
   1.295 + * \retrun              position
   1.296 + */
   1.297 +M3G_API M3Gfloat m3gGetPosition(M3GAnimationController hController, M3Gint worldTime)
   1.298 +{
   1.299 +    AnimationController *controller = (AnimationController *) hController;
   1.300 +    M3G_VALIDATE_OBJECT(controller);
   1.301 +
   1.302 +    return m3gAdd(controller->refSequenceTime,
   1.303 +                  m3gMul(controller->speed, (M3Gfloat) worldTime - controller->refWorldTime));
   1.304 +}
   1.305 +
   1.306 +
   1.307 +/*!
   1.308 + * \brief Set controller weight.
   1.309 + *
   1.310 + * \param hController   AnimationController object
   1.311 + * \param weight        controller weight
   1.312 + */
   1.313 +M3G_API void m3gSetWeight(M3GAnimationController hController, M3Gfloat weight)
   1.314 +{
   1.315 +    AnimationController *controller = (AnimationController *) hController;
   1.316 +    M3G_VALIDATE_OBJECT(controller);
   1.317 +
   1.318 +    if (weight < 0) {
   1.319 +        m3gRaiseError(M3G_INTERFACE(controller), M3G_INVALID_VALUE);
   1.320 +        return;
   1.321 +    }
   1.322 +    controller->weight = weight;
   1.323 +}
   1.324 +
   1.325 +/*!
   1.326 + * \brief Get controller weight.
   1.327 + *
   1.328 + * \param hController   AnimationController object
   1.329 + * \return              controller weight
   1.330 + */
   1.331 +M3G_API M3Gfloat m3gGetWeight(M3GAnimationController hController)
   1.332 +{
   1.333 +    AnimationController *controller = (AnimationController *) hController;
   1.334 +    M3G_VALIDATE_OBJECT(controller);
   1.335 +
   1.336 +    return controller->weight;
   1.337 +}
   1.338 +
   1.339 +/*!
   1.340 + * \brief Get reference world time.
   1.341 + *
   1.342 + * \param hController   AnimationController object
   1.343 + * \return              reference world time
   1.344 + */
   1.345 +M3G_API M3Gint m3gGetRefWorldTime(M3GAnimationController hController)
   1.346 +{
   1.347 +    AnimationController *controller = (AnimationController *) hController;
   1.348 +    M3G_VALIDATE_OBJECT(controller);
   1.349 +
   1.350 +    return controller->refWorldTime;
   1.351 +}