Changes following "Remove Geometry scene object"
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / rendering / geometry-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 "geometry-wrapper.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.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>
27
28 namespace Dali
29 {
30
31 namespace V8Plugin
32 {
33
34 v8::Persistent<v8::ObjectTemplate> GeometryWrapper::mGeometryTemplate;
35
36 namespace // un-named name space
37 {
38
39 /**
40  * Contains a list of all functions that can be called
41  */
42 const ApiFunction GeometryFunctionTable[]=
43 {
44     /**************************************
45     * Geometry API (in order of Geometry.h)
46     **************************************/
47
48    { "AddVertexBuffer"             , GeometryApi::AddVertexBuffer },
49    { "GetNumberOfVertexBuffers"    , GeometryApi::GetNumberOfVertexBuffers },
50    { "RemoveVertexBuffer"          , GeometryApi::RemoveVertexBuffer },
51    { "SetIndexBuffer"              , GeometryApi::SetIndexBuffer },
52    { "SetGeometryType"             , GeometryApi::SetGeometryType },
53    { "GetGeometryType"             , GeometryApi::GetGeometryType },
54 };
55
56 const unsigned int GeometryFunctionTableCount = sizeof(GeometryFunctionTable)/sizeof(GeometryFunctionTable[0]);
57 } //un-named space
58
59
60 GeometryWrapper::GeometryWrapper( const Dali::Geometry& geometry, GarbageCollectorInterface& gc )
61 :  BaseWrappedObject(  BaseWrappedObject::GEOMETRY, gc )
62 {
63     mGeometry = geometry;
64 }
65
66 v8::Handle<v8::Object> GeometryWrapper::WrapGeometry(v8::Isolate* isolate, const Dali::Geometry& geometry )
67 {
68   v8::EscapableHandleScope handleScope( isolate );
69   v8::Local<v8::ObjectTemplate> objectTemplate;
70
71   objectTemplate = GetGeometryTemplate( isolate);
72
73   // create an instance of the template
74   v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
75
76   // create the Geometry wrapper
77   GeometryWrapper* pointer =  new GeometryWrapper( geometry, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
78
79   // assign the JavaScript object to the wrapper.
80   pointer->SetJavascriptObject( isolate, localObject );
81
82   return handleScope.Escape( localObject );
83 }
84
85 v8::Local<v8::ObjectTemplate> GeometryWrapper::GetGeometryTemplate( v8::Isolate* isolate)
86 {
87   v8::EscapableHandleScope handleScope( isolate );
88   v8::Local<v8::ObjectTemplate> objectTemplate;
89
90   if( mGeometryTemplate.IsEmpty() )
91   {
92     objectTemplate = MakeGeometryTemplate( isolate );
93     mGeometryTemplate.Reset( isolate, objectTemplate );
94   }
95   else
96   {
97     // get the object template
98     objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, mGeometryTemplate );
99   }
100   return handleScope.Escape( objectTemplate );
101 }
102
103 v8::Handle<v8::ObjectTemplate> GeometryWrapper::MakeGeometryTemplate( v8::Isolate* isolate )
104 {
105   v8::EscapableHandleScope handleScope( isolate );
106
107   v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
108
109   objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
110
111   // add our function properties
112   ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, GeometryFunctionTable, GeometryFunctionTableCount );
113
114   return handleScope.Escape( objTemplate );
115 }
116
117 void GeometryWrapper::NewGeometry( const v8::FunctionCallbackInfo< v8::Value >& args)
118 {
119   v8::Isolate* isolate = args.GetIsolate();
120   v8::HandleScope handleScope( isolate);
121
122   if(!args.IsConstructCall())
123   {
124       DALI_SCRIPT_EXCEPTION( isolate, "Geometry constructor called without 'new'");
125       return;
126   }
127   Dali::Geometry geometry = GeometryApi::New( args );
128
129   if(geometry)
130   {
131     v8::Local<v8::Object> localObject = WrapGeometry( isolate, geometry );
132     args.GetReturnValue().Set( localObject );
133   }
134 }
135
136
137 Geometry GeometryWrapper::GetGeometry()
138 {
139   return mGeometry;
140 }
141
142
143 } // namespace V8Plugin
144
145 } // namespace Dali