import { Observable } from "babylonjs/Misc/observable" ; import { AdvancedDynamicTexture } from "./advancedDynamicTexture" ; /**
 * Class used to specific a value and its associated unit
 */ export class ValueAndUnit { private _value = 1 ; private _unit = ValueAndUnit . UNITMODE_PIXEL ; private _originalUnit : number ; see https public ignoreAdaptiveScaling = false ; /**
     * Observable event triggered each time the value or unit changes
     */ public onChangedObservable = new Observable < void > ( ) ; param value param unit param negativeValueAllowed public constructor ( value , /** defines the unit to store */ unit = ValueAndUnit . UNITMODE_PIXEL , /** defines a boolean indicating if the value can be negative */ public negativeValueAllowed = true ) { this . _value = value ; this . _unit = unit ; this . _originalUnit = unit ; } /** Gets a boolean indicating if the value is a percentage */ public get isPercentage ( ) : boolean { return this . _unit === ValueAndUnit . UNITMODE_PERCENTAGE ; } /** Gets a boolean indicating if the value is store as pixel */ public get isPixel ( ) : boolean { return this . _unit === ValueAndUnit . UNITMODE_PIXEL ; } deprecated public get internalValue ( ) : number { return this . _value ; } /** Gets value (without units) */ public get value ( ) : number { return this . _value ; } /** Sets value (without units) */ public set value ( value ) { if ( value !== this . _value ) { this . _value = value ; this . onChangedObservable . notifyObservers ( ) ; } } /** Gets units (without value) */ public get unit ( ) : number { return this . _unit ; } /** Sets units (without value) */ public set unit ( value ) { if ( value !== this . _unit ) { this . _unit = value ; this . onChangedObservable . notifyObservers ( ) ; } } param host param refValue returns public getValueInPixel ( host , refValue ) { if ( this . isPixel ) { return this . getValue ( host ) ; } return this . getValue ( host ) * refValue ; } param value param unit returns public updateInPlace ( value , unit = ValueAndUnit . UNITMODE_PIXEL ) { if ( this . value !== value || this . unit !== unit ) { this . _value = value ; this . _unit = unit ; this . onChangedObservable . notifyObservers ( ) ; } return this ; } param host returns public getValue ( host ) { if ( host && ! this . ignoreAdaptiveScaling && this . unit !== ValueAndUnit . UNITMODE_PERCENTAGE ) { var width = 0 ; var height = 0 ; if ( host . idealWidth ) { width = ( this . _value * host . getSize ( ) . width ) / host . idealWidth ; } if ( host . idealHeight ) { height = ( this . _value * host . getSize ( ) . height ) / host . idealHeight ; } if ( host . useSmallestIdeal && host . idealWidth && host . idealHeight ) { return window . innerWidth < window . innerHeight ? width : height ; } if ( host . idealWidth ) { return width ; } if ( host . idealHeight ) { return height ; } } return this . _value ; } param host param decimals returns public toString ( host , decimals ? ) { switch ( this . _unit ) { case ValueAndUnit . UNITMODE_PERCENTAGE : let percentage = this . getValue ( host ) * 100 ; return ( decimals ? percentage . toFixed ( decimals ) : percentage ) + "%" ; case ValueAndUnit . UNITMODE_PIXEL : let pixels = this . getValue ( host ) ; return ( decimals ? pixels . toFixed ( decimals ) : pixels ) + "px" ; } return this . _unit . toString ( ) ; } param source returns public fromString ( source ) { var match = ValueAndUnit . _Regex . exec ( source . toString ( ) ) ; if ( ! match || match . length === 0 ) { return false ; } var sourceValue = parseFloat ( match [ 1 ] ) ; var sourceUnit = this . _originalUnit ; if ( ! this . negativeValueAllowed ) { if ( sourceValue < 0 ) { sourceValue = 0 ; } } if ( match . length === 4 ) { switch ( match [ 3 ] ) { case "px" : sourceUnit = ValueAndUnit . UNITMODE_PIXEL ; break ; case "%" : sourceUnit = ValueAndUnit . UNITMODE_PERCENTAGE ; sourceValue /= 100.0 ; break ; } } if ( sourceValue === this . _value && sourceUnit === this . _unit ) { return false ; } this . _value = sourceValue ; this . _unit = sourceUnit ; this . onChangedObservable . notifyObservers ( ) ; return true ; } private static _Regex = /(^-?\d*(\.\d+)?)(%|px)?/ ; private static _UNITMODE_PERCENTAGE = 0 ; private static _UNITMODE_PIXEL = 1 ; /** UNITMODE_PERCENTAGE */ public static get UNITMODE_PERCENTAGE ( ) : number { return ValueAndUnit . _UNITMODE_PERCENTAGE ; } /** UNITMODE_PIXEL */ public static get UNITMODE_PIXEL ( ) : number { return ValueAndUnit . _UNITMODE_PIXEL ; } }