MyIconButton.xaml.vb 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. Public Class MyIconButton
  2. '声明
  3. Public Event Click(sender As Object, e As EventArgs) '自定义事件
  4. '自定义属性
  5. Public Uuid As Integer = GetUuid()
  6. Public Property Logo As String
  7. Get
  8. Return Path.Data.ToString
  9. End Get
  10. Set(value As String)
  11. Path.Data = (New GeometryConverter).ConvertFromString(value)
  12. End Set
  13. End Property
  14. Private _LogoScale As Double = 1
  15. Public Property LogoScale() As Double
  16. Get
  17. Return _LogoScale
  18. End Get
  19. Set(value As Double)
  20. _LogoScale = value
  21. If Not IsNothing(Path) Then Path.RenderTransform = New ScaleTransform With {.ScaleX = LogoScale, .ScaleY = LogoScale}
  22. End Set
  23. End Property
  24. Public Enum Themes
  25. Color
  26. White
  27. Black
  28. Red
  29. Custom
  30. End Enum
  31. Public Property Theme As Themes = Themes.Color
  32. Private _Foreground As New SolidColorBrush(Color.FromRgb(128, 128, 128))
  33. Public Property Foreground As SolidColorBrush
  34. Get
  35. Return _Foreground
  36. End Get
  37. Set(value As SolidColorBrush)
  38. _Foreground = value
  39. AniControlEnabled += 1
  40. RefreshAnim()
  41. AniControlEnabled -= 1
  42. End Set
  43. End Property
  44. '触发点击事件
  45. Private Sub Button_MouseUp(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonUp
  46. If Not IsMouseDown Then Return
  47. Log("[Control] 按下图标按钮" & If(String.IsNullOrEmpty(Name), "", ":" & Name))
  48. RaiseEvent Click(sender, e)
  49. e.Handled = True
  50. Button_MouseUp()
  51. ModEvent.TryStartEvent(EventType, EventData)
  52. End Sub
  53. Public Property EventType As String
  54. Get
  55. Return GetValue(EventTypeProperty)
  56. End Get
  57. Set(value As String)
  58. SetValue(EventTypeProperty, value)
  59. End Set
  60. End Property
  61. Public Shared ReadOnly EventTypeProperty As DependencyProperty = DependencyProperty.Register("EventType", GetType(String), GetType(MyIconButton), New PropertyMetadata(Nothing))
  62. Public Property EventData As String
  63. Get
  64. Return GetValue(EventDataProperty)
  65. End Get
  66. Set(value As String)
  67. SetValue(EventDataProperty, value)
  68. End Set
  69. End Property
  70. Public Shared ReadOnly EventDataProperty As DependencyProperty = DependencyProperty.Register("EventData", GetType(String), GetType(MyIconButton), New PropertyMetadata(Nothing))
  71. '鼠标点击判定(务必放在点击事件之后,以使得 Button_MouseUp 先于 Button_MouseLeave 执行)
  72. Private IsMouseDown As Boolean = False
  73. Private Sub Button_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonDown
  74. IsMouseDown = True
  75. Focus()
  76. '指向
  77. AniStart(AaScaleTransform(PanBack, 0.8 - CType(PanBack.RenderTransform, ScaleTransform).ScaleX, 400,, New AniEaseOutFluent(AniEasePower.Strong)), "MyIconButton Scale " & Uuid)
  78. End Sub
  79. Private Sub Button_MouseUp() Handles Me.MouseLeftButtonUp
  80. If IsMouseDown Then
  81. IsMouseDown = False
  82. AniStart({
  83. AaScaleTransform(PanBack, 1.05 - CType(PanBack.RenderTransform, ScaleTransform).ScaleX, 250,, New AniEaseOutBack(AniEasePower.Weak)),
  84. AaScaleTransform(PanBack, -0.05, 250,, New AniEaseOutFluent(AniEasePower.Strong))
  85. }, "MyIconButton Scale " & Uuid)
  86. End If
  87. RefreshAnim() '直接刷新颜色以判断是否已触发 MouseLeave
  88. End Sub
  89. Private Sub Button_MouseLeave() Handles Me.MouseLeave
  90. IsMouseDown = False
  91. AniStart({
  92. AaScaleTransform(PanBack, 1 - CType(PanBack.RenderTransform, ScaleTransform).ScaleX, 250,, New AniEaseOutFluent)
  93. }, "MyIconButton Scale " & Uuid)
  94. RefreshAnim() '直接刷新颜色以判断是否已触发 MouseLeave
  95. End Sub
  96. '自定义事件
  97. '务必放在 IsMouseDown 更新之后
  98. Private Const AnimationColorIn As Integer = 120
  99. Private Const AnimationColorOut As Integer = 150
  100. Public Sub RefreshAnim() Handles Me.MouseEnter, Me.MouseLeave, Me.Loaded
  101. Try
  102. If IsLoaded AndAlso AniControlEnabled = 0 Then '防止默认属性变更触发动画
  103. If PanBack.Background Is Nothing Then PanBack.Background = New MyColor(0, 255, 255, 255)
  104. If Path.Fill Is Nothing AndAlso Theme = Themes.Black Then Path.Fill = New MyColor(140, 0, 0, 0)
  105. If IsMouseOver Then
  106. '指向
  107. Dim AnimList As New List(Of AniData)
  108. Select Case Theme
  109. Case Themes.Color
  110. AnimList.Add(AaColor(Path, Shape.FillProperty, "ColorBrush2", AnimationColorIn))
  111. Case Themes.White
  112. AnimList.Add(AaColor(PanBack, BackgroundProperty, New MyColor(50, 255, 255, 255) - PanBack.Background, AnimationColorIn))
  113. Case Themes.Red
  114. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(255, 76, 76) - Path.Fill, AnimationColorIn))
  115. Case Themes.Black
  116. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(230, 0, 0, 0) - Path.Fill, AnimationColorIn))
  117. Case Themes.Custom
  118. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(255, Foreground) - Path.Fill, AnimationColorIn))
  119. End Select
  120. AniStart(AnimList, "MyIconButton Color " & Uuid)
  121. Else
  122. '普通
  123. Dim AnimList As New List(Of AniData)
  124. Select Case Theme
  125. Case Themes.Color
  126. AnimList.Add(AaColor(Path, Shape.FillProperty, "ColorBrush4", AnimationColorOut))
  127. PanBack.Background = New MyColor(0, 255, 255, 255)
  128. Case Themes.White
  129. AnimList.Add(AaColor(Path, Shape.FillProperty, "ColorBrush8", AnimationColorOut))
  130. AnimList.Add(AaColor(PanBack, BackgroundProperty, New MyColor(0, 255, 255, 255) - PanBack.Background, AnimationColorOut))
  131. Case Themes.Red
  132. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(160, 255, 76, 76) - Path.Fill, AnimationColorOut))
  133. PanBack.Background = New MyColor(0, 255, 255, 255)
  134. Case Themes.Black
  135. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(160, 0, 0, 0) - Path.Fill, AnimationColorOut))
  136. PanBack.Background = New MyColor(0, 255, 255, 255)
  137. Case Themes.Custom
  138. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(160, Foreground) - Path.Fill, AnimationColorOut))
  139. PanBack.Background = New MyColor(0, 255, 255, 255)
  140. End Select
  141. AniStart(AnimList, "MyIconButton Color " & Uuid)
  142. End If
  143. Else
  144. AniStop("MyIconButton Color " & Uuid)
  145. Select Case Theme
  146. Case Themes.Color
  147. Path.SetResourceReference(Shape.FillProperty, "ColorBrush5")
  148. Case Themes.White
  149. Path.SetResourceReference(Shape.FillProperty, "ColorBrush8")
  150. Case Themes.Red
  151. Path.Fill = New MyColor(160, 255, 76, 76)
  152. Case Themes.Black
  153. Path.Fill = New MyColor(160, 0, 0, 0)
  154. Case Themes.Custom
  155. Path.Fill = New MyColor(160, Foreground)
  156. End Select
  157. PanBack.Background = New MyColor(0, 255, 255, 255)
  158. End If
  159. Catch ex As Exception
  160. Log(ex, "刷新图标按钮动画状态出错")
  161. End Try
  162. End Sub
  163. End Class
  164. Partial Public Module ModAnimation
  165. Public Sub AniDispose(Control As MyIconButton, RemoveFromChildren As Boolean, Optional CallBack As ParameterizedThreadStart = Nothing)
  166. If Not Control.IsHitTestVisible Then Exit Sub
  167. Control.IsHitTestVisible = False
  168. AniStart({
  169. AaScaleTransform(Control, -1.5, 200,, New AniEaseInFluent),
  170. AaCode(Sub()
  171. If RemoveFromChildren Then
  172. CType(Control.Parent, Object).Children.Remove(Control)
  173. Else
  174. Control.Visibility = Visibility.Collapsed
  175. End If
  176. If CallBack IsNot Nothing Then CallBack(Control)
  177. End Sub,, True)
  178. }, "MyIconButton Dispose " & Control.Uuid)
  179. End Sub
  180. End Module