Size negotiation patch 4: Remove SetRelayoutEnabled
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / actors / image-actor-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 "image-actor-api.h"
20
21 // INTERNAL INCLUDES
22 #include <object/handle-wrapper.h>
23 #include <v8-utils.h>
24 #include <object/property-value-wrapper.h>
25 #include <image/image-api.h>
26 #include <image/image-wrapper.h>
27
28
29 namespace Dali
30 {
31
32 namespace V8Plugin
33 {
34
35 namespace //unnamed name space
36 {
37
38 ImageActor GetImageActor( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
39 {
40   HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, args.This() );
41   return ImageActor::DownCast( handleWrapper->mHandle );
42 }
43
44 } //unnamed name space
45
46 /**
47  * Constructor
48  *
49  * @constructor
50  * @method ImageActor
51  * @for ImageActor
52  * @param {Object} [image] Image object
53  * @param {Object} [pixelArea] Vector4
54  * @return {Object} ImageActor
55  * @example
56  *    new DALI.imageActor( image, [10,23,35,56] );
57  */
58 Actor ImageActorApi::New( const v8::FunctionCallbackInfo<v8::Value>& args )
59 {
60   v8::Isolate* isolate = args.GetIsolate();
61   v8::HandleScope handleScope( isolate );
62
63   // Image actor has 3 different constructors
64   // New();
65   // New( image);
66   // New( image, PixelArea pixelArea);
67
68   if( args.Length() == 0 )
69   {
70     return ImageActor::New();
71   }
72
73   // args.Length> 0, must have an Image parameter
74   bool found( false );
75   Image image = V8Utils::GetImageParameter( PARAMETER_0, found, isolate, args );
76   if( !found )
77   {
78     DALI_SCRIPT_EXCEPTION( isolate, "missing image from param 0" );
79     return ImageActor();
80   }
81
82   // check for PixelArea, accept a DALI Vector4 object ( which can be a JavaScript array)
83   // e.g.  new DALI.imageActor( image, [10,23,35,56] );
84   // or    new DALI.imageActor( image, Vector4 );
85
86   if( args.Length() > 1 )
87   {
88     Vector4 rect = V8Utils::GetVector4Parameter( PARAMETER_1, found, isolate, args );
89     if( !found )
90     {
91       DALI_SCRIPT_EXCEPTION( isolate, " bad parameters" );
92       return ImageActor();
93     }
94     Rect<int>rectangle( static_cast<int>(rect.x),
95                         static_cast<int>(rect.y),
96                         static_cast<int>(rect.z),
97                         static_cast<int>(rect.w));
98
99     return ImageActor::New( image, rectangle );
100   }
101   else
102   {
103     return ImageActor::New( image );
104   }
105 }
106
107 /**
108  * Set the image rendered by the actor.
109  *
110  * When the image is loaded the actor's size will be reset to the image size,
111  * unless a custom size was chosen, e.g. via actor.size or a pixel area
112  * was set.
113  * Note: The old image will continue to be displayed until the given image has loaded.
114  * @for ImageActor
115  * @method setImage
116  * @param {Object} image The image to display.
117  *
118  */
119 void ImageActorApi::SetImage( const v8::FunctionCallbackInfo<v8::Value>& args )
120 {
121   v8::Isolate* isolate = args.GetIsolate();
122   v8::HandleScope handleScope( isolate );
123
124   bool found( false );
125   Image image = V8Utils::GetImageParameter( PARAMETER_0, found, isolate, args );
126   if( !found )
127   {
128     DALI_SCRIPT_EXCEPTION( isolate, "bad parameters" );
129     return;
130   }
131   ImageActor imageActor = GetImageActor( isolate, args );
132   imageActor.SetImage( image );
133 }
134
135 /**
136  * brief Retrieve the image rendered by the actor's attachment.
137  * @for ImageActor
138  * @method getImage
139  * @return {Object} the image.
140  */
141 void ImageActorApi::GetImage( const v8::FunctionCallbackInfo<v8::Value>& args )
142 {
143   v8::Isolate* isolate = args.GetIsolate();
144   v8::HandleScope handleScope( isolate );
145   ImageActor imageActor = GetImageActor( isolate, args );
146   Image image = imageActor.GetImage();
147
148   // wrap the image
149   v8::Local<v8::Object> localObject = ImageWrapper::WrapImage( isolate, image );
150   args.GetReturnValue().Set( localObject );
151 }
152
153 /**
154  * Query whether a pixel area has been set.
155  * @for ImageActor
156  * @method isPixelAreaSet
157  * @return {Boolean} True if a pixel area has been set.
158  */
159 void ImageActorApi::IsPixelAreaSet( const v8::FunctionCallbackInfo<v8::Value>& args )
160 {
161   v8::Isolate* isolate = args.GetIsolate();
162   v8::HandleScope handleScope( isolate );
163   ImageActor imageActor = GetImageActor( isolate, args );
164
165   args.GetReturnValue().Set( v8::Boolean::New( isolate, imageActor.IsPixelAreaSet() ) );
166 }
167
168 /**
169  * Remove any pixel areas specified with SetPixelArea; the entire image will be displayed.
170  *
171  * The actor size will change to that of the Image unless a custom size was set, e.g. via
172  * actor.size
173  * @for ImageActor
174  * @method clearPixelArea
175  */
176 void ImageActorApi::ClearPixelArea( const v8::FunctionCallbackInfo<v8::Value>& args )
177 {
178   v8::Isolate* isolate = args.GetIsolate();
179   v8::HandleScope handleScope( isolate );
180   ImageActor imageActor = GetImageActor( isolate, args );
181
182   imageActor.ClearPixelArea();
183 }
184
185
186
187 } // namespace V8Plugin
188
189 } // namespace Dali