1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 06:47:25 +08:00
osu-lazer/osu.Game/Screens/Edit/Setup/ResourcesSection.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

138 lines
4.5 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.
using System.IO;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Localisation;
using osu.Game.Beatmaps;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Overlays;
namespace osu.Game.Screens.Edit.Setup
{
internal class ResourcesSection : SetupSection
{
private LabelledTextBox audioTrackTextBox;
2021-04-04 18:50:50 +08:00
private LabelledTextBox backgroundTextBox;
public override LocalisableString Title => "Resources";
[Resolved]
private MusicController music { get; set; }
[Resolved]
private BeatmapManager beatmaps { get; set; }
[Resolved]
private IBindable<WorkingBeatmap> working { get; set; }
[Resolved(canBeNull: true)]
private Editor editor { get; set; }
2021-04-04 18:50:50 +08:00
[Resolved]
private SetupScreenHeader header { get; set; }
[BackgroundDependencyLoader]
private void load()
{
Children = new Drawable[]
{
2021-08-23 23:01:01 +08:00
backgroundTextBox = new FileChooserLabelledTextBox(".jpg", ".jpeg", ".png")
2021-04-04 18:50:50 +08:00
{
2021-08-23 23:01:01 +08:00
Label = "Background",
FixedLabelWidth = LABEL_WIDTH,
PlaceholderText = "Click to select a background image",
Current = { Value = working.Value.Metadata.BackgroundFile },
TabbableContentContainer = this
2021-04-04 18:50:50 +08:00
},
2021-08-23 23:01:01 +08:00
audioTrackTextBox = new FileChooserLabelledTextBox(".mp3", ".ogg")
{
2021-08-23 23:01:01 +08:00
Label = "Audio Track",
FixedLabelWidth = LABEL_WIDTH,
PlaceholderText = "Click to select a track",
Current = { Value = working.Value.Metadata.AudioFile },
TabbableContentContainer = this
},
};
2021-04-04 18:50:50 +08:00
backgroundTextBox.Current.BindValueChanged(backgroundChanged);
audioTrackTextBox.Current.BindValueChanged(audioTrackChanged);
}
public bool ChangeBackgroundImage(string path)
{
var source = new FileInfo(path);
if (!source.Exists)
return false;
var set = working.Value.BeatmapSetInfo;
var destination = new FileInfo($@"bg{source.Extension}");
// 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 => Path.GetFileNameWithoutExtension(f.Filename) == destination.Name);
using (var stream = source.OpenRead())
{
if (oldFile != null)
beatmaps.DeleteFile(set, oldFile);
beatmaps.AddFile(set, stream, destination.Name);
}
working.Value.Metadata.BackgroundFile = destination.Name;
header.Background.UpdateBackground();
return true;
}
public bool ChangeAudioTrack(string path)
2021-04-04 18:50:50 +08:00
{
var source = new FileInfo(path);
2021-04-04 18:50:50 +08:00
if (!source.Exists)
2021-04-04 18:50:50 +08:00
return false;
var set = working.Value.BeatmapSetInfo;
var destination = new FileInfo($@"audio{source.Extension}");
// remove the previous audio track for now.
2021-04-04 18:50:50 +08:00
// in the future we probably want to check if this is being used elsewhere (other difficulties?)
var oldFile = set.Files.FirstOrDefault(f => Path.GetFileNameWithoutExtension(f.Filename) == destination.Name);
2021-04-04 18:50:50 +08:00
using (var stream = source.OpenRead())
2021-04-04 18:50:50 +08:00
{
if (oldFile != null)
beatmaps.DeleteFile(set, oldFile);
beatmaps.AddFile(set, stream, destination.Name);
2021-04-04 18:50:50 +08:00
}
working.Value.Metadata.AudioFile = destination.Name;
2021-04-04 18:50:50 +08:00
music.ReloadCurrentTrack();
editor?.UpdateClockSource();
2021-04-04 18:50:50 +08:00
return true;
}
private void backgroundChanged(ValueChangedEvent<string> filePath)
{
if (!ChangeBackgroundImage(filePath.NewValue))
backgroundTextBox.Current.Value = filePath.OldValue;
}
private void audioTrackChanged(ValueChangedEvent<string> filePath)
{
if (!ChangeAudioTrack(filePath.NewValue))
audioTrackTextBox.Current.Value = filePath.OldValue;
}
}
}