d6039f29e9f64fc05c2d25ee2d2b8b60d5a24acb
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / controls / shadow-view / shadow-view.h
1 #ifndef __DALI_TOOLKIT_SHADOW_VIEW_H__
2 #define __DALI_TOOLKIT_SHADOW_VIEW_H__
3
4 /*
5  * Copyright (c) 2015 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 // EXTERNAL INCLUDES
22 #include <dali/public-api/actors/image-actor.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/public-api/controls/control.h>
26
27 namespace Dali
28 {
29
30 namespace Toolkit
31 {
32
33 namespace Internal DALI_INTERNAL
34 {
35
36 /**
37  * ShadowView implementation class
38  */
39 class ShadowView;
40
41 } // namespace Internal
42
43 /**
44  *
45  * ShadowView is a class for applying shadows to objects present in the view.
46  *
47  * Basic idea:-
48  *
49  * 1) The ShadowView object will render all its child actors offscreen from the light's point of view projected on to the shadow plane in a seperate render task.\n
50  * 2) The ShadowView object then blurs the result of step 1), using a two pass separated Gaussian blur.\n
51  * 3) The ShadowView object gets rendered automatically in the default render task along with it's children.
52  *
53  * Fundamentally, the ShadowView is simply an Actor in the normal actor tree that affects all of its children. It should be added to your Actor tree and manipulated in the
54  * normal way. It can be considered a 'portal' in the sense that all child actors are clipped to the ShadowView actor bounds.
55  *
56  * LIMITATIONS:
57  * The ShadowView is intended to provide simple planar projection shadows, Which means it needs a flat plane to cast shadows. So Shadows can't be cast on other objects.
58  *
59  * ************\n
60  * NB: It is essential to remove the ShadowView from the stage and also to call Deactivate() on it when you are not using it. This will ensure that resources are freed and
61  * rendering stops.\n
62  * ************\n
63  *
64  * Usage example:-
65  *
66  *  @code
67  *  // initialise\n
68  *  ShadowView shadowView = ShadowView::New();
69  *
70  *  // create and add some visible actors to the ShadowView, all these child actors will therefore cast a shadow.
71  *  Image image = Image::New(...);
72  *  ImageActor imageActor = ImageActor::New(image);
73  *  imageActor.SetParentOrigin( ParentOrigin::CENTER );
74  *  imageActor.SetAnchorPoint( AnchorPoint::CENTER );
75  *  shadowView.Add(imageActor);\n Add the renderable actor to the shadow view
76  *
77  *  ImageActor shadowPlane = ImageActor::New(); //This will be the shadow plane
78  *  shadowPlane.SetParentOrigin( ParentOrigin::CENTER );
79  *  shadowPlane.SetAnchorPoint( AnchorPoint::CENTER );
80  *  shadowPlane.SetSize(700.0f, 700.0f);
81  *  shadowPlane.SetPosition( Vector3(0.0f, 0.0f, -30.0f) ); //Just behind the image actor.
82  *  shadowPlane.SetShadowPlane(ShadowPlane);
83  *
84  *  Actor pointLight = Actor::New(); // This will be the light source
85  *  pointLight.SetPosition(300.0f, 250.0f, 600.0f);
86  *  Stage::GetCurrent().Add(pointLight);
87  *  shadowView.SetPointLight(pointLight);
88  *
89  *  // Start rendering the ShadowView
90  *  Stage::GetCurrent().Add(ShadowPlane);
91  *  shadowView.Activate();
92  *  ...
93  *
94  *  // animate the strength of the blur - this can fade between no blur and full blur. See GetBlurStrengthPropertyIndex().
95  *  Animation blurAnimation = Animation::New( ... );
96  *  blurAnimation.AnimateTo( Property( shadowView, shadowView.GetBlurStrengthPropertyIndex() ), ... );
97  *  blurAnimation.Play();
98  *
99  *  ...
100  *  // Stop rendering the ShadowView
101  *  Stage::GetCurrent().Remove(shadowView);
102  *  shadowView.Deactivate();
103  *  @endcode
104  */
105 class DALI_IMPORT_API ShadowView : public Control
106 {
107 public:
108
109   /**
110    * Create an uninitialized ShadowView; this can be initialized with ShadowView::New()
111    * Calling member functions with an uninitialized Dali::Object is not allowed.
112    */
113   ShadowView();
114
115   /**
116    * Copy constructor. Creates another handle that points to the same real object
117    */
118   ShadowView(const ShadowView& handle);
119
120   /**
121    * Assignment operator. Changes this handle to point to another real object
122    */
123   ShadowView& operator=(const ShadowView& view);
124
125   /**
126    * @brief Destructor
127    *
128    * This is non-virtual since derived Handle types must not contain data or virtual methods.
129    */
130   ~ShadowView();
131
132   /**
133    * Downcast an Object handle to ShadowView. If handle points to a ShadowView the
134    * downcast produces valid handle. If not the returned handle is left uninitialized.
135    * @param[in] handle Handle to an object
136    * @return handle to a ShadowView or an uninitialized handle
137    */
138   static ShadowView DownCast( BaseHandle handle );
139
140   /**
141   * Create an initialized ShadowView. Add children and call SetShadowPlane to make shadows visible\n
142   * @return A handle to a newly allocated Dali resource
143   */
144   static ShadowView New();
145
146   /**
147    * Create an initialized ShadowView. Add children and call SetShadowPlane to make shadows visible\n
148    * @param[in] downsampleWidthScale The width scale factor applied during the blur process, scaling the size of the source image to the size of the final blurred image output.
149    * Useful for downsampling - trades visual quality for processing speed. A value of 1.0f results in no scaling applied.
150    * @param[in] downsampleHeightScale The height scale factor applied during the blur process, scaling the size of the source image to the size of the final blurred image output.
151    * Useful for downsampling - trades visual quality for processing speed. A value of 1.0f results in no scaling applied.
152    * @return A handle to a newly allocated Dali resource
153    */
154   static ShadowView New(float downsampleWidthScale, float downsampleHeightScale);
155
156   /**
157    * Adds a child Actor to this Actor.
158    * NOTE! if the child already has a parent, it will be removed from old parent
159    * and reparented to this actor. This may change childs position, color, shader effect,
160    * scale etc as it now inherits them from this actor
161    * @pre This Actor (the parent) has been initialized.
162    * @pre The child actor has been initialized.
163    * @pre The child actor is not the same as the parent actor.
164    * @pre The actor is not the Root actor
165    * @param [in] child The child.
166    * @post The child will be referenced by its parent. This means that the child will be kept alive,
167    * even if the handle passed into this method is reset or destroyed.
168    */
169   void Add(Actor child);
170
171   /**
172    * Removes a child Actor from this Actor.
173    * If the actor was not a child of this actor, this is a no-op.
174    * @pre This Actor (the parent) has been initialized.
175    * @pre The child actor is not the same as the parent actor.
176    * @param [in] child The child.
177    */
178   void Remove(Actor child);
179
180   /**
181    * Set the Shadow Plane Background for the shadow effect.
182    *
183    * @param[in] shadowPlaneBackground An actor representing the shadow
184    * plane. The position of the actor represents the origin of the
185    * plane, and the orientation of the actor represents the direction
186    * of the plane normal. Make the plane sufficiently large if the shadows are
187    * clipped.
188    */
189   void SetShadowPlaneBackground(Actor shadowPlaneBackground);
190
191   /**
192    * Set the Point Light for the shadow effect. This is usually NOT a renderable actor.
193    * The orientation of the actor is not considered for the shadow calculation.
194    * @param[in] pointLight An actor representing the location of the
195    * directionless light source that casts the shadow.
196    */
197   void SetPointLight(Actor pointLight);
198
199   /**
200    * Set the field of view of the point light source. This will be used by an additional
201    * internal camera to look at the scene form the light source. If you notice any aritifacts
202    * when the light position is near to the object, Increase the field of view.
203    * @param[in] fieldOfView  New field of view in radians, Typical values are  Math::PI / 4.0f,
204    *  Math::PI / 2.0f
205    */
206   void SetPointLightFieldOfView(float fieldOfView);
207
208   /**
209    * Set shadow color.
210    * @param[in] color The shadow color
211    */
212   void SetShadowColor(Vector4 color);
213
214   /**
215    * Start rendering the ShadowView. Must be called after you Add() it to the stage.
216    * @pre This Actor has been added to the stage.
217    */
218   void Activate();
219
220   /**
221    * Stop rendering the ShadowView. Must be called after you Remove() it from the stage.
222    * @pre This Actor has been removed from the stage.
223    */
224   void Deactivate();
225
226   /**
227    * Get the property index that controls the strength of the blur applied to the shadow. Useful for animating this property.
228    * This property represents a value in the range [0.0 - 1.0] where 0.0 is no blur and 1.0 is full blur. Default 0.2.
229    * @return The property index that can be used with e.g. AnimateTo( ... )
230    */
231   Dali::Property::Index GetBlurStrengthPropertyIndex() const;
232
233   /**
234    * Get the property index that controls the color of the shadow. Useful for animating this property.
235    * This property represents a value in the Vector4 format. Default color value is Vector4(0.2f, 0.2f, 0.2f, 0.8f) (i.e grey color).
236    * @return The property index that can be used with e.g. AnimateTo( ... )
237    */
238   Dali::Property::Index GetShadowColorPropertyIndex() const;
239
240
241 public:
242
243   /**
244    * Creates a handle using the Toolkit::Internal implementation.
245    * @param[in]  implementation  The UI Control implementation.
246    */
247   DALI_INTERNAL ShadowView( Internal::ShadowView& implementation );
248
249   /**
250    * Allows the creation of this UI Control from an Internal::CustomActor pointer.
251    * @param[in]  internal  A pointer to the internal CustomActor.
252    */
253   explicit DALI_INTERNAL ShadowView( Dali::Internal::CustomActor* internal );
254 };
255
256 } // namespace Toolkit
257
258 } // namespace Dali
259
260 #endif // __DALI_TOOLKIT_SHADOW_VIEW_H__