A game framework written with osu! in mind.
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 NUnit.Framework;
5using osu.Framework.Graphics;
6using osu.Framework.Graphics.UserInterface;
7using osu.Framework.Input.Events;
8using osu.Framework.Testing;
9using osuTK.Input;
10
11namespace osu.Framework.Tests.Visual.UserInterface
12{
13 public class TestSceneClosableMenu : MenuTestScene
14 {
15 protected override Menu CreateMenu() => new AnimatedMenu(Direction.Vertical)
16 {
17 Anchor = Anchor.Centre,
18 Origin = Anchor.Centre,
19 State = MenuState.Open,
20 Items = new[]
21 {
22 new MenuItem("Item #1")
23 {
24 Items = new[]
25 {
26 new MenuItem("Sub-item #1"),
27 new MenuItem("Sub-item #2"),
28 }
29 },
30 new MenuItem("Item #2")
31 {
32 Items = new[]
33 {
34 new MenuItem("Sub-item #1"),
35 new MenuItem("Sub-item #2"),
36 }
37 },
38 }
39 };
40
41 [Test]
42 public void TestClickItemClosesMenus()
43 {
44 AddStep("click item", () => ClickItem(0, 0));
45 AddStep("click item", () => ClickItem(1, 0));
46 AddAssert("all menus closed", () =>
47 {
48 for (int i = 1; i >= 0; --i)
49 {
50 if (Menus.GetSubMenu(i).State == MenuState.Open)
51 return false;
52 }
53
54 return true;
55 });
56 }
57
58 [Test]
59 public void TestMenuIgnoresEscapeWhenClosed()
60 {
61 AnimatedMenu menu = null;
62
63 AddStep("find menu", () => menu = (AnimatedMenu)Menus.GetSubMenu(0));
64 AddStep("press escape", () => InputManager.Key(Key.Escape));
65 AddAssert("press handled", () => menu.PressBlocked);
66 AddStep("reset flag", () => menu.PressBlocked = false);
67 AddStep("press escape again", () => InputManager.Key(Key.Escape));
68 AddAssert("press not handled", () => !menu.PressBlocked);
69 }
70
71 private class AnimatedMenu : BasicMenu
72 {
73 public bool PressBlocked { get; set; }
74
75 public AnimatedMenu(Direction direction)
76 : base(direction)
77 {
78 }
79
80 protected override bool OnKeyDown(KeyDownEvent e)
81 {
82 return PressBlocked = base.OnKeyDown(e);
83 }
84
85 protected override void AnimateOpen() => this.FadeIn(500);
86
87 protected override void AnimateClose() => this.FadeOut(5000); // Ensure escape is pressed while menu is still fading
88
89 protected override Menu CreateSubMenu() => new AnimatedMenu(Direction);
90 }
91 }
92}