1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 02:07:24 +08:00
osu-lazer/osu.Game/Overlays/Mods/ModButton.cs

245 lines
7.0 KiB
C#
Raw Normal View History

2017-02-23 10:16:23 +08:00
// Copyright (c) 2007-2016 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 System;
2017-02-23 19:57:58 +08:00
using System.Linq;
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.Graphics.Transforms;
using osu.Framework.Input;
2017-02-23 10:16:23 +08:00
using osu.Game.Graphics.Sprites;
using osu.Game.Modes;
using osu.Game.Modes.UI;
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; }
2017-02-23 10:16:23 +08:00
private SpriteText text;
2017-03-06 19:03:26 +08:00
private 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-06 19:03:26 +08:00
foregroundIcon.Colour = Selected ? SelectedColour : Colour;
2017-02-23 10:16:23 +08:00
displaySelectedMod();
}
}
2017-03-06 17:21:25 +08:00
public bool Selected => selectedIndex != -1;
2017-02-23 10:16:23 +08:00
private Color4 backgroundColour;
public new Color4 Colour
{
get
{
return backgroundColour;
}
set
{
if (value == backgroundColour) return;
backgroundColour = 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
{
if (mod == value) return;
mod = value;
if (mod is MultiMod)
{
mods = ((MultiMod)mod).Mods;
}
else
{
mods = new Mod[] { mod };
}
2017-02-23 10:16:23 +08:00
createIcons();
if (mods.Length > 0)
2017-02-23 10:16:23 +08:00
{
displayMod(mods[0]);
2017-02-23 10:16:23 +08:00
}
}
}
private Mod[] mods;
public Mod[] Mods => mods; // 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
}
2017-03-06 19:03:26 +08:00
private void displaySelectedMod() => displayMod(SelectedMod ?? Mods[0]);
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-06 19:03:26 +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-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),
},
});
}
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-02-23 10:16:23 +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);
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
}
}
}