Add Timer class for simplify use maketimer() function with single shot

This commit is contained in:
PlayeRom
2022-08-21 13:10:42 +02:00
parent 5ce9a0a299
commit 467336590b
4 changed files with 45 additions and 11 deletions
+1
View File
@@ -42,6 +42,7 @@ var main = func(addon) {
#
var loadExtraNasalFiles = func (addon) {
var modules = [
"nasal/timer",
"nasal/aircraft",
"nasal/message",
"nasal/dialogs/route",
+1 -3
View File
@@ -79,11 +79,9 @@
<command>nasal</command>
<script><![CDATA[
# Run stopAerotow() with a delay to ensure that the engine sound turns off
var timer = maketimer(1, func () {
aerotow.Timer.new().singleShot(1, aerotow.g_Aerotow, func () {
aerotow.g_Aerotow.stopAerotow();
});
timer.singleShot = 1;
timer.start();
]]></script>
</binding>
</item>
+2 -8
View File
@@ -51,8 +51,6 @@ var Aerotow = {
#
# Function for restart AI scenario with delay when the sound has to stop.
#
# Return 1 on successful, otherwise 0.
#
restartAerotow: func () {
me.message.success("Aerotow on the way");
@@ -60,11 +58,9 @@ var Aerotow = {
setprop(me.addonNodePath ~ "/addon-devel/sound/enable", 0);
# Wait a second for the engine sound to turn off
var timer = maketimer(1, func () {
Timer.new().singleShot(1, me, func () {
me.unloadScenario();
});
timer.singleShot = 1;
timer.start();
},
#
@@ -76,11 +72,9 @@ var Aerotow = {
}
# Start aerotow with delay to avoid duplicate engine sound playing
var timer = maketimer(1, func () {
Timer.new().singleShot(1, me, func () {
me.startAerotow();
});
timer.singleShot = 1;
timer.start();
},
#
+41
View File
@@ -0,0 +1,41 @@
#
# Aerotow Everywhere - Add-on for FlightGear
#
# Written and developer by Roman Ludwicki (PlayeRom, SP-ROM)
#
# Copyright (C) 2022 Roman Ludwicki
#
# Aerotow Everywhere is an Open Source project and it is licensed
# under the GNU Public License v3 (GPLv3)
#
#
# Class Timer for wrapping maketimer()
#
var Timer = {
#
# Constructor
#
new: func () {
return { parents: [Timer] };
},
#
# Run timer as single shot
#
# delaySec - Delay in seconds for execute timer's callback.
# self - Specifying what any "me" references in the function being called will refer to.
# callback - Function to be called after given delay.
#
# Return timer handler object
#
singleShot: func (delaySec, self, callback) {
var timer = maketimer(delaySec, self, func () {
callback();
});
timer.singleShot = 1;
timer.start();
return timer;
},
};