(Button) Allow button size to return natural size
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / buttons / check-box-button-default-painter-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_CHECK_BOX_BUTTON_DEFAULT_PAINTER_H__
2 #define __DALI_TOOLKIT_INTERNAL_CHECK_BOX_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 #include <dali-toolkit/public-api/shader-effects/image-region-effect.h>
26
27 #include "check-box-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 CheckBoxButton;
41 class CheckBoxButtonDefaultPainter;
42
43 // Type definitions
44
45 typedef IntrusivePtr<CheckBoxButtonDefaultPainter> CheckBoxButtonDefaultPainterPtr;
46
47 /**
48  * CheckBoxButtonDefaultPainter controls the Dali::Toolkit::CheckBoxButton appearance.
49  *
50  * This class inherits from Dali::Toolkit::Internal::CheckBoxButtonPainter and is registered in a
51  * Dali::Toolkit::Internal::CheckBoxButton object in order to receive the state changes.
52  */
53 class CheckBoxButtonDefaultPainter : public CheckBoxButtonPainter
54 {
55 public:
56   /**
57    * Constructor.
58    *
59    * Set actors and animations to NULL.
60    */
61   CheckBoxButtonDefaultPainter();
62
63   /**
64    * Destructor.
65    *
66    * It clears all fade in or fade out animations.
67    */
68   ~CheckBoxButtonDefaultPainter();
69
70   /**
71    * Sets the background image.
72    *
73    * It adds the background image to the root actor and creates the image transition if needed.
74    *
75    * @param[inout] checkBox The button in which all actors that form its appearance are going to be added.
76    * @param[in] image The background image.
77    */
78   void SetBackgroundImage( Toolkit::CheckBoxButton& checkBox, Actor image );
79
80   /**
81    * Sets the checked image.
82    *
83    * It adds the checked image to the root actor and creates the image transition if needed.
84    *
85    * @param[inout] checkBox The button in which all actors that form its appearance are going to be added.
86    * @param[in] image The checked image.
87    */
88   void SetCheckedImage( Toolkit::CheckBoxButton& checkBox, Actor image );
89
90   /**
91    * Sets the  dimmed backgroundimage.
92    *
93    * It adds the  dimmed backgroundimage to the root actor and creates the image transition if needed.
94    *
95    * @param[inout] checkBox The button in which all actors that form its appearance are going to be added.
96    * @param[in] image The  dimmed backgroundimage.
97    */
98   void SetDimmedBackgroundImage( Toolkit::CheckBoxButton& checkBox, Actor image );
99
100   /**
101    * Sets the dimmed checked image.
102    *
103    * It adds the dimmed checked image to the root actor and creates the image transition if needed.
104    *
105    * @param[inout] checkBox The button in which all actors that form its appearance are going to be added.
106    * @param[in] image The dimmed checked image.
107    */
108   void SetDimmedCheckedImage( Toolkit::CheckBoxButton& checkBox, Actor image );
109
110   /////////////////////////////////////////////////////////////////////////////
111   // ButtonPainter interface
112   /////////////////////////////////////////////////////////////////////////////
113
114   /**
115    * Initializes the painter by setting the default images.
116    *
117    * @param[inout] button The button in which all actors that form its appearance are going to be added.
118    */
119   void Initialize( Toolkit::Button& button );
120
121   /**
122    * Sets the new size.
123    *
124    * Resizes images. It applies size constraints.
125    *
126    * @param[inout] button The button which stores button's images.
127    * @param[in] size The new size.
128    */
129   void SetSize( Toolkit::Button& button, const Vector3& size );
130
131   /**
132    * Changes the Vega::Toolkit::CheckBoxButton for the given dimmed state.
133    *
134    * It creates the image transition if needed.
135    *
136    * @param[inout] button The button in which all actors that form its appearance are going to be added.
137    * @param[in] dimmed The dimmed state.
138    */
139   void SetDimmed( Toolkit::Button& button, bool dimmed );
140
141   /**
142    * Sets the animation time.
143    * @param[in] animationTime The animation time.
144    */
145   void SetAnimationTime( float animationTime );
146
147   /**
148    * Retrieves the animation time.
149    * @return The animation time.
150    */
151   float GetAnimationTime() const;
152
153   /////////////////////////////////////////////////////////////////////////////
154   // CheckBoxButtonPainter interface
155   /////////////////////////////////////////////////////////////////////////////
156
157   /**
158    * This method is called when the Dali::Toolkit::Internal::CheckBoxButton in which this object is registered
159    * changes its state.
160    *
161    * @param[inout] checkBox The Dali::Toolkit::CheckBoxButton in which this object is registered.
162    */
163   void Checked( Toolkit::CheckBoxButton& checkBox );
164
165 private:
166
167   // Undefined
168   CheckBoxButtonDefaultPainter( const CheckBoxButtonDefaultPainter& );
169
170   // Undefined
171   CheckBoxButtonDefaultPainter& operator=( const CheckBoxButtonDefaultPainter& );
172
173 private:
174
175   /**
176    * Default check box button painter states.
177    */
178   enum PaintState
179   {
180     UncheckedState,             ///< The check box button is unchecked.
181     CheckedState,               ///< The check box button is checked.
182     DimmedUncheckedState,       ///< The check box button is dimmed and unchecked.
183     DimmedCheckedState,         ///< The check box button is dimmed and checked.
184     UncheckedCheckedTransition, ///< The check box button is in transition from unchecked to checked.
185     CheckedUncheckedTransition, ///< The check box button is in transition from checked to unchecked.
186     UncheckedDimmedTransition,  ///< The check box button is in transition from unchecked to dimmed.
187     DimmedUncheckedTransition,  ///< The check box button is in transition from dimmed to unchecked.
188     CheckedDimmedTransition,    ///< The check box button is in transition from checked to dimmed.
189     DimmedCheckedTransition     ///< The check box button is in transition from dimmed to checked.
190   };
191
192   /**
193    * Used in the FadeOut functions.
194    */
195   enum ImageLayer
196   {
197     Background, ///< Fade out the background.
198     Foreground  ///< Fade out the foreground.
199   };
200
201 private:
202   /**
203    * Apply size and position constraints to painter actors.
204    *
205    * @param[inout] actor The actor.
206    * @param[in] depth Depth position.
207    */
208   void ApplyConstraint( Actor& actor, float depth );
209
210   /**
211    * Apply size constraint to check tick
212    *
213    * @param[inout] actor The actor.
214    * @param[in] depth Depth position.
215    */
216   void ApplyCheckedConstraint( Actor& actor, float depth );
217
218   /**
219    * Adds the actor to the fade in animation. It creates a fade in animation if needed.
220    *
221    * @param[in] actor The actor.
222    */
223   void AddToFadeInAnimation( const Actor& actor );
224
225   /**
226    * Starts the check in animation.
227    *
228    * CheckBoxButtonDefaultPainter::CheckInAnimationFinished slot is called when the animation finishes.
229    */
230   void StartCheckInAnimation();
231
232   /**
233    * Stops the check in animation.
234    */
235   void StopCheckInAnimation();
236
237   /**
238    * Adds the actor to the fade out animation. It creates a fade out animation if needed.
239    *
240    * @param[in] actor The actor.
241    */
242   void AddToFadeOutAnimation( const Actor& actor );
243
244   /**
245    * Starts the check out animation.
246    *
247    * CheckBoxButtonDefaultPainter::CheckOutAnimationFinished slot is called when the animation finishes.
248    *
249    * @param[inout] checkBox The button which holds images.
250    */
251   void StartCheckOutAnimation( Toolkit::CheckBoxButton& checkBox );
252
253   /**
254    * Stops the fade out animation.
255    *
256    * It removes the actor stored in CheckBoxButtonDefaultPainter::mFadeOutBackgroundImage and
257    * CheckBoxButtonDefaultPainter::mFadeOutCheckedImage.
258    *
259    * @param[inout] checkBox The button which holds images.
260    * @param[in] remove If true, removes the fadeout actor from root.
261    */
262   void StopCheckOutAnimation( Toolkit::CheckBoxButton& checkBox, bool remove = true );
263
264   /**
265    * It adds the actor to the root actor and to the fade in animation.
266    *
267    * @param[inout] checkBox The button which holds images.
268    * @param[inout] image The actor.
269    * @param[in] opacity The initial opacity.
270    */
271   void FadeInImage( Toolkit::CheckBoxButton& checkBox, Actor& image, float opacity = 0.f );
272
273   /**
274    * It adds the actor fade out animation and stores it to be removed when the animation finishes.
275    *
276    * @param[inout] checkBox The button which holds images.
277    * @param[in] layer Defines if the actor is going to be stored in the mFadeOutBackgroundImage or mFadeOutCheckedImage member.
278    * @param[inout] image The actor.
279    * @param[in] opacity The initial opacity.
280    */
281   void FadeOutImage( Toolkit::CheckBoxButton& checkBox, ImageLayer layer, Actor& image, float opacity = 1.f );
282
283   /**
284    * Adds the actor to the fade in animation. It creates a fade in animation if needed.
285    *
286    * @param[in] actor The actor.
287    */
288   void AddToCheckInAnimation( const Actor& actor );
289
290   /**
291    * It adds the actor to the root actor and to the fade in animation.
292    *
293    * @param[inout] checkBox The button which holds images.
294    * @param[inout] image The actor.
295    */
296   void SetupCheckedAnimation( Toolkit::CheckBoxButton& checkBox, Actor& image );
297
298   /**
299    * Signal end of check out animation
300    */
301   void EndCheckOutAnimation();
302
303   // slots
304
305   /**
306    * Called when the fade out animation finishes.
307    *
308    * It changes the check button painter state and removes actors from the root.
309    */
310   void CheckOutAnimationFinished( Dali::Animation& source );
311
312   /**
313    * Called when the fade in animation finishes.
314    *
315    * It changes the check button painter state.
316    */
317   void CheckInAnimationFinished( Dali::Animation& source );
318
319 private:
320   bool                      mDimmed;           ///< Stores the dimmed property.
321
322   PaintState                mPaintState;       ///< The painter state.
323
324   Animation                 mCheckInAnimation;  ///< Animation used in the state transitions.
325   Animation                 mCheckOutAnimation; ///< Animation used in the state transitions.
326   Internal::CheckBoxButton* mButton;           ///< Temporary pointer used to remove fade out images from button.
327   float                     mAnimationTime;    ///< The animation time.
328
329   ImageRegionEffect         mTickUVEffect;     ///< ImageRegionEffect to expand the tick across
330
331   Property::Index           mPercentageParentSizeProperty;  ///< Dynamic property on the image actor
332 };
333
334 } // namespace Internal
335
336 } // namespace Toolkit
337
338 } // namespace Dali
339
340 #endif // __DALI_TOOLKIT_INTERNAL_CHECK_BOX_BUTTON_DEFAULT_PAINTER_H__