1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 23:37:26 +08:00
osu-lazer/osu.Game/Screens/Edit/Setup/ResourcesSection.cs

159 lines
5.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.IO;
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;
namespace osu.Game.Screens.Edit.Setup
{
2022-11-24 13:32:20 +08:00
internal partial class ResourcesSection : SetupSection
{
2023-01-07 08:08:02 +08:00
private LabelledFileChooser audioTrackChooser = null!;
private LabelledFileChooser 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:08:02 +08:00
private EditorBeatmap editorBeatmap { get; set; } = null!;
[Resolved]
2023-01-07 08:03:52 +08:00
private Editor? editor { get; set; }
2021-04-04 18:50:50 +08:00
[Resolved]
2023-01-07 08:08:02 +08:00
private SetupScreenHeader header { get; set; } = null!;
2021-04-04 18:50:50 +08:00
[BackgroundDependencyLoader]
private void load()
{
Children = new Drawable[]
{
backgroundChooser = new LabelledFileChooser(".jpg", ".jpeg", ".png")
2021-04-04 18:50:50 +08:00
{
Label = GameplaySettingsStrings.BackgroundHeader,
2021-08-23 23:01:01 +08:00
FixedLabelWidth = LABEL_WIDTH,
TabbableContentContainer = this
2021-04-04 18:50:50 +08:00
},
audioTrackChooser = new LabelledFileChooser(".mp3", ".ogg")
{
Label = EditorSetupStrings.AudioTrack,
2021-08-23 23:01:01 +08:00
FixedLabelWidth = LABEL_WIDTH,
TabbableContentContainer = this
},
};
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);
updatePlaceholderText();
}
public bool ChangeBackgroundImage(FileInfo source)
{
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.GetFile(working.Value.Metadata.BackgroundFile);
using (var stream = source.OpenRead())
{
if (oldFile != null)
beatmaps.DeleteFile(set, oldFile);
beatmaps.AddFile(set, stream, destination.Name);
}
editorBeatmap.SaveState();
working.Value.Metadata.BackgroundFile = destination.Name;
header.Background.UpdateBackground();
2023-01-07 08:03:52 +08:00
editor?.ApplyToBackground(bg => bg.RefreshBackground());
return true;
}
public bool ChangeAudioTrack(FileInfo source)
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.GetFile(working.Value.Metadata.AudioFile);
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
editorBeatmap.SaveState();
music.ReloadCurrentTrack();
2021-04-04 18:50:50 +08:00
return true;
}
2023-01-07 08:08:02 +08:00
private void backgroundChanged(ValueChangedEvent<FileInfo?> file)
2021-04-04 18:50:50 +08:00
{
2023-01-07 23:15:57 +08:00
if (file.NewValue == null || !ChangeBackgroundImage(file.NewValue))
backgroundChooser.Current.Value = file.OldValue;
updatePlaceholderText();
2021-04-04 18:50:50 +08:00
}
2023-01-07 08:08:02 +08:00
private void audioTrackChanged(ValueChangedEvent<FileInfo?> file)
{
2023-01-07 23:15:57 +08:00
if (file.NewValue == null || !ChangeAudioTrack(file.NewValue))
audioTrackChooser.Current.Value = file.OldValue;
updatePlaceholderText();
}
private void updatePlaceholderText()
{
audioTrackChooser.Text = audioTrackChooser.Current.Value == null
? EditorSetupStrings.ClickToSelectTrack
: EditorSetupStrings.ClickToReplaceTrack;
backgroundChooser.Text = backgroundChooser.Current.Value == null
? EditorSetupStrings.ClickToSelectBackground
: EditorSetupStrings.ClickToReplaceBackground;
}
}
}