fa89f797cf1d8a403cdd461c2d414be83f68154f
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / image / image-attributes-wrapper.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-wrapper.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.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>
27
28 namespace Dali
29 {
30
31 namespace V8Plugin
32 {
33
34 v8::Persistent<v8::ObjectTemplate> ImageAttributesWrapper::mImageAttributesTemplate;
35
36 namespace
37 {
38
39 /**
40  * Contains a list of all functions that can be called
41  */
42 const ApiFunction ImageAttributeFunctionTable[]=
43 {
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 },
55 };
56
57 const unsigned int ImageAttributeFunctionTableCount = sizeof(ImageAttributeFunctionTable)/sizeof(ImageAttributeFunctionTable[0]);
58 } //un-named space
59
60
61 ImageAttributesWrapper::ImageAttributesWrapper( const Dali::ImageAttributes& imageAttributes, GarbageCollectorInterface& gc )
62 : BaseWrappedObject( BaseWrappedObject::IMAGE_ATTRIBUTES , gc )
63 {
64     mImageAttributes = imageAttributes;
65 }
66
67 v8::Handle<v8::Object> ImageAttributesWrapper::WrapImageAttributes(v8::Isolate* isolate, const Dali::ImageAttributes& attributes )
68 {
69   v8::EscapableHandleScope handleScope( isolate );
70   v8::Local<v8::ObjectTemplate> objectTemplate;
71
72   objectTemplate = GetImageAttributesTemplate( isolate);
73
74   // create an instance of the template
75   v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
76
77   // create the ImageAttributes wrapper
78   ImageAttributesWrapper* pointer =  new ImageAttributesWrapper( attributes, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
79
80   // assign the JavaScript object to the wrapper.
81   pointer->SetJavascriptObject( isolate, localObject );
82
83   return handleScope.Escape( localObject );
84 }
85
86 v8::Local<v8::ObjectTemplate> ImageAttributesWrapper::GetImageAttributesTemplate( v8::Isolate* isolate)
87 {
88   v8::EscapableHandleScope handleScope( isolate );
89   v8::Local<v8::ObjectTemplate> objectTemplate;
90
91
92   if( mImageAttributesTemplate.IsEmpty() )
93   {
94     objectTemplate = MakeImageAttributesTemplate( isolate );
95     mImageAttributesTemplate.Reset( isolate, objectTemplate );
96   }
97   else
98   {
99     // get the object template
100     objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, mImageAttributesTemplate );
101   }
102   return handleScope.Escape( objectTemplate );
103 }
104
105 v8::Handle<v8::ObjectTemplate> ImageAttributesWrapper::MakeImageAttributesTemplate( v8::Isolate* isolate )
106 {
107   v8::EscapableHandleScope handleScope( isolate );
108
109   v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
110
111   objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
112
113   // add our function properties
114   ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, ImageAttributeFunctionTable, ImageAttributeFunctionTableCount );
115
116   return handleScope.Escape( objTemplate );
117 }
118
119 void ImageAttributesWrapper::NewImageAttributes( const v8::FunctionCallbackInfo< v8::Value >& args)
120 {
121   v8::Isolate* isolate = args.GetIsolate();
122   v8::HandleScope handleScope( isolate);
123
124   if(!args.IsConstructCall())
125   {
126       DALI_SCRIPT_EXCEPTION(isolate, "ImageAttributes constructor called without 'new'");
127       return;
128   }
129
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 );
134 }
135
136
137 ImageAttributes& ImageAttributesWrapper::GetImageAttributes()
138 {
139   return mImageAttributes;
140 }
141
142
143 } // namespace V8Plugin
144
145 } // namespace Dali