code restructure
This commit is contained in:
242
Multiselect/DrawMultiSelect.cs
Normal file
242
Multiselect/DrawMultiSelect.cs
Normal file
@@ -0,0 +1,242 @@
|
||||
using Comfort.Common;
|
||||
using EFT.UI;
|
||||
using EFT.UI.DragAndDrop;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace UIFixes
|
||||
{
|
||||
public class DrawMultiSelect : MonoBehaviour
|
||||
{
|
||||
private Texture2D selectTexture;
|
||||
|
||||
private Vector3 selectOrigin;
|
||||
private Vector3 selectEnd;
|
||||
|
||||
private GraphicRaycaster localRaycaster;
|
||||
private GraphicRaycaster preloaderRaycaster;
|
||||
|
||||
private bool drawing;
|
||||
private bool secondary;
|
||||
|
||||
public void Start()
|
||||
{
|
||||
selectTexture = new Texture2D(1, 1);
|
||||
selectTexture.SetPixel(0, 0, new Color(1f, 1f, 1f, 0.6f));
|
||||
selectTexture.Apply();
|
||||
|
||||
localRaycaster = GetComponentInParent<GraphicRaycaster>();
|
||||
if (localRaycaster == null)
|
||||
{
|
||||
throw new InvalidOperationException("DrawMultiSelect couldn't find GraphicRayCaster in parents");
|
||||
}
|
||||
|
||||
preloaderRaycaster = Singleton<PreloaderUI>.Instance.transform.GetChild(0).GetComponent<GraphicRaycaster>();
|
||||
if (preloaderRaycaster == null)
|
||||
{
|
||||
throw new InvalidOperationException("DrawMultiSelect couldn't find the PreloaderUI GraphicRayCaster");
|
||||
}
|
||||
}
|
||||
|
||||
public void OnDisable()
|
||||
{
|
||||
MultiSelect.Clear();
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (!MultiSelect.Enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// checking ItemUiContext is a quick and easy way to know the mouse is over an item
|
||||
if (Input.GetKeyDown(Settings.SelectionBoxKey.Value.MainKey) && ItemUiContext.Instance.R().ItemContext == null)
|
||||
{
|
||||
PointerEventData eventData = new(EventSystem.current)
|
||||
{
|
||||
position = Input.mousePosition
|
||||
};
|
||||
|
||||
List<RaycastResult> results = [];
|
||||
localRaycaster.Raycast(eventData, results);
|
||||
preloaderRaycaster.Raycast(eventData, results);
|
||||
|
||||
foreach (GameObject gameObject in results.Select(r => r.gameObject))
|
||||
{
|
||||
var draggables = gameObject.GetComponents<MonoBehaviour>()
|
||||
.Where(c => c is IDragHandler || c is IBeginDragHandler)
|
||||
.Where(c => c is not ScrollRectNoDrag) // this disables scrolling, it doesn't add it
|
||||
.Where(c => c.name != "Inner"); // there's a random DragTrigger sitting in ItemInfoWindows
|
||||
|
||||
var clickables = gameObject.GetComponents<MonoBehaviour>()
|
||||
.Where(c => c is IPointerClickHandler || c is IPointerDownHandler || c is IPointerUpHandler);
|
||||
|
||||
if (draggables.Any() || clickables.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
selectOrigin = Input.mousePosition;
|
||||
drawing = true;
|
||||
secondary = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
|
||||
}
|
||||
|
||||
if (drawing)
|
||||
{
|
||||
selectEnd = Input.mousePosition;
|
||||
|
||||
Rect selectRect = new(selectOrigin, selectEnd - selectOrigin);
|
||||
|
||||
// If not secondary, then we can kick out any non-rendered items, plus they won't be covered by the foreach below
|
||||
if (!secondary)
|
||||
{
|
||||
MultiSelect.Prune();
|
||||
}
|
||||
|
||||
foreach (GridItemView gridItemView in transform.root.GetComponentsInChildren<GridItemView>().Concat(Singleton<PreloaderUI>.Instance.GetComponentsInChildren<GridItemView>()))
|
||||
{
|
||||
RectTransform itemTransform = gridItemView.GetComponent<RectTransform>();
|
||||
Rect itemRect = new((Vector2)itemTransform.position + itemTransform.rect.position * itemTransform.lossyScale, itemTransform.rect.size * itemTransform.lossyScale);
|
||||
|
||||
if (selectRect.Overlaps(itemRect, true))
|
||||
{
|
||||
// Don't re-raycast already selected items - if there were visible before they still are
|
||||
if (MultiSelect.IsSelected(gridItemView, secondary))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Otherwise, ensure it's not overlapped by window UI
|
||||
PointerEventData eventData = new(EventSystem.current);
|
||||
|
||||
if (IsOnTop(itemRect, itemTransform, preloaderRaycaster)) // no preloaderUI on top of this?
|
||||
{
|
||||
if (itemTransform.IsDescendantOf(Singleton<PreloaderUI>.Instance.transform))
|
||||
{
|
||||
MultiSelect.Select(gridItemView, secondary);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (IsOnTop(itemRect, itemTransform, localRaycaster)) // no local UI on top of this?
|
||||
{
|
||||
MultiSelect.Select(gridItemView, secondary);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MultiSelect.Deselect(gridItemView, secondary);
|
||||
}
|
||||
}
|
||||
|
||||
if (drawing && !Input.GetKey(Settings.SelectionBoxKey.Value.MainKey))
|
||||
{
|
||||
drawing = false;
|
||||
if (secondary)
|
||||
{
|
||||
MultiSelect.CombineSecondary();
|
||||
secondary = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnGUI()
|
||||
{
|
||||
if (drawing)
|
||||
{
|
||||
// Invert Y because GUI has upper-left origin
|
||||
Rect area = new(selectOrigin.x, Screen.height - selectOrigin.y, selectEnd.x - selectOrigin.x, selectOrigin.y - selectEnd.y);
|
||||
|
||||
Rect lineArea = area;
|
||||
lineArea.height = 1; // Top
|
||||
GUI.DrawTexture(lineArea, selectTexture);
|
||||
|
||||
lineArea.y = area.yMax - 1; // Bottom
|
||||
GUI.DrawTexture(lineArea, selectTexture);
|
||||
|
||||
lineArea = area;
|
||||
lineArea.width = 1; // Left
|
||||
GUI.DrawTexture(lineArea, selectTexture);
|
||||
|
||||
lineArea.x = area.xMax - 1; // Right
|
||||
GUI.DrawTexture(lineArea, selectTexture);
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsOnTop(Rect itemRect, Transform itemTransform, GraphicRaycaster raycaster)
|
||||
{
|
||||
// Otherwise, ensure it's not overlapped by window UI
|
||||
PointerEventData eventData = new(EventSystem.current);
|
||||
|
||||
float widthMargin = 0.1f * (itemRect.xMax - itemRect.xMin);
|
||||
float heightMargin = 0.1f * (itemRect.yMax - itemRect.yMin);
|
||||
|
||||
List<RaycastResult> raycastResults = [];
|
||||
|
||||
// Lower left
|
||||
eventData.position = new Vector2(itemRect.xMin + widthMargin, itemRect.yMin + heightMargin);
|
||||
raycaster.Raycast(eventData, raycastResults);
|
||||
if (raycastResults.Any() && !raycastResults[0].gameObject.transform.IsDescendantOf(itemTransform))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Upper left
|
||||
raycastResults.Clear();
|
||||
eventData.position = new Vector2(itemRect.xMin + widthMargin, itemRect.yMax - heightMargin);
|
||||
raycaster.Raycast(eventData, raycastResults);
|
||||
if (raycastResults.Any() && !raycastResults[0].gameObject.transform.IsDescendantOf(itemTransform))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Upper right
|
||||
raycastResults.Clear();
|
||||
eventData.position = new Vector2(itemRect.xMax - widthMargin, itemRect.yMax - heightMargin);
|
||||
raycaster.Raycast(eventData, raycastResults);
|
||||
if (raycastResults.Any() && !raycastResults[0].gameObject.transform.IsDescendantOf(itemTransform))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Lower right
|
||||
raycastResults.Clear();
|
||||
eventData.position = new Vector2(itemRect.xMax - widthMargin, itemRect.yMin + heightMargin);
|
||||
raycaster.Raycast(eventData, raycastResults);
|
||||
if (raycastResults.Any() && !raycastResults[0].gameObject.transform.IsDescendantOf(itemTransform))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static class TransformExtensions
|
||||
{
|
||||
public static bool IsDescendantOf(this Transform transform, Transform target)
|
||||
{
|
||||
if (transform == target)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
while (transform.parent != null)
|
||||
{
|
||||
transform = transform.parent;
|
||||
if (transform == target)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
144
Multiselect/MultiGrid.cs
Normal file
144
Multiselect/MultiGrid.cs
Normal file
@@ -0,0 +1,144 @@
|
||||
using EFT.InventoryLogic;
|
||||
using EFT.UI.DragAndDrop;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
using GridItemAddress = GClass2769;
|
||||
|
||||
namespace UIFixes
|
||||
{
|
||||
public static class MultiGrid
|
||||
{
|
||||
private static readonly Dictionary<Item, Dictionary<StashGridClass, Vector2Int>> GridOffsets = [];
|
||||
private static readonly Dictionary<Item, Dictionary<int, Dictionary<int, StashGridClass>>> GridsByLocation = [];
|
||||
|
||||
public static LocationInGrid GetGridLocation(GridItemAddress realAddress)
|
||||
{
|
||||
if (!IsMultiGrid(realAddress))
|
||||
{
|
||||
return realAddress.LocationInGrid;
|
||||
}
|
||||
|
||||
Vector2Int gridOffset = GridOffsets[realAddress.Container.ParentItem][realAddress.Grid];
|
||||
return new LocationInGrid(realAddress.LocationInGrid.x + gridOffset.x, realAddress.LocationInGrid.y + gridOffset.y, realAddress.LocationInGrid.r);
|
||||
}
|
||||
|
||||
public static GridItemAddress GetRealAddress(StashGridClass originGrid, LocationInGrid multigridLocation)
|
||||
{
|
||||
if (!IsMultiGrid(originGrid.ParentItem))
|
||||
{
|
||||
// Clamp to the actual grid
|
||||
multigridLocation.x = Math.Max(0, Math.Min(originGrid.GridWidth.Value, multigridLocation.x));
|
||||
multigridLocation.y = Math.Max(0, Math.Min(originGrid.GridHeight.Value, multigridLocation.y));
|
||||
|
||||
return new GridItemAddress(originGrid, multigridLocation);
|
||||
}
|
||||
|
||||
var gridsByLocation = GridsByLocation[originGrid.ParentItem];
|
||||
|
||||
// Clamp to known "meta" grid
|
||||
int x = Math.Max(0, Math.Min(gridsByLocation.Keys.Max(), multigridLocation.x));
|
||||
int y = Math.Max(0, Math.Min(gridsByLocation[x].Keys.Max(), multigridLocation.y));
|
||||
|
||||
// Sanity check
|
||||
if (!gridsByLocation.ContainsKey(x) || !gridsByLocation[x].ContainsKey(y))
|
||||
{
|
||||
// Perhaps some weird layout with gaps in the middle? Fall back to a known good
|
||||
x = gridsByLocation.Keys.First();
|
||||
y = gridsByLocation[x].Keys.First();
|
||||
}
|
||||
|
||||
StashGridClass grid = gridsByLocation[x][y];
|
||||
Vector2Int offsets = GridOffsets[originGrid.ParentItem][grid];
|
||||
|
||||
LocationInGrid location = new(x - offsets.x, y - offsets.y, multigridLocation.r);
|
||||
return new GridItemAddress(grid, location);
|
||||
}
|
||||
|
||||
public static void Cache(GridView initialGridView)
|
||||
{
|
||||
if (initialGridView == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Item parent = initialGridView.Grid.ParentItem;
|
||||
if (GridOffsets.ContainsKey(parent) || !IsMultiGrid(parent))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Dictionary<StashGridClass, Vector2Int> gridOffsets = [];
|
||||
Dictionary<int, Dictionary<int, StashGridClass>> gridsByLocation = [];
|
||||
|
||||
// Sometimes the parent's pivot is 0,1; sometimes it's 0,0. Thanks BSG
|
||||
RectTransform parentView = initialGridView.transform.parent.RectTransform();
|
||||
Vector2 parentPosition = parentView.pivot.y == 1 ? parentView.position : new Vector2(parentView.position.x, parentView.position.y + parentView.sizeDelta.y);
|
||||
|
||||
Vector2 gridSize = new(64f * parentView.lossyScale.x, 64f * parentView.lossyScale.y);
|
||||
|
||||
foreach (GridView gridView in parentView.GetComponentsInChildren<GridView>())
|
||||
{
|
||||
// Get absolute offsets
|
||||
float xOffset = gridView.transform.position.x - parentPosition.x;
|
||||
float yOffset = -(gridView.transform.position.y - parentPosition.y); // invert y since grid coords are upper-left origin
|
||||
|
||||
int x = (int)Math.Round(xOffset / gridSize.x, MidpointRounding.AwayFromZero);
|
||||
int y = (int)Math.Round(yOffset / gridSize.y, MidpointRounding.AwayFromZero);
|
||||
|
||||
gridOffsets.Add(gridView.Grid, new Vector2Int(x, y));
|
||||
|
||||
// Populate reverse lookup
|
||||
for (int i = 0; i < gridView.Grid.GridWidth.Value; i++)
|
||||
{
|
||||
if (!gridsByLocation.ContainsKey(x + i))
|
||||
{
|
||||
gridsByLocation.Add(x + i, []);
|
||||
}
|
||||
|
||||
var rowGrids = gridsByLocation[x + i];
|
||||
for (int j = 0; j < gridView.Grid.GridHeight.Value; j++)
|
||||
{
|
||||
rowGrids.Add(y + j, gridView.Grid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GridOffsets.Add(parent, gridOffsets);
|
||||
GridsByLocation.Add(parent, gridsByLocation);
|
||||
|
||||
// Best effort attempt at cleanup
|
||||
IItemOwner owner = parent.Owner;
|
||||
if (owner != null)
|
||||
{
|
||||
void onRemoveItem(GEventArgs3 eventArgs)
|
||||
{
|
||||
if (GridOffsets.ContainsKey(eventArgs.Item))
|
||||
{
|
||||
GridOffsets.Remove(eventArgs.Item);
|
||||
GridsByLocation.Remove(eventArgs.Item);
|
||||
owner.RemoveItemEvent -= onRemoveItem;
|
||||
}
|
||||
};
|
||||
owner.RemoveItemEvent += onRemoveItem;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsMultiGrid(GridItemAddress itemAddress)
|
||||
{
|
||||
return IsMultiGrid(itemAddress.Container.ParentItem);
|
||||
}
|
||||
|
||||
private static bool IsMultiGrid(Item item)
|
||||
{
|
||||
if (item is not LootItemClass lootItem)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return lootItem.Grids.Length > 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
454
Multiselect/MultiSelect.cs
Normal file
454
Multiselect/MultiSelect.cs
Normal file
@@ -0,0 +1,454 @@
|
||||
using EFT.InventoryLogic;
|
||||
using EFT.UI;
|
||||
using EFT.UI.DragAndDrop;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
using GridItemAddress = GClass2769;
|
||||
|
||||
namespace UIFixes
|
||||
{
|
||||
public class MultiSelect
|
||||
{
|
||||
private static GameObject SelectedMarkTemplate = null;
|
||||
private static GameObject SelectedBackgroundTemplate = null;
|
||||
|
||||
private static readonly Dictionary<ItemContextClass, GridItemView> SelectedItems = [];
|
||||
private static readonly Dictionary<ItemContextClass, GridItemView> SecondaryItems = [];
|
||||
|
||||
private static ItemContextTaskSerializer UnloadSerializer = null;
|
||||
|
||||
public static bool Enabled
|
||||
{
|
||||
get
|
||||
{
|
||||
return Settings.EnableMultiSelect.Value && (!Plugin.InRaid() || Settings.EnableMultiSelectInRaid.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Initialize()
|
||||
{
|
||||
// Grab the selection objects from ragfair as templates
|
||||
RagfairNewOfferItemView ragfairNewOfferItemView = ItemViewFactory.CreateFromPool<RagfairNewOfferItemView>("ragfair_layout");
|
||||
|
||||
if (SelectedMarkTemplate == null)
|
||||
{
|
||||
SelectedMarkTemplate = UnityEngine.Object.Instantiate(ragfairNewOfferItemView.R().SelectedMark, null, false);
|
||||
UnityEngine.Object.DontDestroyOnLoad(SelectedMarkTemplate);
|
||||
}
|
||||
|
||||
if (SelectedBackgroundTemplate == null)
|
||||
{
|
||||
SelectedBackgroundTemplate = UnityEngine.Object.Instantiate(ragfairNewOfferItemView.R().SelectedBackground, null, false);
|
||||
UnityEngine.Object.DontDestroyOnLoad(SelectedBackgroundTemplate);
|
||||
}
|
||||
|
||||
ragfairNewOfferItemView.ReturnToPool();
|
||||
}
|
||||
|
||||
public static void Toggle(GridItemView itemView, bool secondary = false)
|
||||
{
|
||||
var dictionary = secondary ? SecondaryItems : SelectedItems;
|
||||
ItemContextClass itemContext = dictionary.FirstOrDefault(x => x.Value == itemView).Key;
|
||||
if (itemContext != null)
|
||||
{
|
||||
Deselect(itemContext, secondary);
|
||||
}
|
||||
else
|
||||
{
|
||||
Select(itemView, secondary);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Clear()
|
||||
{
|
||||
// ToList() because modifying the collection
|
||||
foreach (ItemContextClass itemContext in SelectedItems.Keys.ToList())
|
||||
{
|
||||
Deselect(itemContext);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Select(GridItemView itemView, bool secondary = false)
|
||||
{
|
||||
var dictionary = secondary ? SecondaryItems : SelectedItems;
|
||||
|
||||
if (itemView.IsSelectable() && !SelectedItems.Any(x => x.Key.Item == itemView.Item) && !SecondaryItems.Any(x => x.Key.Item == itemView.Item))
|
||||
{
|
||||
ItemContextClass itemContext = new MultiSelectItemContext(itemView.ItemContext, itemView.ItemRotation);
|
||||
|
||||
// Subscribe to window closures to deselect
|
||||
var windowContext = itemView.GetComponentInParent<GridWindow>()?.WindowContext ?? itemView.GetComponentInParent<InfoWindow>()?.WindowContext;
|
||||
if (windowContext != null)
|
||||
{
|
||||
windowContext.OnClose += () => Deselect(itemContext);
|
||||
}
|
||||
|
||||
// Cache the gridview in case we need it
|
||||
MultiGrid.Cache(itemView.Container as GridView);
|
||||
|
||||
dictionary.Add(itemContext, itemView);
|
||||
ShowSelection(itemView);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Deselect(ItemContextClass itemContext, bool secondary = false)
|
||||
{
|
||||
var dictionary = secondary ? SecondaryItems : SelectedItems;
|
||||
|
||||
if (dictionary.TryGetValue(itemContext, out GridItemView itemView))
|
||||
{
|
||||
HideSelection(itemView);
|
||||
}
|
||||
|
||||
dictionary.Remove(itemContext);
|
||||
itemContext.Dispose();
|
||||
}
|
||||
|
||||
public static void Deselect(GridItemView itemView, bool secondary = false)
|
||||
{
|
||||
var dictionary = secondary ? SecondaryItems : SelectedItems;
|
||||
|
||||
ItemContextClass itemContext = dictionary.FirstOrDefault(x => x.Value == itemView).Key;
|
||||
if (itemContext != null)
|
||||
{
|
||||
dictionary.Remove(itemContext);
|
||||
itemContext.Dispose();
|
||||
HideSelection(itemView);
|
||||
}
|
||||
}
|
||||
|
||||
public static void OnKillItemView(GridItemView itemView)
|
||||
{
|
||||
ItemContextClass itemContext = SelectedItems.FirstOrDefault(x => x.Value == itemView).Key;
|
||||
if (itemContext != null)
|
||||
{
|
||||
SelectedItems[itemContext] = null;
|
||||
HideSelection(itemView);
|
||||
}
|
||||
}
|
||||
|
||||
public static void OnNewItemView(GridItemView itemView)
|
||||
{
|
||||
ItemContextClass itemContext = SelectedItems.FirstOrDefault(x => x.Key.Item == itemView.Item).Key;
|
||||
if (itemContext != null)
|
||||
{
|
||||
// We need to refresh the context because if the item moved, it has a new address
|
||||
Deselect(itemContext);
|
||||
Select(itemView);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsSelected(GridItemView itemView, bool secondary = false)
|
||||
{
|
||||
var dictionary = secondary ? SecondaryItems : SelectedItems;
|
||||
return dictionary.Any(x => x.Key.Item == itemView.Item);
|
||||
}
|
||||
|
||||
public static void Prune()
|
||||
{
|
||||
foreach (var entry in SelectedItems.ToList())
|
||||
{
|
||||
if (entry.Value == null)
|
||||
{
|
||||
Deselect(entry.Key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void CombineSecondary()
|
||||
{
|
||||
foreach (var entry in SecondaryItems)
|
||||
{
|
||||
SelectedItems.Add(entry.Key, entry.Value);
|
||||
}
|
||||
|
||||
SecondaryItems.Clear();
|
||||
}
|
||||
|
||||
public static IEnumerable<ItemContextClass> ItemContexts
|
||||
{
|
||||
get { return SelectedItems.Keys; }
|
||||
}
|
||||
|
||||
public static IEnumerable<ItemContextClass> SecondaryContexts
|
||||
{
|
||||
get { return SecondaryItems.Keys; }
|
||||
}
|
||||
|
||||
public static int Count
|
||||
{
|
||||
get { return SelectedItems.Count; }
|
||||
}
|
||||
|
||||
public static int SecondaryCount
|
||||
{
|
||||
get { return SecondaryItems.Count; }
|
||||
}
|
||||
|
||||
public static bool Active
|
||||
{
|
||||
get { return SelectedItems.Count > 0; }
|
||||
}
|
||||
|
||||
// Sort the items to prioritize the items that share a grid with the dragged item, prepend the dragContext as the first one
|
||||
// Can pass no itemContext, and it just sorts items by their grid order
|
||||
public static IEnumerable<ItemContextClass> SortedItemContexts(ItemContextClass first = null, bool prepend = true)
|
||||
{
|
||||
static int gridOrder(LocationInGrid loc) => 100 * loc.y + loc.x;
|
||||
|
||||
var result = ItemContexts
|
||||
.Where(ic => first == null || ic.Item != first.Item)
|
||||
.OrderByDescending(ic => ic.ItemAddress is GridItemAddress)
|
||||
.ThenByDescending(ic => first != null && first.ItemAddress.Container.ParentItem == ic.ItemAddress.Container.ParentItem)
|
||||
.ThenBy(ic => ic.ItemAddress is GridItemAddress selectedGridAddress ? gridOrder(MultiGrid.GetGridLocation(selectedGridAddress)) : 0);
|
||||
|
||||
return first != null && prepend ? result.Prepend(first) : result;
|
||||
}
|
||||
|
||||
public static void ShowDragCount(DraggedItemView draggedItemView)
|
||||
{
|
||||
if (draggedItemView != null && Count > 1)
|
||||
{
|
||||
GameObject textOverlay = new("MultiSelectText", [typeof(RectTransform), typeof(TextMeshProUGUI)]);
|
||||
textOverlay.transform.parent = draggedItemView.transform;
|
||||
textOverlay.transform.SetAsLastSibling();
|
||||
textOverlay.SetActive(true);
|
||||
|
||||
RectTransform overlayRect = textOverlay.GetComponent<RectTransform>();
|
||||
overlayRect.anchorMin = Vector2.zero;
|
||||
overlayRect.anchorMax = Vector2.one;
|
||||
overlayRect.anchoredPosition = new Vector2(0.5f, 0.5f);
|
||||
|
||||
TextMeshProUGUI text = textOverlay.GetComponent<TextMeshProUGUI>();
|
||||
text.text = MultiSelect.Count.ToString();
|
||||
text.fontSize = 36;
|
||||
text.alignment = TextAlignmentOptions.Baseline;
|
||||
}
|
||||
}
|
||||
|
||||
public static int InteractionCount(EItemInfoButton interaction, ItemUiContext itemUiContext)
|
||||
{
|
||||
return ItemContexts.Count(ic => InteractionAvailable(ic, interaction, itemUiContext));
|
||||
}
|
||||
|
||||
private static bool InteractionAvailable(ItemContextClass itemContext, EItemInfoButton interaction, ItemUiContext itemUiContext)
|
||||
{
|
||||
// Since itemContext is for "drag", no context actions are allowed. Get the underlying "inventory" context
|
||||
ItemContextAbstractClass innerContext = itemContext.GClass2813_0;
|
||||
if (innerContext == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool createdContext = false;
|
||||
if (innerContext.Item != itemContext.Item)
|
||||
{
|
||||
// Actual context went away and we're looking at inventory/stash context
|
||||
innerContext = innerContext.CreateChild(itemContext.Item);
|
||||
createdContext = true;
|
||||
}
|
||||
|
||||
var contextInteractions = itemUiContext.GetItemContextInteractions(innerContext, null);
|
||||
bool result = contextInteractions.IsInteractionAvailable(interaction);
|
||||
|
||||
if (createdContext)
|
||||
{
|
||||
innerContext.Dispose();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void EquipAll(ItemUiContext itemUiContext, bool allOrNothing)
|
||||
{
|
||||
if (!allOrNothing || InteractionCount(EItemInfoButton.Equip, itemUiContext) == Count)
|
||||
{
|
||||
var taskSerializer = itemUiContext.gameObject.AddComponent<ItemContextTaskSerializer>();
|
||||
taskSerializer.Initialize(
|
||||
SortedItemContexts().Where(ic => InteractionAvailable(ic, EItemInfoButton.Equip, itemUiContext)),
|
||||
itemContext => itemUiContext.QuickEquip(itemContext.Item));
|
||||
|
||||
itemUiContext.Tooltip?.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public static void UnequipAll(ItemUiContext itemUiContext, bool allOrNothing)
|
||||
{
|
||||
if (!allOrNothing || InteractionCount(EItemInfoButton.Unequip, itemUiContext) == Count)
|
||||
{
|
||||
var taskSerializer = itemUiContext.gameObject.AddComponent<ItemContextTaskSerializer>();
|
||||
taskSerializer.Initialize(
|
||||
SortedItemContexts().Where(ic => InteractionAvailable(ic, EItemInfoButton.Unequip, itemUiContext)),
|
||||
itemContext => itemUiContext.Uninstall(itemContext.GClass2813_0));
|
||||
|
||||
itemUiContext.Tooltip?.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public static void UnloadAmmoAll(ItemUiContext itemUiContext, bool allOrNothing)
|
||||
{
|
||||
StopUnloading();
|
||||
if (!allOrNothing || InteractionCount(EItemInfoButton.UnloadAmmo, itemUiContext) == Count)
|
||||
{
|
||||
// Call Initialize() before setting UnloadSerializer so that the initial synchronous call to StopProcesses()->StopUnloading() doesn't immediately cancel this
|
||||
var taskSerializer = itemUiContext.gameObject.AddComponent<ItemContextTaskSerializer>();
|
||||
taskSerializer.Initialize(
|
||||
SortedItemContexts().Where(ic => InteractionAvailable(ic, EItemInfoButton.UnloadAmmo, itemUiContext)),
|
||||
itemContext => itemUiContext.UnloadAmmo(itemContext.Item));
|
||||
|
||||
UnloadSerializer = taskSerializer;
|
||||
itemUiContext.Tooltip?.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public static void StopUnloading()
|
||||
{
|
||||
if (UnloadSerializer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UnloadSerializer.Cancel();
|
||||
UnloadSerializer = null;
|
||||
}
|
||||
|
||||
public static void UnpackAll(ItemUiContext itemUiContext, bool allOrNothing)
|
||||
{
|
||||
if (!allOrNothing || InteractionCount(EItemInfoButton.Unpack, itemUiContext) == Count)
|
||||
{
|
||||
var taskSerializer = itemUiContext.gameObject.AddComponent<ItemContextTaskSerializer>();
|
||||
taskSerializer.Initialize(
|
||||
SortedItemContexts().Where(ic => InteractionAvailable(ic, EItemInfoButton.Unpack, itemUiContext)),
|
||||
itemContext => itemUiContext.UnpackItem(itemContext.Item));
|
||||
|
||||
itemUiContext.Tooltip?.Close();
|
||||
}
|
||||
}
|
||||
|
||||
private static void ShowSelection(GridItemView itemView)
|
||||
{
|
||||
GameObject selectedMark = itemView.transform.Find("SelectedMark")?.gameObject;
|
||||
if (selectedMark == null)
|
||||
{
|
||||
selectedMark = UnityEngine.Object.Instantiate(SelectedMarkTemplate, itemView.transform, false);
|
||||
selectedMark.name = "SelectedMark";
|
||||
}
|
||||
|
||||
selectedMark.SetActive(true);
|
||||
|
||||
GameObject selectedBackground = itemView.transform.Find("SelectedBackground")?.gameObject;
|
||||
if (selectedBackground == null)
|
||||
{
|
||||
selectedBackground = UnityEngine.Object.Instantiate(SelectedBackgroundTemplate, itemView.transform, false);
|
||||
selectedBackground.transform.SetAsFirstSibling();
|
||||
selectedBackground.name = "SelectedBackground";
|
||||
}
|
||||
|
||||
selectedBackground.SetActive(true);
|
||||
}
|
||||
|
||||
private static void HideSelection(GridItemView itemView)
|
||||
{
|
||||
if (itemView == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GameObject selectedMark = itemView.transform.Find("SelectedMark")?.gameObject;
|
||||
GameObject selectedBackground = itemView.transform.Find("SelectedBackground")?.gameObject;
|
||||
|
||||
selectedMark?.SetActive(false);
|
||||
selectedBackground?.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
public class MultiSelectItemContext : ItemContextClass
|
||||
{
|
||||
public MultiSelectItemContext(ItemContextAbstractClass itemContext, ItemRotation rotation) : base(itemContext, rotation)
|
||||
{
|
||||
// Adjust event handlers
|
||||
if (GClass2813_0 != null)
|
||||
{
|
||||
// Listen for underlying context being disposed, it might mean the item is gone (merged, destroyed, etc)
|
||||
GClass2813_0.OnDisposed += OnParentDispose;
|
||||
// This serves no purpose and causes stack overflows
|
||||
GClass2813_0.OnCloseWindow -= CloseDependentWindows;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
base.Dispose();
|
||||
if (GClass2813_0 != null)
|
||||
{
|
||||
GClass2813_0.OnDisposed -= OnParentDispose;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnParentDispose()
|
||||
{
|
||||
if (Item.CurrentAddress == null || Item.CurrentAddress.Container.ParentItem is MagazineClass)
|
||||
{
|
||||
// This item was entirely merged away, or went into a magazine
|
||||
MultiSelect.Deselect(this);
|
||||
}
|
||||
}
|
||||
|
||||
// used by ItemUiContext.QuickFindAppropriatePlace, the one that picks a container, i.e. ctrl-click
|
||||
// ItemContextClass (drag) defaults to None, but we want what the underlying item allows
|
||||
public override bool CanQuickMoveTo(ETargetContainer targetContainer)
|
||||
{
|
||||
if (GClass2813_0 != null)
|
||||
{
|
||||
return GClass2813_0.CanQuickMoveTo(targetContainer);
|
||||
}
|
||||
|
||||
return base.CanQuickMoveTo(targetContainer);
|
||||
}
|
||||
}
|
||||
|
||||
// Specific type of TaskSerializer because Unity can't understand generics
|
||||
public class ItemContextTaskSerializer : TaskSerializer<ItemContextClass> { }
|
||||
|
||||
public static class MultiSelectExtensions
|
||||
{
|
||||
public static bool IsSelectable(this ItemView itemView)
|
||||
{
|
||||
// Common non-interactable stuff
|
||||
if (!itemView.IsInteractable || !itemView.IsSearched || itemView.RemoveError.Value != null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ironically, SelectableSlotItemView is not selectable. Those are for picking as a choice
|
||||
if (itemView is SelectableSlotItemView)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// You can't multi-select trader's items or items being sold
|
||||
if (itemView is TradingItemView tradingItemView)
|
||||
{
|
||||
if (itemView is not TradingPlayerItemView || tradingItemView.R().IsBeingSold)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static IEnumerable<ItemContextClass> RepeatUntilEmpty(this ItemContextClass itemContext)
|
||||
{
|
||||
while (itemContext.Item.StackObjectsCount > 0)
|
||||
{
|
||||
yield return itemContext;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
58
Multiselect/MultiSelectDebug.cs
Normal file
58
Multiselect/MultiSelectDebug.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UIFixes
|
||||
{
|
||||
public class MultiSelectDebug : MonoBehaviour
|
||||
{
|
||||
private GUIStyle guiStyle;
|
||||
private Rect guiRect = new(20, 70, 0, 0);
|
||||
|
||||
GUIContent guiContent;
|
||||
|
||||
public void OnGUI()
|
||||
{
|
||||
if (!MultiSelect.Enabled || !Settings.ShowMultiSelectDebug.Value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
guiStyle ??= new GUIStyle(GUI.skin.box)
|
||||
{
|
||||
alignment = TextAnchor.MiddleLeft,
|
||||
fontSize = 14,
|
||||
margin = new RectOffset(3, 3, 3, 3),
|
||||
richText = true
|
||||
};
|
||||
|
||||
guiContent ??= new GUIContent();
|
||||
|
||||
StringBuilder builder = new();
|
||||
|
||||
builder.Append("<b>MultiSelect</b>\n");
|
||||
builder.AppendFormat("Active: <color={0}>{1}</color>\n", MultiSelect.Active ? "green" : "red", MultiSelect.Active);
|
||||
builder.AppendFormat("Items: <color=yellow>{0}</color>\n", MultiSelect.Count);
|
||||
|
||||
foreach (ItemContextClass itemContext in MultiSelect.ItemContexts)
|
||||
{
|
||||
builder.AppendFormat("x{0} {1}\n", itemContext.Item.StackObjectsCount, itemContext.Item.ToString());
|
||||
}
|
||||
|
||||
if (MultiSelect.SecondaryContexts.Any())
|
||||
{
|
||||
builder.AppendFormat("Secondary Items: <color=yellow>{0}</color>\n", MultiSelect.SecondaryCount);
|
||||
foreach (ItemContextClass itemContext in MultiSelect.SecondaryContexts)
|
||||
{
|
||||
builder.AppendFormat("x{0} {1}\n", itemContext.Item.StackObjectsCount, itemContext.Item.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
guiContent.text = builder.ToString();
|
||||
|
||||
guiRect.size = guiStyle.CalcSize(guiContent);
|
||||
|
||||
GUI.Box(guiRect, guiContent, guiStyle);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user