Skip to content

Commit ef6c701

Browse files
committed
Added button to jump to template editing from profile tab, made TemplateManager IDisposable
1 parent faa4af9 commit ef6c701

File tree

5 files changed

+178
-20
lines changed

5 files changed

+178
-20
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using CustomizePlus.Templates.Data;
2+
using OtterGui.Classes;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace CustomizePlus.Templates.Events;
10+
11+
/// <summary>
12+
/// Triggered when something related to template editor happens
13+
/// </summary>
14+
public class TemplateEditorEvent() : EventWrapper<TemplateEditorEvent.Type, Template?, TemplateEditorEvent.Priority>(nameof(TemplateEditorEvent))
15+
{
16+
public enum Type
17+
{
18+
/// <summary>
19+
/// Called when something requests editor to be enabled.
20+
/// </summary>
21+
EditorEnableRequested,
22+
/// <summary>
23+
/// Called when something requests editor to be enabled. Stage 2 - logic after tab has been switched.
24+
/// </summary>
25+
EditorEnableRequestedStage2
26+
}
27+
28+
public enum Priority
29+
{
30+
MainWindow = -1,
31+
TemplatePanel
32+
}
33+
}

CustomizePlus/Templates/TemplateManager.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace CustomizePlus.Templates;
1515

16-
public class TemplateManager
16+
public class TemplateManager : IDisposable
1717
{
1818
private readonly SaveService _saveService;
1919
private readonly Logger _logger;
@@ -41,6 +41,11 @@ public TemplateManager(
4141
LoadTemplates();
4242
}
4343

44+
public void Dispose()
45+
{
46+
_reloadEvent.Unsubscribe(OnReload);
47+
}
48+
4449
public Template? GetTemplate(Guid templateId) => _templates.FirstOrDefault(d => d.UniqueId == templateId);
4550

4651
public void LoadTemplates()

CustomizePlus/UI/Windows/MainWindow/MainWindow.cs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
using ECommons.ImGuiMethods;
1717
using static System.Windows.Forms.AxHost;
1818
using Dalamud.Interface.Colors;
19+
using CustomizePlus.Templates.Events;
20+
using CustomizePlus.Templates.Data;
21+
using ECommons.Schedulers;
1922

2023
namespace CustomizePlus.UI.Windows.MainWindow;
2124

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

40+
private readonly TemplateEditorEvent _templateEditorEvent;
41+
42+
/// <summary>
43+
/// Used to force the main window to switch to specific tab
44+
/// </summary>
45+
private string? _switchToTab = null;
46+
47+
private Action? _actionAfterTabSwitch = null;
48+
3749
public MainWindow(
3850
DalamudPluginInterface pluginInterface,
3951
SettingsTab settingsTab,
@@ -45,7 +57,8 @@ public MainWindow(
4557
PluginStateBlock pluginStateBlock,
4658
TemplateEditorManager templateEditorManager,
4759
PluginConfiguration configuration,
48-
HookingService hookingService
60+
HookingService hookingService,
61+
TemplateEditorEvent templateEditorEvent
4962
) : base($"Customize+ v{Plugin.Version}###CPlusMainWindow")
5063
{
5164
_settingsTab = settingsTab;
@@ -61,6 +74,10 @@ HookingService hookingService
6174
_configuration = configuration;
6275
_hookingService = hookingService;
6376

77+
_templateEditorEvent = templateEditorEvent;
78+
79+
_templateEditorEvent.Subscribe(OnTemplateEditorEvent, TemplateEditorEvent.Priority.MainWindow);
80+
6481
pluginInterface.UiBuilder.DisableGposeUiHide = true;
6582
SizeConstraints = new WindowSizeConstraints()
6683
{
@@ -73,7 +90,7 @@ HookingService hookingService
7390

7491
public void Dispose()
7592
{
76-
//throw new NotImplementedException();
93+
_templateEditorEvent.Unsubscribe(OnTemplateEditorEvent);
7794
}
7895

7996
public override void Draw()
@@ -83,13 +100,21 @@ public override void Draw()
83100
using (var disabled = ImRaii.Disabled(_hookingService.RenderHookFailed || _hookingService.MovementHookFailed))
84101
{
85102
LockWindowClosureIfNeeded();
86-
ImGuiEx.EzTabBar("##tabs", [
103+
ImGuiEx.EzTabBar("##tabs", null, _switchToTab, [
87104
("Settings", _settingsTab.Draw, null, true),
88105
("Templates", _templatesTab.Draw, null, true),
89106
("Profiles", _profilesTab.Draw, null, true),
90107
(_configuration.DebuggingModeEnabled ? "IPC Test" : null, _ipcTestTab.Draw, ImGuiColors.DalamudGrey, true),
91108
(_configuration.DebuggingModeEnabled ? "State monitoring" : null, _stateMonitoringTab.Draw, ImGuiColors.DalamudGrey, true),
92109
]);
110+
111+
_switchToTab = null;
112+
113+
if (_actionAfterTabSwitch != null)
114+
{
115+
_actionAfterTabSwitch();
116+
_actionAfterTabSwitch = null;
117+
}
93118
}
94119

95120
_pluginStateBlock.Draw(yPos);
@@ -108,4 +133,24 @@ private void LockWindowClosureIfNeeded()
108133
RespectCloseHotkey = true;
109134
}
110135
}
136+
137+
private void OnTemplateEditorEvent(TemplateEditorEvent.Type type, Template? template)
138+
{
139+
if (type != TemplateEditorEvent.Type.EditorEnableRequested)
140+
return;
141+
142+
if (template == null)
143+
return;
144+
145+
if (!template.IsWriteProtected && !_templateEditorManager.IsEditorActive)
146+
{
147+
new TickScheduler(() =>
148+
{
149+
_switchToTab = "Templates";
150+
151+
//To make sure the tab has switched, ugly but imgui is shit and I don't trust it.
152+
_actionAfterTabSwitch = () => { _templateEditorEvent.Invoke(TemplateEditorEvent.Type.EditorEnableRequestedStage2, template); };
153+
});
154+
}
155+
}
111156
}

CustomizePlus/UI/Windows/MainWindow/Tabs/Profiles/ProfilePanel.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77
using System.Linq;
88
using System.Numerics;
99
using CustomizePlus.Profiles;
10-
using CustomizePlus.Game.Services;
1110
using CustomizePlus.Configuration.Data;
1211
using CustomizePlus.Profiles.Data;
1312
using CustomizePlus.UI.Windows.Controls;
1413
using CustomizePlus.Templates;
15-
using CustomizePlus.Core.Helpers;
16-
using System.Windows.Forms;
1714
using CustomizePlus.Core.Data;
18-
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
15+
using CustomizePlus.Templates.Events;
1916

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

@@ -26,6 +23,7 @@ public class ProfilePanel
2623
private readonly PluginConfiguration _configuration;
2724
private readonly TemplateCombo _templateCombo;
2825
private readonly TemplateEditorManager _templateEditorManager;
26+
private readonly TemplateEditorEvent _templateEditorEvent;
2927

3028
private string? _newName;
3129
private string? _newCharacterName;
@@ -43,13 +41,15 @@ public ProfilePanel(
4341
ProfileManager manager,
4442
PluginConfiguration configuration,
4543
TemplateCombo templateCombo,
46-
TemplateEditorManager templateEditorManager)
44+
TemplateEditorManager templateEditorManager,
45+
TemplateEditorEvent templateEditorEvent)
4746
{
4847
_selector = selector;
4948
_manager = manager;
5049
_configuration = configuration;
5150
_templateCombo = templateCombo;
5251
_templateEditorManager = templateEditorManager;
52+
_templateEditorEvent = templateEditorEvent;
5353
}
5454

5555
public void Draw()
@@ -247,7 +247,7 @@ private void DrawBasicSettings()
247247

248248
private void DrawTemplateArea()
249249
{
250-
using var table = ImRaii.Table("SetTable", 3, ImGuiTableFlags.RowBg | ImGuiTableFlags.ScrollX | ImGuiTableFlags.ScrollY);
250+
using var table = ImRaii.Table("SetTable", 4, ImGuiTableFlags.RowBg | ImGuiTableFlags.ScrollX | ImGuiTableFlags.ScrollY);
251251
if (!table)
252252
return;
253253

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

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

259+
ImGui.TableSetupColumn("##editbtn", ImGuiTableColumnFlags.WidthFixed, 120 * ImGuiHelpers.GlobalScale);
260+
259261
ImGui.TableHeadersRow();
260262

261263
//warn: .ToList() might be performance critical at some point
@@ -277,6 +279,24 @@ private void DrawTemplateArea()
277279
ImGui.TableNextColumn();
278280
_templateCombo.Draw(_selector.Selected!, template, idx);
279281
DrawDragDrop(_selector.Selected!, idx);
282+
ImGui.TableNextColumn();
283+
284+
var disabledCondition = _templateEditorManager.IsEditorActive || template.IsWriteProtected;
285+
using (var disabled = ImRaii.Disabled(disabledCondition))
286+
{
287+
if (ImGui.Button("Open in editor"))
288+
_templateEditorEvent.Invoke(TemplateEditorEvent.Type.EditorEnableRequested, template);
289+
ImGuiUtil.HoverTooltip("Open this template in the template editor");
290+
}
291+
292+
if(disabledCondition)
293+
{
294+
ImGui.SameLine();
295+
ImGui.PushStyleColor(ImGuiCol.Text, Constants.Colors.Warning);
296+
ImGuiUtil.PrintIcon(FontAwesomeIcon.ExclamationTriangle);
297+
ImGui.PopStyleColor();
298+
ImGuiUtil.HoverTooltip("Can not be edited because this template is either write protected or template editor is already enabled.");
299+
}
280300
}
281301

282302
ImGui.TableNextColumn();

CustomizePlus/UI/Windows/MainWindow/Tabs/Templates/TemplatePanel.cs

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
using CustomizePlus.Core.Helpers;
1616
using CustomizePlus.Templates.Data;
1717
using OtterGui.Log;
18+
using CustomizePlus.Templates.Events;
19+
using ECommons.Schedulers;
1820

1921
namespace CustomizePlus.UI.Windows.MainWindow.Tabs.Templates;
2022

21-
public class TemplatePanel
23+
public class TemplatePanel : IDisposable
2224
{
2325
private readonly TemplateFileSystemSelector _selector;
2426
private readonly TemplateManager _manager;
@@ -28,9 +30,16 @@ public class TemplatePanel
2830
private readonly PopupSystem _popupSystem;
2931
private readonly Logger _logger;
3032

33+
private readonly TemplateEditorEvent _editorEvent;
34+
3135
private string? _newName;
3236
private Template? _changedTemplate;
3337

38+
/// <summary>
39+
/// Set to true if we received OnEditorEvent EditorEnableRequested and waiting for selector value to be changed.
40+
/// </summary>
41+
private bool _isEditorEnablePending = false;
42+
3443
private string SelectionName
3544
=> _selector.Selected == null ? "No Selection" : _selector.IncognitoMode ? _selector.Selected.Incognito : _selector.Selected.Name.Text;
3645

@@ -41,7 +50,8 @@ public TemplatePanel(
4150
PluginConfiguration configuration,
4251
MessageService messageService,
4352
PopupSystem popupSystem,
44-
Logger logger)
53+
Logger logger,
54+
TemplateEditorEvent editorEvent)
4555
{
4656
_selector = selector;
4757
_manager = manager;
@@ -51,6 +61,11 @@ public TemplatePanel(
5161
_popupSystem = popupSystem;
5262
_logger = logger;
5363

64+
_editorEvent = editorEvent;
65+
66+
_editorEvent.Subscribe(OnEditorEvent, TemplateEditorEvent.Priority.TemplatePanel);
67+
68+
_selector.SelectionChanged += SelectorSelectionChanged;
5469
}
5570

5671
public void Draw()
@@ -67,6 +82,11 @@ public void Draw()
6782
}
6883
}
6984

85+
public void Dispose()
86+
{
87+
_editorEvent.Unsubscribe(OnEditorEvent);
88+
}
89+
7090
private HeaderDrawer.Button LockButton()
7191
=> _selector.Selected == null
7292
? HeaderDrawer.Button.Invisible
@@ -167,17 +187,23 @@ private void DrawPanel()
167187

168188
private void DrawEditorToggle()
169189
{
190+
(bool isEditorAllowed, bool isEditorActive) = CanToggleEditor();
191+
170192
if (ImGuiUtil.DrawDisabledButton($"{(_boneEditor.IsEditorActive ? "Finish" : "Start")} bone editing", Vector2.Zero,
171-
"Toggle the bone editor for this template",
172-
(_selector.Selected?.IsWriteProtected ?? true) || !_configuration.PluginEnabled))
193+
"Toggle the bone editor for this template", !isEditorAllowed))
173194
{
174-
if (!_boneEditor.IsEditorActive)
195+
if (!isEditorActive)
175196
_boneEditor.EnableEditor(_selector.Selected!);
176197
else
177198
_boneEditor.DisableEditor();
178199
}
179200
}
180201

202+
private (bool isEditorAllowed, bool isEditorActive) CanToggleEditor()
203+
{
204+
return ((!_selector.Selected?.IsWriteProtected ?? false) || _configuration.PluginEnabled, _boneEditor.IsEditorActive);
205+
}
206+
181207
private void DrawBasicSettings()
182208
{
183209
using (var style = ImRaii.PushStyle(ImGuiStyleVar.ButtonTextAlign, new Vector2(0, 0.5f)))
@@ -214,11 +240,6 @@ private void DrawBasicSettings()
214240
}
215241
}
216242

217-
/*private void SetFromClipboard()
218-
{
219-
220-
}*/
221-
222243
private void ExportToClipboard()
223244
{
224245
try
@@ -232,4 +253,38 @@ private void ExportToClipboard()
232253
_popupSystem.ShowPopup(PopupSystem.Messages.ActionError);
233254
}
234255
}
256+
257+
258+
private void SelectorSelectionChanged(Template? oldSelection, Template? newSelection, in TemplateFileSystemSelector.TemplateState state)
259+
{
260+
if (!_isEditorEnablePending)
261+
return;
262+
263+
_isEditorEnablePending = false;
264+
265+
_boneEditor.EnableEditor(_selector.Selected!);
266+
}
267+
268+
private void OnEditorEvent(TemplateEditorEvent.Type type, Template? template)
269+
{
270+
if (type != TemplateEditorEvent.Type.EditorEnableRequestedStage2)
271+
return;
272+
273+
if(template == null)
274+
return;
275+
276+
(bool isEditorAllowed, bool isEditorActive) = CanToggleEditor();
277+
278+
if (!isEditorAllowed || isEditorActive)
279+
return;
280+
281+
if(_selector.Selected != template)
282+
{
283+
_selector.SelectByValue(template);
284+
285+
_isEditorEnablePending = true;
286+
}
287+
else
288+
_boneEditor.EnableEditor(_selector.Selected!);
289+
}
235290
}

0 commit comments

Comments
 (0)