osu/osu.Game/Overlays/Toolbar/ToolbarButton.cs
2018-11-20 17:14:59 +09:00

200 lines
6.3 KiB
C#

// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics;
using osu.Game.Graphics.Backgrounds;
using osu.Game.Graphics.Sprites;
using osuTK;
using osuTK.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
namespace osu.Game.Overlays.Toolbar
{
public class ToolbarButton : OsuClickableContainer
{
public const float WIDTH = Toolbar.HEIGHT * 1.4f;
public void SetIcon(Drawable icon)
{
IconContainer.Icon = icon;
IconContainer.Show();
}
public void SetIcon(FontAwesome icon) => SetIcon(new SpriteIcon
{
Size = new Vector2(20),
Icon = icon
});
public FontAwesome Icon
{
set { SetIcon(value); }
}
public string Text
{
get { return DrawableText.Text; }
set
{
DrawableText.Text = value;
}
}
public string TooltipMain
{
get { return tooltip1.Text; }
set
{
tooltip1.Text = value;
}
}
public string TooltipSub
{
get { return tooltip2.Text; }
set
{
tooltip2.Text = value;
}
}
protected virtual Anchor TooltipAnchor => Anchor.TopLeft;
protected ConstrainedIconContainer IconContainer;
protected SpriteText DrawableText;
protected Box HoverBackground;
private readonly FillFlowContainer tooltipContainer;
private readonly SpriteText tooltip1;
private readonly SpriteText tooltip2;
protected FillFlowContainer Flow;
public ToolbarButton() : base(HoverSampleSet.Loud)
{
Width = WIDTH;
RelativeSizeAxes = Axes.Y;
Children = new Drawable[]
{
HoverBackground = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = OsuColour.Gray(80).Opacity(180),
Blending = BlendingMode.Additive,
Alpha = 0,
},
Flow = new FillFlowContainer
{
Direction = FillDirection.Horizontal,
Spacing = new Vector2(5),
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Padding = new MarginPadding { Left = Toolbar.HEIGHT / 2, Right = Toolbar.HEIGHT / 2 },
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
Children = new Drawable[]
{
IconContainer = new ConstrainedIconContainer
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Size = new Vector2(20),
Alpha = 0,
},
DrawableText = new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
},
},
tooltipContainer = new FillFlowContainer
{
Direction = FillDirection.Vertical,
RelativeSizeAxes = Axes.Both, //stops us being considered in parent's autosize
Anchor = TooltipAnchor.HasFlag(Anchor.x0) ? Anchor.BottomLeft : Anchor.BottomRight,
Origin = TooltipAnchor,
Position = new Vector2(TooltipAnchor.HasFlag(Anchor.x0) ? 5 : -5, 5),
Alpha = 0,
Children = new[]
{
tooltip1 = new OsuSpriteText
{
Anchor = TooltipAnchor,
Origin = TooltipAnchor,
Shadow = true,
TextSize = 22,
Font = @"Exo2.0-Bold",
},
tooltip2 = new OsuSpriteText
{
Anchor = TooltipAnchor,
Origin = TooltipAnchor,
Shadow = true,
TextSize = 16
}
}
}
};
}
protected override bool OnMouseDown(MouseDownEvent e) => true;
protected override bool OnClick(ClickEvent e)
{
HoverBackground.FlashColour(Color4.White.Opacity(100), 500, Easing.OutQuint);
return base.OnClick(e);
}
protected override bool OnHover(HoverEvent e)
{
HoverBackground.FadeIn(200);
tooltipContainer.FadeIn(100);
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
HoverBackground.FadeOut(200);
tooltipContainer.FadeOut(100);
}
}
public class OpaqueBackground : Container
{
public OpaqueBackground()
{
RelativeSizeAxes = Axes.Both;
Masking = true;
MaskingSmoothness = 0;
EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(40),
Radius = 5,
};
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = OsuColour.Gray(30)
},
new Triangles
{
RelativeSizeAxes = Axes.Both,
ColourLight = OsuColour.Gray(40),
ColourDark = OsuColour.Gray(20),
},
};
}
}
}