1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 02:42:54 +08:00

add Tooltips

This commit is contained in:
Jorolf 2017-04-02 18:19:59 +02:00
parent 9bd19e99ee
commit e835b19d4a
6 changed files with 184 additions and 1 deletions

View File

@ -0,0 +1,47 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Testing;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
namespace osu.Desktop.VisualTests.Tests
{
internal class TestCaseTooltip : TestCase
{
public override string Description => "tests tooltips on various elements";
public override void Reset()
{
base.Reset();
Children = new Drawable[]
{
new FillFlowContainer
{
RelativeSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Children = new[]
{
new TooltipSpriteText
{
Text = "Text with some tooltip",
},
new TooltipSpriteText
{
Text = "and another one",
},
},
},
};
}
private class TooltipSpriteText : OsuSpriteText, IHasTooltip
{
public string Tooltip => Text;
}
}
}

View File

@ -202,6 +202,7 @@
<Compile Include="Tests\TestCaseTaikoPlayfield.cs" />
<Compile Include="Tests\TestCaseTextAwesome.cs" />
<Compile Include="Tests\TestCasePlaySongSelect.cs" />
<Compile Include="Tests\TestCaseTooltip.cs" />
<Compile Include="Tests\TestCaseTwoLayerButton.cs" />
<Compile Include="VisualTestGame.cs" />
<Compile Include="Platform\TestStorage.cs" />

View File

@ -12,6 +12,11 @@ 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 osu.Framework.Screens;
using System.Collections.Generic;
namespace osu.Game.Graphics.Cursor
{
@ -19,10 +24,34 @@ namespace osu.Game.Graphics.Cursor
{
protected override Drawable CreateCursor() => new Cursor();
[BackgroundDependencyLoader]
private void load(OsuGameBase game)
{
this.game = game;
}
private bool dragging;
private ScheduledDelegate show;
private OsuGameBase game;
protected override bool OnMouseMove(InputState state)
{
if (state.Mouse.Position != state.Mouse.LastPosition)
{
Tooltip tooltip = ((Cursor)ActiveCursor).Tooltip;
show?.Cancel();
tooltip.Hide();
Delay(250);
show = Schedule(delegate
{
tooltip.TooltipText = "";
searchTooltip(tooltip, ToScreenSpace(state.Mouse.Position), game);
if (tooltip.TooltipText != "")
tooltip.Show();
});
}
if (dragging)
{
Vector2 offset = state.Mouse.Position - state.Mouse.PositionMouseDown ?? state.Mouse.Delta;
@ -40,6 +69,23 @@ namespace osu.Game.Graphics.Cursor
return base.OnMouseMove(state);
}
private void searchTooltip(Tooltip tooltip, Vector2 mousePosition, IContainerEnumerable<Drawable> children)
{
IEnumerable<Drawable> next = children.Children.Where(drawable => drawable.Contains(mousePosition) && !(drawable is CursorContainer));
foreach (Drawable drawable in next)
{
string tooltipText = (drawable as IHasTooltip)?.Tooltip ?? "";
if (tooltipText != "") tooltip.TooltipText = tooltipText;
var childScreen = (drawable as Screen)?.ChildScreen;
if (childScreen != null)
searchTooltip(tooltip, mousePosition, childScreen);
else if (drawable is IContainer)
searchTooltip(tooltip, mousePosition, drawable as IContainerEnumerable<Drawable>);
}
}
protected override bool OnDragStart(InputState state)
{
dragging = true;
@ -94,6 +140,7 @@ namespace osu.Game.Graphics.Cursor
public class Cursor : Container
{
private Container cursorContainer;
public Tooltip Tooltip;
private Bindable<double> cursorScale;
public Sprite AdditiveLayer;
@ -127,7 +174,11 @@ namespace osu.Game.Graphics.Cursor
Texture = textures.Get(@"Cursor/menu-cursor-additive"),
},
}
}
},
Tooltip = new Tooltip
{
Alpha = 0,
},
};
cursorScale = config.GetBindable<double>(OsuConfig.MenuCursorSize);
@ -138,6 +189,7 @@ namespace osu.Game.Graphics.Cursor
private void scaleChanged(object sender, EventArgs e)
{
cursorContainer.Scale = new Vector2((float)cursorScale);
Tooltip.Y = cursorContainer.Height * (float)cursorScale;
}
}
}

View File

@ -0,0 +1,10 @@
// 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

@ -0,0 +1,71 @@
// 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;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input;
using osu.Framework.Threading;
using osu.Game.Graphics.Sprites;
namespace osu.Game.Graphics.UserInterface
{
public class Tooltip : Container
{
private readonly Container actualTooltip;
private readonly Box tooltipBackground;
private readonly OsuSpriteText text;
public string TooltipText {
get => text.Text;
set => text.Text = value;
}
public Vector2 TooltipOffset = new Vector2();
public Tooltip()
{
Depth = float.MinValue;
AlwaysReceiveInput = true;
Children = new[]
{
actualTooltip = new Container
{
AutoSizeAxes = Axes.Both,
CornerRadius = 5,
Masking = true,
AlwaysPresent = true,
EdgeEffect = new EdgeEffect
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(40),
Radius = 5,
},
Children = new Drawable[]
{
tooltipBackground = new Box
{
RelativeSizeAxes = Axes.Both
},
text = new OsuSpriteText
{
Padding = new MarginPadding(3),
Font = @"Exo2.0-Regular",
}
}
}
};
}
[BackgroundDependencyLoader]
private void load(OsuColour colour, OsuGameBase game)
{
tooltipBackground.Colour = colour.Gray3;
}
}
}

View File

@ -87,12 +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\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="Input\Handlers\ReplayInputHandler.cs" />
<Compile Include="IO\Legacy\ILegacySerializable.cs" />
<Compile Include="IO\Legacy\SerializationReader.cs" />