Added ability to automatically set editor preview character to current character on login

Also refactored some things
This commit is contained in:
RisaDev
2024-04-13 01:55:27 +03:00
parent f88f3db2b3
commit 07f89334c1
5 changed files with 95 additions and 50 deletions

View File

@@ -13,6 +13,7 @@ using CustomizePlus.Core.Data;
using CustomizePlus.Configuration.Services; using CustomizePlus.Configuration.Services;
using CustomizePlus.Game.Services; using CustomizePlus.Game.Services;
using CustomizePlus.UI.Windows; using CustomizePlus.UI.Windows;
using Dalamud.Plugin.Services;
namespace CustomizePlus.Configuration.Data; namespace CustomizePlus.Configuration.Data;
@@ -73,7 +74,10 @@ public class PluginConfiguration : IPluginConfiguration, ISavable
public string? PreviewCharacterName { get; set; } = null; public string? PreviewCharacterName { get; set; } = null;
public int EditorValuesPrecision { get; set; } = 3; public int EditorValuesPrecision { get; set; } = 3;
public BoneAttribute EditorMode { get; set; } = BoneAttribute.Position; public BoneAttribute EditorMode { get; set; } = BoneAttribute.Position;
public bool SetPreviewToCurrentCharacterOnLogin { get; set; } = false;
} }
public EditorConfigurationEntries EditorConfiguration { get; set; } = new(); public EditorConfigurationEntries EditorConfiguration { get; set; } = new();
@@ -100,25 +104,15 @@ public class PluginConfiguration : IPluginConfiguration, ISavable
[JsonIgnore] [JsonIgnore]
private readonly SaveService _saveService; private readonly SaveService _saveService;
[JsonIgnore]
private readonly Logger _logger;
[JsonIgnore]
private readonly ChatService _chatService;
[JsonIgnore] [JsonIgnore]
private readonly MessageService _messageService; private readonly MessageService _messageService;
public PluginConfiguration( public PluginConfiguration(
SaveService saveService, SaveService saveService,
Logger logger,
ChatService chatService,
MessageService messageService, MessageService messageService,
ConfigurationMigrator migrator) ConfigurationMigrator migrator)
{ {
_saveService = saveService; _saveService = saveService;
_logger = logger;
_chatService = chatService;
_messageService = messageService; _messageService = messageService;
Load(migrator); Load(migrator);

View File

@@ -59,6 +59,8 @@ public class GameObjectService
/// <returns></returns> /// <returns></returns>
public IEnumerable<(ActorIdentifier, Actor)> FindActorsByName(string name) public IEnumerable<(ActorIdentifier, Actor)> FindActorsByName(string name)
{ {
_objectManager.Update();
foreach (var kvPair in _objectManager.Identifiers) foreach (var kvPair in _objectManager.Identifiers)
{ {
var identifier = kvPair.Key; var identifier = kvPair.Key;
@@ -81,6 +83,7 @@ public class GameObjectService
public Actor GetLocalPlayerActor() public Actor GetLocalPlayerActor()
{ {
_objectManager.Update();
return _objectManager.Player; return _objectManager.Player;
} }

View File

@@ -1,4 +1,5 @@
using CustomizePlus.Core.Data; using CustomizePlus.Configuration.Data;
using CustomizePlus.Core.Data;
using CustomizePlus.Game.Events; using CustomizePlus.Game.Events;
using CustomizePlus.Game.Services; using CustomizePlus.Game.Services;
using CustomizePlus.Profiles; using CustomizePlus.Profiles;
@@ -6,20 +7,24 @@ using CustomizePlus.Profiles.Data;
using CustomizePlus.Profiles.Enums; using CustomizePlus.Profiles.Enums;
using CustomizePlus.Templates.Data; using CustomizePlus.Templates.Data;
using CustomizePlus.Templates.Events; using CustomizePlus.Templates.Events;
using Dalamud.Plugin.Services;
using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene;
using OtterGui.Log; using OtterGui.Log;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Numerics; using System.Numerics;
namespace CustomizePlus.Templates; namespace CustomizePlus.Templates;
public class TemplateEditorManager public class TemplateEditorManager : IDisposable
{ {
private readonly TemplateChanged _event; private readonly TemplateChanged _event;
private readonly Logger _logger; private readonly Logger _logger;
private readonly GameObjectService _gameObjectService; private readonly GameObjectService _gameObjectService;
private readonly TemplateManager _templateManager; private readonly TemplateManager _templateManager;
private readonly IClientState _clientState;
private readonly PluginConfiguration _configuration;
/// <summary> /// <summary>
/// Reference to the original template which is currently being edited, should not be edited! /// Reference to the original template which is currently being edited, should not be edited!
@@ -54,31 +59,59 @@ public class TemplateEditorManager
/// </summary> /// </summary>
public bool HasChanges { get; private set; } public bool HasChanges { get; private set; }
/// <summary>
/// Name of the preview character for the editor
/// </summary>
public string CharacterName => EditorProfile.CharacterName;
/// <summary>
/// Checks if preview character exists at the time of call
/// </summary>
public bool IsCharacterFound => _gameObjectService.FindActorsByName(CharacterName).Count() > 0;
public bool IsKeepOnlyEditorProfileActive { get; set; } //todo public bool IsKeepOnlyEditorProfileActive { get; set; } //todo
public TemplateEditorManager( public TemplateEditorManager(
TemplateChanged @event, TemplateChanged @event,
Logger logger, Logger logger,
TemplateManager templateManager, TemplateManager templateManager,
GameObjectService gameObjectService) GameObjectService gameObjectService,
IClientState clientState,
PluginConfiguration configuration)
{ {
_event = @event; _event = @event;
_logger = logger; _logger = logger;
_templateManager = templateManager; _templateManager = templateManager;
_gameObjectService = gameObjectService; _gameObjectService = gameObjectService;
_clientState = clientState;
_configuration = configuration;
EditorProfile = new Profile() { Templates = new List<Template>(), Enabled = false, Name = "Template editor profile", ProfileType = ProfileType.Editor }; _clientState.Login += OnLogin;
EditorProfile = new Profile()
{
Templates = new List<Template>(),
Enabled = false,
Name = "Template editor profile",
ProfileType = ProfileType.Editor,
CharacterName = configuration.EditorConfiguration.PreviewCharacterName!
};
}
public void Dispose()
{
_clientState.Login -= OnLogin;
} }
/// <summary> /// <summary>
/// Turn on editing of a specific template. If character name not set will default to local player. /// Turn on editing of a specific template. If character name not set will default to local player.
/// </summary> /// </summary>
internal bool EnableEditor(Template template, string? characterName = null) internal bool EnableEditor(Template template)
{ {
if (IsEditorActive || IsEditorPaused) if (IsEditorActive || IsEditorPaused)
return false; return false;
_logger.Debug($"Enabling editor profile for {template.Name} via character {characterName}"); _logger.Debug($"Enabling editor profile for {template.Name} via character {CharacterName}");
CurrentlyEditedTemplateId = template.UniqueId; CurrentlyEditedTemplateId = template.UniqueId;
_currentlyEditedTemplateOriginal = template; _currentlyEditedTemplateOriginal = template;
@@ -90,10 +123,10 @@ public class TemplateEditorManager
Name = "Template editor temporary template" Name = "Template editor temporary template"
}; };
if (characterName != null) if (CharacterName == null) //safeguard
EditorProfile.CharacterName = characterName; ChangeEditorCharacterInternal(_gameObjectService.GetCurrentPlayerName()); //will also set EditorProfile.CharacterName
else //safeguard else
EditorProfile.CharacterName = _gameObjectService.GetCurrentPlayerName(); EditorProfile.CharacterName = CharacterName;
EditorProfile.Templates.Clear(); //safeguard EditorProfile.Templates.Clear(); //safeguard
EditorProfile.Templates.Add(CurrentlyEditedTemplate); EditorProfile.Templates.Add(CurrentlyEditedTemplate);
@@ -101,7 +134,7 @@ public class TemplateEditorManager
HasChanges = false; HasChanges = false;
IsEditorActive = true; IsEditorActive = true;
_event.Invoke(TemplateChanged.Type.EditorEnabled, template, characterName); _event.Invoke(TemplateChanged.Type.EditorEnabled, template, CharacterName);
return true; return true;
} }
@@ -116,17 +149,14 @@ public class TemplateEditorManager
_logger.Debug($"Disabling editor profile"); _logger.Debug($"Disabling editor profile");
string characterName = EditorProfile.CharacterName;
CurrentlyEditedTemplateId = Guid.Empty; CurrentlyEditedTemplateId = Guid.Empty;
CurrentlyEditedTemplate = null; CurrentlyEditedTemplate = null;
EditorProfile.Enabled = false; EditorProfile.Enabled = false;
EditorProfile.CharacterName = "";
EditorProfile.Templates.Clear(); EditorProfile.Templates.Clear();
IsEditorActive = false; IsEditorActive = false;
HasChanges = false; HasChanges = false;
_event.Invoke(TemplateChanged.Type.EditorDisabled, null, characterName); _event.Invoke(TemplateChanged.Type.EditorDisabled, null, CharacterName);
return true; return true;
} }
@@ -145,12 +175,21 @@ public class TemplateEditorManager
public bool ChangeEditorCharacter(string characterName) public bool ChangeEditorCharacter(string characterName)
{ {
if (!IsEditorActive || EditorProfile.CharacterName == characterName || IsEditorPaused) if (!IsEditorActive || CharacterName == characterName || IsEditorPaused)
return false; return false;
return ChangeEditorCharacterInternal(characterName);
}
private bool ChangeEditorCharacterInternal(string characterName)
{
_logger.Debug($"Changing character name for editor profile from {EditorProfile.CharacterName} to {characterName}"); _logger.Debug($"Changing character name for editor profile from {EditorProfile.CharacterName} to {characterName}");
EditorProfile.CharacterName = characterName; EditorProfile.CharacterName = characterName;
_configuration.EditorConfiguration.PreviewCharacterName = CharacterName;
_configuration.Save();
_event.Invoke(TemplateChanged.Type.EditorCharacterChanged, CurrentlyEditedTemplate, (characterName, EditorProfile)); _event.Invoke(TemplateChanged.Type.EditorCharacterChanged, CurrentlyEditedTemplate, (characterName, EditorProfile));
return true; return true;
@@ -265,6 +304,21 @@ public class TemplateEditorManager
return true; return true;
} }
private void OnLogin()
{
if (_configuration.EditorConfiguration.SetPreviewToCurrentCharacterOnLogin ||
string.IsNullOrWhiteSpace(_configuration.EditorConfiguration.PreviewCharacterName))
{
var localPlayerName = _gameObjectService.GetCurrentPlayerName();
if (_configuration.EditorConfiguration.PreviewCharacterName != localPlayerName)
{
_logger.Debug("Resetting editor character because automatic condition triggered in OnLogin");
ChangeEditorCharacterInternal(localPlayerName);
}
}
}
private Vector3 GetResetValueForAttribute(BoneAttribute attribute) private Vector3 GetResetValueForAttribute(BoneAttribute attribute)
{ {
switch (attribute) switch (attribute)

View File

@@ -194,6 +194,7 @@ public class SettingsTab
DrawHideWindowInCutscene(); DrawHideWindowInCutscene();
DrawFoldersDefaultOpen(); DrawFoldersDefaultOpen();
DrawSetPreviewToCurrentCharacterOnLogin();
if (Widget.DoubleModifierSelector("Template Deletion Modifier", if (Widget.DoubleModifierSelector("Template Deletion Modifier",
"A modifier you need to hold while clicking the Delete Template button for it to take effect.", 100 * ImGuiHelpers.GlobalScale, "A modifier you need to hold while clicking the Delete Template button for it to take effect.", 100 * ImGuiHelpers.GlobalScale,
@@ -225,6 +226,18 @@ public class SettingsTab
} }
} }
private void DrawSetPreviewToCurrentCharacterOnLogin()
{
var isChecked = _configuration.EditorConfiguration.SetPreviewToCurrentCharacterOnLogin;
if (CtrlHelper.CheckboxWithTextAndHelp("##setpreviewcharaonlogin", "Automatically Set Current Character as Editor Preview Character",
"Controls whether editor character will be automatically set to the current character during login.", ref isChecked))
{
_configuration.EditorConfiguration.SetPreviewToCurrentCharacterOnLogin = isChecked;
_configuration.Save();
}
}
#endregion #endregion
#region Advanced Settings #region Advanced Settings

View File

@@ -42,12 +42,7 @@ public class BoneEditorPanel
public bool HasChanges => _editorManager.HasChanges; public bool HasChanges => _editorManager.HasChanges;
public bool IsEditorActive => _editorManager.IsEditorActive; public bool IsEditorActive => _editorManager.IsEditorActive;
public bool IsEditorPaused => _editorManager.IsEditorPaused; public bool IsEditorPaused => _editorManager.IsEditorPaused;
public bool IsCharacterFound => _editorManager.IsCharacterFound;
/// <summary>
/// Was character with name from CharacterName found in the object table or not
/// </summary>
public bool IsCharacterFound { get; private set; }
public string CharacterName { get; private set; }
public BoneEditorPanel( public BoneEditorPanel(
TemplateFileSystemSelector templateFileSystemSelector, TemplateFileSystemSelector templateFileSystemSelector,
@@ -64,12 +59,11 @@ public class BoneEditorPanel
_isMirrorModeEnabled = configuration.EditorConfiguration.BoneMirroringEnabled; _isMirrorModeEnabled = configuration.EditorConfiguration.BoneMirroringEnabled;
_precision = configuration.EditorConfiguration.EditorValuesPrecision; _precision = configuration.EditorConfiguration.EditorValuesPrecision;
_editingAttribute = configuration.EditorConfiguration.EditorMode; _editingAttribute = configuration.EditorConfiguration.EditorMode;
CharacterName = configuration.EditorConfiguration.PreviewCharacterName!;
} }
public bool EnableEditor(Template template) public bool EnableEditor(Template template)
{ {
if (_editorManager.EnableEditor(template, CharacterName)) if (_editorManager.EnableEditor(template))
{ {
_editorManager.SetLimitLookupToOwned(_configuration.EditorConfiguration.LimitLookupToOwnedObjects); _editorManager.SetLimitLookupToOwned(_configuration.EditorConfiguration.LimitLookupToOwnedObjects);
@@ -94,18 +88,8 @@ public class BoneEditorPanel
public void Draw() public void Draw()
{ {
IsCharacterFound = _gameObjectService.FindActorsByName(CharacterName).Count() > 0;
_isUnlocked = IsCharacterFound && IsEditorActive && !IsEditorPaused; _isUnlocked = IsCharacterFound && IsEditorActive && !IsEditorPaused;
if (string.IsNullOrWhiteSpace(CharacterName))
{
CharacterName = _gameObjectService.GetCurrentPlayerName();
_editorManager.ChangeEditorCharacter(CharacterName);
_configuration.EditorConfiguration.PreviewCharacterName = CharacterName;
_configuration.Save();
}
DrawEditorConfirmationPopup(); DrawEditorConfirmationPopup();
ImGui.Separator(); ImGui.Separator();
@@ -121,7 +105,7 @@ public class BoneEditorPanel
ImGuiUtil.DrawFrameColumn("Show editor preview on"); ImGuiUtil.DrawFrameColumn("Show editor preview on");
ImGui.TableNextColumn(); ImGui.TableNextColumn();
var width = new Vector2(ImGui.GetContentRegionAvail().X - ImGui.CalcTextSize("Limit to my creatures").X - 68, 0); var width = new Vector2(ImGui.GetContentRegionAvail().X - ImGui.CalcTextSize("Limit to my creatures").X - 68, 0);
var name = _newCharacterName ?? CharacterName; var name = _newCharacterName ?? _editorManager.CharacterName;
ImGui.SetNextItemWidth(width.X); ImGui.SetNextItemWidth(width.X);
using (var disabled = ImRaii.Disabled(!IsEditorActive || IsEditorPaused)) using (var disabled = ImRaii.Disabled(!IsEditorActive || IsEditorPaused))
@@ -135,13 +119,10 @@ public class BoneEditorPanel
if (ImGui.IsItemDeactivatedAfterEdit()) if (ImGui.IsItemDeactivatedAfterEdit())
{ {
if (_newCharacterName == "") if (string.IsNullOrWhiteSpace(_newCharacterName))
_newCharacterName = _gameObjectService.GetCurrentPlayerName(); _newCharacterName = _gameObjectService.GetCurrentPlayerName();
CharacterName = _newCharacterName!;
_editorManager.ChangeEditorCharacter(CharacterName);
_configuration.EditorConfiguration.PreviewCharacterName = CharacterName; _editorManager.ChangeEditorCharacter(_newCharacterName);
_configuration.Save();
_newCharacterName = null; _newCharacterName = null;
} }