-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVisualTimer.js
755 lines (673 loc) · 21 KB
/
VisualTimer.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/**
* # VisualTimer
* Copyright(c) 2021 Stefano Balietti <[email protected]>
* MIT Licensed
*
* Display a configurable timer for the game
*
* Timer can trigger events, only for countdown smaller than 1h.
*
* www.nodegame.org
*/
(function(node) {
"use strict";
node.widgets.register('VisualTimer', VisualTimer);
// ## Meta-data
VisualTimer.version = '0.9.3';
VisualTimer.description = 'Display a configurable timer for the game. ' +
'Can trigger events. Only for countdown smaller than 1h.';
VisualTimer.title = 'Time Left';
VisualTimer.className = 'visualtimer';
// ## Dependencies
VisualTimer.dependencies = {
GameTimer: {}
};
/**
* ## VisualTimer constructor
*
* `VisualTimer` displays and manages a `GameTimer`
*
* @param {object} options Optional. Configuration options
* The options it can take are:
*
* - any options that can be passed to a `GameTimer`
* - `waitBoxOptions`: an option object to be passed to `TimerBox`
* - `mainBoxOptions`: an option object to be passed to `TimerBox`
*
* @see TimerBox
* @see GameTimer
*/
function VisualTimer() {
/**
* ### VisualTimer.gameTimer
*
* The timer which counts down the game time
*
* @see node.timer.createTimer
*/
this.gameTimer = null;
/**
* ### VisualTimer.mainBox
*
* The `TimerBox` which displays the main timer
*
* @see TimerBox
*/
this.mainBox = null;
/**
* ### VisualTimer.waitBox
*
* The `TimerBox` which displays the wait timer
*
* @see TimerBox
*/
this.waitBox = null;
/**
* ### VisualTimer.activeBox
*
* The `TimerBox` in which to display the time
*
* This variable is always a reference to either `waitBox` or
* `mainBox`.
*
* @see TimerBox
*/
this.activeBox = null;
/**
* ### VisualTimer.isInitialized
*
* Indicates whether the instance has been initializded already
*/
this.isInitialized = false;
/**
* ### VisualTimer.options
*
* Currently stored options
*/
this.options = {};
/**
* ### VisualTimer.internalTimer
*
* TRUE, if the timer is created internally
*
* Internal timers are destroyed when widget is destroyed or cleared
*
* @see VisualTimer.gameTimer
* @see VisualTimer.clear
*/
this.internalTimer = null;
}
// ## VisualTimer methods
/**
* ### VisualTimer.init
*
* Initializes the instance. When called again, adds options to current ones
*
* The options it can take are:
*
* - any options that can be passed to a `GameTimer`
* - waitBoxOptions: an option object to be passed to `TimerBox`
* - mainBoxOptions: an option object to be passed to `TimerBox`
*
* @param {object} options Optional. Configuration options
*
* @see TimerBox
* @see GameTimer
*/
VisualTimer.prototype.init = function(options) {
var t, gameTimerOptions;
// We keep the check for object, because this widget is often
// called by users and the restart methods does not guarantee
// an object.
options = options || {};
if ('object' !== typeof options) {
throw new TypeError('VisualTimer.init: options must be ' +
'object or undefined. Found: ' + options);
}
// Important! Do not modify directly options, because it might
// modify a step-property. Will manual clone later.
gameTimerOptions = {};
// If gameTimer is not already set, check options, then
// try to use node.game.timer, if defined, otherwise crete a new timer.
if ('undefined' !== typeof options.gameTimer) {
if (this.gameTimer) {
throw new Error('GameTimer.init: options.gameTimer cannot ' +
'be set if a gameTimer is already existing: ' +
this.name);
}
if ('object' !== typeof options.gameTimer) {
throw new TypeError('VisualTimer.init: options.' +
'gameTimer must be object or ' +
'undefined. Found: ' + options.gameTimer);
}
this.gameTimer = options.gameTimer;
}
else {
if (!this.isInitialized) {
this.internalTimer = true;
this.gameTimer = node.timer.createTimer({
name: options.name || 'VisualTimer_' + J.randomInt(10000000)
});
}
}
if (options.hooks) {
if (!this.internalTimer) {
throw new Error('VisualTimer.init: cannot add hooks on ' +
'external gameTimer.');
}
if (!J.isArray(options.hooks)) {
gameTimerOptions.hooks = [ options.hooks ];
}
}
else {
gameTimerOptions.hooks = [];
}
// Only push this hook once.
if (!this.isInitialized) {
gameTimerOptions.hooks.push({
name: 'VisualTimer_' + this.wid,
hook: this.updateDisplay,
ctx: this
});
}
// Important! Manual clone must be done after hooks and gameTimer.
// Parse milliseconds option.
if ('undefined' !== typeof options.milliseconds) {
gameTimerOptions.milliseconds =
node.timer.parseInput('milliseconds', options.milliseconds);
}
// Parse update option.
if ('undefined' !== typeof options.update) {
gameTimerOptions.update =
node.timer.parseInput('update', options.update);
}
else {
gameTimerOptions.update = 1000;
}
// Parse timeup option.
if ('undefined' !== typeof options.timeup) {
gameTimerOptions.timeup = options.timeup;
}
// Init the gameTimer, regardless of the source (internal vs external).
this.gameTimer.init(gameTimerOptions);
t = this.gameTimer;
// TODO: not using session for now.
// node.session.register('visualtimer', {
// set: function(p) {
// // TODO
// },
// get: function() {
// return {
// startPaused: t.startPaused,
// status: t.status,
// timeLeft: t.timeLeft,
// timePassed: t.timePassed,
// update: t.update,
// updateRemaining: t.updateRemaining,
// updateStart: t. updateStart
// };
// }
// });
this.options = gameTimerOptions;
// Must be after this.options is assigned.
if ('undefined' === typeof this.options.stopOnDone) {
this.options.stopOnDone = true;
}
if ('undefined' === typeof this.options.startOnPlaying) {
this.options.startOnPlaying = true;
}
if (!this.options.mainBoxOptions) {
this.options.mainBoxOptions = {};
}
if (!this.options.waitBoxOptions) {
this.options.waitBoxOptions = {};
}
J.mixout(this.options.mainBoxOptions,
{classNameBody: options.className, hideTitle: true});
J.mixout(this.options.waitBoxOptions,
{title: 'Max. wait timer',
classNameTitle: 'waitTimerTitle',
classNameBody: 'waitTimerBody', hideBox: true});
if (!this.mainBox) {
this.mainBox = new TimerBox(this.options.mainBoxOptions);
}
else {
this.mainBox.init(this.options.mainBoxOptions);
}
if (!this.waitBox) {
this.waitBox = new TimerBox(this.options.waitBoxOptions);
}
else {
this.waitBox.init(this.options.waitBoxOptions);
}
this.activeBox = this.options.activeBox || this.mainBox;
this.isInitialized = true;
};
VisualTimer.prototype.append = function() {
this.bodyDiv.appendChild(this.mainBox.boxDiv);
this.bodyDiv.appendChild(this.waitBox.boxDiv);
this.activeBox = this.mainBox;
this.updateDisplay();
};
/**
* ### VisualTimer.clear
*
* Reverts state of `VisualTimer` to right after creation
*
* @param {object} options Configuration object
*
* @return {object} oldOptions The Old options
*
* @see node.timer.destroyTimer
* @see VisualTimer.init
*/
VisualTimer.prototype.clear = function(options) {
var oldOptions;
options = options || {};
oldOptions = this.options;
destroyTimer(this);
this.gameTimer = null;
this.activeBox = null;
this.isInitialized = false;
this.init(options);
return oldOptions;
};
/**
* ### VisualTimer.updateDisplay
*
* Changes `activeBox` to display current time of `gameTimer`
*
* @see TimerBox.bodyDiv
*/
VisualTimer.prototype.updateDisplay = function() {
var time, minutes, seconds;
if (!this.gameTimer.milliseconds || this.gameTimer.milliseconds === 0) {
this.activeBox.bodyDiv.innerHTML = '00:00';
return;
}
time = this.gameTimer.milliseconds - this.gameTimer.timePassed;
time = J.parseMilliseconds(time);
minutes = (time[2] < 10) ? '' + '0' + time[2] : time[2];
seconds = (time[3] < 10) ? '' + '0' + time[3] : time[3];
this.activeBox.bodyDiv.innerHTML = minutes + ':' + seconds;
};
/**
* ### VisualTimer.start
*
* Starts the timer
*
* @see VisualTimer.updateDisplay
* @see GameTimer.start
*/
VisualTimer.prototype.start = function() {
this.updateDisplay();
this.gameTimer.start();
};
/**
* ### VisualTimer.restart
*
* Restarts the timer with new options
*
* @param {object|number} options Configuration object or the number of
* milliseconds
*
* @see VisualTimer.init
* @see VisualTimer.start
* @see VisualTimer.stop
*/
VisualTimer.prototype.restart = function(options) {
this.stop();
if ('number' === typeof options) options = { milliseconds: options };
this.init(options);
this.start();
};
/**
* ### VisualTimer.stop
*
* Stops the timer display and stores the time left in `activeBox.timeLeft`
*
* @see GameTimer.isStopped
* @see GameTimer.stop
*/
VisualTimer.prototype.stop = function() {
if (!this.gameTimer.isStopped()) {
this.activeBox.timeLeft = this.gameTimer.timeLeft;
this.gameTimer.stop();
}
};
/**
* ### VisualTimer.switchActiveBoxTo
*
* Switches the display of the `gameTimer` into the `TimerBox` `box`
*
* Stores `gameTimer.timeLeft` into `activeBox` and then switches
* `activeBox` to reference `box`.
*
* @param {TimerBox} box TimerBox in which to display `gameTimer` time
*/
VisualTimer.prototype.switchActiveBoxTo = function(box) {
this.activeBox.timeLeft = this.gameTimer.timeLeft || 0;
this.activeBox = box;
this.updateDisplay();
};
/**
* ### VisualTimer.startWaiting
*
* Stops the timer and changes the appearance to a max. wait timer
*
* If options and/or options.milliseconds are undefined, the wait timer
* will start with the current time left on the `gameTimer`. The mainBox
* will be striked out, the waitBox set active and unhidden. All other
* options are forwarded directly to `VisualTimer.restart`.
*
* @param {object} options Configuration object
*
* @see VisualTimer.restart
*/
VisualTimer.prototype.startWaiting = function(options) {
if ('undefined' === typeof options) options = {};
if ('undefined' === typeof options.milliseconds) {
options.milliseconds = this.gameTimer.timeLeft;
}
if ('undefined' === typeof options.mainBoxOptions) {
options.mainBoxOptions = {};
}
if ('undefined' === typeof options.waitBoxOptions) {
options.waitBoxOptions = {};
}
options.mainBoxOptions.classNameBody = 'strike';
options.mainBoxOptions.timeLeft = this.gameTimer.timeLeft || 0;
options.activeBox = this.waitBox;
options.waitBoxOptions.hideBox = false;
this.restart(options);
};
/**
* ### VisualTimer.startTiming
*
* Starts the timer and changes appearance to a regular countdown
*
* The mainBox will be unstriked and set active, the waitBox will be
* hidden. All other options are forwarded directly to
* `VisualTimer.restart`.
*
* @param {object} options Configuration object
*
* @see VisualTimer.restart
*/
VisualTimer.prototype.startTiming = function(options) {
if ('undefined' === typeof options) {
options = {};
}
if ('undefined' === typeof options.mainBoxOptions) {
options.mainBoxOptions = {};
}
if ('undefined' === typeof options.waitBoxOptions) {
options.waitBoxOptions = {};
}
options.activeBox = this.mainBox;
options.waitBoxOptions.timeLeft = this.gameTimer.timeLeft || 0;
options.waitBoxOptions.hideBox = true;
options.mainBoxOptions.classNameBody = '';
this.restart(options);
};
/**
* ### VisualTimer.resume
*
* Resumes the `gameTimer`
*
* @see GameTimer.resume
*/
VisualTimer.prototype.resume = function() {
this.gameTimer.resume();
};
/**
* ### VisualTimer.setToZero
*
* Stops `gameTimer` and sets `activeBox` to display `00:00`
*
* @see GameTimer.resume
*/
VisualTimer.prototype.setToZero = function() {
this.stop();
this.activeBox.bodyDiv.innerHTML = '00:00';
this.activeBox.setClassNameBody('strike');
};
/**
* ### VisualTimer.isTimeup
*
* Returns TRUE if the timer expired
*
* This method is added for backward compatibility.
*
* @see GameTimer.isTimeup
*/
VisualTimer.prototype.isTimeup = function() {
return this.gameTimer.isTimeup();
};
/**
* ### VisualTimer.doTimeUp
*
* Stops the timer and calls the timeup
*
* @see GameTimer.doTimeup
*/
VisualTimer.prototype.doTimeUp = function() {
this.gameTimer.doTimeUp();
};
VisualTimer.prototype.listeners = function() {
var that = this;
// Add listeners only on internal timer.
if (!this.internalTimer) return;
node.on('PLAYING', function() {
var options;
if (that.options.startOnPlaying) {
options = that.gameTimer.getStepOptions();
if (options) {
// Visual update is here (1000 usually).
options.update = that.update;
// Make sure timeup is not used (game.timer does it).
options.timeup = undefined;
// Options other than `update`, `timeup`,
// `milliseconds`, `hooks`, `gameTimer` are ignored.
that.startTiming(options);
}
else {
// Set to zero if it was not started already.
if (!that.gameTimer.isRunning()) that.setToZero();
}
}
});
node.on('REALLY_DONE', function() {
if (that.options.stopOnDone) {
if (!that.gameTimer.isStopped()) {
// This was creating problems, so we just stop it.
// It could be an option, though.
// that.startWaiting();
that.stop();
}
}
});
// Handle destroy.
this.on('destroyed', function() {
destroyTimer(that);
that.bodyDiv.removeChild(that.mainBox.boxDiv);
that.bodyDiv.removeChild(that.waitBox.boxDiv);
});
};
/**
* # TimerBox
*
* Copyright(c) 2015 Stefano Balietti
* MIT Licensed
*
* Represents a box wherin to display a `VisualTimer`
*/
/**
* ## TimerBox constructor
*
* `TimerBox` represents a box wherein to display the timer
*
* @param {object} options Optional. Configuration options
* The options it can take are:
*
* - `hideTitle`
* - `hideBody`
* - `hideBox`
* - `title`
* - `classNameTitle`
* - `classNameBody`
* - `timeLeft`
*/
function TimerBox(options) {
/**
* ### TimerBox.boxDiv
*
* The Div which will contain the title and body Divs
*/
this.boxDiv = null;
/**
* ### TimerBox.titleDiv
*
* The Div which will contain the title
*/
this.titleDiv = null;
/**
* ### TimerBox.bodyDiv
*
* The Div which will contain the numbers
*/
this.bodyDiv = null;
/**
* ### TimerBox.timeLeft
*
* Used to store the last value before focus is taken away
*/
this.timeLeft = null;
this.boxDiv = W.get('div');
this.titleDiv = W.add('div', this.boxDiv);
this.bodyDiv = W.add('div', this.boxDiv);
this.init(options);
}
TimerBox.prototype.init = function(options) {
if (options) {
if (options.hideTitle) {
this.hideTitle();
}
else {
this.unhideTitle();
}
if (options.hideBody) {
this.hideBody();
}
else {
this.unhideBody();
}
if (options.hideBox) {
this.hideBox();
}
else {
this.unhideBox();
}
}
this.setTitle(options.title || '');
this.setClassNameTitle(options.classNameTitle || '');
this.setClassNameBody(options.classNameBody || '');
if (options.timeLeft) {
this.timeLeft = options.timeLeft;
}
};
// ## TimerBox methods
/**
* ### TimerBox.hideBox
*
* Hides entire `TimerBox`
*/
TimerBox.prototype.hideBox = function() {
this.boxDiv.style.display = 'none';
};
/**
* ### TimerBox.unhideBox
*
* Hides entire `TimerBox`
*/
TimerBox.prototype.unhideBox = function() {
this.boxDiv.style.display = '';
};
/**
* ### TimerBox.hideTitle
*
* Hides title of `TimerBox`
*/
TimerBox.prototype.hideTitle = function() {
this.titleDiv.style.display = 'none';
};
/**
* ### TimerBox.unhideTitle
*
* Unhides title of `TimerBox`
*/
TimerBox.prototype.unhideTitle = function() {
this.titleDiv.style.display = '';
};
/**
* ### TimerBox.hideBody
*
* Hides body of `TimerBox`
*/
TimerBox.prototype.hideBody = function() {
this.bodyDiv.style.display = 'none';
};
/**
* ### TimerBox.unhideBody
*
* Unhides Body of `TimerBox`
*/
TimerBox.prototype.unhideBody = function() {
this.bodyDiv.style.display = '';
};
/**
* ### TimerBox.setTitle
*
* Sets title of `TimerBox`
*/
TimerBox.prototype.setTitle = function(title) {
this.titleDiv.innerHTML = title;
};
/**
* ### TimerBox.setClassNameTitle
*
* Sets class name of title of `TimerBox`
*/
TimerBox.prototype.setClassNameTitle = function(className) {
this.titleDiv.className = className;
};
/**
* ### TimerBox.setClassNameBody
*
* Sets class name of body of `TimerBox`
*/
TimerBox.prototype.setClassNameBody = function(className) {
this.bodyDiv.className = className;
};
// Helper function.
function destroyTimer(that) {
if (that.internalTimer) {
if (!that.gameTimer.isDestroyed()) {
node.timer.destroyTimer(that.gameTimer);
}
that.internalTimer = null;
}
else {
that.gameTimer.removeHook('VisualTimer_' + that.wid);
}
}
// if (this.internalTimer) {
// if (!this.gameTimer.isDestroyed()) {
// node.timer.destroyTimer(this.gameTimer);
// }
// this.internalTimer = null;
// }
// else {
// this.gameTimer.removeHook(this.updateHookName);
// }
})(node);