import { MultistateSprite } from "../MultistateSprite" ; import { Texture } from "pixi.js" ; import { EventManager } from "../../event/EventManager" ; import { Component } from "../../core/Component" ; import { EventPoint } from "../utils/EventPoint" ; /**
 * 开关
 */ export class Switch extends Component { protected backgroundSprite : MultistateSprite ; protected sliderSprite : MultistateSprite ; protected _backgroundTexture : Texture ; protected _sliderTexture : Texture ; protected _selected : boolean ; protected _chageCallback : Function | undefined ; private _sliderY : number ; private _sliderX : number ; private _backgroundSelectedTexture : Texture ; private _sliderSelectedTexture : Texture ; protected _anchor : EventPoint ; rootComponent : Component ; private _x : number ; private _y : number ; constructor ( options ? ) { super ( ) ; if ( options ) { if ( options . backgroundTexture ) { this . backgroundTexture = options . backgroundTexture ; } if ( options . sliderTexture ) { this . sliderTexture = options . sliderTexture ; } if ( options . width !== undefined ) { this . width = options . width ; } if ( options . height !== undefined ) { this . height = options . height ; } } this . _anchor . on ( "chageX" , ( ) => { this . x = this . x ; } ) ; this . _anchor . on ( "chageY" , ( ) => { this . y = this . y ; } ) ; this . x = 0 ; this . y = 0 ; this . sliderX = 0 ; this . sliderY = 0 ; this . buttonMode = true ; } /**
     * 设置事件
     */ handleEvents ( ) { this . on ( EventManager . ALL_CLICK , ( ) => { this . selected = ! this . selected ; } ) ; } param callback setChageCallback ( callback ) { this . _chageCallback = callback ; } /**
     * 是否选中
     */ set selected ( selected ) { if ( this . _selected == selected ) { return ; } this . _selected = selected ; this . chageLayuot ( ) ; this . chageView ( ) ; this . emit ( "selected" , selected ) ; if ( this . _chageCallback ) { this . _chageCallback ( selected ) ; } } get selected ( ) : boolean { return this . _selected ; } /**
     * 更改位置
     */ chageLayuot ( ) { this . sliderX = this . sliderX ; } /**
     * 更改渲染
     */ chageView ( ) { this . backgroundSprite . setState ( this . selected ? "selected" : "default" ) ; this . sliderSprite . setState ( this . selected ? "selected" : "default" ) ; } /**
     * 绘制图片
     */ draw ( ) { if ( ! this . _anchor ) { this . _anchor = new EventPoint ( ) ; } if ( ! this . rootComponent ) { this . rootComponent = new Component ( ) ; super . addChild ( this . rootComponent ) ; } if ( ! this . backgroundSprite ) { this . backgroundSprite = new MultistateSprite ( ) ; this . addChild ( this . backgroundSprite ) ; } if ( ! this . sliderSprite ) { this . sliderSprite = new MultistateSprite ( ) ; this . sliderSprite . anchor . set ( 0 , 0.5 ) ; this . addChild ( this . sliderSprite ) ; } this . backgroundSprite . setState ( ) ; this . sliderSprite . setState ( ) ; } /**
     * 默认背景图片
     */ set backgroundTexture ( texture ) { this . backgroundSprite . setDefaultState ( texture ) ; this . _backgroundTexture = texture ; this . draw ( ) ; this . width = texture . width ; this . height = texture . height ; } get backgroundTexture ( ) : Texture { return this . _backgroundTexture ; } /**
     * 背景选中图片
     */ set backgroundSelectedTexture ( texture ) { this . backgroundSprite . addState ( "selected" , texture ) ; this . _backgroundSelectedTexture = texture ; this . draw ( ) ; this . width = texture . width ; this . height = texture . height ; } get backgroundSelectedTexture ( ) : Texture { return this . _backgroundSelectedTexture ; } /**
     * 滑动块  图片
     */ set sliderTexture ( texture ) { this . sliderSprite . setDefaultState ( texture ) ; this . _sliderTexture = texture ; this . draw ( ) ; this . sliderWidth = texture . width ; this . sliderHeight = texture . height ; } get sliderTexture ( ) : Texture { return this . _sliderTexture ; } /**
     * 滑动块 选中 图片
     */ set sliderSelectedTexture ( texture ) { this . sliderSprite . addState ( "selected" , texture ) ; this . _sliderSelectedTexture = texture ; this . draw ( ) ; this . sliderWidth = texture . width ; this . sliderHeight = texture . height ; } get sliderSelectedTexture ( ) : Texture { return this . _sliderSelectedTexture ; } set height ( height ) { this . backgroundSprite . height = height ; this . sliderY = this . sliderY ; this . y = this . y ; } get height ( ) : number { return this . backgroundSprite . height ; } set width ( width ) { this . backgroundSprite . width = width ; this . sliderX = this . sliderX ; this . x = this . x ; } get width ( ) : number { return this . backgroundSprite . width ; } set sliderHeight ( height ) { this . sliderSprite . height = height ; this . sliderY = this . sliderY ; } get sliderHeight ( ) : number { return this . sliderSprite . height ; } set sliderWidth ( width ) { this . sliderSprite . width = width ; this . sliderX = this . sliderX ; } get sliderWidth ( ) : number { return this . sliderSprite . width ; } set sliderY ( y ) { this . _sliderY = y ; this . sliderSprite . y = this . height / 2 + y ; } get sliderY ( ) : number { return this . _sliderY ; } set sliderX ( x ) { this . _sliderX = x ; this . sliderSprite . x = this . selected ? this . width - this . sliderWidth + x : x ; } get sliderX ( ) : number { return this . _sliderX ; } set x ( x ) { this . rootComponent . x = x - this . anchor . x * this . width ; this . _x = x ; } get x ( ) : number { return this . _x ; } set y ( y ) { this . rootComponent . y = y - this . anchor . y * this . height ; this . _y = y ; } get y ( ) : number { return this . _y ; } set anchor ( anchor ) { this . _anchor = anchor ; this . x = this . x ; this . y = this . y ; } get anchor ( ) : Point { return this . _anchor ; } addChild ( child ) { this . rootComponent . addChild ( child ) ; } addChildAt ( child , index ) { this . rootComponent . addChildAt ( child , index ) ; } removeChild ( child ) { this . rootComponent . removeChild ( child ) ; } removeChildAt ( index ) { this . rootComponent . removeChildAt ( index ) ; } removeAllChild ( ) { for ( let i = 0 ; i < this . children . length ; i ++ ) { this . rootComponent . removeChildAt ( i ) ; i -- ; } } }