2006-08-22 10:06:46 +00:00
|
|
|
# Dynamic Cockpit View manager. Tries to simulate the pilot's most likely
|
|
|
|
# deliberate view direction. Doesn't consider forced view changes due to
|
|
|
|
# acceleration.
|
2007-02-10 23:47:02 +00:00
|
|
|
#
|
|
|
|
# To override the default recipes, put something like this into one of
|
|
|
|
# your aircraft's Nasal files:
|
|
|
|
#
|
|
|
|
# dynamic_view.register(func {
|
|
|
|
# # me.default_plane(); # uncomment one of these if you want
|
|
|
|
# # me.default_helicopter(); # to base your code on the defaults
|
|
|
|
#
|
|
|
|
# # positive values rotate (deg) or move (m)
|
|
|
|
# me.heading_offset = ... # left
|
|
|
|
# me.pitch_offset = ... # up
|
|
|
|
# me.roll_offset = ... # right
|
|
|
|
# me.x_offset = ... # right (transversal axis)
|
|
|
|
# me.y_offset = ... # up (vertical axis)
|
|
|
|
# me.z_offset = ... # back/aft (longitudinal axis)
|
|
|
|
# me.fov_offset = ... # zoom out (field of view)
|
|
|
|
# });
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# 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
|
|
|
|
# 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.
|
|
|
|
# All values are relative to aircraft origin (absolute), not relative to
|
2007-02-23 22:17:55 +00:00
|
|
|
# the default cockpit view position. The time and field-of-view argument
|
2007-02-11 16:56:15 +00:00
|
|
|
# is optional.
|
2007-02-10 23:47:02 +00:00
|
|
|
#
|
2007-02-23 22:17:55 +00:00
|
|
|
# dynamic_view.lookat(hdg, pitch, roll, x, y, z [, time=0.2 [, fov=55]]);
|
2007-02-10 23:47:02 +00:00
|
|
|
# dynamic_view.resume();
|
|
|
|
|
2006-08-17 15:59:44 +00:00
|
|
|
|
2007-02-23 22:17:55 +00:00
|
|
|
var FREEZE_DURATION = 2;
|
|
|
|
var BLEND_TIME = 0.2;
|
|
|
|
|
|
|
|
|
2007-02-06 14:33:59 +00:00
|
|
|
var sin = func(a) { math.sin(a * math.pi / 180.0) }
|
|
|
|
var cos = func(a) { math.cos(a * math.pi / 180.0) }
|
|
|
|
var sigmoid = func(x) { 1 / (1 + math.exp(-x)) }
|
|
|
|
var nsigmoid = func(x) { 2 / (1 + math.exp(-x)) - 1 }
|
2007-02-10 20:42:36 +00:00
|
|
|
var pow = func(v, w) { v < 0 ? nil : v == 0 ? 0 : math.exp(math.ln(v) * w) }
|
|
|
|
var npow = func(v, w) { v == 0 ? 0 : math.exp(math.ln(abs(v)) * w) * (v < 0 ? -1 : 1) }
|
2007-02-06 14:33:59 +00:00
|
|
|
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 normdeg = func(a) {
|
2006-11-02 21:06:36 +00:00
|
|
|
while (a >= 180) {
|
|
|
|
a -= 360;
|
|
|
|
}
|
|
|
|
while (a < -180) {
|
|
|
|
a += 360;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2006-08-19 22:24:24 +00:00
|
|
|
|
|
|
|
|
2006-08-17 15:59:44 +00:00
|
|
|
# Class that reads a property value, applies factor & offset, clamps to min & max,
|
2006-08-22 10:06:46 +00:00
|
|
|
# and optionally lowpass filters.
|
2006-08-17 15:59:44 +00:00
|
|
|
#
|
2007-02-06 14:33:59 +00:00
|
|
|
var Input = {
|
2006-08-22 10:06:46 +00:00
|
|
|
new : func(prop = "/null", factor = 1, offset = 0, filter = 0, min = nil, max = nil) {
|
2006-08-17 15:59:44 +00:00
|
|
|
var m = { parents : [Input] };
|
|
|
|
m.prop = isa(props.Node, prop) ? prop : props.globals.getNode(prop, 1);
|
|
|
|
m.factor = factor;
|
|
|
|
m.offset = offset;
|
|
|
|
m.min = min;
|
|
|
|
m.max = max;
|
2006-11-03 17:38:32 +00:00
|
|
|
m.lowpass = filter ? aircraft.lowpass.new(filter) : nil;
|
2006-08-17 15:59:44 +00:00
|
|
|
return m;
|
|
|
|
},
|
|
|
|
get : func {
|
|
|
|
var v = me.prop.getValue() * me.factor + me.offset;
|
|
|
|
if (me.min != nil and v < me.min) {
|
|
|
|
v = me.min;
|
|
|
|
}
|
|
|
|
if (me.max != nil and v > me.max) {
|
|
|
|
v = me.max;
|
|
|
|
}
|
2006-08-22 10:06:46 +00:00
|
|
|
return me.lowpass == nil ? v : me.lowpass.filter(v);
|
2006-08-17 15:59:44 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
set : func(v) {
|
|
|
|
me.prop.setDoubleValue(v);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Class that maintains one sim/current-view/goal-*-offset-deg property.
|
|
|
|
#
|
2007-02-06 14:33:59 +00:00
|
|
|
var ViewAxis = {
|
2006-08-17 15:59:44 +00:00
|
|
|
new : func(prop) {
|
|
|
|
var m = { parents : [ViewAxis] };
|
2007-02-07 22:13:51 +00:00
|
|
|
m.prop = props.globals.getNode(prop, 1);
|
|
|
|
if (m.prop.getType() == "NONE") {
|
|
|
|
m.prop.setDoubleValue(0);
|
|
|
|
}
|
2006-08-17 15:59:44 +00:00
|
|
|
m.reset();
|
|
|
|
return m;
|
|
|
|
},
|
|
|
|
reset : func {
|
|
|
|
me.applied_offset = 0;
|
|
|
|
},
|
|
|
|
add_offset : func {
|
|
|
|
me.prop.setValue(me.prop.getValue() + me.applied_offset);
|
|
|
|
},
|
2006-12-10 23:09:42 +00:00
|
|
|
sub_offset : func {
|
|
|
|
var raw = me.prop.getValue() - me.applied_offset;
|
|
|
|
me.prop.setValue(raw);
|
|
|
|
return raw;
|
|
|
|
},
|
2006-08-18 17:00:16 +00:00
|
|
|
apply : func(v) {
|
|
|
|
var raw = me.prop.getValue() - me.applied_offset;
|
|
|
|
me.applied_offset = v;
|
|
|
|
me.prop.setDoubleValue(raw + me.applied_offset);
|
|
|
|
},
|
2007-02-07 16:58:46 +00:00
|
|
|
static : func(v) {
|
|
|
|
normdeg(v - me.prop.getValue() + me.applied_offset);
|
|
|
|
},
|
2006-08-17 15:59:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Class that manages a dynamic cockpit view by manipulating
|
|
|
|
# sim/current-view/goal-*-offset-deg properties.
|
|
|
|
#
|
2007-02-06 14:33:59 +00:00
|
|
|
var ViewManager = {
|
2006-12-09 22:42:13 +00:00
|
|
|
new : func {
|
2006-08-17 15:59:44 +00:00
|
|
|
var m = { parents : [ViewManager] };
|
2006-08-22 10:06:46 +00:00
|
|
|
m.elapsedN = props.globals.getNode("/sim/time/elapsed-sec", 1);
|
|
|
|
m.deltaN = props.globals.getNode("/sim/time/delta-realtime-sec", 1);
|
|
|
|
|
2006-08-18 17:00:16 +00:00
|
|
|
m.headingN = props.globals.getNode("/orientation/heading-deg", 1);
|
|
|
|
m.pitchN = props.globals.getNode("/orientation/pitch-deg", 1);
|
|
|
|
m.rollN = props.globals.getNode("/orientation/roll-deg", 1);
|
2006-08-19 22:24:24 +00:00
|
|
|
m.slipN = props.globals.getNode("/orientation/side-slip-deg", 1);
|
2006-12-07 22:38:07 +00:00
|
|
|
m.speedN = props.globals.getNode("velocities/airspeed-kt", 1);
|
2006-08-19 22:24:24 +00:00
|
|
|
|
|
|
|
m.wind_dirN = props.globals.getNode("/environment/wind-from-heading-deg", 1);
|
|
|
|
m.wind_speedN = props.globals.getNode("/environment/wind-speed-kt", 1);
|
2006-08-17 15:59:44 +00:00
|
|
|
|
2007-02-08 22:16:10 +00:00
|
|
|
m.axes = [
|
|
|
|
m.heading_axis = ViewAxis.new("/sim/current-view/goal-heading-offset-deg"),
|
|
|
|
m.pitch_axis = ViewAxis.new("/sim/current-view/goal-pitch-offset-deg"),
|
|
|
|
m.roll_axis = ViewAxis.new("/sim/current-view/goal-roll-offset-deg"),
|
|
|
|
m.x_axis = ViewAxis.new("/sim/current-view/x-offset-m"),
|
|
|
|
m.y_axis = ViewAxis.new("/sim/current-view/y-offset-m"),
|
|
|
|
m.z_axis = ViewAxis.new("/sim/current-view/z-offset-m"),
|
|
|
|
m.fov_axis = ViewAxis.new("/sim/current-view/field-of-view"),
|
|
|
|
];
|
2007-02-07 22:13:51 +00:00
|
|
|
|
2007-02-06 16:25:44 +00:00
|
|
|
# accelerations are converted to G (Earth gravitation is omitted)
|
2006-12-10 21:50:59 +00:00
|
|
|
m.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);
|
|
|
|
m.az = Input.new("/accelerations/pilot/z-accel-fps_sec", -0.03108095, -1, 0.46);
|
2006-08-17 15:59:44 +00:00
|
|
|
|
|
|
|
# velocities are converted to knots
|
2006-12-10 21:50:59 +00:00
|
|
|
m.vx = Input.new("/velocities/uBody-fps", 0.5924838, 0, 0.45);
|
|
|
|
m.vy = Input.new("/velocities/vBody-fps", 0.5924838, 0);
|
|
|
|
m.vz = Input.new("/velocities/wBody-fps", 0.5924838, 0);
|
2006-08-17 15:59:44 +00:00
|
|
|
|
2006-08-24 20:55:18 +00:00
|
|
|
# turn WoW bool into smooth values ranging from 0 to 1
|
2006-12-10 21:50:59 +00:00
|
|
|
m.wow = Input.new("/gear/gear/wow", 1, 0, 0.74);
|
2006-11-03 17:38:32 +00:00
|
|
|
m.hdg_change = aircraft.lowpass.new(0.95);
|
|
|
|
m.ubody = aircraft.lowpass.new(0.95);
|
2006-08-19 22:24:24 +00:00
|
|
|
m.last_heading = m.headingN.getValue();
|
2006-12-11 18:37:10 +00:00
|
|
|
m.size_factor = getprop("/sim/chase-distance-m") / -25;
|
2006-12-09 22:42:13 +00:00
|
|
|
|
2007-02-08 22:16:10 +00:00
|
|
|
# "lookat" blending
|
2007-02-06 16:25:44 +00:00
|
|
|
m.blendN = props.globals.getNode("/sim/view/dynamic/blend", 1);
|
|
|
|
m.blendN.setDoubleValue(0);
|
2007-02-23 22:17:55 +00:00
|
|
|
m.blendtime = BLEND_TIME;
|
2007-02-06 16:25:44 +00:00
|
|
|
m.frozen = 0;
|
|
|
|
|
2006-12-09 22:42:13 +00:00
|
|
|
if (props.globals.getNode("rotors", 0) != nil) {
|
2006-12-10 21:50:59 +00:00
|
|
|
m.calculate = m.default_helicopter;
|
2006-12-09 22:42:13 +00:00
|
|
|
} else {
|
2006-12-10 21:50:59 +00:00
|
|
|
m.calculate = m.default_plane;
|
2006-12-09 22:42:13 +00:00
|
|
|
}
|
2006-08-17 15:59:44 +00:00
|
|
|
m.reset();
|
|
|
|
return m;
|
|
|
|
},
|
|
|
|
reset : func {
|
2007-02-10 23:47:02 +00:00
|
|
|
me.heading_offset = me.heading = me.target_heading = 0;
|
|
|
|
me.pitch_offset = me.pitch = me.target_pitch = 0;
|
|
|
|
me.roll_offset = me.roll = me.target_roll = 0;
|
|
|
|
me.x_offset = me.x = me.target_x = 0;
|
|
|
|
me.y_offset = me.y = me.target_y = 0;
|
|
|
|
me.z_offset = me.z = me.target_z = 0;
|
|
|
|
me.fov_offset = me.fov = me.target_fov = 0;
|
|
|
|
|
2007-02-06 16:25:44 +00:00
|
|
|
interpolate(me.blendN);
|
|
|
|
me.blendN.setDoubleValue(0);
|
2007-02-08 22:16:10 +00:00
|
|
|
foreach (var a; me.axes) {
|
|
|
|
a.reset();
|
|
|
|
}
|
2006-12-03 23:59:50 +00:00
|
|
|
me.add_offset();
|
2006-08-17 15:59:44 +00:00
|
|
|
},
|
2006-08-18 17:00:16 +00:00
|
|
|
add_offset : func {
|
|
|
|
me.heading_axis.add_offset();
|
|
|
|
me.pitch_axis.add_offset();
|
|
|
|
me.roll_axis.add_offset();
|
2007-02-08 22:16:10 +00:00
|
|
|
me.fov_axis.add_offset();
|
2006-08-18 17:00:16 +00:00
|
|
|
},
|
2006-08-17 15:59:44 +00:00
|
|
|
apply : func {
|
2007-02-06 16:25:44 +00:00
|
|
|
if (me.elapsedN.getValue() < me.frozen) {
|
|
|
|
return;
|
|
|
|
} elsif (me.frozen) {
|
|
|
|
me.unfreeze();
|
|
|
|
}
|
|
|
|
|
2006-12-09 22:42:13 +00:00
|
|
|
me.pitch = me.pitchN.getValue();
|
|
|
|
me.roll = me.rollN.getValue();
|
|
|
|
|
2006-12-10 21:50:59 +00:00
|
|
|
me.calculate();
|
2007-02-06 16:25:44 +00:00
|
|
|
|
|
|
|
var b = me.blendN.getValue();
|
|
|
|
var B = 1 - b;
|
|
|
|
me.heading = me.target_heading * b + me.heading_offset * B;
|
|
|
|
me.pitch = me.target_pitch * b + me.pitch_offset * B;
|
|
|
|
me.roll = me.target_roll * b + me.roll_offset * B;
|
2007-02-08 22:16:10 +00:00
|
|
|
me.x = me.target_x * b + me.x_offset * B;
|
|
|
|
me.y = me.target_y * b + me.y_offset * B;
|
|
|
|
me.z = me.target_z * b + me.z_offset * B;
|
|
|
|
me.fov = me.target_fov * b + me.fov_offset * B;
|
2007-02-06 16:25:44 +00:00
|
|
|
|
|
|
|
me.heading_axis.apply(me.heading);
|
|
|
|
me.pitch_axis.apply(me.pitch);
|
|
|
|
me.roll_axis.apply(me.roll);
|
2007-02-08 22:16:10 +00:00
|
|
|
me.x_axis.apply(me.x);
|
|
|
|
me.y_axis.apply(me.y);
|
|
|
|
me.z_axis.apply(me.z);
|
|
|
|
me.fov_axis.apply(me.fov);
|
2006-12-09 22:42:13 +00:00
|
|
|
},
|
2007-02-23 22:17:55 +00:00
|
|
|
lookat : func(heading, pitch, roll, x, y, z, time, fov) {
|
2007-02-08 22:16:10 +00:00
|
|
|
me.target_heading = me.heading_axis.static(heading);
|
|
|
|
me.target_pitch = me.pitch_axis.static(pitch);
|
|
|
|
me.target_roll = me.roll_axis.static(roll);
|
|
|
|
me.target_x = me.x_axis.static(x);
|
|
|
|
me.target_y = me.y_axis.static(y);
|
|
|
|
me.target_z = me.z_axis.static(z);
|
|
|
|
me.target_fov = me.fov_axis.static(fov);
|
2007-02-11 16:56:15 +00:00
|
|
|
|
2007-02-23 22:17:55 +00:00
|
|
|
me.blendtime = time;
|
2007-02-11 16:56:15 +00:00
|
|
|
me.blendN.setValue(0);
|
2007-02-23 22:17:55 +00:00
|
|
|
interpolate(me.blendN, 1, me.blendtime);
|
2007-02-08 22:16:10 +00:00
|
|
|
},
|
|
|
|
resume : func {
|
2007-02-23 22:17:55 +00:00
|
|
|
interpolate(me.blendN, 0, me.blendtime);
|
|
|
|
me.blendtime = BLEND_TIME;
|
2007-02-06 16:25:44 +00:00
|
|
|
},
|
|
|
|
freeze : func {
|
|
|
|
if (!me.frozen) {
|
|
|
|
me.target_heading = me.heading;
|
|
|
|
me.target_pitch = me.pitch;
|
|
|
|
me.target_roll = me.roll;
|
2007-02-08 22:16:10 +00:00
|
|
|
me.target_x = me.x;
|
|
|
|
me.target_y = me.y;
|
|
|
|
me.target_z = me.z;
|
|
|
|
me.target_fov = me.fov;
|
2007-02-06 16:25:44 +00:00
|
|
|
me.blendN.setDoubleValue(1);
|
2006-12-09 22:42:13 +00:00
|
|
|
}
|
2007-02-11 16:56:15 +00:00
|
|
|
me.frozen = me.elapsedN.getValue() + FREEZE_DURATION;
|
2007-02-06 16:25:44 +00:00
|
|
|
},
|
|
|
|
unfreeze : func {
|
2007-02-23 22:17:55 +00:00
|
|
|
if (me.frozen) {
|
|
|
|
me.frozen = 0;
|
|
|
|
me.resume();
|
|
|
|
}
|
2006-12-09 22:42:13 +00:00
|
|
|
},
|
|
|
|
};
|
2006-08-19 22:24:24 +00:00
|
|
|
|
2006-12-09 22:42:13 +00:00
|
|
|
|
|
|
|
|
2006-12-10 21:50:59 +00:00
|
|
|
# default calculations for a plane
|
|
|
|
#
|
2006-12-09 22:42:13 +00:00
|
|
|
ViewManager.default_plane = func {
|
2006-12-10 21:50:59 +00:00
|
|
|
var wow = me.wow.get();
|
2006-12-09 22:42:13 +00:00
|
|
|
|
2006-12-10 21:50:59 +00:00
|
|
|
# calculate steering factor
|
|
|
|
var hdg = me.headingN.getValue();
|
|
|
|
var hdiff = normdeg(me.last_heading - hdg);
|
|
|
|
me.last_heading = hdg;
|
|
|
|
var steering = normatan(me.hdg_change.filter(hdiff)) * me.size_factor;
|
|
|
|
|
|
|
|
var az = me.az.get();
|
|
|
|
var vx = me.vx.get();
|
|
|
|
|
|
|
|
# calculate sideslip factor (zeroed when no forward ground speed)
|
|
|
|
var wspd = me.wind_speedN.getValue();
|
|
|
|
var wdir = me.headingN.getValue() - me.wind_dirN.getValue();
|
|
|
|
var u = vx - wspd * cos(wdir);
|
|
|
|
var slip = sin(me.slipN.getValue()) * me.ubody.filter(normatan(u / 10));
|
|
|
|
|
2006-12-11 18:37:10 +00:00
|
|
|
me.heading_offset = # view heading
|
2006-12-09 22:42:13 +00:00
|
|
|
-15 * sin(me.roll) * cos(me.pitch) # due to roll
|
2006-12-10 21:50:59 +00:00
|
|
|
+ 40 * steering * wow # due to ground steering
|
|
|
|
+ 10 * slip * (1 - wow); # due to sideslip (in air)
|
2006-12-09 22:42:13 +00:00
|
|
|
|
2006-12-11 18:37:10 +00:00
|
|
|
me.pitch_offset = # view pitch
|
2006-12-09 22:42:13 +00:00
|
|
|
10 * sin(me.roll) * sin(me.roll) # due to roll
|
2006-12-10 21:50:59 +00:00
|
|
|
+ 30 * (1 / (1 + math.exp(2 - az)) # due to G load
|
|
|
|
- 0.119202922); # [move to origin; 1/(1+exp(2)) ]
|
2006-12-09 22:42:13 +00:00
|
|
|
|
2006-12-10 21:50:59 +00:00
|
|
|
me.roll_offset = 0;
|
2006-12-09 22:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-12-10 21:50:59 +00:00
|
|
|
# default calculations for a helicopter
|
|
|
|
#
|
2006-12-09 22:42:13 +00:00
|
|
|
ViewManager.default_helicopter = func {
|
2006-12-10 21:50:59 +00:00
|
|
|
var lowspeed = 1 - normatan(me.speedN.getValue() / 20);
|
2006-12-09 22:42:13 +00:00
|
|
|
|
2006-12-10 21:50:59 +00:00
|
|
|
me.heading_offset = # view heading due to
|
|
|
|
-50 * npow(sin(me.roll) * cos(me.pitch), 2); # roll
|
2006-12-09 22:42:13 +00:00
|
|
|
|
2006-12-10 21:50:59 +00:00
|
|
|
me.pitch_offset = # view pitch due to
|
|
|
|
(me.pitch < 0 ? -35 : -40) * sin(me.pitch) * lowspeed # pitch
|
|
|
|
+ 15 * sin(me.roll) * sin(me.roll); # roll
|
2006-12-09 22:42:13 +00:00
|
|
|
|
2006-12-10 21:50:59 +00:00
|
|
|
me.roll_offset = # view roll due to
|
|
|
|
-15 * sin(me.roll) * cos(me.pitch) * lowspeed; # roll
|
2006-12-09 22:42:13 +00:00
|
|
|
}
|
2006-08-17 15:59:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2006-08-17 22:58:06 +00:00
|
|
|
# Update loop for the whole dynamic view manager. It only runs if
|
2006-08-17 15:59:44 +00:00
|
|
|
# /sim/view[0]/dynamic/enabled is true.
|
|
|
|
#
|
2007-02-06 14:33:59 +00:00
|
|
|
var main_loop = func(id) {
|
2006-08-17 15:59:44 +00:00
|
|
|
if (id != loop_id) {
|
|
|
|
return;
|
|
|
|
}
|
2007-02-05 22:59:51 +00:00
|
|
|
if (cockpit_view and !panel_visible) {
|
|
|
|
if (mouse_button) {
|
|
|
|
freeze();
|
2007-02-06 16:25:44 +00:00
|
|
|
} else {
|
2007-02-05 20:57:19 +00:00
|
|
|
view_manager.apply();
|
|
|
|
}
|
2006-08-17 15:59:44 +00:00
|
|
|
}
|
|
|
|
settimer(func { main_loop(id) }, 0);
|
|
|
|
}
|
|
|
|
|
2007-02-08 22:16:10 +00:00
|
|
|
|
|
|
|
|
2007-02-06 14:33:59 +00:00
|
|
|
var freeze = func {
|
2007-02-05 20:36:29 +00:00
|
|
|
if (mouse_mode == 0) {
|
2007-02-06 16:25:44 +00:00
|
|
|
view_manager.freeze();
|
2007-02-05 20:36:29 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-17 15:59:44 +00:00
|
|
|
|
2007-02-06 14:33:59 +00:00
|
|
|
var register = func(f) {
|
2007-01-22 00:03:07 +00:00
|
|
|
view_manager.calculate = f;
|
2006-12-10 21:50:59 +00:00
|
|
|
}
|
|
|
|
|
2007-02-06 14:33:59 +00:00
|
|
|
var reset = func {
|
2006-12-10 21:50:59 +00:00
|
|
|
view_manager.reset();
|
|
|
|
}
|
|
|
|
|
2007-02-23 22:17:55 +00:00
|
|
|
var lookat = func(h, p, r, x, y, z, s = BLEND_TIME, f = 55) {
|
2007-02-11 16:56:15 +00:00
|
|
|
view_manager.lookat(h, p, r, x, y, z, s, f);
|
2007-02-08 22:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var resume = func {
|
|
|
|
view_manager.resume();
|
2006-12-10 21:50:59 +00:00
|
|
|
}
|
2006-08-17 15:59:44 +00:00
|
|
|
|
2007-02-05 16:14:51 +00:00
|
|
|
|
2006-08-17 15:59:44 +00:00
|
|
|
var original_resetView = nil;
|
|
|
|
var panel_visibilityN = nil;
|
2006-08-22 10:06:46 +00:00
|
|
|
var dynamic_view = nil;
|
|
|
|
var view_manager = nil;
|
2006-08-17 15:59:44 +00:00
|
|
|
|
2006-08-17 18:53:44 +00:00
|
|
|
var cockpit_view = nil;
|
2007-02-06 14:33:59 +00:00
|
|
|
var panel_visible = nil; # whether 2D panel is visible
|
2007-02-05 20:36:29 +00:00
|
|
|
var elapsedN = nil;
|
2007-02-05 22:59:51 +00:00
|
|
|
var mouse_mode = nil;
|
2007-02-05 20:57:19 +00:00
|
|
|
var mouse_button = nil;
|
2006-08-17 15:59:44 +00:00
|
|
|
|
2006-08-18 17:00:16 +00:00
|
|
|
var loop_id = 0;
|
|
|
|
|
2006-08-17 15:59:44 +00:00
|
|
|
|
|
|
|
# Initialization.
|
|
|
|
#
|
2007-02-05 16:14:51 +00:00
|
|
|
var L = _setlistener("/sim/signals/nasal-dir-initialized", func {
|
|
|
|
removelistener(L);
|
2006-08-22 10:06:46 +00:00
|
|
|
# disable menu entry and return for inappropriate FDMs (see Main/fg_init.cxx)
|
|
|
|
var fdms = {
|
|
|
|
acms:0, ada:0, balloon:0, external:0,
|
|
|
|
jsb:1, larcsim:1, magic:0, network:0,
|
|
|
|
null:0, pipe:0, ufo:0, yasim:1,
|
|
|
|
};
|
2006-08-21 09:45:48 +00:00
|
|
|
var fdm = getprop("/sim/flight-model");
|
|
|
|
if (!contains(fdms, fdm) or !fdms[fdm]) {
|
|
|
|
return gui.menuEnable("dynamic-view", 0);
|
|
|
|
}
|
|
|
|
|
2006-08-18 17:00:16 +00:00
|
|
|
# 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/y-accel-fps_sec", 1).setDoubleValue(0);
|
|
|
|
props.globals.getNode("/accelerations/pilot/z-accel-fps_sec", 1).setDoubleValue(-32);
|
2006-08-19 22:24:24 +00:00
|
|
|
props.globals.getNode("/orientation/side-slip-deg", 1).setDoubleValue(0);
|
2006-08-18 17:00:16 +00:00
|
|
|
props.globals.getNode("/gear/gear/wow", 1).setBoolValue(1);
|
2007-02-05 20:36:29 +00:00
|
|
|
elapsedN = props.globals.getNode("/sim/time/elapsed-sec", 1);
|
2006-08-17 15:59:44 +00:00
|
|
|
|
2006-08-17 18:53:44 +00:00
|
|
|
# let listeners keep some variables up-to-date, so that they don't have
|
|
|
|
# to be queried in the loop
|
2007-02-05 16:14:51 +00:00
|
|
|
setlistener("/sim/current-view/view-number", func { cockpit_view = !cmdarg().getValue() }, 1);
|
|
|
|
setlistener("/sim/panel/visibility", func { panel_visible = cmdarg().getValue() }, 1);
|
2007-02-06 14:33:59 +00:00
|
|
|
setlistener("/devices/status/mice/mouse/button", func { mouse_button = cmdarg().getValue() }, 1);
|
|
|
|
setlistener("/devices/status/mice/mouse/x", freeze);
|
|
|
|
setlistener("/devices/status/mice/mouse/y", freeze);
|
2007-02-23 22:17:55 +00:00
|
|
|
setlistener("/devices/status/mice/mouse/mode", func {
|
|
|
|
if (mouse_mode = cmdarg().getValue()) {
|
|
|
|
view_manager.unfreeze();
|
|
|
|
}
|
|
|
|
}, 1);
|
2006-08-17 18:53:44 +00:00
|
|
|
|
2007-02-05 16:14:51 +00:00
|
|
|
setlistener("/sim/signals/reinit", func {
|
2007-02-06 14:33:59 +00:00
|
|
|
cmdarg().getValue() and return;
|
2006-08-17 18:53:44 +00:00
|
|
|
cockpit_view = getprop("/sim/current-view/view-number") == 0;
|
2006-12-11 18:37:10 +00:00
|
|
|
view_manager.reset();
|
2007-02-05 16:14:51 +00:00
|
|
|
}, 0);
|
2006-08-17 18:53:44 +00:00
|
|
|
|
2006-12-09 22:42:13 +00:00
|
|
|
view_manager = ViewManager.new();
|
2006-08-17 15:59:44 +00:00
|
|
|
|
2006-08-18 17:00:16 +00:00
|
|
|
original_resetView = view.resetView;
|
2006-08-17 15:59:44 +00:00
|
|
|
view.resetView = func {
|
|
|
|
original_resetView();
|
|
|
|
if (cockpit_view and dynamic_view) {
|
2006-08-18 17:00:16 +00:00
|
|
|
view_manager.add_offset();
|
2006-08-17 15:59:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-08 22:16:10 +00:00
|
|
|
settimer(func {
|
|
|
|
setlistener("/sim/view/dynamic/enabled", func {
|
|
|
|
dynamic_view = cmdarg().getBoolValue();
|
|
|
|
loop_id += 1;
|
|
|
|
view.resetView();
|
|
|
|
if (dynamic_view) {
|
|
|
|
main_loop(loop_id);
|
|
|
|
}
|
|
|
|
}, 1);
|
|
|
|
}, 0);
|
2007-01-22 00:03:07 +00:00
|
|
|
});
|
2006-08-17 15:59:44 +00:00
|
|
|
|
|
|
|
|