1
0
Fork 0

- make view_manager singleton class

- simplify lookat wrapper with a call()
- cosmetics
This commit is contained in:
mfranz 2007-05-11 18:59:34 +00:00
parent 47d675bfd5
commit 5f97465d6e

View file

@ -22,7 +22,7 @@
# All offsets are by default 0, and you only need to set them if they should # All offsets are by default 0, and you only need to set them if they should
# be non-zero. The registered function is called for each frame and the respective # be non-zero. The registered function is called for each frame and the respective
# view parameters are set accordingly. The function can access all internal # view parameters are set accordingly. The function can access all internal
# variables of the ViewManager class, such as me.roll, me.pitch, etc., and it # variables of the view_manager class, such as me.roll, me.pitch, etc., and it
# can, of course, also use module variables from the file where it's defined. # can, of course, also use module variables from the file where it's defined.
# #
# The following commands move smoothly to a fixed view position and back. # The following commands move smoothly to a fixed view position and back.
@ -48,12 +48,10 @@ var clamp = func(v, min, max) { v < min ? min : v > max ? max : v }
var normatan = func(x) { math.atan2(x, 1) * 2 / math.pi } var normatan = func(x) { math.atan2(x, 1) * 2 / math.pi }
var normdeg = func(a) { var normdeg = func(a) {
while (a >= 180) { while (a >= 180)
a -= 360; a -= 360;
} while (a < -180)
while (a < -180) {
a += 360; a += 360;
}
return a; return a;
} }
@ -75,14 +73,12 @@ var Input = {
}, },
get : func { get : func {
var v = me.prop.getValue() * me.factor + me.offset; var v = me.prop.getValue() * me.factor + me.offset;
if (me.min != nil and v < me.min) { if (me.min != nil and v < me.min)
v = me.min; v = me.min;
} if (me.max != nil and v > me.max)
if (me.max != nil and v > me.max) {
v = me.max; v = me.max;
}
return me.lowpass == nil ? v : me.lowpass.filter(v);
return me.lowpass == nil ? v : me.lowpass.filter(v);
}, },
set : func(v) { set : func(v) {
me.prop.setDoubleValue(v); me.prop.setDoubleValue(v);
@ -97,9 +93,9 @@ var ViewAxis = {
new : func(prop) { new : func(prop) {
var m = { parents : [ViewAxis] }; var m = { parents : [ViewAxis] };
m.prop = props.globals.getNode(prop, 1); m.prop = props.globals.getNode(prop, 1);
if (m.prop.getType() == "NONE") { if (m.prop.getType() == "NONE")
m.prop.setDoubleValue(0); m.prop.setDoubleValue(0);
}
m.reset(); m.reset();
return m; return m;
}, },
@ -126,64 +122,61 @@ var ViewAxis = {
# Class that manages a dynamic cockpit view by manipulating # Singleton class that manages a dynamic cockpit view by manipulating
# sim/current-view/goal-*-offset-deg properties. # sim/current-view/goal-*-offset-deg properties.
# #
var ViewManager = { var view_manager = {
new : func { init : func {
var m = { parents : [ViewManager] }; me.elapsedN = props.globals.getNode("/sim/time/elapsed-sec", 1);
m.elapsedN = props.globals.getNode("/sim/time/elapsed-sec", 1); me.deltaN = props.globals.getNode("/sim/time/delta-realtime-sec", 1);
m.deltaN = props.globals.getNode("/sim/time/delta-realtime-sec", 1);
m.headingN = props.globals.getNode("/orientation/heading-deg", 1); me.headingN = props.globals.getNode("/orientation/heading-deg", 1);
m.pitchN = props.globals.getNode("/orientation/pitch-deg", 1); me.pitchN = props.globals.getNode("/orientation/pitch-deg", 1);
m.rollN = props.globals.getNode("/orientation/roll-deg", 1); me.rollN = props.globals.getNode("/orientation/roll-deg", 1);
m.slipN = props.globals.getNode("/orientation/side-slip-deg", 1); me.slipN = props.globals.getNode("/orientation/side-slip-deg", 1);
m.speedN = props.globals.getNode("velocities/airspeed-kt", 1); me.speedN = props.globals.getNode("velocities/airspeed-kt", 1);
m.wind_dirN = props.globals.getNode("/environment/wind-from-heading-deg", 1); me.wind_dirN = props.globals.getNode("/environment/wind-from-heading-deg", 1);
m.wind_speedN = props.globals.getNode("/environment/wind-speed-kt", 1); me.wind_speedN = props.globals.getNode("/environment/wind-speed-kt", 1);
m.axes = [ me.axes = [
m.heading_axis = ViewAxis.new("/sim/current-view/goal-heading-offset-deg"), me.heading_axis = ViewAxis.new("/sim/current-view/goal-heading-offset-deg"),
m.pitch_axis = ViewAxis.new("/sim/current-view/goal-pitch-offset-deg"), me.pitch_axis = ViewAxis.new("/sim/current-view/goal-pitch-offset-deg"),
m.roll_axis = ViewAxis.new("/sim/current-view/goal-roll-offset-deg"), me.roll_axis = ViewAxis.new("/sim/current-view/goal-roll-offset-deg"),
m.x_axis = ViewAxis.new("/sim/current-view/x-offset-m"), me.x_axis = ViewAxis.new("/sim/current-view/x-offset-m"),
m.y_axis = ViewAxis.new("/sim/current-view/y-offset-m"), me.y_axis = ViewAxis.new("/sim/current-view/y-offset-m"),
m.z_axis = ViewAxis.new("/sim/current-view/z-offset-m"), me.z_axis = ViewAxis.new("/sim/current-view/z-offset-m"),
m.fov_axis = ViewAxis.new("/sim/current-view/field-of-view"), me.fov_axis = ViewAxis.new("/sim/current-view/field-of-view"),
]; ];
# accelerations are converted to G (Earth gravitation is omitted) # accelerations are converted to G (Earth gravitation is omitted)
m.ax = Input.new("/accelerations/pilot/x-accel-fps_sec", 0.03108095, 0, 0.58, 0); me.ax = Input.new("/accelerations/pilot/x-accel-fps_sec", 0.03108095, 0, 0.58, 0);
m.ay = Input.new("/accelerations/pilot/y-accel-fps_sec", 0.03108095, 0, 0.95); me.ay = Input.new("/accelerations/pilot/y-accel-fps_sec", 0.03108095, 0, 0.95);
m.az = Input.new("/accelerations/pilot/z-accel-fps_sec", -0.03108095, -1, 0.46); me.az = Input.new("/accelerations/pilot/z-accel-fps_sec", -0.03108095, -1, 0.46);
# velocities are converted to knots # velocities are converted to knots
m.vx = Input.new("/velocities/uBody-fps", 0.5924838, 0, 0.45); me.vx = Input.new("/velocities/uBody-fps", 0.5924838, 0, 0.45);
m.vy = Input.new("/velocities/vBody-fps", 0.5924838, 0); me.vy = Input.new("/velocities/vBody-fps", 0.5924838, 0);
m.vz = Input.new("/velocities/wBody-fps", 0.5924838, 0); me.vz = Input.new("/velocities/wBody-fps", 0.5924838, 0);
# turn WoW bool into smooth values ranging from 0 to 1 # turn WoW bool into smooth values ranging from 0 to 1
m.wow = Input.new("/gear/gear/wow", 1, 0, 0.74); me.wow = Input.new("/gear/gear/wow", 1, 0, 0.74);
m.hdg_change = aircraft.lowpass.new(0.95); me.hdg_change = aircraft.lowpass.new(0.95);
m.ubody = aircraft.lowpass.new(0.95); me.ubody = aircraft.lowpass.new(0.95);
m.last_heading = m.headingN.getValue(); me.last_heading = me.headingN.getValue();
m.size_factor = getprop("/sim/chase-distance-m") / -25; me.size_factor = getprop("/sim/chase-distance-m") / -25;
# "lookat" blending # "lookat" blending
m.blendN = props.globals.getNode("/sim/view/dynamic/blend", 1); me.blendN = props.globals.getNode("/sim/view/dynamic/blend", 1);
m.blendN.setDoubleValue(0); me.blendN.setDoubleValue(0);
m.blendtime = BLEND_TIME; me.blendtime = BLEND_TIME;
m.frozen = 0; me.frozen = 0;
if (props.globals.getNode("rotors", 0) != nil) { if (props.globals.getNode("rotors", 0) != nil)
m.calculate = m.default_helicopter; me.calculate = me.default_helicopter;
} else { else
m.calculate = m.default_plane; me.calculate = me.default_plane;
} me.reset();
m.reset();
return m;
}, },
reset : func { reset : func {
me.heading_offset = me.heading = me.target_heading = 0; me.heading_offset = me.heading = me.target_heading = 0;
@ -196,9 +189,9 @@ var ViewManager = {
interpolate(me.blendN); interpolate(me.blendN);
me.blendN.setDoubleValue(0); me.blendN.setDoubleValue(0);
foreach (var a; me.axes) { foreach (var a; me.axes)
a.reset(); a.reset();
}
me.add_offset(); me.add_offset();
}, },
add_offset : func { add_offset : func {
@ -208,11 +201,10 @@ var ViewManager = {
me.fov_axis.add_offset(); me.fov_axis.add_offset();
}, },
apply : func { apply : func {
if (me.elapsedN.getValue() < me.frozen) { if (me.elapsedN.getValue() < me.frozen)
return; return;
} elsif (me.frozen) { elsif (me.frozen)
me.unfreeze(); me.unfreeze();
}
me.pitch = me.pitchN.getValue(); me.pitch = me.pitchN.getValue();
me.roll = me.rollN.getValue(); me.roll = me.rollN.getValue();
@ -279,7 +271,7 @@ var ViewManager = {
# default calculations for a plane # default calculations for a plane
# #
ViewManager.default_plane = func { view_manager.default_plane = func {
var wow = me.wow.get(); var wow = me.wow.get();
# calculate steering factor # calculate steering factor
@ -314,7 +306,7 @@ ViewManager.default_plane = func {
# default calculations for a helicopter # default calculations for a helicopter
# #
ViewManager.default_helicopter = func { view_manager.default_helicopter = func {
var lowspeed = 1 - normatan(me.speedN.getValue() / 20); var lowspeed = 1 - normatan(me.speedN.getValue() / 20);
me.heading_offset = # view heading due to me.heading_offset = # view heading due to
@ -334,15 +326,12 @@ ViewManager.default_helicopter = func {
# /sim/view[0]/dynamic/enabled is true. # /sim/view[0]/dynamic/enabled is true.
# #
var main_loop = func(id) { var main_loop = func(id) {
if (id != loop_id) { id == loop_id or return;
return;
}
if (cockpit_view and !panel_visible) { if (cockpit_view and !panel_visible) {
if (mouse_button) { if (mouse_button)
freeze(); freeze();
} else { else
view_manager.apply(); view_manager.apply();
}
} }
settimer(func { main_loop(id) }, 0); settimer(func { main_loop(id) }, 0);
} }
@ -350,9 +339,8 @@ var main_loop = func(id) {
var freeze = func { var freeze = func {
if (mouse_mode == 0) { if (mouse_mode == 0)
view_manager.freeze(); view_manager.freeze();
}
} }
var register = func(f) { var register = func(f) {
@ -363,8 +351,8 @@ var reset = func {
view_manager.reset(); view_manager.reset();
} }
var lookat = func(h, p, r, x, y, z, s = BLEND_TIME, f = 55) { var lookat = func {
view_manager.lookat(h, p, r, x, y, z, s, f); call(view_manager.lookat, arg, view_manager);
} }
var resume = func { var resume = func {
@ -375,7 +363,6 @@ var resume = func {
var original_resetView = nil; var original_resetView = nil;
var panel_visibilityN = nil; var panel_visibilityN = nil;
var dynamic_view = nil; var dynamic_view = nil;
var view_manager = nil;
var cockpit_view = nil; var cockpit_view = nil;
var panel_visible = nil; # whether 2D panel is visible var panel_visible = nil; # whether 2D panel is visible
@ -388,8 +375,7 @@ var loop_id = 0;
# Initialization. # Initialization.
# #
var L = _setlistener("/sim/signals/nasal-dir-initialized", func { _setlistener("/sim/signals/nasal-dir-initialized", func {
removelistener(L);
# disable menu entry and return for inappropriate FDMs (see Main/fg_init.cxx) # disable menu entry and return for inappropriate FDMs (see Main/fg_init.cxx)
var fdms = { var fdms = {
acms:0, ada:0, balloon:0, external:0, acms:0, ada:0, balloon:0, external:0,
@ -397,9 +383,8 @@ var L = _setlistener("/sim/signals/nasal-dir-initialized", func {
null:0, pipe:0, ufo:0, yasim:1, null:0, pipe:0, ufo:0, yasim:1,
}; };
var fdm = getprop("/sim/flight-model"); var fdm = getprop("/sim/flight-model");
if (!contains(fdms, fdm) or !fdms[fdm]) { if (!contains(fdms, fdm) or !fdms[fdm])
return gui.menuEnable("dynamic-view", 0); return gui.menuEnable("dynamic-view", 0);
}
# some properties may still be unavailable or nil # some properties may still be unavailable or nil
props.globals.getNode("/accelerations/pilot/x-accel-fps_sec", 1).setDoubleValue(0); props.globals.getNode("/accelerations/pilot/x-accel-fps_sec", 1).setDoubleValue(0);
@ -411,15 +396,14 @@ var L = _setlistener("/sim/signals/nasal-dir-initialized", func {
# let listeners keep some variables up-to-date, so that they don't have # let listeners keep some variables up-to-date, so that they don't have
# to be queried in the loop # to be queried in the loop
setlistener("/sim/current-view/view-number", func { cockpit_view = !cmdarg().getValue() }, 1);
setlistener("/sim/panel/visibility", func { panel_visible = cmdarg().getValue() }, 1); setlistener("/sim/panel/visibility", func { panel_visible = cmdarg().getValue() }, 1);
setlistener("/sim/current-view/view-number", func { cockpit_view = !cmdarg().getValue() }, 1);
setlistener("/devices/status/mice/mouse/button", func { mouse_button = cmdarg().getValue() }, 1); setlistener("/devices/status/mice/mouse/button", func { mouse_button = cmdarg().getValue() }, 1);
setlistener("/devices/status/mice/mouse/x", freeze); setlistener("/devices/status/mice/mouse/x", freeze);
setlistener("/devices/status/mice/mouse/y", freeze); setlistener("/devices/status/mice/mouse/y", freeze);
setlistener("/devices/status/mice/mouse/mode", func { setlistener("/devices/status/mice/mouse/mode", func {
if (mouse_mode = cmdarg().getValue()) { if (mouse_mode = cmdarg().getValue())
view_manager.unfreeze(); view_manager.unfreeze();
}
}, 1); }, 1);
setlistener("/sim/signals/reinit", func { setlistener("/sim/signals/reinit", func {
@ -428,14 +412,13 @@ var L = _setlistener("/sim/signals/nasal-dir-initialized", func {
view_manager.reset(); view_manager.reset();
}, 0); }, 0);
view_manager = ViewManager.new(); view_manager.init();
original_resetView = view.resetView; original_resetView = view.resetView;
view.resetView = func { view.resetView = func {
original_resetView(); original_resetView();
if (cockpit_view and dynamic_view) { if (cockpit_view and dynamic_view)
view_manager.add_offset(); view_manager.add_offset();
}
} }
settimer(func { settimer(func {
@ -443,9 +426,8 @@ var L = _setlistener("/sim/signals/nasal-dir-initialized", func {
dynamic_view = cmdarg().getBoolValue(); dynamic_view = cmdarg().getBoolValue();
loop_id += 1; loop_id += 1;
view.resetView(); view.resetView();
if (dynamic_view) { if (dynamic_view)
main_loop(loop_id); main_loop(loop_id);
}
}, 1); }, 1);
}, 0); }, 0);
}); });