mirror of
https://github.com/ppy/osu.git
synced 2024-12-14 13:22:55 +08:00
Add basic implementation of a toggleable menu item
This commit is contained in:
parent
29672c48e1
commit
c3a3b4091b
@ -0,0 +1,33 @@
|
||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Game.Graphics.UserInterface;
|
||||
|
||||
namespace osu.Game.Tests.Visual.UserInterface
|
||||
{
|
||||
public class TestSceneToggleMenuItem : OsuTestScene
|
||||
{
|
||||
public override IReadOnlyList<Type> RequiredTypes => new[]
|
||||
{
|
||||
typeof(OsuMenu),
|
||||
typeof(ToggleMenuItem),
|
||||
typeof(DrawableToggleMenuItem)
|
||||
};
|
||||
|
||||
public TestSceneToggleMenuItem()
|
||||
{
|
||||
Add(new OsuMenu(Direction.Vertical, true)
|
||||
{
|
||||
Anchor = Anchor.Centre,
|
||||
Origin = Anchor.Centre,
|
||||
Items = new[]
|
||||
{
|
||||
new ToggleMenuItem("Toggle"),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -16,10 +16,10 @@ namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
public class DrawableOsuMenuItem : Menu.DrawableMenuItem
|
||||
{
|
||||
private const int margin_horizontal = 17;
|
||||
public const int MARGIN_HORIZONTAL = 17;
|
||||
public const int MARGIN_VERTICAL = 4;
|
||||
private const int text_size = 17;
|
||||
private const int transition_length = 80;
|
||||
public const int MARGIN_VERTICAL = 4;
|
||||
|
||||
private SampleChannel sampleClick;
|
||||
private SampleChannel sampleHover;
|
||||
@ -115,7 +115,7 @@ namespace osu.Game.Graphics.UserInterface
|
||||
Anchor = Anchor.CentreLeft,
|
||||
Origin = Anchor.CentreLeft,
|
||||
Font = OsuFont.GetFont(size: text_size),
|
||||
Margin = new MarginPadding { Horizontal = margin_horizontal, Vertical = MARGIN_VERTICAL },
|
||||
Margin = new MarginPadding { Horizontal = MARGIN_HORIZONTAL, Vertical = MARGIN_VERTICAL },
|
||||
},
|
||||
BoldText = new OsuSpriteText
|
||||
{
|
||||
@ -124,7 +124,7 @@ namespace osu.Game.Graphics.UserInterface
|
||||
Anchor = Anchor.CentreLeft,
|
||||
Origin = Anchor.CentreLeft,
|
||||
Font = OsuFont.GetFont(size: text_size, weight: FontWeight.Bold),
|
||||
Margin = new MarginPadding { Horizontal = margin_horizontal, Vertical = MARGIN_VERTICAL },
|
||||
Margin = new MarginPadding { Horizontal = MARGIN_HORIZONTAL, Vertical = MARGIN_VERTICAL },
|
||||
}
|
||||
};
|
||||
}
|
||||
|
60
osu.Game/Graphics/UserInterface/DrawableToggleMenuItem.cs
Normal file
60
osu.Game/Graphics/UserInterface/DrawableToggleMenuItem.cs
Normal file
@ -0,0 +1,60 @@
|
||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Sprites;
|
||||
using osuTK;
|
||||
|
||||
namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
public class DrawableToggleMenuItem : DrawableOsuMenuItem
|
||||
{
|
||||
protected new ToggleMenuItem Item => (ToggleMenuItem)base.Item;
|
||||
|
||||
public DrawableToggleMenuItem(ToggleMenuItem item)
|
||||
: base(item)
|
||||
{
|
||||
}
|
||||
|
||||
protected override TextContainer CreateTextContainer() => new ToggleTextContainer
|
||||
{
|
||||
State = { BindTarget = Item.State }
|
||||
};
|
||||
|
||||
private class ToggleTextContainer : TextContainer
|
||||
{
|
||||
public readonly Bindable<bool> State = new Bindable<bool>();
|
||||
|
||||
private readonly SpriteIcon checkmark;
|
||||
|
||||
public ToggleTextContainer()
|
||||
{
|
||||
Add(checkmark = new SpriteIcon
|
||||
{
|
||||
Anchor = Anchor.CentreLeft,
|
||||
Origin = Anchor.CentreLeft,
|
||||
Icon = FontAwesome.Solid.Check,
|
||||
Size = new Vector2(10),
|
||||
Margin = new MarginPadding { Horizontal = MARGIN_HORIZONTAL },
|
||||
AlwaysPresent = true,
|
||||
});
|
||||
}
|
||||
|
||||
protected override void LoadComplete()
|
||||
{
|
||||
base.LoadComplete();
|
||||
|
||||
State.BindValueChanged(state => checkmark.Alpha = state.NewValue ? 1 : 0, true);
|
||||
}
|
||||
|
||||
protected override void Update()
|
||||
{
|
||||
base.Update();
|
||||
|
||||
// Todo: This is bad. This can maybe be done better with a refactor of DrawableOsuMenuItem.
|
||||
checkmark.X = BoldText.DrawWidth + 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -39,7 +39,16 @@ namespace osu.Game.Graphics.UserInterface
|
||||
}
|
||||
}
|
||||
|
||||
protected override DrawableMenuItem CreateDrawableMenuItem(MenuItem item) => new DrawableOsuMenuItem(item);
|
||||
protected override DrawableMenuItem CreateDrawableMenuItem(MenuItem item)
|
||||
{
|
||||
switch (item)
|
||||
{
|
||||
case ToggleMenuItem toggle:
|
||||
return new DrawableToggleMenuItem(toggle);
|
||||
}
|
||||
|
||||
return new DrawableOsuMenuItem(item);
|
||||
}
|
||||
|
||||
protected override ScrollContainer<Drawable> CreateScrollContainer(Direction direction) => new OsuScrollContainer(direction);
|
||||
|
||||
|
25
osu.Game/Graphics/UserInterface/ToggleMenuItem.cs
Normal file
25
osu.Game/Graphics/UserInterface/ToggleMenuItem.cs
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using System;
|
||||
using osu.Framework.Bindables;
|
||||
|
||||
namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
public class ToggleMenuItem : OsuMenuItem
|
||||
{
|
||||
public readonly BindableBool State = new BindableBool();
|
||||
|
||||
public ToggleMenuItem(string text, MenuItemType type = MenuItemType.Standard)
|
||||
: this(text, type, null)
|
||||
{
|
||||
}
|
||||
|
||||
public ToggleMenuItem(string text, MenuItemType type, Action<bool> action)
|
||||
: base(text, type)
|
||||
{
|
||||
Action.Value = () => State.Toggle();
|
||||
State.BindValueChanged(state => action?.Invoke(state.NewValue));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user