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

@ -1,144 +0,0 @@
// 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;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
namespace osu.Game.Screens.Edit.Setup
{
public class BackgroundChooser : CompositeDrawable, ICanAcceptFiles
{
public IEnumerable<string> HandledExtensions => ImageExtensions;
public static string[] ImageExtensions { get; } = { ".jpg", ".jpeg", ".png" };
[Resolved]
private OsuGameBase game { get; set; }
[Resolved]
private OsuColour colours { get; set; }
[Resolved]
private BeatmapManager beatmaps { get; set; }
[Resolved]
private IBindable<WorkingBeatmap> working { get; set; }
private readonly Container content;
public BackgroundChooser()
{
InternalChild = content = new Container
{
RelativeSizeAxes = Axes.Both,
Masking = true
};
}
[BackgroundDependencyLoader]
private void load()
{
updateBackgroundSprite();
}
protected override void LoadComplete()
{
base.LoadComplete();
game.RegisterImportHandler(this);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
game?.UnregisterImportHandler(this);
}
Task ICanAcceptFiles.Import(params string[] paths)
{
Schedule(() =>
{
var firstFile = new FileInfo(paths.First());
ChangeBackgroundImage(firstFile.FullName);
});
return Task.CompletedTask;
}
Task ICanAcceptFiles.Import(params ImportTask[] tasks) => throw new NotImplementedException();
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;
updateBackgroundSprite();
return true;
}
private void updateBackgroundSprite()
{
LoadComponentAsync(new BeatmapBackgroundSprite(working.Value)
{
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
FillMode = FillMode.Fill,
}, background =>
{
if (background.Texture != null)
content.Child = background;
else
{
content.Children = new Drawable[]
{
new Box
{
Colour = colours.GreySeafoamDarker,
RelativeSizeAxes = Axes.Both,
},
new OsuTextFlowContainer(t => t.Font = OsuFont.Default.With(size: 24))
{
Text = "Drag image here to set beatmap background!",
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.Both
}
};
}
background.FadeInFromZero(500);
});
}
}
}

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))

View File

@ -24,6 +24,9 @@ namespace osu.Game.Screens.Edit.Setup
[Cached]
private SectionsContainer<SetupSection> sections = new SectionsContainer<SetupSection>();
[Cached]
private SetupScreenHeader header = new SetupScreenHeader();
public SetupScreen()
: base(EditorScreenMode.SongSetup)
{
@ -51,7 +54,7 @@ namespace osu.Game.Screens.Edit.Setup
},
sections = new SectionsContainer<SetupSection>
{
FixedHeader = new SetupScreenHeader(),
FixedHeader = header,
RelativeSizeAxes = Axes.Both,
Children = new SetupSection[]
{

View File

@ -14,6 +14,8 @@ namespace osu.Game.Screens.Edit.Setup
{
internal class SetupScreenHeader : OverlayHeader
{
public SetupScreenHeaderBackground Background { get; private set; }
[Resolved]
private SectionsContainer<SetupSection> sections { get; set; }
@ -38,7 +40,7 @@ namespace osu.Game.Screens.Edit.Setup
RelativeSizeAxes = Axes.X,
Height = 30
},
new BackgroundChooser
Background = new SetupScreenHeaderBackground
{
RelativeSizeAxes = Axes.X,
Height = 120

View File

@ -0,0 +1,76 @@
// 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 osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
namespace osu.Game.Screens.Edit.Setup
{
public class SetupScreenHeaderBackground : CompositeDrawable
{
[Resolved]
private OsuColour colours { get; set; }
[Resolved]
private IBindable<WorkingBeatmap> working { get; set; }
private readonly Container content;
public SetupScreenHeaderBackground()
{
InternalChild = content = new Container
{
RelativeSizeAxes = Axes.Both,
Masking = true
};
}
[BackgroundDependencyLoader]
private void load()
{
UpdateBackground();
}
public void UpdateBackground()
{
LoadComponentAsync(new BeatmapBackgroundSprite(working.Value)
{
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
FillMode = FillMode.Fill,
}, background =>
{
if (background.Texture != null)
content.Child = background;
else
{
content.Children = new Drawable[]
{
new Box
{
Colour = colours.GreySeafoamDarker,
RelativeSizeAxes = Axes.Both,
},
new OsuTextFlowContainer(t => t.Font = OsuFont.Default.With(size: 24))
{
Text = "Drag image here to set beatmap background!",
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.Both
}
};
}
background.FadeInFromZero(500);
});
}
}
}