PageRenderTime 64ms CodeModel.GetById 12ms app.highlight 46ms RepoModel.GetById 1ms app.codeStats 0ms

/Source Code/Projection/Projectlet.vb

#
Visual Basic | 314 lines | 247 code | 60 blank | 7 comment | 0 complexity | fb08eb47b7175c59669d9f3fc724ed04 MD5 | raw file
  1Imports System.Drawing.Drawing2D
  2Imports PowerSong.SongDatabase.Style
  3Imports PowerSong.SongDatabase.ProjectletStyle
  4Imports PowerSong.SongDatabase.Style.ETextAlignment
  5Imports PowerSong.SongDatabase.Items
  6
  7Public Class Projectlet
  8
  9    Public Function PopulateTags(ByVal item As BaseItem, _
 10                                 ByVal activeVerseIndex As Integer) As String
 11
 12        If activeVerseIndex < 0 OrElse activeVerseIndex >= item.Parts.Count Then Return String.Empty
 13
 14        ' Compose an authors string
 15        Dim Authors As String = String.Empty
 16        If TypeOf item Is SongItem Then
 17            For Each Author As String In DirectCast(item, SongItem).Authors
 18                If Authors = "" Then Authors = Author Else Authors += ", " + Author
 19            Next
 20        End If
 21
 22        ' Replace tags
 23        Dim Result As String = FContentFormat
 24        Result = Result.Replace(DATE_TAG, Now.ToShortDateString)
 25        Result = Result.Replace(TIME_TAG, Now.ToShortTimeString)
 26        Result = Result.Replace(TITLE_TAG, item.Title)
 27        Result = Result.Replace(VERSE_TAG, item.Parts(activeVerseIndex))
 28        Result = Result.Replace(SIMPLE_COPYRIGHT_TAG, item.CopyrightLine)
 29        Result = Result.Replace(AUTHORS_TAG, Authors)
 30        Return Result
 31
 32    End Function
 33
 34    Private FCategory As String = String.Empty
 35
 36    Public ReadOnly Property Category() As String
 37        Get
 38            Return FCategory
 39        End Get
 40    End Property
 41
 42    Private FArea As New RectangleF(0.05, 0.05, 0.9, 0.9)
 43
 44    Public Property Area() As RectangleF
 45        Get
 46            Return FArea
 47        End Get
 48        Set(ByVal value As RectangleF)
 49            FArea = value
 50            FRenderedVerses = New List(Of Integer)
 51            FCurrentVerse = -1
 52        End Set
 53    End Property
 54
 55    Private FPreviousText As String = String.Empty
 56
 57    Friend Property PreviousText() As String
 58        Get
 59            Return FPreviousText
 60        End Get
 61        Set(ByVal value As String)
 62            FPreviousText = value
 63        End Set
 64    End Property
 65
 66    Private FText As String = String.Empty
 67
 68    Public Property Text() As String
 69        Get
 70            Return FText
 71        End Get
 72        Set(ByVal value As String)
 73            FPreviousText = FText
 74            FText = value
 75            FRenderedVerses = New List(Of Integer)
 76            FCurrentVerse = -1
 77        End Set
 78    End Property
 79
 80    Private FFont As Font = SystemFonts.DefaultFont
 81
 82    Public Property Font() As Font
 83        Get
 84            Return FFont
 85        End Get
 86        Set(ByVal value As Font)
 87            FFont = value
 88            FProjectingFont = value
 89            FRenderedVerses = New List(Of Integer)
 90            FCurrentVerse = -1
 91        End Set
 92    End Property
 93
 94    Private FProjectingFont As Font = SystemFonts.DefaultFont
 95
 96    Friend Property ProjectingFont() As Font
 97        Get
 98            Return FProjectingFont
 99        End Get
100        Set(ByVal value As Font)
101            FProjectingFont = value
102        End Set
103    End Property
104
105    Private FFontFormat As New StringFormat
106
107    Public Property FontFormat() As StringFormat
108        Get
109            Return FFontFormat
110        End Get
111        Set(ByVal value As StringFormat)
112            FFontFormat = value
113            FRenderedVerses = New List(Of Integer)
114            FCurrentVerse = -1
115        End Set
116    End Property
117
118    Private FFontColour As Color = Color.White
119
120    Public Property FontColour() As Color
121        Get
122            Return FFontColour
123        End Get
124        Set(ByVal value As Color)
125            FFontColour = value
126            FRenderedVerses = New List(Of Integer)
127            FCurrentVerse = -1
128        End Set
129    End Property
130
131    Private FFontEffect As ETextEffect = ETextEffect.Shadow
132
133    Public Property FontEffect() As ETextEffect
134        Get
135            Return FFontEffect
136        End Get
137        Set(ByVal value As ETextEffect)
138            FFontEffect = value
139            FRenderedVerses = New List(Of Integer)
140            FCurrentVerse = -1
141        End Set
142    End Property
143
144    Private FFontEffectColour As Color = Color.Black
145
146    Public Property FontEffectColour() As Color
147        Get
148            Return FFontEffectColour
149        End Get
150        Set(ByVal value As Color)
151            FFontEffectColour = value
152            FRenderedVerses = New List(Of Integer)
153            FCurrentVerse = -1
154        End Set
155    End Property
156
157    Private FDisplayOption As EDisplayOption = EDisplayOption.Always
158
159    Public Property DisplayOption() As EDisplayOption
160        Get
161            Return FDisplayOption
162        End Get
163        Set(ByVal value As EDisplayOption)
164            FDisplayOption = value
165            FRenderedVerses = New List(Of Integer)
166            FCurrentVerse = -1
167        End Set
168    End Property
169
170    Private FContentFormat As String = String.Empty
171
172    Public Property ContentFormat() As String
173        Get
174            Return FContentFormat
175        End Get
176        Set(ByVal value As String)
177            FContentFormat = value
178        End Set
179    End Property
180
181    Private FCurrentVerse As Integer = -1
182    Private FRenderedVerses As New List(Of Integer)
183
184    Public Sub New(ByVal category As String, ByVal contentFormat As String)
185
186        FCategory = category
187        FContentFormat = contentFormat
188        FFontFormat.Alignment = StringAlignment.Center
189        FFontFormat.LineAlignment = StringAlignment.Center
190
191    End Sub
192
193    Public Sub SetStyle(ByVal style As SongDatabase.ProjectletStyle)
194
195        Area = style.Area
196        Font = New Font(style.FontName, style.FontSize, style.FontStyle, GraphicsUnit.Pixel)
197        FontColour = style.FontColour
198        FontEffect = style.FontEffect
199        FontEffectColour = style.FontEffectColour
200        DisplayOption = style.DisplayOption
201        FontFormat = CreateFontFormat(style.Alignment)
202        ContentFormat = style.Content
203
204    End Sub
205
206    Public Shared Function CreateFontFormat(ByVal alignment As ETextAlignment) As StringFormat
207
208        Dim Result As New StringFormat
209
210        ' Set horizontal alignment
211        Select Case alignment
212            Case TopLeft, MiddleLeft, BottomLeft : Result.Alignment = StringAlignment.Near
213            Case TopMiddle, MiddleMiddle, BottomMiddle : Result.Alignment = StringAlignment.Center
214            Case TopRight, MiddleRight, BottomRight : Result.Alignment = StringAlignment.Far
215        End Select
216
217        ' Set vertical alignment
218        Select Case alignment
219            Case TopLeft, TopMiddle, TopRight : Result.LineAlignment = StringAlignment.Near
220            Case MiddleLeft, MiddleMiddle, MiddleRight : Result.LineAlignment = StringAlignment.Center
221            Case BottomLeft, BottomMiddle, BottomRight : Result.LineAlignment = StringAlignment.Far
222        End Select
223
224        Return Result
225
226    End Function
227
228    Public Sub DrawText(ByVal graphics As Graphics, _
229                        ByVal size As Size, _
230                        ByVal opacity As Byte, _
231                        ByVal text As String, _
232                        ByVal verseIndex As Integer, _
233                        ByVal verseCount As Integer)
234
235        If verseIndex <> FCurrentVerse Then
236            If Not FRenderedVerses.Contains(FCurrentVerse) Then FRenderedVerses.Add(FCurrentVerse)
237            FCurrentVerse = verseIndex
238        End If
239
240        ' Determine whether or not to display the text based on the verse or iteration
241        Dim DoDisplay As Boolean = True
242        Select Case FDisplayOption
243            Case EDisplayOption.FirstVerse : DoDisplay = verseIndex = 0
244            Case EDisplayOption.SecondVerse : DoDisplay = verseIndex = 1
245            Case EDisplayOption.LastVerse : DoDisplay = verseIndex = verseCount - 1
246            Case EDisplayOption.FirstVerseOnce : DoDisplay = verseIndex = 0 AndAlso Not FRenderedVerses.Contains(verseIndex)
247            Case EDisplayOption.SecondVerseOnce : DoDisplay = verseIndex = 1 AndAlso Not FRenderedVerses.Contains(verseIndex)
248            Case EDisplayOption.LastVerseOnce : DoDisplay = verseIndex = verseCount - 1 AndAlso Not FRenderedVerses.Contains(verseIndex)
249            Case EDisplayOption.Never : DoDisplay = False
250        End Select
251
252        ' Don't display the verse under these conditions
253        If Not DoDisplay Then Exit Sub
254        If text = "" Then Exit Sub
255
256        ' Determine the projectlet screen area
257        Dim ScreenArea As New Rectangle(FArea.Left * size.Width, _
258                                        FArea.Top * size.Height, _
259                                        FArea.Width * size.Width, _
260                                        FArea.Height * size.Height)
261
262        Dim Font As Font = FProjectingFont
263
264        Dim FontCol As Color = Color.FromArgb(opacity, FFontColour.R, FFontColour.G, FFontColour.B)
265        Dim EffectCol As Color = Color.FromArgb(opacity, FFontEffectColour.R, FFontEffectColour.G, FFontEffectColour.B)
266
267        Select Case FFontEffect
268
269            Case ETextEffect.Shadow
270                Dim DeviantSize As Integer = ScreenArea.Width / 200
271                ScreenArea.X += DeviantSize
272                ScreenArea.Y += DeviantSize
273                graphics.DrawString(text, Font, New SolidBrush(EffectCol), ScreenArea, FFontFormat)
274                ScreenArea.X -= DeviantSize
275                ScreenArea.Y -= DeviantSize
276                graphics.DrawString(text, Font, New SolidBrush(FontCol), ScreenArea, FFontFormat)
277
278            Case ETextEffect.InnerOutline
279                Dim OutlineWidth As Integer = ScreenArea.Width / 300
280                Dim Path As New GraphicsPath
281                Path.AddString(text, Font.FontFamily, Font.Style, Font.Size, ScreenArea, FFontFormat)
282                graphics.FillPath(New SolidBrush(FontCol), Path)
283                graphics.DrawPath(New Pen(EffectCol, OutlineWidth), Path)
284
285            Case ETextEffect.Outline
286                Dim OutlineWidth As Integer = Math.Max(Math.Min(ScreenArea.Width / 400, 4), 1)
287                Dim Brush As New SolidBrush(EffectCol)
288                For X As Integer = -OutlineWidth To OutlineWidth
289                    For Y As Integer = -OutlineWidth To OutlineWidth
290                        If X <> 0 AndAlso Y <> 0 Then
291                            Dim newArea As New Rectangle(ScreenArea.X + X, ScreenArea.Y + Y, ScreenArea.Width, ScreenArea.Height)
292                            graphics.DrawString(text, Font, Brush, newArea, FFontFormat)
293                        End If
294                    Next
295                Next
296                graphics.DrawString(text, Font, New SolidBrush(FontCol), ScreenArea, FFontFormat)
297
298            Case ETextEffect.OutlineBox
299                Dim PenWidth As Integer = ScreenArea.Width / 150
300                graphics.DrawRectangle(New Pen(FFontEffectColour, PenWidth), ScreenArea)
301                graphics.DrawString(text, Font, New SolidBrush(FontCol), ScreenArea, FFontFormat)
302
303            Case ETextEffect.FilledBox
304                graphics.FillRectangle(New SolidBrush(FFontEffectColour), ScreenArea)
305                graphics.DrawString(text, Font, New SolidBrush(FontCol), ScreenArea, FFontFormat)
306
307            Case Else
308                graphics.DrawString(text, Font, New SolidBrush(FontCol), ScreenArea, FFontFormat)
309
310        End Select
311
312    End Sub
313
314End Class