ddef36559a3408c1cf457d7aca365dc7f65968c9
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / image / nine-patch-image-api.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "nine-patch-image-api.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <image/image-wrapper.h>
24
25 namespace Dali
26 {
27
28 namespace V8Plugin
29 {
30
31 /**
32  *
33  * NinePatchImage represents an image resource that can be added to ImageActors.
34  * It contains a bitmap that is synchronously loaded from the file system that contains
35  * a 9 patch border - a 1 pixel border that describes the stretch borders and the child
36  * area.
37  *
38  * The class offers an API to read the stretch area and child area, but it does not
39  * remove the border from it's bitmap. An API can be used to obtain a BufferImage with
40  * the border removed.
41  *
42  * Adding this image to an ImageActor using an Image handle will automatically convert
43  * to use the cropped BufferImage - if you don't retain a handle to this object, it will
44  * be automatically destroyed.
45  * @class NinePatchImage
46  * @extends ResourceImage
47  */
48
49 NinePatchImage NinePatchImageApi::GetNinePatchImage( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
50 {
51   v8::HandleScope handleScope( isolate );
52
53   v8::Local<v8::Object> object = args.This();
54   v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
55   void* ptr = field->Value();
56
57   ImageWrapper* wrapper = static_cast< ImageWrapper *>(ptr);
58   return NinePatchImage ::DownCast( wrapper->GetImage() );
59 }
60
61
62 /**
63  * Create a new nine patch image object.
64  *
65  * @constructor
66  * @method NinePatchImage
67  * @for NinePatchImage
68  * @param {Object} options
69  * @param {String} options.url The URL of the nine patch image file to use.
70  * @return {Object} Image
71  */
72 Image NinePatchImageApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
73 {
74   v8::Isolate* isolate = args.GetIsolate();
75   v8::HandleScope handleScope( isolate );
76
77   std::string url;
78   v8::Local<v8::Value> options( args[0] );
79
80   if( !options->IsObject() )
81   {
82     DALI_SCRIPT_EXCEPTION( isolate, "Missing params");
83     return NinePatchImage();
84   }
85
86   v8::Local<v8::Object> optionsObject = options->ToObject();
87
88   v8::Local<v8::Value> urlValue = optionsObject->Get( v8::String::NewFromUtf8( isolate, "url" ) );
89   if( urlValue->IsString() )
90   {
91     url = V8Utils::v8StringToStdString( urlValue );
92   }
93   else
94   {
95     DALI_SCRIPT_EXCEPTION( isolate, "Missing url");
96     return NinePatchImage();
97   }
98
99   return NinePatchImage::New( url );
100 }
101
102
103 /**
104  * Get the Stretch Borders
105  *
106  * @method getStretchBorders
107  * @for NinePatchImage
108  * @return object containing x,y,w,h properties
109  */
110 void NinePatchImageApi::GetStretchBorders( const v8::FunctionCallbackInfo< v8::Value >& args )
111 {
112   v8::Isolate* isolate = args.GetIsolate();
113   v8::HandleScope handleScope( isolate );
114
115   NinePatchImage image = GetNinePatchImage( isolate, args );
116
117   v8::Local<v8::Object> rectObject = v8::Object::New( isolate );
118
119   Vector4 borders = image.GetStretchBorders();
120   // Set the direction
121
122   rectObject->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Integer::New( isolate,borders.x ) );
123   rectObject->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Integer::New( isolate,borders.y ) );
124   rectObject->Set( v8::String::NewFromUtf8( isolate, "w" ), v8::Integer::New( isolate,borders.z ) );
125   rectObject->Set( v8::String::NewFromUtf8( isolate, "h" ), v8::Integer::New( isolate,borders.w ) );
126
127   args.GetReturnValue().Set( rectObject );
128 }
129
130 /**
131  * Get the child rectangle
132  * @method getChildRectangle
133  * @for NinePatchImage
134  * @return {Object} position/size of of the child rectangle with x,y,w,h properties
135  *
136  */
137 void NinePatchImageApi::GetChildRectangle( const v8::FunctionCallbackInfo< v8::Value >& args )
138 {
139   v8::Isolate* isolate = args.GetIsolate();
140   v8::HandleScope handleScope( isolate );
141
142   NinePatchImage image = GetNinePatchImage( isolate, args );
143
144   v8::Local<v8::Object> rectObject = v8::Object::New( isolate );
145   Rect<int> childRect = image.GetChildRectangle();
146
147   rectObject->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Integer::New( isolate,childRect.x ) );
148   rectObject->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Integer::New( isolate,childRect.y ) );
149   rectObject->Set( v8::String::NewFromUtf8( isolate, "w" ), v8::Integer::New( isolate,childRect.width ) );
150   rectObject->Set( v8::String::NewFromUtf8( isolate, "h" ), v8::Integer::New( isolate,childRect.height ) );
151
152   args.GetReturnValue().Set( rectObject );
153 }
154
155 /**
156  * Creates a buffer image from the bitmap with the 1 pixel border cropped off.
157  * This does not change the internal bitmap.
158  * @method createCroppedBufferImage
159  * @for NinePatchImage
160  */
161 void NinePatchImageApi::CreateCroppedBufferImage( const v8::FunctionCallbackInfo< v8::Value >& args )
162 {
163   v8::Isolate* isolate = args.GetIsolate();
164   v8::HandleScope handleScope( isolate );
165
166   Image image = GetNinePatchImage( isolate, args );
167
168   args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetWidth() ) );
169 }
170
171
172 } // namespace V8Plugin
173
174 } // namespace Dali