script: Support ClutterPoint and ClutterSize

Point and Size can be described both as an array of values or as an
object.
This commit is contained in:
Emmanuele Bassi 2012-04-01 17:54:11 +01:00
parent 7814ec2eb5
commit 2276f24ffd
3 changed files with 159 additions and 8 deletions

View File

@ -492,6 +492,114 @@ _clutter_script_parse_color (ClutterScript *script,
return FALSE; return FALSE;
} }
static gboolean
parse_point_from_array (JsonArray *array,
ClutterPoint *point)
{
if (json_array_get_length (array) != 2)
return FALSE;
point->x = json_array_get_double_element (array, 0);
point->y = json_array_get_double_element (array, 1);
return TRUE;
}
static gboolean
parse_point_from_object (JsonObject *object,
ClutterPoint *point)
{
if (json_object_has_member (object, "x"))
point->x = json_object_get_double_member (object, "x");
else
point->x = 0.f;
if (json_object_has_member (object, "y"))
point->y = json_object_get_double_member (object, "y");
else
point->y = 0.f;
return TRUE;
}
gboolean
_clutter_script_parse_point (ClutterScript *script,
JsonNode *node,
ClutterPoint *point)
{
g_return_val_if_fail (CLUTTER_IS_SCRIPT (script), FALSE);
g_return_val_if_fail (node != NULL, FALSE);
g_return_val_if_fail (point != NULL, FALSE);
switch (JSON_NODE_TYPE (node))
{
case JSON_NODE_ARRAY:
return parse_point_from_array (json_node_get_array (node), point);
case JSON_NODE_OBJECT:
return parse_point_from_object (json_node_get_object (node), point);
default:
break;
}
return FALSE;
}
static gboolean
parse_size_from_array (JsonArray *array,
ClutterSize *size)
{
if (json_array_get_length (array) != 2)
return FALSE;
size->width = json_array_get_double_element (array, 0);
size->height = json_array_get_double_element (array, 1);
return TRUE;
}
static gboolean
parse_size_from_object (JsonObject *object,
ClutterSize *size)
{
if (json_object_has_member (object, "width"))
size->width = json_object_get_double_member (object, "width");
else
size->width = 0.f;
if (json_object_has_member (object, "height"))
size->height = json_object_get_double_member (object, "height");
else
size->height = 0.f;
return TRUE;
}
gboolean
_clutter_script_parse_size (ClutterScript *script,
JsonNode *node,
ClutterSize *size)
{
g_return_val_if_fail (CLUTTER_IS_SCRIPT (script), FALSE);
g_return_val_if_fail (node != NULL, FALSE);
g_return_val_if_fail (size != NULL, FALSE);
switch (JSON_NODE_TYPE (node))
{
case JSON_NODE_ARRAY:
return parse_size_from_array (json_node_get_array (node), size);
case JSON_NODE_OBJECT:
return parse_size_from_object (json_node_get_object (node), size);
default:
break;
}
return FALSE;
}
const gchar * const gchar *
_clutter_script_get_id_from_node (JsonNode *node) _clutter_script_get_id_from_node (JsonNode *node)
{ {
@ -1256,6 +1364,26 @@ _clutter_script_parse_node (ClutterScript *script,
return TRUE; return TRUE;
} }
} }
else if (p_type == CLUTTER_TYPE_POINT)
{
ClutterPoint point = CLUTTER_POINT_INIT_ZERO;
if (_clutter_script_parse_point (script, node, &point))
{
g_value_set_boxed (value, &point);
return TRUE;
}
}
else if (p_type == CLUTTER_TYPE_SIZE)
{
ClutterSize size = CLUTTER_SIZE_INIT_ZERO;
if (_clutter_script_parse_size (script, node, &size))
{
g_value_set_boxed (value, &size);
return TRUE;
}
}
else if (p_type == G_TYPE_STRING) else if (p_type == G_TYPE_STRING)
{ {
char *str = NULL; char *str = NULL;
@ -1313,6 +1441,26 @@ _clutter_script_parse_node (ClutterScript *script,
return TRUE; return TRUE;
} }
} }
else if (G_VALUE_HOLDS (value, CLUTTER_TYPE_POINT))
{
ClutterPoint point = CLUTTER_POINT_INIT_ZERO;
if (_clutter_script_parse_point (script, node, &point))
{
g_value_set_boxed (value, &point);
return TRUE;
}
}
else if (G_VALUE_HOLDS (value, CLUTTER_TYPE_SIZE))
{
ClutterSize size = CLUTTER_SIZE_INIT_ZERO;
if (_clutter_script_parse_size (script, node, &size))
{
g_value_set_boxed (value, &size);
return TRUE;
}
}
else if (G_VALUE_HOLDS (value, G_TYPE_STRV)) else if (G_VALUE_HOLDS (value, G_TYPE_STRV))
{ {
JsonArray *array = json_node_get_array (node); JsonArray *array = json_node_get_array (node);

View File

@ -130,6 +130,13 @@ gboolean _clutter_script_parse_color (ClutterScript *script,
ClutterColor *color); ClutterColor *color);
GObject *_clutter_script_parse_alpha (ClutterScript *script, GObject *_clutter_script_parse_alpha (ClutterScript *script,
JsonNode *node); JsonNode *node);
gboolean _clutter_script_parse_point (ClutterScript *script,
JsonNode *node,
ClutterPoint *point);
gboolean _clutter_script_parse_size (ClutterScript *script,
JsonNode *node,
ClutterSize *size);
gboolean _clutter_script_parse_translatable_string (ClutterScript *script, gboolean _clutter_script_parse_translatable_string (ClutterScript *script,
JsonNode *node, JsonNode *node,
char **str); char **str);

View File

@ -26,10 +26,8 @@
"color" : "#00ff00ff", "color" : "#00ff00ff",
"border-width" : 5, "border-width" : 5,
"border-color" : "#00cc00ff", "border-color" : "#00cc00ff",
"x" : 200, "position" : [ 200.0, 50.0 ],
"y" : 50, "size" : { "width" : 100.0, "height" : 100.0 },
"width" : 100,
"height" : 100,
"depth" : -200.0, "depth" : -200.0,
"reactive" : true, "reactive" : true,
"signals" : [ "signals" : [
@ -40,8 +38,7 @@
"id" : "red-hand", "id" : "red-hand",
"type" : "ClutterTexture", "type" : "ClutterTexture",
"filename" : "redhand.png", "filename" : "redhand.png",
"x" : 100, "position" : { "x" : 100.0, "y" : 100.0 },
"y" : 100,
"width" : "20 mm", "width" : "20 mm",
"keep-aspect-ratio" : true, "keep-aspect-ratio" : true,
"anchor-x" : "5 em", "anchor-x" : "5 em",
@ -53,8 +50,7 @@
"id" : "red-hand-clone", "id" : "red-hand-clone",
"type" : "ClutterClone", "type" : "ClutterClone",
"source" : "red-hand", "source" : "red-hand",
"x" : 250, "position" : [ 250.0, 150.0 ],
"y" : 150,
"opacity" : 100 "opacity" : 100
}, },
{ {