import * as PIXI from 'pixi.js' ; import FD , { IngredientOrResult , ColorWithAlpha , Item , Icon } from '../../core/factorioData' ; import { styles } from '../style' ; import G from '../../common/globals' ; param color param percent function ShadeColor ( color , percent ) { const amt = Math . round ( 2.55 * percent ) ; const R = ( color >> 16 ) + amt ; const G = ( ( color >> 8 ) & 0x00ff ) + amt ; const B = ( color & 0x0000ff ) + amt ; return ( 0x1000000 + ( R < 255 ? ( R < 1 ? 0 : R ) : 255 ) * 0x10000 + ( G < 255 ? ( G < 1 ? 0 : G ) : 255 ) * 0x100 + ( B < 255 ? ( B < 1 ? 0 : B ) : 255 ) ) ; } param width param height param background param alpha param border param pressed function DrawRectangle ( width , height , background , alpha = 1 , border = 0 , pressed = false ) { const rectangle = new PIXI . Graphics ( ) ; rectangle . alpha = alpha ; rectangle . beginFill ( background ) ; if ( border === 0 ) { rectangle . drawRect ( 0 , 0 , width , height ) ; } else { if ( border > 0 ) { rectangle . lineStyle ( { width : 1 , color : ShadeColor ( background , pressed ? - 12.5 : 22.5 ) , alignment : 0 , } ) . moveTo ( 0 , height ) . lineTo ( 0 , 0 ) . lineTo ( width , 0 ) . lineStyle ( { width : 1 , color : ShadeColor ( background , pressed ? 10 : - 7.5 ) , alignment : 0 , } ) . lineTo ( width , height ) . lineTo ( 0 , height ) ; } if ( border > 1 ) { rectangle . lineStyle ( { width : 1 , color : ShadeColor ( background , pressed ? - 10 : 20 ) , alignment : 0 , } ) . moveTo ( 1 , height - 1 ) . lineTo ( 1 , 1 ) . lineTo ( width - 1 , 1 ) . lineStyle ( { width : 1 , color : ShadeColor ( background , pressed ? 7.5 : - 5 ) , alignment : 0 , } ) . lineTo ( width - 1 , height - 1 ) . lineTo ( 1 , height - 1 ) ; } if ( border > 2 ) { rectangle . lineStyle ( { width : 1 , color : ShadeColor ( background , pressed ? - 7.5 : 17.5 ) , alignment : 0 , } ) . moveTo ( 2 , height - 2 ) . lineTo ( 2 , 2 ) . lineTo ( width - 2 , 2 ) . lineStyle ( { width : 1 , color : ShadeColor ( background , pressed ? 5 : - 2.5 ) , alignment : 0 , } ) . lineTo ( width - 2 , height - 2 ) . lineTo ( 2 , height - 2 ) ; } } rectangle . endFill ( ) ; return rectangle ; } param w param h param f param c param a param p0 param p1 param p2 param p3 function DrawControlFace ( w , h , f , c , a , p0 , p1 , p2 , p3 ) { const wf = w * f ; const hf = h * f ; const mask = new PIXI . Graphics ( ) ; mask . beginFill ( 0x000000 ) . drawRoundedRect ( 0 , 0 , wf , hf , 6 ) . endFill ( ) ; const face = new PIXI . Graphics ( ) ; face . beginFill ( c , a ) . drawRect ( 0 , 0 , wf , hf ) . endFill ( ) . lineStyle ( { width : f , color : ShadeColor ( c , p3 ) , alpha : a , alignment : 0 } ) . moveTo ( wf , 0 ) . lineTo ( wf , hf ) . lineTo ( 0 , hf ) . lineStyle ( { width : f , color : ShadeColor ( c , p2 ) , alpha : a , alignment : 0 } ) . moveTo ( wf - f , f ) . lineTo ( wf - f , hf - f ) . lineTo ( f , hf - f ) . lineStyle ( { width : f , color : ShadeColor ( c , p1 ) , alpha : a , alignment : 0 } ) . moveTo ( wf - f , f ) . lineTo ( f , f ) . lineTo ( f , hf - f ) . lineStyle ( { width : f , color : ShadeColor ( c , p0 ) , alpha : a , alignment : 0 } ) . moveTo ( wf , 0 ) . lineTo ( 0 , 0 ) . lineTo ( 0 , hf ) ; face . cacheAsBitmap = true ; face . scale . set ( 1 / f , 1 / f ) ; face . mask = mask ; return face ; } /** Create Icon from Sprite Item information */ function CreateIcon ( itemName , maxSize = 32 , setAnchor = true , darkBackground = false ) { const item = FD . items [ itemName ] || FD . fluids [ itemName ] || FD . recipes [ itemName ] || FD . signals [ itemName ] || FD . inventoryLayout . find ( g => g . name === itemName ) ; if ( item . icon || ( item as Item ) . dark_background_icon ) { return generateIcon ( item as Icon ) ; } if ( item . icons ) return generateIcons ( item . icons , item . icon_size , item . icon_mipmaps ) ; function generateIcon ( data ) { const icon = darkBackground && data . dark_background_icon ? data . dark_background_icon : data . icon ; const texture = G . getTexture ( icon , 0 , 0 , data . icon_size , data . icon_size ) ; const sprite = new PIXI . Sprite ( texture ) ; sprite . scale . set ( maxSize / data . icon_size ) ; if ( setAnchor ) { sprite . anchor . set ( 0.5 ) ; } return sprite ; } function generateIcons ( icons , icon_size ? , icon_mipmaps ? ) { const img = new PIXI . Container ( ) ; for ( const icon of icons ) { const sprite = generateIcon ( { icon_size , icon_mipmaps , ... icon } ) ; if ( icon . scale ) { sprite . scale . set ( icon . scale , icon . scale ) ; } if ( icon . shift ) { sprite . position . set ( icon . shift [ 0 ] , icon . shift [ 1 ] ) ; } if ( icon . tint ) { applyTint ( sprite , icon . tint ) ; } if ( ! setAnchor && icon . shift ) { sprite . position . x += sprite . width / 2 ; sprite . position . y += sprite . height / 2 ; } img . addChild ( sprite ) ; } return img ; } } param host param x param y param name param amount function CreateIconWithAmount ( host , x , y , name , amount ) { const icon = CreateIcon ( name , undefined , false ) ; icon . position . set ( x , y ) ; host . addChild ( icon ) ; const amountString = amount < 1000 ? amount . toString ( ) : `${ Math . floor ( amount / 1000 ) }k` ; const text = new PIXI . Text ( amountString , styles . icon . amount ) ; text . anchor . set ( 1 , 1 ) ; text . position . set ( x + 33 , y + 33 ) ; host . addChild ( text ) ; } function CreateRecipe ( host , x , y , ingredients , results , time ) { let nextX = x ; for ( const i of ingredients ) { CreateIconWithAmount ( host , nextX , y , i . name , i . amount ) ; nextX += 36 ; } nextX += 2 ; const timeText = `=${ time }s>` ; const timeSize = PIXI . TextMetrics . measureText ( timeText , styles . dialog . label ) ; const timeObject = new PIXI . Text ( timeText , styles . dialog . label ) ; timeObject . position . set ( nextX , 6 + y ) ; host . addChild ( timeObject ) ; nextX += timeSize . width + 6 ; for ( const r of results ) { CreateIconWithAmount ( host , nextX , y , r . name , r . amount ) ; nextX += 36 ; } } function applyTint ( s , tint ) { s . tint = PIXI . utils . rgb2hex ( [ tint . r || 0 , tint . g || 0 , tint . b || 0 ] ) ; s . alpha = tint . a || 1 ; } export default { ShadeColor , DrawRectangle , DrawControlFace , CreateIcon , CreateIconWithAmount , CreateRecipe , applyTint , } ;