1 # Canvas backward support for using 3.0 API with FlightGear 2.8
5 # 2.8 uses multiple properties for representing a color. Also different names
6 # are used for the colors and no CSS syntax like #rrggbb are supported.
7 var color_components = ["red", "green", "blue", "alpha"];
8 var setColorNodes = func(obj, name, color)
10 if( size(color) == 1 )
13 if( typeof(color) != "vector" )
14 debug.warn("Wrong type for color");
15 else if( size(color) < 3 or size(color) > 4 )
16 debug.warn("Color needs 3 or 4 values (RGB or RGBA)");
19 var node = obj._node.getNode(name, 1);
20 for(var i = 0; i < size(color_components); i += 1)
21 node.getNode(color_components[i], 1)
22 .setDoubleValue( i < size(color) ? color[i]
23 : 1 ); # default alpha is 1
29 Element.setColor = func setColorNodes(me, "color", arg);
30 Element.setColorFill = func {
31 setColorNodes(me, "color-fill", arg);
32 me.setBool("fill", 1);
34 Path.setColor = Element.setColor;
35 Path.setColorFill = Element.setColorFill;
36 Text.setColor = Element.setColor;
37 Text.setColorFill = Element.setColorFill;
40 Canvas.setColorBackground = func()
42 me._node = me.texture;
43 setColorNodes(me, "color-background", arg);
46 # 2.8 uses multiple properties instead of a single string property
47 Canvas.setStrokeDashArray = func(pattern)
49 me._node.removeChildren('stroke-dasharray');
51 if( typeof(pattern) == 'vector' )
52 me._node.setValues({'stroke-dasharray': pattern});
54 debug.warn("setStrokeDashArray: vector expected!");
59 # Create a canvas object from the given node. Used instead of ghost available
60 # in newer FG versions.
61 var makeCanvas = func(node)
65 createGroup: func props.wrapNode(me._node_ghost).addChild("group", 0, 0)._g
69 # Internal helper for creating a canvas.
71 # @note In 2.9+ this function is replaced by a C++ function. This
72 # implementation is just used as fallback for 2.8 and for usage
74 canvas._newCanvasGhost = func()
76 return makeCanvas( Canvas.property_root.addChild("texture", 0, 0) );
79 # Internal helper for retrieving an existing canvas.
81 canvas._getCanvasGhost = func(arg_ghost)
83 var arg = props.wrapNode(arg_ghost);
85 # get a canvas specified by its root node
86 if( Canvas.property_root.getPath() == arg.getParent().getPath() )
87 var node = Canvas.property_root.getChild("texture", arg.getIndex());
89 # get a canvas by its name
90 else if( (var name = arg.getChild("name")) != nil )
92 name = name.getValue();
94 foreach(var c; Canvas.property_root.getChildren("texture"))
96 if( c.getValue("name") == name )
101 # get a canvas by its index
102 else if( (var index = arg.getChild("index")) != nil )
103 var node = Canvas.property_root.getChild("texture", index.getValue());
108 return makeCanvas( node );
111 print("Canvas API: FlightGear 2.8 backward support loaded.");