Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class DieControl
- Public Property Sides As Integer
- Get
- Return shhSides
- End Get
- Set(value As Integer)
- If value <> shhSides Then
- shhSides = value
- Invalidate()
- End If
- End Set
- End Property
- Private shhSides As Integer = 6
- Public Property Value As Integer
- Get
- Return shhValue
- End Get
- Set(value As Integer)
- If value <> shhValue Then
- shhValue = value
- Invalidate() ' todo: invalidate only the number area
- End If
- End Set
- End Property
- Private shhValue As Integer = 6
- Public Sub Clear()
- If Value <> 0 Then
- Value = 0
- Invalidate()
- End If
- End Sub
- Public Function Roll() As Integer
- Value = rnd.Next(1, Sides + 1)
- Invalidate() ' force it to flash even if the value didn't change
- Return Value
- End Function
- Private rnd As Random = New Random
- Private Sub DieControl_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- If DesignMode Then
- Value = 6
- Else
- Value = 0
- End If
- End Sub
- Private Sub DieControl_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
- Dim ShortestSide As Integer = Math.Min(e.ClipRectangle.Width, e.ClipRectangle.Height)
- Dim BoundingRectangle As Rectangle = New Rectangle((e.ClipRectangle.Width - ShortestSide) \ 2, (e.ClipRectangle.Height - ShortestSide) \ 2, ShortestSide, ShortestSide)
- Dim NumberRectangle As Rectangle
- Dim gs As Drawing2D.GraphicsState = e.Graphics.Save
- e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
- Select Case Sides
- Case 4
- Case 6
- NumberRectangle = Paint6Sides(e.Graphics, BoundingRectangle)
- Case 8
- Case 10
- Case 12
- Case 20
- End Select
- If Value <> 0 Then
- e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
- Dim sf As StringFormat = New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
- e.Graphics.DrawString(Value, Me.Font, Brushes.Black, NumberRectangle, sf)
- End If
- e.Graphics.Restore(gs)
- End Sub
- Private Function Paint6Sides(g As Graphics, BoundingRectangle As Rectangle) As Rectangle
- ' top face
- g.DrawLine(Pens.Black, BoundingRectangle.Width \ 2, 0, BoundingRectangle.Right, BoundingRectangle.Height \ 3)
- g.DrawLine(Pens.Black, BoundingRectangle.Right, BoundingRectangle.Height \ 3, BoundingRectangle.Width \ 2, 2 * BoundingRectangle.Height \ 3)
- g.DrawLine(Pens.Black, BoundingRectangle.Width \ 2, 2 * BoundingRectangle.Height \ 3, 0, BoundingRectangle.Height \ 3)
- g.DrawLine(Pens.Black, 0, BoundingRectangle.Height \ 3, BoundingRectangle.Width \ 2, 0)
- ' left face
- g.DrawLine(Pens.Black, 0, BoundingRectangle.Height \ 3, 0, 2 * BoundingRectangle.Height \ 3)
- g.DrawLine(Pens.Black, 0, 2 * BoundingRectangle.Height \ 3, BoundingRectangle.Width \ 2, BoundingRectangle.Bottom - 1)
- ' right face
- g.DrawLine(Pens.Black, BoundingRectangle.Width \ 2, BoundingRectangle.Bottom - 1, BoundingRectangle.Right, 2 * BoundingRectangle.Height \ 3)
- g.DrawLine(Pens.Black, BoundingRectangle.Right - 1, 2 * BoundingRectangle.Height \ 3, BoundingRectangle.Right - 1, BoundingRectangle.Height \ 3)
- ' front edge
- g.DrawLine(Pens.Black, BoundingRectangle.Width \ 2, 2 * BoundingRectangle.Height \ 3, BoundingRectangle.Width \ 2, BoundingRectangle.Bottom - 1)
- Return New Rectangle(0, 0, BoundingRectangle.Width, 2 * BoundingRectangle.Height \ 3)
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement