1 /*
2  * Project: VizKit
3  * Version: 2.3
4 
5  * Date: 20090823
6  * File: VisualAsset.h
7  *
8  */
9 
10 /***************************************************************************
11 
12  Copyright (c) 2004-2009 Heiko Wichmann (http://www.imagomat.de/vizkit)
13 
14 
15  This software is provided 'as-is', without any expressed or implied warranty.
16  In no event will the authors be held liable for any damages
17  arising from the use of this software.
18 
19  Permission is granted to anyone to use this software for any purpose,
20  including commercial applications, and to alter it and redistribute it
21  freely, subject to the following restrictions:
22 
23  1. The origin of this software must not be misrepresented;
24  you must not claim that you wrote the original software.
25  If you use this software in a product, an acknowledgment
26  in the product documentation would be appreciated
27  but is not required.
28 
29  2. Altered source versions must be plainly marked as such,
30  and must not be misrepresented as being the original software.
31 
32  3. This notice may not be removed or altered from any source distribution.
33 
34  ***************************************************************************/
35 
36 #ifndef VisualAsset_h
37 #define VisualAsset_h
38 
39 
40 #include "VisualTypes.h"
41 #include "VisualStagePosition.h"
42 #include "VisualAnimationTypes.h"
43 #include "VisualObject.h"
44 
45 
46 namespace VizKit {
47 
48 	class VisualImage; // Forward declaration (to avoid include of header file).
49 	class VisualStageBox; // Forward declaration (to avoid include of header file).
50 	class VisualItemIdentifier; // Forward declaration (to avoid include of header file).
51 	class VisualAnimationComponent; // Forward declaration (to avoid include of header file).
52 	class VisualCamera; // Forward declaration (to avoid include of header file).
53 
54 	/**
55 	 * Dimensionless asset that contains texture, boxing information, and optional animations.
56 	 */
57 	class VisualAsset : public VisualObject {
58 
59 	public:
60 
61 		/**
62 		 * The constructor.
63 		 */
64 		VisualAsset();
65 
66 		/**
67 		 * The destructor.
68 		 */
69 		~VisualAsset();
70 
71 		/**
72 		 * Copy constructor.
73 		 * @param other Reference to another VisualAsset.
74 		 */
75 		VisualAsset(const VisualAsset& other);
76 
77 		/**
78 		 * Assignment operator.
79 		 * @param other Reference to another VisualAsset.
80 		 */
81 		VisualAsset& operator=(const VisualAsset& other);
82 
83 		/**
84 		 * Less than operator.
85 		 * @param other Another VisualAsset.
86 		 * @remarks The less than operator is used by std::map for find() and sorting (if VisualAsset is used as key_type).
87 		 */
88 		bool operator<(const VisualAsset& other) const;
89 
90 		/**
91 		 * Equality operator.
92 		 * @param other Another VisualAsset.
93 		 */
94 		bool operator==(const VisualAsset& other) const;
95 
96 		/**
97 		 * Inequality operator.
98 		 * @param other Another VisualAsset.
99 		 */
100 		bool operator!=(const VisualAsset& other) const;
101 
102 		/**
103 		 * Copies the current VisualAsset and returns a pointer to a new VisualAsset.
104 		 */
105 		virtual VisualAsset* clone(void) const;
106 
107 		/**
108 		 * Returns the stage box of the asset.
109 		 * The stage box is the bounding box.
110 		 * @return The stage box of the asset.
111 		 */
112 		VisualStageBox* getBox(void) const;
113 
114 		/**
115 		 * Sets the dimensions and direction of the camera view.
116 		 * @param cameraRef Camera whose dimensions and direction should be used by asset.
117 		 */
118 		void setCamera(const VisualCamera& cameraRef);
119 
120 		/**
121 		 * Returns the camera of the asset.
122 		 * @return The camera of the asset.
123 		 */
124 		VisualCamera& getCamera(void) const;
125 
126 		/**
127 		 * Returns the position of the asset's stage box.
128 		 * @return The position of the asset's stage box.
129 		 */
130 		VisualStagePosition getPosition(void) const;
131 
132 		/**
133 		 * Sets the position of the asset's stage box.
134 		 * @param aPosition The requested position of the asset's stage box.
135 		 */
136 		void setPosition(const VisualStagePosition& aPosition);
137 
138 		/**
139 		 * Updates the coords of the assets.
140 		 * @remarks Might be useful after a resize event of the enclosing canvas for instance.
141 		 */
142 		void updateCoords(void);
143 
144 		/**
145 		 * Sets the image of the asset.
146 		 * @param anImage Reference to the image.
147 		 * @remarks The image is copied.
148 		 */
149 		void setImage(const VisualImage& anImage);
150 
151 		/**
152 		 * Answers the question whether the asset has image data.
153 		 * @return True if asset has image data, false if not.
154 		 */
155 		bool hasImage(void) const;
156 
157 		/**
158 		 * Returns the image data of the asset.
159 		 * @return The image data of the asset.
160 		 */
161 		VisualImage* getImage(void) const;
162 
163 		/**
164 		 * Removes the image of the asset.
165 		 */
166 		void removeImage(void);
167 
168 		/**
169 		 * Draws the asset.
170 		 * @param vertexChainName The identifier of the VertexChain to draw.
171 		 */
172 		void draw(const VisualItemIdentifier& vertexChainName) const;
173 
174 		/**
175 		 * Draws the asset.
176 		 * @remarks All vertex chains are drawn.
177 		 */
178 		void draw(void) const;
179 
180 		/**
181 		 * Sets the scale value.
182 		 * @param aScaleFactor The factor by which the size of the asset should be multiplied when rendered.
183 		 * @remarks 1.0 means no magnification/minification.
184 		 */
185 		void setScaleFactor(double aScaleFactor);
186 
187 		/**
188 		 * Returns the current scale factor.
189 		 * @return The current scale factor.
190 		 */
191 		double getScaleFactor(void) const;
192 
193 		/**
194 		 * Sets the opacity value (alpha value).
195 		 * @param anOpacityValue The opacity value (alpha value).
196 		 */
197 		void setOpacityValue(double anOpacityValue);
198 
199 		/**
200 		 * Returns the current opacity value (alpha value).
201 		 * @return The current opacity value (alpha value).
202 		 */
203 		double getOpacityValue(void) const;
204 
205 		/**
206 		 * Sets the rotation angle value.
207 		 * @param aRotationAngle The requested rotation angle.
208 		 */
209 		void setRotationAngle(double aRotationAngle);
210 
211 		/**
212 		 * Returns the current rotation angle value.
213 		 * @return The current rotation angle value.
214 		 */
215 		double getRotationAngle(void) const;
216 
217 		/**
218 		 * Sets the debug mode.
219 		 * @param requestedDebugMode The debug mode. True turns debug mode on, false turns it off.
220 		 */
221 		void setDebugMode(bool requestedDebugMode);
222 
223 		/**
224 		 * Returns the current debug mode.
225 		 * @return The current debug mode. True means debug mode is on, false means it is turned off.
226 		 */
227 		bool getDebugMode(void);
228 
229 		/**
230 		 * Adds an animation to the asset.
231 		 * @param anAnimation Reference to an animation.
232 		 * @remarks It is ensured that only one animation animates a distinct property of the asset. When the asset is destructed, all animations of the asset are removed.
233 		 */
234 		void addAnimation(VisualAnimationComponent& anAnimation);
235 
236 		/**
237 		 * Clears all currently running animations.
238 		 * @param anAnimatedProperty Optional parameter that specifies the animated property of the animations.
239 		 */
240 		void removeAnimations(AnimatedProperty anAnimatedProperty = kUndefinedAnimatedProperty);
241 
242 		/**
243 		 * Returns the VisualAnimation where the animated property matches.
244 		 * @param anAnimatedProperty Parameter that specifies the animated property of the animation.
245 		 * @return The VisualAnimation where the animated property matches.
246 		 * @remarks Return value can be NULL.
247 		 */
248 		const VisualAnimationComponent* const getAnimation(const AnimatedProperty& anAnimatedProperty) const;
249 
250 		/**
251 		 * Returns the current animation value of a property which can be animated.
252 		 * @param anAnimatedProperty Parameter that specifies the queried property of the animation.
253 		 * @return The current animation value of the property.
254 		 * @remarks The returned value can differ from the current actual value of the asset property (e\.g\. for rotation return values are clamped between 0\.0 and 1\.0 instead of 0\.0 and 360\.0).
255 		 */
256 		double getCurrentAnimationValueOfProperty(const AnimatedProperty& anAnimatedProperty) const;
257 
258 		/**
259 		 * Returns the current animation value of an animated VisualStagePosition.
260 		 * @param startPosition The start position.
261 		 * @param stopPosition The stop position.
262 		 * @return The current animation value of the property.
263 		 * @remarks The returned value can differ from the current actual value of the asset property (e\.g\. for rotation return values are clamped between 0\.0 and 1\.0 instead of 0\.0 and 360\.0).
264 		 */
265 		double getCurrentAnimationValueForAnimatedLocation(const VisualStagePosition& startPosition, const VisualStagePosition& stopPosition) const;
266 
267 		/**
268 		 * Updates the location for an animation.
269 		 * @param currPosition The current position of the animation.
270 		 * @remarks Location animations are tweened between startValueVisualStagePosition and stopValueVisualStagePosition.
271 		 */
272 		void updateLocation(double currPosition);
273 
274 		/**
275 		 * Sets the start position as it is used for an animation of the VisualStagePosition property of the asset's VisualStageBox.
276 		 * @param position The start position.
277 		 */
278 		void setStartValueVisualStagePosition(const VisualStagePosition& position);
279 
280  		/**
281 		 * Sets the start position as it is used for an animation of the VisualStagePosition property of the asset's VisualStageBox.
282 		 * @param position The start position.
283 		 */
284 		void setStopValueVisualStagePosition(const VisualStagePosition& position);
285 
286 		/**
287 		 * Callback function that is called during animation.
288 		 * @param currentPosition The current position of the animation.
289 		 * @param userData Pointer to VisualAsset instance.
290 		 */
291 		static void animateOpacity(double currentPosition, void* userData);
292 
293 		/**
294 		 * Callback function that is called during animation.
295 		 * @param currentPosition The current position of the animation.
296 		 * @param userData Pointer to VisualAsset instance.
297 		 */
298 		static void animateRotation(double currentPosition, void* userData);
299 
300 		/**
301 		 * Callback function that is called during animation.
302 		 * @param currentPosition The current position of the animation.
303 		 * @param userData Pointer to VisualAsset instance.
304 		 */
305 		static void animateScaleFactor(double currentPosition, void* userData);
306 
307 		/**
308 		 * Callback function that is called during animation.
309 		 * @param currentPosition The current position of the animation.
310 		 * @param userData Pointer to VisualAsset instance.
311 		 */
312 		static void animateLocation(double currentPosition, void* userData);
313 
314 	private:
315 
316 		/**
317 		 * Initializes the VisualAsset.
318 		 */
319 		void init(void);
320 
321 		/**
322 		 * Copy method for assignment operator and copy constructor.
323 		 * @param other Another VisualAsset.
324 		 */
325 		void copy(const VisualAsset& other);
326 
327 		/**
328 		 * Draws a vertex chain (including texture data if available).
329 		 * @param aVertexChain Vertex chain to draw.
330 		 */
331 		void doDraw(const VertexChain& aVertexChain) const;
332 
333 		/**
334 		 * Draws the outline of the stage box.
335 		 * @remarks Useful method for debugging or diagnostic purposes.
336 		 */
337 		void drawBoxOutline(void) const;
338 
339 		/**
340 		 * The image of the asset.
341 		 */
342 		VisualImage* image;
343 
344 		/**
345 		 * The camera of the asset.
346 		 */
347 		VisualCamera* camera;
348 
349 		/**
350 		 * The stage box of the asset.
351 		 */
352 		VisualStageBox* stageBox;
353 
354 		/**
355 		 * The identifier of the default vertex chain.
356 		 * @remarks The default vertex chain is used in case no vertex chain has been initialized explicitely.
357 		 */
358 		VisualItemIdentifier* defaultVertexChainIdentifier;
359 
360 		/** The current rotation angle. */
361 		double rotationAngle;
362 
363 		/** The current opacity value (alpha value). */
364 		double opacityValue;
365 
366 		/** The current scale factor value. */
367 		double scaleFactor;
368 
369 		/** The start value of an animation of the VisualStagePosition property. */
370 		VisualStagePosition startValueVisualStagePosition;
371 
372 		/** The stop value of an animation of the VisualStagePosition property. */
373 		VisualStagePosition stopValueVisualStagePosition;
374 
375 	};
376 
377 }
378 
379 
380 #endif /* VisualAsset_h */
381