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