xref: /trunk/main/sd/inc/CustomAnimationEffect.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _SD_CUSTOMANIMATIONEFFECT_HXX
29 #define _SD_CUSTOMANIMATIONEFFECT_HXX
30 
31 #include <com/sun/star/animations/XAnimationNode.hpp>
32 #include <com/sun/star/animations/XTimeContainer.hpp>
33 #include <com/sun/star/animations/XAudio.hpp>
34 #include <com/sun/star/drawing/XShape.hpp>
35 #include <com/sun/star/util/XChangesListener.hpp>
36 #include <tools/string.hxx>
37 
38 #include <boost/shared_ptr.hpp>
39 
40 #include <comphelper/stl_types.hxx>
41 #include <vcl/timer.hxx>
42 
43 #include <sddllapi.h>
44 
45 #include <list>
46 #include <map>
47 
48 class SdrPathObj;
49 
50 namespace sd {
51 
52 // --------------------------------------------------------------------
53 
54 enum EValue { VALUE_FROM, VALUE_TO, VALUE_BY, VALUE_FIRST, VALUE_LAST };
55 
56 class CustomAnimationEffect;
57 class AnimationTrigger;
58 
59 class CustomAnimationPreset;
60 typedef boost::shared_ptr< CustomAnimationPreset > CustomAnimationPresetPtr;
61 
62 typedef boost::shared_ptr< CustomAnimationEffect > CustomAnimationEffectPtr;
63 
64 typedef std::list< CustomAnimationEffectPtr > EffectSequence;
65 
66 class EffectSequenceHelper;
67 
68 class CustomAnimationEffect
69 {
70 	friend class MainSequence;
71 	friend class EffectSequenceHelper;
72 
73 public:
74 	SD_DLLPUBLIC CustomAnimationEffect( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& xNode );
75 	SD_DLLPUBLIC virtual ~CustomAnimationEffect();
76 
77 	const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& getNode() const { return mxNode; }
78 	void setNode( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& xNode );
79 	void replaceNode( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& xNode );
80 
81 	CustomAnimationEffectPtr clone() const;
82 
83 	// attributes
84 	const rtl::OUString&	getPresetId() const { return maPresetId; }
85 	const rtl::OUString&	getPresetSubType() const { return maPresetSubType; }
86 	const rtl::OUString&	getProperty() const { return maProperty; }
87 
88 	sal_Int16				getPresetClass() const { return mnPresetClass; }
89 	void					setPresetClass( sal_Int16 nPresetClass );
90 
91 	sal_Int16		getNodeType() const { return mnNodeType; }
92 	SD_DLLPUBLIC void			setNodeType( sal_Int16 nNodeType );
93 
94 	::com::sun::star::uno::Any				getRepeatCount() const;
95 	void			setRepeatCount( const ::com::sun::star::uno::Any& rRepeatCount );
96 
97 	::com::sun::star::uno::Any				getEnd() const;
98 	void			setEnd( const ::com::sun::star::uno::Any& rEnd );
99 
100 	sal_Int16		getFill() const;
101 	void			setFill( sal_Int16 nFill );
102 
103 	double			getBegin() const { return mfBegin; }
104 	SD_DLLPUBLIC void			setBegin( double fBegin );
105 
106 	double			getDuration() const { return mfDuration; }
107 	SD_DLLPUBLIC void			setDuration( double fDuration );
108 
109 	double			getAbsoluteDuration() const { return mfAbsoluteDuration; }
110 
111 	const String&	getName() const { return maName; }
112 	void			setName( const String& rName ) { maName = rName; }
113 
114 	sal_Int16		getIterateType() const { return mnIterateType; }
115 	SD_DLLPUBLIC void			setIterateType( sal_Int16 nIterateType );
116 
117 	double			getIterateInterval() const { return mfIterateInterval; }
118 	SD_DLLPUBLIC void			setIterateInterval( double fIterateInterval );
119 
120 	::com::sun::star::uno::Any	getTarget() const { return maTarget; }
121 	SD_DLLPUBLIC void						setTarget( const ::com::sun::star::uno::Any& rTarget );
122 
123 	sal_Bool		hasAfterEffect() const { return mbHasAfterEffect; }
124 	void			setHasAfterEffect( sal_Bool bHasAfterEffect ) { mbHasAfterEffect = bHasAfterEffect; }
125 
126 	::com::sun::star::uno::Any	getDimColor() const { return maDimColor; }
127 	void						setDimColor( ::com::sun::star::uno::Any aDimColor ) { maDimColor = aDimColor; }
128 
129 	bool			IsAfterEffectOnNext() const { return mbAfterEffectOnNextEffect; }
130 	void			setAfterEffectOnNext( bool bOnNextEffect ) { mbAfterEffectOnNextEffect = bOnNextEffect; }
131 
132 	sal_Int32		getParaDepth() const { return mnParaDepth; }
133 
134 	sal_Bool		hasText() const { return mbHasText; }
135 
136 	sal_Int16		getCommand() const { return mnCommand; }
137 
138 	double			getAcceleration() const { return mfAcceleration; }
139 	void			setAcceleration( double fAcceleration );
140 
141 	double			getDecelerate() const { return mfDecelerate; }
142 	void			setDecelerate( double fDecelerate );
143 
144 	sal_Bool		getAutoReverse() const { return mbAutoReverse; }
145 	void			setAutoReverse( sal_Bool bAutoReverse );
146 
147 	::com::sun::star::uno::Any	getProperty( sal_Int32 nNodeType, const rtl::OUString& rAttributeName, EValue eValue );
148 	bool						setProperty( sal_Int32 nNodeType, const rtl::OUString& rAttributeName, EValue eValue, const ::com::sun::star::uno::Any& rValue );
149 
150 	::com::sun::star::uno::Any	getTransformationProperty( sal_Int32 nTransformType, EValue eValue );
151 	bool						setTransformationProperty( sal_Int32 nTransformType, EValue eValue, const ::com::sun::star::uno::Any& rValue );
152 
153 	::com::sun::star::uno::Any	getColor( sal_Int32 nIndex );
154 	void						setColor( sal_Int32 nIndex, const ::com::sun::star::uno::Any& rColor );
155 
156 	::com::sun::star::uno::Any	getRotation();
157 	void						setRotation( const ::com::sun::star::uno::Any& rRotation );
158 
159 	sal_Int32		getGroupId() const { return mnGroupId; }
160 	void			setGroupId( sal_Int32 nGroupId );
161 
162 	sal_Int16		getTargetSubItem() const { return mnTargetSubItem; }
163 	SD_DLLPUBLIC void			setTargetSubItem( sal_Int16 nSubItem );
164 
165 	::rtl::OUString	getPath() const;
166 	void setPath( const ::rtl::OUString& rPath );
167 
168 	bool checkForText();
169 	bool calculateIterateDuration();
170 
171 	void setAudio( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAudio >& xAudio );
172 	bool getStopAudio() const;
173 	SD_DLLPUBLIC void setStopAudio();
174 	SD_DLLPUBLIC void createAudio( const ::com::sun::star::uno::Any& rSource, double fVolume = 1.0 );
175 	void removeAudio();
176 	const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAudio >& getAudio() const { return mxAudio; }
177 
178 	EffectSequenceHelper*	getEffectSequence() const { return mpEffectSequence; }
179 
180 	// helper
181 	::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode > createAfterEffectNode() const throw (com::sun::star::uno::Exception);
182 	::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape > getTargetShape() const;
183 
184 	// static helpers
185 	static sal_Int32 get_node_type( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& xNode );
186 	static sal_Int32 getNumberOfSubitems( const ::com::sun::star::uno::Any& aTarget, sal_Int16 nIterateType );
187 
188 	SdrPathObj* createSdrPathObjFromPath();
189 	void updateSdrPathObjFromPath( SdrPathObj& rPathObj );
190 	void updatePathFromSdrPathObj( const SdrPathObj& rPathObj );
191 
192 protected:
193 	void setEffectSequence( EffectSequenceHelper* pSequence ) { mpEffectSequence = pSequence; }
194 
195 private:
196 	sal_Int16		mnNodeType;
197 	rtl::OUString	maPresetId;
198 	rtl::OUString	maPresetSubType;
199 	rtl::OUString	maProperty;
200 	sal_Int16		mnPresetClass;
201 	double			mfBegin;
202 	double			mfDuration;					// this is the maximum duration of the subeffects
203 	double			mfAbsoluteDuration;			// this is the maximum duration of the subeffects including possible iterations
204 	sal_Int32		mnGroupId;
205 	sal_Int16		mnIterateType;
206 	double			mfIterateInterval;
207 	sal_Int32		mnParaDepth;
208 	sal_Bool		mbHasText;
209 	double			mfAcceleration;
210 	double			mfDecelerate;
211 	sal_Bool		mbAutoReverse;
212 	sal_Int16		mnTargetSubItem;
213 	sal_Int16		mnCommand;
214 
215 	EffectSequenceHelper* mpEffectSequence;
216 
217 	String			maName;
218 
219 	::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode > mxNode;
220 	::com::sun::star::uno::Reference< ::com::sun::star::animations::XAudio > mxAudio;
221 	::com::sun::star::uno::Any maTarget;
222 
223 	sal_Bool		mbHasAfterEffect;
224 	::com::sun::star::uno::Any maDimColor;
225 	bool		mbAfterEffectOnNextEffect;
226 };
227 
228 struct stl_CustomAnimationEffect_search_node_predict
229 {
230 	stl_CustomAnimationEffect_search_node_predict( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& xSearchNode );
231 	bool operator()( CustomAnimationEffectPtr pEffect ) const;
232 	const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& mxSearchNode;
233 };
234 
235 enum ESequenceHint { EFFECT_EDITED, EFFECT_REMOVED, EFFECT_ADDED };
236 
237 /** this listener is implemented by UI components to track changes in the animation core */
238 class ISequenceListener
239 {
240 public:
241 	virtual void notify_change() = 0;
242 };
243 
244 /** this class keeps track of a group of animations that build up
245 	a text animation for a single shape */
246 class CustomAnimationTextGroup
247 {
248 	friend class EffectSequenceHelper;
249 
250 public:
251 	CustomAnimationTextGroup( const ::com::sun::star::uno::Reference< com::sun::star::drawing::XShape >& rTarget, sal_Int32 nGroupId );
252 
253 	void reset();
254 	void addEffect( CustomAnimationEffectPtr& pEffect );
255 
256 	const ::com::sun::star::uno::Reference< com::sun::star::drawing::XShape >& getTarget() const { return maTarget; }
257 	const EffectSequence& getEffects() const { return maEffects; }
258 
259 	/* -1: as single object, 0: all at once, n > 0: by n Th paragraph */
260 	sal_Int32 getTextGrouping() const { return mnTextGrouping; }
261 
262 	sal_Bool getAnimateForm() const { return mbAnimateForm; }
263 	sal_Bool getTextReverse() const { return mbTextReverse; }
264 	double getTextGroupingAuto() const { return mfGroupingAuto; }
265 
266 private:
267 	EffectSequence maEffects;
268 	::com::sun::star::uno::Reference< com::sun::star::drawing::XShape > maTarget;
269 
270 	sal_Int32 mnTextGrouping;
271 	sal_Bool mbAnimateForm;
272 	sal_Bool mbTextReverse;
273 	double mfGroupingAuto;
274 	sal_Int32 mnLastPara;
275 	sal_Int8 mnDepthFlags[5];
276 	sal_Int32 mnGroupId;
277 };
278 
279 typedef boost::shared_ptr< CustomAnimationTextGroup > CustomAnimationTextGroupPtr;
280 typedef std::map< sal_Int32, CustomAnimationTextGroupPtr > CustomAnimationTextGroupMap;
281 
282 class EffectSequenceHelper
283 {
284 friend class MainSequence;
285 
286 public:
287 	EffectSequenceHelper();
288 	EffectSequenceHelper( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XTimeContainer >& xSequenceRoot );
289 	virtual ~EffectSequenceHelper();
290 
291 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode > getRootNode();
292 
293 	CustomAnimationEffectPtr append( const CustomAnimationPresetPtr& pDescriptor, const ::com::sun::star::uno::Any& rTarget, double fDuration = -1.0 );
294 	CustomAnimationEffectPtr append( const SdrPathObj& rPathObj, const ::com::sun::star::uno::Any& rTarget, double fDuration = -1.0 );
295 	SD_DLLPUBLIC void append( const CustomAnimationEffectPtr& pEffect );
296 	void insert( EffectSequence::iterator& rPos, const CustomAnimationEffectPtr& pEffect );
297 	void replace( const CustomAnimationEffectPtr& pEffect, const CustomAnimationPresetPtr& pDescriptor, double fDuration = -1.0 );
298 	void replace( const CustomAnimationEffectPtr& pEffect, const CustomAnimationPresetPtr& pDescriptor, const rtl::OUString& rPresetSubType, double fDuration = -1.0 );
299 	void remove( const CustomAnimationEffectPtr& pEffect );
300 
301 	void create( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& xNode );
302 	void createEffectsequence( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& xNode );
303 	void processAfterEffect( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& xNode );
304 	void createEffects( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& xNode );
305 
306 	sal_Int32 getCount() const { return sal::static_int_cast< sal_Int32 >( maEffects.size() ); }
307 
308 	virtual CustomAnimationEffectPtr findEffect( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& xNode ) const;
309 
310 	virtual bool disposeShape( const com::sun::star::uno::Reference< com::sun::star::drawing::XShape >& xShape );
311 	virtual void insertTextRange( const com::sun::star::uno::Any& aTarget );
312 	virtual void disposeTextRange( const com::sun::star::uno::Any& aTarget );
313 	virtual bool hasEffect( const com::sun::star::uno::Reference< com::sun::star::drawing::XShape >& xShape );
314 	virtual void onTextChanged( const com::sun::star::uno::Reference< com::sun::star::drawing::XShape >& xShape );
315 
316 	/** this must be called if effects from this sequence are changed.
317 		the method will call the registered listeners */
318 	void update( const CustomAnimationEffectPtr& pEffect );
319 
320 	/** this method rebuilds the animation nodes */
321 	virtual void rebuild();
322 
323 	EffectSequence::iterator getBegin() { return maEffects.begin(); }
324 	EffectSequence::iterator getEnd() { return maEffects.end(); }
325 	EffectSequence::iterator find( const CustomAnimationEffectPtr& pEffect );
326 
327 	EffectSequence& getSequence() { return maEffects; }
328 
329 	void addListener( ISequenceListener* pListener );
330 	void removeListener( ISequenceListener* pListener );
331 
332 	// text group methods
333 
334 	CustomAnimationTextGroupPtr findGroup( sal_Int32 nGroupId );
335 	SD_DLLPUBLIC CustomAnimationTextGroupPtr	createTextGroup( CustomAnimationEffectPtr pEffect, sal_Int32 nTextGrouping, double fTextGroupingAuto, sal_Bool bAnimateForm, sal_Bool bTextReverse );
336 	void setTextGrouping( CustomAnimationTextGroupPtr pTextGroup, sal_Int32 nTextGrouping );
337 	void setAnimateForm( CustomAnimationTextGroupPtr pTextGroup, sal_Bool bAnimateForm );
338 	void setTextGroupingAuto( CustomAnimationTextGroupPtr pTextGroup, double fTextGroupingAuto );
339 	void setTextReverse( CustomAnimationTextGroupPtr pTextGroup, sal_Bool bAnimateForm );
340 
341 	sal_Int32 getSequenceType() const { return mnSequenceType; }
342 
343 	::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape > getTriggerShape() const { return mxEventSource; }
344 	void setTriggerShape( const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape >& xTrigger ) { mxEventSource = xTrigger; }
345 
346 	virtual sal_Int32 getOffsetFromEffect( const CustomAnimationEffectPtr& xEffect ) const;
347 	virtual CustomAnimationEffectPtr getEffectFromOffset( sal_Int32 nOffset ) const;
348 
349 protected:
350 	virtual void implRebuild();
351 	virtual void reset();
352 
353 	void createTextGroupParagraphEffects( CustomAnimationTextGroupPtr pTextGroup, CustomAnimationEffectPtr pEffect, bool bUsed );
354 
355 	void notify_listeners();
356 
357 	::com::sun::star::uno::Reference< ::com::sun::star::animations::XTimeContainer > createParallelTimeContainer() const;
358 
359 	void updateTextGroups();
360 
361 protected:
362 	::com::sun::star::uno::Reference< ::com::sun::star::animations::XTimeContainer > mxSequenceRoot;
363 	EffectSequence maEffects;
364 	std::list< ISequenceListener* > maListeners;
365 	CustomAnimationTextGroupMap maGroupMap;
366 	sal_Int32 mnSequenceType;
367 	::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape > mxEventSource;
368 };
369 
370 class MainSequence;
371 
372 class InteractiveSequence : public EffectSequenceHelper
373 {
374 friend class MainSequence;
375 friend class MainSequenceChangeGuard;
376 
377 public:
378 	InteractiveSequence( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XTimeContainer >& xSequenceRoot, MainSequence* pMainSequence );
379 
380 	/** this method rebuilds the animation nodes */
381 	virtual void rebuild();
382 
383 private:
384 	virtual void implRebuild();
385 
386 	MainSequence*	mpMainSequence;
387 };
388 
389 typedef boost::shared_ptr< InteractiveSequence > InteractiveSequencePtr;
390 typedef std::list< InteractiveSequencePtr > InteractiveSequenceList;
391 
392 class MainSequence : public EffectSequenceHelper, public ISequenceListener
393 {
394 	friend class UndoAnimation;
395 	friend class MainSequenceRebuildGuard;
396 	friend class MainSequenceChangeGuard;
397 
398 public:
399 	MainSequence();
400 	MainSequence( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& xTimingRootNode );
401 	~MainSequence();
402 
403 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode > getRootNode();
404 	void reset( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& xTimingRootNode );
405 
406 	/** this method rebuilds the animation nodes */
407 	virtual void rebuild();
408 
409 	virtual CustomAnimationEffectPtr findEffect( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& xNode ) const;
410 
411 	virtual bool disposeShape( const com::sun::star::uno::Reference< com::sun::star::drawing::XShape >& xShape );
412 	virtual void insertTextRange( const com::sun::star::uno::Any& aTarget );
413 	virtual void disposeTextRange( const com::sun::star::uno::Any& aTarget );
414 	virtual bool hasEffect( const com::sun::star::uno::Reference< com::sun::star::drawing::XShape >& xShape );
415 	virtual void onTextChanged( const com::sun::star::uno::Reference< com::sun::star::drawing::XShape >& xShape );
416 
417 	const InteractiveSequenceList& getInteractiveSequenceList() const { return maInteractiveSequenceList; }
418 
419 	virtual void notify_change();
420 
421 	bool setTrigger( const CustomAnimationEffectPtr& pEffect, const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape >& xTriggerShape );
422 
423 	/** starts a timer that recreates the internal structure from the API core after 1 second */
424 	void startRecreateTimer();
425 
426 	/** starts a timer that rebuilds the API core from the internal structure after 1 second */
427 	void startRebuildTimer();
428 
429 	virtual sal_Int32 getOffsetFromEffect( const CustomAnimationEffectPtr& xEffect ) const;
430 	virtual CustomAnimationEffectPtr getEffectFromOffset( sal_Int32 nOffset ) const;
431 
432 protected:
433 	/** permits rebuilds until unlockRebuilds() is called. All rebuild calls during a locked sequence are
434 		process after unlockRebuilds() call. lockRebuilds() and unlockRebuilds() calls can be nested. */
435 	void lockRebuilds();
436 	void unlockRebuilds();
437 
438 	DECL_LINK( onTimerHdl, Timer * );
439 
440 	virtual void implRebuild();
441 
442 	void init();
443 
444 	void createMainSequence();
445 	virtual void reset();
446 
447 	InteractiveSequencePtr createInteractiveSequence( const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape >& xShape );
448 
449 	InteractiveSequenceList maInteractiveSequenceList;
450 
451 	::com::sun::star::uno::Reference< ::com::sun::star::util::XChangesListener > mxChangesListener;
452 	::com::sun::star::uno::Reference< ::com::sun::star::animations::XTimeContainer > mxTimingRootNode;
453 	Timer maTimer;
454 	bool mbTimerMode;
455 	bool mbRebuilding;
456 
457 	long mnRebuildLockGuard;
458 	bool mbPendingRebuildRequest;
459 	sal_Int32 mbIgnoreChanges;
460 };
461 
462 typedef boost::shared_ptr< MainSequence > MainSequencePtr;
463 
464 class MainSequenceRebuildGuard
465 {
466 public:
467 	MainSequenceRebuildGuard( const MainSequencePtr& pMainSequence );
468 	~MainSequenceRebuildGuard();
469 
470 private:
471 	MainSequencePtr mpMainSequence;
472 };
473 
474 }
475 
476 #endif // _SD_CUSTOMANIMATIONEFFECT_HXX
477