Use FileInfo as current bindable type

This commit is contained in:
Salman Ahmed 2022-06-15 19:29:09 +03:00
parent 5d74d92fcf
commit 2c35b1404b
3 changed files with 27 additions and 32 deletions

View File

@ -96,7 +96,7 @@ namespace osu.Game.Tests.Visual.Editing
using (var zip = ZipArchive.Open(temp)) using (var zip = ZipArchive.Open(temp))
zip.WriteToDirectory(extractedFolder); zip.WriteToDirectory(extractedFolder);
bool success = setup.ChildrenOfType<ResourcesSection>().First().ChangeAudioTrack(Path.Combine(extractedFolder, "03. Renatus - Soleily 192kbps.mp3")); bool success = setup.ChildrenOfType<ResourcesSection>().First().ChangeAudioTrack(new FileInfo(Path.Combine(extractedFolder, "03. Renatus - Soleily 192kbps.mp3")));
File.Delete(temp); File.Delete(temp);
Directory.Delete(extractedFolder, true); Directory.Delete(extractedFolder, true);

View File

@ -27,20 +27,18 @@ namespace osu.Game.Screens.Edit.Setup
/// A labelled drawable displaying file chooser on click, with placeholder text support. /// A labelled drawable displaying file chooser on click, with placeholder text support.
/// todo: this should probably not use PopoverTextBox just to display placeholder text, but is the best way for now. /// todo: this should probably not use PopoverTextBox just to display placeholder text, but is the best way for now.
/// </summary> /// </summary>
internal class LabelledFileChooser : LabelledDrawable<LabelledTextBoxWithPopover.PopoverTextBox>, IHasCurrentValue<string>, ICanAcceptFiles, IHasPopover internal class LabelledFileChooser : LabelledDrawable<LabelledTextBoxWithPopover.PopoverTextBox>, IHasCurrentValue<FileInfo?>, ICanAcceptFiles, IHasPopover
{ {
private readonly string[] handledExtensions; private readonly string[] handledExtensions;
public IEnumerable<string> HandledExtensions => handledExtensions; public IEnumerable<string> HandledExtensions => handledExtensions;
private readonly Bindable<FileInfo?> currentFile = new Bindable<FileInfo?>();
[Resolved] [Resolved]
private OsuGameBase game { get; set; } = null!; private OsuGameBase game { get; set; } = null!;
private readonly BindableWithCurrent<string> current = new BindableWithCurrent<string>(); private readonly BindableWithCurrent<FileInfo?> current = new BindableWithCurrent<FileInfo?>();
public Bindable<string> Current public Bindable<FileInfo?> Current
{ {
get => current.Current; get => current.Current;
set => current.Current = value; set => current.Current = value;
@ -68,21 +66,18 @@ namespace osu.Game.Screens.Edit.Setup
base.LoadComplete(); base.LoadComplete();
game.RegisterImportHandler(this); game.RegisterImportHandler(this);
currentFile.BindValueChanged(onFileSelected); Current.BindValueChanged(onFileSelected);
} }
private void onFileSelected(ValueChangedEvent<FileInfo?> file) private void onFileSelected(ValueChangedEvent<FileInfo?> file)
{ {
if (file.NewValue == null) if (file.NewValue != null)
return;
this.HidePopover(); this.HidePopover();
Current.Value = file.NewValue.FullName;
} }
Task ICanAcceptFiles.Import(params string[] paths) Task ICanAcceptFiles.Import(params string[] paths)
{ {
Schedule(() => currentFile.Value = new FileInfo(paths.First())); Schedule(() => Current.Value = new FileInfo(paths.First()));
return Task.CompletedTask; return Task.CompletedTask;
} }
@ -105,7 +100,7 @@ namespace osu.Game.Screens.Edit.Setup
OnFocused = this.ShowPopover, OnFocused = this.ShowPopover,
}; };
public Popover GetPopover() => new FileChooserPopover(handledExtensions, currentFile); public Popover GetPopover() => new FileChooserPopover(handledExtensions, Current);
private class FileChooserPopover : OsuPopover private class FileChooserPopover : OsuPopover
{ {

View File

@ -43,26 +43,28 @@ namespace osu.Game.Screens.Edit.Setup
{ {
Label = "Background", Label = "Background",
FixedLabelWidth = LABEL_WIDTH, FixedLabelWidth = LABEL_WIDTH,
Current = { Value = working.Value.Metadata.BackgroundFile },
TabbableContentContainer = this TabbableContentContainer = this
}, },
audioTrackChooser = new LabelledFileChooser(".mp3", ".ogg") audioTrackChooser = new LabelledFileChooser(".mp3", ".ogg")
{ {
Label = "Audio Track", Label = "Audio Track",
FixedLabelWidth = LABEL_WIDTH, FixedLabelWidth = LABEL_WIDTH,
Current = { Value = working.Value.Metadata.AudioFile },
TabbableContentContainer = this 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, true); backgroundChooser.Current.BindValueChanged(backgroundChanged, true);
audioTrackChooser.Current.BindValueChanged(audioTrackChanged, true); audioTrackChooser.Current.BindValueChanged(audioTrackChanged, true);
} }
public bool ChangeBackgroundImage(string path) public bool ChangeBackgroundImage(FileInfo source)
{ {
var source = new FileInfo(path);
if (!source.Exists) if (!source.Exists)
return false; return false;
@ -88,10 +90,8 @@ namespace osu.Game.Screens.Edit.Setup
return true; return true;
} }
public bool ChangeAudioTrack(string path) public bool ChangeAudioTrack(FileInfo source)
{ {
var source = new FileInfo(path);
if (!source.Exists) if (!source.Exists)
return false; return false;
@ -119,29 +119,29 @@ namespace osu.Game.Screens.Edit.Setup
return true; return true;
} }
private void backgroundChanged(ValueChangedEvent<string> filePath) private void backgroundChanged(ValueChangedEvent<FileInfo> file)
{ {
backgroundChooser.Text = string.IsNullOrEmpty(filePath.NewValue) backgroundChooser.Text = file.NewValue == null
? "Click to select a background image" ? "Click to select a background image"
: "Click to replace the background image"; : "Click to replace the background image";
if (filePath.NewValue != filePath.OldValue) if (file.NewValue != file.OldValue)
{ {
if (!ChangeBackgroundImage(filePath.NewValue)) if (!ChangeBackgroundImage(file.NewValue))
backgroundChooser.Current.Value = filePath.OldValue; backgroundChooser.Current.Value = file.OldValue;
} }
} }
private void audioTrackChanged(ValueChangedEvent<string> filePath) private void audioTrackChanged(ValueChangedEvent<FileInfo> file)
{ {
audioTrackChooser.Text = string.IsNullOrEmpty(filePath.NewValue) audioTrackChooser.Text = file.NewValue == null
? "Click to select a track" ? "Click to select a track"
: "Click to replace the track"; : "Click to replace the track";
if (filePath.NewValue != filePath.OldValue) if (file.NewValue != file.OldValue)
{ {
if (!ChangeAudioTrack(filePath.NewValue)) if (!ChangeAudioTrack(file.NewValue))
audioTrackChooser.Current.Value = filePath.OldValue; audioTrackChooser.Current.Value = file.OldValue;
} }
} }
} }