| 1 |
gui.Style = { |
| 2 |
new: func(name, name_icon_theme) |
| 3 |
{ |
| 4 |
var root_node = props.globals.getNode("/sim/gui/canvas", 1) |
| 5 |
.addChild("style"); |
| 6 |
var gui_path = getprop("/sim/fg-root") ~ "/gui"; |
| 7 |
var style_path = gui_path ~ "/styles/" ~ name; |
| 8 |
|
| 9 |
var m = { |
| 10 |
parents: [gui.Style], |
| 11 |
_path: style_path, |
| 12 |
_dir_icons: gui_path ~ "/icons/" ~ name_icon_theme, |
| 13 |
_node: io.read_properties(style_path ~ "/style.xml", root_node), |
| 14 |
_colors: {}, |
| 15 |
_sizes: {} |
| 16 |
}; |
| 17 |
|
| 18 |
# parse theme colors |
| 19 |
var colors = m._node.getChild("colors"); |
| 20 |
if( colors != nil ) |
| 21 |
{ |
| 22 |
foreach(var color; colors.getChildren()) |
| 23 |
{ |
| 24 |
var tintNode = color.getChild("tint"); |
| 25 |
var shadeNode = color.getChild("shade"); |
| 26 |
var baseNode = color.getChild("base"); |
| 27 |
|
| 28 |
if (tintNode and baseNode) { |
| 29 |
|
| 30 |
} elsif (shadeNode and baseNode) { |
| 31 |
|
| 32 |
} else { |
| 33 |
# todo : support a CSS style color as a direct text child, |
| 34 |
# instead of seperate RGBA components |
| 35 |
m._colors[ color.getName() ] = m._parseRGBANodes(color); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
var sizes = m._node.getChild("sizes"); |
| 41 |
if (sizes) { |
| 42 |
foreach(var sizeNode; sizes.getChildren()) { |
| 43 |
var factorNode = sizeNode.getChild("factor"); |
| 44 |
var baseNode = sizeNode.getChild("base"); |
| 45 |
if (factorNode or baseNode) { |
| 46 |
var f = factorNode.getValue() or 1.0; |
| 47 |
m._sizes[sizeNode.getName()] = f * m.getSize(baseNode.getValue(), 1.0); |
| 48 |
} else { |
| 49 |
# simple literal value, easy |
| 50 |
m._sizes[sizeNode.getName()] = sizeNode.getValue(); |
| 51 |
} |
| 52 |
} # of sizes iteration |
| 53 |
} |
| 54 |
|
| 55 |
m._dir_decoration = |
| 56 |
m._path ~ "/" ~ (m._node.getValue("folders/decoration") or "decoration"); |
| 57 |
m._dir_widgets = |
| 58 |
m._path ~ "/" ~ (m._node.getValue("folders/widgets") or "widgets"); |
| 59 |
|
| 60 |
return m; |
| 61 |
}, |
| 62 |
_parseRGBANodes: func(color) |
| 63 |
{ |
| 64 |
var comp_names = ["red", "green", "blue", "alpha"]; |
| 65 |
var str = "rgba("; |
| 66 |
for(var i = 0; i < size(comp_names); i += 1) |
| 67 |
{ |
| 68 |
if( i > 0 ) |
| 69 |
str ~= ","; |
| 70 |
var val = color.getValue(comp_names[i]); |
| 71 |
if( val == nil ) |
| 72 |
val = 1; |
| 73 |
if( i < 3 ) |
| 74 |
str ~= int(val * 255 + 0.5); |
| 75 |
else |
| 76 |
str ~= int(val * 100) / 100; |
| 77 |
} |
| 78 |
return str ~ ")"; |
| 79 |
}, |
| 80 |
|
| 81 |
getColor: func(name, def = "#00ffff") |
| 82 |
{ |
| 83 |
return me._colors[name] or def; |
| 84 |
}, |
| 85 |
getSize: func(name, def = 1.0) |
| 86 |
{ |
| 87 |
return me._sizes[name] or def; |
| 88 |
} |
| 89 |
}; |