import * as PIXI from 'pixi.js' ; import FD from '../../../core/factorioData' ; import G from '../../../common/globals' ; import F from '../../controls/functions' ; import { Slot } from '../../controls/Slot' ; import { Entity , IFilter } from '../../../core/Entity' ; /** Module Slots for Entity */ export class Filters extends PIXI . Container { /** Blueprint Editor Entity reference */ private readonly m_Entity : Entity ; /** Field to indicate whether counts shall be shown (Used for 2 chests) */ private readonly m_Amount : boolean ; /** Field to hold data for module visualization */ private m_Filters : IFilter [ ] ; public constructor ( entity , amount = false ) { super ( ) ; this . m_Entity = entity ; this . m_Amount = amount ; this . m_UpdateFilters ( ) ; for ( let slotIndex = 0 ; slotIndex < this . m_Filters . length ; slotIndex ++ ) { const slot = new Slot ( ) ; slot . position . set ( ( slotIndex % 6 ) * 38 , Math . floor ( slotIndex / 6 ) * 38 ) ; slot . data = slotIndex ; slot . on ( 'pointerdown' , this . onSlotPointerDown ) ; this . addChild ( slot ) ; } this . m_UpdateSlots ( ) ; this . onEntityChange ( 'filters' , ( ) => { this . m_UpdateFilters ( ) ; this . m_UpdateSlots ( ) ; } ) ; } private onEntityChange ( event , fn ) { this . m_Entity . on ( event , fn ) ; this . once ( 'destroy' , ( ) => this . m_Entity . off ( event , fn ) ) ; } public destroy ( opts ? ) { this . emit ( 'destroy' ) ; super . destroy ( opts ) ; } param index param count public updateFilter ( index , count ) { if ( this . m_Filters [ index ] . count === count ) return ; this . m_Filters [ index ] . count = count ; this . m_Entity . filters = this . m_Filters ; this . m_UpdateSlots ( ) ; } param index public getFilterCount ( index ) { return this . m_Filters [ index ] . count ; } /** Update local filters array */ private m_UpdateFilters ( ) { const slots = this . m_Entity . filterSlots ; if ( slots > 0 ) { this . m_Filters = new Array ( slots ) ; const filters = this . m_Entity . filters ; if ( filters !== undefined ) { for ( const item of filters ) { this . m_Filters [ item . index - 1 ] = { index : item . index , name : item . name , count : item . count , } ; } } for ( let slotIndex = 0 ; slotIndex < slots ; slotIndex ++ ) { this . m_Filters [ slotIndex ] = this . m_Filters [ slotIndex ] === undefined ? { index : slotIndex + 1 , name : undefined } : this . m_Filters [ slotIndex ] ; } } } /** Update slot icons */ private m_UpdateSlots ( ) { for ( const slot of this . children ) { if ( ! ( slot instanceof Slot ) ) { continue ; } const slotIndex = slot . data as number ; const slotFilter = this . m_Filters [ slotIndex ] ; if ( slotFilter . name === undefined ) { if ( slot . content !== undefined ) { slot . content = undefined ; } } else { if ( slot . content === undefined || slot . name !== slotFilter . name || this . m_Amount ) { if ( this . m_Amount ) { if ( slot . content !== undefined ) { const text = slot . children [ 1 ] as PIXI . Text ; if ( text . text !== slotFilter . count . toString ( ) ) { slot . content = undefined ; } } slot . content = new PIXI . Container ( ) ; F . CreateIconWithAmount ( slot . content as PIXI . Container , - 16 , - 16 , slotFilter . name , slotFilter . count ) ; } else { slot . content = F . CreateIcon ( slotFilter . name ) ; } slot . name = slotFilter . name ; } } } this . emit ( 'changed' ) ; } /** Slot pointer down event handler */ private readonly onSlotPointerDown = ( e ) : void => { e . stopPropagation ( ) ; const slot = e . target as Slot ; const index = slot . data as number ; if ( e . data . button === 0 ) { if ( ! this . m_Amount || this . m_Filters [ index ] . name === undefined ) { G . UI . createInventory ( 'Select Filter' , this . m_Entity . acceptedFilters , name => { this . m_Filters [ index ] . name = name ; if ( this . m_Amount ) { this . m_Filters [ index ] . count = FD . items [ name ] . stack_size ; } this . m_Entity . filters = this . m_Filters ; if ( this . m_Amount ) { this . emit ( 'selected' , index , this . m_Filters [ index ] . count ) ; } } ) ; } else { if ( this . m_Amount ) { this . emit ( 'selected' , index , this . m_Filters [ index ] . count ) ; } } } else if ( e . data . button === 2 ) { this . m_Filters [ index ] . name = undefined ; this . m_Entity . filters = this . m_Filters ; if ( this . m_Amount ) { this . emit ( 'selected' , - 1 , 0 ) ; } } } ; }