1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 17:02:55 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/ClickableText.cs

102 lines
2.9 KiB
C#
Raw Normal View History

2018-07-22 12:01:04 +08:00
// 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.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Input;
2018-07-22 14:28:12 +08:00
using osu.Game.Graphics.Sprites;
2018-07-22 12:01:04 +08:00
using System;
namespace osu.Game.Graphics.UserInterface
{
2018-07-22 14:28:12 +08:00
public class ClickableText : OsuSpriteText, IHasTooltip
2018-07-22 12:01:04 +08:00
{
private bool isEnabled;
2018-07-22 14:28:12 +08:00
private bool isMuted;
2018-07-22 12:01:04 +08:00
private SampleChannel sampleHover;
private SampleChannel sampleClick;
2018-07-22 14:28:12 +08:00
/// <summary>
/// An action that can be set to execute after click.
/// </summary>
2018-07-22 12:01:04 +08:00
public Action Action;
2018-07-22 14:28:12 +08:00
/// <summary>
/// If set to true, a sound will be played on click.
/// </summary>
public bool IsClickMuted;
/// <summary>
/// If set to true, a sound will be played on hover.
/// </summary>
public bool IsHoverMuted;
/// <summary>
/// If disabled, no sounds will be played and <see cref="Action"/> wont execute.
/// True by default.
/// </summary>
2018-07-22 12:01:04 +08:00
public bool IsEnabled
{
get { return isEnabled; }
set
{
isEnabled = value;
2018-07-22 14:28:12 +08:00
this.FadeTo(value ? 1 : 0.5f, 250);
}
}
/// <summary>
/// Whether to play sounds on hover and click. Automatically sets
/// <see cref="IsClickMuted"/> and <see cref="IsHoverMuted"/> to the same value.>
/// </summary>
public bool IsMuted {
get { return isMuted; }
set
{
IsHoverMuted = value;
IsClickMuted = value;
isMuted = value;
2018-07-22 12:01:04 +08:00
}
}
2018-07-22 14:28:12 +08:00
/// <summary>
/// A text with sounds on hover and click,
/// an action that can be set to execute on click,
/// and a tooltip.
/// </summary>
2018-07-22 12:01:04 +08:00
public ClickableText() => isEnabled = true;
public override bool HandleMouseInput => true;
protected override bool OnHover(InputState state)
{
2018-07-22 14:28:12 +08:00
if (isEnabled && !IsHoverMuted)
2018-07-22 12:01:04 +08:00
sampleHover?.Play();
return base.OnHover(state);
}
protected override bool OnClick(InputState state)
{
if (isEnabled)
{
2018-07-22 14:28:12 +08:00
if (!IsClickMuted)
sampleClick?.Play();
2018-07-22 12:01:04 +08:00
Action?.Invoke();
}
return base.OnClick(state);
}
public string TooltipText { get; set; }
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
2018-07-22 14:28:12 +08:00
sampleClick = audio.Sample.Get(@"UI/generic-select-soft");
2018-07-22 12:01:04 +08:00
sampleHover = audio.Sample.Get(@"UI/generic-hover-soft");
}
}
}