MyIconButton.xaml.vb 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 Then
  105. Select Case Theme
  106. Case Themes.Red
  107. Path.Fill = New MyColor(160, 255, 76, 76)
  108. Case Themes.Black
  109. Path.Fill = New MyColor(160, 0, 0, 0)
  110. Case Themes.Custom
  111. Path.Fill = New MyColor(160, Foreground)
  112. End Select
  113. End If
  114. If IsMouseOver Then
  115. '指向
  116. Dim AnimList As New List(Of AniData)
  117. Select Case Theme
  118. Case Themes.Color
  119. AnimList.Add(AaColor(Path, Shape.FillProperty, "ColorBrush2", AnimationColorIn))
  120. Case Themes.White
  121. AnimList.Add(AaColor(PanBack, BackgroundProperty, New MyColor(50, 255, 255, 255) - PanBack.Background, AnimationColorIn))
  122. Case Themes.Red
  123. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(255, 76, 76) - Path.Fill, AnimationColorIn))
  124. Case Themes.Black
  125. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(230, 0, 0, 0) - Path.Fill, AnimationColorIn))
  126. Case Themes.Custom
  127. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(255, Foreground) - Path.Fill, AnimationColorIn))
  128. End Select
  129. AniStart(AnimList, "MyIconButton Color " & Uuid)
  130. Else
  131. '普通
  132. Dim AnimList As New List(Of AniData)
  133. Select Case Theme
  134. Case Themes.Color
  135. AnimList.Add(AaColor(Path, Shape.FillProperty, "ColorBrush4", AnimationColorOut))
  136. PanBack.Background = New MyColor(0, 255, 255, 255)
  137. Case Themes.White
  138. AnimList.Add(AaColor(Path, Shape.FillProperty, "ColorBrush8", AnimationColorOut))
  139. AnimList.Add(AaColor(PanBack, BackgroundProperty, New MyColor(0, 255, 255, 255) - PanBack.Background, AnimationColorOut))
  140. Case Themes.Red
  141. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(160, 255, 76, 76) - Path.Fill, AnimationColorOut))
  142. PanBack.Background = New MyColor(0, 255, 255, 255)
  143. Case Themes.Black
  144. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(160, 0, 0, 0) - Path.Fill, AnimationColorOut))
  145. PanBack.Background = New MyColor(0, 255, 255, 255)
  146. Case Themes.Custom
  147. AnimList.Add(AaColor(Path, Shape.FillProperty, New MyColor(160, Foreground) - Path.Fill, AnimationColorOut))
  148. PanBack.Background = New MyColor(0, 255, 255, 255)
  149. End Select
  150. AniStart(AnimList, "MyIconButton Color " & Uuid)
  151. End If
  152. Else
  153. AniStop("MyIconButton Color " & Uuid)
  154. Select Case Theme
  155. Case Themes.Color
  156. Path.SetResourceReference(Shape.FillProperty, "ColorBrush5")
  157. Case Themes.White
  158. Path.SetResourceReference(Shape.FillProperty, "ColorBrush8")
  159. Case Themes.Red
  160. Path.Fill = New MyColor(160, 255, 76, 76)
  161. Case Themes.Black
  162. Path.Fill = New MyColor(160, 0, 0, 0)
  163. Case Themes.Custom
  164. Path.Fill = New MyColor(160, Foreground)
  165. End Select
  166. PanBack.Background = New MyColor(0, 255, 255, 255)
  167. End If
  168. Catch ex As Exception
  169. Log(ex, "刷新图标按钮动画状态出错")
  170. End Try
  171. End Sub
  172. End Class
  173. Partial Public Module ModAnimation
  174. Public Sub AniDispose(Control As MyIconButton, RemoveFromChildren As Boolean, Optional CallBack As ParameterizedThreadStart = Nothing)
  175. If Not Control.IsHitTestVisible Then Return
  176. Control.IsHitTestVisible = False
  177. AniStart({
  178. AaScaleTransform(Control, -1.5, 200,, New AniEaseInFluent),
  179. AaCode(Sub()
  180. If RemoveFromChildren Then
  181. CType(Control.Parent, Object).Children.Remove(Control)
  182. Else
  183. Control.Visibility = Visibility.Collapsed
  184. End If
  185. If CallBack IsNot Nothing Then CallBack(Control)
  186. End Sub,, True)
  187. }, "MyIconButton Dispose " & Control.Uuid)
  188. End Sub
  189. End Module