PageRenderTime 31ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Source Code/Projection/Projectlet.vb

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