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