Added button to jump to template editing from profile tab, made TemplateManager IDisposable

This commit is contained in:
RisaDev
2024-06-16 02:58:17 +03:00
parent faa4af97ad
commit ef6c7014e4
5 changed files with 178 additions and 20 deletions

View File

@@ -15,10 +15,12 @@ using CustomizePlus.Configuration.Data;
using CustomizePlus.Core.Helpers;
using CustomizePlus.Templates.Data;
using OtterGui.Log;
using CustomizePlus.Templates.Events;
using ECommons.Schedulers;
namespace CustomizePlus.UI.Windows.MainWindow.Tabs.Templates;
public class TemplatePanel
public class TemplatePanel : IDisposable
{
private readonly TemplateFileSystemSelector _selector;
private readonly TemplateManager _manager;
@@ -28,9 +30,16 @@ public class TemplatePanel
private readonly PopupSystem _popupSystem;
private readonly Logger _logger;
private readonly TemplateEditorEvent _editorEvent;
private string? _newName;
private Template? _changedTemplate;
/// <summary>
/// Set to true if we received OnEditorEvent EditorEnableRequested and waiting for selector value to be changed.
/// </summary>
private bool _isEditorEnablePending = false;
private string SelectionName
=> _selector.Selected == null ? "No Selection" : _selector.IncognitoMode ? _selector.Selected.Incognito : _selector.Selected.Name.Text;
@@ -41,7 +50,8 @@ public class TemplatePanel
PluginConfiguration configuration,
MessageService messageService,
PopupSystem popupSystem,
Logger logger)
Logger logger,
TemplateEditorEvent editorEvent)
{
_selector = selector;
_manager = manager;
@@ -51,6 +61,11 @@ public class TemplatePanel
_popupSystem = popupSystem;
_logger = logger;
_editorEvent = editorEvent;
_editorEvent.Subscribe(OnEditorEvent, TemplateEditorEvent.Priority.TemplatePanel);
_selector.SelectionChanged += SelectorSelectionChanged;
}
public void Draw()
@@ -67,6 +82,11 @@ public class TemplatePanel
}
}
public void Dispose()
{
_editorEvent.Unsubscribe(OnEditorEvent);
}
private HeaderDrawer.Button LockButton()
=> _selector.Selected == null
? HeaderDrawer.Button.Invisible
@@ -167,17 +187,23 @@ public class TemplatePanel
private void DrawEditorToggle()
{
(bool isEditorAllowed, bool isEditorActive) = CanToggleEditor();
if (ImGuiUtil.DrawDisabledButton($"{(_boneEditor.IsEditorActive ? "Finish" : "Start")} bone editing", Vector2.Zero,
"Toggle the bone editor for this template",
(_selector.Selected?.IsWriteProtected ?? true) || !_configuration.PluginEnabled))
"Toggle the bone editor for this template", !isEditorAllowed))
{
if (!_boneEditor.IsEditorActive)
if (!isEditorActive)
_boneEditor.EnableEditor(_selector.Selected!);
else
_boneEditor.DisableEditor();
}
}
private (bool isEditorAllowed, bool isEditorActive) CanToggleEditor()
{
return ((!_selector.Selected?.IsWriteProtected ?? false) || _configuration.PluginEnabled, _boneEditor.IsEditorActive);
}
private void DrawBasicSettings()
{
using (var style = ImRaii.PushStyle(ImGuiStyleVar.ButtonTextAlign, new Vector2(0, 0.5f)))
@@ -214,11 +240,6 @@ public class TemplatePanel
}
}
/*private void SetFromClipboard()
{
}*/
private void ExportToClipboard()
{
try
@@ -232,4 +253,38 @@ public class TemplatePanel
_popupSystem.ShowPopup(PopupSystem.Messages.ActionError);
}
}
private void SelectorSelectionChanged(Template? oldSelection, Template? newSelection, in TemplateFileSystemSelector.TemplateState state)
{
if (!_isEditorEnablePending)
return;
_isEditorEnablePending = false;
_boneEditor.EnableEditor(_selector.Selected!);
}
private void OnEditorEvent(TemplateEditorEvent.Type type, Template? template)
{
if (type != TemplateEditorEvent.Type.EditorEnableRequestedStage2)
return;
if(template == null)
return;
(bool isEditorAllowed, bool isEditorActive) = CanToggleEditor();
if (!isEditorAllowed || isEditorActive)
return;
if(_selector.Selected != template)
{
_selector.SelectByValue(template);
_isEditorEnablePending = true;
}
else
_boneEditor.EnableEditor(_selector.Selected!);
}
}