optimize code for coloring names

This commit is contained in:
2022-09-03 12:01:06 +02:00
parent 39b49fb4a8
commit e0718ac856
3 changed files with 90 additions and 63 deletions

View File

@@ -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);

View File

@@ -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<bool> enableFlag, InheritableValue<ushort> 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);
}
}
}

View File

@@ -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<bool>[] 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<bool> enableFlag, InheritableValue<ushort> 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<bool> enableFlag, InheritableValue<ushort> 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<bool> 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);
}
}
}
}