[dali_1.4.26] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / object / property-buffer-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 // CLASS HEADER
19 #include "property-buffer-api.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <object/property-buffer-wrapper.h>
24
25 namespace Dali
26 {
27
28 namespace V8Plugin
29 {
30
31 namespace // unnamed namespace
32 {
33
34 struct PropertyBufferParameters
35 {
36   PropertyBufferParameters()
37   {
38   }
39
40   PropertyBuffer NewPropertyBuffer()
41   {
42     return PropertyBuffer::New( mBufferFormat );
43   }
44
45   Property::Map mBufferFormat;
46 };
47
48 } // unnamed space
49
50 /**
51  * ## PropertyBuffer API
52  *
53  * PropertyBuffer is a handle to an object that contains a buffer of structured properties.
54  * It can be used to provide data to Geometry objects.
55  *
56  * ### Simple example
57  *
58  *```
59  *    var vertexFormat ={ "aPosition" : dali.PROPERTY_VECTOR2 };
60  *    var vertexData = [    0,     1,
61  *                      -0.95,  0.31,
62  *                      -0.59, -0.81,
63  *                       0.59, -0.81,
64  *                       0.95,  0.31];
65  *
66  *    var vertexDataArray = new Float32Array(vertexData.length);
67  *    vertexDataArray.set(vertexData, 0);
68  *    var vertices = new dali.PropertyBuffer(vertexFormat, 5);
69  *    vertices.setData(vertexDataArray);
70  *
71  *    var geometry = new dali.Geometry();
72  *    geometry.addVertexBuffer( vertices );
73  *```
74  * @class PropertyBuffer
75  */
76
77 PropertyBuffer PropertyBufferApi::GetPropertyBuffer( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
78 {
79   v8::HandleScope handleScope( isolate );
80
81   v8::Local<v8::Object> object = args.This();
82   v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
83   void* ptr = field->Value();
84
85   PropertyBufferWrapper* wrapper = static_cast< PropertyBufferWrapper *>(ptr);
86   return wrapper->GetPropertyBuffer();
87 }
88
89 PropertyBuffer PropertyBufferApi::GetPropertyBufferFromParams( int paramIndex,
90                           bool& found,
91                           v8::Isolate* isolate,
92                           const v8::FunctionCallbackInfo< v8::Value >& args )
93 {
94   found = false;
95
96   v8::HandleScope handleScope( isolate );
97   BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::PROPERTY_BUFFER, isolate, args );
98   if( wrappedObject )
99   {
100     found = true;
101     PropertyBufferWrapper* wrapper = static_cast< PropertyBufferWrapper *>(wrappedObject);
102     return wrapper->GetPropertyBuffer();
103   }
104   else
105   {
106     return PropertyBuffer();
107   }
108 }
109
110 /**
111  * Create a new PropertyBuffer
112  *
113  * PropertyBuffers contains a buffer of structured properties and can be
114  * used to provide data to Geometry objects.
115  *
116  * @constructor
117  * @for PropertyBuffer
118  * @method PropertyBuffer
119  * @param {Object} bufferFormat Map of names and types that describes the components of the buffer
120  * @param {integer} size The number of elements in the property buffer
121  * @return {Object} PropertyBuffer
122  * @example
123  *```
124  *    var bufferFormat = {
125  *                         "aPosition" : dali.PROPERTY_VECTOR2,
126  *                         "aTexCoord" : dali.PROPERTY_VECTOR2,
127  *                         "aHue"      : dali.PROPERTY_FLOAT,
128  *                       };
129  *
130  *    var buffer = new dali.PropertyBuffer(bufferFormat, 5);
131  *```
132  */
133 PropertyBuffer PropertyBufferApi::New( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
134 {
135   v8::HandleScope handleScope( isolate );
136
137   bool found( false );
138   Dali::Property::Map bufferFormat = V8Utils::GetPropertyMapParameter( PARAMETER_0, found, isolate, args);
139   if( !found || bufferFormat.Empty() )
140   {
141     DALI_SCRIPT_EXCEPTION( isolate, "invalid property map parameter" );
142     return PropertyBuffer();
143   }
144
145   return PropertyBuffer::New( bufferFormat );
146 }
147
148 /**
149  * Update the whole buffer information
150  *
151  * This function expects an array of data with the same format that was given
152  * in the construction, and the number of elements to be the same as the size
153  * of the buffer.
154  *
155  * @method setData
156  * @for PropertyBuffer
157  * @param {Float32Array} data The data that will be copied to the buffer.
158  * @example
159  *```
160  *   var vertexData = [    0,     1,
161  *                     -0.95,  0.31,
162  *                     -0.59, -0.81,
163  *                      0.59, -0.81,
164  *                      0.95,  0.31];
165  *
166  *   var vertexDataArray = new Float32Array(vertexData.length);
167  *   vertexDataArray.set(vertexData, 0);
168  *
169  *   propertyBuffer.setData( vertexDataArray, vertexData.length );
170  *```
171  */
172 void PropertyBufferApi::SetData( const v8::FunctionCallbackInfo< v8::Value >& args )
173 {
174   v8::Isolate* isolate = args.GetIsolate();
175   v8::HandleScope handleScope( isolate );
176
177   PropertyBuffer buffer = GetPropertyBuffer( isolate, args );
178
179   bool found( false );
180   void* data = V8Utils::GetArrayBufferViewParameter( PARAMETER_0, found, isolate, args);
181
182   if( ! found )
183   {
184     DALI_SCRIPT_EXCEPTION( isolate, "invalid data parameter" );
185   }
186   else
187   {
188     int size = V8Utils::GetIntegerParameter( PARAMETER_1, found, isolate, args, 0);
189     if( !found )
190     {
191       DALI_SCRIPT_EXCEPTION( isolate, "missing buffer size from param 1" );
192     }
193     else
194     {
195       buffer.SetData( data, size );
196     }
197   }
198 }
199
200 } // namespace V8Plugin
201
202 } // namespace Dali