2019-01-24 16:43:03 +08:00
|
|
|
|
// 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
|
|
|
|
|
2020-05-19 15:44:22 +08:00
|
|
|
|
using System;
|
2018-11-28 19:36:21 +08:00
|
|
|
|
using System.Collections.Generic;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using System.Linq;
|
2021-07-23 04:43:35 +08:00
|
|
|
|
using JetBrains.Annotations;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2019-03-27 18:29:27 +08:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2021-02-22 16:14:13 +08:00
|
|
|
|
using osu.Framework.Localisation;
|
2020-05-24 12:44:11 +08:00
|
|
|
|
using osu.Framework.Logging;
|
2021-11-25 15:36:30 +08:00
|
|
|
|
using osu.Framework.Platform;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Game.Configuration;
|
2021-11-25 15:36:30 +08:00
|
|
|
|
using osu.Game.Database;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
2021-08-12 11:40:22 +08:00
|
|
|
|
using osu.Game.Localisation;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Game.Skinning;
|
2021-07-20 18:36:12 +08:00
|
|
|
|
using osu.Game.Skinning.Editor;
|
2021-11-29 15:18:34 +08:00
|
|
|
|
using Realms;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Settings.Sections
|
|
|
|
|
{
|
|
|
|
|
public class SkinSection : SettingsSection
|
|
|
|
|
{
|
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
|
|
|
|
|
2020-04-26 02:35:46 +08:00
|
|
|
|
public override Drawable CreateIcon() => new SpriteIcon
|
|
|
|
|
{
|
|
|
|
|
Icon = FontAwesome.Solid.PaintBrush
|
|
|
|
|
};
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-11-29 17:04:07 +08:00
|
|
|
|
private readonly Bindable<ILive<SkinInfo>> dropdownBindable = new Bindable<ILive<SkinInfo>> { Default = SkinInfo.Default.ToLive() };
|
2021-11-23 15:04:55 +08:00
|
|
|
|
private readonly Bindable<string> configBindable = new Bindable<string>();
|
2018-11-14 17:02:38 +08:00
|
|
|
|
|
2021-11-29 17:04:07 +08:00
|
|
|
|
private static readonly ILive<SkinInfo> random_skin_info = new SkinInfo
|
2020-11-11 10:54:40 +08:00
|
|
|
|
{
|
|
|
|
|
ID = SkinInfo.RANDOM_SKIN,
|
|
|
|
|
Name = "<Random Skin>",
|
2021-11-29 17:04:07 +08:00
|
|
|
|
}.ToLive();
|
2018-11-25 10:50:26 +08:00
|
|
|
|
|
2021-11-29 17:04:07 +08:00
|
|
|
|
private List<ILive<SkinInfo>> skinItems;
|
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
|
|
|
|
|
2021-11-29 15:18:34 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private RealmContextFactory realmFactory { get; set; }
|
|
|
|
|
|
|
|
|
|
private IDisposable realmSubscription;
|
|
|
|
|
private IQueryable<SkinInfo> realmSkins;
|
|
|
|
|
|
2021-07-20 18:36:12 +08:00
|
|
|
|
[BackgroundDependencyLoader(permitNulls: true)]
|
2021-07-23 04:43:35 +08:00
|
|
|
|
private void load(OsuConfigManager config, [CanBeNull] SkinEditorOverlay skinEditor)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2021-10-12 13:42:22 +08:00
|
|
|
|
skinDropdown = new SkinSettingsDropdown
|
|
|
|
|
{
|
|
|
|
|
LabelText = SkinSettingsStrings.CurrentSkin
|
|
|
|
|
},
|
2021-07-20 18:36:12 +08:00
|
|
|
|
new SettingsButton
|
|
|
|
|
{
|
2021-08-12 11:40:22 +08:00
|
|
|
|
Text = SkinSettingsStrings.SkinLayoutEditor,
|
2021-07-20 18:36:12 +08:00
|
|
|
|
Action = () => skinEditor?.Toggle(),
|
|
|
|
|
},
|
2020-05-24 22:15:24 +08:00
|
|
|
|
new ExportSkinButton(),
|
2018-04-13 17:19:50 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-11-14 17:02:38 +08:00
|
|
|
|
config.BindWith(OsuSetting.Skin, configBindable);
|
2021-11-29 17:04:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
2018-11-25 10:50:26 +08:00
|
|
|
|
|
2020-10-06 16:18:41 +08:00
|
|
|
|
skinDropdown.Current = dropdownBindable;
|
2021-11-29 15:18:34 +08:00
|
|
|
|
|
|
|
|
|
realmSkins = realmFactory.Context.All<SkinInfo>()
|
|
|
|
|
.Where(s => !s.DeletePending)
|
|
|
|
|
.OrderBy(s => s.Name, StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
realmSubscription = realmSkins
|
|
|
|
|
.SubscribeForNotifications((sender, changes, error) =>
|
|
|
|
|
{
|
|
|
|
|
if (changes == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Eventually this should be handling the individual changes rather than refreshing the whole dropdown.
|
|
|
|
|
updateItems();
|
|
|
|
|
});
|
|
|
|
|
|
2020-11-11 10:34:48 +08:00
|
|
|
|
updateItems();
|
2018-07-02 15:49:07 +08:00
|
|
|
|
|
|
|
|
|
// Todo: This should not be necessary when OsuConfigManager is databased
|
2021-11-23 15:04:55 +08:00
|
|
|
|
if (!Guid.TryParse(configBindable.Value, out var configId) || skinDropdown.Items.All(s => s.ID != configId))
|
|
|
|
|
configBindable.Value = string.Empty;
|
2018-07-02 15:49:07 +08:00
|
|
|
|
|
2021-01-15 15:34:28 +08:00
|
|
|
|
configBindable.BindValueChanged(id => Scheduler.AddOnce(updateSelectedSkinFromConfig), true);
|
2019-07-29 00:55:57 +08:00
|
|
|
|
dropdownBindable.BindValueChanged(skin =>
|
2018-11-25 10:50:26 +08:00
|
|
|
|
{
|
2021-11-23 15:04:55 +08:00
|
|
|
|
if (skin.NewValue.Equals(random_skin_info))
|
2020-11-11 10:54:40 +08:00
|
|
|
|
{
|
2021-11-29 15:18:34 +08:00
|
|
|
|
var skinBefore = skins.CurrentSkinInfo.Value;
|
|
|
|
|
|
2020-11-11 12:05:03 +08:00
|
|
|
|
skins.SelectRandomSkin();
|
2021-11-29 15:18:34 +08:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 10:54:40 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 15:04:55 +08:00
|
|
|
|
configBindable.Value = skin.NewValue.ID.ToString();
|
2018-11-25 10:50:26 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-29 18:16:10 +08:00
|
|
|
|
private void updateSelectedSkinFromConfig()
|
|
|
|
|
{
|
2021-11-23 15:04:55 +08:00
|
|
|
|
if (!Guid.TryParse(configBindable.Value, out var configId)) return;
|
2021-01-29 18:16:10 +08:00
|
|
|
|
|
2021-11-23 15:04:55 +08:00
|
|
|
|
var skin = skinDropdown.Items.FirstOrDefault(s => s.ID == configId);
|
2021-01-29 18:16:10 +08:00
|
|
|
|
|
2021-11-29 15:18:34 +08:00
|
|
|
|
// TODO: i don't think this will be required any more.
|
2021-01-29 18:16:10 +08:00
|
|
|
|
if (skin == null)
|
|
|
|
|
{
|
|
|
|
|
// there may be a thread race condition where an item is selected that hasn't yet been added to the dropdown.
|
|
|
|
|
// to avoid adding complexity, let's just ensure the item is added so we can perform the selection.
|
2021-11-23 15:04:55 +08:00
|
|
|
|
skin = skins.Query(s => s.ID == configId);
|
2021-11-29 15:18:34 +08:00
|
|
|
|
updateItems();
|
2021-01-29 18:16:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dropdownBindable.Value = skin;
|
|
|
|
|
}
|
2021-01-15 15:34:28 +08:00
|
|
|
|
|
2020-11-11 10:34:48 +08:00
|
|
|
|
private void updateItems()
|
2018-11-28 19:36:21 +08:00
|
|
|
|
{
|
2021-11-29 15:18:34 +08:00
|
|
|
|
skinItems = realmSkins.ToLive();
|
2018-11-28 19:36:21 +08:00
|
|
|
|
|
2021-11-29 15:18:34 +08:00
|
|
|
|
skinItems.Insert(0, SkinInfo.Default.ToLive());
|
|
|
|
|
skinItems.Insert(1, DefaultLegacySkin.Info.ToLive());
|
|
|
|
|
skinItems.Insert(2, random_skin_info);
|
2021-01-29 18:16:10 +08:00
|
|
|
|
|
2021-11-29 15:18:34 +08:00
|
|
|
|
skinDropdown.Items = skinItems;
|
2021-01-19 03:51:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 17:05:31 +08:00
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
2021-11-29 15:18:34 +08:00
|
|
|
|
realmSubscription?.Dispose();
|
2021-11-05 17:05:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 17:04:07 +08:00
|
|
|
|
private class SkinSettingsDropdown : SettingsDropdown<ILive<SkinInfo>>
|
2018-11-14 17:02:38 +08:00
|
|
|
|
{
|
2021-11-29 17:04:07 +08:00
|
|
|
|
protected override OsuDropdown<ILive<SkinInfo>> CreateDropdown() => new SkinDropdownControl();
|
2018-11-14 17:02:38 +08:00
|
|
|
|
|
|
|
|
|
private class SkinDropdownControl : DropdownControl
|
|
|
|
|
{
|
2021-11-29 17:04:07 +08:00
|
|
|
|
protected override LocalisableString GenerateItemText(ILive<SkinInfo> item) => item.ToString();
|
2018-11-14 17:02:38 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-25 10:50:26 +08:00
|
|
|
|
|
2020-05-24 22:15:24 +08:00
|
|
|
|
private class ExportSkinButton : SettingsButton
|
|
|
|
|
{
|
|
|
|
|
[Resolved]
|
|
|
|
|
private SkinManager skins { get; set; }
|
|
|
|
|
|
2021-11-25 15:36:30 +08:00
|
|
|
|
[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;
|
2021-11-29 15:18:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
2020-05-24 22:15:24 +08:00
|
|
|
|
|
|
|
|
|
currentSkin = skins.CurrentSkin.GetBoundCopy();
|
2021-11-29 17:04:07 +08:00
|
|
|
|
currentSkin.BindValueChanged(skin => Enabled.Value = skin.NewValue.SkinInfo.PerformRead(s => s.IsManaged), true);
|
2020-05-24 22:15:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void export()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-11-29 17:04:07 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|