2 * Copyright (c) 2014 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.
20 #include <dali/devel-api/adaptor-framework/color-controller.h>
23 #include <dali-toolkit/internal/builder/builder-impl.h>
24 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
25 #include <dali-toolkit/internal/builder/replacement.h>
38 * Set a property value from a tree node.
39 * This function guesses the type of the property from the format of the string in the node.
40 * This is not always possible and could be surprising.
41 * @param node The node string to convert from
42 * @param value The property value to set
43 * @return true if the string could be converted.
45 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
48 * Set a property value from a tree node as SetPropertyFromNode() above
49 * @param node The node string to convert from
50 * @param value The property value to set
51 * @param replacement The overriding replacement map (if any)
52 * @return true if the string could be converted.
54 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
55 const Replacement& replacement );
58 * Set a property value as the given type from a tree node.
59 * @param node The node string to convert from
60 * @param type The property type to convert to.
61 * @param value The property value to set
62 * @return true if the string could be converted to the correct type.
64 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value );
67 * Set a property value as the given type from a tree node as SetPropertyFromNode() above
68 * @param node The node string to convert from
69 * @param type The property type to convert to.
70 * @param value The property value to set
71 * @param replacement The overriding replacement map (if any)
72 * @return true if the string could be converted to the correct type.
74 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
75 const Replacement& replacement );
82 * Converts a HTML style 'color' hex string ("#FF0000" for bright red) to a Vector4.
83 * The Vector4 alpha component will be set to 1.0f
84 * @param hexString The HTML style hex string
85 * @return a Vector4 containing the new color value
87 Vector4 HexStringToVector4( const char* s )
89 unsigned int value(0u);
90 std::istringstream( s ) >> std::hex >> value;
91 return Vector4( ((value >> 16 ) & 0xff ) / 255.0f,
92 ((value >> 8 ) & 0xff ) / 255.0f,
93 (value & 0xff ) / 255.0f,
101 * A property value type can be forced when its unknown by a disambiguation convention in the json
102 * ie "myarray": [1,2,3,4] ; would be a vector but
103 * "myarray": {'type-cast':'array', 'value':[1,2,3,4]} would be an array
104 * @param child The node whos string to search for a disambiguated type
105 * @param value The value to set
106 * @param overrideMap The user overriding constant map
107 * @param defaultMap The default map.
108 * @return True if child contained a disambiguated string that could be converted.
110 bool Disambiguated(const TreeNode& child, // ConstantLut& constantLut,
111 Dali::Property::Value& value,
112 const Replacement& replacement )
114 OptionalString childType = IsString( IsChild(child, "type-cast") );
115 OptionalChild childValue = IsChild(child, "value");
117 if( childType && childValue && (2 == child.Size()) )
119 // this case allows disambiguation but normally the type is guessed
120 // 2 == child.count() is an extra check as the user could have a user dictionary/map with
121 // type-cast and value keys. If they do then a work around is to add a bogus key to not run this case.
122 if(*childType == "boolean")
124 return SetPropertyFromNode( *childValue, Dali::Property::BOOLEAN, value, replacement);
126 else if(*childType == "float")
128 return SetPropertyFromNode( *childValue, Dali::Property::FLOAT, value, replacement);
130 else if(*childType == "vector2")
132 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR2, value, replacement);
134 else if(*childType == "vector3")
136 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR3, value, replacement);
138 else if(*childType == "vector4")
140 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR4, value, replacement);
142 else if(*childType == "rotation")
144 return SetPropertyFromNode( *childValue, Dali::Property::ROTATION, value, replacement);
146 else if(*childType == "rect")
148 return SetPropertyFromNode( *childValue, Dali::Property::RECTANGLE, value, replacement);
150 else if(*childType == "string")
152 return SetPropertyFromNode( *childValue, Dali::Property::STRING, value, replacement);
154 else if(*childType == "map")
156 return SetPropertyFromNode( *childValue, Dali::Property::MAP, value, replacement);
158 else if(*childType == "array")
160 return SetPropertyFromNode( *childValue, Dali::Property::ARRAY, value, replacement);
164 // else we failed to disambiguate
169 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value)
171 Replacement noReplacement;
172 return SetPropertyFromNode( node, type, value, noReplacement );
175 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
176 const Replacement& replacer )
182 case Property::BOOLEAN:
184 if( OptionalBoolean v = replacer.IsBoolean(node) )
191 case Property::FLOAT:
193 if( OptionalFloat v = replacer.IsFloat(node) )
200 case Property::INTEGER:
202 if( OptionalInteger v = replacer.IsInteger(node) )
209 case Property::UNSIGNED_INTEGER:
211 if( OptionalUnsignedInt v = replacer.IsUnsignedInteger( node) )
213 if( *v >= 0 ) // with a loss of resolution....
221 case Property::VECTOR2:
223 if( OptionalVector2 v = replacer.IsVector2(node) )
230 case Property::VECTOR3:
232 if( OptionalVector3 v = replacer.IsVector3(node) )
239 case Property::VECTOR4:
241 if( OptionalVector4 v = replacer.IsVector4(node) )
246 else if( OptionalString s = replacer.IsString(node) )
248 if( (*s)[0] == '#' && 7 == (*s).size() )
250 value = HexStringToVector4( &(*s)[1] );
253 else if( Dali::ColorController::Get() )
256 done = Dali::ColorController::Get().RetrieveColor( *s, color );
260 else if( TreeNode::OBJECT == node.GetType() )
262 // check for "r", "g" and "b" child color component nodes
263 OptionalInteger r = replacer.IsInteger( IsChild(node, "r") );
264 OptionalInteger g = replacer.IsInteger( IsChild(node, "g") );
265 OptionalInteger b = replacer.IsInteger( IsChild(node, "b") );
268 float red( (*r) * (1.0f/255.0f) );
269 float green( (*g) * (1.0f/255.0f) );
270 float blue( (*b) * (1.0f/255.0f) );
271 // check for optional "a" (alpha) node, default to fully opaque if it is not found.
273 OptionalInteger a = replacer.IsInteger( IsChild(node, "a") );
276 alpha = (*a) * (1.0f/255.0f);
278 value = Vector4( red, green, blue, alpha );
284 case Property::MATRIX3:
286 if( OptionalMatrix3 v = replacer.IsMatrix3(node) )
293 case Property::MATRIX:
295 if( OptionalMatrix v = replacer.IsMatrix(node) )
302 case Property::RECTANGLE:
304 if( OptionalRect v = replacer.IsRect(node) )
311 case Property::ROTATION:
315 if( OptionalVector4 ov = replacer.IsVector4(node) )
317 const Vector4& v = *ov;
318 // angle, axis as per spec
319 value = Quaternion(Radian(Degree(v[3])),
320 Vector3(v[0],v[1],v[2]));
326 // degrees Euler as per spec
327 if( OptionalVector3 v = replacer.IsVector3(node) )
329 value = Quaternion(Radian(Degree((*v).x)),
330 Radian(Degree((*v).y)),
331 Radian(Degree((*v).z)));
337 case Property::STRING:
339 if( OptionalString v = replacer.IsString(node) )
346 case Property::ARRAY:
348 if( replacer.IsArray( node, value ) )
354 value = Property::Value(Property::ARRAY);
356 TreeNode::ConstIterator iter(node.CBegin());
357 for( ; i < node.Size(); ++i, ++iter)
360 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
366 if( value.GetSize() == static_cast<int>(node.Size()) )
379 if( replacer.IsMap( node, value ) )
385 value = Property::Value(Property::MAP);
387 TreeNode::ConstIterator iter(node.CBegin());
388 for( ; i < node.Size(); ++i, ++iter)
391 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
393 value.SetValue( (*iter).first, v );
397 if( value.GetSize() == static_cast<int>(node.Size()) )
408 case Property::TYPE_COUNT:
418 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
421 Replacement replacer;
422 return SetPropertyFromNode( node, value, replacer );
425 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
426 const Replacement& replacer )
430 // some values are ambiguous as we have no Property::Type but can be disambiguated in the json
432 // Currently Rotations and Rectangle must always be disambiguated when a type isnt available
433 if( Disambiguated( node, value, replacer ) )
441 // our current heuristic for deciding an array is actually a vector and not say a map
442 // is to check if the values are all floats
443 bool allNumbers = true;
444 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
446 OptionalFloat f = IsFloat((*iter).second);
456 // prefer finding vectors over presuming composite Property::Array...
457 if( OptionalMatrix v = IsMatrix(node) )
462 else if( OptionalMatrix3 v = IsMatrix3(node) )
467 else if( OptionalVector4 v = IsVector4(node) )
472 else if( OptionalVector3 v = IsVector3(node) )
477 else if( OptionalVector2 v = IsVector2(node) )
482 else if( 4 == node.Size() )
484 if( OptionalVector4 v = IsVector4(node) )
492 value = Property::Value(Property::ARRAY);
495 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
497 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
508 // presume an array or map
509 // container of size 1
510 TreeNode::ConstIterator iter = node.CBegin();
512 // its seems legal with current json parser for a map to have an empty key
513 // but here we take that to mean the structure is a list
514 if( ((*iter).first) == 0 )
516 value = Property::Value(Property::ARRAY);
518 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
520 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
529 value = Property::Value(Property::MAP);
531 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
533 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
535 value.SetValue((*iter).first, v);
542 else // if( 0 == node.size() )
544 // no children so either one of bool, float, integer, string
545 OptionalBoolean aBool = replacer.IsBoolean(node);
546 OptionalInteger anInt = replacer.IsInteger(node);
547 OptionalFloat aFloat = replacer.IsFloat(node);
548 OptionalString aString = replacer.IsString(node);
552 // a bool is also an int but here we presume int
566 // Note: These are both floats and strings
569 // This means we can't have a string with purely numeric content without disambiguation.
582 // string always succeeds with the current json parser so its last
589 } // if( node.size() )
591 } // if Disambiguated()
594 } // bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
597 } // namespace Internal
599 } // namespace Toolkit