2 * Copyright (c) 2015 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.
18 #include "property-value-wrapper.h"
21 #include <dali/integration-api/debug.h>
22 #include <dali/public-api/object/property-array.h>
26 #include <dali-wrapper.h>
39 Dali::Property::Type propertyType;
40 BaseWrappedObject::Type wrappedType;
43 * lookup table to convert between dali property types and wrapped types
44 * Wrapped types includes actors / timers / render tasks, hence we can't just
45 * use the dali property types to encode what a wrapped pointer contains
47 const WrapTypeInfo WrapTypeInfoLookup[]=
49 { Dali::Property::VECTOR2, BaseWrappedObject::VECTOR2 },
50 { Dali::Property::VECTOR3, BaseWrappedObject::VECTOR3 },
51 { Dali::Property::VECTOR4, BaseWrappedObject::VECTOR4 },
52 { Dali::Property::RECTANGLE, BaseWrappedObject::RECTANGLE },
53 { Dali::Property::ROTATION, BaseWrappedObject::ROTATION },
54 { Dali::Property::MATRIX, BaseWrappedObject::MATRIX },
55 { Dali::Property::MATRIX3, BaseWrappedObject::MATRIX3 },
59 const unsigned int WrapTypeLookupCount = sizeof( WrapTypeInfoLookup ) / sizeof( WrapTypeInfo );
61 BaseWrappedObject::Type GetWrappedType( Dali::Property::Type type )
63 for( unsigned int i = 0; i < WrapTypeLookupCount; ++i)
65 if( WrapTypeInfoLookup[ i ].propertyType == type )
67 return WrapTypeInfoLookup[ i ].wrappedType;
70 DALI_LOG_ERROR("GetWrappedType failed \n");
71 return BaseWrappedObject::UNKNOWN;
74 // Templates for setting members on propertys, like, x,y,z on a vector
75 template <typename T, typename S>
76 void SetX( Dali::Property::Value &value, S& v)
78 // to set a property value, we need to read into a temporary
79 // then assign it back.
80 // E.g. Vector2 temp = value.Get<Vector2>();
83 T tmp( value.Get<T>() );
88 template <typename T, typename S>
89 void SetY( Dali::Property::Value &value, S& v)
91 T tmp = value.Get<T>();
96 template <typename T, typename S>
97 void SetZ( Dali::Property::Value &value, S& v)
99 T tmp = value.Get<T>();
104 template <typename T, typename S>
105 void SetW( Dali::Property::Value &value, S& v)
107 T tmp = value.Get<T>();
111 template <typename T, typename S>
112 void SetWidth( Dali::Property::Value &value, S& v)
114 T tmp = value.Get<T>();
119 template <typename T, typename S>
120 void SetHeight( Dali::Property::Value &value, S& v)
122 T tmp = value.Get<T>();
127 v8::Handle< v8::Value > GetV8ValueFrom( v8::Isolate* isolate,
128 const Dali::Property::Value& value,
129 const std::string& field)
131 v8::EscapableHandleScope handleScope( isolate );
132 v8::Local<v8::Value> ret;
134 Dali::Property::Type type = value.GetType();
135 std::string objectType="Unknown type";
137 // simple property values are handled by the actor wrapper so we just handle property values with sub fields
140 case Dali::Property::VECTOR2:
142 objectType = "Vector2";
144 if( field == "x" || field == "width" )
146 Dali::Vector2 v2 = value.Get<Dali::Vector2>();
147 ret = v8::Number::New( isolate, v2.x);
149 else if(field == "y" || field == "height")
151 Dali::Vector2 v2 = value.Get<Dali::Vector2>();
152 ret = v8::Number::New( isolate, v2.y);
157 case Dali::Property::VECTOR3:
159 objectType = "Vector3";
161 if(field == "x" || field == "width" || field == "r")
163 Dali::Vector3 v3 = value.Get<Dali::Vector3>();
164 ret = v8::Number::New( isolate, v3.x);
166 else if(field == "y" || field == "height" || field == "g")
168 Dali::Vector3 v3 = value.Get<Dali::Vector3>();
169 ret = v8::Number::New( isolate, v3.y);
171 else if(field == "z" || field == "depth" || field == "b")
173 Dali::Vector3 v3 = value.Get<Dali::Vector3>();
174 ret = v8::Number::New( isolate, v3.z);
178 case Dali::Property::VECTOR4:
180 objectType = "vector4";
182 if(field == "x" || field == "s" || field == "r")
184 Dali::Vector4 v4 = value.Get<Dali::Vector4>();
185 ret = v8::Number::New( isolate, v4.x);
187 else if(field == "y" || field == "g" || field == "t")
189 Dali::Vector4 v4 = value.Get<Dali::Vector4>();
190 ret = v8::Number::New( isolate, v4.y);
192 else if(field == "z" || field == "r" || field == "b")
194 Dali::Vector4 v4 = value.Get<Dali::Vector4>();
195 ret = v8::Number::New( isolate, v4.z);
197 else if(field == "w" || field == "a" || field == "q")
199 Dali::Vector4 v4 = value.Get<Dali::Vector4>();
200 ret = v8::Number::New( isolate, v4.w);
205 case Dali::Property::ROTATION:
207 objectType = "rotation";
211 Dali::Vector4 v4 = value.Get<Dali::Quaternion>().EulerAngles();
212 ret = v8::Number::New( isolate, v4.z);
214 else if(field == "pitch")
216 Dali::Vector4 v4 = value.Get<Dali::Quaternion>().EulerAngles();
217 ret = v8::Number::New( isolate, v4.x);
219 else if(field == "yaw")
221 Dali::Vector4 v4 = value.Get<Dali::Quaternion>().EulerAngles();
222 ret = v8::Number::New( isolate, v4.y);
225 else if(field == "axis")
229 value.Get<Dali::Quaternion>().ToAxisAngle(axis, angle);
230 ret = PropertyValueWrapper::WrapDaliProperty( isolate , Dali::Property::Value( axis ) );
232 else if(field == "angle")
236 value.Get<Dali::Quaternion>().ToAxisAngle(axis, angle);
237 ret = PropertyValueWrapper::WrapDaliProperty( isolate , Dali::Property::Value( angle ) );
241 case Dali::Property::RECTANGLE:
243 objectType = "rectangle";
247 Dali::Rect<int> r = value.Get<Dali::Rect<int> >();
248 ret = v8::Number::New( isolate, r.x);
250 else if(field == "y")
252 Dali::Rect<int> r = value.Get<Dali::Rect<int> >();
253 ret = v8::Number::New( isolate, r.y);
255 else if(field == "width")
257 Dali::Rect<int> r = value.Get<Dali::Rect<int> >();
258 ret = v8::Number::New( isolate, r.width);
260 else if(field == "height")
262 Dali::Rect<int> r = value.Get<Dali::Rect<int> >();
263 ret = v8::Number::New( isolate, r.height);
270 // will trigger a type error as ret.IsEmpty() == true
278 DALI_SCRIPT_EXCEPTION( isolate, "Cannot get property with field " + objectType+ ":" + field );
280 return handleScope.Escape(ret);
285 * Setting a field on a property, e.g. Position.x = 5 or Rect.Width = 100
286 * The only property field that supports a struct is rotation.axis
288 void SetFromV8Value(v8::Isolate* isolate,
289 Dali::Property::Value& value,
290 const std::string& field,
291 v8::Local<v8::Value> v8Value )
293 v8::EscapableHandleScope handleScope( isolate );
295 Dali::Property::Type type = value.GetType();
300 // all are numbers at the moment
301 if( v8Value->IsNumber() )
303 asFloat = static_cast<float>(v8Value->NumberValue());
304 asInt = static_cast<int>(asFloat);
308 // if the v8 value isn't a number, then then only Dali object we support is a Vector3 part of a rotation
309 if( ! (type == Dali::Property::ROTATION && field == "axis") )
311 DALI_SCRIPT_EXCEPTION(isolate,std::string("Cannot set property with field (value is not a number):") + field);
316 // simple property values are handled by the actor wrapper so we just handle property values with sub fields
319 case Dali::Property::VECTOR2:
321 if(field == "x" || field == "width")
323 SetX<Dali::Vector2>(value, asFloat);
325 else if(field == "y" || field == "height")
327 SetY<Dali::Vector2>(value, asFloat);
331 DALI_SCRIPT_EXCEPTION(isolate, std::string("Cannot set property with field (unknown type):") + field);
336 case Dali::Property::VECTOR3:
338 if(field == "x" || field == "width" || field == "r")
340 SetX<Dali::Vector3>(value, asFloat);
342 else if(field == "y" || field == "height" || field == "g")
344 SetY<Dali::Vector3>(value, asFloat);
346 else if(field == "z" || field == "depth" || field == "b")
348 SetZ<Dali::Vector3>(value, asFloat);
352 DALI_SCRIPT_EXCEPTION(isolate, std::string("Cannot set property with field (unknown type):") + field);
357 case Dali::Property::VECTOR4:
359 if(field == "x" || field == "s" || field == "r")
361 SetX<Dali::Vector4>(value, asFloat);
363 else if(field == "y" || field == "g" || field == "t")
365 SetY<Dali::Vector4>(value, asFloat);
367 else if(field == "z" || field == "r" || field == "b")
369 SetZ<Dali::Vector4>(value, asFloat);
371 else if(field == "w" || field == "a" || field == "q")
373 SetW<Dali::Vector4>(value, asFloat);
377 DALI_SCRIPT_EXCEPTION( isolate, std::string("Cannot set property with field (unknown type):") + field);
382 case Dali::Property::ROTATION:
386 Dali::Vector4 v4 = value.Get<Dali::Quaternion>().EulerAngles();
388 value = Dali::Quaternion( Radian(Degree(v4.x)), Radian(Degree(v4.y)), Radian(Degree(v4.z)) );
390 else if(field == "pitch")
392 Dali::Vector4 v4 = value.Get<Dali::Quaternion>().EulerAngles();
394 value = Dali::Quaternion( Radian(Degree(v4.x)), Radian(Degree(v4.y)), Radian(Degree(v4.z)) );
396 else if(field == "yaw")
398 Dali::Vector4 v4 = value.Get<Dali::Quaternion>().EulerAngles();
400 value = Dali::Quaternion( Radian(Degree(v4.x)), Radian(Degree(v4.y)), Radian(Degree(v4.z)) );
402 else if(field == "axis")
406 value.Get<Dali::Quaternion>().ToAxisAngle(axis, angle);
408 if( v8Value->IsObject() )
410 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast( v8Value );
412 PropertyValueWrapper* wrapper = PropertyValueWrapper::Unwrap( isolate, object );
413 Dali::Property::Value v3 = wrapper->GetValue();
415 if(v3.GetType() == Dali::Property::VECTOR3)
417 value = Dali::Quaternion(angle, v3.Get<Dali::Vector3>());
421 DALI_SCRIPT_EXCEPTION( isolate, "Axis expects a Vector3:");
427 else if(field == "angle")
431 value.Get<Dali::Quaternion>().ToAxisAngle( axis, angle );
432 value = Dali::Quaternion( Degree( asFloat ), axis );
436 DALI_SCRIPT_EXCEPTION( isolate, std::string("Cannot set property with field (unknown type):") + field);
441 case Dali::Property::RECTANGLE:
445 SetX<Dali::Rect<int> >(value, asInt);
447 else if(field == "y")
449 SetY<Dali::Rect<int> >(value, asInt);
451 else if(field == "width")
453 SetWidth<Dali::Rect<int> >(value, asInt);
455 else if(field == "height")
457 SetHeight<Dali::Rect<int> >(value, asInt);
461 DALI_SCRIPT_EXCEPTION( isolate, std::string("Cannot set property with field (unknown type):") + field);
468 DALI_SCRIPT_EXCEPTION( isolate, std::string("Cannot set property with field (unknown type):") + field);
478 bool IsPrimitive( const Dali::Property::Value &value )
480 switch( value.GetType() )
482 case Dali::Property::BOOLEAN:
483 case Dali::Property::INTEGER:
484 case Dali::Property::STRING:
485 case Dali::Property::FLOAT:
495 v8::Local<v8::Object> CreateJavaScriptPrimitive( v8::Isolate* isolate, const Dali::Property::Value &value )
497 v8::EscapableHandleScope handleScope( isolate );
499 v8::Local<v8::Value> v8Value;
501 switch( value.GetType() )
503 case Dali::Property::FLOAT:
505 v8Value = v8::Number::New( isolate, value.Get<float>() );
508 case Dali::Property::BOOLEAN:
510 v8Value = v8::Boolean::New( isolate, value.Get<bool>());
513 case Dali::Property::INTEGER:
515 v8Value = v8::Integer::New( isolate, value.Get<int>());
518 case Dali::Property::STRING:
520 std::string string = value.Get< std::string >();
521 v8Value = v8::String::NewFromUtf8( isolate, string.c_str());
526 DALI_LOG_ERROR("Primitive mismatch \n");
530 v8::Local<v8::Object> ret = v8Value->ToObject();
532 return handleScope.Escape( ret );
536 } // un-named namespace
540 Dali::Property::Value PropertyValueWrapper::VectorOrMatrixFromV8Array( v8::Isolate* isolate, const v8::Local<v8::Value>& v8Value)
542 v8::EscapableHandleScope handleScope( isolate );
544 Dali::Property::Value ret;
546 if( !v8Value->IsArray() )
550 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast( v8Value );//v8::Array::Cast( *v8Value);
551 uint32_t len = array->Length();
552 float out[16] = {0.f, 0.f, 0.f, 0.f,0.f, 0.f, 0.f, 0.f,0.f, 0.f, 0.f, 0.f,0.f, 0.f, 0.f, 0.f};
553 const unsigned int size = sizeof(out) / sizeof(float);
554 for( uint32_t i=0; i < (len > size? size:len); ++i)
556 v8::Handle<v8::Value> entry = array->Get( i);// v8::Integer::New( isolate, i));
557 if( V8Utils::IsNumberPrimitiveOrObject( entry ) )
559 out[i] = V8Utils::GetNumberValue( isolate, entry );
560 //printf("entry is a number %d , entry is a int %f \n",(int)i , out[i] );
565 ret = Dali::Matrix( out );
569 ret = Dali::Matrix3( out[0],out[1],out[2],out[3],out[4],out[5],out[6],out[7],out[8] );
573 ret = Dali::Vector4(out[0], out[1], out[2], out[3]);
577 ret = Dali::Vector3(out[0], out[1], out[2]);
581 ret = Dali::Vector2(out[0], out[1]);
591 Dali::Property::Value PropertyValueWrapper::ArrayFromV8Array( v8::Isolate* isolate, const v8::Local<v8::Value>& v8Value)
593 v8::EscapableHandleScope handleScope( isolate );
595 if( !v8Value->IsArray() )
597 return Dali::Property::Value();
600 //Cast v8::Value to v8::Array
601 v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast( v8Value );
602 uint32_t elementCount = v8Array->Length();
603 Dali::Property::Array data;
604 data.Reserve( elementCount );
606 for( uint32_t i(0); i<elementCount; ++i )
608 //Get Property::Value for each element in the array
609 data.PushBack( V8Utils::GetPropertyValueFromObject( bFound, isolate, v8Array->Get(i) ) );
612 //return the Property::Value
613 return Dali::Property::Value( data );
616 v8::Persistent<v8::ObjectTemplate> PropertyValueWrapper::mTemplatePrimitive;
619 PropertyValueWrapper::~PropertyValueWrapper()
624 PropertyValueWrapper::PropertyValueWrapper( const Dali::Property::Value& value, GarbageCollectorInterface& gc )
625 : BaseWrappedObject( GetWrappedType( value.GetType() ), gc ),
630 Dali::Property::Value PropertyValueWrapper::ExtractPropertyValue( v8::Isolate* isolate, v8::Local< v8::Value> v8Value, Dali::Property::Type type)
632 v8::HandleScope handleScope( isolate);
634 Dali::Property::Value daliPropertyValue;
636 // check if it's a dali object (vector, rect etc)
637 if( v8Value->IsObject() )
639 v8::Local<v8::Object> object = v8::Handle<v8::Object>::Cast(v8Value);
640 if( object->InternalFieldCount() == BaseWrappedObject::FIELD_COUNT )
642 if( BaseWrappedObject::IsWrappedType( isolate, object, GetWrappedType(type) ) )
644 PropertyValueWrapper* propertyWrapper = Unwrap( isolate, object );
645 return propertyWrapper->GetValue();
650 // check if it's a javascript Array
651 Dali::Property::Value array = VectorOrMatrixFromV8Array( isolate, v8Value );
655 case Dali::Property::BOOLEAN:
657 if( V8Utils::IsBooleanPrimitiveOrObject( v8Value ) )
659 daliPropertyValue = Dali::Property::Value( V8Utils::GetBooleanValue( isolate, v8Value));
663 case Dali::Property::FLOAT:
665 if( V8Utils::IsNumberPrimitiveOrObject( v8Value ) )
667 daliPropertyValue = Dali::Property::Value( V8Utils::GetNumberValue( isolate, v8Value) );
672 case Dali::Property::INTEGER: // todo is it wise to distinguish int from float in javascript?!
674 if( v8Value->IsInt32() )
676 daliPropertyValue = Dali::Property::Value( v8Value->Int32Value() ) ;//static_cast<int>( V8Utils::GetNumberValue( isolate, v8Value) ));
678 else if( V8Utils::IsStringPrimitiveOrObject( v8Value) ) // Take string as value for properties that internally convert the string to an enum
680 daliPropertyValue = Dali::Property::Value( V8Utils::GetStringValue( isolate, v8Value) );
684 case Dali::Property::STRING:
686 if( V8Utils::IsStringPrimitiveOrObject( v8Value) )
688 daliPropertyValue = Dali::Property::Value( V8Utils::GetStringValue( isolate, v8Value) );
694 case Dali::Property::VECTOR2:
696 if( array.GetType() == Dali::Property::VECTOR2 )
698 daliPropertyValue = array;
702 case Dali::Property::VECTOR3:
704 if( array.GetType() == Dali::Property::VECTOR3)
706 daliPropertyValue = array;
710 case Dali::Property::VECTOR4:
713 if( array.GetType() == Dali::Property::VECTOR4)
715 daliPropertyValue = array;
719 case Dali::Property::RECTANGLE:
721 if( array.GetType() == Dali::Property::VECTOR4)
723 Vector4 vec4( array.Get<Vector4>());
724 daliPropertyValue = Property::Value(Rect<int>( vec4.x,vec4.y, vec4.z, vec4.w ));
728 case Dali::Property::ROTATION:
730 if( array.GetType() == Dali::Property::VECTOR4)
732 // v4 treated as axis angle
733 Dali::Vector4 v = array.Get<Dali::Vector4>();
734 daliPropertyValue = Dali::AngleAxis(Dali::Degree(v[0]), Dali::Vector3(v[1], v[2], v[3]));
736 else if(array.GetType() == Dali::Property::VECTOR3)
738 // v3 treated as euler in degrees
739 Dali::Vector3 v = array.Get<Dali::Vector3>();
740 daliPropertyValue = Dali::Quaternion( Radian(Degree(v[0])), Radian(Degree(v[1])), Radian(Degree(v[2])) );
744 case Dali::Property::MATRIX:
746 Dali::Matrix mat = array.Get<Dali::Matrix>();
747 daliPropertyValue = mat;
750 case Dali::Property::MATRIX3:
752 Dali::Matrix3 mat = array.Get<Dali::Matrix3>();
753 daliPropertyValue = mat;
756 case Dali::Property::ARRAY:
758 daliPropertyValue = ArrayFromV8Array( isolate, v8Value );
761 case Dali::Property::MAP:
763 if( v8Value->IsObject() )
765 v8::Local<v8::Object> object = v8::Handle<v8::Object>::Cast(v8Value);
766 Dali::Property::Map propertyMap = V8Utils::GetPropertyMapFromObject(isolate, object);
767 daliPropertyValue = Dali::Property::Value( propertyMap );
769 else if( V8Utils::IsStringPrimitiveOrObject( v8Value) )
771 // There are special cases where a property with property map type can accept a string value,
772 // so we do the additional check here.
773 daliPropertyValue = Dali::Property::Value( V8Utils::GetStringValue( isolate, v8Value) );
778 case Dali::Property::NONE:
785 return daliPropertyValue;
788 Dali::Property::Value PropertyValueWrapper::ExtractPropertyValue( v8::Isolate* isolate, v8::Local< v8::Value> v8Value)
790 v8::HandleScope handleScope( isolate);
792 Dali::Property::Value daliPropertyValue;
794 // Check if it's a javascript Array
795 Dali::Property::Value array = VectorOrMatrixFromV8Array( isolate, v8Value );
797 if( V8Utils::IsBooleanPrimitiveOrObject( v8Value ) )
799 daliPropertyValue = Dali::Property::Value( V8Utils::GetBooleanValue( isolate, v8Value));
801 else if( V8Utils::IsNumberPrimitiveOrObject( v8Value ) )
803 daliPropertyValue = Dali::Property::Value( V8Utils::GetNumberValue( isolate, v8Value) );
805 else if( v8Value->IsInt32() )
807 daliPropertyValue = Dali::Property::Value( v8Value->Int32Value() ) ;
809 else if( V8Utils::IsStringPrimitiveOrObject( v8Value) )
811 daliPropertyValue = Dali::Property::Value( V8Utils::GetStringValue( isolate, v8Value) );
813 else if( array.GetType() == Dali::Property::VECTOR2
814 || array.GetType() == Dali::Property::VECTOR3
815 || array.GetType() == Dali::Property::VECTOR4 )
817 daliPropertyValue = array;
819 else if( array.GetType() == Dali::Property::MATRIX )
821 Dali::Matrix mat = array.Get<Dali::Matrix>();
822 daliPropertyValue = mat;
824 else if( array.GetType() == Dali::Property::MATRIX3 )
826 Dali::Matrix3 mat = array.Get<Dali::Matrix3>();
827 daliPropertyValue = mat;
829 else if( array.GetType() == Dali::Property::ARRAY )
831 daliPropertyValue = ArrayFromV8Array( isolate, v8Value );
833 else if( v8Value->IsObject() )
835 // Assume this is a property map
836 v8::Local<v8::Object> object = v8::Handle<v8::Object>::Cast(v8Value);
837 Dali::Property::Map propertyMap = V8Utils::GetPropertyMapFromObject(isolate, object);
838 daliPropertyValue = Dali::Property::Value( propertyMap );
841 return daliPropertyValue;
844 void PropertyValueWrapper::NewRotation( const v8::FunctionCallbackInfo< v8::Value >& args)
846 v8::Isolate* isolate = args.GetIsolate();
847 v8::HandleScope handleScope( isolate);
849 if(!args.IsConstructCall())
851 DALI_SCRIPT_EXCEPTION( isolate, "Rotation constructor called without 'new'");
854 v8::Local<v8::Object > object;
856 float v[4] = {0.f,0.f,0.f,0.f};
858 bool foundAllArguments(false);
859 V8Utils::ReadFloatArguments( foundAllArguments, v, 4, args, 0.f );
861 int length = args.Length();
863 // if length = 4 create AngleAxis, else create Quaternion
867 const Dali::AngleAxis axis( Degree( v[0] ), Vector3(v[1], v[2], v[3]) );
868 object = WrapDaliProperty( isolate, Dali::Property::Value( axis ) );
870 else if( length > 2 )
872 object = WrapDaliProperty( isolate, Dali::Quaternion( Degree(v[0]), Degree(v[1]), Degree(v[2]) ) );
876 const Dali::Quaternion quaternion( Dali::Quaternion( Dali::ANGLE_0, Dali::Vector3::YAXIS));
877 object = WrapDaliProperty( isolate, quaternion );
880 args.GetReturnValue().Set( object );
884 void PropertyValueWrapper::NewMatrix( const v8::FunctionCallbackInfo< v8::Value >& args )
886 v8::Isolate* isolate = args.GetIsolate();
887 v8::HandleScope handleScope( isolate);
889 if(!args.IsConstructCall())
891 DALI_SCRIPT_EXCEPTION( isolate, "Rotation constructor called without 'new'");
894 v8::Local<v8::Object > object;
896 // we're constructing either a 4x4 Dali::Matrix or a 3x3 Dali::Matrix
897 // we're expecting an array of floats
898 // 4x4 = m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33
899 // 3x3 or m00, m01, m02, m10, m11, m12,m20, m21, m22, m30, m31, m32,
900 bool foundAllArguments( false );
902 if( args.Length() == 16 )
905 V8Utils::ReadFloatArguments( foundAllArguments, v, 16, args, 0.f );
906 if(! foundAllArguments )
908 DALI_SCRIPT_EXCEPTION( isolate, "invalid Matrix parameters");
911 Dali::Matrix matrix( v[0] );
912 object = WrapDaliProperty( isolate , Dali::Property::Value( matrix ) );
914 else if ( args.Length() == 9 )
917 V8Utils::ReadFloatArguments( foundAllArguments, v, 9, args, 0.f );
918 if(! foundAllArguments )
920 DALI_SCRIPT_EXCEPTION( isolate, "invalid Matrix parameters");
923 Dali::Matrix3 matrix( v[0], v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8] );
924 object = WrapDaliProperty( isolate , Dali::Property::Value( matrix ) );
926 args.GetReturnValue().Set( object );
931 v8::Handle<v8::ObjectTemplate> PropertyValueWrapper::MakeDaliPropertyTemplate( v8::Isolate* isolate )
933 v8::EscapableHandleScope handleScope( isolate );
935 v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
937 objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
939 // property handle intercepts property getters and setters
940 objTemplate->SetNamedPropertyHandler( PropertyGet, PropertySet);
942 return handleScope.Escape( objTemplate );
946 v8::Handle<v8::Object> PropertyValueWrapper::WrapDaliProperty(v8::Isolate* isolate, const Dali::Property::Value &value)
948 v8::EscapableHandleScope handleScope( isolate );
950 // Simple Dali properties (ints, strings, bools etc) are stored as JavaScript primitives (v8::Boolean ...)
951 // more complex properties (Vectors, Rectangles...) are wrapped by a JavaScript object
952 if( IsPrimitive( value ))
954 return handleScope.Escape( CreateJavaScriptPrimitive( isolate, value) );
957 // This will wrap a Dali structure (Vector, Rect, Matrix etc) inside a JavaScript object.
959 v8::Local<v8::ObjectTemplate> objectTemplate;
961 if(PropertyValueWrapper::mTemplatePrimitive.IsEmpty() )
963 objectTemplate = MakeDaliPropertyTemplate( isolate );
964 PropertyValueWrapper::mTemplatePrimitive.Reset( isolate, objectTemplate);
968 // get the object template
969 objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, PropertyValueWrapper::mTemplatePrimitive );
972 v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
974 PropertyValueWrapper* pointer = new PropertyValueWrapper( value, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
976 // assign the JavaScript object to the wrapper.
977 // This also stores Dali object, in an internal field inside the JavaScript object.
978 pointer->SetJavascriptObject( isolate, localObject );
980 return handleScope.Escape( localObject );
984 // Utility function that extracts the C++ map pointer from a wrapper
987 PropertyValueWrapper* PropertyValueWrapper::Unwrap( v8::Isolate* isolate, v8::Handle< v8::Object> obj)
989 v8::HandleScope handleScope( isolate );
991 v8::Local<v8::External> field = v8::Local<v8::External>::Cast( obj->GetInternalField(0) );
992 void* ptr = field->Value();
993 return static_cast< PropertyValueWrapper *>(ptr);
996 void PropertyValueWrapper::PropertyGet( v8::Local<v8::String> propertyName,
997 const v8::PropertyCallbackInfo<v8::Value>& info)
999 v8::Isolate* isolate = info.GetIsolate();
1000 v8::HandleScope handleScope( isolate );
1002 std::string name = V8Utils::v8StringToStdString( propertyName );
1004 // unwrap the object
1005 PropertyValueWrapper* propWrapper = Unwrap( isolate, info.This() );
1007 v8::Local<v8::Value> value = GetV8ValueFrom( isolate, propWrapper->mValue, name );
1009 info.GetReturnValue().Set( value );
1012 void PropertyValueWrapper::PropertySet( v8::Local<v8::String> propertyName,
1013 v8::Local<v8::Value> javaScriptValue,
1014 const v8::PropertyCallbackInfo<v8::Value>& info)
1016 v8::Isolate* isolate = info.GetIsolate();
1017 v8::HandleScope handleScope( isolate );
1019 std::string name = V8Utils::v8StringToStdString( propertyName );
1021 PropertyValueWrapper* propWrapper = Unwrap( isolate, info.This() );
1023 SetFromV8Value( isolate, propWrapper->mValue, name, javaScriptValue );
1028 } // namespace V8Plugin