Skip to content

Commit

Permalink
Add SCH fairies and the option to scale Eos and Selene up to norm…
Browse files Browse the repository at this point in the history
…al fairy size.

Reset combo boxes in ConfigWindow on close.
  • Loading branch information
Kurochi51 committed Mar 21, 2024
1 parent 7877caf commit d2be882
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 43 deletions.
1 change: 1 addition & 0 deletions PetScale/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class Configuration : IPluginConfiguration
{
public int Version { get; set; } = 0;
public IList<PetStruct> PetData { get; set; } = [];
public bool FairyResize { get; set; } = false;


public void Save(DalamudPluginInterface pi) => pi.SavePluginConfig(this);
Expand Down
14 changes: 8 additions & 6 deletions PetScale/Enums/PetEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@ namespace PetScale.Enums;
/// <summary>
/// Pet exd rows corresponding to summoner pets with scaling
/// </summary>
/// <remarks>
/// Important that value name is identical with <see cref="PetModel"/> values to map name and scales of pet
/// </remarks>
public enum PetRow : uint
{
Bahamut = 10,
Phoenix = 14,
Ifrit = 30,
Titan = 31,
Garuda = 32,
// SCH Fairies
Eos = 6,
Selene = 7,
Seraph = 15,
}

/// <summary>
/// Since there's no good way of identifying BNPCs in game without using an external source, this is a direct mapping of Character.CharacterData.ModelCharaId
/// </summary>
/// <remarks>
/// Important that value name is identical with <see cref="PetModel"/> values to map name and scales of pet
/// </remarks>
public enum PetModel
{
AllPets = 0,
Expand All @@ -30,6 +28,10 @@ public enum PetModel
Ifrit = 3122,
Titan = 3124,
Garuda = 3123,
// SCH Fairies
Eos = 407,
Selene = 408,
Seraph = 2619,
}

public enum PetSize
Expand Down
3 changes: 3 additions & 0 deletions PetScale/Helpers/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public unsafe void CachePlayerList(uint playerObjectId, Queue<string> queue, Spa
}
}

public static bool IsFairy(int modelId)
=> (PetModel)modelId is PetModel.Eos or PetModel.Selene;

public unsafe bool PetVisible(BattleChara* pet)
{
if (pet is null || pet->Character.GameObject.GetDrawObject() is null)
Expand Down
20 changes: 13 additions & 7 deletions PetScale/PetScale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace PetScale;
public sealed class PetScale : IDalamudPlugin
{
private const string CommandName = "/pscale";
public const string PluginName = "Pet Scale";
public const string Others = "Other players";

private readonly DalamudPluginInterface pluginInterface;
private readonly Configuration config;
Expand All @@ -42,7 +42,6 @@ public sealed class PetScale : IDalamudPlugin
private readonly Dictionary<string, (float smallScale, float mediumScale, float largeScale)> petSizeMap = new(StringComparer.OrdinalIgnoreCase);
private readonly Stopwatch stopwatch = new();
private readonly TimeSpan dictionaryExpirationTime = TimeSpan.FromMilliseconds(500); // used via .TotalMilliseconds
private const string Others = "Other players";

private readonly Dictionary<PetRow, PetModel> petModelMap = new()
{
Expand Down Expand Up @@ -105,7 +104,7 @@ public sealed class PetScale : IDalamudPlugin
stopwatch.Start();

_ = Task.Run(InitSheet);
ConfigWindow.Save(save: false);
ConfigWindow.ProcessPetData(save: false);
QueueOnlyExistingData();
}

Expand Down Expand Up @@ -152,12 +151,15 @@ private void InitSheet()
continue;
}
var scales = (pet.Unknown5 / 100f, pet.Unknown6 / 100f, pet.Unknown7 / 100f);
if (scales.Item1 >= 1 || scales.Item2 >= 1)
if ((scales.Item1 >= 1 || scales.Item2 >= 1) && !pet.Unknown16)
{
continue;
}
petSizeMap.Add(pet.Name, scales);
ConfigWindow.petMap.Add(pet.Name, petModelMap[(PetRow)pet.RowId]);
if (petModelMap.ContainsKey((PetRow)pet.RowId))
{
ConfigWindow.petMap.Add(pet.Name, petModelMap[(PetRow)pet.RowId]);
}
}
foreach (var entry in petSizeMap)
{
Expand Down Expand Up @@ -264,9 +266,13 @@ private unsafe void ParseDictionary(PlayerCharacter player)
}
#if DEBUG
DevWindow.Print(petName + ": " + pet->Character.CharacterData.ModelSkeletonId + " owned by " + characterName + " size " + pet->Character.GameObject.Scale);
//DevWindow.Print("Visibility: " + pet->Character.GameObject.GetDrawObject()->IsVisible);
//DevWindow.Print("RenderFlags: " + pet->Character.GameObject.RenderFlags);
#endif
if (config.FairyResize && Utilities.IsFairy(pet->Character.CharacterData.ModelCharaId))
{
utilities.SetScale(pet, 1.5f);
activePetDictionary[pair.Key] = (pair.Value.character, true);
continue;
}
if (ParseStruct(pet, characterName, petName, pet->Character.CharacterData.ModelCharaId, character->GameObject.ObjectID == player.ObjectId))
{
activePetDictionary[pair.Key] = (pair.Value.character, true);
Expand Down
102 changes: 72 additions & 30 deletions PetScale/Windows/ConfigWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Dalamud.Plugin.Services;
using Dalamud.Interface.Utility;
using Dalamud.Interface.Windowing;
using Dalamud.Interface.Components;
using Dalamud.Interface.Utility.Raii;
using Dalamud.Interface.ManagedFontAtlas;
using Dalamud.Interface.ImGuiNotification;
Expand Down Expand Up @@ -42,14 +43,16 @@ public sealed class ConfigWindow : Window, IDisposable
private readonly CancellationToken cToken;
private readonly Notification notification = new();
private const string DefaultPetSelection = "Pet";
private const string DefaultSizeSelection = "Size";
private const string DefaultCharacterSelection = "Characters";
private const string LongestCharaName = "WWWWWWWWWWWWWWW WWWWW";
private const string LongestSize = "Medium";

public Dictionary<string, PetModel> petMap { get; } = new(StringComparer.Ordinal);
private Queue<string> players => plugin.players;
private IList<PetStruct> petData => config.PetData;

private string petSelection = DefaultPetSelection, longestPetName = string.Empty, sizeSelection = "Size", charaName = "Characters";
private string petSelection = DefaultPetSelection, longestPetName = string.Empty, sizeSelection = DefaultSizeSelection, charaName = DefaultCharacterSelection;
private float tableButtonAlignmentOffset, charaWidth, petWidth, sizesWidth;
private bool fontChange;

Expand Down Expand Up @@ -89,12 +92,51 @@ public override void OnOpen()

public override void OnClose()
{
Save(save: true);
ProcessPetData(save: true);
charaName = DefaultCharacterSelection;
petSelection = DefaultPetSelection;
sizeSelection = DefaultSizeSelection;
}

public override void Draw()
{
ResizeIfNeeded();
using var tabBar = ImRaii.TabBar("TabBar");
if (!tabBar)
{
return;
}
GeneralTab();
MiscTab();
}

private void MiscTab()
{
using var miscTab = ImRaii.TabItem("Misc");
if (!miscTab)
{
return;
}
var fairyResize = config.FairyResize;
if (ImGui.Checkbox("Scale SCH fairy to match size of other fairies", ref fairyResize))
{
config.FairyResize = fairyResize;
config.Save(pluginInterface);
}
ImGuiComponents.HelpMarker("Seraph is excluded, as she's bigger by default");
DrawBottomButtons(onlyClose: true);
}

private void GeneralTab()
{
using var generalTab = ImRaii.TabItem("General");
if (!generalTab)
{
return;
}
#if DEBUG
DevWindow.Print("Summon entries: " + petData.Count.ToString());
#endif
ImGui.TextUnformatted("Amount of players: " + GetPlayerCount(players.Count, plugin.clientState.IsLoggedIn).ToString(CultureInfo.InvariantCulture));
var buttonPressed = false;
DrawComboBox("Characters", charaName, charaWidth, out charaName, players, filter: true);
Expand All @@ -112,17 +154,17 @@ public override void Draw()
var error = false;
if (charaName.IsNullOrWhitespace() || charaName.Equals("Characters", StringComparison.Ordinal))
{
CreateNotification("Invalid Character selected", PetScale.PluginName, NotificationType.Error);
CreateNotification("Invalid Character selected", "Invalid entry", NotificationType.Error);
error = true;
}
if (petSelection.Equals(DefaultPetSelection, StringComparison.Ordinal))
{
CreateNotification("Invalid Pet selected", PetScale.PluginName, NotificationType.Error);
CreateNotification("Invalid Pet selected", "Invalid entry", NotificationType.Error);
error = true;
}
if (sizeSelection.Equals("Size", StringComparison.Ordinal))
{
CreateNotification("Invalid Pet Size selected", PetScale.PluginName, NotificationType.Error);
CreateNotification("Invalid Pet Size selected", "Invalid entry", NotificationType.Error);
error = true;
}
if (!error)
Expand All @@ -132,9 +174,6 @@ public override void Draw()
}
DisplayEntries();
DrawBottomButtons();
#if DEBUG
DevWindow.Print("Summon entries: " + petData.Count.ToString());
#endif
}

private unsafe void DisplayEntries()
Expand Down Expand Up @@ -185,7 +224,7 @@ private unsafe void DisplayEntries()
if (DrawIconButton(fontHandle: null, deleteButtonIcon, buttonId + deleteButtonIcon))
{
petData.RemoveAt(i);
CreateNotification("Entry " + item.CharacterName + ", " + petSelection + ", " + sizeMap[item.PetSize] + " was removed.", PetScale.PluginName);
CreateNotification("Entry " + item.CharacterName + ", " + petSelection + ", " + sizeMap[item.PetSize] + " was removed.", "Entry removed");
itemRemoved = true;
}
}
Expand All @@ -194,7 +233,7 @@ private unsafe void DisplayEntries()
clipper.Destroy();
if (itemRemoved)
{
Save(save: true);
ProcessPetData(save: true);
}
}

Expand Down Expand Up @@ -332,7 +371,7 @@ private void CheckPossibleEntry()
petData.Add(currentPetData);
CreateNotification(
"Entry " + currentPetData.CharacterName + ", " + petSelection + ", " + sizeMap[currentPetSize.Key] + " was added.",
PetScale.PluginName);
"New entry");
}
else if (!checkPet.PetSize.Equals(currentPetData.PetSize))
{
Expand All @@ -341,27 +380,30 @@ private void CheckPossibleEntry()
checkPet.PetSize = currentPetSize.Key;
petData[index] = checkPet;
log.Debug("Entry {name} with {pet} at {size} got changed.", checkPet.CharacterName, petSelection, checkPet.PetSize);
CreateNotification(entry + sizeMap[petData[index].PetSize], PetScale.PluginName);
CreateNotification(entry + sizeMap[petData[index].PetSize], "Entry changed");
}
Save(save: true);
ProcessPetData(save: true);
}

private void DrawBottomButtons()
private void DrawBottomButtons(bool onlyClose = false)
{
var originPos = ImGui.GetCursorPos();
ImGui.SetCursorPosX(10f);
ImGui.SetCursorPosY(ImGui.GetWindowContentRegionMax().Y - ImGui.GetFrameHeight() - (3f * ImGuiHelpers.GlobalScale) + (ImGui.GetScrollY() * 2));
if (ImGui.Button("Update Character List"))
if (!onlyClose)
{
RequestCache(newCache: true);
}
ImGui.SameLine();
if (ImGui.Button("Clear All Entries"))
{
petData.Clear();
Save(save: true);
ImGui.SetCursorPosX(10f);
ImGui.SetCursorPosY(ImGui.GetWindowContentRegionMax().Y - ImGui.GetFrameHeight() - (3f * ImGuiHelpers.GlobalScale) + (ImGui.GetScrollY() * 2));
if (ImGui.Button("Update Character List"))
{
RequestCache(newCache: true);
}
ImGui.SameLine();
if (ImGui.Button("Clear All Entries"))
{
petData.Clear();
ProcessPetData(save: true);
}
ImGui.SetCursorPos(originPos);
}
ImGui.SetCursorPos(originPos);
ImGui.SetCursorPosX(ImGui.GetWindowContentRegionMax().X - ImGui.CalcTextSize("Close").X - 10f);
ImGui.SetCursorPosY(ImGui.GetWindowContentRegionMax().Y - ImGui.GetFrameHeight() - (3f * ImGuiHelpers.GlobalScale) + (ImGui.GetScrollY() * 2));
if (ImGui.Button("Close"))
Expand Down Expand Up @@ -402,7 +444,7 @@ private void ResizeIfNeeded()
}
}

public void Save(bool save)
public void ProcessPetData(bool save)
{
if (petData.Count is 0)
{
Expand All @@ -413,7 +455,7 @@ public void Save(bool save)
}
return;
}
var tempEnumerable = petData.Where(item => item.CharacterName.Equals("Other players", StringComparison.Ordinal));
var tempEnumerable = petData.Where(item => item.CharacterName.Equals(PetScale.Others, StringComparison.Ordinal));
if (tempEnumerable.Count() is not 0)
{
var tempList = tempEnumerable.ToList();
Expand All @@ -422,7 +464,7 @@ public void Save(bool save)
{
config.PetData = tempList;
}
var otherEntry = tempList.Last(item => item.CharacterName.Equals("Other players", StringComparison.Ordinal));
var otherEntry = tempList.Last(item => item.CharacterName.Equals(PetScale.Others, StringComparison.Ordinal));
plugin.lastIndexOfOthers = tempList.LastIndexOf(otherEntry);
}
else
Expand Down Expand Up @@ -459,10 +501,10 @@ private async void QueueColumnWidthChange(IFontHandle handle, ILockedImFont lock
fontChange = true;
}

private void CreateNotification(string content, string title, NotificationType type = NotificationType.None)
private void CreateNotification(string content, string title, NotificationType type = NotificationType.Success)
{
notification.Content = content;
notification.Title = title;
notification.Title = notification.MinimizedText = title;
notification.Type = type;
notificationManager.AddNotification(notification);
}
Expand Down

0 comments on commit d2be882

Please sign in to comment.