mirror of
https://github.com/ppy/osu.git
synced 2024-12-14 20:22:55 +08:00
Refactor OnScreenDisplay to allow displaying of custom messages on OSD
This commit is contained in:
parent
2432878614
commit
6288cc6e5c
40
osu.Game/Overlays/OSD/OsdToast.cs
Normal file
40
osu.Game/Overlays/OSD/OsdToast.cs
Normal file
@ -0,0 +1,40 @@
|
||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Shapes;
|
||||
using osuTK.Graphics;
|
||||
|
||||
namespace osu.Game.Overlays.OSD
|
||||
{
|
||||
public class OsdToast : Container
|
||||
{
|
||||
private readonly Container content;
|
||||
protected override Container<Drawable> Content => content;
|
||||
|
||||
public OsdToast()
|
||||
{
|
||||
Anchor = Anchor.Centre;
|
||||
Origin = Anchor.Centre;
|
||||
Width = 240;
|
||||
RelativeSizeAxes = Axes.Y;
|
||||
|
||||
InternalChildren = new Drawable[]
|
||||
{
|
||||
new Box
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
Colour = Color4.Black,
|
||||
Alpha = 0.7f
|
||||
},
|
||||
content = new Container
|
||||
{
|
||||
Anchor = Anchor.Centre,
|
||||
Origin = Anchor.Centre,
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -8,17 +8,11 @@ using osu.Framework.Configuration;
|
||||
using osu.Framework.Configuration.Tracking;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Graphics.Shapes;
|
||||
using osu.Framework.Graphics.Sprites;
|
||||
using osu.Game.Graphics;
|
||||
using osuTK;
|
||||
using osuTK.Graphics;
|
||||
using osu.Framework.Extensions.Color4Extensions;
|
||||
using osu.Framework.Graphics.Effects;
|
||||
using osu.Framework.Graphics.Transforms;
|
||||
using osu.Framework.Threading;
|
||||
using osu.Game.Configuration;
|
||||
using osu.Game.Graphics.Sprites;
|
||||
using osu.Game.Overlays.OSD;
|
||||
|
||||
namespace osu.Game.Overlays
|
||||
{
|
||||
@ -26,16 +20,9 @@ namespace osu.Game.Overlays
|
||||
{
|
||||
private readonly Container box;
|
||||
|
||||
private readonly SpriteText textLine1;
|
||||
private readonly SpriteText textLine2;
|
||||
private readonly SpriteText textLine3;
|
||||
|
||||
private const float height = 110;
|
||||
private const float height_notext = 98;
|
||||
private const float height_contracted = height * 0.9f;
|
||||
|
||||
private readonly FillFlowContainer<OptionLight> optionLights;
|
||||
|
||||
public OnScreenDisplay()
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both;
|
||||
@ -52,64 +39,6 @@ namespace osu.Game.Overlays
|
||||
Height = height_contracted,
|
||||
Alpha = 0,
|
||||
CornerRadius = 20,
|
||||
Children = new Drawable[]
|
||||
{
|
||||
new Box
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
Colour = Color4.Black,
|
||||
Alpha = 0.7f,
|
||||
},
|
||||
new Container // purely to add a minimum width
|
||||
{
|
||||
Anchor = Anchor.Centre,
|
||||
Origin = Anchor.Centre,
|
||||
Width = 240,
|
||||
RelativeSizeAxes = Axes.Y,
|
||||
},
|
||||
textLine1 = new OsuSpriteText
|
||||
{
|
||||
Padding = new MarginPadding(10),
|
||||
Font = OsuFont.GetFont(size: 14, weight: FontWeight.Black),
|
||||
Spacing = new Vector2(1, 0),
|
||||
Anchor = Anchor.TopCentre,
|
||||
Origin = Anchor.TopCentre,
|
||||
},
|
||||
textLine2 = new OsuSpriteText
|
||||
{
|
||||
Font = OsuFont.GetFont(size: 24, weight: FontWeight.Light),
|
||||
Padding = new MarginPadding { Left = 10, Right = 10 },
|
||||
Anchor = Anchor.Centre,
|
||||
Origin = Anchor.BottomCentre,
|
||||
},
|
||||
new FillFlowContainer
|
||||
{
|
||||
Anchor = Anchor.BottomCentre,
|
||||
Origin = Anchor.BottomCentre,
|
||||
AutoSizeAxes = Axes.Both,
|
||||
Direction = FillDirection.Vertical,
|
||||
Children = new Drawable[]
|
||||
{
|
||||
optionLights = new FillFlowContainer<OptionLight>
|
||||
{
|
||||
Padding = new MarginPadding { Top = 20, Bottom = 5 },
|
||||
Spacing = new Vector2(5, 0),
|
||||
Direction = FillDirection.Horizontal,
|
||||
Anchor = Anchor.TopCentre,
|
||||
Origin = Anchor.TopCentre,
|
||||
AutoSizeAxes = Axes.Both
|
||||
},
|
||||
textLine3 = new OsuSpriteText
|
||||
{
|
||||
Anchor = Anchor.TopCentre,
|
||||
Origin = Anchor.TopCentre,
|
||||
Margin = new MarginPadding { Bottom = 15 },
|
||||
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold),
|
||||
Alpha = 0.3f,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
@ -142,7 +71,7 @@ namespace osu.Game.Overlays
|
||||
return;
|
||||
|
||||
configManager.LoadInto(trackedSettings);
|
||||
trackedSettings.SettingChanged += display;
|
||||
trackedSettings.SettingChanged += displayTrackedSettingChange;
|
||||
|
||||
trackedConfigManagers.Add((source, configManager), trackedSettings);
|
||||
}
|
||||
@ -162,56 +91,23 @@ namespace osu.Game.Overlays
|
||||
return;
|
||||
|
||||
existing.Unload();
|
||||
existing.SettingChanged -= display;
|
||||
existing.SettingChanged -= displayTrackedSettingChange;
|
||||
|
||||
trackedConfigManagers.Remove((source, configManager));
|
||||
}
|
||||
|
||||
private void display(SettingDescription description)
|
||||
/// <summary>
|
||||
/// Displays the given <see cref="OsdToast"/> as parameter on the OSD
|
||||
/// </summary>
|
||||
/// <param name="toast"></param>
|
||||
public void Display(OsdToast toast)
|
||||
{
|
||||
Schedule(() =>
|
||||
{
|
||||
textLine1.Text = description.Name.ToUpperInvariant();
|
||||
textLine2.Text = description.Value;
|
||||
textLine3.Text = description.Shortcut.ToUpperInvariant();
|
||||
|
||||
if (string.IsNullOrEmpty(textLine3.Text))
|
||||
textLine3.Text = "NO KEY BOUND";
|
||||
|
||||
DisplayTemporarily(box);
|
||||
|
||||
int optionCount = 0;
|
||||
int selectedOption = -1;
|
||||
|
||||
switch (description.RawValue)
|
||||
{
|
||||
case bool val:
|
||||
optionCount = 1;
|
||||
if (val) selectedOption = 0;
|
||||
break;
|
||||
|
||||
case Enum _:
|
||||
var values = Enum.GetValues(description.RawValue.GetType());
|
||||
optionCount = values.Length;
|
||||
selectedOption = Convert.ToInt32(description.RawValue);
|
||||
break;
|
||||
}
|
||||
|
||||
textLine2.Origin = optionCount > 0 ? Anchor.BottomCentre : Anchor.Centre;
|
||||
textLine2.Y = optionCount > 0 ? 0 : 5;
|
||||
|
||||
if (optionLights.Children.Count != optionCount)
|
||||
{
|
||||
optionLights.Clear();
|
||||
for (int i = 0; i < optionCount; i++)
|
||||
optionLights.Add(new OptionLight());
|
||||
}
|
||||
|
||||
for (int i = 0; i < optionCount; i++)
|
||||
optionLights.Children[i].Glowing = i == selectedOption;
|
||||
});
|
||||
box.Child = toast;
|
||||
DisplayTemporarily(box);
|
||||
}
|
||||
|
||||
private void displayTrackedSettingChange(SettingDescription description) => Schedule(() => Display(new OsdTrackedSettingToast(description)));
|
||||
|
||||
private TransformSequence<Drawable> fadeIn;
|
||||
private ScheduledDelegate fadeOut;
|
||||
|
||||
@ -236,80 +132,5 @@ namespace osu.Game.Overlays
|
||||
b => b.ResizeHeightTo(height_contracted, 1500, Easing.InQuint));
|
||||
}, 500);
|
||||
}
|
||||
|
||||
private class OptionLight : Container
|
||||
{
|
||||
private Color4 glowingColour, idleColour;
|
||||
|
||||
private const float transition_speed = 300;
|
||||
|
||||
private const float glow_strength = 0.4f;
|
||||
|
||||
private readonly Box fill;
|
||||
|
||||
public OptionLight()
|
||||
{
|
||||
Children = new[]
|
||||
{
|
||||
fill = new Box
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
Alpha = 1,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
private bool glowing;
|
||||
|
||||
public bool Glowing
|
||||
{
|
||||
set
|
||||
{
|
||||
glowing = value;
|
||||
if (!IsLoaded) return;
|
||||
|
||||
updateGlow();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateGlow()
|
||||
{
|
||||
if (glowing)
|
||||
{
|
||||
fill.FadeColour(glowingColour, transition_speed, Easing.OutQuint);
|
||||
FadeEdgeEffectTo(glow_strength, transition_speed, Easing.OutQuint);
|
||||
}
|
||||
else
|
||||
{
|
||||
FadeEdgeEffectTo(0, transition_speed, Easing.OutQuint);
|
||||
fill.FadeColour(idleColour, transition_speed, Easing.OutQuint);
|
||||
}
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(OsuColour colours)
|
||||
{
|
||||
fill.Colour = idleColour = Color4.White.Opacity(0.4f);
|
||||
glowingColour = Color4.White;
|
||||
|
||||
Size = new Vector2(25, 5);
|
||||
|
||||
Masking = true;
|
||||
CornerRadius = 3;
|
||||
|
||||
EdgeEffect = new EdgeEffectParameters
|
||||
{
|
||||
Colour = colours.BlueDark.Opacity(glow_strength),
|
||||
Type = EdgeEffectType.Glow,
|
||||
Radius = 8,
|
||||
};
|
||||
}
|
||||
|
||||
protected override void LoadComplete()
|
||||
{
|
||||
updateGlow();
|
||||
FinishTransforms(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,4 +21,7 @@
|
||||
<PackageReference Include="SharpRaven" Version="2.4.0" />
|
||||
<PackageReference Include="System.ComponentModel.Annotations" Version="4.5.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Overlays\OSD\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
Loading…
Reference in New Issue
Block a user