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