import * as PIXI from 'pixi.js' ; import G from '../../common/globals' ; import { colors , styles } from '../style' ; import F from './functions' ; import { Panel } from './Panel' ; /**
 * Base Dialog for usage whenever a dialog shall be shown to the user
 *
 * Per default the dialog
 *  + is not visible (this.visible = false)
 *  + is interactive (this.interactive = true)
 *  + has interactive children (this.interactiveChildren = true)
 *  + automatically executes 'setDialogPosition()' on Browser Resizing
 */ export abstract class Dialog extends Panel { /** Stores all open dialogs */ protected static s_openDialogs : Dialog [ ] = [ ] ; public constructor ( width , height , title ? ) { super ( width , height , colors . dialog . background . color , colors . dialog . background . alpha , colors . dialog . background . border ) ; this . visible = true ; this . interactive = true ; this . interactiveChildren = true ; if ( title !== undefined ) { this . addLabel ( 12 , 10 , title , styles . dialog . title ) ; } Dialog . s_openDialogs . push ( this ) ; } /** Closes last open dialog */ public static closeLast ( ) { if ( Dialog . anyOpen ( ) ) { Dialog . s_openDialogs [ Dialog . s_openDialogs . length - 1 ] . close ( ) ; } } /** Closes all open dialogs */ public static closeAll ( ) { for ( const d of Dialog . s_openDialogs ) { d . close ( ) ; } } returns public static anyOpen ( ) { return Dialog . s_openDialogs . length > 0 ; } public static isOpen < T extends Dialog > ( dialog ) { return ! ! Dialog . s_openDialogs . find ( d => d === dialog ) ; } /** Capitalize String */ protected static capitalize ( text ) { return text . split ( '_' ) . map ( ( s ) => s . charAt ( 0 ) . toUpperCase ( ) + s . slice ( 1 ) ) . join ( ' ' ) ; } /** Automatically sets position of dialog to center screen */ protected setPosition ( ) { this . position . set ( G . app . screen . width / 2 - this . width / 2 , G . app . screen . height / 2 - this . height / 2 ) ; } /** Close Dialog */ public close ( ) { Dialog . s_openDialogs = Dialog . s_openDialogs . filter ( d => d !== this ) ; this . emit ( 'close' ) ; this . destroy ( ) ; } description param x param y param text param style returns protected addLabel ( x = 140 , y = 56 , text = 'Recipe:' , style = styles . dialog . label ) { const label = new PIXI . Text ( text , style ) ; label . position . set ( x , y ) ; this . addChild ( label ) ; return label ; } description param x param y param width param style returns protected addLine ( x , y , width , height , border = colors . dialog . line . background . border ) { const line = F . DrawRectangle ( width , height , colors . dialog . line . background . color , colors . dialog . line . background . alpha , border , true ) ; line . position . set ( x , y ) ; this . addChild ( line ) ; return line ; } }