From ec43493e9140dd73ba80e4d44a2b1148e0fc5c30 Mon Sep 17 00:00:00 2001 From: ThorstenB Date: Sat, 2 Apr 2011 11:48:39 +0200 Subject: [PATCH] Use local Nasal vars. Run ATC chatter handler when feature enabled only. --- Nasal/atc-chatter.nas | 60 +++++++++++++++++++++++++++++++------------ Nasal/material.nas | 52 ++++++++++++++++++------------------- 2 files changed, 70 insertions(+), 42 deletions(-) diff --git a/Nasal/atc-chatter.nas b/Nasal/atc-chatter.nas index 6cea6b075..3b1565003 100644 --- a/Nasal/atc-chatter.nas +++ b/Nasal/atc-chatter.nas @@ -11,24 +11,25 @@ # Global shared variables ############################################################################# -fg_root = ""; -chatter = "UK"; -chatter_dir = ""; +var fg_root = nil; +var chatter = "UK"; +var chatter_dir = ""; -chatter_min_interval = 20.0; -chatter_max_interval = 40.0; +var chatter_min_interval = 20.0; +var chatter_max_interval = 40.0; +var next_interval = nil; -chatter_index = 0; -chatter_size = 0; -chatter_list = 0; +var chatter_index = 0; +var chatter_size = 0; +var chatter_list = 0; ############################################################################# -# Use tha nasal timer to call the initialization function once the sim is -# up and running +# Chatter is initialized only when actually enabled. See listener connected +# to /sim/sound/chatter/enabled. ############################################################################# -CHATTER_INIT = func { +var chatter_init = func { # default values fg_root = getprop("/sim/fg-root"); chatter_dir = sprintf("%s/ATC/Chatter/%s", fg_root, chatter); @@ -39,14 +40,13 @@ CHATTER_INIT = func { srand(); chatter_index = int( chatter_size * rand() ); } -settimer(CHATTER_INIT, 0); ############################################################################# # main update function to be called each frame ############################################################################# -chatter_update = func { +var chatter_update = func { if ( chatter_index >= chatter_size ) { chatter_index = 0; } @@ -73,15 +73,43 @@ chatter_update = func { ############################################################################# -# Use tha nasal timer to update every 10 seconds +# Use the nasal timer to update every 10 seconds ############################################################################# -nextChatter = func { +var nextChatter = func { + if (!getprop("/sim/sound/chatter/enabled")) + { + next_interval = nil; + return; + } + # schedule next message in next min-max interval seconds so we have a bit # of a random pacing next_interval = chatter_min_interval + int(rand() * (chatter_max_interval - chatter_min_interval)); + # printlog("info", "next chatter in ", next_interval, " seconds"); + settimer(chatter_update, next_interval ); } -nextChatter(); + +############################################################################# +# Start chatter processing. Also connected to chatter/enabled property as a +# listener. +############################################################################# + +var startChatter = func { + if ( getprop("/sim/sound/chatter/enabled") ) { + if (fg_root == nil) + chatter_init(); + if (next_interval == nil) + nextChatter(); + } +} + +# connect listener +setlistener("/sim/sound/chatter/enabled", startChatter); + +# start chatter immediately, if enable is already set. +settimer(startChatter, 0); + diff --git a/Nasal/material.nas b/Nasal/material.nas index 518e6e7de..1b3dab713 100644 --- a/Nasal/material.nas +++ b/Nasal/material.nas @@ -48,13 +48,13 @@ # # -dialog = nil; +var dialog = nil; -colorgroup = func { - parent = arg[0]; # pui parent - name = arg[1]; # "diffuse" - base = arg[2]; - undef = func { props.globals.getNode(base ~ name ~ "/" ~ arg[0]) == nil }; +var colorgroup = func { + var parent = arg[0]; # pui parent + var name = arg[1]; # "diffuse" + var base = arg[2]; + var undef = func { props.globals.getNode(base ~ name ~ "/" ~ arg[0]) == nil }; if (undef("red") and undef("green") and undef("blue")) { return 0; @@ -64,11 +64,11 @@ colorgroup = func { parent.addChild("hrule").setColor(1, 1, 1, 0.5); } - grp = parent.addChild("group"); + var grp = parent.addChild("group"); grp.set("layout", "vbox"); grp.addChild("text").set("label", name); - foreach (color; ["red", "green", "blue", "factor"]) { + foreach (var color; ["red", "green", "blue", "factor"]) { mat(parent, color, base ~ name ~ "/" ~ color, "%.3f"); } mat(parent, "offset", base ~ name ~ "/" ~ "offset", "%.3f", -1.0, 1.0); @@ -76,19 +76,19 @@ colorgroup = func { } -mat = func { - parent = arg[0]; - name = arg[1]; - path = arg[2]; - format = arg[3]; +var mat = func { + var parent = arg[0]; + var name = arg[1]; + var path = arg[2]; + var format = arg[3]; if (props.globals.getNode(path) != nil) { - grp = parent.addChild("group"); + var grp = parent.addChild("group"); grp.set("layout", "hbox"); grp.addChild("empty").set("stretch", 1); grp.addChild("text").set("label", name); - slider = grp.addChild("slider"); + var slider = grp.addChild("slider"); slider.set("property", path); slider.set("live", 1); if (size(arg) == 6) { @@ -97,7 +97,7 @@ mat = func { } slider.setBinding("dialog-apply"); - number = grp.addChild("text"); + var number = grp.addChild("text"); number.set("label", "-0.123"); number.set("format", format); number.set("property", path); @@ -107,13 +107,13 @@ mat = func { } -showDialog = func { - base = arg[0]; +var showDialog = func { + var base = arg[0]; while (size(base) and substr(base, size(base) - 1, 1) == "/") { base = substr(base, 0, size(base) - 1); } - parentdir = ""; - b = base; + var parentdir = ""; + var b = base; while (size(b)) { c = substr(b, size(b) - 1, 1); if (c == "/") { break } @@ -121,8 +121,8 @@ showDialog = func { parentdir = c ~ parentdir; } - title = if (size(arg) > 1 and arg[1] != nil) { arg[1] } else { parentdir }; - name = "material-" ~ parentdir; + var title = if (size(arg) > 1 and arg[1] != nil) { arg[1] } else { parentdir }; + var name = "material-" ~ parentdir; base = base ~ "/"; dialog = gui.Widget.new(); @@ -131,9 +131,9 @@ showDialog = func { if (size(arg) > 3 and arg[3] != nil) { dialog.set("y", arg[3]) } dialog.set("layout", "vbox"); - titlebar = dialog.addChild("group"); + var titlebar = dialog.addChild("group"); titlebar.set("layout", "hbox"); - w = titlebar.addChild("text"); + var w = titlebar.addChild("text"); w.set("label", "object \"" ~ title ~ "\""); titlebar.addChild("empty").set("stretch", 1); @@ -146,13 +146,13 @@ showDialog = func { w.set("border", 1); w.setBinding("dialog-close"); - h = 0; + var h = 0; h += colorgroup(dialog, "diffuse", base, h); h += colorgroup(dialog, "ambient", base, h); h += colorgroup(dialog, "emission", base, h); h += colorgroup(dialog, "specular", base, h); - undef = func { props.globals.getNode(base ~ arg[0]) == nil }; + var undef = func { props.globals.getNode(base ~ arg[0]) == nil }; if (!(undef("shininess") and undef("transparency/alpha") and undef("threshold"))) { if (h) { dialog.addChild("hrule").setColor(1, 1, 1, 0.5);