diff --git a/Nasal/canvas/api.nas b/Nasal/canvas/api.nas index 3f6d79b6b..f96320fcd 100644 --- a/Nasal/canvas/api.nas +++ b/Nasal/canvas/api.nas @@ -736,17 +736,10 @@ var Canvas = { # @param id Optional id/name for the group createGroup: func(id = nil) { - if( size(me.parents) >= 2 ) - { - var ghost = me.parents[1].createGroup(); - return { - parents: [ Group.new(props.wrapNode(ghost._node_ghost), id), - ghost ] - }; - } - else - # Fallback for Canvas instances not based on a ghost - return Group.new([me.texture, "group"], id); + var ghost = me.parents[1].createGroup(); + return { + parents: [ Group.new(props.wrapNode(ghost._node_ghost), id), ghost ] + }; }, # Set the background color # @@ -759,6 +752,14 @@ var Canvas = { } }; +var wrapCanvas = func(canvas_ghost) +{ + return { + parents: [Canvas, canvas_ghost], + texture: props.wrapNode(canvas_ghost._node_ghost) + }; +} + # Create a new canvas. Pass parameters as hash, eg: # # var my_canvas = canvas.new({ @@ -769,11 +770,8 @@ var Canvas = { # }); var new = func(vals) { - var m = { parents: [Canvas, _newCanvasGhost()] }; - - m.texture = props.wrapNode(m._node_ghost); + var m = wrapCanvas(_newCanvasGhost()); m.texture.setValues(vals); - return m; }; @@ -782,30 +780,20 @@ var new = func(vals) # @param name Name of the canvas # @return #Canvas, if canvas with #name exists # nil, otherwise -var get = func(name) +var get = func(arg) { - var node_canvas = nil; - if( isa(name, props.Node) ) - node_canvas = name; - else if( typeof(name) == 'scalar' ) - { - foreach(var c; Canvas.property_root.getChildren("texture")) - { - if( c.getValue("name") == name ) - node_canvas = c; - } - } + if( isa(arg, props.Node) ) + var node = arg; + else if( typeof(arg) == "hash" ) + var node = props.Node.new(arg); + else + die("canvas.new: Invalid argument."); - if( node_canvas == nil ) - { - debug.warn("Canvas not found: " ~ name); + var canvas_ghost = _getCanvasGhost(node._g); + if( canvas_ghost == nil ) return nil; - } - return { - parents: [Canvas], - texture: node_canvas - }; + return wrapCanvas(canvas_ghost); }; # ------------------------------------------------------------------------------ diff --git a/Nasal/canvas/api.nas.2.8 b/Nasal/canvas/api.nas.2.8 index 031d1c38b..c9921f479 100644 --- a/Nasal/canvas/api.nas.2.8 +++ b/Nasal/canvas/api.nas.2.8 @@ -22,7 +22,7 @@ var setColorNodes = func(obj, name, color) .setDoubleValue( i < size(color) ? color[i] : 1 ); # default alpha is 1 } - + return obj; }; @@ -56,6 +56,16 @@ Canvas.setStrokeDashArray = func(pattern) return me; }; +# Create a canvas object from the given node. Used instead of ghost available +# in newer FG versions. +var makeCanvas = func(node) +{ + return { + _node_ghost: node._g, + createGroup: func props.wrapNode(me._node_ghost).addChild("group", 0, 0)._g + }; +}; + # Internal helper for creating a canvas. # # @note In 2.9+ this function is replaced by a C++ function. This @@ -63,10 +73,39 @@ Canvas.setStrokeDashArray = func(pattern) # in other programs. canvas._newCanvasGhost = func() { - return { - parents: [Canvas], - texture: Canvas.property_root.addChild("texture", 0, 0) - }; + return makeCanvas( Canvas.property_root.addChild("texture", 0, 0) ); +}; + +# Internal helper for retrieving an existing canvas. +# +canvas._getCanvasGhost = func(arg_ghost) +{ + var arg = props.wrapNode(arg_ghost); + + # get a canvas specified by its root node + if( Canvas.property_root.getPath() == arg.getParent().getPath() ) + var node = Canvas.property_root.getChild("texture", arg.getIndex()); + + # get a canvas by its name + else if( (var name = arg.getChild("name")) != nil ) + { + name = name.getValue(); + var node = nil; + foreach(var c; Canvas.property_root.getChildren("texture")) + { + if( c.getValue("name") == name ) + node_canvas = c; + } + } + + # get a canvas by its index + else if( (var index = arg.getChild("index")) != nil ) + var node = Canvas.property_root.getChild("texture", index.getValue()); + + if( node == nil ) + return nil; + + return makeCanvas( node ); }; print("Canvas API: FlightGear 2.8 backward support loaded.");