Add background chooser text box

This commit is contained in:
Bartłomiej Dach
2021-04-04 12:50:50 +02:00
parent 9394af32f5
commit f2d4ca7676
5 changed files with 139 additions and 151 deletions

View File

@ -17,6 +17,7 @@ namespace osu.Game.Screens.Edit.Setup
internal class ResourcesSection : SetupSection
{
private LabelledTextBox audioTrackTextBox;
private LabelledTextBox backgroundTextBox;
public override LocalisableString Title => "Resources";
@ -32,17 +33,26 @@ namespace osu.Game.Screens.Edit.Setup
[Resolved(canBeNull: true)]
private Editor editor { get; set; }
[Resolved]
private SetupScreenHeader header { get; set; }
[BackgroundDependencyLoader]
private void load()
{
Container audioTrackFileChooserContainer = new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
};
Container audioTrackFileChooserContainer = createFileChooserContainer();
Container backgroundFileChooserContainer = createFileChooserContainer();
Children = new Drawable[]
{
backgroundTextBox = new FileChooserLabelledTextBox(".jpg", ".jpeg", ".png")
{
Label = "Background",
PlaceholderText = "Click to select a background image",
Current = { Value = working.Value.Metadata.BackgroundFile },
Target = backgroundFileChooserContainer,
TabbableContentContainer = this
},
backgroundFileChooserContainer,
audioTrackTextBox = new FileChooserLabelledTextBox(".mp3", ".ogg")
{
Label = "Audio Track",
@ -54,9 +64,17 @@ namespace osu.Game.Screens.Edit.Setup
audioTrackFileChooserContainer,
};
backgroundTextBox.Current.BindValueChanged(backgroundChanged);
audioTrackTextBox.Current.BindValueChanged(audioTrackChanged);
}
private static Container createFileChooserContainer() =>
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
};
public bool ChangeAudioTrack(string path)
{
var info = new FileInfo(path);
@ -86,6 +104,39 @@ namespace osu.Game.Screens.Edit.Setup
return true;
}
public bool ChangeBackgroundImage(string path)
{
var info = new FileInfo(path);
if (!info.Exists)
return false;
var set = working.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 == working.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);
}
working.Value.Metadata.BackgroundFile = info.Name;
header.Background.UpdateBackground();
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))