Remove JavaScript binding for NativeImage, NinePatchImage, ImageActor and ShaderEffect
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / image / resource-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 "resource-image-api.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/images/image-operations.h>
23
24 // INTERNAL INCLUDES
25 #include <v8-utils.h>
26 #include <image/image-wrapper.h>
27
28 namespace Dali
29 {
30
31 namespace V8Plugin
32 {
33
34 ResourceImage ResourceImageApi::GetResourceImage( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
35 {
36   v8::HandleScope handleScope( isolate );
37
38   v8::Local<v8::Object> object = args.This();
39   v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
40   void* ptr = field->Value();
41
42   ImageWrapper* wrapper = static_cast< ImageWrapper *>(ptr);
43   return ResourceImage::DownCast( wrapper->GetImage() );
44 }
45
46 /**
47  * Create a new resource image object.
48  *
49  * @constructor
50  * @method ResourceImage
51  * @for ResourceImage
52  * @param {Object} options
53  * @param {String} options.url The URL of the image file to use.
54  * @param {Float} options.width The width to fit the loaded image to
55  * @param {Float} options.height The height to fit the loaded image to
56  * @return {Object} Image
57  */
58 Image ResourceImageApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
59 {
60   v8::Isolate* isolate = args.GetIsolate();
61   v8::HandleScope handleScope( isolate );
62
63   std::string url;
64   ImageDimensions dimensions;
65   v8::Local<v8::Value> options( args[0] );
66
67   if( !options->IsObject() )
68   {
69     DALI_SCRIPT_EXCEPTION( isolate, "Missing params" );
70     return Image();
71   }
72
73   v8::Local<v8::Object> optionsObject = options->ToObject();
74
75   v8::Local<v8::Value> urlValue = optionsObject->Get( v8::String::NewFromUtf8( isolate, "url" ) );
76   if( urlValue->IsString() )
77   {
78     url = V8Utils::v8StringToStdString( urlValue );
79   }
80   else
81   {
82     DALI_SCRIPT_EXCEPTION( isolate, "Missing url");
83     return Image();
84   }
85
86   v8::Local<v8::Value> widthValue = optionsObject->Get( v8::String::NewFromUtf8( isolate, "width" ) );
87   if( widthValue->IsUint32() )
88   {
89     const uint32_t width = widthValue->ToUint32()->Value();
90     dimensions = ImageDimensions( width, dimensions.GetHeight() );
91   }
92
93   v8::Local<v8::Value> heightValue = optionsObject->Get( v8::String::NewFromUtf8( isolate, "height" ) );
94   if( heightValue->IsUint32() )
95   {
96     const uint32_t height = heightValue->ToUint32()->Value();
97     dimensions = ImageDimensions( dimensions.GetWidth(), height );
98   }
99
100   return ResourceImage::New( url, dimensions );
101 }
102
103 /**
104  * Query whether the image data has loaded.
105  *
106  * The asynchronous loading begins when the Image object is created.
107  * After the Image object is discarded, the image data will be released from memory
108  * this will occur when the object is garbage collected.
109  * @method getLoadPolicy
110  * @for ResourceImage
111  * @return {Integer} loading state either dali.RESOURCE_LOADING, dali.RESOURCE_LOADING_SUCCEEDED or dali.RESOUCE_LOADING_FAILED
112  */
113 void ResourceImageApi::GetLoadingState( const v8::FunctionCallbackInfo< v8::Value >& args )
114 {
115   v8::Isolate* isolate = args.GetIsolate();
116   v8::HandleScope handleScope( isolate );
117
118   ResourceImage image = GetResourceImage( isolate, args );
119
120   args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetLoadingState() ) );
121 }
122
123 /**
124  * Return the image url
125  *
126  * @method getUrl
127  * @for ResourceImage
128  * @return {String} filename
129  */
130 void ResourceImageApi::GetUrl( const v8::FunctionCallbackInfo< v8::Value >& args )
131 {
132   v8::Isolate* isolate = args.GetIsolate();
133   v8::HandleScope handleScope( isolate );
134
135   ResourceImage image = GetResourceImage( isolate, args );
136
137   v8::Local<v8::String> v8String = v8::String::NewFromUtf8( isolate, image.GetUrl().c_str() );
138   args.GetReturnValue().Set( v8String  );
139 }
140
141
142 /**
143  * Reload the image
144  * @note if Image is offstage and OnDemand policy is set, reload request is ignored.
145  * @method reload
146  * @for ResourceImage
147  */
148 void ResourceImageApi::Reload( const v8::FunctionCallbackInfo< v8::Value >& args )
149 {
150   v8::Isolate* isolate = args.GetIsolate();
151   v8::HandleScope handleScope( isolate );
152
153   ResourceImage image = GetResourceImage( isolate, args );
154   image.Reload();
155 }
156
157 } // namespace V8Plugin
158
159 } // namespace Dali