2021-03-19 15:40:07 +01:00
import { mediaManager } from "../../WebRtc/MediaManager" ;
import { HtmlUtils } from "../../WebRtc/HtmlUtils" ;
2021-03-24 15:59:18 +01:00
import { localUserStore } from "../../Connexion/LocalUserStore" ;
2021-05-06 16:25:13 +02:00
import { DirtyScene } from "../Game/DirtyScene" ;
2021-03-19 15:40:07 +01:00
export const HelpCameraSettingsSceneName = 'HelpCameraSettingsScene' ;
const helpCameraSettings = 'helpCameraSettings' ;
/ * *
* The scene that show how to permit Camera and Microphone access if there are not already allowed
* /
2021-05-06 16:25:13 +02:00
export class HelpCameraSettingsScene extends DirtyScene {
2021-03-19 15:40:07 +01:00
private helpCameraSettingsElement ! : Phaser . GameObjects . DOMElement ;
private helpCameraSettingsOpened : boolean = false ;
constructor ( ) {
super ( { key : HelpCameraSettingsSceneName } ) ;
}
preload() {
this . load . html ( helpCameraSettings , 'resources/html/helpCameraSettings.html' ) ;
}
create ( ) {
this . createHelpCameraSettings ( ) ;
}
private createHelpCameraSettings ( ) : void {
2021-04-21 00:53:45 +02:00
const middleX = this . getMiddleX ( ) ;
2021-03-19 15:40:07 +01:00
this . helpCameraSettingsElement = this . add . dom ( middleX , - 800 , undefined , { overflow : 'scroll' } ) . createFromCache ( helpCameraSettings ) ;
this . revealMenusAfterInit ( this . helpCameraSettingsElement , helpCameraSettings ) ;
this . helpCameraSettingsElement . addListener ( 'click' ) ;
this . helpCameraSettingsElement . on ( 'click' , ( event :MouseEvent ) = > {
2021-05-05 01:49:04 +02:00
if ( ( event ? . target as HTMLInputElement ) . id === 'mailto' ) {
return ;
}
2021-03-19 15:40:07 +01:00
event . preventDefault ( ) ;
if ( ( event ? . target as HTMLInputElement ) . id === 'helpCameraSettingsFormRefresh' ) {
window . location . reload ( ) ;
} else if ( ( event ? . target as HTMLInputElement ) . id === 'helpCameraSettingsFormContinue' ) {
this . closeHelpCameraSettingsOpened ( ) ;
}
} ) ;
2021-05-05 01:49:04 +02:00
if ( ! localUserStore . getHelpCameraSettingsShown ( ) && ( ! mediaManager . constraintsMedia . audio || ! mediaManager . constraintsMedia . video ) ) {
2021-03-19 15:40:07 +01:00
this . openHelpCameraSettingsOpened ( ) ;
2021-05-05 01:49:04 +02:00
localUserStore . setHelpCameraSettingsShown ( ) ;
2021-03-19 15:40:07 +01:00
}
2021-05-05 01:49:04 +02:00
mediaManager . setHelpCameraSettingsCallBack ( ( ) = > {
this . openHelpCameraSettingsOpened ( ) ;
} ) ;
2021-03-19 15:40:07 +01:00
}
private openHelpCameraSettingsOpened ( ) : void {
HtmlUtils . getElementByIdOrFail < HTMLDivElement > ( 'webRtcSetup' ) . style . display = 'none' ;
this . helpCameraSettingsOpened = true ;
2021-05-05 01:49:04 +02:00
try {
if ( window . navigator . userAgent . includes ( 'Firefox' ) ) {
HtmlUtils . getElementByIdOrFail < HTMLParagraphElement > ( 'browserHelpSetting' ) . innerHTML = '<img src="/resources/objects/help-setting-camera-permission-firefox.png"/>' ;
} else if ( window . navigator . userAgent . includes ( 'Chrome' ) ) {
HtmlUtils . getElementByIdOrFail < HTMLParagraphElement > ( 'browserHelpSetting' ) . innerHTML = '<img src="/resources/objects/help-setting-camera-permission-chrome.png"/>' ;
}
} catch ( err ) {
console . error ( 'openHelpCameraSettingsOpened => getElementByIdOrFail => error' , err ) ;
2021-03-19 15:40:07 +01:00
}
2021-04-21 00:53:45 +02:00
const middleY = this . getMiddleY ( ) ;
const middleX = this . getMiddleX ( ) ;
2021-03-19 15:40:07 +01:00
this . tweens . add ( {
targets : this.helpCameraSettingsElement ,
y : middleY ,
x : middleX ,
duration : 1000 ,
ease : 'Power3' ,
overflow : 'scroll'
} ) ;
2021-05-06 16:25:13 +02:00
this . dirty = true ;
2021-03-19 15:40:07 +01:00
}
private closeHelpCameraSettingsOpened ( ) : void {
2021-04-21 00:53:45 +02:00
const middleX = this . getMiddleX ( ) ;
2021-05-05 01:49:04 +02:00
/ * c o n s t h e l p C a m e r a S e t t i n g s I n f o = t h i s . h e l p C a m e r a S e t t i n g s E l e m e n t . g e t C h i l d B y I D ( ' h e l p C a m e r a S e t t i n g s ' ) a s H T M L P a r a g r a p h E l e m e n t ;
2021-03-19 15:40:07 +01:00
helpCameraSettingsInfo . innerText = '' ;
2021-05-05 01:49:04 +02:00
helpCameraSettingsInfo . style . display = 'none' ; * /
2021-03-19 15:40:07 +01:00
this . helpCameraSettingsOpened = false ;
this . tweens . add ( {
targets : this.helpCameraSettingsElement ,
2021-05-05 01:49:04 +02:00
y : - 1000 ,
2021-04-21 00:53:45 +02:00
x : middleX ,
2021-03-19 15:40:07 +01:00
duration : 1000 ,
ease : 'Power3' ,
overflow : 'scroll'
} ) ;
2021-05-06 16:25:13 +02:00
this . dirty = true ;
2021-03-19 15:40:07 +01:00
}
private revealMenusAfterInit ( menuElement : Phaser.GameObjects.DOMElement , rootDomId : string ) {
//Dom elements will appear inside the viewer screen when creating before being moved out of it, which create a flicker effect.
//To prevent this, we put a 'hidden' attribute on the root element, we remove it only after the init is done.
setTimeout ( ( ) = > {
( menuElement . getChildByID ( rootDomId ) as HTMLElement ) . hidden = false ;
} , 250 ) ;
}
2021-04-21 00:53:45 +02:00
update ( time : number , delta : number ) : void {
2021-05-06 16:25:13 +02:00
this . dirty = false ;
2021-04-21 00:53:45 +02:00
}
public onResize ( ev : UIEvent ) : void {
2021-05-06 16:25:13 +02:00
super . onResize ( ev ) ;
2021-05-10 19:05:01 +02:00
if ( this . helpCameraSettingsOpened ) {
const middleX = this . getMiddleX ( ) ;
const middleY = this . getMiddleY ( ) ;
this . tweens . add ( {
targets : this.helpCameraSettingsElement ,
x : middleX ,
y : middleY ,
duration : 1000 ,
ease : 'Power3'
} ) ;
this . dirty = true ;
}
2021-04-21 00:53:45 +02:00
}
private getMiddleX ( ) : number {
2021-05-05 17:07:03 +02:00
return ( this . scale . width / 2 ) -
2021-04-22 11:47:59 +02:00
(
2021-04-21 00:53:45 +02:00
this . helpCameraSettingsElement
&& this . helpCameraSettingsElement . node
&& this . helpCameraSettingsElement . node . getBoundingClientRect ( ) . width > 0
2021-05-05 17:07:03 +02:00
? ( this . helpCameraSettingsElement . node . getBoundingClientRect ( ) . width / ( 2 * this . scale . zoom ) )
2021-04-22 11:47:59 +02:00
: ( 400 / 2 )
) ;
2021-04-21 00:53:45 +02:00
}
private getMiddleY ( ) : number {
2021-05-05 17:07:03 +02:00
const middleY = ( ( this . scale . height ) - (
2021-04-21 00:53:45 +02:00
( this . helpCameraSettingsElement
&& this . helpCameraSettingsElement . node
&& this . helpCameraSettingsElement . node . getBoundingClientRect ( ) . height > 0
2021-05-05 17:07:03 +02:00
? this . helpCameraSettingsElement . node . getBoundingClientRect ( ) . height : 400 /*FIXME to use a const will be injected in HTMLElement*/ ) / this . scale . zoom ) ) / 2 ;
return ( middleY > 0 ? middleY : 0 ) ;
2021-04-21 00:53:45 +02:00
}
2021-05-06 16:25:13 +02:00
public isDirty ( ) : boolean {
return this . dirty ;
}
2021-03-19 15:40:07 +01:00
}