Phi: nicer scroll animation for METAR widget
[fg:fgdata.git] / Nasal / environment.nas
1 ##
2 ## environment.nas
3 ##
4 ##  Nasal code for implementing environment-specific functionality.
5
6 ##
7 # Handler.  Increase visibility by one step
8 #
9 var increaseVisibility = func {
10         adjustVisibility(1.1);
11 }
12
13 ##
14 # Handler.  Decrease visibility by one step
15 #
16 var decreaseVisibility = func {
17         adjustVisibility(0.9);
18 }
19
20 var adjustVisibility = func( factor ) {
21         var val = 0;
22         var aux_val = 0;
23     # better to use a non gui/dialog property here, but there doesn't
24     # seem to be one for local-weather. 
25         var localWeatherEnabled = getprop("sim/gui/dialogs/metar/mode/local-weather");
26         var max_aux_vis = 12.429216196;
27         var min_aux_vis = 9.90348;
28
29     if (localWeatherEnabled) {
30                 if (factor == 1.1)
31                         factor = 1.001;
32                 else 
33                         factor = 0.999;
34
35                 aux_val = auxvisibilityProp.getValue() * factor;
36
37                 if( aux_val <= min_aux_vis)
38                         auxvisibilityProp.setDoubleValue(min_aux_vis);
39                 elsif(aux_val >= max_aux_vis)
40                         auxvisibilityProp.setDoubleValue(max_aux_vis);
41                 else
42                         auxvisibilityProp.setDoubleValue(aux_val);
43  
44                 gui.popupTip(sprintf("Max Visibility: %.0f m", getprop("/local-weather/config/max-vis-range-m")));
45         } else {
46                 val = visibilityProp.getValue() * factor;
47
48                 if( val < 1.0 ) val = getprop("/environment/visibility-m");
49
50                 if( val > 30 ) {
51                         visibilityProp.setDoubleValue(val);
52                         visibilityOverrideProp.setBoolValue(1);
53                         }
54
55                 gui.popupTip(sprintf("Visibility: %.0f m", val));
56         }
57 }
58
59 ##
60 # Handler.  Reset visibility to default.
61 #
62 var resetVisibility = func {
63     visibilityProp.setDoubleValue(0);
64     visibilityOverrideProp.setBoolValue(0);
65 }
66
67
68 var visibilityProp = nil;
69 var visibilityOverrideProp = nil;
70 var auxvisibilityProp = nil;
71
72 _setlistener("/sim/signals/nasal-dir-initialized", func {
73         print ("environment init");
74         visibilityProp = props.globals.initNode("/environment/config/presets/visibility-m", 0, "DOUBLE" );
75         visibilityOverrideProp = props.globals.initNode("/environment/config/presets/visibility-m-override", 0, "BOOL" );
76         auxvisibilityProp = props.globals.initNode("/local-weather/config/aux-max-vis-range-m", 0, "DOUBLE" );
77 });