2 * Copyright (c) 2016 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 "geometry-wrapper.h"
23 #include <rendering/geometry-api.h>
24 #include <shared/api-function.h>
25 #include <shared/object-template-helper.h>
26 #include <dali-wrapper.h>
34 v8::Persistent<v8::ObjectTemplate> GeometryWrapper::mGeometryTemplate;
36 namespace // un-named name space
40 * Contains a list of all functions that can be called
42 const ApiFunction GeometryFunctionTable[]=
44 /**************************************
45 * Geometry API (in order of Geometry.h)
46 **************************************/
48 { "AddVertexBuffer" , GeometryApi::AddVertexBuffer },
49 { "GetNumberOfVertexBuffers" , GeometryApi::GetNumberOfVertexBuffers },
50 { "RemoveVertexBuffer" , GeometryApi::RemoveVertexBuffer },
51 { "SetIndexBuffer" , GeometryApi::SetIndexBuffer },
52 { "SetType" , GeometryApi::SetType },
53 { "GetType" , GeometryApi::GetType },
56 const unsigned int GeometryFunctionTableCount = sizeof(GeometryFunctionTable)/sizeof(GeometryFunctionTable[0]);
60 GeometryWrapper::GeometryWrapper( const Dali::Geometry& geometry, GarbageCollectorInterface& gc )
61 : BaseWrappedObject( BaseWrappedObject::GEOMETRY, gc )
66 v8::Handle<v8::Object> GeometryWrapper::WrapGeometry(v8::Isolate* isolate, const Dali::Geometry& geometry )
68 v8::EscapableHandleScope handleScope( isolate );
69 v8::Local<v8::ObjectTemplate> objectTemplate;
71 objectTemplate = GetGeometryTemplate( isolate);
73 // create an instance of the template
74 v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
76 // create the Geometry wrapper
77 GeometryWrapper* pointer = new GeometryWrapper( geometry, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
79 // assign the JavaScript object to the wrapper.
80 pointer->SetJavascriptObject( isolate, localObject );
82 return handleScope.Escape( localObject );
85 v8::Local<v8::ObjectTemplate> GeometryWrapper::GetGeometryTemplate( v8::Isolate* isolate)
87 v8::EscapableHandleScope handleScope( isolate );
88 v8::Local<v8::ObjectTemplate> objectTemplate;
90 if( mGeometryTemplate.IsEmpty() )
92 objectTemplate = MakeGeometryTemplate( isolate );
93 mGeometryTemplate.Reset( isolate, objectTemplate );
97 // get the object template
98 objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, mGeometryTemplate );
100 return handleScope.Escape( objectTemplate );
103 v8::Handle<v8::ObjectTemplate> GeometryWrapper::MakeGeometryTemplate( v8::Isolate* isolate )
105 v8::EscapableHandleScope handleScope( isolate );
107 v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
109 objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
111 // add our function properties
112 ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, GeometryFunctionTable, GeometryFunctionTableCount );
114 return handleScope.Escape( objTemplate );
117 void GeometryWrapper::NewGeometry( const v8::FunctionCallbackInfo< v8::Value >& args)
119 v8::Isolate* isolate = args.GetIsolate();
120 v8::HandleScope handleScope( isolate);
122 if(!args.IsConstructCall())
124 DALI_SCRIPT_EXCEPTION( isolate, "Geometry constructor called without 'new'");
127 Dali::Geometry geometry = GeometryApi::New( args );
131 v8::Local<v8::Object> localObject = WrapGeometry( isolate, geometry );
132 args.GetReturnValue().Set( localObject );
137 Geometry GeometryWrapper::GetGeometry()
143 } // namespace V8Plugin