From a11d53406752fbf4aea67b99934d5c6c9c7c98a8 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Sat, 5 Nov 2016 20:00:14 +0900 Subject: [PATCH] Add beatmap backgrounds. --- .../Beatmaps/IO/LegacyFilesystemReader.cs | 4 +-- .../Beatmaps/IO/OszArchiveReaderTest.cs | 2 +- osu.Game/Beatmaps/Drawable/BeatmapGroup.cs | 6 ++--- .../Beatmaps/Drawable/BeatmapSetHeader.cs | 25 ++++++------------- osu.Game/Beatmaps/Drawable/Panel.cs | 9 ++++++- osu.Game/Beatmaps/IO/ArchiveReader.cs | 20 +++++++++++++-- osu.Game/Beatmaps/IO/OszArchiveReader.cs | 4 +-- osu.Game/Beatmaps/WorkingBeatmap.cs | 23 +++++++++++++++-- osu.Game/Database/BeatmapDatabase.cs | 2 +- osu.Game/GameModes/Play/PlaySongSelect.cs | 5 +++- 10 files changed, 68 insertions(+), 32 deletions(-) diff --git a/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs b/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs index c57498f5dd..471f48c750 100644 --- a/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs +++ b/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs @@ -29,7 +29,7 @@ public LegacyFilesystemReader(string path) beatmaps = Directory.GetFiles(basePath, @"*.osu").Select(f => Path.GetFileName(f)).ToArray(); if (beatmaps.Length == 0) throw new FileNotFoundException(@"This directory contains no beatmaps"); - using (var stream = new StreamReader(ReadFile(beatmaps[0]))) + using (var stream = new StreamReader(GetStream(beatmaps[0]))) { var decoder = BeatmapDecoder.GetDecoder(stream); firstMap = decoder.Decode(stream); @@ -41,7 +41,7 @@ public override string[] ReadBeatmaps() return beatmaps; } - public override Stream ReadFile(string name) + public override Stream GetStream(string name) { return File.OpenRead(Path.Combine(basePath, name)); } diff --git a/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs b/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs index 568c08cfc6..ea26cce467 100644 --- a/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs +++ b/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs @@ -70,7 +70,7 @@ public void TestReadFile() { var reader = new OszArchiveReader(osz); using (var stream = new StreamReader( - reader.ReadFile("Soleily - Renatus (Deif) [Platter].osu"))) + reader.GetStream("Soleily - Renatus (Deif) [Platter].osu"))) { Assert.AreEqual("osu file format v13", stream.ReadLine().Trim()); } diff --git a/osu.Game/Beatmaps/Drawable/BeatmapGroup.cs b/osu.Game/Beatmaps/Drawable/BeatmapGroup.cs index 673394553e..5e10402206 100644 --- a/osu.Game/Beatmaps/Drawable/BeatmapGroup.cs +++ b/osu.Game/Beatmaps/Drawable/BeatmapGroup.cs @@ -52,7 +52,7 @@ public BeatmapGroupState State header.State = PanelSelectedState.Selected; break; case BeatmapGroupState.Collapsed: - FadeTo(0.5f, 250); + FadeTo(0.8f, 250); header.State = PanelSelectedState.NotSelected; difficulties.Hide(); @@ -61,7 +61,7 @@ public BeatmapGroupState State } } - public BeatmapGroup(BeatmapSetInfo beatmapSet) + public BeatmapGroup(BeatmapSetInfo beatmapSet, WorkingBeatmap working) { this.beatmapSet = beatmapSet; @@ -78,7 +78,7 @@ public BeatmapGroup(BeatmapSetInfo beatmapSet) Direction = FlowDirection.VerticalOnly, Children = new Framework.Graphics.Drawable[] { - header = new BeatmapSetHeader(beatmapSet) + header = new BeatmapSetHeader(beatmapSet, working) { GainedSelection = headerGainedSelection, RelativeSizeAxes = Axes.X, diff --git a/osu.Game/Beatmaps/Drawable/BeatmapSetHeader.cs b/osu.Game/Beatmaps/Drawable/BeatmapSetHeader.cs index 9ce0bafdf4..7c6fd5a963 100644 --- a/osu.Game/Beatmaps/Drawable/BeatmapSetHeader.cs +++ b/osu.Game/Beatmaps/Drawable/BeatmapSetHeader.cs @@ -31,27 +31,18 @@ protected override void Deselected() Width = 0.8f; } - public BeatmapSetHeader(BeatmapSetInfo beatmapSet) + public BeatmapSetHeader(BeatmapSetInfo beatmapSet, WorkingBeatmap working) { Children = new Framework.Graphics.Drawable[] { - new Box + working.Background == null ? new Box{ RelativeSizeAxes = Axes.Both, Colour = new Color4(20, 20, 20, 255) } : new Sprite { - Colour = new Color4(85, 85, 85, 255), - RelativeSizeAxes = Axes.Both, - }, - new Container - { - RelativeSizeAxes = Axes.Both, - Children = new Framework.Graphics.Drawable[] - { - new Box // TODO: Gradient - { - Colour = new Color4(0, 0, 0, 100), - RelativeSizeAxes = Axes.Both, - } - } - }, + Texture = working.Background, + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Scale = new Vector2(0.5f), + Colour = new Color4(200, 200, 200, 255), + }, new FlowContainer { Direction = FlowDirection.VerticalOnly, diff --git a/osu.Game/Beatmaps/Drawable/Panel.cs b/osu.Game/Beatmaps/Drawable/Panel.cs index 98b7cc4e97..c9da9e9ac2 100644 --- a/osu.Game/Beatmaps/Drawable/Panel.cs +++ b/osu.Game/Beatmaps/Drawable/Panel.cs @@ -10,6 +10,7 @@ using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Input; +using OpenTK; using OpenTK.Graphics; namespace osu.Game.Beatmaps.Drawable @@ -69,7 +70,13 @@ protected virtual void Deselected() BorderColour = new Color4(BorderColour.R, BorderColour.G, BorderColour.B, 0); BorderThickness = 0; - EdgeEffect = new EdgeEffect { Type = EdgeEffectType.None }; + EdgeEffect = new EdgeEffect + { + Type = EdgeEffectType.Shadow, + Offset = new Vector2(1), + Radius = 10, + Colour = new Color4(0, 0, 0, 100), + }; } protected override bool OnClick(InputState state) diff --git a/osu.Game/Beatmaps/IO/ArchiveReader.cs b/osu.Game/Beatmaps/IO/ArchiveReader.cs index 5f1afe3a00..bde6bb8b35 100644 --- a/osu.Game/Beatmaps/IO/ArchiveReader.cs +++ b/osu.Game/Beatmaps/IO/ArchiveReader.cs @@ -1,12 +1,13 @@ using System; using System.Collections.Generic; using System.IO; +using osu.Framework.IO.Stores; using osu.Framework.Platform; using osu.Game.Database; namespace osu.Game.Beatmaps.IO { - public abstract class ArchiveReader : IDisposable + public abstract class ArchiveReader : IDisposable, IResourceStore { private class Reader { @@ -42,8 +43,23 @@ protected static void AddReader(Func test) where /// /// Opens a stream for reading a specific file from this archive. /// - public abstract Stream ReadFile(string name); + public abstract Stream GetStream(string name); public abstract void Dispose(); + + public virtual byte[] Get(string name) + { + using (Stream input = GetStream(name)) + { + if (input == null) + return null; + + using (MemoryStream ms = new MemoryStream()) + { + input.CopyTo(ms); + return ms.ToArray(); + } + } + } } } \ No newline at end of file diff --git a/osu.Game/Beatmaps/IO/OszArchiveReader.cs b/osu.Game/Beatmaps/IO/OszArchiveReader.cs index edfaca7708..85ee2d19b9 100644 --- a/osu.Game/Beatmaps/IO/OszArchiveReader.cs +++ b/osu.Game/Beatmaps/IO/OszArchiveReader.cs @@ -33,7 +33,7 @@ public OszArchiveReader(Stream archiveStream) .Select(e => e.FileName).ToArray(); if (beatmaps.Length == 0) throw new FileNotFoundException(@"This directory contains no beatmaps"); - using (var stream = new StreamReader(ReadFile(beatmaps[0]))) + using (var stream = new StreamReader(GetStream(beatmaps[0]))) { var decoder = BeatmapDecoder.GetDecoder(stream); firstMap = decoder.Decode(stream); @@ -45,7 +45,7 @@ public override string[] ReadBeatmaps() return beatmaps; } - public override Stream ReadFile(string name) + public override Stream GetStream(string name) { ZipEntry entry = archive.Entries.SingleOrDefault(e => e.FileName == name); if (entry == null) diff --git a/osu.Game/Beatmaps/WorkingBeatmap.cs b/osu.Game/Beatmaps/WorkingBeatmap.cs index a2a0a27e02..07e88af517 100644 --- a/osu.Game/Beatmaps/WorkingBeatmap.cs +++ b/osu.Game/Beatmaps/WorkingBeatmap.cs @@ -4,6 +4,7 @@ using System; using System.IO; using osu.Framework.Audio.Track; +using osu.Framework.Graphics.Textures; using osu.Game.Beatmaps.Formats; using osu.Game.Beatmaps.IO; using osu.Game.Database; @@ -19,6 +20,24 @@ public class WorkingBeatmap : IDisposable private ArchiveReader reader => database.GetReader(BeatmapSetInfo); + private Texture background; + public Texture Background + { + get + { + if (background != null) return background; + + try + { + background = new TextureStore(new RawTextureLoaderStore(reader)).Get(BeatmapInfo.Metadata.BackgroundFile); + } + catch { } + + return background; + } + set { background = value; } + } + private Beatmap beatmap; public Beatmap Beatmap { @@ -28,7 +47,7 @@ public Beatmap Beatmap try { - using (var stream = new StreamReader(reader.ReadFile(BeatmapInfo.Path))) + using (var stream = new StreamReader(reader.GetStream(BeatmapInfo.Path))) beatmap = BeatmapDecoder.GetDecoder(stream)?.Decode(stream); } catch { } @@ -47,7 +66,7 @@ public AudioTrack Track try { - var trackData = reader.ReadFile(BeatmapInfo.Metadata.AudioFile); + var trackData = reader.GetStream(BeatmapInfo.Metadata.AudioFile); if (trackData != null) track = new AudioTrackBass(trackData); } diff --git a/osu.Game/Database/BeatmapDatabase.cs b/osu.Game/Database/BeatmapDatabase.cs index 06007dba9c..aa68a3be15 100644 --- a/osu.Game/Database/BeatmapDatabase.cs +++ b/osu.Game/Database/BeatmapDatabase.cs @@ -95,7 +95,7 @@ public void Import(params string[] paths) string[] mapNames = reader.ReadBeatmaps(); foreach (var name in mapNames) { - using (var stream = new StreamReader(reader.ReadFile(name))) + using (var stream = new StreamReader(reader.GetStream(name))) { var decoder = BeatmapDecoder.GetDecoder(stream); Beatmap beatmap = decoder.Decode(stream); diff --git a/osu.Game/GameModes/Play/PlaySongSelect.cs b/osu.Game/GameModes/Play/PlaySongSelect.cs index d7136e52d0..a2855d55b5 100644 --- a/osu.Game/GameModes/Play/PlaySongSelect.cs +++ b/osu.Game/GameModes/Play/PlaySongSelect.cs @@ -240,7 +240,10 @@ private void addBeatmapSet(BeatmapSetInfo beatmapSet) beatmapSet = database.GetWithChildren(beatmapSet.BeatmapSetID); beatmapSet.Beatmaps.ForEach(b => database.GetChildren(b)); beatmapSet.Beatmaps = beatmapSet.Beatmaps.OrderBy(b => b.BaseDifficulty.OverallDifficulty).ToList(); - var group = new BeatmapGroup(beatmapSet) { SelectionChanged = selectionChanged }; + + var working = database.GetWorkingBeatmap(beatmapSet.Beatmaps.FirstOrDefault()); + + var group = new BeatmapGroup(beatmapSet, working) { SelectionChanged = selectionChanged }; group.Preload(Game, g => {