1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 08:22:56 +08:00
osu-lazer/osu.Game/Overlays/Settings/Sections/SkinSection.cs

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

243 lines
8.1 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.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2020-05-19 15:44:22 +08:00
using System;
2018-11-28 19:36:21 +08:00
using System.Collections.Generic;
using System.Linq;
2021-07-23 04:43:35 +08:00
using JetBrains.Annotations;
2017-02-07 12:52:19 +08:00
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2017-02-07 12:52:19 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Localisation;
2020-05-24 12:44:11 +08:00
using osu.Framework.Logging;
using osu.Framework.Platform;
2017-02-07 12:52:19 +08:00
using osu.Game.Configuration;
using osu.Game.Database;
2017-02-07 12:52:19 +08:00
using osu.Game.Graphics.UserInterface;
2021-08-12 11:40:22 +08:00
using osu.Game.Localisation;
using osu.Game.Screens.Select;
using osu.Game.Skinning;
using osu.Game.Skinning.Editor;
using Realms;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays.Settings.Sections
2017-02-07 12:52:19 +08:00
{
public class SkinSection : SettingsSection
2017-02-07 12:52:19 +08:00
{
2018-11-14 17:02:38 +08:00
private SkinSettingsDropdown skinDropdown;
2018-04-13 17:19:50 +08:00
2021-08-12 11:40:22 +08:00
public override LocalisableString Header => SkinSettingsStrings.SkinSectionHeader;
2018-04-13 17:19:50 +08:00
public override Drawable CreateIcon() => new SpriteIcon
{
Icon = FontAwesome.Solid.PaintBrush
};
2018-04-13 17:19:50 +08:00
private readonly Bindable<Live<SkinInfo>> dropdownBindable = new Bindable<Live<SkinInfo>> { Default = DefaultSkin.CreateInfo().ToLiveUnmanaged() };
private readonly Bindable<string> configBindable = new Bindable<string>();
2018-11-14 17:02:38 +08:00
private static readonly Live<SkinInfo> random_skin_info = new SkinInfo
{
ID = SkinInfo.RANDOM_SKIN,
Name = "<Random Skin>",
}.ToLiveUnmanaged();
2018-11-25 10:50:26 +08:00
private readonly List<Live<SkinInfo>> dropdownItems = new List<Live<SkinInfo>>();
2018-04-13 17:19:50 +08:00
2020-02-14 21:14:00 +08:00
[Resolved]
private SkinManager skins { get; set; }
2018-04-13 17:19:50 +08:00
[Resolved]
private RealmAccess realm { get; set; }
private IDisposable realmSubscription;
[BackgroundDependencyLoader(permitNulls: true)]
2021-07-23 04:43:35 +08:00
private void load(OsuConfigManager config, [CanBeNull] SkinEditorOverlay skinEditor)
2017-02-07 12:52:19 +08:00
{
Children = new Drawable[]
{
2021-10-12 13:42:22 +08:00
skinDropdown = new SkinSettingsDropdown
{
LabelText = SkinSettingsStrings.CurrentSkin,
Keywords = new[] { @"skins" }
2021-10-12 13:42:22 +08:00
},
new SettingsButton
{
2021-08-12 11:40:22 +08:00
Text = SkinSettingsStrings.SkinLayoutEditor,
Action = () => skinEditor?.ToggleVisibility(),
},
2020-05-24 22:15:24 +08:00
new ExportSkinButton(),
new DeleteSkinButton(),
2017-02-07 12:52:19 +08:00
};
2018-04-13 17:19:50 +08:00
2018-11-14 17:02:38 +08:00
config.BindWith(OsuSetting.Skin, configBindable);
}
protected override void LoadComplete()
{
base.LoadComplete();
2018-11-25 10:50:26 +08:00
skinDropdown.Current = dropdownBindable;
2022-06-24 20:25:23 +08:00
realmSubscription = realm.RegisterForNotifications(_ => realm.Realm.All<SkinInfo>()
.Where(s => !s.DeletePending)
.OrderByDescending(s => s.Protected) // protected skins should be at the top.
.ThenBy(s => s.Name, StringComparer.OrdinalIgnoreCase), skinsChanged);
2022-06-24 20:25:23 +08:00
configBindable.BindValueChanged(_ => Scheduler.AddOnce(updateSelectedSkinFromConfig));
dropdownBindable.BindValueChanged(dropdownSelectionChanged);
}
private void dropdownSelectionChanged(ValueChangedEvent<Live<SkinInfo>> skin)
{
// Only handle cases where it's clear the user has intent to change skins.
if (skin.OldValue == null) return;
if (skin.NewValue.Equals(random_skin_info))
2018-11-25 10:50:26 +08:00
{
var skinBefore = skins.CurrentSkinInfo.Value;
skins.SelectRandomSkin();
if (skinBefore == skins.CurrentSkinInfo.Value)
{
// the random selection didn't change the skin, so we should manually update the dropdown to match.
dropdownBindable.Value = skins.CurrentSkinInfo.Value;
}
return;
}
configBindable.Value = skin.NewValue.ID.ToString();
}
private void skinsChanged(IRealmCollection<SkinInfo> sender, ChangeSet changes, Exception error)
{
// This can only mean that realm is recycling, else we would see the protected skins.
// Because we are using `Live<>` in this class, we don't need to worry about this scenario too much.
if (!sender.Any())
return;
int protectedCount = sender.Count(s => s.Protected);
// For simplicity repopulate the full list.
// In the future we should change this to properly handle ChangeSet events.
dropdownItems.Clear();
foreach (var skin in sender)
dropdownItems.Add(skin.ToLive(realm));
dropdownItems.Insert(protectedCount, random_skin_info);
Schedule(() =>
{
skinDropdown.Items = dropdownItems;
updateSelectedSkinFromConfig();
});
}
2018-11-25 10:50:26 +08:00
private void updateSelectedSkinFromConfig()
{
if (!skinDropdown.Items.Any())
return;
Live<SkinInfo> skin = null;
if (Guid.TryParse(configBindable.Value, out var configId))
skin = skinDropdown.Items.FirstOrDefault(s => s.ID == configId);
dropdownBindable.Value = skin ?? skinDropdown.Items.First();
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
realmSubscription?.Dispose();
}
private class SkinSettingsDropdown : SettingsDropdown<Live<SkinInfo>>
2018-11-14 17:02:38 +08:00
{
protected override OsuDropdown<Live<SkinInfo>> CreateDropdown() => new SkinDropdownControl();
2018-11-14 17:02:38 +08:00
private class SkinDropdownControl : DropdownControl
{
protected override LocalisableString GenerateItemText(Live<SkinInfo> item) => item.ToString();
2018-11-14 17:02:38 +08:00
}
}
2018-11-25 10:50:26 +08:00
public class ExportSkinButton : SettingsButton
2020-05-24 22:15:24 +08:00
{
[Resolved]
private SkinManager skins { get; set; }
[Resolved]
private Storage storage { get; set; }
2020-05-24 22:15:24 +08:00
private Bindable<Skin> currentSkin;
[BackgroundDependencyLoader]
private void load()
{
2021-08-12 11:40:22 +08:00
Text = SkinSettingsStrings.ExportSkinButton;
2020-05-24 22:15:24 +08:00
Action = export;
}
protected override void LoadComplete()
{
base.LoadComplete();
2020-05-24 22:15:24 +08:00
currentSkin = skins.CurrentSkin.GetBoundCopy();
currentSkin.BindValueChanged(skin => Enabled.Value = skin.NewValue.SkinInfo.PerformRead(s => !s.Protected), true);
2020-05-24 22:15:24 +08:00
}
private void export()
{
try
{
currentSkin.Value.SkinInfo.PerformRead(s => new LegacySkinExporter(storage).Export(s));
2020-05-24 22:15:24 +08:00
}
catch (Exception e)
{
Logger.Log($"Could not export current skin: {e.Message}", level: LogLevel.Error);
}
}
}
public class DeleteSkinButton : DangerousSettingsButton
{
[Resolved]
private SkinManager skins { get; set; }
[Resolved(CanBeNull = true)]
private IDialogOverlay dialogOverlay { get; set; }
private Bindable<Skin> currentSkin;
[BackgroundDependencyLoader]
private void load()
{
Text = SkinSettingsStrings.DeleteSkinButton;
Action = delete;
}
protected override void LoadComplete()
{
base.LoadComplete();
currentSkin = skins.CurrentSkin.GetBoundCopy();
currentSkin.BindValueChanged(skin => Enabled.Value = skin.NewValue.SkinInfo.PerformRead(s => !s.Protected), true);
}
private void delete()
{
2022-06-08 16:57:59 +08:00
dialogOverlay?.Push(new SkinDeleteDialog(currentSkin.Value));
}
}
2017-02-07 12:52:19 +08:00
}
2017-03-18 22:22:03 +08:00
}