1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 13:22:55 +08:00

changed way the tool tip is found and displayed

This commit is contained in:
Jorolf 2017-04-13 23:00:49 +02:00
parent a9baeddaa5
commit c2b2e5ec19
6 changed files with 108 additions and 43 deletions

View File

@ -1,11 +1,16 @@
// 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;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Testing;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Framework.Configuration;
using osu.Framework.Input;
using osu.Game.Graphics.Cursor;
using OpenTK;
namespace osu.Desktop.VisualTests.Tests
{
@ -23,25 +28,73 @@ namespace osu.Desktop.VisualTests.Tests
{
RelativeSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Children = new[]
Children = new Drawable[]
{
new TooltipSpriteText
new TooltipContainer("Text with some tooltip"),
new TooltipContainer("and another one"),
new TooltipTextbox
{
Text = "Text with some tooltip",
Text = "a box with a tooltip",
Width = 300,
},
new TooltipSpriteText
new TooltipSlider
{
Text = "and another one",
Bindable = new BindableInt(5)
{
MaxValue = 10,
MinValue = 0,
},
Size = new Vector2(300,16),
},
},
},
};
}
private class TooltipSpriteText : OsuSpriteText, IHasTooltip
private class TooltipContainer : Container, IHasTooltip
{
private readonly OsuSpriteText text;
public string Tooltip => text.Text;
public TooltipContainer(string tooltipText)
{
AutoSizeAxes = Axes.Both;
Children = new[]
{
text = new OsuSpriteText
{
Text = tooltipText,
}
};
}
}
private class TooltipTextbox : OsuTextBox, IHasTooltip
{
public string Tooltip => Text;
}
private class TooltipSlider : OsuSliderBar<int>, IHasDelayedTooltip
{
public string Tooltip => Bindable.Value.ToString();
int IHasDelayedTooltip.Delay => mousePressed ? 0 : 250;
private bool mousePressed;
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
{
mousePressed = true;
return base.OnMouseDown(state, args);
}
protected override bool OnMouseUp(InputState state, MouseUpEventArgs args)
{
mousePressed = false;
return base.OnMouseUp(state, args);
}
}
}
}

View File

@ -0,0 +1,24 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
namespace osu.Game.Graphics.Cursor
{
public interface IHasTooltip
{
/// <summary>
/// Tooltip that shows when hovering the object
/// </summary>
string Tooltip { get; }
}
/// <summary>
/// Interface of <see cref="IHasTooltip"/> with custom appear time
/// </summary>
public interface IHasDelayedTooltip : IHasTooltip
{
/// <summary>
/// Time until the tooltip appears (in milliseconds)
/// </summary>
int Delay { get; }
}
}

View File

@ -12,10 +12,8 @@ using osu.Framework.Input;
using osu.Game.Configuration;
using System;
using osu.Framework.Graphics.Textures;
using osu.Game.Graphics.UserInterface;
using osu.Framework.Threading;
using System.Linq;
using System.Collections.Generic;
namespace osu.Game.Graphics.Cursor
{
@ -40,15 +38,16 @@ namespace osu.Game.Graphics.Cursor
{
Tooltip tooltip = ((Cursor)ActiveCursor).Tooltip;
show?.Cancel();
tooltip.Hide();
Delay(250);
show = Schedule(delegate
tooltip.TooltipText = string.Empty;
IHasTooltip hasTooltip = null;
if (game.InternalChildren.OfType<IContainer>().Any(child => (hasTooltip = searchTooltip(child as IContainerEnumerable<Drawable>)) != null))
{
tooltip.TooltipText = "";
searchTooltip(tooltip, ToScreenSpace(state.Mouse.Position), game);
if (tooltip.TooltipText != "")
tooltip.Show();
});
IHasDelayedTooltip delayedTooltip = hasTooltip as IHasDelayedTooltip;
show = Scheduler.AddDelayed(delegate
{
tooltip.TooltipText = hasTooltip.Tooltip;
}, delayedTooltip?.Delay ?? 250);
}
}
if (dragging)
@ -68,18 +67,16 @@ namespace osu.Game.Graphics.Cursor
return base.OnMouseMove(state);
}
private void searchTooltip(Tooltip tooltip, Vector2 mousePosition, IContainerEnumerable<Drawable> children)
private IHasTooltip searchTooltip(IContainerEnumerable<Drawable> children)
{
IEnumerable<Drawable> next = children.InternalChildren.Where(drawable => drawable.Contains(mousePosition) && !(drawable is CursorContainer));
Drawable next = children.InternalChildren.OrderBy(drawable => drawable.Depth).FirstOrDefault(drawable => drawable.Hovering && !(drawable is CursorContainer));
foreach (Drawable drawable in next)
{
string tooltipText = (drawable as IHasTooltip)?.Tooltip ?? "";
if (tooltipText != "") tooltip.TooltipText = tooltipText;
IHasTooltip tooltipText = next as IHasTooltip;
if (tooltipText != null) return tooltipText;
if (drawable is IContainer)
searchTooltip(tooltip, mousePosition, drawable as IContainerEnumerable<Drawable>);
}
if (next is IContainer)
return searchTooltip(next as IContainerEnumerable<Drawable>);
return null;
}
protected override bool OnDragStart(InputState state)

View File

@ -1,7 +1,6 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using OpenTK;
using OpenTK.Graphics;
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
@ -11,7 +10,7 @@ using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics.Sprites;
namespace osu.Game.Graphics.UserInterface
namespace osu.Game.Graphics.Cursor
{
public class Tooltip : Container
{
@ -26,11 +25,13 @@ namespace osu.Game.Graphics.UserInterface
set
{
text.Text = value;
if (string.IsNullOrEmpty(value))
Hide();
else
Show();
}
}
public Vector2 TooltipOffset = new Vector2();
public Tooltip()
{
Depth = float.MinValue;

View File

@ -1,10 +0,0 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
namespace osu.Game.Graphics.UserInterface
{
public interface IHasTooltip
{
string Tooltip { get; }
}
}

View File

@ -87,14 +87,14 @@
<Compile Include="Graphics\Transforms\TransformAccent.cs" />
<Compile Include="Graphics\UserInterface\BackButton.cs" />
<Compile Include="Graphics\UserInterface\FocusedTextBox.cs" />
<Compile Include="Graphics\UserInterface\IHasTooltip.cs" />
<Compile Include="Graphics\Cursor\IHasTooltip.cs" />
<Compile Include="Graphics\UserInterface\Nub.cs" />
<Compile Include="Graphics\UserInterface\OsuMenu.cs" />
<Compile Include="Graphics\UserInterface\OsuPasswordTextBox.cs" />
<Compile Include="Graphics\UserInterface\OsuSliderBar.cs" />
<Compile Include="Graphics\UserInterface\OsuTextBox.cs" />
<Compile Include="Graphics\UserInterface\TwoLayerButton.cs" />
<Compile Include="Graphics\UserInterface\Tooltip.cs" />
<Compile Include="Graphics\Cursor\Tooltip.cs" />
<Compile Include="Input\Handlers\ReplayInputHandler.cs" />
<Compile Include="IO\Legacy\ILegacySerializable.cs" />
<Compile Include="IO\Legacy\SerializationReader.cs" />