2ef708cb77997af2f638d1546a28bf74140b17b2
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / push-button-default-painter-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_PUSH_BUTTON_DEFAULT_PAINTER_H__
2 #define __DALI_TOOLKIT_INTERNAL_PUSH_BUTTON_DEFAULT_PAINTER_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/images/image.h>
23 #include <dali/public-api/actors/actor.h>
24 #include <dali/public-api/math/vector2.h>
25 #include <dali/public-api/animation/animation.h>
26
27 #include "push-button-painter-impl.h"
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 // Forward declarations
39
40 class PushButton;
41 class PushButtonDefaultPainter;
42
43 // Type definitions
44
45 typedef IntrusivePtr<PushButtonDefaultPainter> PushButtonDefaultPainterPtr;
46
47 /**
48  * PushButtonDefaultPainter controls the Dali::Toolkit::PushButton appearance.
49  *
50  * This class inherits from Dali::Toolkit::Internal::PushButtonPainter and is registered in a
51  * Dali::Toolkit::Internal::PushButton object in order to receive the state changes.
52  */
53 class PushButtonDefaultPainter : public PushButtonPainter
54 {
55 public:
56
57   /**
58    * Constructor.
59    *
60    * Set actors and animations to NULL.
61    */
62   PushButtonDefaultPainter();
63
64   /**
65    * Destructor.
66    *
67    * It clears all fade in or fade out animations.
68    */
69   ~PushButtonDefaultPainter();
70
71   /**
72    * Sets the button image.
73    *
74    * It adds the button image to the root actor and creates the image transition if needed.
75    *
76    * @param[inout] pushButton The button in which all actors that form its appearance are going to be added.
77    * @param[in] image The button image.
78    */
79   void SetButtonImage( Toolkit::PushButton& pushButton, Actor image );
80
81   /**
82    * Sets the background image.
83    *
84    * It adds the background image to the root actor and creates the image transition if needed.
85    *
86    * @param[inout] pushButton The button in which all actors that form its appearance are going to be added.
87    * @param[in] image The background image.
88    */
89   void SetBackgroundImage( Toolkit::PushButton& pushButton, Actor image );
90
91   /**
92    * Sets the selected image.
93    *
94    * It adds the selected image to the root actor and creates the image transition if needed.
95    *
96    * @param[inout] pushButton The button in which all actors that form its appearance are going to be added.
97    * @param[in] image The selected image.
98    */
99   void SetSelectedImage( Toolkit::PushButton& pushButton, Actor image );
100
101   /**
102    * Sets the disabled background image.
103    *
104    * It adds the disabled background image to the root actor and creates the image transition if needed.
105    *
106    * @param[inout] pushButton The button in which all actors that form its appearance are going to be added.
107    * @param[in] image The disabled background image.
108    */
109   void SetDisabledBackgroundImage( Toolkit::PushButton& pushButton, Actor image );
110
111   /**
112    * Sets the disabled image.
113    *
114    * It adds the disabled image to the root actor and creates the image transition if needed.
115    *
116    * @param[inout] pushButton The button in which all actors that form its appearance are going to be added.
117    * @param[in] image The  image.
118    */
119   void SetDisabledImage( Toolkit::PushButton& pushButton, Actor image );
120
121   /**
122    * Sets the label.
123    *
124    * It adds the label to the root actor.
125    *
126    * @param[inout] pushButton The button in which all actors that form its appearance are going to be added.
127    * @param[in] label Button label.
128    */
129   void SetLabel( Toolkit::PushButton& pushButton, Actor label );
130
131   /////////////////////////////////////////////////////////////////////////////
132   // ButtonPainter interface
133   /////////////////////////////////////////////////////////////////////////////
134
135   /**
136    * Initializes the painter by setting the default images.
137    *
138    * @param[inout] button The button in which all actors that form its appearance are going to be added.
139    */
140   void Initialize( Toolkit::Button& button );
141
142   /**
143    * Sets the new size.
144    *
145    * Resizes actors. It applies size constraints.
146    *
147    * @param[inout] button The button which stores button's images.
148    * @param[in] size The new size.
149    */
150   void SetSize( Toolkit::Button& button, const Vector3& size );
151
152   /**
153    * This method is called when the \e disabled property in the Dali::Toolkit::PushButton changes.
154    *
155    * Creates image transitions if needed.
156    *
157    * @param[inout] button The button in which all actors that form its appearance are going to be added.
158    * @param[in] disabled property.
159    */
160   void SetDisabled( Toolkit::Button& button, bool disabled );
161
162   /**
163    * Sets the animation time.
164    * @param[in] animationTime The animation time.
165    */
166   void SetAnimationTime( float animationTime );
167
168   /**
169    * Retrieves the animation time.
170    * @return The animation time.
171    */
172   float GetAnimationTime() const;
173
174   /////////////////////////////////////////////////////////////////////////////
175   // PushButtonPainter interface
176   /////////////////////////////////////////////////////////////////////////////
177
178   /**
179    * This method is called when the \e autorepeating property in the Dali::Toolkit::PushButton changes.
180    * @param[in] autorepeating property.
181    */
182   void SetAutoRepeating( bool autorepeating );
183
184   /**
185    * This method is called when the Dali::Toolkit::Internal::PushButton in which this object is registered
186    * is pressed. It changes to the selected image with a transition.
187    *
188    * @param[inout] button The Dali::Toolkit::PushButton in which this object is registered.
189    */
190   void Pressed( Toolkit::PushButton& button );
191
192   /**
193    * This method is called when the Dali::Toolkit::Internal::PushButton in which this object is registered
194    * is released. It changes to the button image with a transition.
195    *
196    * @param[inout] button The Dali::Toolkit::PushButton in which this object is registered.
197    */
198   void Released( Toolkit::PushButton& button );
199
200   /**
201    * This method is called when the Dali::Toolkit::Internal::PushButton in which this object is registered
202    * is clicked.
203    *
204    * @param[inout] button The Dali::Toolkit::PushButton in which this object is registered.
205    */
206   void Clicked( Toolkit::PushButton& button );
207
208   /**
209    * This method is called when the Dali::Toolkit::Internal::PushButton in which this object is registered
210    * is toggled.
211    *
212    * @param[inout] button The Dali::Toolkit::PushButton in which this object is registered.
213    */
214   void Toggled( Toolkit::PushButton& button );
215
216 private:
217
218   // Undefined
219   PushButtonDefaultPainter( const PushButtonDefaultPainter& );
220
221   // Undefined
222   PushButtonDefaultPainter& operator=( const PushButtonDefaultPainter& );
223
224 private:
225
226   /**
227    * Default push button painter states.
228    */
229   enum PaintState
230   {
231     ReleasedState,              ///< The push button is released.
232     PressedState,               ///< The push button is pressed.
233     DisabledReleasedState,      ///< The push button is disabled and released.
234     DisabledPressedState,       ///< The push button is Disabled and pressed.
235     ReleasedPressedTransition,  ///< The push button is in transition from released to pressed.
236     PressedReleasedTransition,  ///< The push button is in transition from pressed to released.
237     ReleasedDisabledTransition, ///< The push button is in transition from released to disabled.
238     DisabledReleasedTransition, ///< The push button is in transition from disabled to released.
239     PressedDisabledTransition,  ///< The push button is in transition from pressed to disabled.
240     DisabledPressedTransition   ///< The push button is in transition from disabled to pressed.
241   };
242
243   /**
244    * Used in the FadeOut functions.
245    */
246   enum ImageLayer
247   {
248     Background, ///< Fade out the background.
249     Foreground  ///< Fade out the foreground.
250   };
251
252 private:
253   /**
254    * Apply size and position constraints to painter actors.
255    *
256    * @param[inout] actor The actor.
257    * @param[in] depth Depth position.
258    */
259   void ApplyConstraint( Actor& actor, float depth );
260
261   /**
262    * Adds the actor to the fade in animation. It creates a fade in animation if needed.
263    *
264    * @param[in] actor The actor.
265    */
266   void AddToFadeInAnimation( const Actor& actor );
267
268   /**
269    * Starts the fade in animation.
270    *
271    * PushButtonDefaultPainter::PressedReleasedFadeInAnimationFinished slot is called when the animation finishes.
272    */
273   void StartFadeInAnimation();
274
275   /**
276    * Stops the fade in animation.
277    */
278   void StopFadeInAnimation();
279
280   /**
281    * Adds the actor to the fade out animation. It creates a fade out animation if needed.
282    */
283   void AddToFadeOutAnimation( const Actor& actor );
284
285   /**
286    * Starts the fade out animation.
287    *
288    * PushButtonDefaultPainter::PressedReleasedFadeOutAnimationFinished slot is called when the animation finishes.
289    * @param[inout] pushButton The button which holds images.
290    */
291   void StartFadeOutAnimation( Toolkit::PushButton& pushButton );
292
293   /**
294    * Stops the fade out animation.
295    *
296    * It removes the actor stored in PushButtonDefaultPainter::mFadeOutBackgroundImage and
297    * PushButtonDefaultPainter::mFadeOutCheckedImage.
298    *
299    * @param[inout] pushButton The button which holds images.
300    * @param[in] remove If true, removes the fadeout actor from root.
301    */
302   void StopFadeOutAnimation( Toolkit::PushButton& pushButton, bool remove = true );
303
304   /**
305    * It adds the actor to the root actor and to the fade in animation.
306    *
307    * @param[inout] pushButton The button which holds images.
308    * @param[inout] image The actor.
309    * @param[in] opacity The initial opacity.
310    */
311   void FadeInImage( Toolkit::PushButton& pushButton, Actor& image, float opacity = 0.f );
312
313   /**
314    * It adds the actor fade out animation and stores it to be removed when the animation finishes.
315    * @param[inout] pushButton The button which holds images.
316    * @param[in] layer Defines if the actor is going to be stored in the mFadeOutBackgroundImage or mFadeOutCheckedImage member.
317    * @param[inout] image The actor.
318    * @param[in] opacity The initial opacity.
319    */
320   void FadeOutImage( Toolkit::PushButton& pushButton, ImageLayer layer, Actor& image, float opacity = 1.f );
321
322   // slots
323
324   /**
325    * Called when the fade out animation finishes.
326    *
327    * It changes the check button painter state and removes actors from the root.
328    */
329   void PressedReleasedFadeOutAnimationFinished( Dali::Animation& source );
330
331   /**
332    * Called when the fade in animation finishes.
333    *
334    * It changes the check button painter state.
335    */
336   void PressedReleasedFadeInAnimationFinished( Dali::Animation& source );
337
338 private:
339   bool                  mAutoRepeating;    ///< Stores the autorepeating property.
340   bool                  mDisabled;         ///< Stores the disabled property.
341
342   PaintState            mPaintState;       ///< The painter state.
343   Animation             mFadeInAnimation;  ///< Animation used in the state transitions.
344   Animation             mFadeOutAnimation; ///< Animation used in the state transitions.
345   Internal::PushButton* mButton;           ///< Temporary pointer used to remove fade out images from button.
346   float                 mAnimationTime;    ///< The animation time.
347   Vector3               mSize;             ///< The button's size.
348 };
349
350 } // namespace Internal
351
352 } // namespace Toolkit
353
354 } // namespace Dali
355
356 #endif // __DALI_TOOLKIT_INTERNAL_PUSH_BUTTON_DEFAULT_PAINTER_H__