ea757a1ef3d7348dd464ef2d9d86b993def78d4c
[platform/core/uifw/dali-core.git] / dali / internal / event / images / nine-patch-image-impl.h
1 #ifndef __DALI_INTERNAL_NINE_PATCH_IMAGE_H__
2 #define __DALI_INTERNAL_NINE_PATCH_IMAGE_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/nine-patch-image.h>
22 #include <dali/internal/event/images/bitmap-image-impl.h>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 class NinePatchImage;
31 typedef IntrusivePtr<NinePatchImage> NinePatchImagePtr;
32
33 class ResourceClient;
34 class ResourceManager;
35
36 namespace SceneGraph
37 {
38 class UpdateManager;
39 }
40
41 /**
42  * NinePatchImage represents an image resource that can be added to actors etc.
43  * It's image data has a border which determines stretch and fill areas
44  * Its pixel buffer data is loaded synchronously from file.
45  */
46 class NinePatchImage : public Image
47 {
48 public:
49
50   /**
51    * Create a new NinePatchImage.
52    * Also a pixel buffer for image data is allocated.
53    * Dali has ownership of the buffer.
54    * @param [in] filename    File to load synchronously into buffer
55    * @param [in] attributes  Image attributes of the file
56    * @param [in] loadPol     controls time of loading a resource from the filesystem (default: load when Image is created).
57    * @param [in] releasePol  optionally relase memory when image is not visible on screen (default: keep image data until Image object is alive).
58    */
59   static NinePatchImagePtr New( const std::string& filename,
60                                 const ImageAttributes& attributes,
61                                 LoadPolicy    loadPol    = ImageLoadPolicyDefault,
62                                 ReleasePolicy releasePol = ImageReleasePolicyDefault );
63
64   /**
65    * Create a new NinePatchImage
66    * For better performance and portability use power of two dimensions.
67    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
68    * @param [in] filename    File to load synchronously into buffer
69    * @param [in] attributes  Image attributes of the file
70    * @param [in] loadPol     controls time of loading a resource from the filesystem (default: load when Image is created).
71    * @param [in] releasePol  optionally relase memory when image is not visible on screen (default: keep image data until Image object is alive).
72    */
73   NinePatchImage( const std::string& filename,
74                   const ImageAttributes& attributes,
75                   LoadPolicy    loadPol    = ImageLoadPolicyDefault,
76                   ReleasePolicy releasePol = ImageReleasePolicyDefault );
77
78   /**
79    * Convert Image object to a 9 patch image object if possible.
80    * @param[in] image The image to convert
81    * @return A pointer to the 9 patch image object, or NULL
82    * if the conversion is not possible.
83    */
84   static NinePatchImage* GetNinePatchImage( Image* image);
85
86
87 protected:
88   /**
89    * A reference counted object may only be deleted by calling Unreference()
90    */
91   virtual ~NinePatchImage();
92
93 public:
94   /**
95    * Get the stretch borders
96    * @return The border in pixels from the left, top, right, and bottom of the image respectively.
97    */
98   Vector4 GetStretchBorders();
99
100   /**
101    * Get the child rectangle
102    * @return the position and size of the child rectangle
103    */
104   Rect<int> GetChildRectangle();
105
106   /**
107    * @brief Create a cropped image from the bitmap with the 1 pixel border cropped off.
108    * This does not change the internal bitmap.
109    *
110    * @return the cropped bitmap.
111    */
112   BitmapImagePtr CreateCroppedBitmapImage();
113
114 private:
115   /**
116    * Initializes internal data.
117    */
118   void Initialize();
119
120 protected: // From Resource
121   /**
122    * @copydoc Dali::Internal::Image::Connect
123    */
124   virtual void Connect();
125
126   /**
127    * @copydoc Dali::Internal::Image::Disconnect
128    */
129   virtual void Disconnect();
130
131 private:
132   /**
133    * Read the borders of the bitmap and determine the child area
134    * and stretch borders
135    */
136   void ParseBorders();
137
138 private:
139   ResourceClient*               mResourceClient;
140   Integration::BitmapPtr        mBitmap;
141   SceneGraph::UpdateManager*    mUpdateManager;
142   Vector4                       mStretchBorders;
143   Rect<int>                     mChildRectangle;
144   bool                          mParsedBorder;
145 };
146
147 } // namespace Internal
148
149 /**
150  * Helper methods for public API.
151  */
152 inline Internal::NinePatchImage& GetImplementation(Dali::NinePatchImage& handle)
153 {
154   DALI_ASSERT_ALWAYS( handle && "NinePatchImage handle is empty" );
155
156   BaseObject& image = handle.GetBaseObject();
157
158   return static_cast<Internal::NinePatchImage&>(image);
159 }
160
161 inline const Internal::NinePatchImage& GetImplementation(const Dali::NinePatchImage& handle)
162 {
163   DALI_ASSERT_ALWAYS( handle && "NinePatchImage handle is empty" );
164
165   const BaseObject& image = handle.GetBaseObject();
166
167   return static_cast<const Internal::NinePatchImage&>(image);
168 }
169
170 } // namespace Dali
171
172 #endif // __DALI_INTERNAL_NINE_PATCH_IMAGE_H__