1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-13 18:32:55 +08:00

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))
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);
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.
/// todo: this should probably not use PopoverTextBox just to display placeholder text, but is the best way for now.
/// </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;
public IEnumerable<string> HandledExtensions => handledExtensions;
private readonly Bindable<FileInfo?> currentFile = new Bindable<FileInfo?>();
[Resolved]
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;
set => current.Current = value;
@ -68,21 +66,18 @@ namespace osu.Game.Screens.Edit.Setup
base.LoadComplete();
game.RegisterImportHandler(this);
currentFile.BindValueChanged(onFileSelected);
Current.BindValueChanged(onFileSelected);
}
private void onFileSelected(ValueChangedEvent<FileInfo?> file)
{
if (file.NewValue == null)
return;
this.HidePopover();
Current.Value = file.NewValue.FullName;
if (file.NewValue != null)
this.HidePopover();
}
Task ICanAcceptFiles.Import(params string[] paths)
{
Schedule(() => currentFile.Value = new FileInfo(paths.First()));
Schedule(() => Current.Value = new FileInfo(paths.First()));
return Task.CompletedTask;
}
@ -105,7 +100,7 @@ namespace osu.Game.Screens.Edit.Setup
OnFocused = this.ShowPopover,
};
public Popover GetPopover() => new FileChooserPopover(handledExtensions, currentFile);
public Popover GetPopover() => new FileChooserPopover(handledExtensions, Current);
private class FileChooserPopover : OsuPopover
{

View File

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