155
edits
No edit summary |
No edit summary |
||
Line 199: | Line 199: | ||
}; | }; | ||
</code> | </code> | ||
= Helping Functions and Classes = | |||
Although written for the CDU framework, these functions are fairly general and might be useful for other code projects. Please feel free to reuse them. | |||
== prepend == | |||
var prepend = func(vec, elements...) | |||
This function is the counterpart to the Nasal provided append. Instead of adding stuff to the end of the vector <tt>vec</tt> is adds them at the front, '''altering the original vector'''. (Implemented in <tt>helping_functions.nas</tt>.) | |||
== stack == | |||
var stack = func(elements...) | |||
Instead of appending or prepending, this function simply stacks its argument vectors and '''returns a new vector''', leaving the original function arguments unaltered. (Implemented in <tt>helping_functions.nas</tt>.) | |||
== latitude_to_string == | |||
var latitude_to_string = func(lat_deg) | |||
Take a Nasal scalar as input variable and return something like N23°14.8 . Actually, as the "°" seems to have some trouble in Helvetica, is returns something like N23*14.8. (Implemented in <tt>helping_functions.nas</tt>.) | |||
== longitude_to_string == | |||
var longitude_to_string = func(lon_deg) | |||
Take a Nasal scalar as input variable and return something like E123°14.8 . Actually, as the "°" seems to have some trouble in Helvetica, is returns something like E123*14.8. (Implemented in <tt>helping_functions.nas</tt>.) | |||
== Timer == | |||
A timer can be made as an instance of this class. Each timer provides the functions tic and toc, the later returning the delta time passed. | |||
var timer1 = Timer.new(); # uses /sim/time/elapsed-sec as a time source | |||
var timer2 = Timer.new(non_default_time_source_variable); # provide an alternate source of time | |||
timer1.tic(); # start the internal timer on the default time source | |||
var passed_time = timer1.toc(); # stop and reset the timer, get back the passed time in seconds. | |||
This class is implemented in <tt>timer.nas</tt>. | |||
== Button == | |||
As an example for the use of the before mentioned timer, this is a generic button class to be used for all kinds of GUI interfaces. Although not used in the CDU, it still might be useful for other projects that require buttons to do several different things - depending on how long it has been pushed. | |||
var b_clr = Button.new("CLEAR"); # make a new button and give it a name, stored in Button.name | |||
b_clr.addButtonAction(func() {print("NO delay");} ); # zero delay is the default. This function will be executed if no other criteria are met | |||
b_clr.addButtonAction(func() {print("2s delay");},2 ); # this function will be executed if the button has been pressed longer than 2 seconds when released. | |||
b_clr.addButtonAction(func() {print("1s delay");},1 ); # this function will be executed if the button has been pressed longer than 1 seconds when released. | |||
The corresponding XML code for your model.xml could look like this: | |||
<animation> | |||
<type>pick</type> | |||
<!-- UPDATE the name of the button object to match you case --> | |||
<object-name>Btn.clr</object-name> | |||
<action> | |||
<button>0</button> | |||
<repeatable>false</repeatable> | |||
<binding> | |||
<command>nasal</command> | |||
<nowiki><!-- UPDATE the namespace --></nowiki> | |||
<script><![CDATA[NAMESPACE.b_clr.pressed();]]></script> | |||
</binding> | |||
<mod-up> | |||
<binding> | |||
<command>nasal</command> | |||
<nowiki><!-- UPDATE the namespace --></nowiki> | |||
<script><![CDATA[NAMESPACE.b_clr.released();]]></script> | |||
</binding> | |||
</mod-up> | |||
</action> | |||
</animation> | |||
The sequence in which functions are added does not matter, note how the example created the 2 s before the 1 s functions. As a result of this, the button has to be released to work, i.e. currently it is not possible to make the button do something if the button has been pressed longer than X s and still is pressed. |
edits