Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement layout tooltip #198

Merged
merged 4 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions osu.Game.Rulesets.Karaoke.Tests/Graphics/TestSceneLayoutToolTip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using NUnit.Framework;
using osu.Game.Rulesets.Karaoke.Graphics.Cursor;
using osu.Game.Tests.Visual;
using osu.Framework.Graphics;
using osu.Game.Rulesets.Karaoke.Skinning;
using osu.Game.Skinning;
using System.Collections.Generic;
using System;
using osu.Game.Rulesets.Karaoke.Objects;

namespace osu.Game.Rulesets.Karaoke.Tests.Graphics
{
[TestFixture]
public class TestSceneLayoutToolTip : OsuTestScene
{
private KaraokeLayoutTestSkin skin = new KaraokeLayoutTestSkin();
private LayoutToolTip toolTip;

[SetUp]
public void SetUp() => Schedule(() =>
{
Child = new SkinProvidingContainer(skin)
{
RelativeSizeAxes = Axes.Both,
Child = toolTip = new LayoutToolTip
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre
}
};
toolTip.Show();
});

[Test]
public void TestDisplayToolTip()
{
var layouts = skin.GetConfig<KaraokeIndexLookup, IDictionary<int, string>>(KaraokeIndexLookup.Layout)?.Value;
foreach (var layout in layouts)
{
setTooltip($"Test lyric with layout {layout.Value}", lyric => {
lyric.LayoutIndex = layout.Key;
});
}
}

private void setTooltip(string testName, Action<LyricLine> callBack)
{
AddStep(testName, () =>
{
var singer = new LyricLine
{
Text = "karaoke!"
};
callBack?.Invoke(singer);
toolTip.SetContent(singer);
});
}

public class KaraokeLayoutTestSkin : KaraokeLegacySkinTransformer
{
public KaraokeLayoutTestSkin()
: base(null)
{
}
}
}
}
108 changes: 108 additions & 0 deletions osu.Game.Rulesets.Karaoke/Graphics/Cursor/LayoutToolTip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Skinning;
using osu.Game.Rulesets.Karaoke.Skinning.Components;
using osu.Game.Skinning;
using osuTK;

namespace osu.Game.Rulesets.Karaoke.Graphics.Cursor
{
public class LayoutToolTip : BackgroundToolTip
{
private const float scale = 0.4f;

private readonly Box background;
private readonly Box previewLyric;
private readonly OsuSpriteText notSupportText;

[Resolved(canBeNull: true)]
private ISkinSource skinSource { get; set; }

public LayoutToolTip()
{
Children = new Drawable[]
{
background = new Box
{
Size = new Vector2(512 * scale, 384 * scale),
},
previewLyric = new Box
{
Height = 15
},
notSupportText = new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
}
};

previewLyric.Hide();
notSupportText.Hide();
}

public override bool SetContent(object content)
{
if (!(content is LyricLine lyric))
return false;

// Get layout
var layoutIndex = lyric.LayoutIndex;
var layout = skinSource?.GetConfig<KaraokeSkinLookup, KaraokeLayout>(new KaraokeSkinLookup(KaraokeSkinConfiguration.LyricLayout, layoutIndex)).Value;

// Display in content
if (layout == null)
{
// mark layout as not supported, or skin is not loaded
notSupportText.Show();

if(skinSource == null)
notSupportText.Text = "Sorry, skin is not exist.";
else
notSupportText.Text = "Sorry, layout is not exist.";
}
else
{
// Display box preview position
previewLyric.Show();

// Set preview width
const float text_size = 20;
previewLyric.Width = (lyric.Text?.Length ?? 10) * text_size * scale;
previewLyric.Height = text_size * 1.5f * scale;

// Set relative position
previewLyric.Anchor = layout.Alignment;
previewLyric.Origin = layout.Alignment;

// Set margin
const float padding = 30 * scale;
var horizontalMargin = (layout.HorizontalMargin * scale) + padding;
var verticalMargin = (layout.VerticalMargin * scale) + padding;
previewLyric.Margin = new MarginPadding
{
Left = layout.Alignment.HasFlag(Anchor.x0) ? horizontalMargin : 0,
Right = layout.Alignment.HasFlag(Anchor.x2) ? horizontalMargin : 0,
Top = layout.Alignment.HasFlag(Anchor.y0) ? verticalMargin : 0,
Bottom = layout.Alignment.HasFlag(Anchor.y2) ? verticalMargin : 0
};
}

return true;
}

[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
background.Colour = colours.Gray2;
previewLyric.Colour = colours.Yellow;
}
}
}
1 change: 0 additions & 1 deletion osu.Game.Rulesets.Karaoke/Graphics/Cursor/SingerToolTip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class SingerToolTip : BackgroundToolTip

public SingerToolTip()
{
AutoSizeAxes = Axes.Both;
Child = new FillFlowContainer
{
AutoSizeAxes = Axes.Y,
Expand Down