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

229 lines
6.5 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;
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>
public class ModButton : ModButtonEmpty
2017-02-23 10:16:23 +08:00
{
2017-05-08 18:56:04 +08:00
private ModIcon foregroundIcon;
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-03-06 17:21:25 +08:00
private int _selectedIndex = -1;
private int selectedIndex
2017-02-23 10:16:23 +08:00
{
get
{
2017-03-06 17:21:25 +08:00
return _selectedIndex;
2017-02-23 10:16:23 +08:00
}
set
{
2017-03-06 17:21:25 +08:00
if (value == _selectedIndex) return;
_selectedIndex = value;
2017-02-17 04:05:03 +08:00
if (value >= Mods.Length)
{
2017-03-06 17:21:25 +08:00
_selectedIndex = -1;
2017-02-17 04:05:03 +08:00
}
else if (value <= -2)
{
2017-03-06 17:21:25 +08:00
_selectedIndex = Mods.Length - 1;
2017-02-23 10:16:23 +08:00
}
iconsContainer.RotateTo(Selected ? 5f : 0f, 300, EasingTypes.OutElastic);
iconsContainer.ScaleTo(Selected ? 1.1f : 1f, 300, EasingTypes.OutElastic);
2017-05-08 10:33:22 +08:00
foregroundIcon.Highlighted = Selected;
2017-02-23 10:16:23 +08:00
if (mod != null)
displayMod(SelectedMod ?? Mods[0]);
2017-02-23 10:16:23 +08:00
}
}
2017-03-06 17:21:25 +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)
{
sampleOn = audio.Sample.Get(@"Checkbox/check-on");
sampleOff = audio.Sample.Get(@"Checkbox/check-off");
}
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-03-06 19:03:26 +08:00
(++selectedIndex == -1 ? sampleOff : sampleOn).Play();
2017-02-23 10:16:23 +08:00
Action?.Invoke(SelectedMod);
}
public void SelectPrevious()
{
2017-03-06 19:03:26 +08:00
(--selectedIndex == -1 ? sampleOff : sampleOn).Play();
2017-02-23 10:16:23 +08:00
Action?.Invoke(SelectedMod);
}
public void Deselect()
{
2017-03-06 17:21:25 +08:00
selectedIndex = -1;
2017-02-23 10:16:23 +08:00
}
private void displayMod(Mod mod)
{
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-03 14:48:10 +08:00
new ModIcon(Mods[0])
2017-02-23 10:16:23 +08:00
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
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
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
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
}
}
}