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