using file-scoped namespaces
This commit is contained in:
@@ -9,236 +9,235 @@ using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace UIFixes
|
||||
namespace UIFixes;
|
||||
|
||||
public class DrawMultiSelect : MonoBehaviour
|
||||
{
|
||||
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()
|
||||
{
|
||||
private Texture2D selectTexture;
|
||||
selectTexture = new Texture2D(1, 1);
|
||||
selectTexture.SetPixel(0, 0, new Color(1f, 1f, 1f, 0.6f));
|
||||
selectTexture.Apply();
|
||||
|
||||
private Vector3 selectOrigin;
|
||||
private Vector3 selectEnd;
|
||||
|
||||
private GraphicRaycaster localRaycaster;
|
||||
private GraphicRaycaster preloaderRaycaster;
|
||||
|
||||
private bool drawing;
|
||||
private bool secondary;
|
||||
|
||||
public void Start()
|
||||
localRaycaster = GetComponentInParent<GraphicRaycaster>();
|
||||
if (localRaycaster == null)
|
||||
{
|
||||
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");
|
||||
}
|
||||
throw new InvalidOperationException("DrawMultiSelect couldn't find GraphicRayCaster in parents");
|
||||
}
|
||||
|
||||
public void OnDisable()
|
||||
preloaderRaycaster = Singleton<PreloaderUI>.Instance.transform.GetChild(0).GetComponent<GraphicRaycaster>();
|
||||
if (preloaderRaycaster == null)
|
||||
{
|
||||
drawing = false;
|
||||
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 || c is TextMeshProUGUI) // tmp_inputfield is draggable, but textmesh isn't so explicitly include
|
||||
.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;
|
||||
throw new InvalidOperationException("DrawMultiSelect couldn't find the PreloaderUI GraphicRayCaster");
|
||||
}
|
||||
}
|
||||
|
||||
public static class TransformExtensions
|
||||
public void OnDisable()
|
||||
{
|
||||
public static bool IsDescendantOf(this Transform transform, Transform target)
|
||||
drawing = false;
|
||||
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 || c is TextMeshProUGUI) // tmp_inputfield is draggable, but textmesh isn't so explicitly include
|
||||
.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;
|
||||
}
|
||||
|
||||
while (transform.parent != null)
|
||||
{
|
||||
transform = transform.parent;
|
||||
if (transform == target)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,123 +5,122 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UIFixes
|
||||
namespace UIFixes;
|
||||
|
||||
public static class MultiGrid
|
||||
{
|
||||
public static class MultiGrid
|
||||
private static readonly Dictionary<string, Dictionary<string, Vector2Int>> GridOffsets = [];
|
||||
private static readonly Dictionary<string, Dictionary<int, Dictionary<int, string>>> GridsByLocation = [];
|
||||
|
||||
public static LocationInGrid GetGridLocation(GridItemAddress realAddress)
|
||||
{
|
||||
private static readonly Dictionary<string, Dictionary<string, Vector2Int>> GridOffsets = [];
|
||||
private static readonly Dictionary<string, Dictionary<int, Dictionary<int, string>>> GridsByLocation = [];
|
||||
|
||||
public static LocationInGrid GetGridLocation(GridItemAddress realAddress)
|
||||
if (!IsMultiGrid(realAddress))
|
||||
{
|
||||
if (!IsMultiGrid(realAddress))
|
||||
{
|
||||
return realAddress.LocationInGrid;
|
||||
}
|
||||
|
||||
Vector2Int gridOffset = GridOffsets[realAddress.Container.ParentItem.TemplateId][realAddress.Grid.Id];
|
||||
return new LocationInGrid(realAddress.LocationInGrid.x + gridOffset.x, realAddress.LocationInGrid.y + gridOffset.y, realAddress.LocationInGrid.r);
|
||||
return realAddress.LocationInGrid;
|
||||
}
|
||||
|
||||
public static GridItemAddress GetRealAddress(StashGridClass originGrid, LocationInGrid multigridLocation)
|
||||
Vector2Int gridOffset = GridOffsets[realAddress.Container.ParentItem.TemplateId][realAddress.Grid.Id];
|
||||
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))
|
||||
{
|
||||
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));
|
||||
// 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.TemplateId];
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
string gridId = gridsByLocation[x][y];
|
||||
StashGridClass grid = (originGrid.ParentItem as LootItemClass).Grids.Single(g => g.Id == gridId);
|
||||
Vector2Int offsets = GridOffsets[originGrid.ParentItem.TemplateId][gridId];
|
||||
|
||||
LocationInGrid location = new(x - offsets.x, y - offsets.y, multigridLocation.r);
|
||||
return new GridItemAddress(grid, location);
|
||||
return new GridItemAddress(originGrid, multigridLocation);
|
||||
}
|
||||
|
||||
public static void Cache(GridView initialGridView)
|
||||
var gridsByLocation = GridsByLocation[originGrid.ParentItem.TemplateId];
|
||||
|
||||
// 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))
|
||||
{
|
||||
if (initialGridView == null)
|
||||
// Perhaps some weird layout with gaps in the middle? Fall back to a known good
|
||||
x = gridsByLocation.Keys.First();
|
||||
y = gridsByLocation[x].Keys.First();
|
||||
}
|
||||
|
||||
string gridId = gridsByLocation[x][y];
|
||||
StashGridClass grid = (originGrid.ParentItem as LootItemClass).Grids.Single(g => g.Id == gridId);
|
||||
Vector2Int offsets = GridOffsets[originGrid.ParentItem.TemplateId][gridId];
|
||||
|
||||
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.TemplateId) || !IsMultiGrid(parent))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Dictionary<string, Vector2Int> gridOffsets = [];
|
||||
Dictionary<int, Dictionary<int, string>> 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.Id, new Vector2Int(x, y));
|
||||
|
||||
// Populate reverse lookup
|
||||
for (int i = 0; i < gridView.Grid.GridWidth.Value; i++)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Item parent = initialGridView.Grid.ParentItem;
|
||||
if (GridOffsets.ContainsKey(parent.TemplateId) || !IsMultiGrid(parent))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Dictionary<string, Vector2Int> gridOffsets = [];
|
||||
Dictionary<int, Dictionary<int, string>> 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.Id, new Vector2Int(x, y));
|
||||
|
||||
// Populate reverse lookup
|
||||
for (int i = 0; i < gridView.Grid.GridWidth.Value; i++)
|
||||
if (!gridsByLocation.ContainsKey(x + i))
|
||||
{
|
||||
if (!gridsByLocation.ContainsKey(x + i))
|
||||
{
|
||||
gridsByLocation.Add(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.Id);
|
||||
}
|
||||
var rowGrids = gridsByLocation[x + i];
|
||||
for (int j = 0; j < gridView.Grid.GridHeight.Value; j++)
|
||||
{
|
||||
rowGrids.Add(y + j, gridView.Grid.Id);
|
||||
}
|
||||
}
|
||||
|
||||
GridOffsets.Add(parent.TemplateId, gridOffsets);
|
||||
GridsByLocation.Add(parent.TemplateId, gridsByLocation);
|
||||
}
|
||||
|
||||
private static bool IsMultiGrid(GridItemAddress itemAddress)
|
||||
GridOffsets.Add(parent.TemplateId, gridOffsets);
|
||||
GridsByLocation.Add(parent.TemplateId, gridsByLocation);
|
||||
}
|
||||
|
||||
private static bool IsMultiGrid(GridItemAddress itemAddress)
|
||||
{
|
||||
return IsMultiGrid(itemAddress.Container.ParentItem);
|
||||
}
|
||||
|
||||
private static bool IsMultiGrid(Item item)
|
||||
{
|
||||
if (item is not LootItemClass lootItem)
|
||||
{
|
||||
return IsMultiGrid(itemAddress.Container.ParentItem);
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool IsMultiGrid(Item item)
|
||||
{
|
||||
if (item is not LootItemClass lootItem)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return lootItem.Grids.Length > 1;
|
||||
}
|
||||
return lootItem.Grids.Length > 1;
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -2,62 +2,61 @@
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UIFixes
|
||||
namespace UIFixes;
|
||||
|
||||
public class MultiSelectDebug : MonoBehaviour
|
||||
{
|
||||
public class MultiSelectDebug : MonoBehaviour
|
||||
private GUIStyle guiStyle;
|
||||
private Rect guiRect = new(20, 70, 0, 0);
|
||||
|
||||
GUIContent guiContent;
|
||||
|
||||
public void OnGUI()
|
||||
{
|
||||
private GUIStyle guiStyle;
|
||||
private Rect guiRect = new(20, 70, 0, 0);
|
||||
|
||||
GUIContent guiContent;
|
||||
|
||||
public void OnGUI()
|
||||
if (!MultiSelect.Enabled || !Settings.ShowMultiSelectDebug.Value)
|
||||
{
|
||||
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 (DragItemContext itemContext in MultiSelect.SortedItemContexts())
|
||||
{
|
||||
LocationInGrid location = itemContext.ItemAddress is GridItemAddress gridAddress ? MultiGrid.GetGridLocation(gridAddress) : null;
|
||||
builder.AppendFormat("x{0} {1} {2} {3}\n",
|
||||
itemContext.Item.StackObjectsCount,
|
||||
itemContext.ItemAddress.Container.ID,
|
||||
location != null ? $"({location.x}, {location.y})" : "(slot)",
|
||||
itemContext.Item.Name.Localized());
|
||||
}
|
||||
|
||||
if (MultiSelect.SecondaryContexts.Any())
|
||||
{
|
||||
builder.AppendFormat("Secondary Items: <color=yellow>{0}</color>\n", MultiSelect.SecondaryCount);
|
||||
foreach (DragItemContext 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);
|
||||
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 (DragItemContext itemContext in MultiSelect.SortedItemContexts())
|
||||
{
|
||||
LocationInGrid location = itemContext.ItemAddress is GridItemAddress gridAddress ? MultiGrid.GetGridLocation(gridAddress) : null;
|
||||
builder.AppendFormat("x{0} {1} {2} {3}\n",
|
||||
itemContext.Item.StackObjectsCount,
|
||||
itemContext.ItemAddress.Container.ID,
|
||||
location != null ? $"({location.x}, {location.y})" : "(slot)",
|
||||
itemContext.Item.Name.Localized());
|
||||
}
|
||||
|
||||
if (MultiSelect.SecondaryContexts.Any())
|
||||
{
|
||||
builder.AppendFormat("Secondary Items: <color=yellow>{0}</color>\n", MultiSelect.SecondaryCount);
|
||||
foreach (DragItemContext 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