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"
33 void RecursiveSetProperty(Dali::Property::Value& propertyValue, const emscripten::val& fromVal)
35 static const std::string number("number"); // we could maybe just check the first three chars? (avoiding null ...if that's returned)
36 const std::string fromType( fromVal.typeof().as<std::string>() );
38 if( fromType == "object" )
40 // hasOwnProperty is the only way I can find to tell if the object is an array
41 // (keys in HasKey returns the JS keys "0","1","2","3",...)
42 if( fromVal.hasOwnProperty("length") )
44 int length = fromVal["length"].as<int>();
45 // we can't tell if what the user of the property value wants with a JS Array
46 // by default 'standard' length arrays are always interpreted as Vector2/3/4 etc
47 // If the user specifically wants an array they must recast.
48 bool isArray = false; // nested ie [ [1,2,3], [4,5,6] ]
51 if( number == fromVal["0"].typeof().as<std::string>() &&
52 number == fromVal["1"].typeof().as<std::string>() &&
53 number == fromVal["2"].typeof().as<std::string>() &&
54 number == fromVal["3"].typeof().as<std::string>() )
56 propertyValue = Dali::Vector4( fromVal["0"].as<float>(),
57 fromVal["1"].as<float>(),
58 fromVal["2"].as<float>(),
59 fromVal["3"].as<float>() );
66 else if( 3 == length )
68 if( number == fromVal["0"].typeof().as<std::string>() &&
69 number == fromVal["1"].typeof().as<std::string>() &&
70 number == fromVal["2"].typeof().as<std::string>() )
72 propertyValue = Dali::Vector3( fromVal["0"].as<float>(),
73 fromVal["1"].as<float>(),
74 fromVal["2"].as<float>() );
81 else if( 2 == length )
83 if( number == fromVal["0"].typeof().as<std::string>() &&
84 number == fromVal["1"].typeof().as<std::string>() )
86 propertyValue = Dali::Vector2( fromVal["0"].as<float>(),
87 fromVal["1"].as<float>() );
101 propertyValue = Dali::Property::Value(Dali::Property::ARRAY);
102 Dali::Property::Array* array = propertyValue.GetArray();
103 for( int j = 0; j < length; ++j )
105 Dali::Property::Value add;
106 array->PushBack( add );
108 std::stringstream ss;
111 emscripten::val val = fromVal[ ss.str() ];
112 RecursiveSetProperty( array->GetElementAt(j), val );
118 propertyValue = Dali::Property::Value(Dali::Property::MAP);
119 Dali::Property::Map* map = propertyValue.GetMap();
120 emscripten::val keys = emscripten::val::global("Object").call<emscripten::val>("keys", fromVal);
121 int keyLength = keys["length"].as<int>();
122 for( int j = 0; j < keyLength; ++j )
124 Dali::Property::Value add;
125 std::string key = keys[j].as<std::string>();
127 emscripten::val keyVal = fromVal[ key ];
128 RecursiveSetProperty( *(map->Find( key )), keyVal );
132 else if( fromType == "number" )
134 propertyValue = Dali::Property::Value( fromVal.as<float>() );
136 else if( fromType == "string" )
138 propertyValue = Dali::Property::Value( fromVal.as<std::string>() );
147 emscripten::val JavascriptValue( const Dali::Property::Value& v )
149 switch( v.GetType() )
151 case Dali::Property::BOOLEAN:
153 return emscripten::val(v.Get<bool>());
156 case Dali::Property::FLOAT:
158 return emscripten::val(v.Get<float>());
161 case Dali::Property::INTEGER:
163 return emscripten::val(v.Get<int>());
166 case Dali::Property::VECTOR2:
168 Dali::Vector2 in = v.Get<Dali::Vector2>();
169 emscripten::val out = emscripten::val::array();
170 out.set("0", emscripten::val(in.x) );
171 out.set("1", emscripten::val(in.y) );
175 case Dali::Property::VECTOR3:
177 Dali::Vector3 in = v.Get<Dali::Vector3>();
178 emscripten::val out = emscripten::val::array();
179 out.set("0", emscripten::val(in.x) );
180 out.set("1", emscripten::val(in.y) );
181 out.set("2", emscripten::val(in.z) );
185 case Dali::Property::VECTOR4:
187 Dali::Vector4 in = v.Get<Dali::Vector4>();
188 emscripten::val out = emscripten::val::array();
189 out.set("0", emscripten::val(in.x) );
190 out.set("1", emscripten::val(in.y) );
191 out.set("2", emscripten::val(in.z) );
192 out.set("3", emscripten::val(in.w) );
196 case Dali::Property::MATRIX3:
198 emscripten::val val = emscripten::val::array();
199 Dali::Matrix3 mat3 = v.Get<Dali::Matrix3>();
201 for( int i = 0; i < 9; ++i )
203 std::stringstream key;
205 val.set( key.str(), emscripten::val(mat3.AsFloat()[i]) );
210 case Dali::Property::MATRIX:
212 emscripten::val val = emscripten::val::array();
213 Dali::Matrix mat = v.Get<Dali::Matrix>();
215 for( int i = 0; i < 16; ++i )
217 std::stringstream key;
219 val.set( key.str(), emscripten::val(mat.AsFloat()[i]) );
224 case Dali::Property::RECTANGLE:
226 Dali::Rect<int> in = v.Get<Dali::Rect<int> >();
227 emscripten::val out = emscripten::val::array();
228 out.set("0", emscripten::val(in.x) );
229 out.set("1", emscripten::val(in.y) );
230 out.set("2", emscripten::val(in.width) );
231 out.set("3", emscripten::val(in.height) );
235 case Dali::Property::ROTATION:
237 Dali::Quaternion in = v.Get<Dali::Quaternion>();
238 emscripten::val out = emscripten::val::array();
241 in.ToAxisAngle(axis, angle);
243 out.set("0", emscripten::val( axis.x ) );
244 out.set("1", emscripten::val( axis.y ) );
245 out.set("2", emscripten::val( axis.z ) );
246 out.set("3", emscripten::val( Dali::Degree(angle).degree ) );
251 case Dali::Property::STRING:
253 return emscripten::val( v.Get<std::string>() );
256 case Dali::Property::ARRAY:
258 emscripten::val val = emscripten::val::array();
259 Dali::Property::Array *array = v.GetArray();
260 DALI_ASSERT_ALWAYS(array);
262 for( int i = 0; i < array->Count(); ++i )
264 Dali::Property::Value& property = array->GetElementAt( i );
265 std::stringstream key;
267 val.set( key.str(), JavascriptValue( property ) );
273 case Dali::Property::MAP:
275 emscripten::val val = emscripten::val::object();
276 Dali::Property::Map *map = v.GetMap();
277 DALI_ASSERT_ALWAYS(map);
279 for( int i = 0; i < map->Count(); ++i )
282 StringValuePair pair = map->GetPair(i);
283 val.set( pair.first, JavascriptValue( pair.second ) );
289 case Dali::Property::NONE:
295 return emscripten::val::undefined();
299 Dali::Property::Value PropertyMapGet( Dali::Property::Map& self, const std::string& key)
301 Dali::Property::Value ret;
303 Dali::Property::Value* v = self.Find(key);
313 }; // namespace Emscripten
314 }; // namespace Internal