1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-17 18:22:54 +08:00
osu-lazer/osu.Game/Overlays/Toolbar/ToolbarButton.cs

198 lines
6.4 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
2016-12-05 20:09:41 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input;
using osu.Game.Graphics;
using osu.Game.Graphics.Backgrounds;
using osu.Game.Graphics.Sprites;
using OpenTK;
using OpenTK.Graphics;
2016-12-01 13:22:29 +08:00
namespace osu.Game.Overlays.Toolbar
{
public class ToolbarButton : Container
{
public const float WIDTH = Toolbar.HEIGHT * 1.4f;
public FontAwesome Icon
{
get { return DrawableIcon.Icon; }
set { DrawableIcon.Icon = 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;
}
}
2017-02-10 15:26:43 +08:00
protected virtual Anchor TooltipAnchor => Anchor.TopLeft;
public Action Action;
protected TextAwesome DrawableIcon;
protected SpriteText DrawableText;
protected Box HoverBackground;
private readonly FillFlowContainer tooltipContainer;
private readonly SpriteText tooltip1;
private readonly SpriteText tooltip2;
2017-03-02 02:33:01 +08:00
protected FillFlowContainer Flow;
private SampleChannel sampleClick;
public ToolbarButton()
{
Width = WIDTH;
RelativeSizeAxes = Axes.Y;
Children = new Drawable[]
{
HoverBackground = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = OsuColour.Gray(80).Opacity(180),
BlendingMode = BlendingMode.Additive,
Alpha = 0,
},
2017-03-02 02:33:01 +08:00
Flow = new FillFlowContainer
{
2017-03-04 18:00:17 +08:00
Direction = FillDirection.Horizontal,
2017-03-02 02:33:01 +08:00
Spacing = new Vector2(5),
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Padding = new MarginPadding { Left = Toolbar.HEIGHT / 2, Right = Toolbar.HEIGHT / 2 },
RelativeSizeAxes = Axes.Y,
2016-10-22 17:05:46 +08:00
AutoSizeAxes = Axes.X,
Children = new Drawable[]
{
DrawableIcon = new TextAwesome
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
TextSize = 20
},
DrawableText = new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
},
},
2017-03-02 02:33:01 +08:00
tooltipContainer = new FillFlowContainer
{
2017-03-04 18:00:17 +08:00
Direction = FillDirection.Vertical,
2017-02-04 21:27:05 +08:00
RelativeSizeAxes = Axes.Both, //stops us being considered in parent's autosize
2017-02-10 15:26:43 +08:00
Anchor = (TooltipAnchor & Anchor.x0) > 0 ? Anchor.BottomLeft : Anchor.BottomRight,
Origin = TooltipAnchor,
Position = new Vector2((TooltipAnchor & Anchor.x0) > 0 ? 5 : -5, 5),
Alpha = 0,
Children = new[]
{
tooltip1 = new OsuSpriteText
{
2017-02-10 15:26:43 +08:00
Anchor = TooltipAnchor,
Origin = TooltipAnchor,
2016-11-23 10:35:31 +08:00
Shadow = true,
TextSize = 22,
Font = @"Exo2.0-Bold",
},
tooltip2 = new OsuSpriteText
{
2017-02-10 15:26:43 +08:00
Anchor = TooltipAnchor,
Origin = TooltipAnchor,
2016-11-23 10:35:31 +08:00
Shadow = true,
TextSize = 16
}
}
}
};
}
2016-12-05 20:09:41 +08:00
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
sampleClick = audio.Sample.Get(@"Menu/menuclick");
}
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args) => true;
protected override bool OnClick(InputState state)
{
Action?.Invoke();
2016-12-05 20:09:41 +08:00
sampleClick.Play();
2017-01-11 02:44:40 +08:00
HoverBackground.FlashColour(Color4.White.Opacity(100), 500, EasingTypes.OutQuint);
return true;
}
protected override bool OnHover(InputState state)
{
HoverBackground.FadeIn(200);
tooltipContainer.FadeIn(100);
return false;
}
protected override void OnHoverLost(InputState state)
{
HoverBackground.FadeOut(200);
tooltipContainer.FadeOut(100);
}
}
public class OpaqueBackground : Container
{
public OpaqueBackground()
{
RelativeSizeAxes = Axes.Both;
Masking = true;
MaskingSmoothness = 0;
2017-01-30 21:16:17 +08:00
EdgeEffect = new EdgeEffect
{
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),
},
};
}
}
}