A game framework written with osu! in mind.
at master 4.4 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using System; 5using System.Collections.Generic; 6using System.ComponentModel; 7using System.Linq; 8using System.Reflection; 9using osu.Framework.Graphics; 10using osu.Framework.Graphics.Containers; 11using osuTK.Graphics; 12using Container = osu.Framework.Graphics.Containers.Container; 13 14namespace osu.Framework.Testing.Drawables 15{ 16 internal abstract class TestButtonBase : ClickableContainer, IFilterable 17 { 18 public IEnumerable<string> FilterTerms => text.Children.OfType<IHasFilterTerms>().SelectMany(c => c.FilterTerms); 19 20 private bool matchingFilter = true; 21 22 public bool MatchingFilter 23 { 24 get => matchingFilter; 25 set 26 { 27 matchingFilter = value; 28 updateVisibility(); 29 } 30 } 31 32 public bool FilteringActive { get; set; } 33 34 private readonly Container content; 35 private readonly TextFlowContainer text; 36 public readonly Type TestType; 37 38 public const float LEFT_TEXT_PADDING = 16; 39 40 protected const float TRANSITION_DURATION = 100; 41 42 protected override Container<Drawable> Content => content; 43 44 private TestButtonBase() 45 { 46 AutoSizeAxes = Axes.Y; 47 RelativeSizeAxes = Axes.X; 48 49 Padding = new MarginPadding { Bottom = 3 }; 50 51 InternalChildren = new Drawable[] 52 { 53 new Container 54 { 55 RelativeSizeAxes = Axes.X, 56 AutoSizeAxes = Axes.Y, 57 Children = new Drawable[] 58 { 59 new SafeAreaContainer 60 { 61 SafeAreaOverrideEdges = Edges.Left, 62 RelativeSizeAxes = Axes.Both, 63 Child = content = new Container { RelativeSizeAxes = Axes.Both }, 64 }, 65 text = new TextFlowContainer(s => s.Font = FrameworkFont.Condensed.With(size: 14f)) 66 { 67 RelativeSizeAxes = Axes.X, 68 AutoSizeAxes = Axes.Y, 69 Padding = new MarginPadding 70 { 71 Top = 4, 72 Left = LEFT_TEXT_PADDING, 73 Right = 4 + LEFT_TEXT_PADDING, 74 Bottom = 5, 75 }, 76 } 77 } 78 }, 79 }; 80 } 81 82 protected TestButtonBase(Type test) 83 : this() 84 { 85 TestType = test; 86 text.AddText(TestScene.RemovePrefix(test.Name)); 87 88 var description = test.GetCustomAttribute<DescriptionAttribute>()?.Description; 89 90 if (description != null) 91 { 92 text.NewLine(); 93 text.AddText(description, t => 94 { 95 t.Font = t.Font.With("Roboto", 11); 96 t.Colour = FrameworkColour.Yellow; 97 }); 98 } 99 } 100 101 protected TestButtonBase(string header) 102 : this() 103 { 104 text.AddText(header); 105 } 106 107 private bool collapsed; 108 109 public virtual bool Collapsed 110 { 111 set 112 { 113 if (collapsed == value) return; 114 115 collapsed = value; 116 updateVisibility(); 117 } 118 } 119 120 private void updateVisibility() 121 { 122 if (FilteringActive) 123 { 124 if (matchingFilter) 125 Show(); 126 else 127 Hide(); 128 } 129 else 130 { 131 if (Current || !collapsed) 132 Show(); 133 else 134 Hide(); 135 } 136 } 137 138 private bool current; 139 140 public virtual bool Current 141 { 142 get => current; 143 set 144 { 145 if (current == value) 146 return; 147 148 current = value; 149 150 text.FadeColour(value ? Color4.Black : Color4.White, TRANSITION_DURATION); 151 updateVisibility(); 152 } 153 } 154 } 155}