MyCard.vb 18 KB

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