-
Notifications
You must be signed in to change notification settings - Fork 18
/
Timer.hx
97 lines (78 loc) · 3.25 KB
/
Timer.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package defold;
import defold.types.Hash;
/**
Timers allow you to set a delay and a callback to be called when the timer completes.
The timers created with this API are updated with the collection timer where they
are created. If you pause or speed up the collection (using `set_time_step`) it will
also affect the new timer.
**/
@:native("_G.timer")
extern final class Timer
{
/**
Cancel a timer.
You may cancel a timer from inside a timer callback.
Cancelling a timer that is already executed or cancelled is safe.
@param handle the timer handle returned by timer.delay()
@return true if the timer was active, false if the timer is already cancelled / complete
**/
static function cancel(handle:TimerHandle):Bool;
/**
Create a timer.
Adds a timer and returns a unique handle
You may create more timers from inside a timer callback.
Using a delay of 0 will result in a timer that triggers at the next frame just before
script update functions.
If you want a timer that triggers on each frame, set delay to 0.0f and repeat to true.
Timers created within a script will automatically die when the script is deleted.
@param delay time interval in seconds
@param repeat true = repeat timer until cancel, false = one-shot timer
@param callback timer callback function
@return identifier for the create timer, returns `TimerHandle.Invalid` if the timer can not be created
**/
static inline function delay(delay:Float, repeat:Bool, callback:(handle:TimerHandle, timeElapsed:Float)->Void):TimerHandle
{
// 1. hide the reall callback parameter which expects a function with a "self" argument
// 2. ensure that the global self reference is present for the callback
return delay_(delay, repeat, (self, handle, timeElapsed) ->
{
untyped __lua__('_G._hxdefold_self_ = {0}', self);
callback(handle, timeElapsed);
untyped __lua__('_G._hxdefold_self_ = nil');
});
}
@:native('delay') private static function delay_(delay:Float, repeat:Bool, callback:(Any, TimerHandle, Float)->Void):TimerHandle;
/**
Manual triggering a callback for a timer.
@param handle the timer handle returned by `timer.delay()`
@return `true` if the timer was active, `false` if the timer is already cancelled / complete
**/
static function trigger(handle:TimerHandle):Bool;
/**
Get information about timer.
@param handle the timer handle returned by `timer.delay()`
@return the timer info, or `null` if timer is cancelled/completed
*/
@:pure
@:native('get_info')
static function getInfo(handle:TimerHandle):TimerInfo;
}
@:native("_G.timer")
extern enum abstract TimerHandle(Hash)
{
@:native('INVALID_TIMER_HANDLE')
var Invalid;
}
/**
Timer information returned by the `get_info()` method.
**/
extern final class TimerInfo
{
/** Time remaining until the next time a timer.delay() fires. */
@:native('time_remaining')
var timeRemaining:Float;
/** Time interval. */
var delay: Float;
/** `true` = repeat timer until cancel, `false` = one-shot timer. */
var repeat: Bool;
}