1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-26 05:12:57 +08:00
osu-lazer/osu.Game/Overlays/Mods/ModButton.cs

287 lines
9.1 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-11-26 02:28:11 +08:00
using osu.Game.Graphics.UserInterface;
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;
/// <summary>
/// Fired when the selection changes.
/// </summary>
public Action<Mod> SelectionChanged;
2017-02-23 10:16:23 +08:00
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
2017-07-23 02:50:25 +08:00
private const Easing mod_switch_easing = Easing.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;
/// <summary>
/// Change the selected mod index of this button.
/// </summary>
2018-01-03 12:12:49 +08:00
/// <param name="newIndex">The new index.</param>
/// <returns>Whether the selection changed.</returns>
2018-01-02 15:55:03 +08:00
private bool changeSelectedIndex(int newIndex)
2017-02-23 10:16:23 +08:00
{
2018-01-02 15:55:03 +08:00
if (newIndex == selectedIndex) return false;
2017-05-26 22:10:28 +08:00
2018-01-02 15:55:03 +08:00
int direction = newIndex < selectedIndex ? -1 : 1;
bool beforeSelected = Selected;
Mod modBefore = SelectedMod ?? Mods[0];
2017-02-17 04:05:03 +08:00
2018-01-02 15:55:03 +08:00
if (newIndex >= Mods.Length)
newIndex = -1;
else if (newIndex < -1)
newIndex = Mods.Length - 1;
2018-01-02 15:55:03 +08:00
if (newIndex >= 0 && !Mods[newIndex].HasImplementation)
return false;
2018-01-02 15:55:03 +08:00
selectedIndex = newIndex;
Mod modAfter = SelectedMod ?? Mods[0];
if (beforeSelected != Selected)
{
iconsContainer.RotateTo(Selected ? 5f : 0f, 300, Easing.OutElastic);
iconsContainer.ScaleTo(Selected ? 1.1f : 1f, 300, Easing.OutElastic);
}
if (modBefore != modAfter)
{
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;
using (BeginDelayedSequence(mod_switch_duration, true))
{
foregroundIcon
.RotateTo(-rotate_angle * direction)
.RotateTo(0f, mod_switch_duration, mod_switch_easing);
2017-07-16 23:28:20 +08:00
backgroundIcon
.RotateTo(rotate_angle * direction)
.RotateTo(0f, mod_switch_duration, mod_switch_easing);
Schedule(() => displayMod(modAfter));
2017-05-26 22:10:28 +08:00
}
2017-02-23 10:16:23 +08:00
}
foregroundIcon.Highlighted = Selected;
(selectedIndex == -1 ? sampleOff : sampleOn).Play();
SelectionChanged?.Invoke(SelectedMod);
return true;
2017-02-23 10:16:23 +08:00
}
public bool Selected => selectedIndex != -1;
2017-02-23 10:16:23 +08:00
private Color4 selectedColour;
2017-02-23 10:16:23 +08:00
public Color4 SelectedColour
{
get { return selectedColour; }
2017-02-23 10:16:23 +08:00
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)
{
2017-09-24 03:45:46 +08:00
Mods = Array.Empty<Mod>();
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; }
public virtual 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:
2018-01-02 15:55:03 +08:00
SelectNext(1);
break;
case MouseButton.Right:
2018-01-02 15:55:03 +08:00
SelectNext(-1);
break;
}
2017-02-17 04:05:03 +08:00
return true;
2017-02-23 10:16:23 +08:00
}
2018-01-02 15:55:03 +08:00
/// <summary>
/// Select the next available mod in a specified direction.
/// </summary>
/// <param name="direction">1 for forwards, -1 for backwards.</param>
public void SelectNext(int direction)
{
int start = selectedIndex + direction;
// wrap around if we are at an extremity.
if (start >= Mods.Length)
start = -1;
else if (start < -1)
start = Mods.Length - 1;
for (int i = start; i < Mods.Length && i >= 0; i += direction)
{
if (Mods[i].HasImplementation)
{
changeSelectedIndex(i);
return;
}
}
2017-02-23 10:16:23 +08:00
2018-01-02 15:55:03 +08:00
Deselect();
}
2017-02-23 10:16:23 +08:00
public void Deselect() => changeSelectedIndex(-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;
Colour = mod.HasImplementation ? Color4.White : Color4.Gray;
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-07-11 21:58:06 +08:00
iconsContainer.AddRange(new[]
2017-02-23 10:16:23 +08:00
{
2017-12-30 15:55:01 +08:00
backgroundIcon = new PassThroughTooltipModIcon(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
Position = new Vector2(1.5f),
},
2017-12-30 15:55:01 +08:00
foregroundIcon = new PassThroughTooltipModIcon(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-02-23 10:16:23 +08:00
Position = new Vector2(-1.5f),
},
});
}
else
{
2017-12-30 15:55:01 +08:00
iconsContainer.Add(foregroundIcon = new PassThroughTooltipModIcon(Mod)
2017-02-23 10:16:23 +08:00
{
2017-03-06 19:03:26 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
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,
},
2017-11-26 02:28:11 +08:00
new HoverClickSounds()
2017-02-23 10:16:23 +08:00
};
Mod = mod;
2017-02-23 10:16:23 +08:00
}
2017-12-30 15:55:01 +08:00
private class PassThroughTooltipModIcon : ModIcon
{
public override string TooltipText => null;
public PassThroughTooltipModIcon(Mod mod)
: base(mod)
2017-12-30 15:55:01 +08:00
{
}
}
2017-02-23 10:16:23 +08:00
}
}