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