1
0

MyCard.vb 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. Public Class MyCard
  2. '控件
  3. Inherits Grid
  4. Private ReadOnly MainGrid As Grid
  5. Private ReadOnly MainChrome As SystemDropShadowChrome
  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. Public MainTextBlock As TextBlock
  16. Public MainSwap As Shapes.Path
  17. '属性
  18. Public Uuid As Integer = GetUuid()
  19. Public Property Title As String
  20. Get
  21. Return GetValue(TitleProperty)
  22. End Get
  23. Set(value As String)
  24. SetValue(TitleProperty, value)
  25. If MainTextBlock IsNot Nothing Then MainTextBlock.Text = value
  26. End Set
  27. End Property
  28. Public Shared ReadOnly TitleProperty As DependencyProperty = DependencyProperty.Register("Title", GetType(String), GetType(MyCard), New PropertyMetadata(""))
  29. 'UI 建立
  30. Public Sub New()
  31. MainChrome = New SystemDropShadowChrome With {.Margin = New Thickness(-9.5, -9, 0.5, -0.5), .Opacity = 0.1, .CornerRadius = New CornerRadius(6)}
  32. MainChrome.SetResourceReference(SystemDropShadowChrome.ColorProperty, "ColorObject1")
  33. Children.Insert(0, MainChrome)
  34. MainBorder = New Border With {.Background = New SolidColorBrush(Color.FromArgb(205, 255, 255, 255)), .CornerRadius = New CornerRadius(6), .IsHitTestVisible = False}
  35. Children.Insert(1, MainBorder)
  36. MainGrid = New Grid
  37. Children.Add(MainGrid)
  38. End Sub
  39. Private IsLoad As Boolean = False
  40. Private Sub MyCard_Loaded() Handles Me.Loaded
  41. If IsLoad Then Exit Sub
  42. '初次加载限定
  43. If Not Title = "" AndAlso IsNothing(MainTextBlock) Then
  44. MainTextBlock = New TextBlock With {.HorizontalAlignment = HorizontalAlignment.Left, .VerticalAlignment = VerticalAlignment.Top, .Margin = New Thickness(15, 12, 0, 0), .FontWeight = FontWeights.Bold, .FontSize = 13, .IsHitTestVisible = False}
  45. MainTextBlock.SetResourceReference(TextBlock.ForegroundProperty, "ColorBrush1")
  46. MainTextBlock.SetBinding(TextBlock.TextProperty, New Binding("Title") With {.Source = Me, .Mode = BindingMode.OneWay})
  47. MainGrid.Children.Add(MainTextBlock)
  48. End If
  49. If CanSwap OrElse SwapControl IsNot Nothing Then
  50. If SwapControl Is Nothing AndAlso Children.Count > 3 Then SwapControl = Children(3)
  51. 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)}
  52. MainSwap.SetResourceReference(Shapes.Path.FillProperty, "ColorBrush1")
  53. MainGrid.Children.Add(MainSwap)
  54. End If
  55. IsLoad = True
  56. '改变默认的折叠
  57. If IsSwaped AndAlso SwapControl IsNot Nothing Then
  58. MainSwap.RenderTransform = New RotateTransform(If(SwapLogoRight, 270, 0))
  59. '取消由于高度变化被迫触发的高度动画
  60. Dim RawUseAnimation As Boolean = UseAnimation
  61. UseAnimation = False
  62. Height = SwapedHeight
  63. AniStop("MyCard Height " & Uuid)
  64. IsHeightAnimating = False
  65. RunInUi(Sub() UseAnimation = RawUseAnimation, True)
  66. End If
  67. End Sub
  68. Public Sub StackInstall()
  69. StackInstall(SwapControl, SwapType, Title)
  70. TriggerForceResize()
  71. End Sub
  72. Public Shared Sub StackInstall(ByRef Stack As StackPanel, Type As Integer, Optional CardTitle As String = "")
  73. '这一部分的代码是好几年前留下的究极屎坑,当时还不知道该咋正确调用这种方法,就写了这么一坨屎
  74. '但是现在……反正勉强能用……懒得改了就这样吧.jpg
  75. '别骂了别骂了.jpg
  76. If IsNothing(Stack.Tag) Then Exit Sub
  77. '排序
  78. Select Case Type
  79. Case 3
  80. Stack.Tag = Sort(CType(Stack.Tag, List(Of DlOptiFineListEntry)), Function(Left As DlOptiFineListEntry, Right As DlOptiFineListEntry) As Boolean
  81. Return VersionSortBoolean(Left.NameDisplay, Right.NameDisplay)
  82. End Function)
  83. Case 4, 10
  84. Stack.Tag = Sort(CType(Stack.Tag, List(Of DlLiteLoaderListEntry)), Function(Left As DlLiteLoaderListEntry, Right As DlLiteLoaderListEntry) As Boolean
  85. Return VersionSortBoolean(Left.Inherit, Right.Inherit)
  86. End Function)
  87. Case 6
  88. Stack.Tag = Sort(CType(Stack.Tag, List(Of DlForgeVersionEntry)), Function(Left As DlForgeVersionEntry, Right As DlForgeVersionEntry) As Boolean
  89. Return VersionSortBoolean(Left.Version, Right.Version)
  90. End Function)
  91. Case 8, 9
  92. Stack.Tag = Sort(CType(Stack.Tag, List(Of CompFile)), Function(Left As CompFile, Right As CompFile) As Boolean
  93. Return Left.ReleaseDate > Right.ReleaseDate
  94. End Function)
  95. End Select
  96. '控件转换
  97. Select Case Type
  98. Case 5
  99. Dim LoadingPickaxe As New MyLoading With {.Text = "正在获取版本列表", .Margin = New Thickness(5)}
  100. Dim Loader = New LoaderTask(Of String, List(Of DlForgeVersionEntry))("DlForgeVersion Main", AddressOf DlForgeVersionMain)
  101. LoadingPickaxe.State = Loader
  102. Loader.Start(Stack.Tag)
  103. AddHandler LoadingPickaxe.StateChanged, AddressOf FrmDownloadForge.Forge_StateChanged
  104. AddHandler LoadingPickaxe.Click, AddressOf FrmDownloadForge.Forge_Click
  105. Stack.Children.Add(LoadingPickaxe)
  106. Case 6
  107. ForgeDownloadListItemPreload(Stack, Stack.Tag, AddressOf ForgeSave_Click, True)
  108. Case 8
  109. CompFilesCardPreload(Stack, Stack.Tag)
  110. End Select
  111. '实现控件虚拟化
  112. For Each Data As Object In Stack.Tag
  113. Select Case Type
  114. Case 0
  115. Stack.Children.Add(PageSelectRight.McVersionListItem(Data))
  116. Case 2
  117. Stack.Children.Add(McDownloadListItem(Data, AddressOf McDownloadMenuSave, True))
  118. Case 3
  119. Stack.Children.Add(OptiFineDownloadListItem(Data, AddressOf OptiFineSave_Click, True))
  120. Case 4
  121. Stack.Children.Add(LiteLoaderDownloadListItem(Data, AddressOf FrmDownloadLiteLoader.DownloadStart, False))
  122. Case 5
  123. Case 6
  124. Stack.Children.Add(ForgeDownloadListItem(Data, AddressOf ForgeSave_Click, True))
  125. Case 7
  126. Stack.Children.Add(McDownloadListItem(Data, Sub(sender, e) '不能使用 AddressOf,这导致了 #535,原因完全不明,疑似是编译器 Bug
  127. FrmDownloadInstall.MinecraftSelected(sender, e)
  128. End Sub, False))
  129. Case 8
  130. If Distinct(CType(Stack.Tag, List(Of CompFile)), Function(a, b) a.DisplayName = b.DisplayName).Count <>
  131. CType(Stack.Tag, List(Of CompFile)).Count Then
  132. '存在重复的名称(#1344)
  133. Stack.Children.Add(CType(Data, CompFile).ToListItem(AddressOf FrmDownloadCompDetail.Save_Click, BadDisplayName:=True))
  134. Else
  135. '不存在重复的名称,正常加载
  136. Stack.Children.Add(CType(Data, CompFile).ToListItem(AddressOf FrmDownloadCompDetail.Save_Click))
  137. End If
  138. Case 9
  139. If Distinct(CType(Stack.Tag, List(Of CompFile)), Function(a, b) a.DisplayName = b.DisplayName).Count <>
  140. CType(Stack.Tag, List(Of CompFile)).Count Then
  141. '存在重复的名称(#1344)
  142. Stack.Children.Add(CType(Data, CompFile).ToListItem(AddressOf FrmDownloadCompDetail.Install_Click, AddressOf FrmDownloadCompDetail.Save_Click, BadDisplayName:=True))
  143. Else
  144. '不存在重复的名称,正常加载
  145. Stack.Children.Add(CType(Data, CompFile).ToListItem(AddressOf FrmDownloadCompDetail.Install_Click, AddressOf FrmDownloadCompDetail.Save_Click))
  146. End If
  147. Case 10
  148. Stack.Children.Add(LiteLoaderDownloadListItem(Data, AddressOf LiteLoaderSave_Click, True))
  149. Case 11
  150. Stack.Children.Add(CType(Data, HelpEntry).ToListItem)
  151. Case 12
  152. Stack.Children.Add(FabricDownloadListItem(CType(Data, JObject), AddressOf FrmDownloadInstall.Fabric_Selected))
  153. Case Else
  154. Log("未知的虚拟化种类:" & Type, LogLevel.Feedback)
  155. End Select
  156. Next
  157. Stack.Children.Add(New FrameworkElement With {.Height = 18}) '下边距,同时适应折叠
  158. Stack.Tag = Nothing
  159. End Sub
  160. '事件
  161. Public Property HasMouseAnimation As Boolean = True
  162. Private Sub MyCard_MouseEnter(sender As Object, e As MouseEventArgs) Handles Me.MouseEnter
  163. If Not HasMouseAnimation Then Exit Sub
  164. Dim AniList As New List(Of AniData)
  165. If Not IsNothing(MainTextBlock) Then AniList.Add(AaColor(MainTextBlock, TextBlock.ForegroundProperty, "ColorBrush2", 150))
  166. If Not IsNothing(MainSwap) Then AniList.Add(AaColor(MainSwap, Shapes.Path.FillProperty, "ColorBrush2", 150))
  167. AniList.AddRange({
  168. AaColor(MainChrome, SystemDropShadowChrome.ColorProperty, "ColorObject2", 180),
  169. AaColor(MainBorder, Border.BackgroundProperty, New MyColor(230, 255, 255, 255) - MainBorder.Background, 180),
  170. AaOpacity(MainChrome, 0.3 - MainChrome.Opacity, 180)
  171. })
  172. AniStart(AniList, "MyCard Mouse " & Uuid)
  173. End Sub
  174. Private Sub MyCard_MouseLeave(sender As Object, e As MouseEventArgs) Handles Me.MouseLeave
  175. If Not HasMouseAnimation Then Exit Sub
  176. Dim AniList As New List(Of AniData)
  177. If Not IsNothing(MainTextBlock) Then AniList.Add(AaColor(MainTextBlock, TextBlock.ForegroundProperty, "ColorBrush1", 250))
  178. If Not IsNothing(MainSwap) Then AniList.Add(AaColor(MainSwap, Shapes.Path.FillProperty, "ColorBrush1", 250))
  179. AniList.AddRange({
  180. AaColor(MainChrome, SystemDropShadowChrome.ColorProperty, "ColorObject1", 300),
  181. AaColor(MainBorder, Border.BackgroundProperty, New MyColor(205, 255, 255, 255) - MainBorder.Background, 300),
  182. AaOpacity(MainChrome, 0.1 - MainChrome.Opacity, 300)
  183. })
  184. AniStart(AniList, "MyCard Mouse " & Uuid)
  185. End Sub
  186. #Region "高度改变动画"
  187. ''' <summary>
  188. ''' 是否启用高度改变动画。
  189. ''' </summary>
  190. Public Property UseAnimation As Boolean = True
  191. Private IsHeightAnimating As Boolean = False
  192. Private ActualUsedHeight As Double '回滚实际高度(例如 NaN)
  193. Private Sub MySizeChanged(sender As Object, e As SizeChangedEventArgs) Handles Me.SizeChanged
  194. If Not UseAnimation Then Exit Sub
  195. Dim DeltaHeight As Double = If(IsSwaped, SwapedHeight, e.NewSize.Height) - e.PreviousSize.Height
  196. '卡片的进入时动画已被页面通用切换动画替代
  197. If e.PreviousSize.Height = 0 OrElse IsHeightAnimating OrElse Math.Abs(DeltaHeight) < 1 OrElse ActualHeight = 0 Then Exit Sub
  198. StartHeightAnimation(DeltaHeight, e.PreviousSize.Height, False)
  199. End Sub
  200. Private Sub StartHeightAnimation(DeltaHeight As Double, PreviousHeight As Double, IsLoadAnimation As Boolean)
  201. If IsHeightAnimating OrElse FrmMain Is Nothing Then Exit Sub '避免 XAML 设计器出错
  202. Dim AnimList As New List(Of AniData)
  203. If DeltaHeight > 10 OrElse (DeltaHeight < -10 AndAlso Not IsNothing(SwapControl)) Then '如果不是需要折叠的卡片,高度减小时的弹跳会吞掉按钮下边框
  204. '高度增加较大,使用弹起动画
  205. Dim Delta As Double = MathClamp(Math.Abs(DeltaHeight) * 0.05, 3, 10) * Math.Sign(DeltaHeight)
  206. AnimList.AddRange({
  207. AaHeight(Me, DeltaHeight + Delta, 300, If(IsLoadAnimation, 30, 0), If(DeltaHeight > FrmMain.Height, New AniEaseInFluent(AniEasePower.ExtraStrong), New AniEaseOutFluent(AniEasePower.ExtraStrong))),
  208. AaHeight(Me, -Delta, 150, 260, Ease:=New AniEaseOutFluent(AniEasePower.Strong))
  209. })
  210. Else
  211. '普通的改变就行啦
  212. AnimList.AddRange({
  213. AaHeight(Me, DeltaHeight, MathClamp(Math.Abs(DeltaHeight) * 4, 150, 250),, New AniEaseOutFluent)
  214. })
  215. End If
  216. AnimList.Add(AaCode(Sub()
  217. IsHeightAnimating = False
  218. Height = ActualUsedHeight
  219. If IsSwaped Then SwapControl.Visibility = Visibility.Collapsed
  220. End Sub,, True))
  221. AniStart(AnimList, "MyCard Height " & Uuid)
  222. IsHeightAnimating = True
  223. ActualUsedHeight = If(IsSwaped, SwapedHeight, Height)
  224. Height = PreviousHeight
  225. End Sub
  226. ''' <summary>
  227. ''' 通知 MyCard,控件内容已改变,需要中断动画并更新高度。
  228. ''' </summary>
  229. Public Sub TriggerForceResize()
  230. Height = If(IsSwaped, SwapedHeight, Double.NaN)
  231. AniStop("MyCard Height " & Uuid)
  232. IsHeightAnimating = False
  233. End Sub
  234. #End Region
  235. '折叠
  236. '若设置了 CanSwap,或 SwapControl 不为空,则判定为会进行折叠
  237. '这是因为不能直接在 XAML 中设置 SwapControl
  238. Public SwapControl As Object
  239. Public Property CanSwap As Boolean = False
  240. ''' <summary>
  241. ''' 被折叠的种类,用于控件虚拟化。
  242. ''' </summary>
  243. Public Property SwapType As Integer
  244. ''' <summary>
  245. ''' 是否已被折叠。
  246. ''' </summary>
  247. Public Property IsSwaped As Boolean
  248. Get
  249. Return _IsSwaped
  250. End Get
  251. Set(value As Boolean)
  252. If _IsSwaped = value Then Exit Property
  253. _IsSwaped = value
  254. If SwapControl Is Nothing Then Exit Property
  255. '展开
  256. If Not IsSwaped AndAlso TypeOf SwapControl Is StackPanel Then StackInstall(SwapControl, SwapType, Title)
  257. '若尚未加载,会在 Loaded 事件中触发无动画的折叠,不需要在这里进行
  258. If Not IsLoaded Then Exit Property
  259. '更新高度
  260. SwapControl.Visibility = Visibility.Visible
  261. TriggerForceResize()
  262. '改变箭头
  263. AniStart(AaRotateTransform(MainSwap, If(_IsSwaped, If(SwapLogoRight, 270, 0), 180) - CType(MainSwap.RenderTransform, RotateTransform).Angle, 400,, New AniEaseOutBack(AniEasePower.Weak)), "MyCard Swap " & Uuid, True)
  264. End Set
  265. End Property
  266. Private _IsSwaped As Boolean = False
  267. Public Property SwapLogoRight As Boolean = False
  268. Private IsMouseDown As Boolean = False
  269. Public Event PreviewSwap(sender As Object, e As RouteEventArgs)
  270. Public Event Swap(sender As Object, e As RouteEventArgs)
  271. Public Const SwapedHeight As Integer = 40
  272. Private Sub MyCard_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonDown
  273. Dim Pos As Double = Mouse.GetPosition(Me).Y
  274. If Not IsSwaped AndAlso (IsNothing(SwapControl) OrElse Pos > SwapedHeight OrElse (Pos = 0 AndAlso Not IsMouseDirectlyOver)) Then Exit Sub '检测点击位置;或已经不在可视树上的误判
  275. IsMouseDown = True
  276. End Sub
  277. Private Sub MyCard_MouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonUp
  278. If IsMouseDown Then
  279. IsMouseDown = False
  280. Dim Pos As Double = Mouse.GetPosition(Me).Y
  281. If Not IsSwaped AndAlso (IsNothing(SwapControl) OrElse Pos > SwapedHeight OrElse (Pos = 0 AndAlso Not IsMouseDirectlyOver)) Then Exit Sub '检测点击位置;或已经不在可视树上的误判
  282. Dim ee = New RouteEventArgs(True)
  283. RaiseEvent PreviewSwap(Me, ee)
  284. If ee.Handled Then
  285. IsMouseDown = False
  286. Exit Sub
  287. End If
  288. IsSwaped = Not IsSwaped
  289. Log("[Control] " & If(IsSwaped, "折叠卡片", "展开卡片") & If(Title Is Nothing, "", ":" & Title))
  290. RaiseEvent Swap(Me, ee)
  291. End If
  292. End Sub
  293. Private Sub MyCard_MouseLeave_Swap(sender As Object, e As MouseEventArgs) Handles Me.MouseLeave
  294. IsMouseDown = False
  295. End Sub
  296. End Class
  297. Partial Public Module ModAnimation
  298. Public Sub AniDispose(Control As MyCard, RemoveFromChildren As Boolean, Optional CallBack As ParameterizedThreadStart = Nothing)
  299. If Control.IsHitTestVisible Then
  300. Control.IsHitTestVisible = False
  301. AniStart({
  302. AaScaleTransform(Control, -0.08, 200,, New AniEaseInFluent),
  303. AaOpacity(Control, -1, 200,, New AniEaseOutFluent),
  304. AaHeight(Control, -Control.ActualHeight, 150, 100, New AniEaseOutFluent),
  305. AaCode(Sub()
  306. If RemoveFromChildren Then
  307. If Control.Parent Is Nothing Then Exit Sub
  308. CType(Control.Parent, Object).Children.Remove(Control)
  309. Else
  310. Control.Visibility = Visibility.Collapsed
  311. End If
  312. If CallBack IsNot Nothing Then CallBack(Control)
  313. End Sub,, True)
  314. }, "MyCard Dispose " & Control.Uuid)
  315. Else
  316. If RemoveFromChildren Then
  317. If Control.Parent Is Nothing Then Exit Sub
  318. CType(Control.Parent, Object).Children.Remove(Control)
  319. Else
  320. Control.Visibility = Visibility.Collapsed
  321. End If
  322. If CallBack IsNot Nothing Then CallBack(Control)
  323. End If
  324. End Sub
  325. End Module