[dali_1.0.38] Merge branch 'tizen'
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / image / image-attributes-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-attributes-api.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <image/image-attributes-wrapper.h>
24 #include <object/property-value-wrapper.h>
25 #include <shared/base-wrapped-object.h>
26
27 namespace Dali
28 {
29
30 namespace V8Plugin
31 {
32
33 ImageAttributes ImageAttributesApi::GetImageAttributesFromObject( v8::Isolate* isolate, v8::Local<v8::Object> object )
34 {
35   v8::HandleScope handleScope( isolate);
36
37   if( BaseWrappedObject::IsWrappedType ( isolate, object, BaseWrappedObject::IMAGE_ATTRIBUTES ))
38   {
39     v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
40     void* ptr = field->Value();
41     BaseWrappedObject* wrappedObject = static_cast< BaseWrappedObject *>(ptr);
42     ImageAttributesWrapper* wrapper = static_cast< ImageAttributesWrapper*>( wrappedObject );;
43     return  wrapper->GetImageAttributes();
44   }
45   else
46   {
47     DALI_SCRIPT_EXCEPTION( isolate, "bad image attributes url");
48     return ImageAttributes();
49   }
50
51 }
52
53 /***************************************
54  * IMAGE ATTRIBUTES FUNCTIONS
55  *
56  ****************************************/
57 ImageAttributes ImageAttributesApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
58 {
59   v8::Isolate* isolate = args.GetIsolate();
60   // two combinations of constructor
61   // 1 = no parameters
62   // 2 = width, height
63   bool foundParams( false );
64   int params[2];
65
66   V8Utils::ReadIntegerArguments( foundParams, &params[0],2,args,0);
67   if( !foundParams )
68   {
69     if( args.Length() != 0 )
70     {
71       DALI_SCRIPT_EXCEPTION( isolate, " ImageAttributes::New invalid params");
72       return Dali::ImageAttributes();
73     }
74     return Dali::ImageAttributes::New();
75   }
76   else
77   {
78     return  Dali::ImageAttributes::New( params[0], params[1] );
79   }
80
81 }
82
83 ImageAttributes& ImageAttributesApi::GetImageAttributes( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
84 {
85   v8::Local<v8::Object> object = args.This();
86   v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
87   void* ptr = field->Value();
88
89   ImageAttributesWrapper* wrapper = static_cast< ImageAttributesWrapper *>(ptr);
90   return wrapper->GetImageAttributes();
91 }
92
93 void ImageAttributesApi::SetSize( const v8::FunctionCallbackInfo< v8::Value >& args )
94 {
95   v8::Isolate* isolate = args.GetIsolate();
96   v8::HandleScope handleScope( isolate );
97   ImageAttributes& attribs( GetImageAttributes( isolate, args ));
98
99   bool found;
100   Vector2 size = V8Utils::GetVector2Parameter( PARAMETER_0, found, isolate, args );
101   if (!found)
102   {
103     DALI_SCRIPT_EXCEPTION( isolate, "invalid size parameter" );
104     return;
105   }
106   else
107   {
108     attribs.SetSize( size );
109   }
110 }
111
112 void ImageAttributesApi::SetScalingMode( const v8::FunctionCallbackInfo< v8::Value >& args )
113 {
114   v8::Isolate* isolate = args.GetIsolate();
115   v8::HandleScope handleScope( isolate );
116   ImageAttributes& attribs( GetImageAttributes( isolate, args ));
117
118   bool found(false);
119   int value = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0);
120   if( !found  )
121   {
122     DALI_SCRIPT_EXCEPTION( isolate, "invalid scaling mode parameter");
123     return;
124   }
125   if( value!= ImageAttributes::ShrinkToFit ||
126       value!= ImageAttributes::ScaleToFill ||
127       value!= ImageAttributes::FitWidth ||
128       value!= ImageAttributes::FitHeight )
129   {
130      DALI_SCRIPT_EXCEPTION( isolate, "invalid scaling mode parameter");
131      return;
132   }
133
134   attribs.SetScalingMode(static_cast<ImageAttributes::ScalingMode >( value) );
135 }
136
137 void ImageAttributesApi::SetOrientationCorrection( const v8::FunctionCallbackInfo< v8::Value >& args )
138 {
139   v8::Isolate* isolate = args.GetIsolate();
140   v8::HandleScope handleScope( isolate );
141   ImageAttributes& attribs( GetImageAttributes( isolate, args ));
142
143   bool parameterFound(false);
144   bool value = V8Utils::GetBooleanParameter( PARAMETER_0 , parameterFound, isolate, args );
145
146   if( !parameterFound )
147   {
148     DALI_SCRIPT_EXCEPTION( isolate, "boolean parameter not found");
149     return;
150   }
151   attribs.SetOrientationCorrection( value );
152 }
153
154 void ImageAttributesApi::GetWidth( const v8::FunctionCallbackInfo< v8::Value >& args )
155 {
156   v8::Isolate* isolate = args.GetIsolate();
157   v8::HandleScope handleScope( isolate );
158   ImageAttributes& attribs( GetImageAttributes( isolate, args ));
159
160   args.GetReturnValue().Set( v8::Integer::New( isolate, attribs.GetWidth() ) );
161 }
162
163 void ImageAttributesApi::GetHeight( const v8::FunctionCallbackInfo< v8::Value >& args )
164 {
165   v8::Isolate* isolate = args.GetIsolate();
166   v8::HandleScope handleScope( isolate );
167   ImageAttributes& attribs( GetImageAttributes( isolate, args ));
168
169   args.GetReturnValue().Set( v8::Integer::New( isolate, attribs.GetHeight() ) );
170
171 }
172
173 void ImageAttributesApi::GetSize( const v8::FunctionCallbackInfo< v8::Value >& args )
174 {
175   v8::Isolate* isolate = args.GetIsolate();
176   v8::HandleScope handleScope( isolate );
177   ImageAttributes& attribs( GetImageAttributes( isolate, args ));
178
179   Vector2 vec( attribs.GetSize() );
180   Dali::Property::Value value( vec );
181
182   v8::Local <v8::Object> object = PropertyValueWrapper::WrapDaliProperty( isolate, value);
183   args.GetReturnValue().Set(  object );
184 }
185
186 void ImageAttributesApi::GetScalingMode( const v8::FunctionCallbackInfo< v8::Value >& args )
187 {
188   v8::Isolate* isolate = args.GetIsolate();
189   v8::HandleScope handleScope( isolate );
190   ImageAttributes& attribs( GetImageAttributes( isolate, args ));
191
192   args.GetReturnValue().Set( v8::Integer::New( isolate, attribs.GetScalingMode() ) );
193
194 }
195
196 void ImageAttributesApi::GetOrientationCorrection( const v8::FunctionCallbackInfo< v8::Value >& args )
197 {
198   v8::Isolate* isolate = args.GetIsolate();
199   v8::HandleScope handleScope( isolate );
200   ImageAttributes& attribs( GetImageAttributes( isolate, args ));
201
202   args.GetReturnValue().Set(  v8::Boolean::New(  isolate, attribs.GetOrientationCorrection() ) );
203
204 }
205
206
207 } // namespace V8Plugin
208
209 } // namespace Dali