[692] | 1 | /*
|
---|
| 2 | -----------------------------------------------------------------------------
|
---|
| 3 | This source file is part of OGRE
|
---|
| 4 | (Object-oriented Graphics Rendering Engine)
|
---|
| 5 | For the latest info, see http://www.ogre3d.org/
|
---|
| 6 |
|
---|
| 7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
| 8 | Also see acknowledgements in Readme.html
|
---|
| 9 |
|
---|
| 10 | This program is free software; you can redistribute it and/or modify it under
|
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
| 13 | version.
|
---|
| 14 |
|
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
| 18 |
|
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
| 23 | -----------------------------------------------------------------------------
|
---|
| 24 | */
|
---|
| 25 | #ifndef __ANIMABLE_H__
|
---|
| 26 | #define __ANIMABLE_H__
|
---|
| 27 |
|
---|
| 28 | #include "OgrePrerequisites.h"
|
---|
| 29 | #include "OgreVector2.h"
|
---|
| 30 | #include "OgreVector3.h"
|
---|
| 31 | #include "OgreVector4.h"
|
---|
| 32 | #include "OgreQuaternion.h"
|
---|
| 33 | #include "OgreColourValue.h"
|
---|
| 34 | #include "OgreSharedPtr.h"
|
---|
| 35 | #include "OgreStringVector.h"
|
---|
| 36 | #include "OgreException.h"
|
---|
| 37 | #include "OgreAny.h"
|
---|
| 38 |
|
---|
| 39 | namespace Ogre {
|
---|
| 40 |
|
---|
| 41 | /** Defines an object property which is animable, ie may be keyframed.
|
---|
| 42 | @remarks
|
---|
| 43 | Animable properties are those which can be altered over time by a
|
---|
| 44 | predefined keyframe sequence. They may be set directly, or they may
|
---|
| 45 | be modified from their existing state (common if multiple animations
|
---|
| 46 | are expected to apply at once). Implementors of this interface are
|
---|
| 47 | expected to override the 'setValue', 'setCurrentStateAsBaseValue' and
|
---|
| 48 | 'applyDeltaValue' methods appropriate to the type in question, and to
|
---|
| 49 | initialise the type.
|
---|
| 50 | @par
|
---|
| 51 | AnimableValue instances are accessible through any class which extends
|
---|
| 52 | AnimableObject in order to expose it's animable properties.
|
---|
| 53 | @note
|
---|
| 54 | This class is an instance of the Adapter pattern, since it generalises
|
---|
| 55 | access to a particular property. Whilst it could have been templated
|
---|
| 56 | such that the type which was being referenced was compiled in, this would
|
---|
| 57 | make it more difficult to aggregated generically, and since animations
|
---|
| 58 | are often comprised of multiple properties it helps to be able to deal
|
---|
| 59 | with all values through a single class.
|
---|
| 60 | */
|
---|
| 61 | class _OgreExport AnimableValue
|
---|
| 62 | {
|
---|
| 63 | public:
|
---|
| 64 | /// The type of the value being animated
|
---|
| 65 | enum ValueType
|
---|
| 66 | {
|
---|
| 67 | INT,
|
---|
| 68 | REAL,
|
---|
| 69 | VECTOR2,
|
---|
| 70 | VECTOR3,
|
---|
| 71 | VECTOR4,
|
---|
| 72 | QUATERNION,
|
---|
| 73 | COLOUR
|
---|
| 74 | };
|
---|
| 75 | protected:
|
---|
| 76 | /// Value type
|
---|
| 77 | ValueType mType;
|
---|
| 78 |
|
---|
| 79 | /// Base value data
|
---|
| 80 | union
|
---|
| 81 | {
|
---|
| 82 | int mBaseValueInt;
|
---|
| 83 | Real mBaseValueReal[4];
|
---|
| 84 | };
|
---|
| 85 |
|
---|
| 86 | /// Internal method to set a value as base
|
---|
| 87 | virtual void setAsBaseValue(int val) { mBaseValueInt = val; }
|
---|
| 88 | /// Internal method to set a value as base
|
---|
| 89 | virtual void setAsBaseValue(Real val) { mBaseValueReal[0] = val; }
|
---|
| 90 | /// Internal method to set a value as base
|
---|
| 91 | virtual void setAsBaseValue(const Vector2& val)
|
---|
| 92 | { memcpy(mBaseValueReal, val.val, sizeof(Real)*2); }
|
---|
| 93 | /// Internal method to set a value as base
|
---|
| 94 | virtual void setAsBaseValue(const Vector3& val)
|
---|
| 95 | { memcpy(mBaseValueReal, val.val, sizeof(Real)*3); }
|
---|
| 96 | /// Internal method to set a value as base
|
---|
| 97 | virtual void setAsBaseValue(const Vector4& val)
|
---|
| 98 | { memcpy(mBaseValueReal, val.val, sizeof(Real)*4); }
|
---|
| 99 | /// Internal method to set a value as base
|
---|
| 100 | virtual void setAsBaseValue(const Quaternion& val)
|
---|
| 101 | { memcpy(mBaseValueReal, val.val, sizeof(Real)*4); }
|
---|
| 102 | /// Internal method to set a value as base
|
---|
| 103 | virtual void setAsBaseValue(const Any& val);
|
---|
| 104 | /// Internal method to set a value as base
|
---|
| 105 | virtual void setAsBaseValue(const ColourValue& val)
|
---|
| 106 | {
|
---|
| 107 | mBaseValueReal[0] = val.r;
|
---|
| 108 | mBaseValueReal[1] = val.g;
|
---|
| 109 | mBaseValueReal[2] = val.b;
|
---|
| 110 | mBaseValueReal[3] = val.a;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 |
|
---|
| 114 | public:
|
---|
| 115 | AnimableValue(ValueType t) : mType(t) {}
|
---|
| 116 | virtual ~AnimableValue() {}
|
---|
| 117 |
|
---|
| 118 | /// Gets the value type of this animable value
|
---|
| 119 | ValueType getType(void) const { return mType; }
|
---|
| 120 |
|
---|
| 121 | /// Sets the current state as the 'base' value; used for delta animation
|
---|
| 122 | virtual void setCurrentStateAsBaseValue(void) = 0;
|
---|
| 123 |
|
---|
| 124 | /// Set value
|
---|
| 125 | virtual void setValue(int val) {
|
---|
| 126 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", "");
|
---|
| 127 | }
|
---|
| 128 | /// Set value
|
---|
| 129 | virtual void setValue(Real val) {
|
---|
| 130 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", "");
|
---|
| 131 | }
|
---|
| 132 | /// Set value
|
---|
| 133 | virtual void setValue(const Vector2& val) {
|
---|
| 134 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", "");
|
---|
| 135 | }
|
---|
| 136 | /// Set value
|
---|
| 137 | virtual void setValue(const Vector3& val) {
|
---|
| 138 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", "");
|
---|
| 139 | }
|
---|
| 140 | /// Set value
|
---|
| 141 | virtual void setValue(const Vector4& val) {
|
---|
| 142 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", "");
|
---|
| 143 | }
|
---|
| 144 | /// Set value
|
---|
| 145 | virtual void setValue(const Quaternion& val) {
|
---|
| 146 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", "");
|
---|
| 147 | }
|
---|
| 148 | /// Set value
|
---|
| 149 | virtual void setValue(const ColourValue& val) {
|
---|
| 150 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", "");
|
---|
| 151 | }
|
---|
| 152 | /// Set value
|
---|
| 153 | virtual void setValue(const Any& val);
|
---|
| 154 |
|
---|
| 155 | // reset to base value
|
---|
| 156 | virtual void resetToBaseValue(void);
|
---|
| 157 |
|
---|
| 158 | /// Apply delta value
|
---|
| 159 | virtual void applyDeltaValue(int val) {
|
---|
| 160 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", "");
|
---|
| 161 | }
|
---|
| 162 | /// Set value
|
---|
| 163 | virtual void applyDeltaValue(Real val) {
|
---|
| 164 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", "");
|
---|
| 165 | }
|
---|
| 166 | /// Apply delta value
|
---|
| 167 | virtual void applyDeltaValue(const Vector2& val) {
|
---|
| 168 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", "");
|
---|
| 169 | }
|
---|
| 170 | /// Apply delta value
|
---|
| 171 | virtual void applyDeltaValue(const Vector3& val) {
|
---|
| 172 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", "");
|
---|
| 173 | }
|
---|
| 174 | /// Apply delta value
|
---|
| 175 | virtual void applyDeltaValue(const Vector4& val) {
|
---|
| 176 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", "");
|
---|
| 177 | }
|
---|
| 178 | /// Apply delta value
|
---|
| 179 | virtual void applyDeltaValue(const Quaternion& val) {
|
---|
| 180 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", "");
|
---|
| 181 | }
|
---|
| 182 | /// Apply delta value
|
---|
| 183 | virtual void applyDeltaValue(const ColourValue& val) {
|
---|
| 184 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", "");
|
---|
| 185 | }
|
---|
| 186 | /// Apply delta value
|
---|
| 187 | virtual void applyDeltaValue(const Any& val);
|
---|
| 188 |
|
---|
| 189 |
|
---|
| 190 | };
|
---|
| 191 |
|
---|
| 192 | typedef SharedPtr<AnimableValue> AnimableValuePtr;
|
---|
| 193 |
|
---|
| 194 |
|
---|
| 195 |
|
---|
| 196 | /** Defines an interface to classes which have one or more AnimableValue
|
---|
| 197 | instances to expose.
|
---|
| 198 | */
|
---|
| 199 | class _OgreExport AnimableObject
|
---|
| 200 | {
|
---|
| 201 | protected:
|
---|
| 202 | typedef std::map<String, StringVector> AnimableDictionaryMap;
|
---|
| 203 | /// Static map of class name to list of animable value names
|
---|
| 204 | static AnimableDictionaryMap msAnimableDictionary;
|
---|
| 205 | /** Get the name of the animable dictionary for this class.
|
---|
| 206 | @remarks
|
---|
| 207 | Subclasses must override this if they want to support animation of
|
---|
| 208 | their values.
|
---|
| 209 | */
|
---|
| 210 | virtual const String& getAnimableDictionaryName(void) const
|
---|
| 211 | { return StringUtil::BLANK; }
|
---|
| 212 | /** Internal method for creating a dictionary of animable value names
|
---|
| 213 | for the class, if it does not already exist.
|
---|
| 214 | */
|
---|
| 215 | void createAnimableDictionary(void) const
|
---|
| 216 | {
|
---|
| 217 | if (msAnimableDictionary.find(getAnimableDictionaryName())
|
---|
| 218 | == msAnimableDictionary.end())
|
---|
| 219 | {
|
---|
| 220 | StringVector vec;
|
---|
| 221 | initialiseAnimableDictionary(vec);
|
---|
| 222 | msAnimableDictionary[getAnimableDictionaryName()] = vec;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | /// Get an updateable reference to animable value list
|
---|
| 228 | StringVector& getAnimableValueNames(void)
|
---|
| 229 | {
|
---|
| 230 | AnimableDictionaryMap::iterator i =
|
---|
| 231 | msAnimableDictionary.find(getAnimableDictionaryName());
|
---|
| 232 | if (i != msAnimableDictionary.end())
|
---|
| 233 | {
|
---|
| 234 | return i->second;
|
---|
| 235 | }
|
---|
| 236 | else
|
---|
| 237 | {
|
---|
| 238 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
|
---|
| 239 | "Animable value list not found for " + getAnimableDictionaryName(),
|
---|
| 240 | "AnimableObject::getAnimableValueNames");
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | /** Internal method for initialising dictionary; should be implemented by
|
---|
| 246 | subclasses wanting to expose animable parameters.
|
---|
| 247 | */
|
---|
| 248 | virtual void initialiseAnimableDictionary(StringVector& vec) const {}
|
---|
| 249 |
|
---|
| 250 |
|
---|
| 251 | public:
|
---|
| 252 | AnimableObject() {}
|
---|
| 253 | virtual ~AnimableObject() {}
|
---|
| 254 |
|
---|
| 255 | /** Gets a list of animable value names for this object. */
|
---|
| 256 | const StringVector& getAnimableValueNames(void) const
|
---|
| 257 | {
|
---|
| 258 | createAnimableDictionary();
|
---|
| 259 |
|
---|
| 260 | AnimableDictionaryMap::iterator i =
|
---|
| 261 | msAnimableDictionary.find(getAnimableDictionaryName());
|
---|
| 262 | if (i != msAnimableDictionary.end())
|
---|
| 263 | {
|
---|
| 264 | return i->second;
|
---|
| 265 | }
|
---|
| 266 | else
|
---|
| 267 | {
|
---|
| 268 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
|
---|
| 269 | "Animable value list not found for " + getAnimableDictionaryName(),
|
---|
| 270 | "AnimableObject::getAnimableValueNames");
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | /** Create a reference-counted AnimableValuePtr for the named value.
|
---|
| 276 | @remarks
|
---|
| 277 | You can use the returned object to animate a value on this object,
|
---|
| 278 | using AnimationTrack. Subclasses must override this if they wish
|
---|
| 279 | to support animation of their values.
|
---|
| 280 | */
|
---|
| 281 | virtual AnimableValuePtr createAnimableValue(const String& valueName)
|
---|
| 282 | {
|
---|
| 283 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
|
---|
| 284 | "No animable value named '" + valueName + "' present.",
|
---|
| 285 | "AnimableObject::createAnimableValue");
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 |
|
---|
| 289 |
|
---|
| 290 | };
|
---|
| 291 |
|
---|
| 292 |
|
---|
| 293 | }
|
---|
| 294 | #endif
|
---|
| 295 |
|
---|