Skip to content

Commit

Permalink
Unset generic pet entries
Browse files Browse the repository at this point in the history
Clear the filter for any given combo when entry selected or  successfully created
Clamp custom size to the currently selected pet, when a new pet is selected
  • Loading branch information
Kurochi51 committed Jul 24, 2024
1 parent 519b8a7 commit 6c2a090
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 41 deletions.
5 changes: 3 additions & 2 deletions PetScale/Helpers/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public unsafe void CheckPetRemoval(IDictionary<ulong, PetStruct> removalQueue, I
{
continue;
}
if (removedPlayer.Key != character->ContentId)
if (removedPlayer.Key != character->ContentId && !removedPlayer.Value.Generic)
{
continue;
}
Expand Down Expand Up @@ -448,7 +448,8 @@ internal static void SortList(ref List<PetStruct> petList, bool customSize)
tempList.AddRange([.. petList
.Except(tempList)
.OrderBy(item => SortedModels.FindIndex(sItem => sItem == item.PetID))
.ThenBy(item => item.CharacterName, StringComparer.Ordinal),]);
.ThenBy(item => item.CharacterName, StringComparer.Ordinal),
]);
if (tempList.Count == petList.Count && petList.ToHashSet().SetEquals(tempList))
{
petList = tempList;
Expand Down
6 changes: 3 additions & 3 deletions PetScale/PetScale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,11 @@ private unsafe void ParseDictionary(uint playerEntityId)
petModelDic[pet->NameString] = current;
}
#endif
if(queueFairyForRemoval)
if (queueFairyForRemoval)
{
CheckFairies(pet);
}

if (config.FairyState is not PetState.Off && (PetModel)pet->ModelCharaId is PetModel.Eos or PetModel.Selene)
{
switch (config.FairyState)
Expand Down Expand Up @@ -568,7 +568,7 @@ private unsafe void CheckFairies(BattleChara* pet)
{
continue;
}
if(Utilities.ResetFairy(fairy.Value, 1.5f))
if (Utilities.ResetFairy(fairy.Value, 1.5f))
{
removedFairies.Add(fairy.Value);
}
Expand Down
103 changes: 67 additions & 36 deletions PetScale/Windows/ConfigWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,14 @@ private void PresetTab()
currentTab = Tab.Summoner;
ImGui.TextUnformatted("Amount of players: " + GetPlayerCount(players.Count, plugin.clientState.IsLoggedIn).ToString(CultureInfo.InvariantCulture));
var buttonPressed = false;
DrawComboBox("Characters", charaName, charaWidth, out charaName, players.Select(player => player.Name).ToList(), filter: true, newEntryPossible: true);
if (DrawComboBox("Characters", charaName, charaWidth, out charaName, players.Select(player => player.Name).ToList(), filter: true, newEntryPossible: true))
{
var label = "##ComboCharacters" + "_" + currentTab.ToString();
if (comboFilter.ContainsKey(label))
{
comboFilter[label] = string.Empty;
}
}
ImGui.SameLine();
DrawComboBox("Pets", petSelection, petWidth, out petSelection, presetPetMap.Keys, filter: false);
ImGui.SameLine();
Expand Down Expand Up @@ -187,37 +194,19 @@ private void CustomTab()
currentTab = Tab.Others;
ImGui.TextUnformatted("Amount of players: " + GetPlayerCount(players.Count, plugin.clientState.IsLoggedIn).ToString(CultureInfo.InvariantCulture));
var buttonPressed = false;
DrawComboBox("Characters", charaName, charaWidth, out charaName, players.Select(player => player.Name).ToList(), filter: true, newEntryPossible: true);
ImGui.SameLine();
DrawComboBox("Pets", otherPetSelection, petWidth, out otherPetSelection, customPetMap.Keys, filter: false);
ImGui.SameLine();
ImGui.SetNextItemWidth(sizesWidth);
if (!otherPetSelection.Equals(DefaultPetSelection, StringComparison.Ordinal)
&& tempPetSize < Utilities.GetDefaultScale(customPetMap[otherPetSelection], PetSize.Custom))
{
tempPetSize = Utilities.GetDefaultScale(customPetMap[otherPetSelection], PetSize.Custom);
}
if (!otherPetSelection.Equals(DefaultPetSelection, StringComparison.Ordinal))
{
if (tempPetSize > Utilities.GetDefaultScale(customPetMap[otherPetSelection], PetSize.Custom) * 4f)
{
tempPetSize = Math.Max(Utilities.GetDefaultScale(customPetMap[otherPetSelection], PetSize.Custom) * 4f, 4f);
}
ImGui.DragFloat("##TempPetSize", ref tempPetSize, 0.01f, Utilities.GetDefaultScale(customPetMap[otherPetSelection], PetSize.Custom), Math.Max(Utilities.GetDefaultScale(customPetMap[otherPetSelection], PetSize.Custom) * 4f, 4f), "%.3g", ImGuiSliderFlags.AlwaysClamp);
if (ImGui.IsItemHovered())
{
ImGui.SetTooltip("Pet Size");
}
}
else
if (DrawComboBox("Characters", charaName, charaWidth, out charaName, players.Select(player => player.Name).ToList(), filter: true, newEntryPossible: true))
{
ImGui.DragFloat("##TempPetSize", ref tempPetSize, 0.01f, 1f, 4f, "%.3g", ImGuiSliderFlags.AlwaysClamp);
if (ImGui.IsItemHovered())
var label = "##ComboCharacters" + "_" + currentTab.ToString();
if (comboFilter.ContainsKey(label))
{
ImGui.SetTooltip("Pet Size");
comboFilter[label] = string.Empty;
}
}
ImGui.SameLine();
DrawComboBox("Pets", otherPetSelection, petWidth, out otherPetSelection, customPetMap.Keys, filter: false);
ImGui.SameLine();
CustomSizeInput(sizesWidth, ref tempPetSize);
ImGui.SameLine();
if (ImGuiUtils.IconButton(iconFont, addButtonIcon, "AddButton", 1))
{
buttonPressed = true;
Expand All @@ -237,6 +226,11 @@ private void CustomTab()
}
if (!error)
{
var label = "##ComboCharacters" + "_" + currentTab.ToString();
if (comboFilter.ContainsKey(label))
{
comboFilter[label] = string.Empty;
}
CheckOtherPossibleEntry();
}
}
Expand Down Expand Up @@ -349,11 +343,12 @@ private void DisplayTableRow(PetStruct item, bool customSize, string buttonId, r
}

// I don't like the way filters are managed, but I can't think of a better way
private void DrawComboBox<T>(string label, string current, float width, out string result,
private bool DrawComboBox<T>(string label, string current, float width, out string result,
IReadOnlyCollection<T> list, bool filter, bool newEntryPossible = false) where T : notnull
{
var itemSelected = false;
ImGui.SetNextItemWidth(width);
var comboLabel = "##Combo" + label;
var comboLabel = "##Combo" + label + "_" + currentTab.ToString();
using var combo = ImRaii.Combo(comboLabel, current);
result = current;
if (!combo)
Expand All @@ -362,7 +357,7 @@ private void DrawComboBox<T>(string label, string current, float width, out stri
{
comboFilter.Remove(comboLabel);
}
return;
return itemSelected;
}
var tempList = list.Select(item => item.ToString()!).ToList();
if (tempList.Count > 1 && filter)
Expand All @@ -371,22 +366,21 @@ private void DrawComboBox<T>(string label, string current, float width, out stri
}
if (filter)
{
comboLabel += "_" + currentTab.ToString();
comboFilter.TryAdd(comboLabel, string.Empty);
ImGui.SetNextItemWidth(width);
var filterStr = comboFilter[comboLabel];
if (ImGui.InputTextWithHint("##Filter" + label, newEntryPossible ? "New Entry or Filter.." : "Filter..", ref filterStr, 21, ImGuiInputTextFlags.EnterReturnsTrue))
{
comboFilter[comboLabel] = filterStr;
comboFilter[comboLabel] = filterStr.Trim();
log.Debug("filter matches {a}", tempList.Count(item => item.Contains(comboFilter[comboLabel], StringComparison.OrdinalIgnoreCase)));
if (newEntryPossible && tempList.Count(item => item.Contains(comboFilter[comboLabel], StringComparison.OrdinalIgnoreCase)) is 0)
{
showModal = true;
filterStr = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(comboFilter[comboLabel].ToLower(CultureInfo.CurrentCulture));
filterStr = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(comboFilter[comboLabel].ToLower(CultureInfo.CurrentCulture)).Trim();
ImGui.OpenPopup(WorldSelectModal);
}
}
comboFilter[comboLabel] = filterStr;
comboFilter[comboLabel] = filterStr.Trim();
if (newEntryPossible && ImGui.IsItemHovered())
{
ImGui.SetTooltip("New entry can be made, by pressing enter, if your search doesn't return any returns in the list");
Expand All @@ -407,11 +401,16 @@ private void DrawComboBox<T>(string label, string current, float width, out stri
var height = ImGui.GetTextLineHeightWithSpacing() * Math.Min(itemCount + 1.5f, 8);
height += itemCount > 0 ? -ImGui.GetFrameHeight() - ImGui.GetStyle().WindowPadding.Y - ImGui.GetStyle().FramePadding.Y : 0;
using var listChild = ImRaii.Child("###child" + label, new Vector2(width, height));
DrawClippedList(itemCount, current, tempList, out result);
if (DrawClippedList(itemCount, current, tempList, out result))
{
itemSelected = true;
}
return itemSelected;
}

private static unsafe void DrawClippedList(int itemCount, string preview, IReadOnlyList<string> list, out string result)
private static unsafe bool DrawClippedList(int itemCount, string preview, IReadOnlyList<string> list, out string result)
{
var itemSelected = false;
result = preview;
var clipper = new ImGuiListClipperPtr(ImGuiNative.ImGuiListClipper_ImGuiListClipper());
clipper.Begin(itemCount, ImGui.GetTextLineHeightWithSpacing());
Expand All @@ -436,11 +435,13 @@ private static unsafe void DrawClippedList(int itemCount, string preview, IReadO
continue;
}
result = item;
itemSelected = true;
ImGui.CloseCurrentPopup();
}
}
clipper.End();
clipper.Destroy();
return itemSelected;
}

private void CheckOtherPossibleEntry(string? altName = null)
Expand Down Expand Up @@ -806,6 +807,36 @@ private bool PopupModal(string label, Vector2 size, string newCharacter)
return false;
}

private void CustomSizeInput(float size, ref float petSize)
{
ImGui.SetNextItemWidth(size);
if (!otherPetSelection.Equals(DefaultPetSelection, StringComparison.Ordinal)
&& petSize < Utilities.GetDefaultScale(customPetMap[otherPetSelection], PetSize.Custom))
{
petSize = Utilities.GetDefaultScale(customPetMap[otherPetSelection], PetSize.Custom);
}
if (!otherPetSelection.Equals(DefaultPetSelection, StringComparison.Ordinal))
{
if (petSize > Utilities.GetDefaultScale(customPetMap[otherPetSelection], PetSize.Custom) * 4f)
{
petSize = Math.Max(Utilities.GetDefaultScale(customPetMap[otherPetSelection], PetSize.Custom) * 4f, 4f);
}
ImGui.DragFloat("##TempPetSize", ref petSize, 0.01f, Utilities.GetDefaultScale(customPetMap[otherPetSelection], PetSize.Custom), Math.Max(Utilities.GetDefaultScale(customPetMap[otherPetSelection], PetSize.Custom) * 4f, 4f), "%.3g", ImGuiSliderFlags.AlwaysClamp);
if (ImGui.IsItemHovered())
{
ImGui.SetTooltip("Pet Size");
}
}
else
{
ImGui.DragFloat("##TempPetSize", ref petSize, 0.01f, 1f, 4f, "%.3g", ImGuiSliderFlags.AlwaysClamp);
if (ImGui.IsItemHovered())
{
ImGui.SetTooltip("Pet Size");
}
}
}

public void Dispose()
{
pluginInterface.UiBuilder.DefaultFontHandle.ImFontChanged -= QueueColumnWidthChange;
Expand Down

0 comments on commit 6c2a090

Please sign in to comment.