| 1 |
# material dialog |
| 2 |
# =============== |
| 3 |
# |
| 4 |
# Usage: material.showDialog(<path>, [<title>], [<x>], [<y>]); |
| 5 |
# |
| 6 |
# the path should point to a property "directory" (usually set in |
| 7 |
# the aircraft's *-set.xml file) that contains any of |
| 8 |
# (shininess|transparency|texture) and (diffuse|ambient|specular|emission), |
| 9 |
# whereby the latter four are directories containing any of |
| 10 |
# (red|green|blue|factor|offset). |
| 11 |
# |
| 12 |
# If <title> is omitted or nil, then the last path component is used as title. |
| 13 |
# If <x> and <y> are undefined, then the dialog is centered. |
| 14 |
# |
| 15 |
# |
| 16 |
# Example: |
| 17 |
# <foo> |
| 18 |
# <diffuse> |
| 19 |
# <red>1.0</red> |
| 20 |
# <green>0.5</green> |
| 21 |
# <blue>0.5</blue> |
| 22 |
# </diffuse> |
| 23 |
# <transparency>0.5</transparency> |
| 24 |
# <texture>bar.rgb</texture> |
| 25 |
# </foo> |
| 26 |
# |
| 27 |
# |
| 28 |
# material.showDialog("/sim/model/foo/", "FOO"); |
| 29 |
# |
| 30 |
# |
| 31 |
# |
| 32 |
# Of course, these properties are only used if a "material" animation |
| 33 |
# references them via <*-prop> definition. |
| 34 |
# |
| 35 |
# Example: |
| 36 |
# |
| 37 |
# <animation> |
| 38 |
# <type>material</type> |
| 39 |
# <object-name>foo</object-name> |
| 40 |
# <property-base>/sim/model/foo</property-base> |
| 41 |
# <diffuse> |
| 42 |
# <red-prop>diffuse/red</red-prop> |
| 43 |
# <green-prop>diffuse/green</green-prop> |
| 44 |
# <blue-prop>diffuse/blue</blue-prop> |
| 45 |
# </diffuse> |
| 46 |
# <transparency-prop>transparency</transparency-prop> |
| 47 |
# <texture-prop>texture</texture-prop> |
| 48 |
# </animation> |
| 49 |
# |
| 50 |
|
| 51 |
var dialog = nil; |
| 52 |
|
| 53 |
var colorgroup = func(parent, name, base) { |
| 54 |
var undef = func(color) { props.globals.getNode(base ~ name ~ "/" ~ color) == nil }; |
| 55 |
|
| 56 |
if (undef("red") and undef("green") and undef("blue")) { |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
if (base != nil) { |
| 61 |
parent.addChild("hrule").setColor(1, 1, 1, 0.5); |
| 62 |
} |
| 63 |
|
| 64 |
var grp = parent.addChild("group"); |
| 65 |
grp.set("layout", "vbox"); |
| 66 |
grp.addChild("text").set("label", name); |
| 67 |
|
| 68 |
foreach (var color; ["red", "green", "blue", "factor"]) { |
| 69 |
mat(parent, color, base ~ name ~ "/" ~ color, "%.3f"); |
| 70 |
} |
| 71 |
mat(parent, "offset", base ~ name ~ "/" ~ "offset", "%.3f", -1.0, 1.0); |
| 72 |
return 1; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
var mat = func(parent, name, path, format, min=nil, max=nil) { |
| 77 |
if (props.globals.getNode(path) != nil) { |
| 78 |
var grp = parent.addChild("group"); |
| 79 |
grp.set("layout", "hbox"); |
| 80 |
|
| 81 |
grp.addChild("empty").set("stretch", 1); |
| 82 |
grp.addChild("text").set("label", name); |
| 83 |
|
| 84 |
var slider = grp.addChild("slider"); |
| 85 |
slider.set("property", path); |
| 86 |
slider.set("live", 1); |
| 87 |
if (min != nil and max != nil) { |
| 88 |
slider.set("min", min); |
| 89 |
slider.set("max", max); |
| 90 |
} |
| 91 |
slider.setBinding("dialog-apply"); |
| 92 |
|
| 93 |
var number = grp.addChild("text"); |
| 94 |
number.set("label", "-0.123"); |
| 95 |
number.set("format", format); |
| 96 |
number.set("property", path); |
| 97 |
number.set("live", 1); |
| 98 |
number.setColor(1, 0, 0); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
var showDialog = func(base, title=nil, x=nil, y=nil) { |
| 104 |
while (size(base) and substr(base, size(base) - 1, 1) == "/") { |
| 105 |
base = substr(base, 0, size(base) - 1); |
| 106 |
} |
| 107 |
var parentdir = ""; |
| 108 |
var b = base; |
| 109 |
while (size(b)) { |
| 110 |
c = substr(b, size(b) - 1, 1); |
| 111 |
if (c == "/") { break } |
| 112 |
b = substr(b, 0, size(b) - 1); |
| 113 |
parentdir = c ~ parentdir; |
| 114 |
} |
| 115 |
|
| 116 |
if (title == nil) var title = parentdir; |
| 117 |
var name = "material-" ~ parentdir; |
| 118 |
base = base ~ "/"; |
| 119 |
|
| 120 |
dialog = gui.Widget.new(); |
| 121 |
dialog.set("name", name); |
| 122 |
if (x != nil) dialog.set("x", x); |
| 123 |
if (y != nil) dialog.set("y", y); |
| 124 |
dialog.set("layout", "vbox"); |
| 125 |
|
| 126 |
var titlebar = dialog.addChild("group"); |
| 127 |
titlebar.set("layout", "hbox"); |
| 128 |
var w = titlebar.addChild("text"); |
| 129 |
w.set("label", "object \"" ~ title ~ "\""); |
| 130 |
titlebar.addChild("empty").set("stretch", 1); |
| 131 |
|
| 132 |
w = titlebar.addChild("button"); |
| 133 |
w.set("pref-width", 16); |
| 134 |
w.set("pref-height", 16); |
| 135 |
w.set("legend", ""); |
| 136 |
w.set("default", 1); |
| 137 |
w.set("keynum", 27); |
| 138 |
w.set("border", 1); |
| 139 |
w.setBinding("dialog-close"); |
| 140 |
|
| 141 |
var h = 0; |
| 142 |
h += colorgroup(dialog, "diffuse", base, h); |
| 143 |
h += colorgroup(dialog, "ambient", base, h); |
| 144 |
h += colorgroup(dialog, "emission", base, h); |
| 145 |
h += colorgroup(dialog, "specular", base, h); |
| 146 |
|
| 147 |
var undef = func(prop) { props.globals.getNode(base ~ prop) == nil }; |
| 148 |
if (!(undef("shininess") and undef("transparency/alpha") and undef("threshold"))) { |
| 149 |
if (h) { |
| 150 |
dialog.addChild("hrule").setColor(1, 1, 1, 0.5); |
| 151 |
} |
| 152 |
|
| 153 |
w = dialog.addChild("group"); |
| 154 |
w.set("layout", "hbox"); |
| 155 |
w.addChild("text").set("label", "misc"); |
| 156 |
|
| 157 |
mat(dialog, "shi", base ~ "shininess", "%.0f", 0.0, 128.0); |
| 158 |
mat(dialog, "alpha", base ~ "transparency/alpha", "%.3f"); |
| 159 |
mat(dialog, "thresh", base ~ "threshold", "%.3f"); |
| 160 |
h += 1; |
| 161 |
} |
| 162 |
|
| 163 |
if (!undef("texture")) { |
| 164 |
if (h) { |
| 165 |
dialog.addChild("hrule").setColor(1, 1, 1, 0.5); |
| 166 |
} |
| 167 |
|
| 168 |
w = dialog.addChild("group"); |
| 169 |
w.set("layout", "hbox"); |
| 170 |
w.addChild("text").set("label", "texture"); |
| 171 |
|
| 172 |
w = dialog.addChild("input"); |
| 173 |
w.set("live", 1); |
| 174 |
w.set("pref-width", 200); |
| 175 |
w.set("property", base ~ "texture"); |
| 176 |
w.setBinding("dialog-apply"); |
| 177 |
} |
| 178 |
dialog.addChild("empty").set("pref-height", "3"); |
| 179 |
|
| 180 |
dialog.setColor(0.6, 0.6, 0.6, 0.6); |
| 181 |
|
| 182 |
fgcommand("dialog-new", dialog.prop()); |
| 183 |
gui.showDialog(name); |
| 184 |
} |
| 185 |
|
| 186 |
|