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

Merge pull request #10329 from peppy/editor-file-drop-support

Add drag-drop support in editor setup screen for audio and background images
This commit is contained in:
Dan Balasescu 2020-10-05 20:05:41 +09:00 committed by GitHub
commit 051ea7eda9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 113 additions and 24 deletions

View File

@ -57,7 +57,7 @@ namespace osu.Game.Beatmaps
/// </summary> /// </summary>
public readonly WorkingBeatmap DefaultBeatmap; public readonly WorkingBeatmap DefaultBeatmap;
public override string[] HandledExtensions => new[] { ".osz" }; public override IEnumerable<string> HandledExtensions => new[] { ".osz" };
protected override string[] HashableFileTypes => new[] { ".osu" }; protected override string[] HashableFileTypes => new[] { ".osu" };

View File

@ -70,7 +70,7 @@ namespace osu.Game.Database
private readonly Bindable<WeakReference<TModel>> itemRemoved = new Bindable<WeakReference<TModel>>(); private readonly Bindable<WeakReference<TModel>> itemRemoved = new Bindable<WeakReference<TModel>>();
public virtual string[] HandledExtensions => new[] { ".zip" }; public virtual IEnumerable<string> HandledExtensions => new[] { ".zip" };
public virtual bool SupportsImportFromStable => RuntimeInfo.IsDesktop; public virtual bool SupportsImportFromStable => RuntimeInfo.IsDesktop;

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace osu.Game.Database namespace osu.Game.Database
@ -19,6 +20,6 @@ namespace osu.Game.Database
/// <summary> /// <summary>
/// An array of accepted file extensions (in the standard format of ".abc"). /// An array of accepted file extensions (in the standard format of ".abc").
/// </summary> /// </summary>
string[] HandledExtensions { get; } IEnumerable<string> HandledExtensions { get; }
} }
} }

View File

@ -232,9 +232,9 @@ namespace osu.Game
dependencies.Cache(new SessionStatics()); dependencies.Cache(new SessionStatics());
dependencies.Cache(new OsuColour()); dependencies.Cache(new OsuColour());
fileImporters.Add(BeatmapManager); RegisterImportHandler(BeatmapManager);
fileImporters.Add(ScoreManager); RegisterImportHandler(ScoreManager);
fileImporters.Add(SkinManager); RegisterImportHandler(SkinManager);
// tracks play so loud our samples can't keep up. // tracks play so loud our samples can't keep up.
// this adds a global reduction of track volume for the time being. // this adds a global reduction of track volume for the time being.
@ -343,6 +343,18 @@ namespace osu.Game
private readonly List<ICanAcceptFiles> fileImporters = new List<ICanAcceptFiles>(); private readonly List<ICanAcceptFiles> fileImporters = new List<ICanAcceptFiles>();
/// <summary>
/// Register a global handler for file imports. Most recently registered will have precedence.
/// </summary>
/// <param name="handler">The handler to register.</param>
public void RegisterImportHandler(ICanAcceptFiles handler) => fileImporters.Insert(0, handler);
/// <summary>
/// Unregister a global handler for file imports.
/// </summary>
/// <param name="handler">The previously registered handler.</param>
public void UnregisterImportHandler(ICanAcceptFiles handler) => fileImporters.Remove(handler);
public async Task Import(params string[] paths) public async Task Import(params string[] paths)
{ {
var extension = Path.GetExtension(paths.First())?.ToLowerInvariant(); var extension = Path.GetExtension(paths.First())?.ToLowerInvariant();
@ -354,7 +366,7 @@ namespace osu.Game
} }
} }
public string[] HandledExtensions => fileImporters.SelectMany(i => i.HandledExtensions).ToArray(); public IEnumerable<string> HandledExtensions => fileImporters.SelectMany(i => i.HandledExtensions);
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
{ {

View File

@ -27,7 +27,7 @@ namespace osu.Game.Scoring
{ {
public class ScoreManager : DownloadableArchiveModelManager<ScoreInfo, ScoreFileInfo> public class ScoreManager : DownloadableArchiveModelManager<ScoreInfo, ScoreFileInfo>
{ {
public override string[] HandledExtensions => new[] { ".osr" }; public override IEnumerable<string> HandledExtensions => new[] { ".osr" };
protected override string[] HashableFileTypes => new[] { ".osr" }; protected override string[] HashableFileTypes => new[] { ".osr" };

View File

@ -44,10 +44,5 @@ namespace osu.Game.Screens.Edit
.Then() .Then()
.FadeTo(1f, 250, Easing.OutQuint); .FadeTo(1f, 250, Easing.OutQuint);
} }
public void Exit()
{
Expire();
}
} }
} }

View File

@ -2,8 +2,10 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -13,6 +15,7 @@ using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events; using osu.Framework.Input.Events;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables; using osu.Game.Beatmaps.Drawables;
using osu.Game.Database;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Sprites;
@ -23,14 +26,24 @@ using osuTK;
namespace osu.Game.Screens.Edit.Setup namespace osu.Game.Screens.Edit.Setup
{ {
public class SetupScreen : EditorScreen public class SetupScreen : EditorScreen, ICanAcceptFiles
{ {
public IEnumerable<string> HandledExtensions => ImageExtensions.Concat(AudioExtensions);
public static string[] ImageExtensions { get; } = { ".jpg", ".jpeg", ".png" };
public static string[] AudioExtensions { get; } = { ".mp3", ".ogg" };
private FillFlowContainer flow; private FillFlowContainer flow;
private LabelledTextBox artistTextBox; private LabelledTextBox artistTextBox;
private LabelledTextBox titleTextBox; private LabelledTextBox titleTextBox;
private LabelledTextBox creatorTextBox; private LabelledTextBox creatorTextBox;
private LabelledTextBox difficultyTextBox; private LabelledTextBox difficultyTextBox;
private LabelledTextBox audioTrackTextBox; private LabelledTextBox audioTrackTextBox;
private Container backgroundSpriteContainer;
[Resolved]
private OsuGameBase game { get; set; }
[Resolved] [Resolved]
private MusicController music { get; set; } private MusicController music { get; set; }
@ -83,19 +96,12 @@ namespace osu.Game.Screens.Edit.Setup
Direction = FillDirection.Vertical, Direction = FillDirection.Vertical,
Children = new Drawable[] Children = new Drawable[]
{ {
new Container backgroundSpriteContainer = new Container
{ {
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
Height = 250, Height = 250,
Masking = true, Masking = true,
CornerRadius = 10, CornerRadius = 10,
Child = new BeatmapBackgroundSprite(Beatmap.Value)
{
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
FillMode = FillMode.Fill,
},
}, },
new OsuSpriteText new OsuSpriteText
{ {
@ -144,12 +150,81 @@ namespace osu.Game.Screens.Edit.Setup
} }
}; };
updateBackgroundSprite();
audioTrackTextBox.Current.BindValueChanged(audioTrackChanged); audioTrackTextBox.Current.BindValueChanged(audioTrackChanged);
foreach (var item in flow.OfType<LabelledTextBox>()) foreach (var item in flow.OfType<LabelledTextBox>())
item.OnCommit += onCommit; item.OnCommit += onCommit;
} }
Task ICanAcceptFiles.Import(params string[] paths)
{
Schedule(() =>
{
var firstFile = new FileInfo(paths.First());
if (ImageExtensions.Contains(firstFile.Extension))
{
ChangeBackgroundImage(firstFile.FullName);
}
else if (AudioExtensions.Contains(firstFile.Extension))
{
audioTrackTextBox.Text = firstFile.FullName;
}
});
return Task.CompletedTask;
}
private void updateBackgroundSprite()
{
LoadComponentAsync(new BeatmapBackgroundSprite(Beatmap.Value)
{
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
FillMode = FillMode.Fill,
}, background =>
{
backgroundSpriteContainer.Child = background;
background.FadeInFromZero(500);
});
}
protected override void LoadComplete()
{
base.LoadComplete();
game.RegisterImportHandler(this);
}
public bool ChangeBackgroundImage(string path)
{
var info = new FileInfo(path);
if (!info.Exists)
return false;
var set = Beatmap.Value.BeatmapSetInfo;
// remove the previous background for now.
// in the future we probably want to check if this is being used elsewhere (other difficulties?)
var oldFile = set.Files.FirstOrDefault(f => f.Filename == Beatmap.Value.Metadata.BackgroundFile);
using (var stream = info.OpenRead())
{
if (oldFile != null)
beatmaps.ReplaceFile(set, oldFile, stream, info.Name);
else
beatmaps.AddFile(set, stream, info.Name);
}
Beatmap.Value.Metadata.BackgroundFile = info.Name;
updateBackgroundSprite();
return true;
}
public bool ChangeAudioTrack(string path) public bool ChangeAudioTrack(string path)
{ {
var info = new FileInfo(path); var info = new FileInfo(path);
@ -196,6 +271,12 @@ namespace osu.Game.Screens.Edit.Setup
Beatmap.Value.Metadata.AuthorString = creatorTextBox.Current.Value; Beatmap.Value.Metadata.AuthorString = creatorTextBox.Current.Value;
Beatmap.Value.BeatmapInfo.Version = difficultyTextBox.Current.Value; Beatmap.Value.BeatmapInfo.Version = difficultyTextBox.Current.Value;
} }
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
game?.UnregisterImportHandler(this);
}
} }
internal class FileChooserLabelledTextBox : LabelledTextBox internal class FileChooserLabelledTextBox : LabelledTextBox
@ -230,7 +311,7 @@ namespace osu.Game.Screens.Edit.Setup
public void DisplayFileChooser() public void DisplayFileChooser()
{ {
Target.Child = new FileSelector(validFileExtensions: new[] { ".mp3", ".ogg" }) Target.Child = new FileSelector(validFileExtensions: SetupScreen.AudioExtensions)
{ {
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
Height = 400, Height = 400,

View File

@ -33,7 +33,7 @@ namespace osu.Game.Skinning
public readonly Bindable<Skin> CurrentSkin = new Bindable<Skin>(new DefaultSkin()); public readonly Bindable<Skin> CurrentSkin = new Bindable<Skin>(new DefaultSkin());
public readonly Bindable<SkinInfo> CurrentSkinInfo = new Bindable<SkinInfo>(SkinInfo.Default) { Default = SkinInfo.Default }; public readonly Bindable<SkinInfo> CurrentSkinInfo = new Bindable<SkinInfo>(SkinInfo.Default) { Default = SkinInfo.Default };
public override string[] HandledExtensions => new[] { ".osk" }; public override IEnumerable<string> HandledExtensions => new[] { ".osk" };
protected override string[] HashableFileTypes => new[] { ".ini" }; protected override string[] HashableFileTypes => new[] { ".ini" };