A game framework written with osu! in mind.
at master 2.2 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.Diagnostics; 6using osu.Framework.Graphics; 7using osuTK.Graphics; 8 9namespace osu.Framework.Testing.Drawables.Steps 10{ 11 public class UntilStepButton : StepButton 12 { 13 private bool success; 14 15 private int invocations; 16 17 private const int max_attempt_milliseconds = 10000; 18 19 public override int RequiredRepetitions => success ? 0 : int.MaxValue; 20 21 public new Action Action; 22 23 private string text; 24 25 public new string Text 26 { 27 get => text; 28 set => base.Text = text = value; 29 } 30 31 private Stopwatch elapsedTime; 32 33 public UntilStepButton(Func<bool> waitUntilTrueDelegate, bool isSetupStep = false) 34 : base(isSetupStep) 35 { 36 updateText(); 37 LightColour = Color4.Sienna; 38 39 base.Action = () => 40 { 41 invocations++; 42 43 elapsedTime ??= Stopwatch.StartNew(); 44 45 updateText(); 46 47 if (waitUntilTrueDelegate()) 48 { 49 elapsedTime = null; 50 success = true; 51 Success(); 52 } 53 else if (!Debugger.IsAttached && elapsedTime.ElapsedMilliseconds >= max_attempt_milliseconds) 54 throw new TimeoutException($"\"{Text}\" timed out"); 55 56 Action?.Invoke(); 57 }; 58 } 59 60 public override void Reset() 61 { 62 base.Reset(); 63 64 invocations = 0; 65 elapsedTime = null; 66 success = false; 67 } 68 69 protected override void Success() 70 { 71 base.Success(); 72 Light.FadeColour(Color4.YellowGreen); 73 } 74 75 protected override void Failure() 76 { 77 base.Failure(); 78 Light.FadeColour(Color4.Red); 79 } 80 81 private void updateText() => base.Text = $@"{Text} ({invocations} tries)"; 82 83 public override string ToString() => "Repeat: " + base.ToString(); 84 } 85}