|
@@ -428,12 +428,15 @@ class ViewportResizer
|
428
|
428
|
/** the viewport to be resized */
|
429
|
429
|
private readonly _viewport: Viewport;
|
430
|
430
|
|
431
|
|
- /** is this viewport subject to being resized? */
|
432
|
|
- private _active: boolean;
|
|
431
|
+ /** a helper */
|
|
432
|
+ private _timeout: Nullable<ReturnType<typeof setTimeout>>;
|
433
|
433
|
|
434
|
434
|
/** bound resize method */
|
435
|
435
|
private readonly _resize: () => void;
|
436
|
436
|
|
|
437
|
+ /** bound event trigger */
|
|
438
|
+ private readonly _triggerResize: () => void;
|
|
439
|
+
|
437
|
440
|
/** resize strategy */
|
438
|
441
|
private _resizeStrategy: ViewportResizeStrategy;
|
439
|
442
|
|
|
@@ -447,13 +450,15 @@ class ViewportResizer
|
447
|
450
|
constructor(viewport: Viewport)
|
448
|
451
|
{
|
449
|
452
|
this._viewport = viewport;
|
450
|
|
- this._active = false;
|
|
453
|
+ this._timeout = null;
|
451
|
454
|
this._resize = this._onResize.bind(this);
|
|
455
|
+ this._triggerResize = this.triggerResize.bind(this);
|
452
|
456
|
this._resizeStrategy = new InlineResizeStrategy();
|
453
|
457
|
|
454
|
458
|
// initial setup
|
455
|
459
|
// (the size is yet unknown)
|
456
|
|
- this._resize();
|
|
460
|
+ this._viewport.addEventListener('resize', this._resize);
|
|
461
|
+ this.triggerResize(0);
|
457
|
462
|
}
|
458
|
463
|
|
459
|
464
|
/**
|
|
@@ -461,39 +466,19 @@ class ViewportResizer
|
461
|
466
|
*/
|
462
|
467
|
init(): void
|
463
|
468
|
{
|
464
|
|
- // Configure the resize listener. We want the viewport
|
465
|
|
- // to adjust itself if the phone/screen is resized or
|
466
|
|
- // changes orientation
|
467
|
|
- let timeout: Nullable<ReturnType<typeof setTimeout>> = null;
|
468
|
|
- const onWindowResize = () => {
|
469
|
|
- if(!this._active) {
|
470
|
|
- window.removeEventListener('resize', onWindowResize);
|
471
|
|
- return;
|
472
|
|
- }
|
473
|
|
-
|
474
|
|
- if(timeout !== null)
|
475
|
|
- clearTimeout(timeout);
|
476
|
|
-
|
477
|
|
- timeout = setTimeout(() => {
|
478
|
|
- timeout = null;
|
479
|
|
- this._resize();
|
480
|
|
- }, 50);
|
481
|
|
- };
|
482
|
|
- window.addEventListener('resize', onWindowResize);
|
|
469
|
+ // Configure the resize listener. We want the viewport to adjust itself
|
|
470
|
+ // if the phone/screen is resized or changes orientation
|
|
471
|
+ window.addEventListener('resize', this._triggerResize); // a delay is welcome
|
483
|
472
|
|
484
|
473
|
// handle changes of orientation
|
485
|
474
|
// (is this needed? we already listen to resize events)
|
486
|
475
|
if(screen.orientation !== undefined)
|
487
|
|
- screen.orientation.addEventListener('change', this._resize);
|
|
476
|
+ screen.orientation.addEventListener('change', this._triggerResize);
|
488
|
477
|
else
|
489
|
|
- window.addEventListener('orientationchange', this._resize); // deprecated
|
490
|
|
-
|
491
|
|
- // setup event listener & finish!
|
492
|
|
- this._viewport.addEventListener('resize', this._resize);
|
493
|
|
- this._active = true;
|
|
478
|
+ window.addEventListener('orientationchange', this._triggerResize); // deprecated
|
494
|
479
|
|
495
|
480
|
// trigger a resize to setup the sizes / the CSS
|
496
|
|
- this.resize();
|
|
481
|
+ this.triggerResize(0);
|
497
|
482
|
}
|
498
|
483
|
|
499
|
484
|
/**
|
|
@@ -501,23 +486,37 @@ class ViewportResizer
|
501
|
486
|
*/
|
502
|
487
|
release(): void
|
503
|
488
|
{
|
504
|
|
- this._resizeStrategy.clear(this._viewport);
|
505
|
|
- this._active = false;
|
506
|
|
- this._viewport.removeEventListener('resize', this._resize);
|
507
|
|
-
|
508
|
489
|
if(screen.orientation !== undefined)
|
509
|
|
- screen.orientation.removeEventListener('change', this._resize);
|
|
490
|
+ screen.orientation.removeEventListener('change', this._triggerResize);
|
510
|
491
|
else
|
511
|
|
- window.removeEventListener('orientationchange', this._resize); // deprecated
|
|
492
|
+ window.removeEventListener('orientationchange', this._triggerResize);
|
|
493
|
+
|
|
494
|
+ window.removeEventListener('resize', this._triggerResize);
|
|
495
|
+
|
|
496
|
+ this._viewport.removeEventListener('resize', this._resize);
|
|
497
|
+ this._resizeStrategy.clear(this._viewport);
|
512
|
498
|
}
|
513
|
499
|
|
514
|
500
|
/**
|
515
|
|
- * Trigger a resize event
|
|
501
|
+ * Trigger a resize event after a delay
|
|
502
|
+ * @param delay in milliseconds
|
516
|
503
|
*/
|
517
|
|
- resize(): void
|
|
504
|
+ triggerResize(delay: number = 50): void
|
518
|
505
|
{
|
519
|
506
|
const event = new ViewportEvent('resize');
|
520
|
|
- this._viewport.dispatchEvent(event);
|
|
507
|
+
|
|
508
|
+ if(delay <= 0) {
|
|
509
|
+ this._viewport.dispatchEvent(event);
|
|
510
|
+ return;
|
|
511
|
+ }
|
|
512
|
+
|
|
513
|
+ if(this._timeout !== null)
|
|
514
|
+ clearTimeout(this._timeout);
|
|
515
|
+
|
|
516
|
+ this._timeout = setTimeout(() => {
|
|
517
|
+ this._timeout = null;
|
|
518
|
+ this._viewport.dispatchEvent(event);
|
|
519
|
+ }, delay);
|
521
|
520
|
}
|
522
|
521
|
|
523
|
522
|
/**
|
|
@@ -528,7 +527,31 @@ class ViewportResizer
|
528
|
527
|
{
|
529
|
528
|
this._resizeStrategy.clear(this._viewport);
|
530
|
529
|
this._resizeStrategy = strategy;
|
531
|
|
- this.resize();
|
|
530
|
+ this.triggerResize(0);
|
|
531
|
+ }
|
|
532
|
+
|
|
533
|
+ /**
|
|
534
|
+ * Change the resize strategy
|
|
535
|
+ * @param strategyName name of the new strategy
|
|
536
|
+ */
|
|
537
|
+ setStrategyByName(strategyName: ViewportStyle): void
|
|
538
|
+ {
|
|
539
|
+ switch(strategyName) {
|
|
540
|
+ case 'best-fit':
|
|
541
|
+ this.setStrategy(new BestFitResizeStrategy());
|
|
542
|
+ break;
|
|
543
|
+
|
|
544
|
+ case 'stretch':
|
|
545
|
+ this.setStrategy(new StretchResizeStrategy());
|
|
546
|
+ break;
|
|
547
|
+
|
|
548
|
+ case 'inline':
|
|
549
|
+ this.setStrategy(new InlineResizeStrategy());
|
|
550
|
+ break;
|
|
551
|
+
|
|
552
|
+ default:
|
|
553
|
+ throw new IllegalArgumentError('Invalid viewport style: ' + strategyName);
|
|
554
|
+ }
|
532
|
555
|
}
|
533
|
556
|
|
534
|
557
|
/**
|
|
@@ -714,7 +737,7 @@ export class Viewport extends ViewportEventTarget
|
714
|
737
|
protected readonly _hud: HUD;
|
715
|
738
|
|
716
|
739
|
/** Viewport style */
|
717
|
|
- protected _style: Nullable<ViewportStyle>;
|
|
740
|
+ protected _style: ViewportStyle;
|
718
|
741
|
|
719
|
742
|
/** The canvases of the viewport */
|
720
|
743
|
private readonly _canvases: ViewportCanvases;
|
|
@@ -746,14 +769,15 @@ export class Viewport extends ViewportEventTarget
|
746
|
769
|
this._mediaSize = () => initialSize;
|
747
|
770
|
|
748
|
771
|
this._resolution = settings.resolution;
|
749
|
|
- this._style = null;
|
|
772
|
+ this._style = settings.style;
|
|
773
|
+
|
750
|
774
|
this._containers = new ViewportContainers(settings.container);
|
751
|
775
|
this._hud = new HUD(this._subContainer, settings.hudContainer);
|
752
|
776
|
this._canvases = new ViewportCanvases(this._subContainer, initialSize, settings.canvas);
|
753
|
777
|
|
754
|
778
|
this._fullscreen = new ViewportFullscreenHelper(this.container);
|
755
|
779
|
this._resizer = new ViewportResizer(this);
|
756
|
|
- this.style = settings.style;
|
|
780
|
+ this._resizer.setStrategyByName(this._style);
|
757
|
781
|
}
|
758
|
782
|
|
759
|
783
|
/**
|
|
@@ -769,9 +793,6 @@ export class Viewport extends ViewportEventTarget
|
769
|
793
|
*/
|
770
|
794
|
get style(): ViewportStyle
|
771
|
795
|
{
|
772
|
|
- if(this._style === null)
|
773
|
|
- throw new IllegalOperationError();
|
774
|
|
-
|
775
|
796
|
return this._style;
|
776
|
797
|
}
|
777
|
798
|
|
|
@@ -780,31 +801,11 @@ export class Viewport extends ViewportEventTarget
|
780
|
801
|
*/
|
781
|
802
|
set style(value: ViewportStyle)
|
782
|
803
|
{
|
783
|
|
- // nothing to do
|
784
|
|
- if(value === this._style)
|
785
|
|
- return;
|
786
|
|
-
|
787
|
|
- // change style
|
788
|
|
- switch(value) {
|
789
|
|
- case 'best-fit':
|
790
|
|
- this._resizer.setStrategy(new BestFitResizeStrategy());
|
791
|
|
- break;
|
792
|
|
-
|
793
|
|
- case 'stretch':
|
794
|
|
- this._resizer.setStrategy(new StretchResizeStrategy());
|
795
|
|
- break;
|
796
|
|
-
|
797
|
|
- case 'inline':
|
798
|
|
- this._resizer.setStrategy(new InlineResizeStrategy());
|
799
|
|
- break;
|
800
|
|
-
|
801
|
|
- default:
|
802
|
|
- throw new IllegalArgumentError('Invalid viewport style: ' + value);
|
803
|
|
- }
|
804
|
|
-
|
805
|
|
- this._style = value;
|
806
|
|
-
|
807
|
804
|
// note: the viewport style is independent of the session mode!
|
|
805
|
+ if(value !== this._style) {
|
|
806
|
+ this._resizer.setStrategyByName(value);
|
|
807
|
+ this._style = value;
|
|
808
|
+ }
|
808
|
809
|
}
|
809
|
810
|
|
810
|
811
|
/**
|