ad4a51b1dc182714382e2335d15866cf1370134e
[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  * Get the child rectangle
104  * @method getChildRectangle
105  * @for NinePatchImage
106  * @return {Object} position/size of of the child rectangle with x,y,w,h properties
107  *
108  */
109 void NinePatchImageApi::GetChildRectangle( const v8::FunctionCallbackInfo< v8::Value >& args )
110 {
111   v8::Isolate* isolate = args.GetIsolate();
112   v8::HandleScope handleScope( isolate );
113
114   NinePatchImage image = GetNinePatchImage( isolate, args );
115
116   v8::Local<v8::Object> rectObject = v8::Object::New( isolate );
117   Rect<int> childRect = image.GetChildRectangle();
118
119   rectObject->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Integer::New( isolate,childRect.x ) );
120   rectObject->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Integer::New( isolate,childRect.y ) );
121   rectObject->Set( v8::String::NewFromUtf8( isolate, "w" ), v8::Integer::New( isolate,childRect.width ) );
122   rectObject->Set( v8::String::NewFromUtf8( isolate, "h" ), v8::Integer::New( isolate,childRect.height ) );
123
124   args.GetReturnValue().Set( rectObject );
125 }
126
127 /**
128  * Creates a buffer image from the bitmap with the 1 pixel border cropped off.
129  * This does not change the internal bitmap.
130  * @method createCroppedBufferImage
131  * @for NinePatchImage
132  */
133 void NinePatchImageApi::CreateCroppedBufferImage( const v8::FunctionCallbackInfo< v8::Value >& args )
134 {
135   v8::Isolate* isolate = args.GetIsolate();
136   v8::HandleScope handleScope( isolate );
137
138   Image image = GetNinePatchImage( isolate, args );
139
140   args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetWidth() ) );
141 }
142
143
144 } // namespace V8Plugin
145
146 } // namespace Dali