[148] | 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-2013 Torus Knot Software Ltd |
---|
| 8 | |
---|
| 9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 10 | of this software and associated documentation files (the "Software"), to deal |
---|
| 11 | in the Software without restriction, including without limitation the rights |
---|
| 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 13 | copies of the Software, and to permit persons to whom the Software is |
---|
| 14 | furnished to do so, subject to the following conditions: |
---|
| 15 | |
---|
| 16 | The above copyright notice and this permission notice shall be included in |
---|
| 17 | all copies or substantial portions of the Software. |
---|
| 18 | |
---|
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 25 | THE SOFTWARE. |
---|
| 26 | ----------------------------------------------------------------------------- |
---|
| 27 | */ |
---|
| 28 | #ifndef __ANIMABLE_H__ |
---|
| 29 | #define __ANIMABLE_H__ |
---|
| 30 | |
---|
| 31 | #include "OgrePrerequisites.h" |
---|
| 32 | #include "OgreVector2.h" |
---|
| 33 | #include "OgreVector3.h" |
---|
| 34 | #include "OgreVector4.h" |
---|
| 35 | #include "OgreQuaternion.h" |
---|
| 36 | #include "OgreColourValue.h" |
---|
| 37 | #include "OgreSharedPtr.h" |
---|
| 38 | #include "OgreStringVector.h" |
---|
| 39 | #include "OgreException.h" |
---|
| 40 | #include "OgreAny.h" |
---|
| 41 | #include "OgreHeaderPrefix.h" |
---|
| 42 | |
---|
| 43 | namespace Ogre { |
---|
| 44 | /** \addtogroup Core |
---|
| 45 | * @{ |
---|
| 46 | */ |
---|
| 47 | |
---|
| 48 | /** \addtogroup Animation |
---|
| 49 | * @{ |
---|
| 50 | */ |
---|
| 51 | |
---|
| 52 | /** Defines an object property which is animable, i.e. may be keyframed. |
---|
| 53 | @remarks |
---|
| 54 | Animable properties are those which can be altered over time by a |
---|
| 55 | predefined keyframe sequence. They may be set directly, or they may |
---|
| 56 | be modified from their existing state (common if multiple animations |
---|
| 57 | are expected to apply at once). Implementors of this interface are |
---|
| 58 | expected to override the 'setValue', 'setCurrentStateAsBaseValue' and |
---|
| 59 | 'applyDeltaValue' methods appropriate to the type in question, and to |
---|
| 60 | initialise the type. |
---|
| 61 | @par |
---|
| 62 | AnimableValue instances are accessible through any class which extends |
---|
| 63 | AnimableObject in order to expose it's animable properties. |
---|
| 64 | @note |
---|
| 65 | This class is an instance of the Adapter pattern, since it generalises |
---|
| 66 | access to a particular property. Whilst it could have been templated |
---|
| 67 | such that the type which was being referenced was compiled in, this would |
---|
| 68 | make it more difficult to aggregated generically, and since animations |
---|
| 69 | are often comprised of multiple properties it helps to be able to deal |
---|
| 70 | with all values through a single class. |
---|
| 71 | */ |
---|
| 72 | class _OgreExport AnimableValue : public AnimableAlloc |
---|
| 73 | { |
---|
| 74 | public: |
---|
| 75 | /// The type of the value being animated |
---|
| 76 | enum ValueType |
---|
| 77 | { |
---|
| 78 | INT, |
---|
| 79 | REAL, |
---|
| 80 | VECTOR2, |
---|
| 81 | VECTOR3, |
---|
| 82 | VECTOR4, |
---|
| 83 | QUATERNION, |
---|
| 84 | COLOUR, |
---|
| 85 | RADIAN, |
---|
| 86 | DEGREE |
---|
| 87 | }; |
---|
| 88 | protected: |
---|
| 89 | /// Value type |
---|
| 90 | ValueType mType; |
---|
| 91 | |
---|
| 92 | /// Base value data |
---|
| 93 | union |
---|
| 94 | { |
---|
| 95 | int mBaseValueInt; |
---|
| 96 | Real mBaseValueReal[4]; |
---|
| 97 | }; |
---|
| 98 | |
---|
| 99 | /// Internal method to set a value as base |
---|
| 100 | virtual void setAsBaseValue(int val) { mBaseValueInt = val; } |
---|
| 101 | /// Internal method to set a value as base |
---|
| 102 | virtual void setAsBaseValue(Real val) { mBaseValueReal[0] = val; } |
---|
| 103 | /// Internal method to set a value as base |
---|
| 104 | virtual void setAsBaseValue(const Vector2& val) |
---|
| 105 | { memcpy(mBaseValueReal, val.ptr(), sizeof(Real)*2); } |
---|
| 106 | /// Internal method to set a value as base |
---|
| 107 | virtual void setAsBaseValue(const Vector3& val) |
---|
| 108 | { memcpy(mBaseValueReal, val.ptr(), sizeof(Real)*3); } |
---|
| 109 | /// Internal method to set a value as base |
---|
| 110 | virtual void setAsBaseValue(const Vector4& val) |
---|
| 111 | { memcpy(mBaseValueReal, val.ptr(), sizeof(Real)*4); } |
---|
| 112 | /// Internal method to set a value as base |
---|
| 113 | virtual void setAsBaseValue(const Quaternion& val) |
---|
| 114 | { memcpy(mBaseValueReal, val.ptr(), sizeof(Real)*4); } |
---|
| 115 | /// Internal method to set a value as base |
---|
| 116 | virtual void setAsBaseValue(const Any& val); |
---|
| 117 | /// Internal method to set a value as base |
---|
| 118 | virtual void setAsBaseValue(const ColourValue& val) |
---|
| 119 | { |
---|
| 120 | mBaseValueReal[0] = val.r; |
---|
| 121 | mBaseValueReal[1] = val.g; |
---|
| 122 | mBaseValueReal[2] = val.b; |
---|
| 123 | mBaseValueReal[3] = val.a; |
---|
| 124 | } |
---|
| 125 | /// Internal method to set a value as base |
---|
| 126 | virtual void setAsBaseValue(const Radian& val) |
---|
| 127 | { |
---|
| 128 | mBaseValueReal[0] = val.valueRadians(); |
---|
| 129 | } |
---|
| 130 | /// Internal method to set a value as base |
---|
| 131 | virtual void setAsBaseValue(const Degree& val) |
---|
| 132 | { |
---|
| 133 | mBaseValueReal[0] = val.valueRadians(); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | public: |
---|
| 138 | AnimableValue(ValueType t) : mType(t) {} |
---|
| 139 | virtual ~AnimableValue() {} |
---|
| 140 | |
---|
| 141 | /// Gets the value type of this animable value |
---|
| 142 | ValueType getType(void) const { return mType; } |
---|
| 143 | |
---|
| 144 | /// Sets the current state as the 'base' value; used for delta animation |
---|
| 145 | virtual void setCurrentStateAsBaseValue(void) = 0; |
---|
| 146 | |
---|
| 147 | /// Set value |
---|
| 148 | virtual void setValue(int) { |
---|
| 149 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 150 | } |
---|
| 151 | /// Set value |
---|
| 152 | virtual void setValue(Real) { |
---|
| 153 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 154 | } |
---|
| 155 | /// Set value |
---|
| 156 | virtual void setValue(const Vector2&) { |
---|
| 157 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 158 | } |
---|
| 159 | /// Set value |
---|
| 160 | virtual void setValue(const Vector3&) { |
---|
| 161 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 162 | } |
---|
| 163 | /// Set value |
---|
| 164 | virtual void setValue(const Vector4&) { |
---|
| 165 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 166 | } |
---|
| 167 | /// Set value |
---|
| 168 | virtual void setValue(const Quaternion&) { |
---|
| 169 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 170 | } |
---|
| 171 | /// Set value |
---|
| 172 | virtual void setValue(const ColourValue&) { |
---|
| 173 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 174 | } |
---|
| 175 | /// Set value |
---|
| 176 | virtual void setValue(const Radian&) { |
---|
| 177 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 178 | } |
---|
| 179 | /// Set value |
---|
| 180 | virtual void setValue(const Degree&) { |
---|
| 181 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 182 | } |
---|
| 183 | /// Set value |
---|
| 184 | virtual void setValue(const Any& val); |
---|
| 185 | |
---|
| 186 | // reset to base value |
---|
| 187 | virtual void resetToBaseValue(void); |
---|
| 188 | |
---|
| 189 | /// Apply delta value |
---|
| 190 | virtual void applyDeltaValue(int) { |
---|
| 191 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 192 | } |
---|
| 193 | /// Set value |
---|
| 194 | virtual void applyDeltaValue(Real) { |
---|
| 195 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 196 | } |
---|
| 197 | /// Apply delta value |
---|
| 198 | virtual void applyDeltaValue(const Vector2&) { |
---|
| 199 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 200 | } |
---|
| 201 | /// Apply delta value |
---|
| 202 | virtual void applyDeltaValue(const Vector3&) { |
---|
| 203 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 204 | } |
---|
| 205 | /// Apply delta value |
---|
| 206 | virtual void applyDeltaValue(const Vector4&) { |
---|
| 207 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 208 | } |
---|
| 209 | /// Apply delta value |
---|
| 210 | virtual void applyDeltaValue(const Quaternion&) { |
---|
| 211 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 212 | } |
---|
| 213 | /// Apply delta value |
---|
| 214 | virtual void applyDeltaValue(const ColourValue&) { |
---|
| 215 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 216 | } |
---|
| 217 | /// Apply delta value |
---|
| 218 | virtual void applyDeltaValue(const Degree&) { |
---|
| 219 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 220 | } |
---|
| 221 | /// Apply delta value |
---|
| 222 | virtual void applyDeltaValue(const Radian&) { |
---|
| 223 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "", ""); |
---|
| 224 | } |
---|
| 225 | /// Apply delta value |
---|
| 226 | virtual void applyDeltaValue(const Any& val); |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | }; |
---|
| 230 | |
---|
| 231 | typedef SharedPtr<AnimableValue> AnimableValuePtr; |
---|
| 232 | |
---|
| 233 | |
---|
| 234 | |
---|
| 235 | /** Defines an interface to classes which have one or more AnimableValue |
---|
| 236 | instances to expose. |
---|
| 237 | */ |
---|
| 238 | class _OgreExport AnimableObject |
---|
| 239 | { |
---|
| 240 | protected: |
---|
| 241 | typedef map<String, StringVector>::type AnimableDictionaryMap; |
---|
| 242 | /// Static map of class name to list of animable value names |
---|
| 243 | static AnimableDictionaryMap msAnimableDictionary; |
---|
| 244 | /** Get the name of the animable dictionary for this class. |
---|
| 245 | @remarks |
---|
| 246 | Subclasses must override this if they want to support animation of |
---|
| 247 | their values. |
---|
| 248 | */ |
---|
| 249 | virtual const String& getAnimableDictionaryName(void) const |
---|
| 250 | { return StringUtil::BLANK; } |
---|
| 251 | /** Internal method for creating a dictionary of animable value names |
---|
| 252 | for the class, if it does not already exist. |
---|
| 253 | */ |
---|
| 254 | void createAnimableDictionary(void) const |
---|
| 255 | { |
---|
| 256 | if (msAnimableDictionary.find(getAnimableDictionaryName()) |
---|
| 257 | == msAnimableDictionary.end()) |
---|
| 258 | { |
---|
| 259 | StringVector vec; |
---|
| 260 | initialiseAnimableDictionary(vec); |
---|
| 261 | msAnimableDictionary[getAnimableDictionaryName()] = vec; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | /// Get an updateable reference to animable value list |
---|
| 267 | StringVector& _getAnimableValueNames(void) |
---|
| 268 | { |
---|
| 269 | AnimableDictionaryMap::iterator i = |
---|
| 270 | msAnimableDictionary.find(getAnimableDictionaryName()); |
---|
| 271 | if (i != msAnimableDictionary.end()) |
---|
| 272 | { |
---|
| 273 | return i->second; |
---|
| 274 | } |
---|
| 275 | else |
---|
| 276 | { |
---|
| 277 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, |
---|
| 278 | "Animable value list not found for " + getAnimableDictionaryName(), |
---|
| 279 | "AnimableObject::getAnimableValueNames"); |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | /** Internal method for initialising dictionary; should be implemented by |
---|
| 285 | subclasses wanting to expose animable parameters. |
---|
| 286 | */ |
---|
| 287 | virtual void initialiseAnimableDictionary(StringVector&) const {} |
---|
| 288 | |
---|
| 289 | |
---|
| 290 | public: |
---|
| 291 | AnimableObject() {} |
---|
| 292 | virtual ~AnimableObject() {} |
---|
| 293 | |
---|
| 294 | /** Gets a list of animable value names for this object. */ |
---|
| 295 | const StringVector& getAnimableValueNames(void) const |
---|
| 296 | { |
---|
| 297 | createAnimableDictionary(); |
---|
| 298 | |
---|
| 299 | AnimableDictionaryMap::iterator i = |
---|
| 300 | msAnimableDictionary.find(getAnimableDictionaryName()); |
---|
| 301 | if (i != msAnimableDictionary.end()) |
---|
| 302 | { |
---|
| 303 | return i->second; |
---|
| 304 | } |
---|
| 305 | else |
---|
| 306 | { |
---|
| 307 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, |
---|
| 308 | "Animable value list not found for " + getAnimableDictionaryName(), |
---|
| 309 | "AnimableObject::getAnimableValueNames"); |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | /** Create a reference-counted AnimableValuePtr for the named value. |
---|
| 315 | @remarks |
---|
| 316 | You can use the returned object to animate a value on this object, |
---|
| 317 | using AnimationTrack. Subclasses must override this if they wish |
---|
| 318 | to support animation of their values. |
---|
| 319 | */ |
---|
| 320 | virtual AnimableValuePtr createAnimableValue(const String& valueName) |
---|
| 321 | { |
---|
| 322 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, |
---|
| 323 | "No animable value named '" + valueName + "' present.", |
---|
| 324 | "AnimableObject::createAnimableValue"); |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | |
---|
| 328 | |
---|
| 329 | }; |
---|
| 330 | |
---|
| 331 | /** @} */ |
---|
| 332 | /** @} */ |
---|
| 333 | |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | #include "OgreHeaderSuffix.h" |
---|
| 337 | |
---|
| 338 | #endif |
---|
| 339 | |
---|