osu/osu.Game/Overlays/Mods/ModButton.cs

277 lines
8.7 KiB
C#
Raw Normal View History

2018-01-05 11:21:19 +00:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
2017-02-16 20:05:03 +00:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2017-02-23 02:16:23 +00:00
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Input;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
2017-02-28 06:25:38 +00:00
using osu.Framework.Input;
2017-02-23 02:16:23 +00:00
using osu.Game.Graphics.Sprites;
2017-04-18 07:05:58 +00:00
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.UI;
using System;
using System.Linq;
using osu.Framework.Graphics.Cursor;
2017-11-25 18:28:11 +00:00
using osu.Game.Graphics.UserInterface;
2017-02-22 16:22:29 +00:00
2017-02-23 02:16:23 +00:00
namespace osu.Game.Overlays.Mods
{
/// <summary>
/// Represents a clickable button which can cycle through one of more mods.
/// </summary>
2017-05-20 02:56:48 +00:00
public class ModButton : ModButtonEmpty, IHasTooltip
2017-02-23 02:16:23 +00:00
{
2017-05-08 10:56:04 +00:00
private ModIcon foregroundIcon;
2017-05-26 14:10:28 +00:00
private ModIcon backgroundIcon;
private readonly SpriteText text;
private readonly Container<ModIcon> iconsContainer;
2017-02-23 02:16:23 +00:00
/// <summary>
/// Fired when the selection changes.
/// </summary>
public Action<Mod> SelectionChanged;
2017-02-23 02:16:23 +00:00
2017-05-20 06:22:37 +00:00
public string TooltipText => (SelectedMod?.Description ?? Mods.FirstOrDefault()?.Description) ?? string.Empty;
2017-05-20 02:56:48 +00:00
2017-07-22 18:50:25 +00:00
private const Easing mod_switch_easing = Easing.InOutSine;
private const double mod_switch_duration = 120;
2017-05-26 14:10:28 +00: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 04:12:49 +00:00
/// <param name="newIndex">The new index.</param>
/// <returns>Whether the selection changed.</returns>
2018-01-02 07:55:03 +00:00
private bool changeSelectedIndex(int newIndex)
2017-02-23 02:16:23 +00:00
{
2018-01-02 07:55:03 +00:00
if (newIndex == selectedIndex) return false;
2017-05-26 14:10:28 +00:00
2018-01-02 07:55:03 +00:00
int direction = newIndex < selectedIndex ? -1 : 1;
bool beforeSelected = Selected;
Mod modBefore = SelectedMod ?? Mods[0];
2017-02-16 20:05:03 +00:00
2018-01-02 07:55:03 +00:00
if (newIndex >= Mods.Length)
newIndex = -1;
else if (newIndex < -1)
newIndex = Mods.Length - 1;
2018-01-02 07:55:03 +00:00
if (newIndex >= 0 && !Mods[newIndex].HasImplementation)
return false;
2018-01-02 07:55:03 +00: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 15:28:20 +00:00
backgroundIcon
.RotateTo(rotate_angle * direction)
.RotateTo(0f, mod_switch_duration, mod_switch_easing);
Schedule(() => displayMod(modAfter));
2017-05-26 14:10:28 +00:00
}
2017-02-23 02:16:23 +00:00
}
foregroundIcon.Highlighted = Selected;
SelectionChanged?.Invoke(SelectedMod);
return true;
2017-02-23 02:16:23 +00:00
}
public bool Selected => selectedIndex != -1;
2017-02-23 02:16:23 +00:00
private Color4 selectedColour;
2017-02-23 02:16:23 +00:00
public Color4 SelectedColour
{
get { return selectedColour; }
2017-02-23 02:16:23 +00:00
set
{
if (value == selectedColour) return;
selectedColour = value;
2017-03-06 11:03:26 +00:00
if (Selected) foregroundIcon.Colour = value;
2017-02-23 02:16:23 +00:00
}
}
private Mod mod;
public Mod Mod
2017-02-23 02:16:23 +00:00
{
get { return mod; }
2017-02-23 02:16:23 +00:00
set
{
mod = value;
if (mod == null)
{
2017-09-23 19:45:46 +00:00
Mods = Array.Empty<Mod>();
Alpha = 0;
}
else
{
Mods = (mod as MultiMod)?.Mods ?? new[] { mod };
Alpha = 1;
}
2017-02-23 02:16:23 +00:00
createIcons();
2017-03-07 01:59:19 +00:00
if (Mods.Length > 0)
2017-02-23 02:16:23 +00:00
{
2017-03-07 01:59:19 +00:00
displayMod(Mods[0]);
2017-02-23 02:16:23 +00:00
}
}
}
2017-03-07 01:59:19 +00:00
public Mod[] Mods { get; private set; }
public virtual Mod SelectedMod => Mods.ElementAtOrDefault(selectedIndex);
2017-02-23 02:16:23 +00:00
2017-02-28 06:25:38 +00:00
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
2017-02-23 02:16:23 +00:00
{
switch (args.Button)
{
case MouseButton.Left:
2018-01-02 07:55:03 +00:00
SelectNext(1);
break;
case MouseButton.Right:
2018-01-02 07:55:03 +00:00
SelectNext(-1);
break;
}
2017-02-16 20:05:03 +00:00
return true;
2017-02-23 02:16:23 +00:00
}
2018-01-02 07:55:03 +00: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 (SelectAt(i)) return;
2017-02-23 02:16:23 +00:00
2018-01-02 07:55:03 +00:00
Deselect();
}
2017-02-23 02:16:23 +00:00
public bool SelectAt(int index)
{
if (!Mods[index].HasImplementation) return false;
changeSelectedIndex(index);
return true;
}
public void Deselect() => changeSelectedIndex(-1);
2017-02-23 02:16:23 +00:00
private void displayMod(Mod mod)
{
2017-05-29 09:10:02 +00:00
if (backgroundIcon != null)
2017-05-26 14:10:28 +00:00
backgroundIcon.Icon = foregroundIcon.Icon;
2017-03-06 11:03:26 +00:00
foregroundIcon.Icon = mod.Icon;
text.Text = mod.Name;
Colour = mod.HasImplementation ? Color4.White : Color4.Gray;
2017-02-23 02:16:23 +00:00
}
private void createIcons()
{
2017-03-06 11:03:26 +00:00
iconsContainer.Clear();
2017-02-23 02:16:23 +00:00
if (Mods.Length > 1)
{
2017-07-11 13:58:06 +00:00
iconsContainer.AddRange(new[]
2017-02-23 02:16:23 +00:00
{
2017-12-30 07:55:01 +00:00
backgroundIcon = new PassThroughTooltipModIcon(Mods[1])
2017-02-23 02:16:23 +00:00
{
2017-05-26 14:10:28 +00:00
Origin = Anchor.BottomRight,
Anchor = Anchor.BottomRight,
2017-02-23 02:16:23 +00:00
Position = new Vector2(1.5f),
},
2017-12-30 07:55:01 +00:00
foregroundIcon = new PassThroughTooltipModIcon(Mods[0])
2017-02-23 02:16:23 +00:00
{
2017-05-26 14:10:28 +00:00
Origin = Anchor.BottomRight,
Anchor = Anchor.BottomRight,
2017-02-23 02:16:23 +00:00
Position = new Vector2(-1.5f),
},
});
}
else
{
2017-12-30 07:55:01 +00:00
iconsContainer.Add(foregroundIcon = new PassThroughTooltipModIcon(Mod)
2017-02-23 02:16:23 +00:00
{
2017-03-06 11:03:26 +00:00
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
2017-02-23 02:16:23 +00:00
});
}
}
public ModButton(Mod mod)
2017-03-06 16:46:36 +00:00
{
2017-02-23 02:16:23 +00:00
Children = new Drawable[]
{
new Container
{
Size = new Vector2(77f, 80f),
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Children = new Drawable[]
{
2017-03-06 11:03:26 +00:00
iconsContainer = new Container<ModIcon>
2017-02-23 02:16:23 +00:00
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
}
}
},
text = new OsuSpriteText
{
Y = 75,
2017-02-23 02:16:23 +00:00
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
TextSize = 18,
},
2017-11-25 18:28:11 +00:00
new HoverClickSounds()
2017-02-23 02:16:23 +00:00
};
Mod = mod;
2017-02-23 02:16:23 +00:00
}
2017-12-30 07:55:01 +00:00
private class PassThroughTooltipModIcon : ModIcon
{
public override string TooltipText => null;
public PassThroughTooltipModIcon(Mod mod)
: base(mod)
2017-12-30 07:55:01 +00:00
{
}
}
2017-02-23 02:16:23 +00:00
}
}