/// <summary> /// Nulls all bracket controls stored in our internal collection and clears the collection after all controls have been nulled. /// </summary> private void ClearExistingBracketsAndWinner() { for (int i = _bracketControls.Count - 1; i >= 0; i--) { _bracketControls[i].Dispose(); _bracketControls.RemoveAt(i); } _bracketControls.Clear(); if (_winnerControl != null) { _winnerControl.Dispose(); _winnerControl = null; } }
/// <summary> /// Creates/draws tournament all brackets and displays on the appropriate panel. /// </summary> private void DrawTournamentBrackets() { //If the tournament object hasn't been instantiated, just exit (since there is //nothing to draw) if (_tournament == null) { return; } //Clear the existing controls ClearExistingBracketsAndWinner(); const int BRACKET_VERTICAL_MARGIN = 32; const int BRACKET_HORIZONTAL_MARGIN = 96; //Iterate through each tournament level, starting with the lowest brackets (highest //level value) first int bracketX = BRACKET_HORIZONTAL_MARGIN; int bracketY = BRACKET_VERTICAL_MARGIN; int?bracketWidth = null; int totalLevels = _tournament.TournamentLevels; for (int level = totalLevels; level >= 1; level--) { //Get all Bracket objects for the current level List <Bracket> levelBrackets = _tournament.GetBracketsForLevel(level); if (level == totalLevels) { //For the first/lowest level of the tournament, place each bracket evenly foreach (Bracket bracket in levelBrackets) { //Create the bracket control and store it internally ControlBracket newBracket = new ControlBracket() { Bracket = bracket, Parent = scLayout.Panel2, Location = new Point(bracketX, bracketY), Visible = true }; _bracketControls.Add(newBracket); //Adjust the Y position for the next bracket bracketY += newBracket.Height + BRACKET_VERTICAL_MARGIN; //Assign the bracket width, if it hasn't already been assigned already if (!bracketWidth.HasValue) { bracketWidth = newBracket.Width; } } bracketX += bracketWidth.Value + BRACKET_HORIZONTAL_MARGIN; bracketY = BRACKET_VERTICAL_MARGIN; } else { //Adjust the current level brackets according to the previous level //Get the previous level's bracket objects List <ControlBracket> prevLevelBrackets = (from plb in _bracketControls where plb.Bracket.Level == level + 1 select plb) .OrderBy(bc => bc.Bracket.Sequence) .ToList(); int prevLevelBracketIndex = 0; foreach (Bracket bracket in levelBrackets) { //Evaluate the next two previous level brackets to determine sizing requirements for the current //bracket ControlBracket prevLevelTopBracket = prevLevelBrackets[prevLevelBracketIndex++]; ControlBracket prevLevelBottomBracket = prevLevelBrackets[prevLevelBracketIndex++]; //Get the coordinates of the right side's midpoint for each previous level's bracket control int newBracketX = prevLevelTopBracket.Right; int topLeftY = (prevLevelTopBracket.Top) + (prevLevelTopBracket.Bottom - prevLevelTopBracket.Top) / 2; int bottomLeftY = (prevLevelBottomBracket.Top) + (prevLevelBottomBracket.Bottom - prevLevelBottomBracket.Top) / 2; //Determine the height of the new bracket int newBracketHeight = bottomLeftY - topLeftY; //Create the new bracket for the level and store it internally ControlBracket newBracket = new ControlBracket() { Bracket = bracket, Parent = scLayout.Panel2, Location = new Point(newBracketX, topLeftY), Height = newBracketHeight, Visible = true }; _bracketControls.Add(newBracket); } } } //Finally, now that the brackets have been created, create and place the winner control //Get the bracket control from the "finals" level ControlBracket finalBracketControl = (from bc in _bracketControls where bc.Bracket.Level == 1 select bc).First(); //Get the coordinates of the right side's midpoint of the final bracket control int winnerX = finalBracketControl.Right; int finalBracketMidpointY = finalBracketControl.Bottom - finalBracketControl.Top; //Determine location and place it in the UI _winnerControl = new ControlWinner() { Winner = _tournament.Winner, Parent = scLayout.Panel2, Location = new Point(winnerX, finalBracketMidpointY), Visible = true }; }