|
| 1 | +// Copyright (c) Shane Woolcock. Licensed under the MIT Licence. |
| 2 | +// See the LICENCE file in the repository root for full licence text. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Linq; |
| 6 | +using NUnit.Framework; |
| 7 | +using osu.Framework.Allocation; |
| 8 | +using osu.Framework.Graphics; |
| 9 | +using osu.Framework.Graphics.Containers; |
| 10 | +using osu.Framework.Graphics.Shapes; |
| 11 | +using osu.Framework.Input; |
| 12 | +using osu.Game.Rulesets.Rush.Input; |
| 13 | +using osu.Game.Screens.Play; |
| 14 | +using osu.Game.Tests.Visual; |
| 15 | +using osuTK; |
| 16 | +using osuTK.Graphics; |
| 17 | + |
| 18 | +namespace osu.Game.Rulesets.Rush.Tests.Visual |
| 19 | +{ |
| 20 | + public class TestSceneTouchInputHandling : OsuManualInputManagerTestScene |
| 21 | + { |
| 22 | + protected override Ruleset CreateRuleset() => new RushRuleset(); |
| 23 | + |
| 24 | + private static readonly TouchSource[] touch_sources = (TouchSource[])Enum.GetValues(typeof(TouchSource)); |
| 25 | + |
| 26 | + private TouchRegion airRegion; |
| 27 | + private TouchRegion groundRegion; |
| 28 | + private TouchRegion feverRegion; |
| 29 | + |
| 30 | + private KeyCounterDisplay keyCounters; |
| 31 | + private RushInputManager rushInputManager; |
| 32 | + |
| 33 | + [BackgroundDependencyLoader] |
| 34 | + private void load() |
| 35 | + { |
| 36 | + Children = new Drawable[] |
| 37 | + { |
| 38 | + airRegion = new TouchRegion |
| 39 | + { |
| 40 | + RelativeSizeAxes = Axes.Both, |
| 41 | + Anchor = Anchor.TopCentre, |
| 42 | + Origin = Anchor.TopCentre, |
| 43 | + Size = new Vector2(1,0.5f), |
| 44 | + |
| 45 | + Action = RushActionTarget.Air, |
| 46 | + Colour = Color4.Aqua, |
| 47 | + Alpha = 0.8f, |
| 48 | + }, |
| 49 | + groundRegion = new TouchRegion |
| 50 | + { |
| 51 | + RelativeSizeAxes = Axes.Both, |
| 52 | + Anchor = Anchor.BottomCentre, |
| 53 | + Origin = Anchor.BottomCentre, |
| 54 | + Size = new Vector2(1,0.5f), |
| 55 | + |
| 56 | + Action = RushActionTarget.Ground, |
| 57 | + Colour = Color4.Red, |
| 58 | + Alpha = 0.8f, |
| 59 | + }, |
| 60 | + feverRegion = new TouchRegion |
| 61 | + { |
| 62 | + RelativeSizeAxes = Axes.Both, |
| 63 | + Anchor = Anchor.Centre, |
| 64 | + Origin = Anchor.Centre, |
| 65 | + Size = new Vector2(0.25f, 0.25f), |
| 66 | + |
| 67 | + Action = RushActionTarget.Fever, |
| 68 | + Colour = Color4.Purple, |
| 69 | + Alpha = 0.8f, |
| 70 | + }, |
| 71 | + keyCounters = new KeyCounterDisplay |
| 72 | + { |
| 73 | + Origin = Anchor.BottomRight, |
| 74 | + Anchor = Anchor.BottomRight, |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + rushInputManager = new RushInputManager(Ruleset.Value); |
| 79 | + rushInputManager.Attach(keyCounters); |
| 80 | + |
| 81 | + var tmpChild = InputManager.Child; |
| 82 | + InputManager.Clear(false); |
| 83 | + |
| 84 | + InputManager.Child = rushInputManager.WithChild(tmpChild); |
| 85 | + } |
| 86 | + |
| 87 | + [Test] |
| 88 | + public void TestPreferEarliestFreeAction() |
| 89 | + { |
| 90 | + |
| 91 | + AddStep("Touch ground area (1)", () => touchDrawable(TouchSource.Touch1, groundRegion)); |
| 92 | + AddStep("Touch ground area (2)", () => touchDrawable(TouchSource.Touch2, groundRegion)); |
| 93 | + AddAssert("B1 on, B2 on", () => inputStateOnlyContains(RushAction.GroundPrimary, RushAction.GroundSecondary)); |
| 94 | + |
| 95 | + AddStep("Release ground area (1)", () => endTouch(TouchSource.Touch1)); |
| 96 | + AddAssert("B1 off, B2 on", () => inputStateOnlyContains(RushAction.GroundSecondary)); |
| 97 | + |
| 98 | + AddStep("Touch ground area (3)", () => touchDrawable(TouchSource.Touch3, groundRegion)); |
| 99 | + AddAssert("B1 on, B2 on", () => inputStateOnlyContains(RushAction.GroundPrimary, RushAction.GroundSecondary)); |
| 100 | + |
| 101 | + AddStep("Touch ground area (4)", () => touchDrawable(TouchSource.Touch4, groundRegion)); |
| 102 | + AddAssert("B1 on, B2 on, B3 on", () => inputStateOnlyContains(RushAction.GroundPrimary, RushAction.GroundSecondary, RushAction.GroundTertiary)); |
| 103 | + |
| 104 | + AddStep("Release ground area (2)", () => endTouch(TouchSource.Touch2)); |
| 105 | + AddAssert("B1 on, B2 off, B3 on", () => inputStateOnlyContains(RushAction.GroundPrimary, RushAction.GroundTertiary)); |
| 106 | + |
| 107 | + AddStep("Touch ground area (5)", () => touchDrawable(TouchSource.Touch5, groundRegion)); |
| 108 | + AddAssert("B1 on, B2 on, B3 on", () => inputStateOnlyContains(RushAction.GroundPrimary, RushAction.GroundSecondary, RushAction.GroundTertiary)); |
| 109 | + } |
| 110 | + |
| 111 | + [Test] |
| 112 | + public void TestIgnoreExcessiveTouches() |
| 113 | + { |
| 114 | + AddStep("Touch ground area (1)", () => touchDrawable(TouchSource.Touch1, groundRegion)); |
| 115 | + AddStep("Touch ground area (2)", () => touchDrawable(TouchSource.Touch2, groundRegion)); |
| 116 | + AddStep("Touch ground area (3)", () => touchDrawable(TouchSource.Touch3, groundRegion)); |
| 117 | + AddStep("Touch ground area (4)", () => touchDrawable(TouchSource.Touch4, groundRegion)); |
| 118 | + AddAssert("All ground actions on", () => inputStateOnlyContains(RushAction.GroundPrimary, RushAction.GroundSecondary, RushAction.GroundTertiary, RushAction.GroundQuaternary)); |
| 119 | + |
| 120 | + AddStep("Touch ground area (5)", () => touchDrawable(TouchSource.Touch5, groundRegion)); |
| 121 | + AddAssert("Only ground actions on", () => inputStateOnlyContains(RushAction.GroundPrimary, RushAction.GroundSecondary, RushAction.GroundTertiary, RushAction.GroundQuaternary)); |
| 122 | + } |
| 123 | + |
| 124 | + [Test] |
| 125 | + public void TestTouchConversionBlocking() |
| 126 | + { |
| 127 | + AddStep("Touch Fever region", () => touchDrawableWithOffset(TouchSource.Touch1, feverRegion, new Vector2(0, 10))); |
| 128 | + |
| 129 | + AddAssert("Only fever action on", () => inputStateOnlyContains(RushAction.Fever)); |
| 130 | + } |
| 131 | + |
| 132 | + [Test] |
| 133 | + public void TestReleaseAfterSwitchingRegions() |
| 134 | + { |
| 135 | + AddStep("Touch ground region", () => touchDrawable(TouchSource.Touch1, groundRegion)); |
| 136 | + AddStep("Move to air region", () => moveTouchToDrawable(TouchSource.Touch1, airRegion)); |
| 137 | + AddAssert("Ground action still held", () => inputStateOnlyContains(RushAction.GroundPrimary)); |
| 138 | + AddStep("Release touch", () => endTouch(TouchSource.Touch1)); |
| 139 | + AddAssert("Ground action released", () => inputStateOnlyContains()); |
| 140 | + } |
| 141 | + |
| 142 | + [SetUp] |
| 143 | + public void ReleaseAllTouches() |
| 144 | + { |
| 145 | + foreach (var source in touch_sources) |
| 146 | + InputManager.EndTouch(new Touch(source, Vector2.Zero)); |
| 147 | + } |
| 148 | + |
| 149 | + private bool inputStateOnlyContains(params RushAction[] actions) => rushInputManager.PressedActions.ToHashSet().SetEquals(actions); |
| 150 | + |
| 151 | + private void touchDrawable(TouchSource source, Drawable drawable) => InputManager.BeginTouch(new Touch(source, drawable.ScreenSpaceDrawQuad.Centre)); |
| 152 | + private void touchDrawableWithOffset(TouchSource source, Drawable drawable, Vector2 offset) => InputManager.BeginTouch(new Touch(source, drawable.ScreenSpaceDrawQuad.Centre + offset)); |
| 153 | + private void moveTouchToDrawable(TouchSource source, Drawable drawable) => InputManager.MoveTouchTo(new Touch(source, drawable.ScreenSpaceDrawQuad.Centre)); |
| 154 | + private void endTouch(TouchSource source) => InputManager.EndTouch(new Touch(source, Vector2.Zero)); |
| 155 | + |
| 156 | + private class TouchRegion : Box, IKeyBindingTouchHandler |
| 157 | + { |
| 158 | + public override bool HandlePositionalInput => true; |
| 159 | + |
| 160 | + public RushActionTarget Action; |
| 161 | + |
| 162 | + public RushActionTarget ActionTargetForTouchPosition(Vector2 screenSpaceTouchPos) => Action; |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments