osu/osu.Game/Graphics/UserInterface/OsuContextMenuItem.cs

114 lines
3.9 KiB
C#
Raw Normal View History

2017-06-12 09:56:07 +00:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using OpenTK.Graphics;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input;
using osu.Game.Graphics.Sprites;
namespace osu.Game.Graphics.UserInterface
{
public class OsuContextMenuItem : ContextMenuItem
{
2017-06-13 06:27:47 +00:00
private const int transition_length = 80;
2017-06-12 09:56:07 +00:00
private const int margin_horizontal = 17;
public const int MARGIN_VERTICAL = 4;
private const int text_size = 17;
private OsuSpriteText text;
private OsuSpriteText textBold;
private SampleChannel sampleClick;
private SampleChannel sampleHover;
2017-06-13 06:20:21 +00:00
private readonly MenuItemType type;
2017-06-12 09:56:07 +00:00
protected override Container CreateTextContainer(string title) => new Container
{
AutoSizeAxes = Axes.Both,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Children = new Drawable[]
{
text = new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
TextSize = text_size,
Text = title,
Margin = new MarginPadding { Horizontal = margin_horizontal, Vertical = MARGIN_VERTICAL },
},
textBold = new OsuSpriteText
{
AlwaysPresent = true,
Alpha = 0,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
TextSize = text_size,
Text = title,
Font = @"Exo2.0-Bold",
Margin = new MarginPadding { Horizontal = margin_horizontal, Vertical = MARGIN_VERTICAL },
}
}
};
2017-06-13 06:20:21 +00:00
public OsuContextMenuItem(string title, MenuItemType type = MenuItemType.Standard) : base(title)
2017-06-12 09:56:07 +00:00
{
this.type = type;
}
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
sampleHover = audio.Sample.Get(@"UI/generic-hover");
sampleClick = audio.Sample.Get(@"UI/generic-click");
2017-06-12 09:56:07 +00:00
BackgroundColour = Color4.Transparent;
BackgroundColourHover = OsuColour.FromHex(@"172023");
updateTextColour();
}
private void updateTextColour()
{
switch (type)
{
2017-06-13 06:20:21 +00:00
case MenuItemType.Standard:
2017-06-12 09:56:07 +00:00
textBold.Colour = text.Colour = Color4.White;
break;
2017-06-13 06:20:21 +00:00
case MenuItemType.Destructive:
2017-06-12 09:56:07 +00:00
textBold.Colour = text.Colour = Color4.Red;
break;
2017-06-13 06:20:21 +00:00
case MenuItemType.Highlighted:
2017-06-12 09:56:07 +00:00
textBold.Colour = text.Colour = OsuColour.FromHex(@"ffcc22");
break;
}
}
protected override bool OnHover(InputState state)
{
sampleHover.Play();
2017-07-22 18:50:25 +00:00
textBold.FadeIn(transition_length, Easing.OutQuint);
text.FadeOut(transition_length, Easing.OutQuint);
2017-06-12 09:56:07 +00:00
return base.OnHover(state);
}
protected override void OnHoverLost(InputState state)
{
2017-07-22 18:50:25 +00:00
textBold.FadeOut(transition_length, Easing.OutQuint);
text.FadeIn(transition_length, Easing.OutQuint);
2017-06-12 09:56:07 +00:00
base.OnHoverLost(state);
}
protected override bool OnClick(InputState state)
{
sampleClick.Play();
return base.OnClick(state);
}
}
}