af216bd4a45107cfef4beb86a7721c5cc0084474
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / rendering / geometry-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
19 // CLASS HEADER
20 #include "geometry-api.h"
21
22 // EXTERNAL INCLUDES
23 #include <dali/public-api/object/type-registry.h>
24
25 // INTERNAL INCLUDES
26 #include <v8-utils.h>
27 #include <rendering/geometry-wrapper.h>
28 #include <object/property-buffer-api.h>
29
30 namespace Dali
31 {
32
33 namespace V8Plugin
34 {
35
36 /**
37  * ## Geometry API
38  *
39  * Geometry is handle to an object that can be used to define a geometric elements.
40  *
41  * @class Geometry
42  * @extends Handle
43  */
44
45 Geometry GeometryApi::GetGeometry( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
46 {
47   v8::HandleScope handleScope( isolate );
48
49   v8::Local<v8::Object> object = args.This();
50   v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
51   void* ptr = field->Value();
52
53   GeometryWrapper* wrapper = static_cast< GeometryWrapper *>(ptr);
54   return wrapper->GetGeometry();
55 }
56
57 Geometry GeometryApi::GetGeometryFromParams( int paramIndex,
58                                              bool& found,
59                                              v8::Isolate* isolate,
60                                              const v8::FunctionCallbackInfo< v8::Value >& args )
61 {
62   found = false;
63
64   v8::HandleScope handleScope( isolate );
65   BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::GEOMETRY, isolate, args );
66   if( wrappedObject )
67   {
68     found = true;
69     GeometryWrapper* wrapper = static_cast< GeometryWrapper *>(wrappedObject);
70     return wrapper->GetGeometry();
71   }
72   else
73   {
74     return Geometry();
75   }
76 }
77
78 /**
79  * Create a new geometry object.
80  *
81  * @constructor
82  * @method Geometry
83  * @for Geometry
84  * @return {Object} Geometry
85  */
86 Geometry GeometryApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
87 {
88   return Geometry::New();
89 }
90
91 /**
92  * Add a PropertyBuffer to be used as source of geometry vertices
93  *
94  * @method addVertexBuffer
95  * @for Geometry
96  * @param {Object} vertexBuffer PropertyBuffer to be used as source of geometry vertices
97  * @return {interger} Index of the newly added buffer, can be used with RemoveVertexBuffer
98  *                    to remove this buffer if no longer required
99  * @example
100  *```
101  *    var vertexFormat ={ "aPosition" : dali.PROPERTY_VECTOR2 };
102  *    var vertexData = [    0,     1,
103  *                      -0.95,  0.31,
104  *                      -0.59, -0.81,
105  *                       0.59, -0.81,
106  *                       0.95,  0.31];
107  *
108  *    var vertexDataArray = new Float32Array(vertexData.length);
109  *    vertexDataArray.set(vertexData, 0);
110  *    var vertices = new dali.PropertyBuffer(vertexFormat, 5);
111  *    vertices.setData(vertexDataArray);
112  *
113  *    var geometry = new dali.Geometry();
114  *    geometry.addVertexBuffer( vertices );
115  *```
116  */
117 void GeometryApi::AddVertexBuffer( const v8::FunctionCallbackInfo< v8::Value >& args )
118 {
119   v8::Isolate* isolate = args.GetIsolate();
120   v8::HandleScope handleScope( isolate );
121
122   Geometry geometry = GetGeometry( isolate, args );
123
124   bool found( false );
125   PropertyBuffer vertexBuffer = PropertyBufferApi::GetPropertyBufferFromParams( 0, found, isolate, args );
126   if( !found )
127   {
128     DALI_SCRIPT_EXCEPTION( isolate, "invalid property buffer parameter" );
129   }
130   else
131   {
132     args.GetReturnValue().Set( v8::Integer::New( isolate, geometry.AddVertexBuffer(vertexBuffer) ) );
133   }
134 }
135
136 /**
137  * Retrieve the number of vertex buffers that have been added to this geometry
138  *
139  * @method getNumberOfVertexBuffers
140  * @for Geometry
141  * @return {integer} Number of vertex buffers that have been added to this geometry
142  */
143 void GeometryApi::GetNumberOfVertexBuffers( const v8::FunctionCallbackInfo<v8::Value>& args )
144 {
145   v8::Isolate* isolate = args.GetIsolate();
146   v8::HandleScope handleScope( isolate );
147
148   Geometry geometry = GetGeometry( isolate, args );
149
150   args.GetReturnValue().Set( v8::Integer::New( isolate, geometry.GetNumberOfVertexBuffers() ) );
151 }
152
153 /**
154  * Remove a vertex buffer
155  *
156  * @method removeVertexBuffer
157  * @for Geometry
158  * @param {integer} index Index to the vertex buffer to remove,
159  *                  which must be between 0 and getNumberOfVertexBuffers()
160  */
161 void GeometryApi::RemoveVertexBuffer( const v8::FunctionCallbackInfo< v8::Value >& args )
162 {
163   v8::Isolate* isolate = args.GetIsolate();
164   v8::HandleScope handleScope( isolate );
165
166   Geometry geometry = GetGeometry( isolate, args );
167
168   bool found( false );
169   int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
170   if( !found )
171   {
172     DALI_SCRIPT_EXCEPTION( isolate, "missing index from param 0" );
173   }
174   else
175   {
176     geometry.RemoveVertexBuffer(static_cast<std::size_t>(index));
177   }
178 }
179
180 /**
181  * Set a PropertyBuffer to be used as a source of indices for the geometry
182  *
183  * This buffer is required to have exactly one component and it must be of the
184  * type dali.PROPERTY_INTEGER
185  *
186  * By setting this buffer it will cause the geometry to be rendered using indices.
187  * To unset call SetIndexBuffer with an empty handle.
188  *
189  * @method setIndexBuffer
190  * @for Geometry
191  * @param {Uint32Array} data The data that will be copied to the buffer
192  *
193  * @example
194  * var indexData = [0, 1, 1, 2, 2, 3, 3, 4, 4, 0];
195  * var indexDataArray = new Uint32Array(indexData.length);
196  * indexDataArray.set(indexData, 0);
197  *
198  * geometry.SetIndexBuffer( indexDataArray, indexDataArray.length );
199  *```
200  */
201 void GeometryApi::SetIndexBuffer( const v8::FunctionCallbackInfo<v8::Value>& args )
202 {
203   v8::Isolate* isolate = args.GetIsolate();
204   v8::HandleScope handleScope( isolate );
205
206   Geometry geometry = GetGeometry( isolate, args );
207
208   bool found( false );
209   void* data = V8Utils::GetArrayBufferViewParameter( PARAMETER_0, found, isolate, args);
210
211   if( ! found )
212   {
213     DALI_SCRIPT_EXCEPTION( isolate, "invalid data parameter" );
214   }
215   else
216   {
217     unsigned int size = V8Utils::GetIntegerParameter( PARAMETER_1, found, isolate, args, 0);
218     if( !found )
219     {
220       DALI_SCRIPT_EXCEPTION( isolate, "missing buffer size from param 1" );
221     }
222     else
223     {
224       Dali::Vector<unsigned short> indices;
225       indices.Resize( size );
226       unsigned int* indexData = static_cast<unsigned int*>(data);
227       for( size_t i(0); i<size; ++i )
228       {
229         indices[i] = indexData[i];
230       }
231       geometry.SetIndexBuffer( &indices[0], size );
232     }
233   }
234 }
235
236 /**
237  * Set the type of primitives this geometry contains
238  *
239  * @method setGeometryType
240  * @for Geometry
241  * @param {integer} geometryType Type of primitives this geometry contains
242  * @example
243  *      // geometry type is one of the following
244  *      dali.GEOMETRY_POINTS
245  *      dali.GEOMETRY_LINES
246  *      dali.GEOMETRY_LINE_LOOP
247  *      dali.GEOMETRY_LINE_STRIP
248  *      dali.GEOMETRY_TRIANGLES
249  *      dali.GEOMETRY_TRIANGLE_FAN
250  *      dali.GEOMETRY_TRIANGLE_STRIP
251  *
252  *      geometry.SetGeometryType( dali.GEOMETRY_LINES );
253  */
254 void GeometryApi::SetGeometryType( const v8::FunctionCallbackInfo< v8::Value >& args )
255 {
256   v8::Isolate* isolate = args.GetIsolate();
257   v8::HandleScope handleScope( isolate );
258
259   Geometry geometry = GetGeometry( isolate, args );
260
261   bool found( false );
262   int geometryType = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
263   if( !found )
264   {
265     DALI_SCRIPT_EXCEPTION( isolate, "missing geometryType from param 0" );
266   }
267   else
268   {
269     geometry.SetGeometryType(static_cast<Geometry::GeometryType>(geometryType));
270   }
271 }
272
273 /**
274  * Get the type of primitives this geometry contains
275  *
276  * @method getGeometryType
277  * @for Geometry
278  * @return {integer} Type of primitives this geometry contains
279  * @example
280  *      // returns one of the following
281  *      dali.GEOMETRY_POINTS
282  *      dali.GEOMETRY_LINES
283  *      dali.GEOMETRY_LINE_LOOP
284  *      dali.GEOMETRY_LINE_STRIP
285  *      dali.GEOMETRY_TRIANGLES
286  *      dali.GEOMETRY_TRIANGLE_FAN
287  *      dali.GEOMETRY_TRIANGLE_STRIP
288  */
289 void GeometryApi::GetGeometryType( const v8::FunctionCallbackInfo< v8::Value >& args )
290 {
291   v8::Isolate* isolate = args.GetIsolate();
292   v8::HandleScope handleScope( isolate );
293
294   Geometry geometry = GetGeometry( isolate, args );
295
296   args.GetReturnValue().Set( v8::Integer::New( isolate, geometry.GetGeometryType() ) );
297 }
298
299 } // namespace V8Plugin
300
301 } // namespace Dali