1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 11:47:52 +08:00
osu-lazer/osu.Game/Screens/Edit/Setup/MetadataSection.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

130 lines
5.5 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.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Localisation;
2021-06-10 21:38:56 +08:00
using osu.Game.Beatmaps;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Resources.Localisation.Web;
using osu.Game.Localisation;
namespace osu.Game.Screens.Edit.Setup
{
public partial class MetadataSection : SetupSection
{
protected FormTextBox ArtistTextBox = null!;
protected FormTextBox RomanisedArtistTextBox = null!;
2021-06-10 21:38:56 +08:00
protected FormTextBox TitleTextBox = null!;
protected FormTextBox RomanisedTitleTextBox = null!;
private FormTextBox creatorTextBox = null!;
private FormTextBox difficultyTextBox = null!;
private FormTextBox sourceTextBox = null!;
private FormTextBox tagsTextBox = null!;
public override LocalisableString Title => EditorSetupStrings.MetadataHeader;
[BackgroundDependencyLoader]
private void load()
{
2021-06-10 21:38:56 +08:00
var metadata = Beatmap.Metadata;
Children = new[]
{
ArtistTextBox = createTextBox<FormTextBox>(EditorSetupStrings.Artist,
2021-06-10 21:38:56 +08:00
!string.IsNullOrEmpty(metadata.ArtistUnicode) ? metadata.ArtistUnicode : metadata.Artist),
RomanisedArtistTextBox = createTextBox<FormRomanisedTextBox>(EditorSetupStrings.RomanisedArtist,
2021-06-10 21:38:56 +08:00
!string.IsNullOrEmpty(metadata.Artist) ? metadata.Artist : MetadataUtils.StripNonRomanisedCharacters(metadata.ArtistUnicode)),
TitleTextBox = createTextBox<FormTextBox>(EditorSetupStrings.Title,
2021-06-10 21:38:56 +08:00
!string.IsNullOrEmpty(metadata.TitleUnicode) ? metadata.TitleUnicode : metadata.Title),
RomanisedTitleTextBox = createTextBox<FormRomanisedTextBox>(EditorSetupStrings.RomanisedTitle,
2021-06-10 21:38:56 +08:00
!string.IsNullOrEmpty(metadata.Title) ? metadata.Title : MetadataUtils.StripNonRomanisedCharacters(metadata.ArtistUnicode)),
creatorTextBox = createTextBox<FormTextBox>(EditorSetupStrings.Creator, metadata.Author.Username),
difficultyTextBox = createTextBox<FormTextBox>(EditorSetupStrings.DifficultyName, Beatmap.BeatmapInfo.DifficultyName),
sourceTextBox = createTextBox<FormTextBox>(BeatmapsetsStrings.ShowInfoSource, metadata.Source),
tagsTextBox = createTextBox<FormTextBox>(BeatmapsetsStrings.ShowInfoTags, metadata.Tags)
};
}
private TTextBox createTextBox<TTextBox>(LocalisableString label, string initialValue)
where TTextBox : FormTextBox, new()
2021-06-10 21:38:56 +08:00
=> new TTextBox
{
Caption = label,
2021-06-10 21:38:56 +08:00
Current = { Value = initialValue },
TabbableContentContainer = this
};
protected override void LoadComplete()
{
base.LoadComplete();
if (string.IsNullOrEmpty(ArtistTextBox.Current.Value))
ScheduleAfterChildren(() => GetContainingFocusManager()!.ChangeFocus(ArtistTextBox));
2021-06-10 21:38:56 +08:00
ArtistTextBox.Current.BindValueChanged(artist => transferIfRomanised(artist.NewValue, RomanisedArtistTextBox));
TitleTextBox.Current.BindValueChanged(title => transferIfRomanised(title.NewValue, RomanisedTitleTextBox));
foreach (var item in Children.OfType<FormTextBox>())
item.OnCommit += onCommit;
2021-06-10 21:38:56 +08:00
updateReadOnlyState();
}
private void transferIfRomanised(string value, FormTextBox target)
2021-06-10 21:38:56 +08:00
{
if (MetadataUtils.IsRomanised(value))
target.Current.Value = value;
updateReadOnlyState();
}
private void updateReadOnlyState()
{
RomanisedArtistTextBox.ReadOnly = MetadataUtils.IsRomanised(ArtistTextBox.Current.Value);
RomanisedTitleTextBox.ReadOnly = MetadataUtils.IsRomanised(TitleTextBox.Current.Value);
}
private void onCommit(TextBox sender, bool newText)
{
if (!newText) return;
2021-06-10 21:38:56 +08:00
// for now, update on commit rather than making BeatmapMetadata bindables.
// after switching database engines we can reconsider if switching to bindables is a good direction.
updateMetadata();
2021-06-10 21:38:56 +08:00
}
private void updateMetadata()
{
Beatmap.Metadata.ArtistUnicode = ArtistTextBox.Current.Value;
Beatmap.Metadata.Artist = RomanisedArtistTextBox.Current.Value;
2021-06-10 21:38:56 +08:00
Beatmap.Metadata.TitleUnicode = TitleTextBox.Current.Value;
Beatmap.Metadata.Title = RomanisedTitleTextBox.Current.Value;
2022-01-18 22:30:40 +08:00
Beatmap.Metadata.Author.Username = creatorTextBox.Current.Value;
Beatmap.BeatmapInfo.DifficultyName = difficultyTextBox.Current.Value;
Beatmap.Metadata.Source = sourceTextBox.Current.Value;
Beatmap.Metadata.Tags = tagsTextBox.Current.Value;
Beatmap.SaveState();
}
private partial class FormRomanisedTextBox : FormTextBox
{
internal override InnerTextBox CreateTextBox() => new RomanisedTextBox();
private partial class RomanisedTextBox : InnerTextBox
{
protected override bool AllowIme => false;
protected override bool CanAddCharacter(char character)
=> MetadataUtils.IsRomanised(character);
}
}
}
}