Skip to content

Commit

Permalink
Added button to jump to template editing from profile tab, made Templ…
Browse files Browse the repository at this point in the history
…ateManager IDisposable
  • Loading branch information
RisaDev committed Jun 15, 2024
1 parent faa4af9 commit ef6c701
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 20 deletions.
33 changes: 33 additions & 0 deletions CustomizePlus/Templates/Events/TemplateEditorEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using CustomizePlus.Templates.Data;
using OtterGui.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomizePlus.Templates.Events;

/// <summary>
/// Triggered when something related to template editor happens
/// </summary>
public class TemplateEditorEvent() : EventWrapper<TemplateEditorEvent.Type, Template?, TemplateEditorEvent.Priority>(nameof(TemplateEditorEvent))
{
public enum Type
{
/// <summary>
/// Called when something requests editor to be enabled.
/// </summary>
EditorEnableRequested,
/// <summary>
/// Called when something requests editor to be enabled. Stage 2 - logic after tab has been switched.
/// </summary>
EditorEnableRequestedStage2
}

public enum Priority
{
MainWindow = -1,
TemplatePanel
}
}
7 changes: 6 additions & 1 deletion CustomizePlus/Templates/TemplateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace CustomizePlus.Templates;

public class TemplateManager
public class TemplateManager : IDisposable
{
private readonly SaveService _saveService;
private readonly Logger _logger;
Expand Down Expand Up @@ -41,6 +41,11 @@ public TemplateManager(
LoadTemplates();
}

public void Dispose()
{
_reloadEvent.Unsubscribe(OnReload);
}

public Template? GetTemplate(Guid templateId) => _templates.FirstOrDefault(d => d.UniqueId == templateId);

public void LoadTemplates()
Expand Down
51 changes: 48 additions & 3 deletions CustomizePlus/UI/Windows/MainWindow/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
using ECommons.ImGuiMethods;
using static System.Windows.Forms.AxHost;
using Dalamud.Interface.Colors;
using CustomizePlus.Templates.Events;
using CustomizePlus.Templates.Data;
using ECommons.Schedulers;

namespace CustomizePlus.UI.Windows.MainWindow;

Expand All @@ -34,6 +37,15 @@ public class MainWindow : Window, IDisposable
private readonly PluginConfiguration _configuration;
private readonly HookingService _hookingService;

private readonly TemplateEditorEvent _templateEditorEvent;

/// <summary>
/// Used to force the main window to switch to specific tab
/// </summary>
private string? _switchToTab = null;

private Action? _actionAfterTabSwitch = null;

public MainWindow(
DalamudPluginInterface pluginInterface,
SettingsTab settingsTab,
Expand All @@ -45,7 +57,8 @@ public MainWindow(
PluginStateBlock pluginStateBlock,
TemplateEditorManager templateEditorManager,
PluginConfiguration configuration,
HookingService hookingService
HookingService hookingService,
TemplateEditorEvent templateEditorEvent
) : base($"Customize+ v{Plugin.Version}###CPlusMainWindow")
{
_settingsTab = settingsTab;
Expand All @@ -61,6 +74,10 @@ HookingService hookingService
_configuration = configuration;
_hookingService = hookingService;

_templateEditorEvent = templateEditorEvent;

_templateEditorEvent.Subscribe(OnTemplateEditorEvent, TemplateEditorEvent.Priority.MainWindow);

pluginInterface.UiBuilder.DisableGposeUiHide = true;
SizeConstraints = new WindowSizeConstraints()
{
Expand All @@ -73,7 +90,7 @@ HookingService hookingService

public void Dispose()
{
//throw new NotImplementedException();
_templateEditorEvent.Unsubscribe(OnTemplateEditorEvent);
}

public override void Draw()
Expand All @@ -83,13 +100,21 @@ public override void Draw()
using (var disabled = ImRaii.Disabled(_hookingService.RenderHookFailed || _hookingService.MovementHookFailed))
{
LockWindowClosureIfNeeded();
ImGuiEx.EzTabBar("##tabs", [
ImGuiEx.EzTabBar("##tabs", null, _switchToTab, [
("Settings", _settingsTab.Draw, null, true),
("Templates", _templatesTab.Draw, null, true),
("Profiles", _profilesTab.Draw, null, true),
(_configuration.DebuggingModeEnabled ? "IPC Test" : null, _ipcTestTab.Draw, ImGuiColors.DalamudGrey, true),
(_configuration.DebuggingModeEnabled ? "State monitoring" : null, _stateMonitoringTab.Draw, ImGuiColors.DalamudGrey, true),
]);

_switchToTab = null;

if (_actionAfterTabSwitch != null)
{
_actionAfterTabSwitch();
_actionAfterTabSwitch = null;
}
}

_pluginStateBlock.Draw(yPos);
Expand All @@ -108,4 +133,24 @@ private void LockWindowClosureIfNeeded()
RespectCloseHotkey = true;
}
}

private void OnTemplateEditorEvent(TemplateEditorEvent.Type type, Template? template)
{
if (type != TemplateEditorEvent.Type.EditorEnableRequested)
return;

if (template == null)
return;

if (!template.IsWriteProtected && !_templateEditorManager.IsEditorActive)
{
new TickScheduler(() =>
{
_switchToTab = "Templates";

//To make sure the tab has switched, ugly but imgui is shit and I don't trust it.
_actionAfterTabSwitch = () => { _templateEditorEvent.Invoke(TemplateEditorEvent.Type.EditorEnableRequestedStage2, template); };
});
}
}
}
32 changes: 26 additions & 6 deletions CustomizePlus/UI/Windows/MainWindow/Tabs/Profiles/ProfilePanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@
using System.Linq;
using System.Numerics;
using CustomizePlus.Profiles;
using CustomizePlus.Game.Services;
using CustomizePlus.Configuration.Data;
using CustomizePlus.Profiles.Data;
using CustomizePlus.UI.Windows.Controls;
using CustomizePlus.Templates;
using CustomizePlus.Core.Helpers;
using System.Windows.Forms;
using CustomizePlus.Core.Data;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using CustomizePlus.Templates.Events;

namespace CustomizePlus.UI.Windows.MainWindow.Tabs.Profiles;

Expand All @@ -26,6 +23,7 @@ public class ProfilePanel
private readonly PluginConfiguration _configuration;
private readonly TemplateCombo _templateCombo;
private readonly TemplateEditorManager _templateEditorManager;
private readonly TemplateEditorEvent _templateEditorEvent;

private string? _newName;
private string? _newCharacterName;
Expand All @@ -43,13 +41,15 @@ public ProfilePanel(
ProfileManager manager,
PluginConfiguration configuration,
TemplateCombo templateCombo,
TemplateEditorManager templateEditorManager)
TemplateEditorManager templateEditorManager,
TemplateEditorEvent templateEditorEvent)
{
_selector = selector;
_manager = manager;
_configuration = configuration;
_templateCombo = templateCombo;
_templateEditorManager = templateEditorManager;
_templateEditorEvent = templateEditorEvent;
}

public void Draw()
Expand Down Expand Up @@ -247,7 +247,7 @@ private void DrawBasicSettings()

private void DrawTemplateArea()
{
using var table = ImRaii.Table("SetTable", 3, ImGuiTableFlags.RowBg | ImGuiTableFlags.ScrollX | ImGuiTableFlags.ScrollY);
using var table = ImRaii.Table("SetTable", 4, ImGuiTableFlags.RowBg | ImGuiTableFlags.ScrollX | ImGuiTableFlags.ScrollY);
if (!table)
return;

Expand All @@ -256,6 +256,8 @@ private void DrawTemplateArea()

ImGui.TableSetupColumn("Template", ImGuiTableColumnFlags.WidthFixed, 220 * ImGuiHelpers.GlobalScale);

ImGui.TableSetupColumn("##editbtn", ImGuiTableColumnFlags.WidthFixed, 120 * ImGuiHelpers.GlobalScale);

ImGui.TableHeadersRow();

//warn: .ToList() might be performance critical at some point
Expand All @@ -277,6 +279,24 @@ private void DrawTemplateArea()
ImGui.TableNextColumn();
_templateCombo.Draw(_selector.Selected!, template, idx);
DrawDragDrop(_selector.Selected!, idx);
ImGui.TableNextColumn();

var disabledCondition = _templateEditorManager.IsEditorActive || template.IsWriteProtected;
using (var disabled = ImRaii.Disabled(disabledCondition))
{
if (ImGui.Button("Open in editor"))
_templateEditorEvent.Invoke(TemplateEditorEvent.Type.EditorEnableRequested, template);
ImGuiUtil.HoverTooltip("Open this template in the template editor");
}

if(disabledCondition)
{
ImGui.SameLine();
ImGui.PushStyleColor(ImGuiCol.Text, Constants.Colors.Warning);
ImGuiUtil.PrintIcon(FontAwesomeIcon.ExclamationTriangle);
ImGui.PopStyleColor();
ImGuiUtil.HoverTooltip("Can not be edited because this template is either write protected or template editor is already enabled.");
}
}

ImGui.TableNextColumn();
Expand Down
75 changes: 65 additions & 10 deletions CustomizePlus/UI/Windows/MainWindow/Tabs/Templates/TemplatePanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
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;
Expand All @@ -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;

Expand All @@ -41,7 +50,8 @@ public TemplatePanel(
PluginConfiguration configuration,
MessageService messageService,
PopupSystem popupSystem,
Logger logger)
Logger logger,
TemplateEditorEvent editorEvent)
{
_selector = selector;
_manager = manager;
Expand All @@ -51,6 +61,11 @@ public TemplatePanel(
_popupSystem = popupSystem;
_logger = logger;

_editorEvent = editorEvent;

_editorEvent.Subscribe(OnEditorEvent, TemplateEditorEvent.Priority.TemplatePanel);

_selector.SelectionChanged += SelectorSelectionChanged;
}

public void Draw()
Expand All @@ -67,6 +82,11 @@ public void Draw()
}
}

public void Dispose()
{
_editorEvent.Unsubscribe(OnEditorEvent);
}

private HeaderDrawer.Button LockButton()
=> _selector.Selected == null
? HeaderDrawer.Button.Invisible
Expand Down Expand Up @@ -167,17 +187,23 @@ private void DrawPanel()

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)))
Expand Down Expand Up @@ -214,11 +240,6 @@ private void DrawBasicSettings()
}
}

/*private void SetFromClipboard()
{
}*/

private void ExportToClipboard()
{
try
Expand All @@ -232,4 +253,38 @@ private void ExportToClipboard()
_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!);
}
}

0 comments on commit ef6c701

Please sign in to comment.