Skip to content

Commit

Permalink
Added ability to automatically set editor preview character to curren…
Browse files Browse the repository at this point in the history
…t character on login

Also refactored some things
  • Loading branch information
RisaDev committed Apr 12, 2024
1 parent f88f3db commit 07f8933
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 50 deletions.
14 changes: 4 additions & 10 deletions CustomizePlus/Configuration/Data/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using CustomizePlus.Configuration.Services;
using CustomizePlus.Game.Services;
using CustomizePlus.UI.Windows;
using Dalamud.Plugin.Services;

namespace CustomizePlus.Configuration.Data;

Expand Down Expand Up @@ -73,7 +74,10 @@ public class EditorConfigurationEntries
public string? PreviewCharacterName { get; set; } = null;

public int EditorValuesPrecision { get; set; } = 3;

public BoneAttribute EditorMode { get; set; } = BoneAttribute.Position;

public bool SetPreviewToCurrentCharacterOnLogin { get; set; } = false;
}

public EditorConfigurationEntries EditorConfiguration { get; set; } = new();
Expand All @@ -100,25 +104,15 @@ public class ProfileApplicationSettingsEntries
[JsonIgnore]
private readonly SaveService _saveService;

[JsonIgnore]
private readonly Logger _logger;

[JsonIgnore]
private readonly ChatService _chatService;

[JsonIgnore]
private readonly MessageService _messageService;

public PluginConfiguration(
SaveService saveService,
Logger logger,
ChatService chatService,
MessageService messageService,
ConfigurationMigrator migrator)
{
_saveService = saveService;
_logger = logger;
_chatService = chatService;
_messageService = messageService;

Load(migrator);
Expand Down
3 changes: 3 additions & 0 deletions CustomizePlus/Game/Services/GameObjectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public bool IsActorHasScalableRoot(Actor actor)
/// <returns></returns>
public IEnumerable<(ActorIdentifier, Actor)> FindActorsByName(string name)
{
_objectManager.Update();

foreach (var kvPair in _objectManager.Identifiers)
{
var identifier = kvPair.Key;
Expand All @@ -81,6 +83,7 @@ public bool IsActorHasScalableRoot(Actor actor)

public Actor GetLocalPlayerActor()
{
_objectManager.Update();
return _objectManager.Player;
}

Expand Down
86 changes: 70 additions & 16 deletions CustomizePlus/Templates/TemplateEditorManager.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
using CustomizePlus.Core.Data;
using CustomizePlus.Configuration.Data;
using CustomizePlus.Core.Data;
using CustomizePlus.Game.Events;
using CustomizePlus.Game.Services;
using CustomizePlus.Profiles;
using CustomizePlus.Profiles.Data;
using CustomizePlus.Profiles.Enums;
using CustomizePlus.Templates.Data;
using CustomizePlus.Templates.Events;
using Dalamud.Plugin.Services;
using FFXIVClientStructs.FFXIV.Client.Graphics.Scene;
using OtterGui.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

namespace CustomizePlus.Templates;

public class TemplateEditorManager
public class TemplateEditorManager : IDisposable
{
private readonly TemplateChanged _event;
private readonly Logger _logger;
private readonly GameObjectService _gameObjectService;
private readonly TemplateManager _templateManager;
private readonly IClientState _clientState;
private readonly PluginConfiguration _configuration;

/// <summary>
/// Reference to the original template which is currently being edited, should not be edited!
Expand Down Expand Up @@ -54,31 +59,59 @@ public class TemplateEditorManager
/// </summary>
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 TemplateEditorManager(
TemplateChanged @event,
Logger logger,
TemplateManager templateManager,
GameObjectService gameObjectService)
GameObjectService gameObjectService,
IClientState clientState,
PluginConfiguration configuration)
{
_event = @event;
_logger = logger;
_templateManager = templateManager;
_gameObjectService = gameObjectService;
_clientState = clientState;
_configuration = configuration;

_clientState.Login += OnLogin;

EditorProfile = new Profile()
{
Templates = new List<Template>(),
Enabled = false,
Name = "Template editor profile",
ProfileType = ProfileType.Editor,
CharacterName = configuration.EditorConfiguration.PreviewCharacterName!
};
}

EditorProfile = new Profile() { Templates = new List<Template>(), Enabled = false, Name = "Template editor profile", ProfileType = ProfileType.Editor };
public void Dispose()
{
_clientState.Login -= OnLogin;
}

/// <summary>
/// Turn on editing of a specific template. If character name not set will default to local player.
/// </summary>
internal bool EnableEditor(Template template, string? characterName = null)
internal bool EnableEditor(Template template)
{
if (IsEditorActive || IsEditorPaused)
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;
_currentlyEditedTemplateOriginal = template;
Expand All @@ -90,18 +123,18 @@ internal bool EnableEditor(Template template, string? characterName = null)
Name = "Template editor temporary template"
};

if (characterName != null)
EditorProfile.CharacterName = characterName;
else //safeguard
EditorProfile.CharacterName = _gameObjectService.GetCurrentPlayerName();
if (CharacterName == null) //safeguard
ChangeEditorCharacterInternal(_gameObjectService.GetCurrentPlayerName()); //will also set EditorProfile.CharacterName
else
EditorProfile.CharacterName = CharacterName;

EditorProfile.Templates.Clear(); //safeguard
EditorProfile.Templates.Add(CurrentlyEditedTemplate);
EditorProfile.Enabled = true;
HasChanges = false;
IsEditorActive = true;

_event.Invoke(TemplateChanged.Type.EditorEnabled, template, characterName);
_event.Invoke(TemplateChanged.Type.EditorEnabled, template, CharacterName);

return true;
}
Expand All @@ -116,17 +149,14 @@ internal bool DisableEditor()

_logger.Debug($"Disabling editor profile");

string characterName = EditorProfile.CharacterName;

CurrentlyEditedTemplateId = Guid.Empty;
CurrentlyEditedTemplate = null;
EditorProfile.Enabled = false;
EditorProfile.CharacterName = "";
EditorProfile.Templates.Clear();
IsEditorActive = false;
HasChanges = false;

_event.Invoke(TemplateChanged.Type.EditorDisabled, null, characterName);
_event.Invoke(TemplateChanged.Type.EditorDisabled, null, CharacterName);

return true;
}
Expand All @@ -145,12 +175,21 @@ public void SaveChanges(bool asCopy = false)

public bool ChangeEditorCharacter(string characterName)
{
if (!IsEditorActive || EditorProfile.CharacterName == characterName || IsEditorPaused)
if (!IsEditorActive || CharacterName == characterName || IsEditorPaused)
return false;

return ChangeEditorCharacterInternal(characterName);
}

private bool ChangeEditorCharacterInternal(string characterName)
{
_logger.Debug($"Changing character name for editor profile from {EditorProfile.CharacterName} to {characterName}");

EditorProfile.CharacterName = characterName;

_configuration.EditorConfiguration.PreviewCharacterName = CharacterName;
_configuration.Save();

_event.Invoke(TemplateChanged.Type.EditorCharacterChanged, CurrentlyEditedTemplate, (characterName, EditorProfile));

return true;
Expand Down Expand Up @@ -265,6 +304,21 @@ public bool ModifyBoneTransform(string boneName, BoneTransform transform)
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)
{
switch (attribute)
Expand Down
13 changes: 13 additions & 0 deletions CustomizePlus/UI/Windows/MainWindow/Tabs/SettingsTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ private void DrawInterface()

DrawHideWindowInCutscene();
DrawFoldersDefaultOpen();
DrawSetPreviewToCurrentCharacterOnLogin();

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,
Expand Down Expand Up @@ -225,6 +226,18 @@ private void DrawFoldersDefaultOpen()
}
}

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

#region Advanced Settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ public class BoneEditorPanel
public bool HasChanges => _editorManager.HasChanges;
public bool IsEditorActive => _editorManager.IsEditorActive;
public bool IsEditorPaused => _editorManager.IsEditorPaused;

/// <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 bool IsCharacterFound => _editorManager.IsCharacterFound;

public BoneEditorPanel(
TemplateFileSystemSelector templateFileSystemSelector,
Expand All @@ -64,12 +59,11 @@ public BoneEditorPanel(
_isMirrorModeEnabled = configuration.EditorConfiguration.BoneMirroringEnabled;
_precision = configuration.EditorConfiguration.EditorValuesPrecision;
_editingAttribute = configuration.EditorConfiguration.EditorMode;
CharacterName = configuration.EditorConfiguration.PreviewCharacterName!;
}

public bool EnableEditor(Template template)
{
if (_editorManager.EnableEditor(template, CharacterName))
if (_editorManager.EnableEditor(template))
{
_editorManager.SetLimitLookupToOwned(_configuration.EditorConfiguration.LimitLookupToOwnedObjects);

Expand All @@ -94,18 +88,8 @@ public bool DisableEditor()

public void Draw()
{
IsCharacterFound = _gameObjectService.FindActorsByName(CharacterName).Count() > 0;
_isUnlocked = IsCharacterFound && IsEditorActive && !IsEditorPaused;

if (string.IsNullOrWhiteSpace(CharacterName))
{
CharacterName = _gameObjectService.GetCurrentPlayerName();
_editorManager.ChangeEditorCharacter(CharacterName);

_configuration.EditorConfiguration.PreviewCharacterName = CharacterName;
_configuration.Save();
}

DrawEditorConfirmationPopup();

ImGui.Separator();
Expand All @@ -121,7 +105,7 @@ public void Draw()
ImGuiUtil.DrawFrameColumn("Show editor preview on");
ImGui.TableNextColumn();
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);

using (var disabled = ImRaii.Disabled(!IsEditorActive || IsEditorPaused))
Expand All @@ -135,13 +119,10 @@ public void Draw()

if (ImGui.IsItemDeactivatedAfterEdit())
{
if (_newCharacterName == "")
if (string.IsNullOrWhiteSpace(_newCharacterName))
_newCharacterName = _gameObjectService.GetCurrentPlayerName();
CharacterName = _newCharacterName!;
_editorManager.ChangeEditorCharacter(CharacterName);

_configuration.EditorConfiguration.PreviewCharacterName = CharacterName;
_configuration.Save();
_editorManager.ChangeEditorCharacter(_newCharacterName);

_newCharacterName = null;
}
Expand Down

0 comments on commit 07f8933

Please sign in to comment.