2019-01-24 08:43:03 +00:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-11-20 07:51:59 +00:00
|
|
|
|
using osuTK;
|
|
|
|
|
using osuTK.Graphics;
|
|
|
|
|
using osuTK.Input;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
|
using osu.Game.Rulesets.Mods;
|
|
|
|
|
using osu.Game.Rulesets.UI;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using osu.Framework.Graphics.Cursor;
|
2018-10-02 03:02:47 +00:00
|
|
|
|
using osu.Framework.Input.Events;
|
2021-06-25 17:10:04 +00:00
|
|
|
|
using osu.Framework.Localisation;
|
2019-02-12 04:04:46 +00:00
|
|
|
|
using osu.Game.Graphics;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Mods
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a clickable button which can cycle through one of more mods.
|
|
|
|
|
/// </summary>
|
2021-08-31 16:36:27 +00:00
|
|
|
|
public class ModButton : ModButtonEmpty, IHasCustomTooltip<Mod>
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
private ModIcon foregroundIcon;
|
|
|
|
|
private ModIcon backgroundIcon;
|
|
|
|
|
private readonly SpriteText text;
|
|
|
|
|
private readonly Container<ModIcon> iconsContainer;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fired when the selection changes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Action<Mod> SelectionChanged;
|
|
|
|
|
|
2021-06-25 17:10:04 +00:00
|
|
|
|
public LocalisableString TooltipText => (SelectedMod?.Description ?? Mods.FirstOrDefault()?.Description) ?? string.Empty;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
private const Easing mod_switch_easing = Easing.InOutSine;
|
|
|
|
|
private const double mod_switch_duration = 120;
|
|
|
|
|
|
|
|
|
|
// A selected index of -1 means not selected.
|
|
|
|
|
private int selectedIndex = -1;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Change the selected mod index of this button.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="newIndex">The new index.</param>
|
2021-02-10 06:12:29 +00:00
|
|
|
|
/// <param name="resetSettings">Whether any settings applied to the mod should be reset on selection.</param>
|
2018-04-13 09:19:50 +00:00
|
|
|
|
/// <returns>Whether the selection changed.</returns>
|
2021-02-10 06:12:29 +00:00
|
|
|
|
private bool changeSelectedIndex(int newIndex, bool resetSettings = true)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (newIndex == selectedIndex) return false;
|
|
|
|
|
|
|
|
|
|
int direction = newIndex < selectedIndex ? -1 : 1;
|
2021-01-08 05:17:14 +00:00
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
bool beforeSelected = Selected;
|
|
|
|
|
|
2021-01-08 05:17:14 +00:00
|
|
|
|
Mod previousSelection = SelectedMod ?? Mods[0];
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
if (newIndex >= Mods.Length)
|
|
|
|
|
newIndex = -1;
|
|
|
|
|
else if (newIndex < -1)
|
|
|
|
|
newIndex = Mods.Length - 1;
|
|
|
|
|
|
|
|
|
|
if (newIndex >= 0 && !Mods[newIndex].HasImplementation)
|
|
|
|
|
return false;
|
|
|
|
|
|
2021-01-08 05:17:14 +00:00
|
|
|
|
selectedIndex = newIndex;
|
|
|
|
|
|
|
|
|
|
Mod newSelection = SelectedMod ?? Mods[0];
|
|
|
|
|
|
2021-02-10 06:12:29 +00:00
|
|
|
|
if (resetSettings)
|
|
|
|
|
newSelection.ResetSettingsToDefaults();
|
2021-02-09 04:44:42 +00:00
|
|
|
|
|
2021-01-05 07:41:03 +00:00
|
|
|
|
Schedule(() =>
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2021-01-05 07:41:03 +00:00
|
|
|
|
if (beforeSelected != Selected)
|
|
|
|
|
{
|
|
|
|
|
iconsContainer.RotateTo(Selected ? 5f : 0f, 300, Easing.OutElastic);
|
|
|
|
|
iconsContainer.ScaleTo(Selected ? 1.1f : 1f, 300, Easing.OutElastic);
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-01-08 05:17:14 +00:00
|
|
|
|
if (previousSelection != newSelection)
|
2021-01-05 07:41:03 +00:00
|
|
|
|
{
|
|
|
|
|
const float rotate_angle = 16;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-01-05 07:41:03 +00:00
|
|
|
|
foregroundIcon.RotateTo(rotate_angle * direction, mod_switch_duration, mod_switch_easing);
|
|
|
|
|
backgroundIcon.RotateTo(-rotate_angle * direction, mod_switch_duration, mod_switch_easing);
|
2019-04-01 03:16:05 +00:00
|
|
|
|
|
2021-01-08 05:17:14 +00:00
|
|
|
|
backgroundIcon.Mod = newSelection;
|
2021-01-05 07:41:03 +00:00
|
|
|
|
|
2021-07-05 15:52:39 +00:00
|
|
|
|
using (BeginDelayedSequence(mod_switch_duration))
|
2021-01-05 07:41:03 +00:00
|
|
|
|
{
|
|
|
|
|
foregroundIcon
|
|
|
|
|
.RotateTo(-rotate_angle * direction)
|
|
|
|
|
.RotateTo(0f, mod_switch_duration, mod_switch_easing);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-01-05 07:41:03 +00:00
|
|
|
|
backgroundIcon
|
|
|
|
|
.RotateTo(rotate_angle * direction)
|
|
|
|
|
.RotateTo(0f, mod_switch_duration, mod_switch_easing);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-08-27 23:37:46 +00:00
|
|
|
|
Schedule(() => DisplayMod(newSelection));
|
2021-01-05 07:41:03 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 07:41:03 +00:00
|
|
|
|
foregroundIcon.Selected.Value = Selected;
|
|
|
|
|
});
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-01-08 16:02:57 +00:00
|
|
|
|
SelectionChanged?.Invoke(SelectedMod);
|
2021-01-08 05:17:14 +00:00
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Selected => selectedIndex != -1;
|
|
|
|
|
|
|
|
|
|
private Color4 selectedColour;
|
|
|
|
|
|
|
|
|
|
public Color4 SelectedColour
|
|
|
|
|
{
|
2019-02-28 04:58:19 +00:00
|
|
|
|
get => selectedColour;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == selectedColour) return;
|
2019-02-28 04:31:40 +00:00
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
selectedColour = value;
|
|
|
|
|
if (Selected) foregroundIcon.Colour = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Mod mod;
|
2021-08-27 23:37:46 +00:00
|
|
|
|
|
|
|
|
|
protected readonly Container ButtonContent;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
public Mod Mod
|
|
|
|
|
{
|
2019-02-28 04:58:19 +00:00
|
|
|
|
get => mod;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
mod = value;
|
|
|
|
|
|
|
|
|
|
if (mod == null)
|
|
|
|
|
{
|
|
|
|
|
Mods = Array.Empty<Mod>();
|
|
|
|
|
Alpha = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Mods = (mod as MultiMod)?.Mods ?? new[] { mod };
|
|
|
|
|
Alpha = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createIcons();
|
2019-04-01 03:16:05 +00:00
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
if (Mods.Length > 0)
|
|
|
|
|
{
|
2021-08-27 23:37:46 +00:00
|
|
|
|
DisplayMod(Mods[0]);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Mod[] Mods { get; private set; }
|
|
|
|
|
|
|
|
|
|
public virtual Mod SelectedMod => Mods.ElementAtOrDefault(selectedIndex);
|
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
protected override bool OnMouseDown(MouseDownEvent e)
|
2018-05-11 12:40:36 +00:00
|
|
|
|
{
|
2021-08-27 23:37:46 +00:00
|
|
|
|
ButtonContent.ScaleTo(0.9f, 800, Easing.Out);
|
2018-10-02 03:02:47 +00:00
|
|
|
|
return base.OnMouseDown(e);
|
2018-05-11 12:40:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 09:17:21 +00:00
|
|
|
|
protected override void OnMouseUp(MouseUpEvent e)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2021-08-27 23:37:46 +00:00
|
|
|
|
ButtonContent.ScaleTo(1, 500, Easing.OutElastic);
|
2018-05-11 12:40:36 +00:00
|
|
|
|
|
2018-05-02 14:11:55 +00:00
|
|
|
|
// only trigger the event if we are inside the area of the button
|
2018-09-19 11:52:57 +00:00
|
|
|
|
if (Contains(e.ScreenSpaceMousePosition))
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2018-10-02 03:02:47 +00:00
|
|
|
|
switch (e.Button)
|
2018-05-02 14:11:55 +00:00
|
|
|
|
{
|
|
|
|
|
case MouseButton.Right:
|
|
|
|
|
SelectNext(-1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 16:55:45 +00:00
|
|
|
|
protected override bool OnClick(ClickEvent e)
|
|
|
|
|
{
|
|
|
|
|
SelectNext(1);
|
|
|
|
|
|
2019-12-08 17:12:32 +00:00
|
|
|
|
return true;
|
2019-12-08 16:55:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 09:19:50 +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)
|
2019-11-11 11:53:22 +00:00
|
|
|
|
{
|
2018-05-11 12:40:36 +00:00
|
|
|
|
if (SelectAt(i))
|
|
|
|
|
return;
|
2019-11-11 11:53:22 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
Deselect();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 06:12:29 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Select the mod at the provided index.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="index">The index to select.</param>
|
|
|
|
|
/// <param name="resetSettings">Whether any settings applied to the mod should be reset on selection.</param>
|
|
|
|
|
/// <returns>Whether the selection changed.</returns>
|
|
|
|
|
public bool SelectAt(int index, bool resetSettings = true)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (!Mods[index].HasImplementation) return false;
|
|
|
|
|
|
2021-02-10 06:12:29 +00:00
|
|
|
|
changeSelectedIndex(index, resetSettings);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Deselect() => changeSelectedIndex(-1);
|
|
|
|
|
|
2021-08-27 23:37:46 +00:00
|
|
|
|
protected virtual void DisplayMod(Mod mod)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (backgroundIcon != null)
|
2020-01-14 12:11:32 +00:00
|
|
|
|
backgroundIcon.Mod = foregroundIcon.Mod;
|
|
|
|
|
foregroundIcon.Mod = mod;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
text.Text = mod.Name;
|
2021-08-24 01:42:53 +00:00
|
|
|
|
Colour = mod.HasImplementation ? Color4.White : Color4.Gray;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void createIcons()
|
|
|
|
|
{
|
|
|
|
|
iconsContainer.Clear();
|
2019-04-01 03:16:05 +00:00
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
if (Mods.Length > 1)
|
|
|
|
|
{
|
|
|
|
|
iconsContainer.AddRange(new[]
|
|
|
|
|
{
|
2021-01-26 10:11:19 +00:00
|
|
|
|
backgroundIcon = new ModIcon(Mods[1], false)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.BottomRight,
|
|
|
|
|
Anchor = Anchor.BottomRight,
|
|
|
|
|
Position = new Vector2(1.5f),
|
|
|
|
|
},
|
2021-01-26 10:11:19 +00:00
|
|
|
|
foregroundIcon = new ModIcon(Mods[0], false)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.BottomRight,
|
|
|
|
|
Anchor = Anchor.BottomRight,
|
|
|
|
|
Position = new Vector2(-1.5f),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-01-26 10:11:19 +00:00
|
|
|
|
iconsContainer.Add(foregroundIcon = new ModIcon(Mod, false)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ModButton(Mod mod)
|
|
|
|
|
{
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
new Container
|
|
|
|
|
{
|
|
|
|
|
Size = new Vector2(77f, 80f),
|
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2021-08-27 23:37:46 +00:00
|
|
|
|
ButtonContent = new Container
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2021-08-24 01:42:53 +00:00
|
|
|
|
Children = new Drawable[]
|
2018-05-11 12:40:36 +00:00
|
|
|
|
{
|
2021-08-24 01:42:53 +00:00
|
|
|
|
iconsContainer = new Container<ModIcon>
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
},
|
2018-05-11 12:40:36 +00:00
|
|
|
|
},
|
2018-04-13 09:19:50 +00:00
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
text = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Y = 75,
|
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
|
Anchor = Anchor.TopCentre,
|
2019-02-12 04:04:46 +00:00
|
|
|
|
Font = OsuFont.GetFont(size: 18)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
},
|
2021-06-18 12:14:51 +00:00
|
|
|
|
new HoverSounds()
|
2018-04-13 09:19:50 +00:00
|
|
|
|
};
|
|
|
|
|
Mod = mod;
|
|
|
|
|
}
|
2021-08-22 02:13:34 +00:00
|
|
|
|
|
2021-08-31 16:36:27 +00:00
|
|
|
|
public virtual ITooltip<Mod> GetCustomTooltip() => new ModButtonTooltip();
|
2021-08-22 02:13:34 +00:00
|
|
|
|
|
2021-08-31 16:36:27 +00:00
|
|
|
|
public Mod TooltipContent => SelectedMod ?? Mods.FirstOrDefault();
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|