From c2b2e5ec19f1e408d62b9ba8fb5a792877595d7a Mon Sep 17 00:00:00 2001 From: Jorolf Date: Thu, 13 Apr 2017 23:00:49 +0200 Subject: [PATCH] changed way the tool tip is found and displayed --- .../Tests/TestCaseTooltip.cs | 67 +++++++++++++++++-- osu.Game/Graphics/Cursor/IHasTooltip.cs | 24 +++++++ osu.Game/Graphics/Cursor/MenuCursor.cs | 37 +++++----- .../{UserInterface => Cursor}/Tooltip.cs | 9 +-- .../Graphics/UserInterface/IHasTooltip.cs | 10 --- osu.Game/osu.Game.csproj | 4 +- 6 files changed, 108 insertions(+), 43 deletions(-) create mode 100644 osu.Game/Graphics/Cursor/IHasTooltip.cs rename osu.Game/Graphics/{UserInterface => Cursor}/Tooltip.cs (89%) delete mode 100644 osu.Game/Graphics/UserInterface/IHasTooltip.cs diff --git a/osu.Desktop.VisualTests/Tests/TestCaseTooltip.cs b/osu.Desktop.VisualTests/Tests/TestCaseTooltip.cs index 2d9cfcf4b1..dd77eb8fb6 100644 --- a/osu.Desktop.VisualTests/Tests/TestCaseTooltip.cs +++ b/osu.Desktop.VisualTests/Tests/TestCaseTooltip.cs @@ -1,11 +1,16 @@ // Copyright (c) 2007-2017 ppy Pty Ltd . // 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, 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); + } + } } } diff --git a/osu.Game/Graphics/Cursor/IHasTooltip.cs b/osu.Game/Graphics/Cursor/IHasTooltip.cs new file mode 100644 index 0000000000..d6748236b5 --- /dev/null +++ b/osu.Game/Graphics/Cursor/IHasTooltip.cs @@ -0,0 +1,24 @@ +// Copyright (c) 2007-2017 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +namespace osu.Game.Graphics.Cursor +{ + public interface IHasTooltip + { + /// + /// Tooltip that shows when hovering the object + /// + string Tooltip { get; } + } + + /// + /// Interface of with custom appear time + /// + public interface IHasDelayedTooltip : IHasTooltip + { + /// + /// Time until the tooltip appears (in milliseconds) + /// + int Delay { get; } + } +} diff --git a/osu.Game/Graphics/Cursor/MenuCursor.cs b/osu.Game/Graphics/Cursor/MenuCursor.cs index cb6dcc451f..361d0cc1ef 100644 --- a/osu.Game/Graphics/Cursor/MenuCursor.cs +++ b/osu.Game/Graphics/Cursor/MenuCursor.cs @@ -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().Any(child => (hasTooltip = searchTooltip(child as IContainerEnumerable)) != 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 children) + private IHasTooltip searchTooltip(IContainerEnumerable children) { - IEnumerable 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; - - if (drawable is IContainer) - searchTooltip(tooltip, mousePosition, drawable as IContainerEnumerable); - } + IHasTooltip tooltipText = next as IHasTooltip; + if (tooltipText != null) return tooltipText; + + if (next is IContainer) + return searchTooltip(next as IContainerEnumerable); + return null; } protected override bool OnDragStart(InputState state) diff --git a/osu.Game/Graphics/UserInterface/Tooltip.cs b/osu.Game/Graphics/Cursor/Tooltip.cs similarity index 89% rename from osu.Game/Graphics/UserInterface/Tooltip.cs rename to osu.Game/Graphics/Cursor/Tooltip.cs index b2031a81d9..d57b83e05f 100644 --- a/osu.Game/Graphics/UserInterface/Tooltip.cs +++ b/osu.Game/Graphics/Cursor/Tooltip.cs @@ -1,7 +1,6 @@ // Copyright (c) 2007-2017 ppy Pty Ltd . // 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; diff --git a/osu.Game/Graphics/UserInterface/IHasTooltip.cs b/osu.Game/Graphics/UserInterface/IHasTooltip.cs deleted file mode 100644 index d8997349bb..0000000000 --- a/osu.Game/Graphics/UserInterface/IHasTooltip.cs +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright (c) 2007-2017 ppy Pty Ltd . -// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE - -namespace osu.Game.Graphics.UserInterface -{ - public interface IHasTooltip - { - string Tooltip { get; } - } -} diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index c29f3a49f2..e84c55db0b 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -87,14 +87,14 @@ - + - +