A game framework written with osu! in mind.
at master 3.8 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.Collections.Generic; 5using NUnit.Framework; 6using osu.Framework.Graphics; 7using osu.Framework.Graphics.Containers; 8using osu.Framework.Graphics.Shapes; 9using osu.Framework.Graphics.Sprites; 10using osu.Framework.Input.Bindings; 11using osu.Framework.Testing; 12using osuTK; 13using osuTK.Graphics; 14using osuTK.Input; 15 16namespace osu.Framework.Tests.Visual.Input 17{ 18 public class TestSceneKeyBindingInputQueueChange : ManualInputManagerTestScene 19 { 20 [Test] 21 public void TestReleaseDoesNotTriggerWithoutPress() 22 { 23 TestInputReceptor shownReceptor = null; 24 TestInputReceptor hiddenReceptor = null; 25 26 AddStep("set children", () => 27 { 28 Child = new TestKeyBindingContainer 29 { 30 Children = new[] 31 { 32 shownReceptor = new TestInputReceptor("first") 33 { 34 Anchor = Anchor.Centre, 35 Origin = Anchor.Centre, 36 Size = new Vector2(100), 37 Colour = Color4.LightPink 38 }, 39 hiddenReceptor = new TestInputReceptor("second") 40 { 41 Anchor = Anchor.Centre, 42 Origin = Anchor.Centre, 43 Size = new Vector2(100), 44 Alpha = 0, 45 Colour = Color4.LightGreen 46 } 47 } 48 }; 49 }); 50 51 AddStep("click-hold shown receptor", () => 52 { 53 InputManager.MoveMouseTo(shownReceptor); 54 InputManager.PressButton(MouseButton.Left); 55 }); 56 AddStep("hide shown receptor", () => shownReceptor.Hide()); 57 AddStep("show hidden receptor", () => hiddenReceptor.Show()); 58 AddStep("release button", () => InputManager.ReleaseButton(MouseButton.Left)); 59 60 AddAssert("shown pressed", () => shownReceptor.Pressed); 61 AddAssert("shown released", () => shownReceptor.Released); 62 AddAssert("hidden not pressed", () => !hiddenReceptor.Pressed); 63 AddAssert("hidden not released", () => !hiddenReceptor.Released); 64 } 65 66 private class TestInputReceptor : CompositeDrawable, IKeyBindingHandler<TestAction> 67 { 68 public bool Pressed; 69 public bool Released; 70 71 public TestInputReceptor(string name) 72 { 73 InternalChildren = new Drawable[] 74 { 75 new Box { RelativeSizeAxes = Axes.Both }, 76 new SpriteText 77 { 78 Anchor = Anchor.Centre, 79 Origin = Anchor.Centre, 80 Colour = Color4.Black, 81 Text = name 82 } 83 }; 84 } 85 86 public bool OnPressed(TestAction action) 87 { 88 Pressed = true; 89 return true; 90 } 91 92 public void OnReleased(TestAction action) 93 { 94 Released = true; 95 } 96 } 97 98 private enum TestAction 99 { 100 Action1 101 } 102 103 private class TestKeyBindingContainer : KeyBindingContainer<TestAction> 104 { 105 public override IEnumerable<IKeyBinding> DefaultKeyBindings => new[] 106 { 107 new KeyBinding(InputKey.MouseLeft, TestAction.Action1) 108 }; 109 } 110 } 111}