/Source Code/ExperimentalProjection/GDI/GdiProjector.vb
Visual Basic | 209 lines | 125 code | 64 blank | 20 comment | 1 complexity | f5c5fbb506d7381976e14fdb50df2dca MD5 | raw file
- Imports System.Drawing
- Imports System.Drawing.Imaging
-
- Public NotInheritable Class GdiProjector
- Inherits BaseProjector
-
- Private FControl As Control = Nothing
-
- Private Shared FMatrices As ImageAttributes() = PrecomputeMatrices()
-
- Private Shared Function PrecomputeMatrices() As ImageAttributes()
-
- Dim Result As New List(Of ImageAttributes)
-
- For Index As Integer = 0 To 255
-
- ' Create the next matrix
- Dim Values As Single()() = {New Single() {1, 0, 0, 0, 0}, _
- New Single() {0, 1, 0, 0, 0}, _
- New Single() {0, 0, 1, 0, 0}, _
- New Single() {0, 0, 0, Index / 255, 0}, _
- New Single() {0, 0, 0, 0, 1}}
- Dim Matrix As New ColorMatrix(Values)
-
- ' Add to the list of available alpha-blending matrices
- Dim Attributes As New ImageAttributes
- Attributes.SetColorMatrix(Matrix)
- Result.Add(Attributes)
-
- Next
-
- Return Result.ToArray
-
- End Function
-
- Friend Sub New(ByVal control As Control)
- FControl = control
- End Sub
-
- Private FCanvas As Bitmap = Nothing
- Private FCanvasGraphics As Graphics = Nothing
-
- Public Overrides Sub RenderFrame()
-
- ' Detect when the control has been resized
- If FCanvasGraphics IsNot Nothing AndAlso (FCanvas.Width <> FControl.Width OrElse FCanvas.Height <> FControl.Height) Then
- HandleControlResized()
- End If
-
- ' If we haven't already, create a context in which to draw on
- If FCanvas Is Nothing Then FCanvas = New Bitmap(FControl.Width, FControl.Height, PixelFormat.Format32bppPArgb)
- If FCanvasGraphics Is Nothing Then FCanvasGraphics = Graphics.FromImage(FCanvas)
-
- ' Render each of the projectlets onto a buffered canvas
- ' TODO: Resolve
- FCanvasGraphics.Clear(Color.DarkBlue)
- For Each Projectlet As GdiProjectlet In _Projectlets
-
- Dim ActualOpacity As Double = Projectlet.Opacity
- Dim Visible As Boolean = Projectlet.VisibleWhenNotFading
-
- ' Animate if an animation is defined
- Dim Animation As GdiProjectletAnimation = Projectlet.PeekNextAnimation
- If Animation IsNot Nothing Then
- If Animation.StartTick = 0 Then Animation.StartTick = Now.Ticks
- Visible = True
-
- Select Case Animation.AnimationType
-
- Case FadeEffect.FadeIn
- ActualOpacity = Animation.PercentageThrough * Projectlet.Opacity
-
- Case FadeEffect.FadeOut
- ActualOpacity = Projectlet.Opacity - (Animation.PercentageThrough * Projectlet.Opacity)
-
- End Select
-
- If Animation.IsFinished Then Projectlet.RemoveNextAnimation()
-
- End If
-
- ' Draw the projectlet image
- If Visible Then
- Dim X As Integer = FControl.Width * Projectlet.Area.Left
- Dim Y As Integer = FControl.Height * Projectlet.Area.Top
- Dim Width As Integer = FControl.Width * Projectlet.Area.Width
- Dim Height As Integer = FControl.Height * Projectlet.Area.Height
- FCanvasGraphics.DrawImage(Projectlet.Bitmap, _
- New Rectangle(X, Y, Width, Height), _
- 0, 0, Projectlet.Bitmap.Width, Projectlet.Bitmap.Height, _
- GraphicsUnit.Pixel, _
- FMatrices(CInt(ActualOpacity)))
- End If
-
- Next
-
- ' Draw the buffer image onto the assigned control
- FControl.CreateGraphics.DrawImageUnscaled(FCanvas, 0, 0)
-
- End Sub
-
- Public Overloads Overrides Sub ShowProjectlet(ByVal name As String)
-
- Dim Projectlet As GdiProjectlet = GetProjectlet(name)
- Projectlet.ClearAnimations()
- Projectlet.VisibleWhenNotFading = True
-
- End Sub
-
- Public Overloads Overrides Sub ShowProjectlet(ByVal name As String, _
- ByVal animationMethod As FadeEffect, _
- ByVal duration As System.TimeSpan)
-
- Dim Projectlet As GdiProjectlet = GetProjectlet(name)
- Projectlet.AddAnimation(New GdiProjectletAnimation(animationMethod, duration.Ticks))
- Projectlet.VisibleWhenNotFading = True
-
- End Sub
-
- Public Overloads Overrides Sub HideProjectlet(ByVal name As String)
-
- Dim Projectlet As GdiProjectlet = GetProjectlet(name)
- Projectlet.ClearAnimations()
- Projectlet.VisibleWhenNotFading = False
-
- End Sub
-
- Public Overloads Overrides Sub HideProjectlet(ByVal name As String, _
- ByVal animationMethod As FadeEffect, _
- ByVal duration As System.TimeSpan)
-
- Dim Projectlet As GdiProjectlet = GetProjectlet(name)
- Projectlet.AddAnimation(New GdiProjectletAnimation(animationMethod, duration.Ticks))
- Projectlet.VisibleWhenNotFading = False
-
- End Sub
-
- #Region " Methods for Creating and Updating Projectlets "
-
- Friend Overrides Function CreateProjectlet(ByVal details As ProjectletDetails) As BaseProjectlet
-
- ' Create the projectlet
- Dim Result As New GdiProjectlet(details.Name, details)
- UpdateProjectlet(Result)
- Return Result
-
- End Function
-
- Friend Overloads Overrides Function CreateProjectlet(ByVal name As String, _
- ByVal area As AgnosticRectangle, _
- ByVal bitmapImage As Object) As BaseProjectlet
-
- Dim Result As New GdiProjectlet(name)
- Result.Bitmap = bitmapImage
- ' TODO: Need to fix this - are you going to use integers or doubles? Also, need to include width and height
- Result.Area = New RectangleF(area.Left, area.Top, area.Width, area.Height)
- Return Result
-
- End Function
-
- Private Sub UpdateProjectlet(ByVal projectlet As GdiProjectlet)
-
- ' Are there details for us to be able to update the projectlet?
- If projectlet.Details IsNot Nothing Then
-
- ' Extract the details
- Dim Details As ProjectletDetails = projectlet.Details
-
- ' Define the area
- projectlet.Area = New RectangleF(Details.Area.Left, Details.Area.Top, Details.Area.Width, Details.Area.Height)
-
- ' Determine the actual font size to use
- Dim ActualFontSize As Double = Details.FontSizeRatio * FControl.Width
-
- ' Determine the actual font style to use
- Dim ActualFontStyle As FontStyle = FontStyle.Regular
- If (Details.FontStyle And AgnosticFontStyle.Bold) = AgnosticFontStyle.Bold Then ActualFontStyle += FontStyle.Bold
- If (Details.FontStyle And AgnosticFontStyle.Italic) = AgnosticFontStyle.Italic Then ActualFontStyle += FontStyle.Italic
- If (Details.FontStyle And AgnosticFontStyle.Underline) = AgnosticFontStyle.Underline Then ActualFontStyle += FontStyle.Underline
-
- ' Create the bitmap
- Dim ControlSize As New Size(FControl.Width, FControl.Height)
- Dim Font As New Font(Details.FontName, CInt(ActualFontSize), ActualFontStyle)
- projectlet.Bitmap = CreateTextBitmap(ControlSize, Font, Details.Effect, Color.Blue, Color.White, Details.Alignment, Details.Text)
-
- End If
-
- End Sub
-
- #End Region
-
- Private Sub HandleControlResized()
-
- ' Dispose the canvas
- FCanvas.Dispose()
- FCanvas = Nothing
-
- ' Dispose the canvas graphics
- FCanvasGraphics.Dispose()
- FCanvasGraphics = Nothing
-
- ' Recreate all the projectlet bitmaps
- For Each Projectlet As GdiProjectlet In _Projectlets
- UpdateProjectlet(Projectlet)
- Next
-
- End Sub
-
- End Class