1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 04:22:54 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/TooltipIconButton.cs

101 lines
2.7 KiB
C#
Raw Normal View History

2019-05-13 16:14:52 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-07-20 01:07:24 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2018-07-20 01:07:24 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-07-20 01:07:24 +08:00
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using System;
2019-05-12 23:36:05 +08:00
using osu.Framework.Graphics.Sprites;
using osuTK;
2018-07-20 01:07:24 +08:00
namespace osu.Game.Graphics.UserInterface
{
2018-08-22 19:43:39 +08:00
/// <summary>
/// An icon with an action upon click that can be disabled.
/// </summary>
2018-08-22 19:47:09 +08:00
public class TooltipIconButton : Container, IHasTooltip
2018-07-20 01:07:24 +08:00
{
private readonly SpriteIcon icon;
private SampleChannel sampleHover;
private SampleChannel sampleClick;
2018-07-25 00:55:50 +08:00
/// <summary>
2018-08-22 19:49:08 +08:00
/// The action to fire upon click if <see cref="IsEnabled"/> is set to true.
2018-07-25 00:55:50 +08:00
/// </summary>
public Action Action;
private bool isEnabled;
2018-07-25 00:55:50 +08:00
/// <summary>
2018-08-22 19:49:08 +08:00
/// If set to true, upon click the <see cref="Action"/> will execute.
2018-07-25 00:55:50 +08:00
/// </summary>
public bool IsEnabled
{
get => isEnabled;
set
{
isEnabled = value;
icon.FadeTo(value ? 1 : 0.5f, 250);
}
}
2018-07-20 01:07:24 +08:00
2019-05-12 23:36:05 +08:00
public IconUsage Icon
2018-07-20 01:07:24 +08:00
{
2019-05-12 23:36:05 +08:00
get => icon.Icon;
set => icon.Icon = value;
2018-07-20 01:07:24 +08:00
}
public TooltipIconButton()
{
isEnabled = true;
2018-07-20 01:07:24 +08:00
Children = new Drawable[]
2018-07-20 06:52:50 +08:00
{
new Box
2018-07-20 01:07:24 +08:00
{
2018-07-20 06:52:50 +08:00
RelativeSizeAxes = Axes.Both,
Alpha = 0,
},
icon = new SpriteIcon
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
2018-07-21 04:11:51 +08:00
RelativeSizeAxes = Axes.Both,
Size = new Vector2(0.8f),
2018-07-20 06:52:50 +08:00
}
};
2018-07-20 01:07:24 +08:00
}
protected override bool OnClick(ClickEvent e)
{
if (isEnabled)
{
sampleClick?.Play();
Action?.Invoke();
}
return base.OnClick(e);
}
protected override bool OnHover(HoverEvent e)
{
if (isEnabled)
sampleHover?.Play();
return base.OnHover(e);
}
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
sampleHover = audio.Sample.Get(@"UI/generic-hover-soft");
sampleClick = audio.Sample.Get(@"UI/generic-select-soft");
}
2018-07-20 01:07:24 +08:00
public string TooltipText { get; set; }
}
}