2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "image-attributes-wrapper.h"
23 #include <image/image-attributes-api.h>
24 #include <dali-wrapper.h>
25 #include <shared/api-function.h>
26 #include <shared/object-template-helper.h>
34 v8::Persistent<v8::ObjectTemplate> ImageAttributesWrapper::mImageAttributesTemplate;
40 * Contains a list of all functions that can be called
42 const ApiFunction ImageAttributeFunctionTable[]=
44 /**************************************
45 * ImageAttributes API (in order of image-attributes.h)
46 **************************************/
47 { "SetSize", ImageAttributesApi::SetSize },
48 { "SetScalingMode", ImageAttributesApi::SetScalingMode },
49 { "SetOrientationCorrection", ImageAttributesApi::SetOrientationCorrection },
50 { "GetWidth", ImageAttributesApi::GetWidth },
51 { "GetHeight", ImageAttributesApi::GetHeight },
52 { "GetSize", ImageAttributesApi::GetSize },
53 { "GetScalingMode", ImageAttributesApi::GetScalingMode },
54 { "GetOrientationCorrection", ImageAttributesApi::GetOrientationCorrection },
57 const unsigned int ImageAttributeFunctionTableCount = sizeof(ImageAttributeFunctionTable)/sizeof(ImageAttributeFunctionTable[0]);
61 ImageAttributesWrapper::ImageAttributesWrapper( const Dali::ImageAttributes& imageAttributes, GarbageCollectorInterface& gc )
62 : BaseWrappedObject( BaseWrappedObject::IMAGE_ATTRIBUTES , gc )
64 mImageAttributes = imageAttributes;
67 v8::Handle<v8::Object> ImageAttributesWrapper::WrapImageAttributes(v8::Isolate* isolate, const Dali::ImageAttributes& attributes )
69 v8::EscapableHandleScope handleScope( isolate );
70 v8::Local<v8::ObjectTemplate> objectTemplate;
72 objectTemplate = GetImageAttributesTemplate( isolate);
74 // create an instance of the template
75 v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
77 // create the ImageAttributes wrapper
78 ImageAttributesWrapper* pointer = new ImageAttributesWrapper( attributes, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
80 // assign the JavaScript object to the wrapper.
81 pointer->SetJavascriptObject( isolate, localObject );
83 return handleScope.Escape( localObject );
86 v8::Local<v8::ObjectTemplate> ImageAttributesWrapper::GetImageAttributesTemplate( v8::Isolate* isolate)
88 v8::EscapableHandleScope handleScope( isolate );
89 v8::Local<v8::ObjectTemplate> objectTemplate;
92 if( mImageAttributesTemplate.IsEmpty() )
94 objectTemplate = MakeImageAttributesTemplate( isolate );
95 mImageAttributesTemplate.Reset( isolate, objectTemplate );
99 // get the object template
100 objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, mImageAttributesTemplate );
102 return handleScope.Escape( objectTemplate );
105 v8::Handle<v8::ObjectTemplate> ImageAttributesWrapper::MakeImageAttributesTemplate( v8::Isolate* isolate )
107 v8::EscapableHandleScope handleScope( isolate );
109 v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
111 objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
113 // add our function properties
114 ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, ImageAttributeFunctionTable, ImageAttributeFunctionTableCount );
116 return handleScope.Escape( objTemplate );
119 void ImageAttributesWrapper::NewImageAttributes( const v8::FunctionCallbackInfo< v8::Value >& args)
121 v8::Isolate* isolate = args.GetIsolate();
122 v8::HandleScope handleScope( isolate);
124 if(!args.IsConstructCall())
126 DALI_SCRIPT_EXCEPTION(isolate, "ImageAttributes constructor called without 'new'");
130 // attribs can be passed by value
131 Dali::ImageAttributes attribs = ImageAttributesApi::New( args );
132 v8::Local<v8::Object> localObject = WrapImageAttributes( isolate, attribs );
133 args.GetReturnValue().Set( localObject );
137 ImageAttributes& ImageAttributesWrapper::GetImageAttributes()
139 return mImageAttributes;
143 } // namespace V8Plugin