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

Merge branch 'master' into fix-update-button-test-failure

This commit is contained in:
Salman Ahmed 2022-07-21 11:15:43 +03:00 committed by GitHub
commit 559254db31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 463 additions and 16 deletions

View File

@ -0,0 +1,51 @@
// 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 NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Testing;
using osu.Game.Graphics.UserInterface;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Tests.Visual.UserInterface
{
public class TestSceneFPSCounter : OsuTestScene
{
[SetUpSteps]
public void SetUpSteps()
{
AddStep("create display", () =>
{
Children = new Drawable[]
{
new Box
{
Colour = Color4.White,
RelativeSizeAxes = Axes.Both,
},
new FillFlowContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
new FPSCounter(),
new FPSCounter { Scale = new Vector2(2) },
new FPSCounter { Scale = new Vector2(4) },
}
},
};
});
}
[Test]
public void TestBasic()
{
}
}
}

View File

@ -224,6 +224,12 @@ namespace osu.Game.Configuration
return new TrackedSettings return new TrackedSettings
{ {
new TrackedSetting<bool>(OsuSetting.ShowFpsDisplay, state => new SettingDescription(
rawValue: state,
name: GlobalActionKeyBindingStrings.ToggleFPSCounter,
value: state ? CommonStrings.Enabled.ToLower() : CommonStrings.Disabled.ToLower(),
shortcut: LookupKeyBindings(GlobalAction.ToggleFPSDisplay))
),
new TrackedSetting<bool>(OsuSetting.MouseDisableButtons, disabledState => new SettingDescription( new TrackedSetting<bool>(OsuSetting.MouseDisableButtons, disabledState => new SettingDescription(
rawValue: !disabledState, rawValue: !disabledState,
name: GlobalActionKeyBindingStrings.ToggleGameplayMouseButtons, name: GlobalActionKeyBindingStrings.ToggleGameplayMouseButtons,

View File

@ -0,0 +1,287 @@
// 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 System;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Framework.Platform;
using osu.Framework.Threading;
using osu.Framework.Utils;
using osu.Game.Configuration;
using osu.Game.Graphics.Sprites;
using osuTK;
namespace osu.Game.Graphics.UserInterface
{
public class FPSCounter : VisibilityContainer, IHasCustomTooltip
{
private RollingCounter<double> counterUpdateFrameTime = null!;
private RollingCounter<double> counterDrawFPS = null!;
private Container mainContent = null!;
private Container background = null!;
private const float idle_background_alpha = 0.4f;
private readonly BindableBool showFpsDisplay = new BindableBool(true);
[Resolved]
private OsuColour colours { get; set; } = null!;
public FPSCounter()
{
AutoSizeAxes = Axes.Both;
}
[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
{
InternalChildren = new Drawable[]
{
mainContent = new Container
{
Alpha = 0,
Size = new Vector2(42, 26),
Children = new Drawable[]
{
background = new Container
{
RelativeSizeAxes = Axes.Both,
CornerRadius = 5,
CornerExponent = 5f,
Masking = true,
Alpha = idle_background_alpha,
Children = new Drawable[]
{
new Box
{
Colour = colours.Gray0,
RelativeSizeAxes = Axes.Both,
},
}
},
counterUpdateFrameTime = new FrameTimeCounter
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Margin = new MarginPadding(1),
Y = -2,
},
counterDrawFPS = new FramesPerSecondCounter
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Margin = new MarginPadding(2),
Y = 10,
Scale = new Vector2(0.8f),
}
}
},
};
config.BindWith(OsuSetting.ShowFpsDisplay, showFpsDisplay);
}
protected override void LoadComplete()
{
base.LoadComplete();
displayTemporarily();
showFpsDisplay.BindValueChanged(showFps =>
{
State.Value = showFps.NewValue ? Visibility.Visible : Visibility.Hidden;
if (showFps.NewValue)
displayTemporarily();
}, true);
State.BindValueChanged(state => showFpsDisplay.Value = state.NewValue == Visibility.Visible);
}
protected override void PopIn() => this.FadeIn(100);
protected override void PopOut() => this.FadeOut(100);
protected override bool OnHover(HoverEvent e)
{
background.FadeTo(1, 200);
displayTemporarily();
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
background.FadeTo(idle_background_alpha, 200);
displayTemporarily();
base.OnHoverLost(e);
}
private bool isDisplayed;
private ScheduledDelegate? fadeOutDelegate;
private double aimDrawFPS;
private double aimUpdateFPS;
private void displayTemporarily()
{
if (!isDisplayed)
{
mainContent.FadeTo(1, 300, Easing.OutQuint);
isDisplayed = true;
}
fadeOutDelegate?.Cancel();
fadeOutDelegate = null;
if (!IsHovered)
{
fadeOutDelegate = Scheduler.AddDelayed(() =>
{
mainContent.FadeTo(0, 300, Easing.OutQuint);
isDisplayed = false;
}, 2000);
}
}
[Resolved]
private GameHost gameHost { get; set; } = null!;
protected override void Update()
{
base.Update();
// Handle the case where the window has become inactive or the user changed the
// frame limiter (we want to show the FPS as it's changing, even if it isn't an outlier).
bool aimRatesChanged = updateAimFPS();
// TODO: this is wrong (elapsed clock time, not actual run time).
double newUpdateFrameTime = gameHost.UpdateThread.Clock.ElapsedFrameTime;
// use elapsed frame time rather then FramesPerSecond to better catch stutter frames.
double newDrawFps = 1000 / Math.Max(0.001, gameHost.DrawThread.Clock.ElapsedFrameTime);
const double spike_time_ms = 20;
bool hasUpdateSpike = counterUpdateFrameTime.Current.Value < spike_time_ms && newUpdateFrameTime > spike_time_ms;
bool hasDrawSpike = counterDrawFPS.Current.Value > (1000 / spike_time_ms) && newDrawFps <= (1000 / spike_time_ms);
// If the frame time spikes up, make sure it shows immediately on the counter.
if (hasUpdateSpike)
counterUpdateFrameTime.SetCountWithoutRolling(newUpdateFrameTime);
else
counterUpdateFrameTime.Current.Value = newUpdateFrameTime;
if (hasDrawSpike)
counterDrawFPS.SetCountWithoutRolling(newDrawFps);
else
counterDrawFPS.Current.Value = newDrawFps;
counterDrawFPS.Colour = getColour(counterDrawFPS.DisplayedCount / aimDrawFPS);
double displayedUpdateFPS = 1000 / counterUpdateFrameTime.DisplayedCount;
counterUpdateFrameTime.Colour = getColour(displayedUpdateFPS / aimUpdateFPS);
bool hasSignificantChanges = aimRatesChanged
|| hasDrawSpike
|| hasUpdateSpike
|| counterDrawFPS.DisplayedCount < aimDrawFPS * 0.8
|| displayedUpdateFPS < aimUpdateFPS * 0.8;
if (hasSignificantChanges)
displayTemporarily();
}
private bool updateAimFPS()
{
if (gameHost.UpdateThread.Clock.Throttling)
{
double newAimDrawFPS = gameHost.DrawThread.Clock.MaximumUpdateHz;
double newAimUpdateFPS = gameHost.UpdateThread.Clock.MaximumUpdateHz;
if (aimDrawFPS != newAimDrawFPS || aimUpdateFPS != newAimUpdateFPS)
{
aimDrawFPS = newAimDrawFPS;
aimUpdateFPS = newAimUpdateFPS;
return true;
}
}
else
{
double newAimFPS = gameHost.InputThread.Clock.MaximumUpdateHz;
if (aimDrawFPS != newAimFPS || aimUpdateFPS != newAimFPS)
{
aimUpdateFPS = aimDrawFPS = newAimFPS;
return true;
}
}
return false;
}
private ColourInfo getColour(double performanceRatio)
{
if (performanceRatio < 0.5f)
return Interpolation.ValueAt(performanceRatio, colours.Red, colours.Orange2, 0, 0.5);
return Interpolation.ValueAt(performanceRatio, colours.Orange2, colours.Lime0, 0.5, 0.9);
}
public ITooltip GetCustomTooltip() => new FPSCounterTooltip();
public object TooltipContent => this;
public class FramesPerSecondCounter : RollingCounter<double>
{
protected override double RollingDuration => 1000;
protected override OsuSpriteText CreateSpriteText()
{
return new OsuSpriteText
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Font = OsuFont.Default.With(fixedWidth: true, size: 16, weight: FontWeight.SemiBold),
Spacing = new Vector2(-2),
};
}
protected override LocalisableString FormatCount(double count)
{
return $"{count:#,0}fps";
}
}
public class FrameTimeCounter : RollingCounter<double>
{
protected override double RollingDuration => 1000;
protected override OsuSpriteText CreateSpriteText()
{
return new OsuSpriteText
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Font = OsuFont.Default.With(fixedWidth: true, size: 16, weight: FontWeight.SemiBold),
Spacing = new Vector2(-1),
};
}
protected override LocalisableString FormatCount(double count)
{
if (count < 1)
return $"{count:N1}ms";
return $"{count:N0}ms";
}
}
}
}

View File

@ -0,0 +1,97 @@
// 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 System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Platform;
using osu.Game.Graphics.Containers;
using osuTK;
namespace osu.Game.Graphics.UserInterface
{
public class FPSCounterTooltip : CompositeDrawable, ITooltip
{
private OsuTextFlowContainer textFlow = null!;
[Resolved]
private GameHost gameHost { get; set; } = null!;
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
AutoSizeAxes = Axes.Both;
CornerRadius = 15;
Masking = true;
InternalChildren = new Drawable[]
{
new Box
{
Colour = colours.Gray1,
Alpha = 1,
RelativeSizeAxes = Axes.Both,
},
new OsuTextFlowContainer(cp =>
{
cp.Font = OsuFont.Default.With(weight: FontWeight.SemiBold);
})
{
AutoSizeAxes = Axes.Both,
TextAnchor = Anchor.TopRight,
Margin = new MarginPadding { Left = 5, Vertical = 10 },
Text = string.Join('\n', gameHost.Threads.Select(t => t.Name))
},
textFlow = new OsuTextFlowContainer(cp =>
{
cp.Font = OsuFont.Default.With(fixedWidth: true, weight: FontWeight.Regular);
cp.Spacing = new Vector2(-1);
})
{
Width = 190,
Margin = new MarginPadding { Left = 35, Right = 10, Vertical = 10 },
AutoSizeAxes = Axes.Y,
TextAnchor = Anchor.TopRight,
},
};
}
private int lastUpdate;
protected override void Update()
{
int currentSecond = (int)(Clock.CurrentTime / 100);
if (currentSecond != lastUpdate)
{
lastUpdate = currentSecond;
textFlow.Clear();
foreach (var thread in gameHost.Threads)
{
var clock = thread.Clock;
string maximum = clock.Throttling
? $"/{(clock.MaximumUpdateHz > 0 && clock.MaximumUpdateHz < 10000 ? clock.MaximumUpdateHz.ToString("0") : ""),4}"
: string.Empty;
textFlow.AddParagraph($"{clock.FramesPerSecond:0}{maximum}fps ({clock.ElapsedFrameTime:0.00}ms)");
}
}
}
public void SetContent(object content)
{
}
public void Move(Vector2 pos)
{
Position = pos;
}
}
}

View File

@ -45,6 +45,7 @@ namespace osu.Game.Input.Bindings
new KeyBinding(InputKey.F9, GlobalAction.ToggleSocial), new KeyBinding(InputKey.F9, GlobalAction.ToggleSocial),
new KeyBinding(InputKey.F10, GlobalAction.ToggleGameplayMouseButtons), new KeyBinding(InputKey.F10, GlobalAction.ToggleGameplayMouseButtons),
new KeyBinding(InputKey.F12, GlobalAction.TakeScreenshot), new KeyBinding(InputKey.F12, GlobalAction.TakeScreenshot),
new KeyBinding(new[] { InputKey.Control, InputKey.Shift, InputKey.F }, GlobalAction.ToggleFPSDisplay),
new KeyBinding(new[] { InputKey.Control, InputKey.Alt, InputKey.R }, GlobalAction.ResetInputSettings), new KeyBinding(new[] { InputKey.Control, InputKey.Alt, InputKey.R }, GlobalAction.ResetInputSettings),
new KeyBinding(new[] { InputKey.Control, InputKey.T }, GlobalAction.ToggleToolbar), new KeyBinding(new[] { InputKey.Control, InputKey.T }, GlobalAction.ToggleToolbar),
@ -328,5 +329,8 @@ namespace osu.Game.Input.Bindings
[LocalisableDescription(typeof(GlobalActionKeyBindingStrings), nameof(GlobalActionKeyBindingStrings.EditorTapForBPM))] [LocalisableDescription(typeof(GlobalActionKeyBindingStrings), nameof(GlobalActionKeyBindingStrings.EditorTapForBPM))]
EditorTapForBPM, EditorTapForBPM,
[LocalisableDescription(typeof(GlobalActionKeyBindingStrings), nameof(GlobalActionKeyBindingStrings.ToggleFPSCounter))]
ToggleFPSDisplay,
} }
} }

View File

@ -274,6 +274,11 @@ namespace osu.Game.Localisation
/// </summary> /// </summary>
public static LocalisableString ToggleSkinEditor => new TranslatableString(getKey(@"toggle_skin_editor"), @"Toggle skin editor"); public static LocalisableString ToggleSkinEditor => new TranslatableString(getKey(@"toggle_skin_editor"), @"Toggle skin editor");
/// <summary>
/// "Toggle FPS counter"
/// </summary>
public static LocalisableString ToggleFPSCounter => new TranslatableString(getKey(@"toggle_fps_counter"), @"Toggle FPS counter");
/// <summary> /// <summary>
/// "Previous volume meter" /// "Previous volume meter"
/// </summary> /// </summary>

View File

@ -159,6 +159,8 @@ namespace osu.Game
protected FirstRunSetupOverlay FirstRunOverlay { get; private set; } protected FirstRunSetupOverlay FirstRunOverlay { get; private set; }
private FPSCounter fpsCounter;
private VolumeOverlay volume; private VolumeOverlay volume;
private OsuLogo osuLogo; private OsuLogo osuLogo;
@ -814,6 +816,13 @@ namespace osu.Game
ScreenStack.ScreenPushed += screenPushed; ScreenStack.ScreenPushed += screenPushed;
ScreenStack.ScreenExited += screenExited; ScreenStack.ScreenExited += screenExited;
loadComponentSingleFile(fpsCounter = new FPSCounter
{
Anchor = Anchor.BottomRight,
Origin = Anchor.BottomRight,
Margin = new MarginPadding(5),
}, topMostOverlayContent.Add);
if (!args?.Any(a => a == @"--no-version-overlay") ?? true) if (!args?.Any(a => a == @"--no-version-overlay") ?? true)
loadComponentSingleFile(versionManager = new VersionManager { Depth = int.MinValue }, ScreenContainer.Add); loadComponentSingleFile(versionManager = new VersionManager { Depth = int.MinValue }, ScreenContainer.Add);
@ -1114,6 +1123,10 @@ namespace osu.Game
switch (e.Action) switch (e.Action)
{ {
case GlobalAction.ToggleFPSDisplay:
fpsCounter.ToggleVisibility();
return true;
case GlobalAction.ToggleSkinEditor: case GlobalAction.ToggleSkinEditor:
skinEditor.ToggleVisibility(); skinEditor.ToggleVisibility();
return true; return true;

View File

@ -17,7 +17,6 @@ using osu.Framework.Development;
using osu.Framework.Extensions; using osu.Framework.Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Performance;
using osu.Framework.Graphics.Textures; using osu.Framework.Graphics.Textures;
using osu.Framework.Input; using osu.Framework.Input;
using osu.Framework.Input.Handlers; using osu.Framework.Input.Handlers;
@ -192,8 +191,6 @@ namespace osu.Game
private DependencyContainer dependencies; private DependencyContainer dependencies;
private Bindable<bool> fpsDisplayVisible;
private readonly BindableNumber<double> globalTrackVolumeAdjust = new BindableNumber<double>(global_track_volume_adjust); private readonly BindableNumber<double> globalTrackVolumeAdjust = new BindableNumber<double>(global_track_volume_adjust);
/// <summary> /// <summary>
@ -406,19 +403,6 @@ namespace osu.Game
AddFont(Resources, @"Fonts/Venera/Venera-Black"); AddFont(Resources, @"Fonts/Venera/Venera-Black");
} }
protected override void LoadComplete()
{
base.LoadComplete();
// TODO: This is temporary until we reimplement the local FPS display.
// It's just to allow end-users to access the framework FPS display without knowing the shortcut key.
fpsDisplayVisible = LocalConfig.GetBindable<bool>(OsuSetting.ShowFpsDisplay);
fpsDisplayVisible.ValueChanged += visible => { FrameStatistics.Value = visible.NewValue ? FrameStatisticsMode.Minimal : FrameStatisticsMode.None; };
fpsDisplayVisible.TriggerChange();
FrameStatistics.ValueChanged += e => fpsDisplayVisible.Value = e.NewValue != FrameStatisticsMode.None;
}
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) => protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) =>
dependencies = new DependencyContainer(base.CreateChildDependencies(parent)); dependencies = new DependencyContainer(base.CreateChildDependencies(parent));