1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 18:20:34 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/OsuTabControl.cs

245 lines
8.1 KiB
C#
Raw Normal View History

2017-03-05 12:07:47 +08:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2017-03-15 11:19:41 +08:00
using System;
2017-03-16 10:39:09 +08:00
using System.Linq;
2017-03-16 21:09:35 +08:00
using OpenTK;
2017-03-16 08:52:31 +08:00
using OpenTK.Graphics;
using osu.Framework.Allocation;
2017-03-23 07:44:52 +08:00
using osu.Framework.Extensions;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shapes;
2017-03-23 07:44:52 +08:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input;
2017-03-23 07:44:52 +08:00
using osu.Game.Graphics.Sprites;
2017-03-05 12:07:47 +08:00
2017-03-16 08:11:50 +08:00
namespace osu.Game.Graphics.UserInterface
2017-03-05 12:07:47 +08:00
{
2017-03-16 08:11:50 +08:00
public class OsuTabControl<T> : TabControl<T>
2017-03-05 12:07:47 +08:00
{
2017-03-22 22:32:32 +08:00
protected override Dropdown<T> CreateDropdown() => new OsuTabDropdown();
2017-03-05 12:07:47 +08:00
2017-04-03 17:35:33 +08:00
protected override TabItem<T> CreateTabItem(T value) => new OsuTabItem(value);
2017-03-08 17:19:00 +08:00
2017-06-09 19:04:29 +08:00
private static bool isEnumType => typeof(T).IsEnum;
public OsuTabControl()
2017-03-08 17:19:00 +08:00
{
2017-03-23 11:22:31 +08:00
TabContainer.Spacing = new Vector2(10f, 0f);
if (isEnumType)
foreach (var val in (T[])Enum.GetValues(typeof(T)))
AddItem(val);
2017-03-08 17:19:00 +08:00
}
2017-03-16 08:52:31 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
if (accentColour == default(Color4))
2017-03-16 08:52:31 +08:00
AccentColour = colours.Blue;
}
private Color4 accentColour;
2017-03-16 08:52:31 +08:00
public Color4 AccentColour
{
get { return accentColour; }
2017-03-16 08:52:31 +08:00
set
{
accentColour = value;
var dropdown = Dropdown as IHasAccentColour;
2017-08-25 17:41:12 +08:00
if (dropdown != null)
dropdown.AccentColour = value;
foreach (var i in TabContainer.Children.OfType<IHasAccentColour>())
i.AccentColour = value;
2017-03-16 08:52:31 +08:00
}
}
public class OsuTabItem : TabItem<T>, IHasAccentColour
2017-03-23 07:44:52 +08:00
{
protected readonly SpriteText Text;
protected readonly Box Bar;
2017-03-23 07:44:52 +08:00
private Color4 accentColour;
2017-03-23 07:44:52 +08:00
public Color4 AccentColour
{
get { return accentColour; }
2017-03-23 07:44:52 +08:00
set
{
accentColour = value;
if (!Active)
Text.Colour = value;
2017-03-23 07:44:52 +08:00
}
}
private const float transition_length = 500;
private void fadeActive()
{
Bar.FadeIn(transition_length, Easing.OutQuint);
2017-07-23 02:50:25 +08:00
Text.FadeColour(Color4.White, transition_length, Easing.OutQuint);
2017-03-23 07:44:52 +08:00
}
private void fadeInactive()
{
Bar.FadeOut(transition_length, Easing.OutQuint);
2017-07-23 02:50:25 +08:00
Text.FadeColour(AccentColour, transition_length, Easing.OutQuint);
2017-03-23 07:44:52 +08:00
}
protected override bool OnHover(InputState state)
{
if (!Active)
fadeActive();
return true;
}
protected override void OnHoverLost(InputState state)
{
if (!Active)
fadeInactive();
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
if (accentColour == default(Color4))
2017-03-23 07:44:52 +08:00
AccentColour = colours.Blue;
}
2017-04-03 17:35:33 +08:00
public OsuTabItem(T value) : base(value)
2017-03-23 07:44:52 +08:00
{
AutoSizeAxes = Axes.X;
RelativeSizeAxes = Axes.Y;
Children = new Drawable[]
{
Text = new OsuSpriteText
2017-04-03 17:35:33 +08:00
{
Margin = new MarginPadding { Top = 5, Bottom = 5 },
Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft,
Text = (value as Enum)?.GetDescription() ?? value.ToString(),
2017-04-03 17:35:33 +08:00
TextSize = 14,
Font = @"Exo2.0-Bold", // Font should only turn bold when active?
},
Bar = new Box
2017-04-03 17:35:33 +08:00
{
RelativeSizeAxes = Axes.X,
Height = 1,
Alpha = 0,
Colour = Color4.White,
Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft,
},
new HoverClickSounds()
2017-03-23 07:44:52 +08:00
};
}
2017-06-12 17:39:22 +08:00
protected override void OnActivated() => fadeActive();
protected override void OnDeactivated() => fadeInactive();
2017-03-23 07:44:52 +08:00
}
2017-08-25 17:41:12 +08:00
// todo: this needs to go
2017-03-23 07:44:52 +08:00
private class OsuTabDropdown : OsuDropdown<T>
{
public OsuTabDropdown()
{
RelativeSizeAxes = Axes.X;
}
protected override DropdownMenu CreateMenu() => new OsuTabDropdownMenu();
protected override DropdownHeader CreateHeader() => new OsuTabDropdownHeader
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight
};
private class OsuTabDropdownMenu : OsuDropdownMenu
{
public OsuTabDropdownMenu()
{
Anchor = Anchor.TopRight;
Origin = Anchor.TopRight;
BackgroundColour = Color4.Black.Opacity(0.7f);
MaxHeight = 400;
}
protected override DrawableMenuItem CreateDrawableMenuItem(MenuItem item) => new DrawableOsuTabDropdownMenuItem(item) { AccentColour = AccentColour };
2017-08-25 17:41:12 +08:00
private class DrawableOsuTabDropdownMenuItem : DrawableOsuDropdownMenuItem
{
2017-08-28 13:42:52 +08:00
public DrawableOsuTabDropdownMenuItem(MenuItem item)
2017-08-25 17:41:12 +08:00
: base(item)
{
ForegroundColourHover = Color4.Black;
}
}
}
2017-08-25 17:41:12 +08:00
protected class OsuTabDropdownHeader : OsuDropdownHeader
{
public override Color4 AccentColour
{
get
{
return base.AccentColour;
}
set
{
base.AccentColour = value;
Foreground.Colour = value;
}
}
2017-03-22 22:32:32 +08:00
public OsuTabDropdownHeader()
{
RelativeSizeAxes = Axes.None;
AutoSizeAxes = Axes.X;
BackgroundColour = Color4.Black.Opacity(0.5f);
Background.Height = 0.5f;
Background.CornerRadius = 5;
Background.Masking = true;
Foreground.RelativeSizeAxes = Axes.None;
Foreground.AutoSizeAxes = Axes.X;
Foreground.RelativeSizeAxes = Axes.Y;
Foreground.Margin = new MarginPadding(5);
Foreground.Children = new Drawable[]
{
new SpriteIcon
{
Icon = FontAwesome.fa_ellipsis_h,
Size = new Vector2(14),
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
}
};
Padding = new MarginPadding { Left = 5, Right = 5 };
}
protected override bool OnHover(InputState state)
{
Foreground.Colour = BackgroundColour;
return base.OnHover(state);
}
protected override void OnHoverLost(InputState state)
{
Foreground.Colour = BackgroundColourHover;
base.OnHoverLost(state);
}
}
}
2017-03-05 12:07:47 +08:00
}
}