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;
|
2017-03-12 21:13:43 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2017-06-03 20:50:35 +08:00
|
|
|
|
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
|
|
|
|
|
{
|
2017-05-05 11:16:41 +08:00
|
|
|
|
/// <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;
|
2017-03-23 12:41:50 +08:00
|
|
|
|
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
|
|
|
|
|
2017-05-29 16:20:55 +08:00
|
|
|
|
private const EasingTypes mod_switch_easing = EasingTypes.InOutSine;
|
2017-05-29 17:03:40 +08:00
|
|
|
|
private const double mod_switch_duration = 120;
|
2017-05-26 22:10:28 +08:00
|
|
|
|
|
2017-05-29 16:20:55 +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
|
|
|
|
|
{
|
2017-05-29 16:20:55 +08:00
|
|
|
|
return selectedIndex;
|
2017-02-23 10:16:23 +08:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2017-05-29 16:20:55 +08:00
|
|
|
|
if (value == selectedIndex) return;
|
2017-05-26 22:10:28 +08:00
|
|
|
|
|
2017-05-29 16:20:55 +08:00
|
|
|
|
int direction = value < selectedIndex ? -1 : 1;
|
2017-05-29 17:03:40 +08:00
|
|
|
|
bool beforeSelected = Selected;
|
2017-05-29 16:20:55 +08:00
|
|
|
|
|
2017-05-29 17:03:40 +08:00
|
|
|
|
Mod modBefore = SelectedMod ?? Mods[0];
|
2017-02-17 04:05:03 +08:00
|
|
|
|
|
|
|
|
|
if (value >= Mods.Length)
|
2017-05-29 16:20:55 +08:00
|
|
|
|
selectedIndex = -1;
|
2017-05-29 17:03:40 +08:00
|
|
|
|
else if (value < -1)
|
2017-05-29 16:20:55 +08:00
|
|
|
|
selectedIndex = Mods.Length - 1;
|
2017-05-29 17:03:40 +08:00
|
|
|
|
else
|
|
|
|
|
selectedIndex = value;
|
2017-05-29 16:20:55 +08:00
|
|
|
|
|
2017-05-29 17:03:40 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
2017-05-29 17:03:40 +08:00
|
|
|
|
|
|
|
|
|
if (modBefore != modAfter)
|
2017-05-26 22:10:28 +08:00
|
|
|
|
{
|
2017-05-29 16:20:55 +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);
|
|
|
|
|
|
2017-05-29 17:03:40 +08:00
|
|
|
|
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
|
|
|
|
{
|
2017-05-29 16:20:55 +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
|
|
|
|
|
2017-05-29 16:20:55 +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-05-29 16:20:55 +08:00
|
|
|
|
|
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
|
|
|
|
|
2017-05-29 16:20:55 +08:00
|
|
|
|
foregroundIcon.Highlighted = Selected;
|
2017-02-23 10:16:23 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-29 16:20:55 +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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-02 13:07:28 +08:00
|
|
|
|
private Mod mod;
|
|
|
|
|
public Mod Mod
|
2017-02-23 10:16:23 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-03-02 13:07:28 +08:00
|
|
|
|
return mod;
|
2017-02-23 10:16:23 +08:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2017-03-02 13:07:28 +08:00
|
|
|
|
mod = value;
|
|
|
|
|
|
2017-03-07 19:52:51 +08:00
|
|
|
|
if (mod == null)
|
|
|
|
|
{
|
|
|
|
|
Mods = new Mod[0];
|
|
|
|
|
Alpha = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Mods = (mod as MultiMod)?.Mods ?? new[] { mod };
|
|
|
|
|
Alpha = 1;
|
|
|
|
|
}
|
2017-03-02 13:07:28 +08:00
|
|
|
|
|
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
|
2017-03-02 13:07:28 +08:00
|
|
|
|
|
2017-05-29 16:20:55 +08:00
|
|
|
|
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
|
|
|
|
{
|
2017-03-06 18:02:51 +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()
|
|
|
|
|
{
|
2017-05-29 16:20:55 +08:00
|
|
|
|
(--SelectedIndex == -1 ? sampleOff : sampleOn).Play();
|
2017-02-23 10:16:23 +08:00
|
|
|
|
Action?.Invoke(SelectedMod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Deselect()
|
|
|
|
|
{
|
2017-05-29 16:20:55 +08:00
|
|
|
|
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;
|
2017-03-06 17:28:30 +08:00
|
|
|
|
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-07-11 21:58:06 +08:00
|
|
|
|
iconsContainer.AddRange(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
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-05 11:16:41 +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
|
|
|
|
|
{
|
2017-05-05 11:16:41 +08:00
|
|
|
|
Y = 75,
|
2017-02-23 10:16:23 +08:00
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
|
TextSize = 18,
|
|
|
|
|
},
|
|
|
|
|
};
|
2017-03-02 13:07:28 +08:00
|
|
|
|
|
2017-05-05 11:16:41 +08:00
|
|
|
|
Mod = mod;
|
2017-02-23 10:16:23 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|