1
0
Fork 0

Canvas API: Use more exposed core functions

This commit is contained in:
Thomas Geymayer 2012-11-30 17:39:39 +01:00
parent d758f6e28a
commit ccee791529

View file

@ -134,12 +134,13 @@ var Transform = {
var Element = { var Element = {
# Constructor # Constructor
# #
# @param node Node to be used for element or vector [parent, type] for # @param ghost Element ghost as retrieved from core methods
# creation of a new node with name type and given parent new: func(ghost)
# @param id ID/Name (Should be unique)
new: func(node, id)
{ {
var m = { parents: [PropertyElement.new(node, id), Element] }; var m = {
parents: [PropertyElement, Element, ghost],
_node: props.wrapNode(ghost._node_ghost)
};
m._center = [ m._center = [
m._node.getNode("center[0]"), m._node.getNode("center[0]"),
@ -235,6 +236,8 @@ var Element = {
if( size(center) != 2 ) if( size(center) != 2 )
return debug.warn("invalid arg"); return debug.warn("invalid arg");
me._setupCenterNodes();
if( me._center[0] == nil ) if( me._center[0] == nil )
me._center[0] = me._node.getNode("center[0]", 1); me._center[0] = me._node.getNode("center[0]", 1);
if( me._center[1] == nil ) if( me._center[1] == nil )
@ -251,6 +254,8 @@ var Element = {
var bb = me.getBoundingBox(); var bb = me.getBoundingBox();
var center = [0, 0]; var center = [0, 0];
me._setupCenterNodes();
if( me._center[0] != nil ) if( me._center[0] != nil )
center[0] = me._center[0].getValue() or 0; center[0] = me._center[0].getValue() or 0;
if( me._center[1] != nil ) if( me._center[1] != nil )
@ -268,6 +273,14 @@ var Element = {
if( me['_tf'] == nil ) if( me['_tf'] == nil )
me['_tf'] = me.createTransform(); me['_tf'] = me.createTransform();
return me._tf; return me._tf;
},
_setupCenterNodes: func()
{
if( me["_center"] == nil )
me["_center"] = [
me._node.getNode("center[0]"),
me._node.getNode("center[1]")
];
} }
}; };
@ -277,19 +290,20 @@ var Element = {
# #
var Group = { var Group = {
# public: # public:
new: func(node, id) new: func(ghost)
{ {
return { parents: [Group, Element.new(node, id)] }; return { parents: [Group, Element.new(ghost)] };
}, },
# Create a child of given type with specified id. # Create a child of given type with specified id.
# type can be group, text # type can be group, text
createChild: func(type, id = nil) createChild: func(type, id = nil)
{ {
var ghost = me._createChild(type, id);
var factory = me._getFactory(type); var factory = me._getFactory(type);
if( factory == nil ) if( factory == nil )
return nil; return ghost;
return factory([me._node, type], id); return factory(ghost);
}, },
# Create multiple children of given type # Create multiple children of given type
createChildren: func(type, count) createChildren: func(type, count)
@ -298,9 +312,9 @@ var Group = {
if( factory == nil ) if( factory == nil )
return []; return [];
var nodes = me._node.addChildren(type, count, 0, 0); var nodes = props._addChildren(me._node._g, [type, count, 0, 0]);
for(var i = 0; i < count; i += 1) for(var i = 0; i < count; i += 1)
nodes[i] = factory(nodes[i], nil); # TODO id. Maybe <base id>-<index>? nodes[i] = factory( me._getChild(nodes[i]) );
return nodes; return nodes;
}, },
@ -318,29 +332,19 @@ var Group = {
# Get first child with given id (breadth-first search) # Get first child with given id (breadth-first search)
# #
# @note Use with care as it can take several miliseconds (for me eg. ~2ms). # @note Use with care as it can take several miliseconds (for me eg. ~2ms).
# TODO check with new C++ implementation
getElementById: func(id) getElementById: func(id)
{ {
# TODO can we improve the queue or better port this to C++ or use some kind var ghost = me._getElementById(id);
# of lookup hash? Searching is really slow now... if( ghost == nil )
var stack = [me._node]; return nil;
var index = 0;
while( index < size(stack) ) var node = props.wrapNode(ghost._node_ghost);
{ var factory = me._getFactory( node.getName() );
var node = stack[index]; if( factory == nil )
index += 1; return ghost;
if( node != me._node ) return factory(ghost);
{
var node_id = node.getNode("id");
if( node_id != nil and node_id.getValue() == id )
return me._wrapElement(node);
}
foreach(var c; node.getChildren())
if( me._isElementNode(c) )
append(stack, c);
}
}, },
# Remove all children # Remove all children
removeAllChildren: func() removeAllChildren: func()
@ -360,7 +364,7 @@ var Group = {
_wrapElement: func(node) _wrapElement: func(node)
{ {
# Create element from existing node # Create element from existing node
return me._element_factories[ node.getName() ](node, nil); return me._element_factories[ node.getName() ]( me._getChild(node._g) );
}, },
_getFactory: func(type) _getFactory: func(type)
{ {
@ -379,9 +383,9 @@ var Group = {
# which automatically get projected according to the specified projection. # which automatically get projected according to the specified projection.
# #
var Map = { var Map = {
new: func(node, id) new: func(ghost)
{ {
return { parents: [Map, Group.new(node, id)] }; return { parents: [Map, Group.new(ghost)] };
} }
# TODO # TODO
}; };
@ -391,9 +395,9 @@ var Map = {
# Class for a text element on a canvas # Class for a text element on a canvas
# #
var Text = { var Text = {
new: func(node, id) new: func(ghost)
{ {
return { parents: [Text, Element.new(node, id)] }; return { parents: [Text, Element.new(ghost)] };
}, },
# Set the text # Set the text
setText: func(text) setText: func(text)
@ -523,10 +527,10 @@ var Path = {
], ],
# #
new: func(node, id) new: func(ghost)
{ {
return { return {
parents: [Path, Element.new(node, id)], parents: [Path, Element.new(ghost)],
_num_cmds: 0, _num_cmds: 0,
_num_coords: 0 _num_coords: 0
}; };
@ -660,9 +664,9 @@ var Path = {
# Class for an image element on a canvas # Class for an image element on a canvas
# #
var Image = { var Image = {
new: func(node, id) new: func(ghost)
{ {
return {parents: [Image, Element.new(node, id)]}; return {parents: [Image, Element.new(ghost)]};
}, },
# Set image file to be used # Set image file to be used
# #
@ -736,9 +740,9 @@ var Canvas = {
# @param id Optional id/name for the group # @param id Optional id/name for the group
createGroup: func(id = nil) createGroup: func(id = nil)
{ {
var ghost = me.parents[1].createGroup(); var ghost = me._createGroup();
return { return {
parents: [ Group.new(props.wrapNode(ghost._node_ghost), id), ghost ] parents: [ Group.new(ghost) ]
}; };
}, },
# Set the background color # Set the background color