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

256 lines
7.4 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
{
public class ModButton : FillFlowContainer
2017-02-23 10:16:23 +08:00
{
2017-03-06 19:03:26 +08:00
private ModIcon foregroundIcon { get; set; }
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-03-07 16:17:51 +08:00
foregroundIcon.Colour = Selected ? SelectedColour : ButtonColour;
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
2017-03-07 16:17:51 +08:00
private Color4 buttonColour;
public Color4 ButtonColour
2017-02-23 10:16:23 +08:00
{
get
{
2017-03-07 16:17:51 +08:00
return buttonColour;
2017-02-23 10:16:23 +08:00
}
set
{
2017-03-07 16:17:51 +08:00
if (value == buttonColour) return;
buttonColour = value;
2017-03-06 19:03:26 +08:00
foreach (ModIcon icon in iconsContainer.Children)
2017-02-23 10:16:23 +08:00
{
icon.Colour = value;
}
}
}
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
2017-03-06 17:21:25 +08:00
public 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
{
new ModIcon
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
AutoSizeAxes = Axes.Both,
Position = new Vector2(1.5f),
2017-03-09 16:44:45 +08:00
Colour = ButtonColour
2017-02-23 10:16:23 +08:00
},
2017-03-06 19:03:26 +08:00
foregroundIcon = new ModIcon
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),
2017-03-09 16:44:45 +08:00
Colour = ButtonColour
2017-02-23 10:16:23 +08:00
},
});
}
else
{
2017-03-06 19:03:26 +08:00
iconsContainer.Add(foregroundIcon = new ModIcon
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-03-09 16:44:45 +08:00
Colour = ButtonColour
2017-02-23 10:16:23 +08:00
});
}
}
2017-03-07 00:46:36 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
foreach (ModIcon icon in iconsContainer.Children)
2017-03-07 16:17:51 +08:00
icon.Colour = ButtonColour;
2017-03-07 00:46:36 +08:00
}
public ModButton(Mod m)
2017-02-23 10:16:23 +08:00
{
2017-03-04 18:00:17 +08:00
Direction = FillDirection.Vertical;
2017-02-23 10:16:23 +08:00
Spacing = new Vector2(0f, -5f);
Size = new Vector2(100f);
AlwaysPresent = true;
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
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
TextSize = 18,
},
};
Mod = m;
2017-02-23 10:16:23 +08:00
}
}
}