264 lines
11 KiB
C#
264 lines
11 KiB
C#
using System;
|
|
using Dalamud.Bindings.ImGui;
|
|
using Dalamud.Interface;
|
|
using Dalamud.Interface.Utility.Raii;
|
|
using MareSynchronos.API.Data.Extensions;
|
|
using MareSynchronos.MareConfiguration;
|
|
using MareSynchronos.UI.Handlers;
|
|
using MareSynchronos.WebAPI;
|
|
using MareSynchronos.Localization;
|
|
|
|
namespace MareSynchronos.UI.Components;
|
|
|
|
public class PairGroupsUi
|
|
{
|
|
private readonly ApiController _apiController;
|
|
private readonly MareConfigService _mareConfig;
|
|
private readonly SelectPairForGroupUi _selectGroupForPairUi;
|
|
private readonly TagHandler _tagHandler;
|
|
private readonly UidDisplayHandler _uidDisplayHandler;
|
|
private readonly UiSharedService _uiSharedService;
|
|
|
|
private string L(string key, string fallback, params object[] args)
|
|
{
|
|
var safeArgs = args ?? Array.Empty<object>();
|
|
return LocalizationService.Instance?.GetString(key, fallback, safeArgs)
|
|
?? string.Format(System.Globalization.CultureInfo.CurrentCulture, fallback, safeArgs);
|
|
}
|
|
|
|
public PairGroupsUi(MareConfigService mareConfig, TagHandler tagHandler, UidDisplayHandler uidDisplayHandler, ApiController apiController,
|
|
SelectPairForGroupUi selectGroupForPairUi, UiSharedService uiSharedService)
|
|
{
|
|
_mareConfig = mareConfig;
|
|
_tagHandler = tagHandler;
|
|
_uidDisplayHandler = uidDisplayHandler;
|
|
_apiController = apiController;
|
|
_selectGroupForPairUi = selectGroupForPairUi;
|
|
_uiSharedService = uiSharedService;
|
|
}
|
|
|
|
public void Draw<T>(List<T> visibleUsers, List<T> onlineUsers, List<T> offlineUsers) where T : DrawPairBase
|
|
{
|
|
// Only render those tags that actually have pairs in them, otherwise
|
|
// we can end up with a bunch of useless pair groups
|
|
var tagsWithPairsInThem = _tagHandler.GetAllTagsSorted();
|
|
var allUsers = onlineUsers.Concat(offlineUsers).ToList();
|
|
if (typeof(T) == typeof(DrawUserPair))
|
|
{
|
|
DrawUserPairs(tagsWithPairsInThem, allUsers.Cast<DrawUserPair>().ToList(), visibleUsers.Cast<DrawUserPair>(), onlineUsers.Cast<DrawUserPair>(), offlineUsers.Cast<DrawUserPair>());
|
|
}
|
|
}
|
|
|
|
private void DrawButtons(string tag, List<DrawUserPair> availablePairsInThisTag)
|
|
{
|
|
var allArePaused = availablePairsInThisTag.All(pair => pair.UserPair!.OwnPermissions.IsPaused());
|
|
var pauseButton = allArePaused ? FontAwesomeIcon.Play : FontAwesomeIcon.Pause;
|
|
var flyoutMenuX = _uiSharedService.GetIconButtonSize(FontAwesomeIcon.Bars).X;
|
|
var pauseButtonX = _uiSharedService.GetIconButtonSize(pauseButton).X;
|
|
var windowX = ImGui.GetWindowContentRegionMin().X;
|
|
var windowWidth = UiSharedService.GetWindowContentRegionWidth();
|
|
var spacingX = ImGui.GetStyle().ItemSpacing.X;
|
|
|
|
var buttonPauseOffset = windowX + windowWidth - flyoutMenuX - spacingX - pauseButtonX;
|
|
ImGui.SameLine(buttonPauseOffset);
|
|
if (_uiSharedService.IconButton(pauseButton))
|
|
{
|
|
if (allArePaused)
|
|
{
|
|
ResumeAllPairs(availablePairsInThisTag);
|
|
}
|
|
else
|
|
{
|
|
PauseRemainingPairs(availablePairsInThisTag);
|
|
}
|
|
}
|
|
if (allArePaused)
|
|
{
|
|
UiSharedService.AttachToolTip(L("PairGroups.ResumeAll", "Resume pairing with all pairs in {0}", tag));
|
|
}
|
|
else
|
|
{
|
|
UiSharedService.AttachToolTip(L("PairGroups.PauseAll", "Pause pairing with all pairs in {0}", tag));
|
|
}
|
|
|
|
var buttonDeleteOffset = windowX + windowWidth - flyoutMenuX;
|
|
ImGui.SameLine(buttonDeleteOffset);
|
|
if (_uiSharedService.IconButton(FontAwesomeIcon.Bars))
|
|
{
|
|
ImGui.OpenPopup(L("PairGroups.Menu.Title", "Group Flyout Menu"));
|
|
}
|
|
|
|
if (ImGui.BeginPopup(L("PairGroups.Menu.Title", "Group Flyout Menu")))
|
|
{
|
|
using (ImRaii.PushId($"buttons-{tag}")) DrawGroupMenu(tag);
|
|
ImGui.EndPopup();
|
|
}
|
|
}
|
|
|
|
private void DrawCategory(string tag, IEnumerable<DrawPairBase> onlineUsers, IEnumerable<DrawPairBase> allUsers, IEnumerable<DrawPairBase>? visibleUsers = null)
|
|
{
|
|
IEnumerable<DrawPairBase> usersInThisTag;
|
|
HashSet<string>? otherUidsTaggedWithTag = null;
|
|
bool isSpecialTag = false;
|
|
int visibleInThisTag = 0;
|
|
if (tag is TagHandler.CustomOfflineTag or TagHandler.CustomOnlineTag or TagHandler.CustomVisibleTag or TagHandler.CustomUnpairedTag)
|
|
{
|
|
usersInThisTag = onlineUsers;
|
|
isSpecialTag = true;
|
|
}
|
|
else
|
|
{
|
|
otherUidsTaggedWithTag = _tagHandler.GetOtherUidsForTag(tag);
|
|
usersInThisTag = onlineUsers
|
|
.Where(pair => otherUidsTaggedWithTag.Contains(pair.UID))
|
|
.ToList();
|
|
visibleInThisTag = visibleUsers?.Count(p => otherUidsTaggedWithTag.Contains(p.UID)) ?? 0;
|
|
}
|
|
|
|
if (isSpecialTag && !usersInThisTag.Any()) return;
|
|
|
|
DrawName(tag, isSpecialTag, visibleInThisTag, usersInThisTag.Count(), otherUidsTaggedWithTag?.Count);
|
|
if (!isSpecialTag)
|
|
{
|
|
using (ImRaii.PushId($"group-{tag}-buttons")) DrawButtons(tag, allUsers.Cast<DrawUserPair>().Where(p => otherUidsTaggedWithTag!.Contains(p.UID)).ToList());
|
|
}
|
|
else
|
|
{
|
|
if (!_tagHandler.IsTagOpen(tag))
|
|
{
|
|
var size = ImGui.CalcTextSize("").Y + ImGui.GetStyle().FramePadding.Y * 2f;
|
|
ImGui.SameLine();
|
|
ImGui.Dummy(new(size, size));
|
|
}
|
|
}
|
|
|
|
if (!_tagHandler.IsTagOpen(tag)) return;
|
|
|
|
ImGui.Indent(20);
|
|
DrawPairs(tag, usersInThisTag);
|
|
ImGui.Unindent(20);
|
|
}
|
|
|
|
private void DrawGroupMenu(string tag)
|
|
{
|
|
if (_uiSharedService.IconTextButton(FontAwesomeIcon.Users, L("PairGroups.Menu.AddPeople", "Add people to {0}", tag)))
|
|
{
|
|
_selectGroupForPairUi.Open(tag);
|
|
}
|
|
UiSharedService.AttachToolTip(L("PairGroups.Menu.AddPeople.Tooltip", "Add more users to Group {0}", tag));
|
|
|
|
if (_uiSharedService.IconTextButton(FontAwesomeIcon.Trash, L("PairGroups.Menu.Delete", "Delete {0}", tag)) && UiSharedService.CtrlPressed())
|
|
{
|
|
_tagHandler.RemoveTag(tag);
|
|
}
|
|
UiSharedService.AttachToolTip(L("PairGroups.Menu.Delete.Tooltip", "Delete Group {0} (Will not delete the pairs)\nHold CTRL to delete", tag));
|
|
}
|
|
|
|
private void DrawName(string tag, bool isSpecialTag, int visible, int online, int? total)
|
|
{
|
|
string displayedName = tag switch
|
|
{
|
|
TagHandler.CustomUnpairedTag => L("PairGroups.Tag.Unpaired", "Unpaired"),
|
|
TagHandler.CustomOfflineTag => L("PairGroups.Tag.Offline", "Offline"),
|
|
TagHandler.CustomOnlineTag => _mareConfig.Current.ShowOfflineUsersSeparately
|
|
? L("PairGroups.Tag.Online", "Online")
|
|
: L("PairGroups.Tag.Contacts", "Contacts"),
|
|
TagHandler.CustomVisibleTag => L("PairGroups.Tag.Visible", "Visible"),
|
|
_ => tag
|
|
};
|
|
|
|
string resultFolderName = !isSpecialTag
|
|
? L("PairGroups.Header.WithCounts", "{0} ({1}/{2}/{3} Pairs)", displayedName, visible, online, total ?? 0)
|
|
: L("PairGroups.Header.Special", "{0} ({1} Pairs)", displayedName, online);
|
|
var icon = _tagHandler.IsTagOpen(tag) ? FontAwesomeIcon.CaretSquareDown : FontAwesomeIcon.CaretSquareRight;
|
|
_uiSharedService.IconText(icon);
|
|
if (ImGui.IsItemClicked(ImGuiMouseButton.Left))
|
|
{
|
|
ToggleTagOpen(tag);
|
|
}
|
|
ImGui.SameLine();
|
|
ImGui.TextUnformatted(resultFolderName);
|
|
if (ImGui.IsItemClicked(ImGuiMouseButton.Left))
|
|
{
|
|
ToggleTagOpen(tag);
|
|
}
|
|
|
|
if (!isSpecialTag && ImGui.IsItemHovered())
|
|
{
|
|
ImGui.BeginTooltip();
|
|
ImGui.TextUnformatted(L("PairGroups.Tooltip.Title", "Group {0}", tag));
|
|
ImGui.Separator();
|
|
ImGui.TextUnformatted(L("PairGroups.Tooltip.Visible", "{0} Pairs visible", visible));
|
|
ImGui.TextUnformatted(L("PairGroups.Tooltip.Online", "{0} Pairs online/paused", online));
|
|
ImGui.TextUnformatted(L("PairGroups.Tooltip.Total", "{0} Pairs total", total ?? 0));
|
|
ImGui.EndTooltip();
|
|
}
|
|
}
|
|
|
|
private void DrawPairs(string tag, IEnumerable<DrawPairBase> availablePairsInThisCategory)
|
|
{
|
|
// These are all the OtherUIDs that are tagged with this tag
|
|
_uidDisplayHandler.RenderPairList(availablePairsInThisCategory);
|
|
ImGui.Separator();
|
|
}
|
|
|
|
private void DrawUserPairs(List<string> tagsWithPairsInThem, List<DrawUserPair> allUsers, IEnumerable<DrawUserPair> visibleUsers, IEnumerable<DrawUserPair> onlineUsers, IEnumerable<DrawUserPair> offlineUsers)
|
|
{
|
|
if (_mareConfig.Current.ShowVisibleUsersSeparately)
|
|
{
|
|
using (ImRaii.PushId("$group-VisibleCustomTag")) DrawCategory(TagHandler.CustomVisibleTag, visibleUsers, allUsers);
|
|
}
|
|
foreach (var tag in tagsWithPairsInThem)
|
|
{
|
|
if (_mareConfig.Current.ShowOfflineUsersSeparately)
|
|
{
|
|
using (ImRaii.PushId($"group-{tag}")) DrawCategory(tag, onlineUsers, allUsers, visibleUsers);
|
|
}
|
|
else
|
|
{
|
|
using (ImRaii.PushId($"group-{tag}")) DrawCategory(tag, allUsers, allUsers, visibleUsers);
|
|
}
|
|
}
|
|
if (_mareConfig.Current.ShowOfflineUsersSeparately)
|
|
{
|
|
using (ImRaii.PushId($"group-OnlineCustomTag")) DrawCategory(TagHandler.CustomOnlineTag,
|
|
onlineUsers.Where(u => !_tagHandler.HasAnyTag(u.UID)).ToList(), allUsers);
|
|
using (ImRaii.PushId($"group-OfflineCustomTag")) DrawCategory(TagHandler.CustomOfflineTag,
|
|
offlineUsers.Where(u => u.UserPair!.OtherPermissions.IsPaired()).ToList(), allUsers);
|
|
}
|
|
else
|
|
{
|
|
using (ImRaii.PushId($"group-OnlineCustomTag")) DrawCategory(TagHandler.CustomOnlineTag,
|
|
onlineUsers.Concat(offlineUsers.Where(u => u.UserPair!.OtherPermissions.IsPaired())).Where(u => !_tagHandler.HasAnyTag(u.UID)).ToList(), allUsers);
|
|
}
|
|
using (ImRaii.PushId($"group-UnpairedCustomTag")) DrawCategory(TagHandler.CustomUnpairedTag,
|
|
offlineUsers.Where(u => !u.UserPair!.OtherPermissions.IsPaired()).ToList(), allUsers);
|
|
}
|
|
|
|
private void PauseRemainingPairs(List<DrawUserPair> availablePairs)
|
|
{
|
|
foreach (var pairToPause in availablePairs.Where(pair => !pair.UserPair!.OwnPermissions.IsPaused()))
|
|
{
|
|
var perm = pairToPause.UserPair!.OwnPermissions;
|
|
perm.SetPaused(paused: true);
|
|
_ = _apiController.UserSetPairPermissions(new(new(pairToPause.UID), perm));
|
|
}
|
|
}
|
|
|
|
private void ResumeAllPairs(List<DrawUserPair> availablePairs)
|
|
{
|
|
foreach (var pairToPause in availablePairs)
|
|
{
|
|
var perm = pairToPause.UserPair!.OwnPermissions;
|
|
perm.SetPaused(paused: false);
|
|
_ = _apiController.UserSetPairPermissions(new(new(pairToPause.UID), perm));
|
|
}
|
|
}
|
|
|
|
private void ToggleTagOpen(string tag)
|
|
{
|
|
bool open = !_tagHandler.IsTagOpen(tag);
|
|
_tagHandler.SetTagOpen(tag, open);
|
|
}
|
|
}
|