/* Ċ Ġ* ĠĠĠ ĠCopyright Ġ 2 0 2 1 ĠThe ĠBoard Game Work ĠAuthors Ċ Ġ* ĠĠĠ ĠSPDX - License - Identifier : ĠApache - 2 . 0 Ċ Ġ* Ċ Ġ* ĠĠĠ ĠLicensed Ġunder Ġthe ĠApache ĠLicense , ĠVersion Ġ 2 . 0 Ġ( the Ġ" License "); Ċ Ġ* ĠĠĠ Ġyou Ġmay Ġnot Ġuse Ġthis Ġfile Ġexcept Ġin Ġcompliance Ġwith Ġthe ĠLicense . Ċ Ġ* ĠĠĠ ĠYou Ġmay Ġobtain Ġa Ġcopy Ġof Ġthe ĠLicense Ġat Ċ Ġ* Ċ Ġ* ĠĠĠĠĠĠĠ Ġhttp :// www . apache . org / licenses / LICENSE - 2 . 0 Ċ Ġ* Ċ Ġ* ĠĠĠ ĠUnless Ġrequired Ġby Ġapplicable Ġlaw Ġor Ġagreed Ġto Ġin Ġwriting , Ġsoftware Ċ Ġ* ĠĠĠ Ġdistributed Ġunder Ġthe ĠLicense Ġis Ġdistributed Ġon Ġan Ġ" AS ĠIS " ĠBASIS , Ċ Ġ* ĠĠĠ ĠWITHOUT ĠWARRANTIES ĠOR ĠCONDITIONS ĠOF ĠANY ĠKIND , Ġeither Ġexpress Ġor Ġimplied . Ċ Ġ* ĠĠĠ ĠSee Ġthe ĠLicense Ġfor Ġthe Ġspecific Ġlanguage Ġgoverning Ġpermissions Ġand Ċ Ġ* ĠĠĠ Ġlimitations Ġunder Ġthe ĠLicense . Ċ Ġ*/ Ċ Ċ package Ġtools . a qua . bg w . builder Ċ Ċ import Ġjavafx . scene . Node Ċ import Ġjavafx . scene . layout . Region Ċ import Ġtools . a qua . bg w . builder . Node Builder . Companion . build Children Ċ import Ġtools . a qua . bg w . components . Component View Ċ import Ġtools . a qua . bg w . components . layout views . Grid Pane Ċ import Ġtools . a qua . bg w . components . layout views . Grid Pane . Companion . COLUMN _ WIDTH _ AUTO Ċ import Ġtools . a qua . bg w . components . layout views . Grid Pane . Companion . ROW _ HEIGHT _ AUTO Ċ import Ġtools . a qua . bg w . components . layout views . Layout View Ċ import Ġtools . a qua . bg w . components . layout views . Pane Ċ import Ġtools . a qua . bg w . core . Scene Ċ import Ġtools . a qua . bg w . util . Component View Grid Ċ import Ġjavafx . scene . layout . Pane Ġas ĠFX Pane Ċ Ċ /** Ċ Ġ* ĠLayout Node Builder . Ċ Ġ* ĠFactory Ġfor Ġall ĠBG W Ġlayout Ġcomponents . Ċ Ġ*/ Ċ internal Ġclass ĠLayout Node Builder Ġ{ Ċ ĉ comp anion Ġobject Ġ{ Ċĉ ĉ /** Ċĉĉ Ġ* ĠSw itches Ġbetween ĠLayout Components . Ċĉĉ Ġ*/ Ċĉ ĉ internal Ġfun Ġbuild Layout View ( Ċĉĉ ĉ scene : ĠScene < out ĠComponent View >, Ċĉĉ ĉ layout View View : ĠLayout View < out ĠComponent View > Ċĉ ĉ ): ĠRegion Ġ= Ċĉĉ ĉ when Ġ( layout View View ) Ġ{ Ċĉĉĉ ĉ is ĠGrid Pane < *> Ġ-> Ċĉĉĉĉ ĉ build Grid ( scene , Ġlayout View View ) Ċĉĉĉ ĉ is ĠP ane < *> Ġ-> Ċĉĉĉĉ ĉ build Pane ( scene , Ġlayout View View ) Ċĉĉ ĉ } ĊĉĉĊĉ ĉ private Ġfun Ġbuild Pane ( scene : ĠScene < out ĠComponent View >, Ġpane : ĠP ane < out ĠComponent View >): ĠRegion Ġ= Ċĉĉ ĉ FX Pane (). apply Ġ{ Ċĉĉĉ ĉ pane . observable Components . set GUI Listener And Invoke ( listOf ()) Ġ{ ĠoldValue , Ġ_ Ġ-> Ċĉĉĉĉ ĉ build Children ( scene , Ġpane . observable Components , ĠoldValue . toSet ()) Ċĉĉĉ ĉ } Ċĉĉ ĉ } ĊĉĉĊĉ ĉ /** Ċĉĉ Ġ* ĠBuilds Ġ[ Grid Pane ]. Ċĉĉ Ġ*/ Ċĉ ĉ private Ġfun Ġbuild Grid ( scene : ĠScene < out ĠComponent View >, Ġgrid View : ĠGrid Pane < out ĠComponent View >): ĠRegion Ġ= Ċĉĉ ĉ FX Pane (). apply Ġ{ Ċĉĉĉ ĉ grid View . update Gui Ġ= Ġ{ Ġrefresh Grid ( scene , Ġgrid View ) Ġ}. also Ġ{ Ġit . invoke () Ġ} Ċĉĉ ĉ } ĊĉĉĊĉ ĉ /** Ċĉĉ Ġ* ĠRef res hes Ġ[ Grid Pane ]. Ċĉĉ Ġ*/ Ċĉ ĉ private Ġfun ĠFX Pane . refresh Grid ( scene : ĠScene < out ĠComponent View >, Ġgrid View : ĠGrid Pane < out ĠComponent View >) Ġ{ Ċĉĉ ĉ val Ġgrid : ĠComponent View Grid < out ĠComponent View > Ġ= Ġgrid View . grid ĊĉĉĉĊĉĉ ĉ children . clear () ĊĉĉĉĊĉĉ ĉ // Build ĠNodes Ċĉĉ ĉ val Ġnodes Ġ= ĠArrayList < Triple < Pair < Int , ĠInt >, ĠComponent View , ĠNode >>() Ċĉĉ ĉ grid . get Columns (). forEach Indexed Ġ{ Ġcol Index , Ġcol Ġ-> Ċĉĉĉ ĉ for Ġ( rowIndex Ġin Ġcol . indices ) Ġ{ Ċĉĉĉĉ ĉ val Ġgame Component Ġ= Ġcol [ rowIndex ] Ġ?: Ġcontinue ĊĉĉĉĉĉĊĉĉĉĉ ĉ nodes . add ( Ċĉĉĉĉĉ ĉ Triple ( Ċĉĉĉĉĉĉ ĉ Pair ( col Index , ĠrowIndex ), Ċĉĉĉĉĉĉ ĉ game Component , Ċĉĉĉĉĉĉ ĉ Node Builder . build ( scene Ġ= Ġscene , Ġcomponent View Ġ= Ġgame Component ) Ċĉĉĉĉĉ ĉ ) Ċĉĉĉĉ ĉ ) Ċĉĉĉ ĉ } Ċĉĉ ĉ } ĊĉĉĉĊĉĉ ĉ // Calculate Ġrow Ġheight s Ċĉĉ ĉ grid View . rendered Row Height s Ġ= ĠDouble Array ( grid . rows ) Ġ{ Ċĉĉĉ ĉ var Ġheight Ġ= Ġgrid . getRow Height ( it ) ĊĉĉĉĉĊĉĉĉ ĉ if ( height Ġ== ĠROW _ HEIGHT _ AUTO ) Ġ{ Ċĉĉĉĉ ĉ height Ġ= Ġgrid . getRow ( it ). filter NotNull (). max Of OrNull Ġ{ Ġentry Ġ-> Ċĉĉĉĉĉ ĉ entry . let Ġ{ Ġt Ġ-> Ġt . layout Bounds . height Ġ+ Ġt . pos Y Ġ} Ċĉĉĉĉ ĉ } ?: 0 . 0 Ċĉĉĉ ĉ } ĊĉĉĉĉĊĉĉĉ ĉ height Ċĉĉ ĉ } ĊĉĉĉĊĉĉ ĉ // Calculate Ġcolumn Ġwidths Ċĉĉ ĉ grid View . rendered Col Widths Ġ= ĠDouble Array ( grid . columns ) Ġ{ Ċĉĉĉ ĉ var Ġwidth Ġ= Ġgrid . getColumn Width ( it ) ĊĉĉĉĉĊĉĉĉ ĉ if ( width Ġ== ĠCOLUMN _ WIDTH _ AUTO ) Ġ{ Ċĉĉĉĉ ĉ width Ġ= Ġgrid . getColumn ( it ). filter NotNull (). max Of OrNull Ġ{ Ġentry Ġ-> Ċĉĉĉĉĉ ĉ entry . let Ġ{ Ġt Ġ-> Ġt . layout Bounds . width Ġ+ Ġt . pos X Ġ} Ċĉĉĉĉ ĉ } ?: 0 . 0 Ċĉĉĉ ĉ } ĊĉĉĉĉĊĉĉĉ ĉ width Ċĉĉ ĉ } ĊĉĉĉĊĉĉ ĉ grid View . width Ġ= Ċĉĉĉ ĉ grid View . rendered Col Widths . sum () Ġ+ Ġ( grid View . rendered Col Widths . size Ġ- Ġ 1 ) Ġ* Ġgrid View . spacing Ċĉĉ ĉ grid View . height Ġ= Ċĉĉĉ ĉ grid View . rendered Row Height s . sum () Ġ+ Ġ( grid View . rendered Row Height s . size Ġ- Ġ 1 ) Ġ* Ġgrid View . spacing ĊĉĉĉĊĉĉ ĉ nodes . forEach Ġ{ Ġtriple Ġ-> Ġrefresh Grid Node ( grid View , Ġtriple ) Ġ} Ċĉ ĉ } ĊĉĉĊĉ ĉ /** Ċĉĉ Ġ* ĠRef res hes Ġgrid Ġnode . Ċĉĉ Ġ*/ Ċĉ ĉ private Ġfun ĠFX Pane . refresh Grid Node ( Ċĉĉ ĉ grid View : ĠGrid Pane < out ĠComponent View >, Ċĉĉ ĉ triple : ĠTrip le < Pair < Int , ĠInt >, ĠComponent View , ĠNode >) Ġ{ ĊĉĉĉĊĉĉ ĉ val Ġcol Index Ġ= Ġtriple . first . first Ċĉĉ ĉ val ĠrowIndex Ġ= Ġtriple . first . second Ċĉĉ ĉ val Ġcomponent Ġ= Ġtriple . second Ċĉĉ ĉ val Ġnode Ġ= Ġtriple . third Ċĉĉ ĉ val Ġpos X Ġ= Ġ( 0 Ġuntil Ġcol Index ). sum Of Ġ{ Ġgrid View . rendered Col Widths [ it ] Ġ} Ġ+ Ġcol Index Ġ* Ġgrid View . spacing Ċĉĉ ĉ val Ġpos Y Ġ= Ġ( 0 Ġuntil ĠrowIndex ). sum Of Ġ{ Ġgrid View . rendered Row Height s [ it ] Ġ} Ġ+ ĠrowIndex Ġ* Ġgrid View . spacing ĊĉĉĉĊĉĉ ĉ children . add ( node . apply Ġ{ Ċĉĉĉ ĉ val Ġnode Width Ġ= Ġcomponent . layout Bounds . width Ċĉĉĉ ĉ val Ġnode Height Ġ= Ġcomponent . layout Bounds . height ĊĉĉĉĉĊĉĉĉ ĉ // Calculate Ġdelta Ġdue Ġto Ġscale Ġand Ġrotation Ċĉĉĉ ĉ val Ġdelta X Ġ= Ġ( node Width Ġ- Ġcomponent . width ) Ġ/ Ġ 2 Ċĉĉĉ ĉ val Ġdelta Y Ġ= Ġ( node Height Ġ- Ġcomponent . height ) Ġ/ Ġ 2 ĊĉĉĉĉĊĉĉĉ ĉ // Calculate Ġanchor Ġpoint Ġfor Ġflush ĠTOP _ LEFT Ġplacement Ċĉĉĉ ĉ val Ġanchor X Ġ= Ġpos X Ġ+ Ġcomponent . pos X Ġ+ Ġdelta X Ċĉĉĉ ĉ val Ġanchor Y Ġ= Ġpos Y Ġ+ Ġcomponent . pos Y Ġ+ Ġdelta Y ĊĉĉĉĉĊĉĉĉ ĉ // Account Ġfor Ġcenter ing Ċĉĉĉ ĉ val Ġcenter Mode Ġ= Ġgrid View . getCell Center Mode ( column Index Ġ= Ġcol Index , ĠrowIndex Ġ= ĠrowIndex ) Ċĉĉĉ ĉ val Ġremaining Space X Ġ= Ġgrid View . rendered Col Widths [ col Index ] Ġ- Ġnode Width Ġ- Ġcomponent . pos X Ċĉĉĉ ĉ val Ġremaining Space Y Ġ= Ġgrid View . rendered Row Height s [ rowIndex ] Ġ- Ġnode Height Ġ- Ġcomponent . pos Y ĊĉĉĉĉĊĉĉĉ ĉ layout X Ġ= Ġanchor X Ġ+ Ġremaining Space X Ġ* Ġcenter Mode . horizontal Alignment . position Multiplier Ċĉĉĉ ĉ layout Y Ġ= Ġanchor Y Ġ+ Ġremaining Space Y Ġ* Ġcenter Mode . vertical Alignment . position Multiplier Ċĉĉ ĉ }) Ċĉ ĉ } Ċ ĉ } Ċ }