From e0718ac856995919ec29b2fbbedafc8a824df339 Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Sat, 3 Sep 2022 12:01:06 +0200 Subject: [PATCH] optimize code for coloring names --- PlayerTags/Features/ChatTagTargetFeature.cs | 39 ++------- .../Features/NameplateTagTargetFeature.cs | 29 ++----- PlayerTags/Features/TagTargetFeature.cs | 85 +++++++++++++++++-- 3 files changed, 90 insertions(+), 63 deletions(-) diff --git a/PlayerTags/Features/ChatTagTargetFeature.cs b/PlayerTags/Features/ChatTagTargetFeature.cs index 3300cca..5906835 100644 --- a/PlayerTags/Features/ChatTagTargetFeature.cs +++ b/PlayerTags/Features/ChatTagTargetFeature.cs @@ -3,11 +3,13 @@ using Dalamud.Game.ClientState.Objects.Types; using Dalamud.Game.Text; using Dalamud.Game.Text.SeStringHandling; using Dalamud.Game.Text.SeStringHandling.Payloads; +using Lumina.Excel.GeneratedSheets; using PlayerTags.Configuration; using PlayerTags.Data; using System; using System.Collections.Generic; using System.Linq; +using Action = System.Action; namespace PlayerTags.Features { @@ -240,44 +242,17 @@ namespace PlayerTags.Features { var customTag = m_PluginData.CustomTags.FirstOrDefault(tag => tag.CustomId.Value == customTagId); if (customTag != null) - { - if (IsTagVisible(customTag, stringMatch.GameObject)) - { - if (customTag.TextColor.InheritedValue != null) - { - if (message.Payloads.Any(payload => payload is TextPayload || payload is PlayerPayload) - && customTag.IsTextColorAppliedToChatName.InheritedValue != null - && customTag.IsTextColorAppliedToChatName.InheritedValue.Value) - { - int payloadIndex = message.Payloads.IndexOf(stringMatch.PreferredPayload); - message.Payloads.Insert(payloadIndex + 1, new UIForegroundPayload(0)); - message.Payloads.Insert(payloadIndex, (new UIForegroundPayload(customTag.TextColor.InheritedValue.Value))); - } - } - } - } + applyTextFormatting(customTag); } if (stringMatch.GameObject is PlayerCharacter playerCharacter1) { if (playerCharacter1.ClassJob.GameData != null && m_PluginData.JobTags.TryGetValue(playerCharacter1.ClassJob.GameData.Abbreviation, out var jobTag)) - { - if (IsTagVisible(jobTag, stringMatch.GameObject)) - { - if (jobTag.TextColor.InheritedValue != null) - { - if (message.Payloads.Any(payload => payload is TextPayload || payload is PlayerPayload) - && jobTag.IsTextColorAppliedToChatName.InheritedValue != null - && jobTag.IsTextColorAppliedToChatName.InheritedValue.Value) - { - int payloadIndex = message.Payloads.IndexOf(stringMatch.PreferredPayload); - message.Payloads.Insert(payloadIndex + 1, new UIForegroundPayload(0)); - message.Payloads.Insert(payloadIndex, (new UIForegroundPayload(jobTag.TextColor.InheritedValue.Value))); - } - } - } - } + applyTextFormatting(jobTag); } + + void applyTextFormatting(Tag tag) + => ApplyTextFormatting(stringMatch.GameObject, tag, new[] { message }, new[] { tag.IsTextColorAppliedToChatName }, stringMatch.PreferredPayload); } ApplyStringChanges(message, stringChanges, stringMatch.PreferredPayload); diff --git a/PlayerTags/Features/NameplateTagTargetFeature.cs b/PlayerTags/Features/NameplateTagTargetFeature.cs index 122eb0d..ff3a3b3 100644 --- a/PlayerTags/Features/NameplateTagTargetFeature.cs +++ b/PlayerTags/Features/NameplateTagTargetFeature.cs @@ -219,34 +219,17 @@ namespace PlayerTags.Features { var customTag = m_PluginData.CustomTags.FirstOrDefault(tag => tag.CustomId.Value == customTagId); if (customTag != null) - applyTextColor(customTag); + applyTextFormatting(customTag); } if (playerCharacter1.ClassJob.GameData != null && m_PluginData.JobTags.TryGetValue(playerCharacter1.ClassJob.GameData.Abbreviation, out var jobTag)) - applyTextColor(jobTag); + applyTextFormatting(jobTag); - - void applyTextColor(Tag tag) + void applyTextFormatting(Tag tag) { - if (IsTagVisible(tag, gameObject)) - { - if (tag.TextColor.InheritedValue != null) - { - applyTextColor2(name, tag.IsTextColorAppliedToNameplateName, tag.TextColor); - applyTextColor2(title, tag.IsTextColorAppliedToNameplateTitle, tag.TextColor); - applyTextColor2(freeCompany, tag.IsTextColorAppliedToNameplateFreeCompany, tag.TextColor); - } - } - } - void applyTextColor2(SeString destPayload, InheritableValue enableFlag, InheritableValue colorValue) - { - if (destPayload.Payloads.Any(payload => payload is TextPayload) - && enableFlag.InheritedValue != null - && enableFlag.InheritedValue.Value) - { - destPayload.Payloads.Insert(0, (new UIForegroundPayload(colorValue.InheritedValue.Value))); - destPayload.Payloads.Add(new UIForegroundPayload(0)); - } + var destStrings = new[] { name, title, freeCompany }; + var isTextColorApplied = new[] { tag.IsTextColorAppliedToNameplateName, tag.IsTextColorAppliedToNameplateTitle, tag.IsTextColorAppliedToNameplateFreeCompany }; + ApplyTextFormatting(gameObject, tag, new[] { name, title, freeCompany }, isTextColorApplied, null); } } } diff --git a/PlayerTags/Features/TagTargetFeature.cs b/PlayerTags/Features/TagTargetFeature.cs index 2c2bd4b..849c732 100644 --- a/PlayerTags/Features/TagTargetFeature.cs +++ b/PlayerTags/Features/TagTargetFeature.cs @@ -2,11 +2,15 @@ using Dalamud.Game.ClientState.Objects.Types; using Dalamud.Game.Text.SeStringHandling; using Dalamud.Game.Text.SeStringHandling.Payloads; +using FFXIVClientStructs.FFXIV.Client.Game.Object; using Lumina.Excel.GeneratedSheets; using PlayerTags.Data; +using PlayerTags.Inheritables; using System; using System.Collections.Generic; using System.Linq; +using System.Xml.Linq; +using GameObject = Dalamud.Game.ClientState.Objects.Types.GameObject; namespace PlayerTags.Features { @@ -111,10 +115,10 @@ namespace PlayerTags.Features newPayloads.Add(new EmphasisItalicPayload(true)); } - if (tag.TextGlowColor.InheritedValue != null) - { - newPayloads.Add(new UIGlowPayload(tag.TextGlowColor.InheritedValue.Value)); - } + //if (tag.TextGlowColor.InheritedValue != null) + //{ + // newPayloads.Add(new UIGlowPayload(tag.TextGlowColor.InheritedValue.Value)); + //} //if (tag.TextColor.InheritedValue != null) //{ @@ -128,10 +132,10 @@ namespace PlayerTags.Features // newPayloads.Add(new UIForegroundPayload(0)); //} - if (tag.TextGlowColor.InheritedValue != null) - { - newPayloads.Add(new UIGlowPayload(0)); - } + //if (tag.TextGlowColor.InheritedValue != null) + //{ + // newPayloads.Add(new UIGlowPayload(0)); + //} if (tag.IsTextItalic.InheritedValue != null && tag.IsTextItalic.InheritedValue.Value) { @@ -305,5 +309,70 @@ namespace PlayerTags.Features } } } + + protected void ApplyTextFormatting(GameObject gameObject, Tag tag, SeString[] destStrings, InheritableValue[] textColorApplied, Payload preferedPayload) + { + if (IsTagVisible(tag, gameObject)) + { + for (int i = 0; i < destStrings.Length; i++) + { + var destString = destStrings[i]; + var isTextColorApplied = textColorApplied[i]; + applyTextColor(destString, isTextColorApplied, tag.TextColor); + applyTextGlowColor(destString, isTextColorApplied, tag.TextGlowColor); + //applyTextItalicColor(destString, tag.IsTextItalic); // Disabled, because that is needed only for a few parts somewhere else. + } + } + + void applyTextColor(SeString destPayload, InheritableValue enableFlag, InheritableValue colorValue) + { + if (shouldApplyFormattingPayloads(destPayload) + && enableFlag.InheritedValue != null + && enableFlag.InheritedValue.Value + && colorValue.InheritedValue != null) + applyTextFormattingPayloads(destPayload, new UIForegroundPayload(colorValue.InheritedValue.Value), new UIForegroundPayload(0)); + } + + void applyTextGlowColor(SeString destPayload, InheritableValue enableFlag, InheritableValue colorValue) + { + if (shouldApplyFormattingPayloads(destPayload) + && enableFlag.InheritedValue != null + && enableFlag.InheritedValue.Value + && colorValue.InheritedValue != null) + applyTextFormattingPayloads(destPayload, new UIGlowPayload(colorValue.InheritedValue.Value), new UIGlowPayload(0)); + } + + //void applyTextItalicColor(SeString destPayload, InheritableValue italicValue) + //{ + // if (shouldApplyFormattingPayloads(destPayload) + // && italicValue.InheritedValue != null + // && italicValue.InheritedValue.Value) + // applyTextFormattingPayloads(destPayload, new EmphasisItalicPayload(true), new EmphasisItalicPayload(false)); + //} + + bool shouldApplyFormattingPayloads(SeString destPayload) + => destPayload.Payloads.Any(payload => payload is TextPayload || payload is PlayerPayload); + + void applyTextFormattingPayloads(SeString destPayload, Payload startPayload, Payload endPayload) + { + if (preferedPayload == null) + applyTextFormattingPayloadToStartAndEnd(destPayload, startPayload, endPayload); + else + applyTextFormattingPayloadsToSpecificPosition(destPayload, startPayload, endPayload, preferedPayload); + } + + void applyTextFormattingPayloadToStartAndEnd(SeString destPayload, Payload startPayload, Payload endPayload) + { + destPayload.Payloads.Insert(0, startPayload); + destPayload.Payloads.Add(endPayload); + } + + void applyTextFormattingPayloadsToSpecificPosition(SeString destPayload, Payload startPayload, Payload endPayload, Payload preferedPayload) + { + int payloadIndex = destPayload.Payloads.IndexOf(preferedPayload); + destPayload.Payloads.Insert(payloadIndex + 1, endPayload); + destPayload.Payloads.Insert(payloadIndex, startPayload); + } + } } }