1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-09 13:13:04 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/OsuContextMenu.cs

165 lines
6.0 KiB
C#
Raw Normal View History

2017-06-12 17:56:07 +08:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2017-08-28 13:42:52 +08:00
using System;
2017-06-12 17:56:07 +08:00
using OpenTK.Graphics;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2017-06-12 17:56:07 +08:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2017-08-28 11:33:31 +08:00
using osu.Framework.Graphics.Sprites;
2017-06-12 17:56:07 +08:00
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input;
using osu.Game.Graphics.Sprites;
2017-06-12 17:56:07 +08:00
namespace osu.Game.Graphics.UserInterface
{
2017-08-28 13:42:52 +08:00
public class OsuContextMenu : OsuMenu
2017-06-12 17:56:07 +08:00
{
private const int fade_duration = 250;
2017-06-12 17:56:07 +08:00
public OsuContextMenu()
2017-06-12 17:56:07 +08:00
{
CornerRadius = 5;
EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(0.1f),
Radius = 4,
};
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
BackgroundColour = colours.ContextMenuGray;
}
protected override void AnimateOpen() => this.FadeIn(fade_duration, Easing.OutQuint);
protected override void AnimateClose() => this.FadeOut(fade_duration, Easing.OutQuint);
2017-08-25 17:41:12 +08:00
protected override MarginPadding ItemFlowContainerPadding => new MarginPadding { Vertical = DrawableOsuContextMenuItem.MARGIN_VERTICAL };
2017-08-28 13:42:52 +08:00
protected override DrawableMenuItem CreateDrawableMenuItem(MenuItem item) => new DrawableOsuContextMenuItem(item);
2017-08-25 17:41:12 +08:00
#region DrawableOsuContextMenuItem
private class DrawableOsuContextMenuItem : DrawableMenuItem
{
private const int margin_horizontal = 17;
private const int text_size = 17;
private const int transition_length = 80;
public const int MARGIN_VERTICAL = 4;
private SampleChannel sampleClick;
private SampleChannel sampleHover;
2017-08-28 11:33:31 +08:00
private TextContainer text;
2017-06-12 17:56:07 +08:00
2017-08-28 13:42:52 +08:00
public DrawableOsuContextMenuItem(MenuItem item)
2017-08-25 17:41:12 +08:00
: base(item)
2017-06-12 17:56:07 +08:00
{
2017-08-28 13:42:52 +08:00
if (!(Item is OsuMenuItem))
throw new ArgumentException($"{nameof(item)} must be a {nameof(OsuMenuItem)}.");
2017-06-12 17:56:07 +08:00
}
[BackgroundDependencyLoader]
private void load(AudioManager audio)
2017-06-12 17:56:07 +08:00
{
sampleHover = audio.Sample.Get(@"UI/generic-hover");
sampleClick = audio.Sample.Get(@"UI/generic-click");
BackgroundColour = Color4.Transparent;
BackgroundColourHover = OsuColour.FromHex(@"172023");
updateTextColour();
2017-06-12 17:56:07 +08:00
}
private void updateTextColour()
{
2017-08-28 13:42:52 +08:00
switch (((OsuMenuItem)Item).Type)
{
case MenuItemType.Standard:
2017-08-28 11:33:31 +08:00
text.Colour = Color4.White;
break;
case MenuItemType.Destructive:
2017-08-28 11:33:31 +08:00
text.Colour = Color4.Red;
break;
case MenuItemType.Highlighted:
2017-08-28 11:33:31 +08:00
text.Colour = OsuColour.FromHex(@"ffcc22");
break;
}
}
2017-06-12 18:39:45 +08:00
protected override bool OnHover(InputState state)
2017-06-12 18:39:45 +08:00
{
sampleHover.Play();
2017-08-28 11:33:31 +08:00
text.BoldText.FadeIn(transition_length, Easing.OutQuint);
text.NormalText.FadeOut(transition_length, Easing.OutQuint);
return base.OnHover(state);
2017-06-12 18:39:45 +08:00
}
protected override void OnHoverLost(InputState state)
{
2017-08-28 11:33:31 +08:00
text.BoldText.FadeOut(transition_length, Easing.OutQuint);
text.NormalText.FadeIn(transition_length, Easing.OutQuint);
base.OnHoverLost(state);
}
protected override bool OnClick(InputState state)
{
sampleClick.Play();
return base.OnClick(state);
}
2017-08-28 11:33:31 +08:00
protected override Drawable CreateContent() => text = new TextContainer();
private class TextContainer : Container, IHasText
{
2017-08-28 11:33:31 +08:00
public string Text
{
get { return NormalText.Text; }
set
{
NormalText.Text = value;
BoldText.Text = value;
}
}
public readonly SpriteText NormalText;
public readonly SpriteText BoldText;
public TextContainer()
{
2017-08-28 11:33:31 +08:00
Anchor = Anchor.CentreLeft;
Origin = Anchor.CentreLeft;
AutoSizeAxes = Axes.Both;
Children = new Drawable[]
{
2017-08-28 11:33:31 +08:00
NormalText = new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
TextSize = text_size,
Margin = new MarginPadding { Horizontal = margin_horizontal, Vertical = MARGIN_VERTICAL },
},
2017-08-28 11:33:31 +08:00
BoldText = new OsuSpriteText
{
AlwaysPresent = true,
Alpha = 0,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
TextSize = text_size,
Font = @"Exo2.0-Bold",
Margin = new MarginPadding { Horizontal = margin_horizontal, Vertical = MARGIN_VERTICAL },
}
2017-08-28 11:33:31 +08:00
};
}
}
2017-06-12 17:56:07 +08:00
}
#endregion
2017-06-12 17:56:07 +08:00
}
}