1
0
Fork 0

gui.nas: remove trailing slashes in nameprop/sortprop

aircraft.nas: add generic overlay_update function, and make livery_update
         a wrapper for it
This commit is contained in:
mfranz 2008-10-19 11:52:56 +00:00
parent 4393681be9
commit 32bf0f7265
2 changed files with 84 additions and 27 deletions

View file

@ -251,7 +251,7 @@ var light = {
# lowpass.new(<coefficient>); # lowpass.new(<coefficient>);
# #
# EXAMPLE: # EXAMPLE:
# var lp = aircraft.lowpass.new(0.5); # var lp = aircraft.lowpass.new(1.5);
# print(lp.filter(10)); # prints 10 # print(lp.filter(10)); # prints 10
# print(lp.filter(0)); # print(lp.filter(0));
# #
@ -511,9 +511,10 @@ var timer = {
# #
var livery = { var livery = {
init: func(dir, nameprop = "/sim/model/livery/name", sortprop = nil) { init: func(dir, nameprop = "/sim/model/livery/name", sortprop = nil) {
me.parents = [gui.OverlaySelector.new("Select Livery", dir, nameprop, sortprop,
"sim/model/livery/file")];
me.dialog = me.parents[0];
data.add(nameprop); data.add(nameprop);
me.parents = [me.dialog = gui.OverlaySelector.new("Select Livery", dir, nameprop,
sortprop, func setprop("sim/model/livery/file", me.data[me.current][2]))];
}, },
}; };
@ -524,7 +525,7 @@ var livery = {
# Class for maintaining liveries in MP aircraft. It is used in Nasal code that's # Class for maintaining liveries in MP aircraft. It is used in Nasal code that's
# embedded in aircraft animation XML files, and checks in intervals whether the # embedded in aircraft animation XML files, and checks in intervals whether the
# parent aircraft has changed livery, in which case it changes the livery # parent aircraft has changed livery, in which case it changes the livery
# in the remote aircraft accordingly. # in the remote aircraft accordingly. This class is a wrapper for overlay_update.
# #
# SYNOPSIS: # SYNOPSIS:
# livery_update.new(<livery-dir> [, <interval:10> [, <func>]]); # livery_update.new(<livery-dir> [, <interval:10> [, <func>]]);
@ -540,7 +541,7 @@ var livery = {
# <load> # <load>
# var livery_update = aircraft.livery_update.new( # var livery_update = aircraft.livery_update.new(
# "Aircraft/R22/Models/Liveries", 30, # "Aircraft/R22/Models/Liveries", 30,
# func { print("R22 livery update") }); # func print("R22 livery update"));
# </load> # </load>
# #
# <unload> # <unload>
@ -549,31 +550,75 @@ var livery = {
# </nasal> # </nasal>
# #
var livery_update = { var livery_update = {
new: func(liveriesdir, interval = 10, callback = nil) { new: func(liveriesdir, interval = 10.01, callback = nil) {
var m = { parents: [livery_update] }; var m = { parents: [livery_update, overlay_update.new()] };
var root = cmdarg(); m.parents[1].add(liveriesdir, "sim/model/livery/file", callback);
m.root = root.getPath(); m.parents[1].interval = interval;
m.fileN = root.getNode("sim/model/livery/file", 1);
m.dir = getprop("/sim/fg-root") ~ "/" ~ liveriesdir ~ "/";
m.interval = interval;
m.last = "";
m.running = 1;
m.callback = callback;
if (root.getName() == "multiplayer")
m._loop_();
return m; return m;
}, },
stop: func { stop: func {
me.running = 0; me.parents[1].stop();
},
};
# overlay_update
# =============================================================================
# Class for maintaining overlays in MP aircraft. It is used in Nasal code that's
# embedded in aircraft animation XML files, and checks in intervals whether the
# parent aircraft has changed an overlay, in which case it copies the respective
# overlay to the aircraft's root directory.
#
# SYNOPSIS:
# livery_update.new();
# livery_update.add(<overlay-dir>, <property> [, <callback>]);
#
# <overlay-dir> ... directory with overlay files, relative to $FG_ROOT
# <property> ... MP property where the overlay file name can be found
# (usually one of the sim/multiplay/generic/string properties)
# <callback> ... callback function that's called with two arguments:
# the file name (without extension) and the overlay directory
#
# EXAMPLE:
# <nasal>
# <load>
# var update = aircraft.overlay_update.new();
# update.add("Aircraft/F4U/Models/Logos", "sim/multiplay/generic/string");
# </load>
#
# <unload>
# update.stop();
# </unload>
# </nasal>
#
var overlay_update = {
new: func {
var m = { parents: [overlay_update] };
m.root = cmdarg();
m.data = {};
m.interval = 10.01;
if (m.root.getName() == "multiplayer")
m._loop_();
return m;
},
add: func(path, prop, callback = nil) {
var path = string.normpath(getprop("/sim/fg-root") ~ '/' ~ path) ~ '/';
me.data[path] = [props.initNode(me.root.getNode(prop, 1), ""), "",
typeof(callback) == "func" ? callback : func nil];
return me;
},
stop: func {
me._loop_ = func nil;
}, },
_loop_: func { _loop_: func {
me.running or return; foreach (var path; keys(me.data)) {
var file = me.fileN.getValue(); var v = me.data[path];
if (file != nil and file != me.last) { var file = v[0].getValue();
io.read_properties(me.dir ~ file ~ ".xml", me.root); if (file != v[1]) {
me.last = file; io.read_properties(path ~ file ~ ".xml", me.root);
if (me.callback != nil) v[2](v[1] = file, path);
me.callback(file); }
} }
settimer(func me._loop_(), me.interval); settimer(func me._loop_(), me.interval);
}, },

View file

@ -270,7 +270,7 @@ var Dialog = {
# fed to "select" and "material" animations. # fed to "select" and "material" animations.
# #
# SYNOPSIS: # SYNOPSIS:
# OverlaySelector.new(<title>, <dir>, <nameprop> [, <sortprop> [, <callback>]]); # OverlaySelector.new(<title>, <dir>, <nameprop> [, <sortprop> [, <mpprop> [, <callback>]]]);
# #
# title ... dialog title # title ... dialog title
# dir ... directory where to find the XML overlay files, # dir ... directory where to find the XML overlay files,
@ -282,6 +282,8 @@ var Dialog = {
# as sorting criterion, if alphabetic sorting by # as sorting criterion, if alphabetic sorting by
# name is undesirable. Use nil if you don't need # name is undesirable. Use nil if you don't need
# this, but want to set a callback function. # this, but want to set a callback function.
# mpprop ... property path of MP node where the file name should
# be written to
# callback ... function that's called after a new entry was chosen, # callback ... function that's called after a new entry was chosen,
# with these arguments: # with these arguments:
# #
@ -297,7 +299,7 @@ var Dialog = {
# #
# #
var OverlaySelector = { var OverlaySelector = {
new: func(title, dir, nameprop, sortprop = nil, callback = nil) { new: func(title, dir, nameprop, sortprop = nil, mpprop = nil, callback = nil) {
var name = "overlay-select-"; var name = "overlay-select-";
var data = props.globals.getNode("/sim/gui/dialogs/", 1); var data = props.globals.getNode("/sim/gui/dialogs/", 1);
for (var i = 1; 1; i += 1) for (var i = 1; 1; i += 1)
@ -311,6 +313,7 @@ var OverlaySelector = {
m.dir = string.normpath(getprop("/sim/fg-root") ~ '/' ~ dir) ~ '/'; m.dir = string.normpath(getprop("/sim/fg-root") ~ '/' ~ dir) ~ '/';
m.nameprop = nameprop; m.nameprop = nameprop;
m.sortprop = sortprop or nameprop; m.sortprop = sortprop or nameprop;
m.mpprop = mpprop;
m.callback = callback; m.callback = callback;
m.result = props.initNode(data.getNode("result", 1), ""); m.result = props.initNode(data.getNode("result", 1), "");
m.listener = setlistener(m.result, func(n) m.select(n.getValue())); m.listener = setlistener(m.result, func(n) m.select(n.getValue()));
@ -319,6 +322,13 @@ var OverlaySelector = {
m.list = m.prop.getNode("list"); m.list = m.prop.getNode("list");
m.list.getNode("property").setValue(m.result.getPath()); m.list.getNode("property").setValue(m.result.getPath());
if (m.nameprop[0] == `/`)
m.nameprop = substr(m.nameprop, 1);
if (m.sortprop[0] == `/`)
m.sortprop = substr(m.sortprop, 1);
if (m.mpprop)
aircraft.data.add(m.nameprop);
m.rescan(); m.rescan();
m.current = -1; m.current = -1;
m.select(getprop(m.nameprop) or ""); m.select(getprop(m.nameprop) or "");
@ -353,6 +363,8 @@ var OverlaySelector = {
io.read_properties(me.data[me.current][3], props.globals); io.read_properties(me.data[me.current][3], props.globals);
if (last != me.current and me.callback != nil) if (last != me.current and me.callback != nil)
call(me.callback, [me.current] ~ me.data[me.current], me); call(me.callback, [me.current] ~ me.data[me.current], me);
if (me.mpprop)
setprop(me.mpprop, me.data[me.current][2]);
}, },
select: func(name) { select: func(name) {
forindex (var i; me.data) forindex (var i; me.data)