1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-01 16:32:57 +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.

167 lines
6.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.
using System;
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.Overlays;
using osu.Game.Localisation;
using osu.Game.Models;
using osu.Game.Utils;
namespace osu.Game.Screens.Edit.Setup
{
public partial class ResourcesSection : SetupSection
{
private FormBeatmapFileSelector audioTrackChooser = null!;
private FormBeatmapFileSelector backgroundChooser = null!;
public override LocalisableString Title => EditorSetupStrings.ResourcesHeader;
[Resolved]
2023-01-07 08:08:02 +08:00
private MusicController music { get; set; } = null!;
[Resolved]
2023-01-07 08:08:02 +08:00
private BeatmapManager beatmaps { get; set; } = null!;
[Resolved]
2023-01-07 08:08:02 +08:00
private IBindable<WorkingBeatmap> working { get; set; } = null!;
[Resolved]
2023-01-07 08:03:52 +08:00
private Editor? editor { get; set; }
private SetupScreenHeaderBackground headerBackground = null!;
2021-04-04 18:50:50 +08:00
[BackgroundDependencyLoader]
private void load()
{
headerBackground = new SetupScreenHeaderBackground
{
RelativeSizeAxes = Axes.X,
Height = 110,
};
2024-11-28 14:13:32 +08:00
bool beatmapHasMultipleDifficulties = working.Value.BeatmapSetInfo.Beatmaps.Count > 1;
Children = new Drawable[]
{
2024-11-28 14:13:32 +08:00
backgroundChooser = new FormBeatmapFileSelector(beatmapHasMultipleDifficulties, ".jpg", ".jpeg", ".png")
2021-04-04 18:50:50 +08:00
{
Caption = GameplaySettingsStrings.BackgroundHeader,
PlaceholderText = EditorSetupStrings.ClickToSelectBackground,
2021-04-04 18:50:50 +08:00
},
2024-11-28 14:13:32 +08:00
audioTrackChooser = new FormBeatmapFileSelector(beatmapHasMultipleDifficulties, ".mp3", ".ogg")
{
Caption = EditorSetupStrings.AudioTrack,
PlaceholderText = EditorSetupStrings.ClickToSelectTrack,
2021-08-23 23:01:01 +08:00
},
};
backgroundChooser.PreviewContainer.Add(headerBackground);
if (!string.IsNullOrEmpty(working.Value.Metadata.BackgroundFile))
backgroundChooser.Current.Value = new FileInfo(working.Value.Metadata.BackgroundFile);
if (!string.IsNullOrEmpty(working.Value.Metadata.AudioFile))
audioTrackChooser.Current.Value = new FileInfo(working.Value.Metadata.AudioFile);
backgroundChooser.Current.BindValueChanged(backgroundChanged);
audioTrackChooser.Current.BindValueChanged(audioTrackChanged);
}
public bool ChangeBackgroundImage(FileInfo source, bool applyToAllDifficulties)
{
if (!source.Exists)
return false;
changeResource(source, applyToAllDifficulties, @"bg",
metadata => metadata.BackgroundFile,
(metadata, name) => metadata.BackgroundFile = name);
headerBackground.UpdateBackground();
2023-01-07 08:03:52 +08:00
editor?.ApplyToBackground(bg => bg.RefreshBackground());
return true;
}
public bool ChangeAudioTrack(FileInfo source, bool applyToAllDifficulties)
2021-04-04 18:50:50 +08:00
{
if (!source.Exists)
2021-04-04 18:50:50 +08:00
return false;
changeResource(source, applyToAllDifficulties, @"audio",
metadata => metadata.AudioFile,
(metadata, name) => metadata.AudioFile = name);
music.ReloadCurrentTrack();
return true;
}
private void changeResource(FileInfo source, bool applyToAllDifficulties, string baseFilename, Func<BeatmapMetadata, string> readFilename, Action<BeatmapMetadata, string> writeFilename)
{
2021-04-04 18:50:50 +08:00
var set = working.Value.BeatmapSetInfo;
string newFilename = string.Empty;
if (applyToAllDifficulties)
{
newFilename = $"{baseFilename}{source.Extension}";
foreach (var beatmap in set.Beatmaps)
{
if (set.GetFile(readFilename(beatmap.Metadata)) is RealmNamedFileUsage otherExistingFile)
beatmaps.DeleteFile(set, otherExistingFile);
2021-04-04 18:50:50 +08:00
writeFilename(beatmap.Metadata, newFilename);
}
}
else
{
var thisBeatmap = working.Value.BeatmapInfo;
string[] filenames = set.Files.Select(f => f.Filename).Where(f =>
f.StartsWith(baseFilename, StringComparison.OrdinalIgnoreCase) &&
f.EndsWith(source.Extension, StringComparison.OrdinalIgnoreCase)).ToArray();
string currentFilename = readFilename(working.Value.Metadata);
var oldFile = set.GetFile(currentFilename);
if (oldFile != null && set.Beatmaps.Where(b => !b.Equals(thisBeatmap)).All(b => readFilename(b.Metadata) != currentFilename))
{
beatmaps.DeleteFile(set, oldFile);
newFilename = currentFilename;
}
if (string.IsNullOrEmpty(newFilename))
newFilename = NamingUtils.GetNextBestFilename(filenames, $@"{baseFilename}{source.Extension}");
writeFilename(working.Value.Metadata, newFilename);
}
using (var stream = source.OpenRead())
beatmaps.AddFile(set, stream, newFilename);
// editor change handler cannot be aware of any file changes or other difficulties having their metadata modified.
// for simplicity's sake, trigger a save when changing any resource to ensure the change is correctly saved.
editor?.Save();
}
2023-01-07 08:08:02 +08:00
private void backgroundChanged(ValueChangedEvent<FileInfo?> file)
2021-04-04 18:50:50 +08:00
{
if (file.NewValue == null || !ChangeBackgroundImage(file.NewValue, backgroundChooser.ApplyToAllDifficulties.Value))
backgroundChooser.Current.Value = file.OldValue;
2021-04-04 18:50:50 +08:00
}
2023-01-07 08:08:02 +08:00
private void audioTrackChanged(ValueChangedEvent<FileInfo?> file)
{
if (file.NewValue == null || !ChangeAudioTrack(file.NewValue, audioTrackChooser.ApplyToAllDifficulties.Value))
audioTrackChooser.Current.Value = file.OldValue;
}
}
}