@@ -3,16 +3,21 @@ import { useAppContext } from '../../../app-context'
33import { TournamentGameElement } from './tournament-game'
44import Tournament , { TournamentGame } from '../../../playback/Tournament'
55import { Space } from 'react-zoomable-ui'
6- import { useForceUpdate } from '../../../util/react-util'
76
87export const TournamentRenderer : React . FC = ( ) => {
98 const appContext = useAppContext ( )
109
10+ const spaceRef = useRef < Space | null > ( null )
11+
1112 return (
1213 < div className = "w-full h-screen relative" >
13- < Space treatTwoFingerTrackPadGesturesLikeTouch = { false } >
14- { appContext . state . tournament ? (
15- < TournamentTree tournament = { appContext . state . tournament } />
14+ < Space ref = { spaceRef } treatTwoFingerTrackPadGesturesLikeTouch = { false } >
15+ { appContext . state . tournament && spaceRef . current ? (
16+ < TournamentTree
17+ tournament = { appContext . state . tournament }
18+ minRound = { appContext . state . tournamentMinRound }
19+ spaceRef = { spaceRef . current }
20+ />
1621 ) : (
1722 < > Missing Tournament</ >
1823 ) }
@@ -23,9 +28,11 @@ export const TournamentRenderer: React.FC = () => {
2328
2429interface TournamentTreeProps {
2530 tournament : Tournament
31+ minRound : number
32+ spaceRef : Space
2633}
2734
28- const TournamentTree : React . FC < TournamentTreeProps > = ( { tournament } ) => {
35+ const TournamentTree : React . FC < TournamentTreeProps > = ( props ) => {
2936 // const [leafWidth, setLeafWidth] = useState<number | undefined>(undefined)
3037 // const leafRef = useRef<HTMLDivElement>(null)
3138
@@ -38,18 +45,18 @@ const TournamentTree: React.FC<TournamentTreeProps> = ({ tournament }) => {
3845 // resizeObserver.observe(leafRef.current)
3946 // }, [])
4047
41- const brackets : [ TournamentGame , string ] [ ] = tournament . losersBracketRoot
48+ const brackets : [ TournamentGame , string ] [ ] = props . tournament . losersBracketRoot
4249 ? [
43- [ tournament . winnersBracketRoot , 'Winners Bracket' ] ,
44- [ tournament . losersBracketRoot , 'Losers Bracket' ]
50+ [ props . tournament . winnersBracketRoot , 'Winners Bracket' ] ,
51+ [ props . tournament . losersBracketRoot , 'Losers Bracket' ]
4552 ]
46- : [ [ tournament . winnersBracketRoot , '' ] ]
53+ : [ [ props . tournament . winnersBracketRoot , '' ] ]
4754
4855 return (
4956 < div className = "flex flex-row items-center justify-center w-full h-screen" >
5057 { brackets . map ( ( [ rootGame , bracketTitle ] ) => (
5158 < div className = "flex flex-col justify-center w-max mx-2" key = { bracketTitle } >
52- < TournamentGameWrapper game = { rootGame } />
59+ < TournamentGameWrapper game = { rootGame } minRound = { props . minRound } spaceRef = { props . spaceRef } />
5360 { bracketTitle && (
5461 < div className = "text-white pt-2 text-center border-t border-white" > { bracketTitle } </ div >
5562 ) }
@@ -59,7 +66,13 @@ const TournamentTree: React.FC<TournamentTreeProps> = ({ tournament }) => {
5966 )
6067}
6168
62- const TournamentGameWrapper : React . FC < { game : TournamentGame } > = ( { game } ) => {
69+ interface TournamentGameWrapperProps {
70+ game : TournamentGame
71+ minRound : number
72+ spaceRef : Space
73+ }
74+
75+ const TournamentGameWrapper : React . FC < TournamentGameWrapperProps > = ( props ) => {
6376 const wrapperRef = useRef < HTMLDivElement > ( null )
6477 const childWrapper1Ref = useRef < HTMLDivElement > ( null )
6578 const childWrapper2Ref = useRef < HTMLDivElement > ( null )
@@ -71,56 +84,73 @@ const TournamentGameWrapper: React.FC<{ game: TournamentGame }> = ({ game }) =>
7184 } )
7285
7386 useEffect ( ( ) => {
74- if ( ! wrapperRef . current ) return
87+ setTimeout ( ( ) => {
88+ if ( ! wrapperRef . current ) return
89+
90+ const wrapperRect = wrapperRef . current . getBoundingClientRect ( )
91+ const scale = props . spaceRef . viewPort ?. zoomFactor ?? 1
92+ const startX = wrapperRect . x + wrapperRect . width / 2
93+ const startY = wrapperRect . y + wrapperRect . height - 17.45
7594
76- const wrapperRect = wrapperRef . current . getBoundingClientRect ( )
77- const startX = wrapperRect . x + wrapperRect . width / 2
78- const startY = wrapperRect . y + wrapperRect . height - 17.45
95+ let left = undefined
96+ let right = undefined
97+ let down = 0
7998
80- let left = undefined
81- let right = undefined
82- let down = 0
99+ if ( childWrapper1Ref . current ) {
100+ const childWrapper1Rect = childWrapper1Ref . current . getBoundingClientRect ( )
101+ const child1X = childWrapper1Rect . x + childWrapper1Rect . width / 2
102+ const child1Y = childWrapper1Rect . y + 17.45
83103
84- if ( childWrapper1Ref . current ) {
85- const childWrapper1Rect = childWrapper1Ref . current . getBoundingClientRect ( )
86- const child1X = childWrapper1Rect . x + childWrapper1Rect . width / 2
87- const child1Y = childWrapper1Rect . y + 17.45
104+ left = Math . abs ( child1X - startX ) / scale
105+ down = Math . abs ( child1Y - startY )
106+ }
88107
89- left = Math . abs ( child1X - startX )
90- down = Math . abs ( child1Y - startY )
91- }
108+ if ( childWrapper2Ref . current ) {
109+ const childWrapper2Rect = childWrapper2Ref . current . getBoundingClientRect ( )
110+ const child2X = childWrapper2Rect . x + childWrapper2Rect . width / 2
111+ const child2Y = childWrapper2Rect . y + 17.45
92112
93- if ( childWrapper2Ref . current ) {
94- const childWrapper2Rect = childWrapper2Ref . current . getBoundingClientRect ( )
95- const child2X = childWrapper2Rect . x + childWrapper2Rect . width / 2
96- const child2Y = childWrapper2Rect . y + 17.45
113+ right = Math . abs ( child2X - startX ) / scale
114+ down = Math . abs ( Math . max ( down , child2Y - startY ) )
115+ }
97116
98- right = Math . abs ( child2X - startX )
99- down = Math . abs ( Math . max ( down , child2Y - startY ) )
100- }
117+ setLines ( { left , right , down } )
118+ } , 2 )
119+ } , [ wrapperRef . current , childWrapper1Ref . current , childWrapper2Ref . current , props . minRound ] )
101120
102- setLines ( { left, right, down } )
103- } , [ wrapperRef . current , childWrapper1Ref . current , childWrapper2Ref . current ] )
121+ if ( props . game . round < props . minRound ) {
122+ props . game . viewed = true
123+ }
104124
105125 return (
106- < div className = "flex flex-col" key = { game . id } >
126+ < div className = "flex flex-col" key = { props . game . id } >
107127 < div
108128 className = "flex flex-col flex-grow basis-0 " /*ref={round === 1 && index === 0 ? leafRef : undefined}*/
109129 >
110- < div className = "mx-auto relative" ref = { wrapperRef } >
111- < TournamentGameElement game = { game } />
112- < GameChildrenLines lines = { lines } />
113- </ div >
130+ { props . game . round >= props . minRound && (
131+ < div className = "mx-auto relative" ref = { wrapperRef } >
132+ < TournamentGameElement game = { props . game } />
133+ { props . game . round > props . minRound && < GameChildrenLines lines = { lines } /> }
134+ </ div >
135+ ) }
114136 </ div >
115137 < div className = "flex flex-row" >
116- { game . dependsOn [ 0 ] && (
138+ { props . game . dependsOn [ 0 ] && (
117139 < div className = "mx-auto" ref = { childWrapper1Ref } >
118- < TournamentGameWrapper game = { game . dependsOn [ 0 ] } />
140+ < TournamentGameWrapper
141+ game = { props . game . dependsOn [ 0 ] }
142+ minRound = { props . minRound }
143+ spaceRef = { props . spaceRef }
144+ />
119145 </ div >
120146 ) }
121- { game . dependsOn [ 1 ] && (
147+ { props . game . dependsOn [ 1 ] && (
122148 < div className = "mx-auto" ref = { childWrapper2Ref } >
123- < TournamentGameWrapper game = { game . dependsOn [ 1 ] } />
149+ < TournamentGameWrapper
150+ game = { props . game . dependsOn [ 1 ] }
151+ minRound = { props . minRound }
152+ spaceRef = { props . spaceRef }
153+ />
124154 </ div >
125155 ) }
126156 </ div >
0 commit comments