Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Standing Tab and Standing Rank in games #288

Merged
merged 9 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions NHLGames/Controls/GameControl.vb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Imports System.Text
Imports MetroFramework
Imports MetroFramework.Drawing
Imports NHLGames.NHLStats
Imports NHLGames.Objects
Imports NHLGames.Utilities

Expand All @@ -15,6 +16,7 @@ Namespace Controls
Private ReadOnly _showSeriesRecord As Boolean
Private ReadOnly _showTeamCityAbr As Boolean
Private ReadOnly _showLiveTime As Boolean
Private ReadOnly _showStanding As Boolean
Private ReadOnly _broadcasters As Dictionary(Of String, String)
Public LiveReplayCode As LiveStatusCodeEnum
Private _lnkUnknowns() As Button
Expand All @@ -28,7 +30,7 @@ Namespace Controls
End Property

Public Sub UpdateGame(showScores As Boolean, showLiveScores As Boolean, showSeriesRecord As Boolean,
showTeamCityAbr As Boolean, showLiveTime As Boolean, Optional gameUpdated As Game = Nothing)
showTeamCityAbr As Boolean, showLiveTime As Boolean, showStanding As Boolean, Optional gameUpdated As Game = Nothing)
If gameUpdated IsNot Nothing Then
If gameUpdated.StreamsDict Is Nothing Then Return
_game = gameUpdated
Expand Down Expand Up @@ -123,7 +125,7 @@ Namespace Controls
NHLGamesMetro.RmText.GetString("gamePeriodFinal").ToUpper())
End If
End If
ElseIf _game.GameState <= GameStateEnum.Pregame Then
ElseIf _game.GameState <= GameStateEnum.Pregame Then
lblDivider.Visible = False
lblGameStatus.Visible = True
lblGameStatus.Text = _game.GameDate.ToLocalTime().ToString("h:mm tt")
Expand Down Expand Up @@ -182,6 +184,14 @@ Namespace Controls
lblHomeTeam.Visible = showTeamCityAbr
lblAwayTeam.Visible = showTeamCityAbr

If showStanding Then
Adorner.AddBadgeTo(picAway, Standing.GetCurrentStandings(StandingTypeEnum.League, Seasons.CurrentSeason.seasonId, _game.AwayTeam))
Adorner.AddBadgeTo(picHome, Standing.GetCurrentStandings(StandingTypeEnum.League, Seasons.CurrentSeason.seasonId, _game.HomeTeam))
Else
Adorner.RemoveBadgeFrom(picAway)
Adorner.RemoveBadgeFrom(picHome)
End If

tt.SetToolTip(picAway,
String.Format(NHLGamesMetro.RmText.GetString("lblAwayTeam"), _game.Away, _game.AwayTeam))
tt.SetToolTip(picHome,
Expand All @@ -191,7 +201,7 @@ Namespace Controls
End Sub

Public Sub New(game As Game, showScores As Boolean, showLiveScores As Boolean, showSeriesRecord As Boolean,
showTeamCityAbr As Boolean, showLiveTime As Boolean)
showTeamCityAbr As Boolean, showLiveTime As Boolean, showStanding As Boolean)

InitializeComponent()
_broadcasters = New Dictionary(Of String, String)() From {
Expand Down Expand Up @@ -223,6 +233,7 @@ Namespace Controls
_showSeriesRecord = showSeriesRecord
_showTeamCityAbr = showTeamCityAbr
_showLiveTime = showLiveTime
_showStanding = showStanding
_game = game

SetThemeAndSvgOnControl()
Expand Down Expand Up @@ -286,7 +297,7 @@ Namespace Controls
End If
End If

UpdateGame(_showScores, _showLiveScores, _showSeriesRecord, _showTeamCityAbr, _showLiveTime)
UpdateGame(_showScores, _showLiveScores, _showSeriesRecord, _showTeamCityAbr, _showLiveTime, _showStanding)
End Sub

Private Sub SetWholeGamePanel()
Expand Down Expand Up @@ -378,7 +389,7 @@ Namespace Controls
End Sub

Private Function WatchArgs() As GameWatchArguments
Return ApplicationSettings.Read(Of GameWatchArguments)(SettingsEnum.DefaultWatchArgs, NHLGamesMetro.watchArgs)
Return ApplicationSettings.Read(Of GameWatchArguments)(SettingsEnum.DefaultWatchArgs, NHLGamesMetro.WatchArgs)
End Function

Private Sub WatchStream(streamType As StreamerTypeEnum)
Expand Down
79 changes: 79 additions & 0 deletions NHLGames/Controls/NHLStats/Badge.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
Imports MetroFramework.Components
Imports MetroFramework.Controls

Namespace NHLStats
Public Module Adorner
Private controls As List(Of Control) = New List(Of Control)()

Function AddBadgeTo(ByVal ctl As Control, ByVal Text As String) As Boolean
If controls.Contains(ctl) Then Return False
Dim badge As Badge = New Badge()
badge.AutoSize = True
badge.Text = Text.PadLeft(2, "0")
badge.BackColor = Color.Transparent
controls.Add(ctl)
ctl.Controls.Add(badge)
SetPosition(badge, ctl)
Return True
End Function

Function RemoveBadgeFrom(ByVal ctl As Control) As Boolean
Dim badge As Badge = GetBadge(ctl)

If badge IsNot Nothing Then
ctl.Controls.Remove(badge)
controls.Remove(ctl)
Return True
Else
Return False
End If
End Function

Sub SetBadgeText(ByVal ctl As Control, ByVal newText As String)
Dim badge As Badge = GetBadge(ctl)

If badge IsNot Nothing Then
badge.Text = newText
SetPosition(badge, ctl)
End If
End Sub

Function GetBadgeText(ByVal ctl As Control) As String
Dim badge As Badge = GetBadge(ctl)
If badge IsNot Nothing Then Return badge.Text
Return ""
End Function

Private Sub SetPosition(ByVal badge As Badge, ByVal ctl As Control)
badge.Location = New Point(ctl.Width - badge.Width, ctl.Height - badge.Height)
End Sub

Private Function GetBadge(ByVal ctl As Control) As Badge
For c As Integer = 0 To ctl.Controls.Count - 1
If TypeOf ctl.Controls(c) Is Badge Then Return TryCast(ctl.Controls(c), Badge)
Next

Return Nothing
End Function

Class Badge
Inherits Label

Private ellipseBackColor As Color = Color.FromArgb(0, 174, 219)
Private ellipseForeColor As Color = Color.White
Private ellipseFont As Font = New Font("Sans Serif", 8.0F)

Public Sub New()
Me.AutoSize = True
Me.TextAlign = ContentAlignment.MiddleCenter
End Sub

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
e.Graphics.FillEllipse(New SolidBrush(ellipseBackColor), Me.ClientRectangle)
e.Graphics.DrawString(Text, ellipseFont, New SolidBrush(ellipseForeColor), 1, 4)
End Sub

End Class
End Module

End Namespace
201 changes: 201 additions & 0 deletions NHLGames/Controls/NHLStats/TeamCell.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
Imports NHLGames.Utilities

Namespace NHLStats
Public Class TeamCell
Inherits DataGridViewCell

Public Overrides ReadOnly Property DefaultNewRowValue() As Object
Get
' Use the current date and time as the default value.
Return String.Empty
End Get
End Property

Protected Overrides Sub Paint(graphics As Graphics, clipBounds As Rectangle, cellBounds As Rectangle, rowIndex As Integer, cellState As DataGridViewElementStates, value As Object, formattedValue As Object, errorText As String, cellStyle As DataGridViewCellStyle, advancedBorderStyle As DataGridViewAdvancedBorderStyle, paintParts As DataGridViewPaintParts)
PaintPrivate(graphics,
clipBounds,
cellBounds,
rowIndex,
cellState,
formattedValue,
errorText,
cellStyle,
advancedBorderStyle,
paintParts,
False,
False,
True)
End Sub

Private Function PaintPrivate(ByVal graphics As Graphics, ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex As Integer, ByVal cellState As DataGridViewElementStates, ByVal formattedValue As StandingRowHeaderViewModel, ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal paintParts As DataGridViewPaintParts, ByVal computeContentBounds As Boolean, ByVal computeErrorIconBounds As Boolean, ByVal paint As Boolean) As Rectangle

Dim resultBounds As Rectangle = Rectangle.Empty

If paint AndAlso PaintBorderCheck(paintParts) Then
PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)
End If

Dim borderWidthsRect As Rectangle = BorderWidths(advancedBorderStyle)
Dim valBounds As Rectangle = cellBounds
valBounds.Offset(borderWidthsRect.X, borderWidthsRect.Y)
valBounds.Width -= borderWidthsRect.Right
valBounds.Height -= borderWidthsRect.Bottom
Dim br As SolidBrush
Dim ptCurrentCell As Point = Me.DataGridView.CurrentCellAddress
Dim cellCurrent As Boolean = ptCurrentCell.X = Me.ColumnIndex AndAlso ptCurrentCell.Y = rowIndex
Dim cellEdited As Boolean = cellCurrent AndAlso Me.DataGridView.EditingControl IsNot Nothing
Dim cellSelected As Boolean = (cellState And DataGridViewElementStates.Selected) <> 0

If PaintSelectionBackgroundCheck(paintParts) AndAlso cellSelected AndAlso Not cellEdited Then
br = New SolidBrush(cellStyle.SelectionBackColor)
Else
br = New SolidBrush(cellStyle.BackColor)
End If

If paint AndAlso PaintBackgroundCheck(paintParts) AndAlso br.Color.A = 255 AndAlso valBounds.Width > 0 AndAlso valBounds.Height > 0 Then
graphics.FillRectangle(br, valBounds)
End If

Dim formattedString As String = String.Format($"{formattedValue.Rank.ToString().PadLeft(2)} {formattedValue.TeamName}")
If formattedString IsNot Nothing AndAlso ((paint AndAlso Not cellEdited) OrElse computeContentBounds) Then
If valBounds.Width > 0 AndAlso valBounds.Height > 0 Then
Dim flags As TextFormatFlags = ComputeTextFormatFlagsForCellStyleAlignment(DataGridView.RightToLeft, cellStyle.Alignment, cellStyle.WrapMode)

If paint Then

If PaintContentForegroundCheck(paintParts) Then

If (flags And TextFormatFlags.SingleLine) <> 0 Then
flags = flags Or TextFormatFlags.EndEllipsis
End If

TextRenderer.DrawText(graphics, formattedString, cellStyle.Font, valBounds, If(cellSelected, cellStyle.SelectionForeColor, cellStyle.ForeColor), flags)
End If
End If
End If
End If

Dim themeChar = "l"
If NHLGamesMetro.IsDarkMode Then
themeChar = "d"
End If

Dim teamName = $"{Team.TeamAbbreviation(formattedValue.TeamName)}_{themeChar}"
If paint AndAlso PaintContentForegroundCheck(paintParts) Then

Dim image = ImageFetcher.GetEmbeddedImage(teamName)
Dim width = 24
Dim height = 24
Dim scale = Math.Min(width / image.Width, height / image.Height)

Dim scaleWidth = CType(image.Width * scale, Int32)
Dim scaleHeight = CType(image.Height * scale, Int32)

graphics.DrawImage(image, valBounds.X + 25, valBounds.Y, scaleWidth, scaleHeight)
End If

Return resultBounds
End Function

Friend Shared Function PaintBackgroundCheck(ByVal paintParts As DataGridViewPaintParts) As Boolean
Return (paintParts And DataGridViewPaintParts.Background) <> 0
End Function

Friend Shared Function PaintSelectionBackgroundCheck(ByVal paintParts As DataGridViewPaintParts) As Boolean
Return (paintParts And DataGridViewPaintParts.SelectionBackground) <> 0
End Function

Friend Shared Function PaintBorderCheck(ByVal paintParts As DataGridViewPaintParts) As Boolean
Return (paintParts And DataGridViewPaintParts.Border) <> 0
End Function

Friend Shared Function PaintContentForegroundCheck(ByVal paintParts As DataGridViewPaintParts) As Boolean
Return (paintParts And DataGridViewPaintParts.ContentForeground) <> 0
End Function

Friend Shared Function ComputeTextFormatFlagsForCellStyleAlignment(ByVal rightToLeft As Boolean, ByVal alignment As DataGridViewContentAlignment, ByVal wrapMode As DataGridViewTriState) As TextFormatFlags
Dim tff As TextFormatFlags

Select Case alignment
Case DataGridViewContentAlignment.TopLeft
tff = TextFormatFlags.Top

If rightToLeft Then
tff = tff Or TextFormatFlags.Right
Else
tff = tff Or TextFormatFlags.Left
End If

Case DataGridViewContentAlignment.TopCenter
tff = TextFormatFlags.Top Or TextFormatFlags.HorizontalCenter
Case DataGridViewContentAlignment.TopRight
tff = TextFormatFlags.Top

If rightToLeft Then
tff = tff Or TextFormatFlags.Left
Else
tff = tff Or TextFormatFlags.Right
End If

Case DataGridViewContentAlignment.MiddleLeft
tff = TextFormatFlags.VerticalCenter

If rightToLeft Then
tff = tff Or TextFormatFlags.Right
Else
tff = tff Or TextFormatFlags.Left
End If

Case DataGridViewContentAlignment.MiddleCenter
tff = TextFormatFlags.VerticalCenter Or TextFormatFlags.HorizontalCenter
Case DataGridViewContentAlignment.MiddleRight
tff = TextFormatFlags.VerticalCenter

If rightToLeft Then
tff = tff Or TextFormatFlags.Left
Else
tff = tff Or TextFormatFlags.Right
End If

Case DataGridViewContentAlignment.BottomLeft
tff = TextFormatFlags.Bottom

If rightToLeft Then
tff = tff Or TextFormatFlags.Right
Else
tff = tff Or TextFormatFlags.Left
End If

Case DataGridViewContentAlignment.BottomCenter
tff = TextFormatFlags.Bottom Or TextFormatFlags.HorizontalCenter
Case DataGridViewContentAlignment.BottomRight
tff = TextFormatFlags.Bottom

If rightToLeft Then
tff = tff Or TextFormatFlags.Left
Else
tff = tff Or TextFormatFlags.Right
End If

Case Else
tff = TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter
End Select

If wrapMode = DataGridViewTriState.[False] Then
tff = tff Or TextFormatFlags.SingleLine
Else
tff = tff Or TextFormatFlags.WordBreak
End If

tff = tff Or TextFormatFlags.NoPrefix
tff = tff Or TextFormatFlags.PreserveGraphicsClipping

If rightToLeft Then
tff = tff Or TextFormatFlags.RightToLeft
End If

Return tff
End Function

End Class
End Namespace
28 changes: 28 additions & 0 deletions NHLGames/Controls/NHLStats/TeamColumn.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Imports System.Windows.Forms

Namespace NHLStats
Public Class TeamColumn
Inherits DataGridViewColumn

Public Sub New()
MyBase.New(New TeamCell())
End Sub

Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As DataGridViewCell)

' Ensure that the cell used for the template is a TeamCell.
If (value IsNot Nothing) AndAlso
Not value.GetType().IsAssignableFrom(GetType(TeamCell)) Then
Throw New InvalidCastException("Must be a CalendarCell")
End If
MyBase.CellTemplate = value

End Set
End Property

End Class
End Namespace
Loading