1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 15:17:44 +08:00
osu-lazer/osu.Game.Tests/Visual/UserInterface/TestSceneBeatSyncedContainer.cs

217 lines
8.3 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
using System;
using System.Collections.Generic;
2019-10-30 16:05:15 +08:00
using System.Linq;
2018-04-13 17:19:50 +08:00
using NUnit.Framework;
using osu.Framework.Allocation;
2018-04-13 17:19:50 +08:00
using osu.Framework.Audio.Track;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Timing;
using osu.Game.Beatmaps.ControlPoints;
2019-03-25 00:02:36 +08:00
using osu.Game.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Overlays;
2018-11-20 15:51:59 +08:00
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
2019-03-25 00:02:36 +08:00
namespace osu.Game.Tests.Visual.UserInterface
2018-04-13 17:19:50 +08:00
{
[TestFixture]
public class TestSceneBeatSyncedContainer : OsuTestScene
2018-04-13 17:19:50 +08:00
{
private readonly NowPlayingOverlay np;
public TestSceneBeatSyncedContainer()
2018-04-13 17:19:50 +08:00
{
Clock = new FramedClock();
Clock.ProcessFrame();
AddRange(new Drawable[]
2018-04-13 17:19:50 +08:00
{
new BeatContainer
{
Anchor = Anchor.BottomCentre,
Origin = Anchor.BottomCentre,
},
np = new NowPlayingOverlay
{
Origin = Anchor.TopRight,
Anchor = Anchor.TopRight,
}
2018-04-13 17:19:50 +08:00
});
}
protected override void LoadComplete()
{
base.LoadComplete();
np.ToggleVisibility();
2018-04-13 17:19:50 +08:00
}
private class BeatContainer : BeatSyncedContainer
{
private const int flash_layer_heigth = 150;
private readonly InfoString timingPointCount;
private readonly InfoString currentTimingPoint;
private readonly InfoString beatCount;
private readonly InfoString currentBeat;
private readonly InfoString beatsPerMinute;
private readonly InfoString adjustedBeatLength;
private readonly InfoString timeUntilNextBeat;
private readonly InfoString timeSinceLastBeat;
private readonly Box flashLayer;
[Resolved]
private MusicController musicController { get; set; }
2018-04-13 17:19:50 +08:00
public BeatContainer()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
Children = new Drawable[]
{
new Container
{
Name = @"Info Layer",
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
AutoSizeAxes = Axes.Both,
Margin = new MarginPadding { Bottom = flash_layer_heigth },
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black.Opacity(150),
},
new FillFlowContainer
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
timingPointCount = new InfoString(@"Timing points amount"),
currentTimingPoint = new InfoString(@"Current timing point"),
beatCount = new InfoString(@"Beats amount (in the current timing point)"),
currentBeat = new InfoString(@"Current beat"),
beatsPerMinute = new InfoString(@"BPM"),
adjustedBeatLength = new InfoString(@"Adjusted beat length"),
timeUntilNextBeat = new InfoString(@"Time until next beat"),
timeSinceLastBeat = new InfoString(@"Time since last beat"),
}
}
}
},
new Container
{
Name = @"Color indicator",
Anchor = Anchor.BottomCentre,
Origin = Anchor.BottomCentre,
RelativeSizeAxes = Axes.X,
Height = flash_layer_heigth,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black,
},
flashLayer = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.White,
Alpha = 0,
}
}
}
};
Beatmap.ValueChanged += delegate
{
timingPointCount.Value = 0;
currentTimingPoint.Value = 0;
beatCount.Value = 0;
currentBeat.Value = 0;
beatsPerMinute.Value = 0;
adjustedBeatLength.Value = 0;
timeUntilNextBeat.Value = 0;
timeSinceLastBeat.Value = 0;
};
}
2019-10-30 16:05:15 +08:00
private List<TimingControlPoint> timingPoints => Beatmap.Value.Beatmap.ControlPointInfo.TimingPoints.ToList();
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
private TimingControlPoint getNextTimingPoint(TimingControlPoint current)
{
2019-12-14 20:54:22 +08:00
if (timingPoints[^1] == current)
2018-04-13 17:19:50 +08:00
return current;
int index = timingPoints.IndexOf(current); // -1 means that this is a "default beat"
return index == -1 ? current : timingPoints[index + 1];
2018-04-13 17:19:50 +08:00
}
private int calculateBeatCount(TimingControlPoint current)
{
if (timingPoints.Count == 0) return 0;
2019-12-14 20:54:22 +08:00
if (timingPoints[^1] == current)
2020-08-07 18:43:16 +08:00
return (int)Math.Ceiling((musicController.CurrentTrack.Length - current.Time) / current.BeatLength);
2018-04-13 17:19:50 +08:00
return (int)Math.Ceiling((getNextTimingPoint(current).Time - current.Time) / current.BeatLength);
}
protected override void Update()
{
base.Update();
timeUntilNextBeat.Value = TimeUntilNextBeat;
timeSinceLastBeat.Value = TimeSinceLastBeat;
}
2020-06-23 12:49:18 +08:00
protected override void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, ChannelAmplitudes amplitudes)
2018-04-13 17:19:50 +08:00
{
base.OnNewBeat(beatIndex, timingPoint, effectPoint, amplitudes);
timingPointCount.Value = timingPoints.Count;
currentTimingPoint.Value = timingPoints.IndexOf(timingPoint);
beatCount.Value = calculateBeatCount(timingPoint);
currentBeat.Value = beatIndex;
beatsPerMinute.Value = 60000 / timingPoint.BeatLength;
adjustedBeatLength.Value = timingPoint.BeatLength;
flashLayer.FadeOutFromOne(timingPoint.BeatLength);
}
}
private class InfoString : FillFlowContainer
{
private const int text_size = 20;
private const int margin = 7;
private readonly OsuSpriteText valueText;
public double Value
{
set => valueText.Text = $"{value:G}";
2018-04-13 17:19:50 +08:00
}
public InfoString(string header)
{
AutoSizeAxes = Axes.Both;
Direction = FillDirection.Horizontal;
Add(new OsuSpriteText { Text = header + @": ", Font = OsuFont.GetFont(size: text_size) });
Add(valueText = new OsuSpriteText { Font = OsuFont.GetFont(size: text_size) });
2018-04-13 17:19:50 +08:00
Margin = new MarginPadding(margin);
}
}
}
}