import { Direction } from "bp/global" ; import { Vector } from "bp/math" ; import { clone } from "bp/util" ; import type { JAnchor , JGadget } from "bp/content/json" ; import type { QuadrantDirection } from "bp/global" ; import type { IPoint , Line , Point } from "bp/math" ; import type { Piece } from "bp/design" ; /**
 *  JoinCandidate 是融合過程當中其中一個參與者的抽象化物件；
 * 這裡面分攤了  JoinerCore 的部份程式碼。
  export class JoinCandidate { public readonly p : Piece ; public readonly o : IPoint ; public readonly a : JAnchor [ ] ; public readonly v : Vector ; public readonly pt : IPoint ; public readonly e : Line ; constructor ( p , offset , a , pt , q , additionalOffset = Vector . ZERO ) { this . p = p ; this . o = offset ; this . a = a ; this . v = new Vector ( offset ) . addBy ( additionalOffset ) . neg ; this . pt = pt . add ( this . v ) . $toIPoint ( ) ; this . e = p . $shape . ridges [ q ] . $shift ( additionalOffset ) ; } public $setupDetour ( rawDetour , reverse ) { let detour = rawDetour . map ( p => p . add ( this . v ) . $toIPoint ( ) ) ; detour . push ( this . pt ) ; if ( reverse ) detour . reverse ( ) ; this . p . $clearDetour ( ) ; this . p . $addDetour ( detour ) ; } public $toGadget ( json ? , offset ? ) { let off = clone ( this . o ) ; if ( offset ) { off . x += offset . x ; off . y += offset . y ; } if ( off . x == 0 && off . y == 0 ) off = undefined ; return { pieces : [ json ? this . p . toJSON ( ) : this . p ] , offset : off , anchors : this . a . concat ( ) , } ; } public $isSteeperThan ( that ) { return this . p . $direction . $slope . gt ( that . p . $direction . $slope ) ; } public $setupAnchor ( upperLeft , anchor ) { let q = upperLeft ? Direction . UL : Direction . LR ; this . a [ q ] = { location : anchor . add ( this . v ) . $toIPoint ( ) } ; } }