Advertisement
RJPcat

DieControl.vb

Nov 20th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.89 KB | None | 0 0
  1. Public Class DieControl
  2.  
  3.     Public Property Sides As Integer
  4.         Get
  5.             Return shhSides
  6.         End Get
  7.         Set(value As Integer)
  8.             If value <> shhSides Then
  9.                 shhSides = value
  10.                 Invalidate()
  11.             End If
  12.         End Set
  13.     End Property
  14.     Private shhSides As Integer = 6
  15.  
  16.     Public Property Value As Integer
  17.         Get
  18.             Return shhValue
  19.         End Get
  20.         Set(value As Integer)
  21.             If value <> shhValue Then
  22.                 shhValue = value
  23.                 Invalidate()      ' todo: invalidate only the number area
  24.             End If
  25.         End Set
  26.     End Property
  27.     Private shhValue As Integer = 6
  28.  
  29.     Public Sub Clear()
  30.         If Value <> 0 Then
  31.             Value = 0
  32.             Invalidate()
  33.         End If
  34.     End Sub
  35.  
  36.     Public Function Roll() As Integer
  37.         Value = rnd.Next(1, Sides + 1)
  38.         Invalidate()  ' force it to flash even if the value didn't change
  39.  
  40.         Return Value
  41.     End Function
  42.  
  43.     Private rnd As Random = New Random
  44.  
  45.     Private Sub DieControl_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  46.         If DesignMode Then
  47.             Value = 6
  48.         Else
  49.             Value = 0
  50.         End If
  51.     End Sub
  52.  
  53.     Private Sub DieControl_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
  54.         Dim ShortestSide As Integer = Math.Min(e.ClipRectangle.Width, e.ClipRectangle.Height)
  55.         Dim BoundingRectangle As Rectangle = New Rectangle((e.ClipRectangle.Width - ShortestSide) \ 2, (e.ClipRectangle.Height - ShortestSide) \ 2, ShortestSide, ShortestSide)
  56.         Dim NumberRectangle As Rectangle
  57.  
  58.         Dim gs As Drawing2D.GraphicsState = e.Graphics.Save
  59.         e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
  60.         Select Case Sides
  61.             Case 4
  62.             Case 6
  63.                 NumberRectangle = Paint6Sides(e.Graphics, BoundingRectangle)
  64.             Case 8
  65.             Case 10
  66.             Case 12
  67.             Case 20
  68.         End Select
  69.  
  70.         If Value <> 0 Then
  71.             e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
  72.             Dim sf As StringFormat = New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
  73.             e.Graphics.DrawString(Value, Me.Font, Brushes.Black, NumberRectangle, sf)
  74.         End If
  75.  
  76.         e.Graphics.Restore(gs)
  77.     End Sub
  78.  
  79.     Private Function Paint6Sides(g As Graphics, BoundingRectangle As Rectangle) As Rectangle
  80.         ' top face
  81.         g.DrawLine(Pens.Black, BoundingRectangle.Width \ 2, 0, BoundingRectangle.Right, BoundingRectangle.Height \ 3)
  82.         g.DrawLine(Pens.Black, BoundingRectangle.Right, BoundingRectangle.Height \ 3, BoundingRectangle.Width \ 2, 2 * BoundingRectangle.Height \ 3)
  83.         g.DrawLine(Pens.Black, BoundingRectangle.Width \ 2, 2 * BoundingRectangle.Height \ 3, 0, BoundingRectangle.Height \ 3)
  84.         g.DrawLine(Pens.Black, 0, BoundingRectangle.Height \ 3, BoundingRectangle.Width \ 2, 0)
  85.  
  86.         ' left face
  87.         g.DrawLine(Pens.Black, 0, BoundingRectangle.Height \ 3, 0, 2 * BoundingRectangle.Height \ 3)
  88.         g.DrawLine(Pens.Black, 0, 2 * BoundingRectangle.Height \ 3, BoundingRectangle.Width \ 2, BoundingRectangle.Bottom - 1)
  89.  
  90.         ' right face
  91.         g.DrawLine(Pens.Black, BoundingRectangle.Width \ 2, BoundingRectangle.Bottom - 1, BoundingRectangle.Right, 2 * BoundingRectangle.Height \ 3)
  92.         g.DrawLine(Pens.Black, BoundingRectangle.Right - 1, 2 * BoundingRectangle.Height \ 3, BoundingRectangle.Right - 1, BoundingRectangle.Height \ 3)
  93.  
  94.         ' front edge
  95.         g.DrawLine(Pens.Black, BoundingRectangle.Width \ 2, 2 * BoundingRectangle.Height \ 3, BoundingRectangle.Width \ 2, BoundingRectangle.Bottom - 1)
  96.  
  97.         Return New Rectangle(0, 0, BoundingRectangle.Width, 2 * BoundingRectangle.Height \ 3)
  98.     End Function
  99. End Class
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement