From 73f009757cf3da1767de82727357b37a524c446f Mon Sep 17 00:00:00 2001 From: rootdarkarchon Date: Sun, 12 Feb 2023 13:45:10 +0100 Subject: [PATCH] add dialog to add secret key to main ui when no secret key is present --- .../Managers/ServerConfigurationManager.cs | 2 +- MareSynchronos/MareSynchronos.csproj | 2 +- MareSynchronos/UI/CompactUI.cs | 37 +++++++- MareSynchronos/UI/SettingsUi.cs | 89 ++++++------------- MareSynchronos/UI/UIShared.cs | 40 +++++++++ 5 files changed, 107 insertions(+), 63 deletions(-) diff --git a/MareSynchronos/Managers/ServerConfigurationManager.cs b/MareSynchronos/Managers/ServerConfigurationManager.cs index 96856e3..71a4151 100644 --- a/MareSynchronos/Managers/ServerConfigurationManager.cs +++ b/MareSynchronos/Managers/ServerConfigurationManager.cs @@ -3,7 +3,6 @@ using MareSynchronos.MareConfiguration.Models; using MareSynchronos.Models; using MareSynchronos.Utils; using MareSynchronos.WebAPI; -using Microsoft.Extensions.Logging.Abstractions; using System.Diagnostics; namespace MareSynchronos.Managers; @@ -172,6 +171,7 @@ public class ServerConfigurationManager { var server = GetServerByIndex(serverSelectionIndex); server.Authentications.Remove(item); + Save(); } internal void AddServer(ServerStorage serverStorage) diff --git a/MareSynchronos/MareSynchronos.csproj b/MareSynchronos/MareSynchronos.csproj index ba2566f..fadeb0e 100644 --- a/MareSynchronos/MareSynchronos.csproj +++ b/MareSynchronos/MareSynchronos.csproj @@ -3,7 +3,7 @@ - 0.7.25 + 0.7.26 https://github.com/Penumbra-Sync/client diff --git a/MareSynchronos/UI/CompactUI.cs b/MareSynchronos/UI/CompactUI.cs index ced41ba..4c6487d 100644 --- a/MareSynchronos/UI/CompactUI.cs +++ b/MareSynchronos/UI/CompactUI.cs @@ -722,9 +722,44 @@ public class CompactUi : WindowMediatorSubscriberBase, IDisposable if (_apiController.ServerState is not ServerState.Connected) { UiShared.ColorTextWrapped(GetServerError(), GetUidColor()); + if (_apiController.ServerState is ServerState.NoSecretKey) + { + DrawAddCharacter(); + } } } + private void DrawAddCharacter() + { + ImGui.Dummy(new(10)); + var keys = _serverManager.CurrentServer!.SecretKeys; + if (keys.TryGetValue(secretKeyIdx, out var secretKey)) + { + var friendlyName = secretKey.FriendlyName; + + if (UiShared.IconTextButton(FontAwesomeIcon.Plus, "Add current character with secret key")) + { + _serverManager.CurrentServer!.Authentications.Add(new MareConfiguration.Models.Authentication() + { + CharacterName = _uiShared.PlayerName, + WorldId = _uiShared.WorldId, + SecretKeyIdx = secretKeyIdx + }); + + _serverManager.Save(); + + _ = _apiController.CreateConnections(true); + } + + _uiShared.DrawCombo("Secret Key##addCharacterSecretKey", keys, (f) => f.Value.FriendlyName, (f) => secretKeyIdx = f.Key); + } + else + { + UiShared.ColorTextWrapped("No secret keys are configured for the current server.", ImGuiColors.DalamudYellow); + } + } + + private int secretKeyIdx = 0; private string GetServerError() { @@ -739,7 +774,7 @@ public class CompactUi : WindowMediatorSubscriberBase, IDisposable "Your plugin or the server you are connecting to is out of date. Please update your plugin now. If you already did so, contact the server provider to update their server to the latest version.", ServerState.RateLimited => "You are rate limited for (re)connecting too often. Disconnect, wait 10 minutes and try again.", ServerState.Connected => string.Empty, - ServerState.NoSecretKey => "You have no secret key set for this current character. Open the settings and set a secret key for this current character. If this character does not exist in Mare, it needs to be added. You can reuse one secret key for different characters.", + ServerState.NoSecretKey => "You have no secret key set for this current character. Use the button below or open the settings and set a secret key for the current character. You can reuse the same secret key for multiple characters.", _ => string.Empty }; } diff --git a/MareSynchronos/UI/SettingsUi.cs b/MareSynchronos/UI/SettingsUi.cs index 7e9ee49..0645c4d 100644 --- a/MareSynchronos/UI/SettingsUi.cs +++ b/MareSynchronos/UI/SettingsUi.cs @@ -275,33 +275,25 @@ public class SettingsUi : WindowMediatorSubscriberBase, IDisposable _serverConfigurationManager.Save(); } - if (ImGui.BeginCombo("World", worldPreview)) - { - foreach (var world in data) + _uiShared.DrawCombo("World##" + item.CharacterName + i, data, (w) => w.Value, + (w) => { - bool isSelected = worldIdx == world.Key; - if (ImGui.Selectable(world.Value, isSelected)) + if (item.WorldId != w.Key) { - item.WorldId = world.Key; + item.WorldId = w.Key; _serverConfigurationManager.Save(); } - } - ImGui.EndCombo(); - } + }, EqualityComparer>.Default.Equals(data.FirstOrDefault(f => f.Key == worldIdx), default) ? data.First() : data.First(f => f.Key == worldIdx)); - if (ImGui.BeginCombo("Secret Key", friendlyName)) - { - foreach (var kvp in keys) + _uiShared.DrawCombo("Secret Key##" + item.CharacterName + i, keys, (w) => w.Value.FriendlyName, + (w) => { - bool isSelected = kvp.Key == secretKeyIdx; - if (ImGui.Selectable(kvp.Value.FriendlyName, isSelected)) + if (w.Key != item.SecretKeyIdx) { - item.SecretKeyIdx = kvp.Key; + item.SecretKeyIdx = w.Key; _serverConfigurationManager.Save(); } - } - ImGui.EndCombo(); - } + }, EqualityComparer>.Default.Equals(keys.FirstOrDefault(f => f.Key == item.SecretKeyIdx), default) ? keys.First() : keys.First(f => f.Key == item.SecretKeyIdx)); if (UiShared.IconTextButton(FontAwesomeIcon.Trash, "Delete Character")) { @@ -482,63 +474,40 @@ public class SettingsUi : WindowMediatorSubscriberBase, IDisposable var onlineNotifs = _configService.Current.ShowOnlineNotifications; var onlineNotifsPairsOnly = _configService.Current.ShowOnlineNotificationsOnlyForIndividualPairs; var onlineNotifsNamedOnly = _configService.Current.ShowOnlineNotificationsOnlyForNamedPairs; - var infoNotifLocation = _configService.Current.InfoNotification; var warnNotifLocation = _configService.Current.WarningNotification; var errorNotifLocation = _configService.Current.ErrorNotification; UiShared.FontText("Notifications", _uiShared.UidFont); - if (ImGui.BeginCombo("Info Notification Display", infoNotifLocation.ToString())) + _uiShared.DrawCombo("Info Notification Display##settingsUi", (NotificationLocation[])Enum.GetValues(typeof(NotificationLocation)), (i) => i.ToString(), + (i) => { - foreach (var item in (NotificationLocation[])Enum.GetValues(typeof(NotificationLocation))) - { - bool isSelected = item == infoNotifLocation; - if (ImGui.Selectable(item.ToString(), isSelected)) - { - _configService.Current.InfoNotification = item; - _configService.Save(); - } - } - ImGui.EndCombo(); - } + _configService.Current.InfoNotification = i; + _configService.Save(); + }, _configService.Current.InfoNotification); UiShared.DrawHelpText("The location where \"Info\" notifications will display." - + Environment.NewLine + "'Nowhere' will not show any Info notifications" - + Environment.NewLine + "'Chat' will print Info notifications in chat" - + Environment.NewLine + "'Toast' will show Warning toast notifications in the bottom right corner" - + Environment.NewLine + "'Both' will show chat as well as the toast notification"); + + Environment.NewLine + "'Nowhere' will not show any Info notifications" + + Environment.NewLine + "'Chat' will print Info notifications in chat" + + Environment.NewLine + "'Toast' will show Warning toast notifications in the bottom right corner" + + Environment.NewLine + "'Both' will show chat as well as the toast notification"); - if (ImGui.BeginCombo("Warning Notification Display", warnNotifLocation.ToString())) + _uiShared.DrawCombo("Warning Notification Display##settingsUi", (NotificationLocation[])Enum.GetValues(typeof(NotificationLocation)), (i) => i.ToString(), + (i) => { - foreach (var item in (NotificationLocation[])Enum.GetValues(typeof(NotificationLocation))) - { - bool isSelected = item == warnNotifLocation; - if (ImGui.Selectable(item.ToString(), isSelected)) - { - _configService.Current.WarningNotification = item; - _configService.Save(); - } - } - ImGui.EndCombo(); - } - + _configService.Current.WarningNotification = i; + _configService.Save(); + }, _configService.Current.WarningNotification); UiShared.DrawHelpText("The location where \"Warning\" notifications will display." + Environment.NewLine + "'Nowhere' will not show any Warning notifications" + Environment.NewLine + "'Chat' will print Warning notifications in chat" + Environment.NewLine + "'Toast' will show Warning toast notifications in the bottom right corner" + Environment.NewLine + "'Both' will show chat as well as the toast notification"); - if (ImGui.BeginCombo("Error Notification Display", errorNotifLocation.ToString())) + _uiShared.DrawCombo("Error Notification Display##settingsUi", (NotificationLocation[])Enum.GetValues(typeof(NotificationLocation)), (i) => i.ToString(), + (i) => { - foreach (var item in (NotificationLocation[])Enum.GetValues(typeof(NotificationLocation))) - { - bool isSelected = item == errorNotifLocation; - if (ImGui.Selectable(item.ToString(), isSelected)) - { - _configService.Current.ErrorNotification = item; - _configService.Save(); - } - } - ImGui.EndCombo(); - } + _configService.Current.ErrorNotification = i; + _configService.Save(); + }, _configService.Current.ErrorNotification); UiShared.DrawHelpText("The location where \"Error\" notifications will display." + Environment.NewLine + "'Nowhere' will not show any Error notifications" + Environment.NewLine + "'Chat' will print Error notifications in chat" diff --git a/MareSynchronos/UI/UIShared.cs b/MareSynchronos/UI/UIShared.cs index 3bd6bbc..ccc5a99 100644 --- a/MareSynchronos/UI/UIShared.cs +++ b/MareSynchronos/UI/UIShared.cs @@ -40,6 +40,7 @@ public partial class UiShared : MediatorSubscriberBase public string PlayerName => _dalamudUtil.PlayerName; public uint WorldId => _dalamudUtil.WorldId; public Dictionary WorldData => _dalamudUtil.WorldData.Value; + private Dictionary _selectedComboItems = new(StringComparer.Ordinal); public bool HasValidPenumbraModPath => !(_ipcManager.PenumbraModDirectory ?? string.Empty).IsNullOrEmpty() && Directory.Exists(_ipcManager.PenumbraModDirectory); public bool EditTrackerPosition { get; set; } public ImFontPtr UidFont { get; private set; } @@ -100,6 +101,45 @@ public partial class UiShared : MediatorSubscriberBase return buttonSize; } + + public T? DrawCombo(string comboName, IEnumerable comboItems, Func toName, + Action? onSelected = null, T? initialSelectedItem = default) + { + if (!comboItems.Any()) return default; + + if (!_selectedComboItems.TryGetValue(comboName, out var selectedItem) && selectedItem == null) + { + if (!EqualityComparer.Default.Equals(initialSelectedItem, default)) + { + selectedItem = initialSelectedItem; + _selectedComboItems[comboName] = selectedItem!; + onSelected?.Invoke(initialSelectedItem); + } + else + { + selectedItem = comboItems.First(); + _selectedComboItems[comboName] = selectedItem!; + } + } + + if (ImGui.BeginCombo(comboName, toName((T)selectedItem!))) + { + foreach (var item in comboItems) + { + bool isSelected = EqualityComparer.Default.Equals(item, (T)selectedItem); + if (ImGui.Selectable(toName(item), isSelected)) + { + _selectedComboItems[comboName] = item!; + onSelected?.Invoke(item!); + } + } + + ImGui.EndCombo(); + } + + return (T)_selectedComboItems[comboName]; + } + private void BuildFont() { var fontFile = Path.Combine(_pluginInterface.DalamudAssetDirectory.FullName, "UIRes", "NotoSansCJKjp-Medium.otf");