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. '取消由于高度变化被迫触发的高度动画
  93. Dim RawUseAnimation As Boolean = UseAnimation
  94. UseAnimation = False
  95. Height = SwapedHeight
  96. AniStop("MyCard Height " & Uuid)
  97. IsHeightAnimating = False
  98. RunInUi(Sub() UseAnimation = RawUseAnimation, True)
  99. End If
  100. End Sub
  101. Public Sub StackInstall()
  102. StackInstall(SwapControl, SwapType, Title)
  103. TriggerForceResize()
  104. End Sub
  105. Public Shared Sub StackInstall(ByRef Stack As StackPanel, Type As Integer, Optional CardTitle As String = "")
  106. '这一部分的代码是好几年前留下的究极屎坑,当时还不知道该咋正确调用这种方法,就写了这么一坨屎
  107. '但是现在……反正勉强能用……懒得改了就这样吧.jpg
  108. '别骂了别骂了.jpg
  109. If IsNothing(Stack.Tag) Then Return
  110. '排序
  111. Select Case Type
  112. Case 3
  113. Stack.Tag = CType(Stack.Tag, List(Of DlOptiFineListEntry)).Sort(Function(a, b) VersionSortBoolean(a.NameDisplay, b.NameDisplay))
  114. Case 4, 10
  115. Stack.Tag = CType(Stack.Tag, List(Of DlLiteLoaderListEntry)).Sort(Function(a, b) VersionSortBoolean(a.Inherit, b.Inherit))
  116. Case 6
  117. Stack.Tag = CType(Stack.Tag, List(Of DlForgeVersionEntry)).Sort(Function(a, b) a.Version > b.Version)
  118. Case 8, 9
  119. Stack.Tag = CType(Stack.Tag, List(Of CompFile)).Sort(Function(a, b) a.ReleaseDate > b.ReleaseDate)
  120. End Select
  121. '控件转换
  122. Select Case Type
  123. Case 6
  124. ForgeDownloadListItemPreload(Stack, Stack.Tag, AddressOf ForgeSave_Click, True)
  125. Case 8
  126. CompFilesCardPreload(Stack, Stack.Tag)
  127. End Select
  128. '实现控件虚拟化
  129. For Each Data As Object In Stack.Tag
  130. Select Case Type
  131. Case 0
  132. Stack.Children.Add(PageSelectRight.McVersionListItem(Data))
  133. Case 2
  134. Stack.Children.Add(McDownloadListItem(Data, AddressOf McDownloadMenuSave, True))
  135. Case 3
  136. Stack.Children.Add(OptiFineDownloadListItem(Data, AddressOf OptiFineSave_Click, True))
  137. Case 6
  138. Stack.Children.Add(ForgeDownloadListItem(Data, AddressOf ForgeSave_Click, True))
  139. Case 7
  140. '不能使用 AddressOf,这导致了 #535,原因完全不明,疑似是编译器 Bug
  141. Stack.Children.Add(McDownloadListItem(Data, Sub(sender, e) FrmDownloadInstall.MinecraftSelected(sender, e), False))
  142. Case 8
  143. If CType(Stack.Tag, List(Of CompFile)).Distinct(Function(a, b) a.DisplayName = b.DisplayName).Count <>
  144. CType(Stack.Tag, List(Of CompFile)).Count Then
  145. '存在重复的名称(#1344)
  146. Stack.Children.Add(CType(Data, CompFile).ToListItem(AddressOf FrmDownloadCompDetail.Save_Click, BadDisplayName:=True))
  147. Else
  148. '不存在重复的名称,正常加载
  149. Stack.Children.Add(CType(Data, CompFile).ToListItem(AddressOf FrmDownloadCompDetail.Save_Click))
  150. End If
  151. Case 9
  152. If CType(Stack.Tag, List(Of CompFile)).Distinct(Function(a, b) a.DisplayName = b.DisplayName).Count <>
  153. CType(Stack.Tag, List(Of CompFile)).Count Then
  154. '存在重复的名称(#1344)
  155. Stack.Children.Add(CType(Data, CompFile).ToListItem(AddressOf FrmDownloadCompDetail.Install_Click, AddressOf FrmDownloadCompDetail.Save_Click, BadDisplayName:=True))
  156. Else
  157. '不存在重复的名称,正常加载
  158. Stack.Children.Add(CType(Data, CompFile).ToListItem(AddressOf FrmDownloadCompDetail.Install_Click, AddressOf FrmDownloadCompDetail.Save_Click))
  159. End If
  160. Case 10
  161. Stack.Children.Add(LiteLoaderDownloadListItem(Data, AddressOf LiteLoaderSave_Click, True))
  162. Case 11
  163. Stack.Children.Add(CType(Data, HelpEntry).ToListItem)
  164. Case 12
  165. Stack.Children.Add(FabricDownloadListItem(CType(Data, JObject), AddressOf FrmDownloadInstall.Fabric_Selected))
  166. Case 13
  167. Stack.Children.Add(NeoForgeDownloadListItem(Data, AddressOf NeoForgeSave_Click, True))
  168. Case Else
  169. Log("未知的虚拟化种类:" & Type, LogLevel.Feedback)
  170. End Select
  171. Next
  172. Stack.Children.Add(New FrameworkElement With {.Height = 18}) '下边距,同时适应折叠
  173. Stack.Tag = Nothing
  174. End Sub
  175. '动画
  176. Private Const DropShadowIdleOpacity As Double = 0.07
  177. Private Const DropShadowHoverOpacity As Double = 0.4
  178. Public Property HasMouseAnimation As Boolean = True
  179. Private Sub MyCard_MouseEnter(sender As Object, e As MouseEventArgs) Handles Me.MouseEnter
  180. If Not HasMouseAnimation Then Return
  181. Dim AniList As New List(Of AniData)
  182. If Not IsNothing(MainTextBlock) Then AniList.Add(AaColor(MainTextBlock, TextBlock.ForegroundProperty, "ColorBrush2", 90))
  183. If Not IsNothing(MainSwap) Then AniList.Add(AaColor(MainSwap, Shapes.Path.FillProperty, "ColorBrush2", 90))
  184. AniList.AddRange({
  185. AaColor(MainChrome, MyDropShadow.ColorProperty, "ColorObject4", 90),
  186. AaOpacity(MainChrome, DropShadowHoverOpacity - MainChrome.Opacity, 90)
  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 Return
  192. Dim AniList As New List(Of AniData)
  193. If Not IsNothing(MainTextBlock) Then AniList.Add(AaColor(MainTextBlock, TextBlock.ForegroundProperty, "ColorBrush1", 90))
  194. If Not IsNothing(MainSwap) Then AniList.Add(AaColor(MainSwap, Shapes.Path.FillProperty, "ColorBrush1", 90))
  195. AniList.AddRange({
  196. AaColor(MainChrome, MyDropShadow.ColorProperty, "ColorObject1", 90),
  197. AaOpacity(MainChrome, DropShadowIdleOpacity - MainChrome.Opacity, 90)
  198. })
  199. AniStart(AniList, "MyCard Mouse " & Uuid)
  200. End Sub
  201. #Region "高度改变动画"
  202. ''' <summary>
  203. ''' 是否启用高度改变动画。
  204. ''' </summary>
  205. Public Property UseAnimation As Boolean = True
  206. Private IsHeightAnimating As Boolean = False
  207. Private ActualUsedHeight As Double '回滚实际高度(例如 NaN)
  208. Private Sub MySizeChanged(sender As Object, e As SizeChangedEventArgs) Handles Me.SizeChanged
  209. If Not UseAnimation Then Return
  210. Dim DeltaHeight As Double = If(IsSwapped, SwapedHeight, e.NewSize.Height) - e.PreviousSize.Height
  211. '卡片的进入时动画已被页面通用切换动画替代
  212. If e.PreviousSize.Height = 0 OrElse IsHeightAnimating OrElse Math.Abs(DeltaHeight) < 1 OrElse ActualHeight = 0 Then Return
  213. StartHeightAnimation(DeltaHeight, e.PreviousSize.Height, False)
  214. End Sub
  215. Private Sub StartHeightAnimation(Delta As Double, PreviousHeight As Double, IsLoadAnimation As Boolean)
  216. If IsHeightAnimating OrElse FrmMain Is Nothing Then Return '避免 XAML 设计器出错
  217. Dim AnimList As New List(Of AniData)
  218. Dim AbsDelta = Math.Abs(Delta)
  219. If AbsDelta <= 800 Then
  220. '短距离,直接使用 150ms 的缓动动画
  221. AnimList.Add(AaHeight(Me, Delta, 150,, New AniEaseOutFluent(AniEasePower.ExtraStrong)))
  222. Else
  223. Dim EaseLength As Integer, EaseTime As Integer
  224. Dim InitSpeed As Integer '到达缓动区前的初速度
  225. If Delta < 0 AndAlso AbsDelta - EaseLength > 5000 * 0.1 Then
  226. '收回距离过长 (>0.1s),强制以 100ms 完成匀速段,然后让减速段更长
  227. EaseLength = 200
  228. EaseTime = 150
  229. InitSpeed = (AbsDelta - EaseLength) / 0.1
  230. ElseIf Delta > 0 AndAlso AbsDelta - EaseLength > 5000 * 0.6 Then
  231. '展开距离过长 (>0.6s),以 5000 速度展示 300ms 匀速段,剩下的距离全部归入减速段
  232. InitSpeed = 5000
  233. EaseLength = AbsDelta - InitSpeed * 0.3
  234. EaseTime = 400
  235. Else
  236. '中程,匀速地快速展开(或收回)
  237. EaseLength = 150
  238. EaseTime = 200
  239. InitSpeed = 4000
  240. End If
  241. '匀速段
  242. AnimList.Add(AaHeight(Me, (AbsDelta - EaseLength) * Math.Sign(Delta),
  243. (AbsDelta - EaseLength) / InitSpeed * 1000))
  244. '减速段
  245. AnimList.Add(AaHeight(Me, EaseLength * Math.Sign(Delta),
  246. EaseTime,, New AniEaseOutFluentWithInitial(InitSpeed, EaseTime / 1000, EaseLength), True))
  247. End If
  248. AnimList.Add(AaCode(
  249. Sub()
  250. IsHeightAnimating = False
  251. Height = ActualUsedHeight
  252. If IsSwapped AndAlso SwapControl IsNot Nothing Then SwapControl.Visibility = Visibility.Collapsed
  253. End Sub,, True))
  254. AniStart(AnimList, "MyCard Height " & Uuid)
  255. IsHeightAnimating = True
  256. ActualUsedHeight = If(IsSwapped, SwapedHeight, Height)
  257. Height = PreviousHeight
  258. End Sub
  259. ''' <summary>
  260. ''' 通知 MyCard,控件内容已改变,需要中断动画并更新高度。
  261. ''' </summary>
  262. Public Sub TriggerForceResize()
  263. Height = If(IsSwapped, SwapedHeight, Double.NaN)
  264. AniStop("MyCard Height " & Uuid)
  265. IsHeightAnimating = False
  266. End Sub
  267. #End Region
  268. #Region "折叠"
  269. '若设置了 CanSwap,或 SwapControl 不为空,则判定为会进行折叠
  270. '这是因为不能直接在 XAML 中设置 SwapControl
  271. Public SwapControl As Object
  272. Public Property CanSwap As Boolean = False
  273. ''' <summary>
  274. ''' 被折叠的种类,用于控件虚拟化。
  275. ''' </summary>
  276. Public Property SwapType As Integer
  277. ''' <summary>
  278. ''' 是否已被折叠。
  279. ''' </summary>
  280. Public Property IsSwapped As Boolean
  281. Get
  282. Return _IsSwapped
  283. End Get
  284. Set(value As Boolean)
  285. If _IsSwapped = value Then Return
  286. _IsSwapped = value
  287. If SwapControl Is Nothing Then Return
  288. '展开
  289. If Not IsSwapped AndAlso TypeOf SwapControl Is StackPanel Then StackInstall(SwapControl, SwapType, Title)
  290. '若尚未加载,会在 Loaded 事件中触发无动画的折叠,不需要在这里进行
  291. If Not IsLoaded Then Return
  292. '更新高度
  293. SwapControl.Visibility = Visibility.Visible
  294. TriggerForceResize()
  295. '改变箭头
  296. AniStart(AaRotateTransform(MainSwap, If(_IsSwapped, If(SwapLogoRight, 270, 0), 180) - CType(MainSwap.RenderTransform, RotateTransform).Angle, 250,, New AniEaseOutFluent(AniEasePower.ExtraStrong)), "MyCard Swap " & Uuid, True)
  297. End Set
  298. End Property
  299. Private _IsSwapped As Boolean = False
  300. ''' <summary>
  301. ''' 是否已被折叠。(已过时,请使用 IsSwapped)
  302. ''' </summary>
  303. <Obsolete("IsSwaped 存在拼写错误,请换用 IsSwapped 属性。")>
  304. Public Property IsSwaped As Boolean
  305. Get
  306. Return GetValue(IsSwapedProperty)
  307. End Get
  308. Set(value As Boolean)
  309. SetValue(IsSwapedProperty, value)
  310. End Set
  311. End Property
  312. Public Shared ReadOnly IsSwapedProperty As DependencyProperty = DependencyProperty.Register("IsSwaped", GetType(Boolean), GetType(MyCard), New PropertyMetadata(False,
  313. Sub(sender, e) If sender IsNot Nothing AndAlso TypeOf sender Is MyCard Then CType(sender, MyCard).IsSwapped = CType(e.NewValue, Boolean)))
  314. Public Property SwapLogoRight As Boolean = False
  315. Private IsMouseDown As Boolean = False
  316. Public Event PreviewSwap(sender As Object, e As RouteEventArgs)
  317. Public Event Swap(sender As Object, e As RouteEventArgs)
  318. Public Const SwapedHeight As Integer = 40
  319. Private Sub MyCard_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonDown
  320. Dim Pos As Double = Mouse.GetPosition(Me).Y
  321. If Not IsSwapped AndAlso
  322. (SwapControl Is Nothing OrElse Pos > If(IsSwapped, SwapedHeight, SwapedHeight - 6) OrElse (Pos = 0 AndAlso Not IsMouseDirectlyOver)) Then Return '检测点击位置;或已经不在可视树上的误判
  323. IsMouseDown = True
  324. End Sub
  325. Private Sub MyCard_MouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonUp
  326. If Not IsMouseDown Then Return
  327. IsMouseDown = False
  328. Dim Pos As Double = Mouse.GetPosition(Me).Y
  329. If Not IsSwapped AndAlso
  330. (SwapControl Is Nothing OrElse Pos > If(IsSwapped, SwapedHeight, SwapedHeight - 6) OrElse (Pos = 0 AndAlso Not IsMouseDirectlyOver)) Then Return '检测点击位置;或已经不在可视树上的误判
  331. Dim ee = New RouteEventArgs(True)
  332. RaiseEvent PreviewSwap(Me, ee)
  333. If ee.Handled Then
  334. IsMouseDown = False
  335. Return
  336. End If
  337. IsSwapped = Not IsSwapped
  338. Log("[Control] " & If(IsSwapped, "折叠卡片", "展开卡片") & If(Title Is Nothing, "", ":" & Title))
  339. RaiseEvent Swap(Me, ee)
  340. End Sub
  341. Private Sub MyCard_MouseLeave_Swap(sender As Object, e As MouseEventArgs) Handles Me.MouseLeave
  342. IsMouseDown = False
  343. End Sub
  344. #End Region
  345. End Class
  346. Partial Public Module ModAnimation
  347. Public Sub AniDispose(Control As MyCard, RemoveFromChildren As Boolean, Optional CallBack As ParameterizedThreadStart = Nothing)
  348. If Control.IsHitTestVisible Then
  349. Control.IsHitTestVisible = False
  350. AniStart({
  351. AaScaleTransform(Control, -0.08, 200,, New AniEaseInFluent),
  352. AaOpacity(Control, -1, 200,, New AniEaseOutFluent),
  353. AaHeight(Control, -Control.ActualHeight, 150, 100, New AniEaseOutFluent),
  354. AaCode(
  355. Sub()
  356. If RemoveFromChildren Then
  357. If Control.Parent Is Nothing Then Return
  358. CType(Control.Parent, Object).Children.Remove(Control)
  359. Else
  360. Control.Visibility = Visibility.Collapsed
  361. End If
  362. If CallBack IsNot Nothing Then CallBack(Control)
  363. End Sub,, True)
  364. }, "MyCard Dispose " & Control.Uuid)
  365. Else
  366. If RemoveFromChildren Then
  367. If Control.Parent Is Nothing Then Return
  368. CType(Control.Parent, Object).Children.Remove(Control)
  369. Else
  370. Control.Visibility = Visibility.Collapsed
  371. End If
  372. If CallBack IsNot Nothing Then CallBack(Control)
  373. End If
  374. End Sub
  375. End Module