2020-10-06 14:17:15 +08:00
|
|
|
// 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.
|
|
|
|
|
2024-11-24 13:32:50 +08:00
|
|
|
using System;
|
2020-10-06 14:17:15 +08:00
|
|
|
using System.IO;
|
2024-11-24 13:32:50 +08:00
|
|
|
using System.Linq;
|
2020-10-06 14:17:15 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
2021-04-04 01:02:33 +08:00
|
|
|
using osu.Framework.Localisation;
|
2020-10-06 14:17:15 +08:00
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Overlays;
|
2022-08-11 01:53:20 +08:00
|
|
|
using osu.Game.Localisation;
|
2024-11-24 13:32:50 +08:00
|
|
|
using osu.Game.Models;
|
|
|
|
using osu.Game.Utils;
|
2020-10-06 14:17:15 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.Edit.Setup
|
|
|
|
{
|
2024-10-03 19:53:21 +08:00
|
|
|
public partial class ResourcesSection : SetupSection
|
2020-10-06 14:17:15 +08:00
|
|
|
{
|
2024-11-27 18:53:22 +08:00
|
|
|
private FormBeatmapFileSelector audioTrackChooser = null!;
|
|
|
|
private FormBeatmapFileSelector backgroundChooser = null!;
|
2020-10-06 14:17:15 +08:00
|
|
|
|
2022-08-15 23:14:16 +08:00
|
|
|
public override LocalisableString Title => EditorSetupStrings.ResourcesHeader;
|
2021-04-04 01:02:33 +08:00
|
|
|
|
2020-10-06 14:17:15 +08:00
|
|
|
[Resolved]
|
2023-01-07 08:08:02 +08:00
|
|
|
private MusicController music { get; set; } = null!;
|
2020-10-06 14:17:15 +08:00
|
|
|
|
|
|
|
[Resolved]
|
2023-01-07 08:08:02 +08:00
|
|
|
private BeatmapManager beatmaps { get; set; } = null!;
|
2020-10-06 14:17:15 +08:00
|
|
|
|
2021-01-04 15:47:08 +08:00
|
|
|
[Resolved]
|
2023-01-07 08:08:02 +08:00
|
|
|
private IBindable<WorkingBeatmap> working { get; set; } = null!;
|
2021-01-04 15:47:08 +08:00
|
|
|
|
2022-08-01 15:36:12 +08:00
|
|
|
[Resolved]
|
2023-01-07 08:08:02 +08:00
|
|
|
private EditorBeatmap editorBeatmap { get; set; } = null!;
|
2020-10-06 14:17:15 +08:00
|
|
|
|
2023-01-07 00:26:30 +08:00
|
|
|
[Resolved]
|
2023-01-07 08:03:52 +08:00
|
|
|
private Editor? editor { get; set; }
|
2023-01-07 00:26:30 +08:00
|
|
|
|
2024-10-04 17:09:14 +08:00
|
|
|
private SetupScreenHeaderBackground headerBackground = null!;
|
2021-04-04 18:50:50 +08:00
|
|
|
|
2020-10-06 14:17:15 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
2024-10-04 17:09:14 +08:00
|
|
|
headerBackground = new SetupScreenHeaderBackground
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Height = 110,
|
|
|
|
};
|
|
|
|
|
2024-11-28 14:13:32 +08:00
|
|
|
bool beatmapHasMultipleDifficulties = working.Value.BeatmapSetInfo.Beatmaps.Count > 1;
|
2024-11-27 18:53:22 +08:00
|
|
|
|
2020-10-06 18:26:57 +08:00
|
|
|
Children = new Drawable[]
|
2020-10-06 14:17:15 +08:00
|
|
|
{
|
2024-11-28 14:13:32 +08:00
|
|
|
backgroundChooser = new FormBeatmapFileSelector(beatmapHasMultipleDifficulties, ".jpg", ".jpeg", ".png")
|
2021-04-04 18:50:50 +08:00
|
|
|
{
|
2024-08-28 18:17:39 +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")
|
2020-10-06 14:17:15 +08:00
|
|
|
{
|
2024-08-28 18:17:39 +08:00
|
|
|
Caption = EditorSetupStrings.AudioTrack,
|
|
|
|
PlaceholderText = EditorSetupStrings.ClickToSelectTrack,
|
2021-08-23 23:01:01 +08:00
|
|
|
},
|
2020-10-06 14:17:15 +08:00
|
|
|
};
|
|
|
|
|
2024-10-04 17:09:14 +08:00
|
|
|
backgroundChooser.PreviewContainer.Add(headerBackground);
|
|
|
|
|
2022-06-16 00:29:09 +08:00
|
|
|
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);
|
|
|
|
|
2022-06-16 23:48:32 +08:00
|
|
|
backgroundChooser.Current.BindValueChanged(backgroundChanged);
|
|
|
|
audioTrackChooser.Current.BindValueChanged(audioTrackChanged);
|
2020-10-06 14:17:15 +08:00
|
|
|
}
|
|
|
|
|
2024-11-27 18:53:22 +08:00
|
|
|
public bool ChangeBackgroundImage(FileInfo source, bool applyToAllDifficulties)
|
2020-10-06 14:17:15 +08:00
|
|
|
{
|
2022-06-15 14:02:48 +08:00
|
|
|
if (!source.Exists)
|
2020-10-06 14:17:15 +08:00
|
|
|
return false;
|
|
|
|
|
2024-11-29 06:57:47 +08:00
|
|
|
changeResource(source, applyToAllDifficulties, @"bg",
|
|
|
|
metadata => metadata.BackgroundFile,
|
|
|
|
(metadata, name) => metadata.BackgroundFile = name);
|
2022-08-01 15:36:12 +08:00
|
|
|
|
2024-08-28 18:17:39 +08:00
|
|
|
headerBackground.UpdateBackground();
|
2023-01-07 08:03:52 +08:00
|
|
|
editor?.ApplyToBackground(bg => bg.RefreshBackground());
|
2020-10-06 14:17:15 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-11-27 18:53:22 +08:00
|
|
|
public bool ChangeAudioTrack(FileInfo source, bool applyToAllDifficulties)
|
2021-04-04 18:50:50 +08:00
|
|
|
{
|
2022-06-15 14:02:48 +08:00
|
|
|
if (!source.Exists)
|
2021-04-04 18:50:50 +08:00
|
|
|
return false;
|
|
|
|
|
2024-11-29 06:57:47 +08:00
|
|
|
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;
|
|
|
|
|
2024-11-29 06:57:47 +08:00
|
|
|
string newFilename = string.Empty;
|
|
|
|
|
2024-11-27 18:53:22 +08:00
|
|
|
if (applyToAllDifficulties)
|
2024-11-24 13:32:27 +08:00
|
|
|
{
|
2024-11-29 06:57:47 +08:00
|
|
|
newFilename = $"{baseFilename}{source.Extension}";
|
2024-11-24 13:32:27 +08:00
|
|
|
|
2024-11-27 18:53:22 +08:00
|
|
|
foreach (var beatmapInSet in set.Beatmaps)
|
|
|
|
{
|
2024-11-29 06:57:47 +08:00
|
|
|
string filenameInBeatmap = readFilename(beatmapInSet.Metadata);
|
|
|
|
|
|
|
|
if (set.GetFile(filenameInBeatmap) is RealmNamedFileUsage existingFile)
|
2024-11-27 18:53:22 +08:00
|
|
|
beatmaps.DeleteFile(set, existingFile);
|
2022-08-01 15:36:12 +08:00
|
|
|
|
2024-11-29 06:57:47 +08:00
|
|
|
if (filenameInBeatmap != newFilename)
|
2024-11-27 18:53:22 +08:00
|
|
|
{
|
2024-11-29 06:57:47 +08:00
|
|
|
writeFilename(beatmapInSet.Metadata, newFilename);
|
2021-04-04 18:50:50 +08:00
|
|
|
|
2024-11-27 18:53:22 +08:00
|
|
|
if (!beatmapInSet.Equals(working.Value.BeatmapInfo))
|
|
|
|
beatmaps.Save(beatmapInSet, beatmaps.GetWorkingBeatmap(beatmapInSet).Beatmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var beatmap = working.Value.BeatmapInfo;
|
2024-11-24 14:11:28 +08:00
|
|
|
|
2024-11-27 18:53:22 +08:00
|
|
|
string[] filenames = set.Files.Select(f => f.Filename).Where(f =>
|
2024-11-29 06:57:47 +08:00
|
|
|
f.StartsWith(baseFilename, StringComparison.OrdinalIgnoreCase) &&
|
2024-11-27 18:53:22 +08:00
|
|
|
f.EndsWith(source.Extension, StringComparison.OrdinalIgnoreCase)).ToArray();
|
2024-11-24 14:11:28 +08:00
|
|
|
|
2024-11-29 06:57:47 +08:00
|
|
|
string currentFilename = readFilename(working.Value.Metadata);
|
2024-11-24 13:32:50 +08:00
|
|
|
|
2024-11-27 18:53:22 +08:00
|
|
|
var oldFile = set.GetFile(currentFilename);
|
2024-11-24 13:32:50 +08:00
|
|
|
|
2024-11-29 06:57:47 +08:00
|
|
|
if (oldFile != null && set.Beatmaps.Where(b => !b.Equals(beatmap)).All(b => readFilename(b.Metadata) != currentFilename))
|
2024-11-24 13:32:50 +08:00
|
|
|
{
|
2024-11-27 18:53:22 +08:00
|
|
|
beatmaps.DeleteFile(set, oldFile);
|
|
|
|
newFilename = currentFilename;
|
2024-11-24 13:32:50 +08:00
|
|
|
}
|
|
|
|
|
2024-11-29 06:57:47 +08:00
|
|
|
if (string.IsNullOrEmpty(newFilename))
|
|
|
|
newFilename = NamingUtils.GetNextBestFilename(filenames, $@"{baseFilename}{source.Extension}");
|
|
|
|
|
|
|
|
writeFilename(working.Value.Metadata, newFilename);
|
2024-11-27 18:53:22 +08:00
|
|
|
}
|
2024-11-24 13:32:50 +08:00
|
|
|
|
2024-11-27 18:53:22 +08:00
|
|
|
using (var stream = source.OpenRead())
|
2024-11-29 06:57:47 +08:00
|
|
|
beatmaps.AddFile(set, stream, newFilename);
|
2024-11-24 13:32:50 +08:00
|
|
|
|
2024-11-27 18:53:22 +08:00
|
|
|
editorBeatmap.SaveState();
|
2024-11-24 13:32:50 +08:00
|
|
|
}
|
|
|
|
|
2023-01-07 08:08:02 +08:00
|
|
|
private void backgroundChanged(ValueChangedEvent<FileInfo?> file)
|
2021-04-04 18:50:50 +08:00
|
|
|
{
|
2024-11-27 18:53:22 +08:00
|
|
|
if (file.NewValue == null || !ChangeBackgroundImage(file.NewValue, backgroundChooser.ApplyToAllDifficulties.Value))
|
2022-06-16 23:48:32 +08:00
|
|
|
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)
|
2020-10-06 14:17:15 +08:00
|
|
|
{
|
2024-11-27 18:53:22 +08:00
|
|
|
if (file.NewValue == null || !ChangeAudioTrack(file.NewValue, audioTrackChooser.ApplyToAllDifficulties.Value))
|
2022-06-16 23:48:32 +08:00
|
|
|
audioTrackChooser.Current.Value = file.OldValue;
|
2020-10-06 14:17:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|