1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 03:02:53 +08:00
osu-lazer/osu.Game/Overlays/Mods/ModButton.cs

263 lines
8.2 KiB
C#
Raw Normal View History

2017-03-08 14:50:52 +08:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
2017-02-17 04:05:03 +08:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2017-02-23 10:16:23 +08:00
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Input;
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.Sprites;
2017-02-28 14:25:38 +08:00
using osu.Framework.Input;
2017-02-23 10:16:23 +08:00
using osu.Game.Graphics.Sprites;
2017-04-18 15:05:58 +08:00
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.UI;
using System;
using System.Linq;
using osu.Framework.Graphics.Cursor;
2017-02-23 00:22:29 +08:00
2017-02-23 10:16:23 +08:00
namespace osu.Game.Overlays.Mods
{
/// <summary>
/// Represents a clickable button which can cycle through one of more mods.
/// </summary>
2017-05-20 10:56:48 +08:00
public class ModButton : ModButtonEmpty, IHasTooltip
2017-02-23 10:16:23 +08:00
{
2017-05-08 18:56:04 +08:00
private ModIcon foregroundIcon;
2017-05-26 22:10:28 +08:00
private ModIcon backgroundIcon;
private readonly SpriteText text;
private readonly Container<ModIcon> iconsContainer;
2017-02-23 10:16:23 +08:00
private SampleChannel sampleOn, sampleOff;
public Action<Mod> Action; // Passed the selected mod or null if none
2017-05-20 14:22:37 +08:00
public string TooltipText => (SelectedMod?.Description ?? Mods.FirstOrDefault()?.Description) ?? string.Empty;
2017-05-20 10:56:48 +08:00
private const EasingTypes mod_switch_easing = EasingTypes.InOutSine;
private const double mod_switch_duration = 120;
2017-05-26 22:10:28 +08:00
// A selected index of -1 means not selected.
private int selectedIndex = -1;
protected int SelectedIndex
2017-02-23 10:16:23 +08:00
{
get
{
return selectedIndex;
2017-02-23 10:16:23 +08:00
}
set
{
if (value == selectedIndex) return;
2017-05-26 22:10:28 +08:00
int direction = value < selectedIndex ? -1 : 1;
bool beforeSelected = Selected;
Mod modBefore = SelectedMod ?? Mods[0];
2017-02-17 04:05:03 +08:00
if (value >= Mods.Length)
selectedIndex = -1;
else if (value < -1)
selectedIndex = Mods.Length - 1;
else
selectedIndex = value;
Mod modAfter = SelectedMod ?? Mods[0];
if (beforeSelected != Selected)
2017-05-26 22:10:28 +08:00
{
iconsContainer.RotateTo(Selected ? 5f : 0f, 300, EasingTypes.OutElastic);
iconsContainer.ScaleTo(Selected ? 1.1f : 1f, 300, EasingTypes.OutElastic);
}
if (modBefore != modAfter)
2017-05-26 22:10:28 +08:00
{
const float rotate_angle = 16;
foregroundIcon.RotateTo(rotate_angle * direction, mod_switch_duration, mod_switch_easing);
backgroundIcon.RotateTo(-rotate_angle * direction, mod_switch_duration, mod_switch_easing);
backgroundIcon.Icon = modAfter.Icon;
2017-06-08 13:51:22 +08:00
using (BeginDelayedSequence(mod_switch_duration, true))
2017-05-26 22:10:28 +08:00
{
foregroundIcon.RotateTo(-rotate_angle * direction);
2017-05-26 22:29:15 +08:00
foregroundIcon.RotateTo(0f, mod_switch_duration, mod_switch_easing);
2017-05-27 15:52:02 +08:00
backgroundIcon.RotateTo(rotate_angle * direction);
2017-05-26 22:29:15 +08:00
backgroundIcon.RotateTo(0f, mod_switch_duration, mod_switch_easing);
2017-06-08 13:51:22 +08:00
Schedule(() => displayMod(modAfter));
2017-05-26 22:10:28 +08:00
}
}
2017-02-23 10:16:23 +08:00
foregroundIcon.Highlighted = Selected;
2017-02-23 10:16:23 +08:00
}
}
public bool Selected => SelectedIndex != -1;
2017-02-23 10:16:23 +08:00
private Color4 selectedColour;
public Color4 SelectedColour
{
get
{
return selectedColour;
}
set
{
if (value == selectedColour) return;
selectedColour = value;
2017-03-06 19:03:26 +08:00
if (Selected) foregroundIcon.Colour = value;
2017-02-23 10:16:23 +08:00
}
}
private Mod mod;
public Mod Mod
2017-02-23 10:16:23 +08:00
{
get
{
return mod;
2017-02-23 10:16:23 +08:00
}
set
{
mod = value;
if (mod == null)
{
Mods = new Mod[0];
Alpha = 0;
}
else
{
Mods = (mod as MultiMod)?.Mods ?? new[] { mod };
Alpha = 1;
}
2017-02-23 10:16:23 +08:00
createIcons();
2017-03-07 09:59:19 +08:00
if (Mods.Length > 0)
2017-02-23 10:16:23 +08:00
{
2017-03-07 09:59:19 +08:00
displayMod(Mods[0]);
2017-02-23 10:16:23 +08:00
}
}
}
2017-03-07 09:59:19 +08:00
public Mod[] Mods { get; private set; }
// the mods from Mod, only multiple if Mod is a MultiMod
public override Mod SelectedMod => Mods.ElementAtOrDefault(SelectedIndex);
2017-02-23 10:16:23 +08:00
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
2017-07-02 19:16:22 +08:00
sampleOn = audio.Sample.Get(@"UI/check-on");
sampleOff = audio.Sample.Get(@"UI/check-off");
2017-02-23 10:16:23 +08:00
}
2017-02-17 04:05:03 +08:00
2017-02-28 14:25:38 +08:00
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
2017-02-23 10:16:23 +08:00
{
switch (args.Button)
{
case MouseButton.Left:
SelectNext();
break;
case MouseButton.Right:
SelectPrevious();
break;
}
2017-02-17 04:05:03 +08:00
return true;
2017-02-23 10:16:23 +08:00
}
public void SelectNext()
{
2017-07-02 19:33:42 +08:00
(++SelectedIndex == Mods.Length ? sampleOff : sampleOn).Play();
2017-02-23 10:16:23 +08:00
Action?.Invoke(SelectedMod);
}
public void SelectPrevious()
{
(--SelectedIndex == -1 ? sampleOff : sampleOn).Play();
2017-02-23 10:16:23 +08:00
Action?.Invoke(SelectedMod);
}
public void Deselect()
{
SelectedIndex = -1;
2017-02-23 10:16:23 +08:00
}
private void displayMod(Mod mod)
{
2017-05-29 17:10:02 +08:00
if (backgroundIcon != null)
2017-05-26 22:10:28 +08:00
backgroundIcon.Icon = foregroundIcon.Icon;
2017-03-06 19:03:26 +08:00
foregroundIcon.Icon = mod.Icon;
text.Text = mod.Name;
2017-02-23 10:16:23 +08:00
}
private void createIcons()
{
2017-03-06 19:03:26 +08:00
iconsContainer.Clear();
2017-02-23 10:16:23 +08:00
if (Mods.Length > 1)
{
2017-03-09 16:44:45 +08:00
iconsContainer.Add(new[]
2017-02-23 10:16:23 +08:00
{
2017-05-26 22:10:28 +08:00
backgroundIcon = new ModIcon(Mods[1])
2017-02-23 10:16:23 +08:00
{
2017-05-26 22:10:28 +08:00
Origin = Anchor.BottomRight,
Anchor = Anchor.BottomRight,
2017-02-23 10:16:23 +08:00
AutoSizeAxes = Axes.Both,
Position = new Vector2(1.5f),
},
2017-05-03 14:48:10 +08:00
foregroundIcon = new ModIcon(Mods[0])
2017-02-23 10:16:23 +08:00
{
2017-05-26 22:10:28 +08:00
Origin = Anchor.BottomRight,
Anchor = Anchor.BottomRight,
2017-03-06 19:03:26 +08:00
AutoSizeAxes = Axes.Both,
2017-02-23 10:16:23 +08:00
Position = new Vector2(-1.5f),
},
});
}
else
{
2017-05-03 14:48:10 +08:00
iconsContainer.Add(foregroundIcon = new ModIcon(Mod)
2017-02-23 10:16:23 +08:00
{
2017-03-06 19:03:26 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
AutoSizeAxes = Axes.Both,
2017-02-23 10:16:23 +08:00
});
}
}
public ModButton(Mod mod)
2017-03-07 00:46:36 +08:00
{
2017-02-23 10:16:23 +08:00
Children = new Drawable[]
{
new Container
{
Size = new Vector2(77f, 80f),
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Children = new Drawable[]
{
2017-03-06 19:03:26 +08:00
iconsContainer = new Container<ModIcon>
2017-02-23 10:16:23 +08:00
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
}
}
},
text = new OsuSpriteText
{
Y = 75,
2017-02-23 10:16:23 +08:00
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
TextSize = 18,
},
};
Mod = mod;
2017-02-23 10:16:23 +08:00
}
}
}