Glamaholic/Glamaholic/PluginUi.cs

115 lines
3.9 KiB
C#
Raw Permalink Normal View History

2021-11-19 17:55:07 +00:00
using System;
using System.Collections.Generic;
2021-11-23 04:18:52 +00:00
using System.Linq;
2023-09-29 01:07:21 +00:00
using Dalamud.Interface.Internal;
2021-11-21 07:17:58 +00:00
using FFXIVClientStructs.FFXIV.Client.System.Framework;
using FFXIVClientStructs.FFXIV.Client.UI.Agent;
2021-11-19 17:55:07 +00:00
using Glamaholic.Ui;
using Glamaholic.Ui.Helpers;
namespace Glamaholic {
internal class PluginUi : IDisposable {
internal Plugin Plugin { get; }
2023-09-29 01:07:21 +00:00
private Dictionary<ushort, IDalamudTextureWrap> Icons { get; } = new();
2021-11-19 17:55:07 +00:00
private MainInterface MainInterface { get; }
private EditorHelper EditorHelper { get; }
private ExamineHelper ExamineHelper { get; }
2021-11-23 18:41:58 +00:00
private TryOnHelper TryOnHelper { get; }
2021-11-23 00:58:39 +00:00
internal List<AlternativeFinder> AlternativeFinders { get; } = new();
2021-11-23 04:18:52 +00:00
internal List<(string, string)> Help { get; } = new();
2021-11-19 17:55:07 +00:00
internal PluginUi(Plugin plugin) {
this.Plugin = plugin;
2021-11-23 04:18:52 +00:00
foreach (var entry in Resourcer.Resource.AsString("help.txt").Split("---")) {
var lines = entry.Trim().Split(new[] { "\n", "\r\n" }, StringSplitOptions.TrimEntries);
if (lines.Length == 0 || !lines[0].StartsWith("#")) {
continue;
}
var title = lines[0][1..].Trim();
var content = string.Join(" ", lines[1..]
.SkipWhile(string.IsNullOrWhiteSpace)
.Select(line => string.IsNullOrWhiteSpace(line) ? "\n" : line));
this.Help.Add((title, content));
}
2021-11-19 17:55:07 +00:00
this.MainInterface = new MainInterface(this);
this.EditorHelper = new EditorHelper(this);
this.ExamineHelper = new ExamineHelper(this);
2021-11-23 18:41:58 +00:00
this.TryOnHelper = new TryOnHelper(this);
2021-11-19 17:55:07 +00:00
this.Plugin.Interface.UiBuilder.Draw += this.Draw;
this.Plugin.Interface.UiBuilder.OpenConfigUi += this.OpenMainInterface;
}
public void Dispose() {
this.Plugin.Interface.UiBuilder.OpenConfigUi -= this.OpenMainInterface;
this.Plugin.Interface.UiBuilder.Draw -= this.Draw;
foreach (var icon in this.Icons.Values) {
icon.Dispose();
}
}
internal void OpenMainInterface() {
this.MainInterface.Open();
}
internal void ToggleMainInterface() {
this.MainInterface.Toggle();
}
2023-09-29 01:07:21 +00:00
internal IDalamudTextureWrap? GetIcon(ushort id) {
2021-11-19 17:55:07 +00:00
if (this.Icons.TryGetValue(id, out var cached)) {
return cached;
}
2023-09-29 01:07:21 +00:00
var icon = this.Plugin.TextureProvider.GetIcon(id);
2021-11-19 17:55:07 +00:00
if (icon == null) {
return null;
}
this.Icons[id] = icon;
return icon;
}
private void Draw() {
this.MainInterface.Draw();
this.EditorHelper.Draw();
this.ExamineHelper.Draw();
2021-11-23 18:41:58 +00:00
this.TryOnHelper.Draw();
2021-11-23 00:58:39 +00:00
this.AlternativeFinders.RemoveAll(finder => {
finder.Draw();
return !finder.Visible;
});
2021-11-19 17:55:07 +00:00
}
internal void SwitchPlate(int idx, bool scrollTo = false) {
this.MainInterface.SwitchPlate(idx, scrollTo);
}
2021-11-21 07:17:58 +00:00
internal unsafe void TryOn(IEnumerable<SavedGlamourItem> items) {
void SetTryOnSave(bool save) {
2021-12-21 07:36:48 +00:00
var tryOnAgent = (IntPtr) Framework.Instance()->GetUiModule()->GetAgentModule()->GetAgentByInternalId(AgentId.Tryon);
2021-11-21 07:17:58 +00:00
if (tryOnAgent != IntPtr.Zero) {
2023-01-15 19:53:35 +00:00
*(byte*) (tryOnAgent + 0x30A) = (byte) (save ? 1 : 0);
2021-11-21 07:17:58 +00:00
}
}
SetTryOnSave(false);
foreach (var mirage in items) {
if (mirage.ItemId == 0) {
continue;
}
2021-11-23 04:20:18 +00:00
2021-11-21 07:17:58 +00:00
this.Plugin.Functions.TryOn(mirage.ItemId, mirage.StainId);
SetTryOnSave(true);
}
}
2021-11-19 17:55:07 +00:00
}
}