MyCard.vb 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. Public Class MyCard
  2. '控件
  3. Inherits Grid
  4. Private ReadOnly MainGrid As Grid
  5. Public ReadOnly Property MainChrome As MyDropShadow
  6. Private ReadOnly MainBorder As Border
  7. Public Property BorderChild As UIElement
  8. Get
  9. Return MainBorder.Child
  10. End Get
  11. Set(value As UIElement)
  12. MainBorder.Child = value
  13. End Set
  14. End Property
  15. Private _MainTextBlock As TextBlock
  16. Public Property MainTextBlock As TextBlock
  17. Get
  18. Init() '当父级触发 Loaded 时,本卡片可能尚未触发 Loaded(该事件从父级向子级调用),因此这会是 null。手动触发以确保控件已加载。
  19. Return _MainTextBlock
  20. End Get
  21. Set(value As TextBlock)
  22. _MainTextBlock = value
  23. End Set
  24. End Property
  25. Private _MainSwap As Shapes.Path
  26. Public Property MainSwap As Shapes.Path
  27. Get
  28. Init()
  29. Return _MainSwap
  30. End Get
  31. Set(value As Shapes.Path)
  32. _MainSwap = value
  33. End Set
  34. End Property
  35. '属性
  36. Public Uuid As Integer = GetUuid()
  37. Public ReadOnly Property Inlines As InlineCollection
  38. Get
  39. Return MainTextBlock.Inlines
  40. End Get
  41. End Property
  42. Public Property CornerRadius As CornerRadius
  43. Get
  44. Return MainChrome.CornerRadius
  45. End Get
  46. Set(value As CornerRadius)
  47. MainChrome.CornerRadius = value
  48. MainBorder.CornerRadius = value
  49. End Set
  50. End Property
  51. Public Property Title As String
  52. Get
  53. Return GetValue(TitleProperty)
  54. End Get
  55. Set(value As String)
  56. SetValue(TitleProperty, value)
  57. If _MainTextBlock IsNot Nothing Then MainTextBlock.Text = value
  58. End Set
  59. End Property
  60. Public Shared ReadOnly TitleProperty As DependencyProperty = DependencyProperty.Register("Title", GetType(String), GetType(MyCard), New PropertyMetadata(""))
  61. 'UI 建立
  62. Public Sub New()
  63. MainChrome = New MyDropShadow With {
  64. .Margin = New Thickness(-3, -3, -3, -3 - GetWPFSize(1)), .ShadowRadius = 3, .Opacity = DropShadowIdleOpacity, .CornerRadius = New CornerRadius(5)}
  65. MainChrome.SetResourceReference(MyDropShadow.ColorProperty, "ColorObject1")
  66. Children.Insert(0, MainChrome)
  67. MainBorder = New Border With {.Background = New SolidColorBrush(Color.FromArgb(245, 255, 255, 255)), .CornerRadius = New CornerRadius(5), .IsHitTestVisible = False}
  68. Children.Insert(1, MainBorder)
  69. MainGrid = New Grid
  70. Children.Add(MainGrid)
  71. End Sub
  72. Private IsLoad As Boolean = False
  73. Private Sub Init() Handles Me.Loaded
  74. If IsLoad Then Return
  75. IsLoad = True
  76. '初次加载限定
  77. If MainTextBlock Is Nothing Then
  78. MainTextBlock = New TextBlock With {.HorizontalAlignment = HorizontalAlignment.Left, .VerticalAlignment = VerticalAlignment.Top, .Margin = New Thickness(15, 12, 0, 0), .FontWeight = FontWeights.Bold, .FontSize = 13, .IsHitTestVisible = False}
  79. MainTextBlock.SetResourceReference(TextBlock.ForegroundProperty, "ColorBrush1")
  80. MainTextBlock.SetBinding(TextBlock.TextProperty, New Binding("Title") With {.Source = Me, .Mode = BindingMode.OneWay})
  81. MainGrid.Children.Add(MainTextBlock)
  82. End If
  83. If CanSwap OrElse SwapControl IsNot Nothing Then
  84. If SwapControl Is Nothing AndAlso Children.Count > 3 Then SwapControl = Children(3)
  85. MainSwap = New Shapes.Path With {.HorizontalAlignment = HorizontalAlignment.Right, .Stretch = Stretch.Uniform, .Height = 6, .Width = 10, .VerticalAlignment = VerticalAlignment.Top, .Margin = New Thickness(0, 17, 16, 0), .Data = New GeometryConverter().ConvertFromString("M2,4 l-2,2 10,10 10,-10 -2,-2 -8,8 -8,-8 z"), .RenderTransform = New RotateTransform(180), .RenderTransformOrigin = New Point(0.5, 0.5)}
  86. MainSwap.SetResourceReference(Shapes.Path.FillProperty, "ColorBrush1")
  87. MainGrid.Children.Add(MainSwap)
  88. End If
  89. '改变默认的折叠
  90. If IsSwapped AndAlso SwapControl IsNot Nothing Then
  91. MainSwap.RenderTransform = New RotateTransform(If(SwapLogoRight, 270, 0))
  92. SwapControl.Visibility = Visibility.Collapsed
  93. '取消由于高度变化被迫触发的高度动画
  94. Dim RawUseAnimation As Boolean = UseAnimation
  95. UseAnimation = False
  96. Height = SwapedHeight
  97. AniStop("MyCard Height " & Uuid)
  98. IsHeightAnimating = False
  99. RunInUi(Sub() UseAnimation = RawUseAnimation, True)
  100. End If
  101. End Sub
  102. Public Sub StackInstall()
  103. StackInstall(SwapControl, SwapType, Title)
  104. TriggerForceResize()
  105. End Sub
  106. Public Shared Sub StackInstall(ByRef Stack As StackPanel, Type As Integer, Optional CardTitle As String = "")
  107. '这一部分的代码是好几年前留下的究极屎坑,当时还不知道该咋正确调用这种方法,就写了这么一坨屎
  108. '但是现在……反正勉强能用……懒得改了就这样吧.jpg
  109. '别骂了别骂了.jpg
  110. If Stack.Tag Is Nothing Then Return
  111. '排序
  112. Dim HasDuplicates As Boolean = False
  113. Select Case Type
  114. Case 3
  115. Stack.Tag = CType(Stack.Tag, List(Of DlOptiFineListEntry)).SortByComparison(Function(a, b) CompareVersionGE(a.DisplayName, b.DisplayName))
  116. Case 4, 10
  117. Stack.Tag = CType(Stack.Tag, List(Of DlLiteLoaderListEntry)).SortByComparison(Function(a, b) CompareVersionGE(a.Inherit, b.Inherit))
  118. Case 6
  119. Stack.Tag = CType(Stack.Tag, List(Of DlForgeVersionEntry)).OrderByDescending(Function(a) a).ToList
  120. Case 8, 9
  121. Stack.Tag = CType(Stack.Tag, List(Of ResourceVersion)).SortByComparison(Function(a, b) a.ReleaseDate > b.ReleaseDate)
  122. HasDuplicates =
  123. CType(Stack.Tag, List(Of ResourceVersion)).DistinctBy(Function(a) a.Display).Count <>
  124. CType(Stack.Tag, List(Of ResourceVersion)).Count
  125. End Select
  126. '控件转换
  127. Select Case Type
  128. Case 6
  129. ForgeDownloadListItemPreload(Stack, Stack.Tag, AddressOf ForgeSave_Click, True)
  130. Case 8
  131. PageDownloadResourceDetail.ResourceFilesCardPreload(Stack, Stack.Tag)
  132. End Select
  133. '实现控件虚拟化
  134. For Each Data As Object In Stack.Tag
  135. Select Case Type
  136. Case 0
  137. Stack.Children.Add(CType(Data, McInstance).ToListItem(AddressOf PageSelectRight.McInstanceListContent))
  138. Case 2
  139. Stack.Children.Add(McDownloadListItem(Data, AddressOf McDownloadMenuSave, True))
  140. Case 3
  141. Stack.Children.Add(OptiFineDownloadListItem(Data, AddressOf OptiFineSave_Click, True))
  142. Case 6
  143. Stack.Children.Add(ForgeDownloadListItem(Data, AddressOf ForgeSave_Click, True))
  144. Case 7
  145. '不能使用 AddressOf,这导致了 #535,原因完全不明,疑似是编译器 Bug
  146. Stack.Children.Add(McDownloadListItem(Data, Sub(sender, e) FrmDownloadInstall.MinecraftSelected(sender, e), False))
  147. Case 8
  148. '若存在重复的版本名,则显示文件名而非版本名(#1344)
  149. Stack.Children.Add(CType(Data, ResourceVersion).ToListItem(
  150. AddressOf FrmDownloadResourceDetail.Save_Click, IsBadDisplay:=HasDuplicates))
  151. Case 9
  152. '若存在重复的版本名,则显示文件名而非版本名(#1344)
  153. Stack.Children.Add(CType(Data, ResourceVersion).ToListItem(
  154. AddressOf FrmDownloadResourceDetail.Install_Click, AddressOf FrmDownloadResourceDetail.Save_Click, IsBadDisplay:=HasDuplicates))
  155. Case 10
  156. Stack.Children.Add(LiteLoaderDownloadListItem(Data, AddressOf LiteLoaderSave_Click, True))
  157. Case 11
  158. Stack.Children.Add(CType(Data, HelpEntry).ToListItem)
  159. Case 12
  160. Stack.Children.Add(FabricDownloadListItem(CType(Data, JObject), AddressOf FrmDownloadInstall.Fabric_Selected))
  161. Case 13
  162. Stack.Children.Add(NeoForgeDownloadListItem(Data, AddressOf NeoForgeSave_Click, True))
  163. Case Else
  164. Log("未知的虚拟化种类:" & Type, NotifyLevel.MsgBoxAndFeedback)
  165. End Select
  166. Next
  167. Stack.Children.Add(New FrameworkElement With {.Height = 18}) '下边距,同时适应折叠
  168. Stack.Tag = Nothing
  169. End Sub
  170. '动画
  171. Private Const DropShadowIdleOpacity As Double = 0.07
  172. Private Const DropShadowHoverOpacity As Double = 0.4
  173. Public Property HasMouseAnimation As Boolean = True
  174. Private Sub MyCard_MouseEnter(sender As Object, e As MouseEventArgs) Handles Me.MouseEnter
  175. If Not HasMouseAnimation Then Return
  176. Dim AniList As New List(Of AniData)
  177. If Not IsNothing(MainTextBlock) Then AniList.Add(AaColor(MainTextBlock, TextBlock.ForegroundProperty, "ColorBrush2", 90))
  178. If Not IsNothing(MainSwap) Then AniList.Add(AaColor(MainSwap, Shapes.Path.FillProperty, "ColorBrush2", 90))
  179. AniList.AddRange({
  180. AaColor(MainChrome, MyDropShadow.ColorProperty, "ColorObject4", 90),
  181. AaOpacity(MainChrome, DropShadowHoverOpacity - MainChrome.Opacity, 90)
  182. })
  183. AniStart(AniList, "MyCard Mouse " & Uuid)
  184. End Sub
  185. Private Sub MyCard_MouseLeave(sender As Object, e As MouseEventArgs) Handles Me.MouseLeave
  186. If Not HasMouseAnimation Then Return
  187. Dim AniList As New List(Of AniData)
  188. If Not IsNothing(MainTextBlock) Then AniList.Add(AaColor(MainTextBlock, TextBlock.ForegroundProperty, "ColorBrush1", 90))
  189. If Not IsNothing(MainSwap) Then AniList.Add(AaColor(MainSwap, Shapes.Path.FillProperty, "ColorBrush1", 90))
  190. AniList.AddRange({
  191. AaColor(MainChrome, MyDropShadow.ColorProperty, "ColorObject1", 90),
  192. AaOpacity(MainChrome, DropShadowIdleOpacity - MainChrome.Opacity, 90)
  193. })
  194. AniStart(AniList, "MyCard Mouse " & Uuid)
  195. End Sub
  196. #Region "高度改变动画"
  197. ''' <summary>
  198. ''' 是否启用高度改变动画。
  199. ''' </summary>
  200. Public Property UseAnimation As Boolean = True
  201. Private IsHeightAnimating As Boolean = False
  202. Private ActualUsedHeight As Double '回滚实际高度(例如 NaN)
  203. Private Sub MySizeChanged(sender As Object, e As SizeChangedEventArgs) Handles Me.SizeChanged
  204. If Not UseAnimation Then Return
  205. Dim DeltaHeight As Double = If(IsSwapped, SwapedHeight, e.NewSize.Height) - e.PreviousSize.Height
  206. '卡片的进入时动画已被页面通用切换动画替代
  207. If e.PreviousSize.Height = 0 OrElse IsHeightAnimating OrElse Math.Abs(DeltaHeight) < 1 OrElse ActualHeight = 0 Then Return
  208. StartHeightAnimation(DeltaHeight, e.PreviousSize.Height, False)
  209. End Sub
  210. Private Sub StartHeightAnimation(Delta As Double, PreviousHeight As Double, IsLoadAnimation As Boolean)
  211. If IsHeightAnimating OrElse FrmMain Is Nothing Then Return '避免 XAML 设计器出错
  212. Dim AnimList As New List(Of AniData)
  213. Dim AbsDelta = Math.Abs(Delta)
  214. If AbsDelta <= 800 Then
  215. '短距离,直接使用 150ms 的缓动动画
  216. AnimList.Add(AaHeight(Me, Delta, 150,, New AniEaseOutFluent(AniEasePower.ExtraStrong)))
  217. Else
  218. Dim EaseLength As Integer, EaseTime As Integer
  219. Dim InitSpeed As Integer '到达缓动区前的初速度
  220. If Delta < 0 AndAlso AbsDelta - EaseLength > 5000 * 0.1 Then
  221. '收回距离过长 (>0.1s),强制以 100ms 完成匀速段,然后让减速段更长
  222. EaseLength = 200
  223. EaseTime = 150
  224. InitSpeed = (AbsDelta - EaseLength) / 0.1
  225. ElseIf Delta > 0 AndAlso AbsDelta - EaseLength > 5000 * 0.6 Then
  226. '展开距离过长 (>0.6s),以 5000 速度展示 300ms 匀速段,剩下的距离全部归入减速段
  227. InitSpeed = 5000
  228. EaseLength = AbsDelta - InitSpeed * 0.3
  229. EaseTime = 400
  230. Else
  231. '中程,匀速地快速展开(或收回)
  232. EaseLength = 150
  233. EaseTime = 200
  234. InitSpeed = 4000
  235. End If
  236. '匀速段
  237. AnimList.Add(AaHeight(Me, (AbsDelta - EaseLength) * Math.Sign(Delta),
  238. (AbsDelta - EaseLength) / InitSpeed * 1000))
  239. '减速段
  240. AnimList.Add(AaHeight(Me, EaseLength * Math.Sign(Delta),
  241. EaseTime,, New AniEaseOutFluentWithInitial(InitSpeed, EaseTime / 1000, EaseLength), True))
  242. End If
  243. AnimList.Add(AaCode(
  244. Sub()
  245. IsHeightAnimating = False
  246. Height = ActualUsedHeight
  247. If IsSwapped AndAlso SwapControl IsNot Nothing Then SwapControl.Visibility = Visibility.Collapsed
  248. End Sub,, True))
  249. AniStart(AnimList, "MyCard Height " & Uuid)
  250. IsHeightAnimating = True
  251. ActualUsedHeight = If(IsSwapped, SwapedHeight, Height)
  252. Height = PreviousHeight
  253. End Sub
  254. ''' <summary>
  255. ''' 通知 MyCard,控件内容已改变,需要中断动画并瞬间更新高度。
  256. ''' </summary>
  257. Public Sub TriggerForceResize()
  258. Height = If(IsSwapped, SwapedHeight, Double.NaN)
  259. AniStop("MyCard Height " & Uuid)
  260. IsHeightAnimating = False
  261. End Sub
  262. #End Region
  263. #Region "折叠"
  264. '若设置了 CanSwap,或 SwapControl 不为空,则判定为会进行折叠
  265. '这是因为不能直接在 XAML 中设置 SwapControl
  266. Public SwapControl As FrameworkElement
  267. Public Property CanSwap As Boolean = False
  268. ''' <summary>
  269. ''' 被折叠的种类,用于控件虚拟化。
  270. ''' </summary>
  271. Public Property SwapType As Integer
  272. ''' <summary>
  273. ''' 是否已被折叠。
  274. ''' </summary>
  275. Public Property IsSwapped As Boolean
  276. Get
  277. Return _IsSwapped
  278. End Get
  279. Set(value As Boolean)
  280. If _IsSwapped = value Then Return
  281. _IsSwapped = value
  282. If SwapControl Is Nothing Then Return
  283. '展开
  284. If Not IsSwapped AndAlso TypeOf SwapControl Is StackPanel Then StackInstall(SwapControl, SwapType, Title)
  285. '若尚未加载,会在 Loaded 事件中触发无动画的折叠,不需要在这里进行
  286. If Not IsLoaded Then Return
  287. '更新高度
  288. SwapControl.Visibility = Visibility.Visible
  289. TriggerForceResize()
  290. '改变箭头
  291. AniStart(AaRotateTransform(MainSwap, If(_IsSwapped, If(SwapLogoRight, 270, 0), 180) - CType(MainSwap.RenderTransform, RotateTransform).Angle, 250,, New AniEaseOutFluent(AniEasePower.ExtraStrong)), "MyCard Swap " & Uuid, True)
  292. End Set
  293. End Property
  294. Private _IsSwapped As Boolean = False
  295. <Obsolete("IsSwaped 存在拼写错误,请换用 IsSwapped 属性。")>
  296. Public Property IsSwaped As Boolean
  297. Get
  298. Return GetValue(IsSwapedProperty)
  299. End Get
  300. Set(value As Boolean)
  301. SetValue(IsSwapedProperty, value)
  302. End Set
  303. End Property
  304. Public Shared ReadOnly IsSwapedProperty As DependencyProperty = DependencyProperty.Register("IsSwaped", GetType(Boolean), GetType(MyCard), New PropertyMetadata(False,
  305. Sub(sender, e) If sender IsNot Nothing AndAlso TypeOf sender Is MyCard Then CType(sender, MyCard).IsSwapped = CType(e.NewValue, Boolean)))
  306. Public Property SwapLogoRight As Boolean = False
  307. Private IsSwapMouseDown As Boolean = False '用于触发卡片展开/折叠的 MouseDown
  308. Private IsCustomMouseDown As Boolean = False '用于触发自定义事件的 MouseDown
  309. Public Event PreviewSwap(sender As Object, e As RouteEventArgs)
  310. Public Event Swap(sender As Object, e As RouteEventArgs)
  311. Public Const SwapedHeight As Integer = 40
  312. Private Sub MyCard_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonDown
  313. Dim Pos As Double = Mouse.GetPosition(Me).Y
  314. If Not IsSwapped AndAlso
  315. (Pos > If(IsSwapped, SwapedHeight, SwapedHeight - 6) OrElse (Pos = 0 AndAlso Not IsMouseDirectlyOver)) Then Return
  316. IsCustomMouseDown = True
  317. If Not IsSwapped AndAlso
  318. (SwapControl Is Nothing OrElse Pos > If(IsSwapped, SwapedHeight, SwapedHeight - 6) OrElse (Pos = 0 AndAlso Not IsMouseDirectlyOver)) Then Return '检测点击位置;或已经不在可视树上的误判
  319. IsSwapMouseDown = True
  320. End Sub
  321. Private Sub MyCard_MouseLeftButtonUp() Handles Me.MouseLeftButtonUp
  322. If Not IsCustomMouseDown Then Return
  323. IsCustomMouseDown = False
  324. RaiseCustomEvent() '触发自定义事件
  325. If Not IsSwapMouseDown Then Return
  326. IsSwapMouseDown = False
  327. Dim Pos As Double = Mouse.GetPosition(Me).Y
  328. If Not IsSwapped AndAlso
  329. (SwapControl Is Nothing OrElse Pos > If(IsSwapped, SwapedHeight, SwapedHeight - 6) OrElse (Pos = 0 AndAlso Not IsMouseDirectlyOver)) Then Return '检测点击位置;或已经不在可视树上的误判
  330. Dim e = New RouteEventArgs(True)
  331. RaiseEvent PreviewSwap(Me, e)
  332. If e.Handled Then
  333. IsSwapMouseDown = False
  334. Return
  335. End If
  336. IsSwapped = Not IsSwapped
  337. Log("[Control] " & If(IsSwapped, "折叠卡片", "展开卡片") & If(Title Is Nothing, "", ":" & Title))
  338. RaiseEvent Swap(Me, e)
  339. End Sub
  340. Private Sub MyCard_MouseLeave_Swap(sender As Object, e As MouseEventArgs) Handles Me.MouseLeave
  341. IsSwapMouseDown = False
  342. End Sub
  343. #End Region
  344. End Class
  345. Partial Public Module ModAnimation
  346. Public Sub AniDispose(Control As MyCard, RemoveFromChildren As Boolean, Optional CallBack As ParameterizedThreadStart = Nothing)
  347. If Control.IsHitTestVisible Then
  348. Control.IsHitTestVisible = False
  349. AniStart({
  350. AaScaleTransform(Control, -0.08, 200,, New AniEaseInFluent),
  351. AaOpacity(Control, -1, 200,, New AniEaseOutFluent),
  352. AaHeight(Control, -Control.ActualHeight, 150, 100, New AniEaseOutFluent),
  353. AaCode(
  354. Sub()
  355. If RemoveFromChildren Then
  356. If Control.Parent Is Nothing Then Return
  357. CType(Control.Parent, Object).Children.Remove(Control)
  358. Else
  359. Control.Visibility = Visibility.Collapsed
  360. End If
  361. If CallBack IsNot Nothing Then CallBack(Control)
  362. End Sub,, True)
  363. }, "MyCard Dispose " & Control.Uuid)
  364. Else
  365. If RemoveFromChildren Then
  366. If Control.Parent Is Nothing Then Return
  367. CType(Control.Parent, Object).Children.Remove(Control)
  368. Else
  369. Control.Visibility = Visibility.Collapsed
  370. End If
  371. If CallBack IsNot Nothing Then CallBack(Control)
  372. End If
  373. End Sub
  374. End Module