Merge "Fix the framebuffer texture context loss handling" into tizen
[platform/core/uifw/dali-core.git] / dali / public-api / actors / image-actor.h
1 #ifndef __DALI_IMAGE_ACTOR_H__
2 #define __DALI_IMAGE_ACTOR_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 #include <string>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/actors/renderable-actor.h>
26 #include <dali/public-api/math/rect.h>
27 #include <dali/public-api/images/image.h>
28
29 namespace Dali DALI_IMPORT_API
30 {
31
32 namespace Internal DALI_INTERNAL
33 {
34 class ImageActor;
35 }
36
37 /**
38  * @brief An actor for displaying images.
39  *
40  * Allows the developer to add an actor to stage which displays the content of an Image object.
41  *
42  * By default CullFaceMode is set to CullNone to enable the ImageActor to be viewed from all angles.
43  *
44  * If an ImageActor is created without setting size, then the actor takes the size of the image -
45  * this is the natural size.
46  * Setting a size on the ImageActor, e.g through the SetSize api or through an animation will
47  * stop the natural size being used.
48  *
49  * Such a set size can be changed back to the image's size by calling SetToNaturalSize().
50  *
51  * If a pixel area is set on an ImageActor with natural size, the actor size will change
52  * to match the pixel area. If a pixel area is set on an ImageActor that has had it's size set,
53  * then the size doesn't change, and the partial image will be stretched to fill the set size.
54  *
55  * Clearing the pixel area on an Image actor with natural size will cause the actor to show the
56  * whole image again, and will change size back to that of the image.
57  *
58  * Clearing the pixel area on an Image actor with a set size will cause the actor to show the
59  * whole image again, but will not change the image size.
60  */
61 class DALI_IMPORT_API ImageActor : public RenderableActor
62 {
63 public:
64
65   // Default Properties additional to RenderableActor
66   static const Property::Index PIXEL_AREA;           ///< name "pixel-area",          type RECTANGLE
67   static const Property::Index STYLE;                ///< name "style",               type STRING
68   static const Property::Index BORDER;               ///< name "border",              type VECTOR4
69   static const Property::Index IMAGE;                ///< name "image",               type MAP {"filename":"", "load-policy":...}
70
71   /**
72    * @brief Style determines how the Image is rendered.
73    *
74    * @code
75    * STYLE_QUAD:
76    *
77    *  0---------2           0-----------------2
78    *  |        /|           |                /|
79    *  |  A    / |           |      A       /  |
80    *  |      /  |           |            /    |
81    *  |     /   | SCALE (X) |          /      |
82    *  |    /    | --------> |        /        |
83    *  |   /     |           |      /          |
84    *  |  /      |           |    /            |
85    *  | /    B  |           |  /        B     |
86    *  |/        |           |/                |
87    *  1---------3           1-----------------3
88    *
89    * Image is rendered as a textured rectangle. The texture
90    * is scaled uniformly as the quad is resized.
91    *
92    * STYLE_NINE_PATCH:
93    *
94    *  |---|---------------|---|       |---|-----------------------------|---|
95    *  | 1 |       2       | 3 |       | 1 |              2              | 3 |
96    *  |---|---------------|---|       |---|-----------------------------|---|
97    *  |   |               |   |       |   |                             |   |
98    *  |   |               |   |       |   |                             |   |
99    *  | 4 |       5       | 6 | SCALE |   |                             |   |
100    *  |   |               |   | ----> |   |                             |   |
101    *  |   |               |   |       | 4 |              5              | 6 |
102    *  |-------------------|---|       |   |                             |   |
103    *  | 7 |       8       | 9 |       |   |                             |   |
104    *  |---|---------------|---|       |   |                             |   |
105    *                                  |---------------------------------|---|
106    *                                  | 7 |              8              | 9 |
107    *                                  |---|-----------------------------|---|
108    *
109    * @endcode
110    * Image is rendered as a textured rectangle. The texture
111    * is scaled differently over each of the 9 sections.
112    *
113    * Visualise a Picture Frame:
114    *
115    * - Corner sections (1,3,7,9) are not scaled, regardless
116    * of how big the Image is.
117    * - Horizontal edge sections (2,8) are scaled only in the
118    * X axis as the image increases in width.
119    * - Vertical edge sections (4,6) are scaled only in the
120    * Y axis as the image increases in height.
121    * - Center section (5) is scaled in both X and Y axes as
122    * the image increases in width and/or height.
123    *
124    * Note: If GRID hints are enabled (via a Shader that requires it),
125    * the above geometry will be further subdivided into rectangles of
126    * approx. 40x40 in size.
127    *
128    */
129   enum Style
130   {
131     STYLE_QUAD,       ///< As a simple quad.
132     STYLE_NINE_PATCH  ///< As a nine-patch.
133   };
134
135   /**
136    * @brief Pixel area is relative to the top-left (0,0) of the image.
137    */
138   typedef Rect<int> PixelArea;
139
140   /**
141    * @brief Create an uninitialized ImageActor handle.
142    *
143    * This can be initialized with ImageActor::New(...)
144    * Calling member functions with an uninitialized Dali::Object is not allowed.
145    */
146   ImageActor();
147
148   /**
149    * @brief Create an empty image actor object.
150    *
151    * @return A handle to a newly allocated actor.
152    */
153   static ImageActor New();
154
155   /**
156    * @brief Create a image actor object.
157    *
158    * The actor will take the image's natural size unless a custom size
159    * is chosen, e.g. via Actor:SetSize().
160    * If the handle is empty, ImageActor will display nothing
161    * @pre ImageActor must be initialized.
162    * @param[in] image The image to display.
163    * @return A handle to a newly allocated actor.
164    */
165   static ImageActor New(Image image);
166
167   /**
168    * @brief Create a image actor object.
169    *
170    * The actor will take the image's natural size unless a custom size
171    * is chosen, e.g. via Actor:SetSize()
172    * If the handle is empty, ImageActor will display nothing
173    * @pre ImageActor must be initialized.
174    * @param [in] image The image to display.
175    * @param [in] pixelArea The area of the image to display.
176    * This in pixels, relative to the top-left (0,0) of the image.
177    * @return A handle to a newly allocated actor.
178    */
179   static ImageActor New(Image image, PixelArea pixelArea);
180
181   /**
182    * @brief Downcast an Object handle to ImageActor.
183    *
184    *
185    * If handle points to a ImageActor the downcast produces valid
186    * handle. If not the returned handle is left uninitialized.
187    *
188    * @param[in] handle to An object
189    * @return handle to a ImageActor or an uninitialized handle
190    */
191   static ImageActor DownCast( BaseHandle handle );
192
193   /**
194    * @brief Destructor
195    *
196    * This is non-virtual since derived Handle types must not contain data or virtual methods.
197    */
198   ~ImageActor();
199
200   /**
201    * @brief Copy constructor
202    *
203    * @param [in] copy The actor to copy.
204    */
205   ImageActor(const ImageActor& copy);
206
207   /**
208    * @brief Assignment operator
209    *
210    * @param [in] rhs The actor to copy.
211    */
212   ImageActor& operator=(const ImageActor& rhs);
213
214   /**
215    * @brief This method is defined to allow assignment of the NULL value,
216    * and will throw an exception if passed any other value.
217    *
218    * Assigning to NULL is an alias for Reset().
219    * @param [in] rhs  A NULL pointer
220    * @return A reference to this handle
221    */
222   ImageActor& operator=(BaseHandle::NullType* rhs);
223
224   /**
225    * @brief Set the image rendered by the actor.
226    * Set the image rendered by the actor.
227    * If actor was already displaying a different image, the old image is dropped and actor may
228    * temporarily display nothing. Setting an empty image (handle) causes the current image to be
229    * dropped and actor displays nothing.
230    * The actor will take the image's natural size unless a custom size
231    * is chosen, e.g. via Actor:SetSize()
232    *
233    * @pre ImageActor must be initialized.
234    * @param [in] image The image to display.
235    */
236   void SetImage(Image image);
237
238   /**
239    * @brief Retrieve the image rendered by the actor.
240    *
241    * If no image is assigned, an empty handle is returned
242    * @return The image.
243    */
244   Image GetImage();
245
246   /**
247    * @brief Tell the image actor to use the natural size of the current image
248    * or future images.
249    *
250    * Calling SetSize on this actor or animating the size of the actor
251    * overrides this behaviour.
252    *
253    * @post The image actor uses the natural image size after an image
254    * has been loaded.
255    * @note Actor::SetSizeSignal() will be triggered if there is a current image.
256    */
257   void SetToNaturalSize();
258
259   /**
260    * @brief Set a region of the image to display, in pixels.
261    *
262    * When the image is loaded the actor's size will be reset to the pixelArea,
263    * unless a custom size was chosen, e.g. via Actor:SetSize().
264    * Note! PixelArea should be inside the image data size. It gets clamped by GL
265    * @pre image must be initialized.
266    * @param [in] pixelArea The area of the image to display.
267    * This in pixels, relative to the top-left (0,0) of the image.
268    */
269   void SetPixelArea(const PixelArea& pixelArea);
270
271   /**
272    * @brief Retrieve the region of the image to display, in pixels.
273    *
274    * @pre image must be initialized.
275    * @return The pixel area, or a default-constructed area if none was set.
276    */
277   PixelArea GetPixelArea() const;
278
279   /**
280    * @brief Query whether a pixel area has been set.
281    *
282    * @pre image must be initialized.
283    * @return True if a pixel area has been set.
284    */
285   bool IsPixelAreaSet() const;
286
287   /**
288    * @brief Remove any pixel areas specified with SetPixelArea; the entire image will be displayed.
289    *
290    * The actor size will change to that of the Image unless a custom size was set, e.g. via
291    * Actor::SetSize().
292    * @pre image must be initialized.
293    */
294   void ClearPixelArea();
295
296   /**
297    * @brief Set how the image is rendered; the default is STYLE_QUAD.
298    *
299    * @pre image must be initialized.
300    * @param [in] style The new style.
301    */
302   void SetStyle(Style style);
303
304   /**
305    * @brief Query how the image is rendered.
306    *
307    * @pre image must be initialized.
308    * @return The rendering style.
309    */
310   Style GetStyle() const;
311
312   /**
313    * @brief Set the border used with STYLE_NINE_PATCH.
314    *
315    * The values are in pixels from the left, top, right, and bottom of the image respectively.
316    * i.e. SetNinePatchBorder( Vector4(1,2,3,4) ) sets the left-border to 1, top-border to 2, right-border to 3, and bottom-border to 4 pixels.
317    * @param [in] border The new nine-patch border.
318    */
319   void SetNinePatchBorder(const Vector4& border);
320
321   /**
322    * @brief Retrieve the border used with STYLE_NINE_PATCH.
323    *
324    * @return The nine-patch border.
325    */
326   Vector4 GetNinePatchBorder() const;
327
328 public: // Not intended for application developers
329
330   explicit DALI_INTERNAL ImageActor(Internal::ImageActor*);
331 };
332
333 } // namespace Dali
334
335 #endif // __DALI_IMAGE_ACTOR_H__