1
0
Fork 0

- make the HUD and the data class singleton classes

- use var everywhere, cosmetics
This commit is contained in:
mfranz 2007-05-11 18:04:51 +00:00
parent c54fbb1e39
commit 47d675bfd5

View file

@ -48,23 +48,21 @@
# creates (if necessary) and returns a property node from arg[0], # creates (if necessary) and returns a property node from arg[0],
# which can be a property node already, or a property path # which can be a property node already, or a property path
# #
makeNode = func { var makeNode = func(n) {
if (isa(arg[0], props.Node)) { if (isa(n, props.Node))
return arg[0]; return n;
} else { else
return props.globals.getNode(arg[0], 1); return props.globals.getNode(n, 1);
}
} }
# returns arg[1]-th optional argument of vector arg[0] or default value arg[2] # returns arg[1]-th optional argument of vector arg[0] or default value arg[2]
# #
optarg = func { var optarg = func {
if (size(arg[0]) > arg[1] and arg[0][arg[1]] != nil) { if (size(arg[0]) > arg[1] and arg[0][arg[1]] != nil)
arg[0][arg[1]]; arg[0][arg[1]];
} else { else
arg[2]; arg[2];
}
} }
@ -90,20 +88,20 @@ optarg = func {
# var canopy = aircraft.door.new("sim/model/foo/canopy", 5); # var canopy = aircraft.door.new("sim/model/foo/canopy", 5);
# canopy.open(); # canopy.open();
# #
door = { var door = {
new : func { new : func {
m = { parents : [door] }; m = { parents : [door] };
m.node = makeNode(arg[0]); m.node = makeNode(arg[0]);
m.swingtime = arg[1]; m.swingtime = arg[1];
m.positionN = m.node.getNode("position-norm", 1); m.positionN = m.node.getNode("position-norm", 1);
m.enabledN = m.node.getNode("enabled", 1); m.enabledN = m.node.getNode("enabled", 1);
if (m.enabledN.getValue() == nil) { if (m.enabledN.getValue() == nil)
m.enabledN.setBoolValue(1); m.enabledN.setBoolValue(1);
}
pos = optarg(arg, 2, 0); pos = optarg(arg, 2, 0);
if (m.positionN.getValue() == nil) { if (m.positionN.getValue() == nil)
m.positionN.setDoubleValue(pos); m.positionN.setDoubleValue(pos);
}
m.target = pos < 0.5; m.target = pos < 0.5;
return m; return m;
}, },
@ -169,7 +167,7 @@ door = {
# aircraft.light.new("sim/model/foo/strobe-top", 1.001, pattern, switch); # aircraft.light.new("sim/model/foo/strobe-top", 1.001, pattern, switch);
# aircraft.light.new("sim/model/foo/strobe-bot", 1.005, pattern, switch); # aircraft.light.new("sim/model/foo/strobe-bot", 1.005, pattern, switch);
# #
light = { var light = {
new : func { new : func {
m = { parents : [light] }; m = { parents : [light] };
m.node = makeNode(arg[0]); m.node = makeNode(arg[0]);
@ -186,21 +184,21 @@ light = {
} }
m.pattern = arg[c]; m.pattern = arg[c];
c += 1; c += 1;
if (size(arg) > c and arg[c] != nil) { if (size(arg) > c and arg[c] != nil)
m.switchN = makeNode(arg[c]); m.switchN = makeNode(arg[c]);
} else { else
m.switchN = m.node.getNode("enabled", 1); m.switchN = m.node.getNode("enabled", 1);
}
if (m.switchN.getValue() == nil) { if (m.switchN.getValue() == nil)
m.switchN.setBoolValue(0); m.switchN.setBoolValue(0);
}
m.stateN = m.node.getNode("state", 1); m.stateN = m.node.getNode("state", 1);
if (m.stateN.getValue() == nil) { if (m.stateN.getValue() == nil)
m.stateN.setBoolValue(0); m.stateN.setBoolValue(0);
}
forindex (var i; m.pattern) { forindex (var i; m.pattern)
m.pattern[i] *= stretch; m.pattern[i] *= stretch;
}
m.index = 0; m.index = 0;
m.loopid = 0; m.loopid = 0;
m.continuous = 0; m.continuous = 0;
@ -295,7 +293,7 @@ light = {
# print(lp.filter(10)); # prints 10 # print(lp.filter(10)); # prints 10
# print(lp.filter(0)); # print(lp.filter(0));
# #
lowpass = { var lowpass = {
new : func(coeff) { new : func(coeff) {
var m = { parents : [lowpass] }; var m = { parents : [lowpass] };
m.dtN = props.globals.getNode("/sim/time/delta-realtime-sec", 1); m.dtN = props.globals.getNode("/sim/time/delta-realtime-sec", 1);
@ -361,18 +359,16 @@ lowpass = {
# # or save now and every 30 sec (and at exit/reinit) # # or save now and every 30 sec (and at exit/reinit)
# aircraft.data.save(0.5); # aircraft.data.save(0.5);
# #
Data = { var data = {
new : func { init : func {
var m = { parents : [Data] }; me.path = getprop("/sim/fg-home") ~ "/aircraft-data/" ~ getprop("/sim/aircraft") ~ ".xml";
m.path = getprop("/sim/fg-home") ~ "/aircraft-data/" ~ getprop("/sim/aircraft") ~ ".xml"; me.signalN = props.globals.getNode("/sim/signals/save", 1);
m.signalN = props.globals.getNode("/sim/signals/save", 1); me.catalog = [];
m.catalog = []; me.loopid = 0;
m.loopid = 0; me.interval = 0;
m.interval = 0;
setlistener("/sim/signals/reinit", func { cmdarg().getBoolValue() and m._save_() }); setlistener("/sim/signals/reinit", func { cmdarg().getBoolValue() and me._save_() });
setlistener("/sim/signals/exit", func { m._save_() }); setlistener("/sim/signals/exit", func { me._save_() });
return m;
}, },
load : func { load : func {
printlog("warn", "trying to load aircraft data from ", me.path, " (OK if not found)"); printlog("warn", "trying to load aircraft data from ", me.path, " (OK if not found)");
@ -396,9 +392,9 @@ Data = {
props.globals.removeChildren(tmp); props.globals.removeChildren(tmp);
var root = props.globals.getNode(tmp, 1); var root = props.globals.getNode(tmp, 1);
foreach (var c; me.catalog) { foreach (var c; me.catalog) {
if (c[0] == `/`) { if (c[0] == `/`)
c = substr(c, 1); c = substr(c, 1);
}
props.copy(props.globals.getNode(c, 1), root.getNode(c, 1)); props.copy(props.globals.getNode(c, 1), root.getNode(c, 1));
} }
fgcommand("savexml", props.Node.new({ "filename": me.path, "sourcenode": tmp })); fgcommand("savexml", props.Node.new({ "filename": me.path, "sourcenode": tmp }));
@ -412,13 +408,11 @@ Data = {
} elsif (t == "scalar") { } elsif (t == "scalar") {
append(me.catalog, a); append(me.catalog, a);
} elsif (t == "vector") { } elsif (t == "vector") {
foreach (var i; a) { foreach (var i; a)
me.add(i); me.add(i);
}
} elsif (t == "hash") { } elsif (t == "hash") {
foreach (var i; keys(a)) { foreach (var i; keys(a))
me.add(a[i]); me.add(a[i]);
}
} else { } else {
die("aircraft.data.add(): invalid item of type " ~ t); die("aircraft.data.add(): invalid item of type " ~ t);
} }
@ -449,13 +443,13 @@ Data = {
# #
# aircraft.timer.new("/sim/time/hobbs/battery", 60).start(); # anonymous timer # aircraft.timer.new("/sim/time/hobbs/battery", 60).start(); # anonymous timer
# #
timer = { var timer = {
new : func(prop, res = 1, save = 1) { new : func(prop, res = 1, save = 1) {
var m = { parents : [timer] }; var m = { parents : [timer] };
m.node = makeNode(prop); m.node = makeNode(prop);
if (m.node.getType() == "NONE") { if (m.node.getType() == "NONE")
m.node.setDoubleValue(0); m.node.setDoubleValue(0);
}
m.systimeN = props.globals.getNode("/sim/time/elapsed-sec", 1); m.systimeN = props.globals.getNode("/sim/time/elapsed-sec", 1);
m.last_systime = nil; m.last_systime = nil;
m.interval = res; m.interval = res;
@ -471,9 +465,8 @@ timer = {
}, },
del : func { del : func {
me.stop(); me.stop();
if (me.saveL != nil) { if (me.saveL != nil)
removelistener(me.saveL); removelistener(me.saveL);
}
}, },
start : func { start : func {
me.running and return; me.running and return;
@ -498,9 +491,8 @@ timer = {
me.last_systime = sys; me.last_systime = sys;
}, },
_save_ : func { _save_ : func {
if (me.running) { if (me.running)
me._apply_(); me._apply_();
}
}, },
_loop_ : func(id) { _loop_ : func(id) {
id != me.loopid and return; id != me.loopid and return;
@ -574,21 +566,19 @@ var steering = {
# HUD control class to handle both HUD implementations # HUD control class to handle both HUD implementations
# ============================================================================== # ==============================================================================
# #
HUDControl = { var HUD = {
new : func { init : func {
var m = { parents : [HUDControl] }; me.vis0N = props.globals.getNode("/sim/hud/visibility[0]", 1);
m.vis0N = props.globals.getNode("/sim/hud/visibility[0]", 1); me.vis1N = props.globals.getNode("/sim/hud/visibility[1]", 1);
m.vis1N = props.globals.getNode("/sim/hud/visibility[1]", 1); me.currcolN = props.globals.getNode("/sim/hud/current-color", 1);
m.currcolN = props.globals.getNode("/sim/hud/current-color", 1); me.paletteN = props.globals.getNode("/sim/hud/palette", 1);
m.paletteN = props.globals.getNode("/sim/hud/palette", 1); me.brightnessN = props.globals.getNode("/sim/hud/color/brightness", 1);
m.brightnessN = props.globals.getNode("/sim/hud/color/brightness", 1); me.currentN = me.vis0N;
m.currentN = m.vis0N;
return m;
}, },
cycle_color : func { # h-key cycle_color : func { # h-key
if (!me.currentN.getBoolValue()) { # if off, turn on if (!me.currentN.getBoolValue()) # if off, turn on
return me.currentN.setBoolValue(1); return me.currentN.setBoolValue(1);
}
var i = me.currcolN.getValue() + 1; # if through, turn off var i = me.currcolN.getValue() + 1; # if through, turn off
if (i < 0 or i >= size(me.paletteN.getChildren("color"))) { if (i < 0 or i >= size(me.paletteN.getChildren("color"))) {
me.currentN.setBoolValue(0); me.currentN.setBoolValue(0);
@ -633,30 +623,23 @@ HUDControl = {
# module initialization # module initialization
# ============================================================================== # ==============================================================================
# #
var HUD = nil;
var data = nil;
var L = _setlistener("/sim/signals/nasal-dir-initialized", func { _setlistener("/sim/signals/nasal-dir-initialized", func {
removelistener(L);
props.globals.getNode("/sim/time/delta-realtime-sec", 1).setDoubleValue(0.00000001); props.globals.getNode("/sim/time/delta-realtime-sec", 1).setDoubleValue(0.00000001);
HUD = HUDControl.new(); HUD.init();
data.init();
data = Data.new();
Data.new = nil;
if (getprop("/sim/startup/save-on-exit")) { if (getprop("/sim/startup/save-on-exit")) {
data.load(); data.load();
var n = props.globals.getNode("/sim/aircraft-data"); var n = props.globals.getNode("/sim/aircraft-data");
if (n != nil) { if (n != nil)
foreach (var c; n.getChildren("path")) { foreach (var c; n.getChildren("path"))
if (c.getType() != "NONE") { if (c.getType() != "NONE")
data.add(c.getValue()); data.add(c.getValue());
}
}
}
} else { } else {
Data._save_ = func {} data._save_ = func {}
Data._loop_ = func {} data._loop_ = func {}
} }
}); });