import { InputHandler } from "./ToolInputHandler" ; import { CurvyWallControl } from '../CurvyWallsToolBar' ; import SETTINGS from "../../../common/Settings" ; export enum ToolMode { NotPlaced , Placing , Placed } export abstract class BezierTool { public static readonly PREF_SMALL_HANDLES = "BezierTool.SmallHandles" ; public static get HANDLE_RADIUS ( ) : number { return SETTINGS . get ( this . PREF_SMALL_HANDLES ) ? 5 : 10 ; } public static readonly LINE_SIZE : number = 2 ; private static readonly TEXT_STYLE_BASE = new PIXI . TextStyle ( { fontFamily : CONFIG . defaultFontFamily , fontSize : 24 , fill : "#BBBBBB" , stroke : "#111111" , strokeThickness : 4 , dropShadow : false , dropShadowColor : "#000000" , dropShadowBlur : Math . max ( Math . round ( 24 / 16 ) , 2 ) , dropShadowAngle : 0 , dropShadowDistance : 0 , padding : 1 } ) ; public static get TEXT_STYLE ( ) : PIXI . TextStyle { this . TEXT_STYLE_BASE . fontSize = SETTINGS . get ( this . PREF_SMALL_HANDLES ) ? 18 : 24 ; return this . TEXT_STYLE_BASE ; } private _mode : ToolMode = ToolMode . NotPlaced ; protected lastSegmentFetch : PIXI . Point [ ] | PIXI . Point [ ] [ ] = [ ] ; abstract get handles ( ) : PIXI . Point [ ] ; abstract get bounds ( ) : PIXI . Bounds ; abstract lineA : PIXI . Point ; abstract lineB : PIXI . Point ; public segments : number = 8 ; get lineCenter ( ) : PIXI . Point { return new PIXI . Point ( this . lineA . x + ( ( this . lineB . x - this . lineA . x ) / 2 ) , this . lineA . y + ( ( this . lineB . y - this . lineA . y ) / 2 ) ) ; } private _modeListener : ( mode : ToolMode ) => void = null ; get mode ( ) : ToolMode { return this . _mode ; } protected setMode ( value ) { if ( this . _mode === value ) return ; this . _mode = value ; if ( this . _modeListener !== null ) this . _modeListener ( value ) ; } startedWithCtrlHeld : boolean = false ; constructor ( segments = 10 ) { this . segments = segments ; } abstract drawHandles ( context ) ; abstract checkPointForDrag ( point ) ; abstract getSegments ( count ) ; abstract placeTool ( point , data ) ; abstract getData ( ) ; abstract getTools ( ) ; checkPointForClick ( _point , _event ) { return false ; } clearContext ( _context ) { } clearTool ( ) { this . setMode ( ToolMode . NotPlaced ) ; } setModeListener ( listener ) { this . _modeListener = listener ; } protected static createText ( text ) { const result = new PreciseText ( text , BezierTool . TEXT_STYLE ) ; result . anchor . set ( 0.5 , 0.5 ) ; return result ; } protected drawSegmentLabel ( context ) { const text = BezierTool . createText ( `⊷${ this . lastSegmentFetch . length - 1 }` ) ; text . position = this . lineCenter ; context . addChild ( text ) ; } protected drawBoundingBox ( context ) { const bounds = this . bounds . getRectangle ( PIXI . Rectangle . EMPTY ) ; context . beginFill ( 0 , 0 ) . lineStyle ( BezierTool . LINE_SIZE , 0xE88D2D , 1 , 0.5 ) . drawRoundedRect ( bounds . left - 20 , bounds . top - 20 , bounds . width + 40 , bounds . height + 40 , 20 ) . endFill ( ) ; } protected drawHandle ( context , fill , point ) { return context . beginFill ( fill , 1 ) . lineStyle ( BezierTool . LINE_SIZE , 0x0 , 1 , 0.5 ) . drawCircle ( point . x , point . y , BezierTool . HANDLE_RADIUS ) . endFill ( ) ; } static pointNearPoint ( a , b , threshold ) { const x = a . x - b . x ; const y = a . y - b . y ; return ( ( x * x ) + ( y * y ) ) <= ( threshold * threshold ) ; } }