Inventory grid: hover events, serialized layout, Padding and RecordAt
The grid raises HoveredCellChanged(was, now) from pointer enter, move and exit (move reaches it during another object's drag), its cell size, padding and orientation are authored fields, and RecordAt is the exact inverse of SetCell for display copies (the drag ghost, the harness). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
afb6d69b76
commit
7f9880f2e4
1 changed files with 47 additions and 5 deletions
|
|
@ -2,7 +2,9 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
using int2 = CultMath.int2;
|
||||
|
||||
|
|
@ -24,9 +26,11 @@ public enum GridOrientation { LowerLeft, UpperRight }
|
|||
|
||||
// One quad that draws a whole inventory panel's cells through UI/Inventory Grid (map section IG). It owns the layout of the
|
||||
// cells, their pixels and the point-to-cell mapping, and decides nothing about a panel: SetCell is the one write path.
|
||||
// The record texture is a cache of what SetCell was given and is never read back as truth.
|
||||
// The record texture is a cache of what SetCell was given and is never read back as truth: RecordAt is a display read (the drag
|
||||
// ghost's copy and the parity harness), and no decision reads it. The cell size, padding and orientation are serialized, authored on
|
||||
// the object; Resize still overwrites them.
|
||||
[RequireComponent(typeof(CanvasRenderer))]
|
||||
public class InventoryGrid : MaskableGraphic, ILayoutElement, ICanvasRaycastFilter
|
||||
public class InventoryGrid : MaskableGraphic, ILayoutElement, ICanvasRaycastFilter, IPointerEnterHandler, IPointerMoveHandler, IPointerExitHandler
|
||||
{
|
||||
public Shader Shader;
|
||||
|
||||
|
|
@ -52,9 +56,10 @@ public class InventoryGrid : MaskableGraphic, ILayoutElement, ICanvasRaycastFilt
|
|||
}
|
||||
|
||||
private int _width, _height;
|
||||
private float _cellSize = 32;
|
||||
private RectOffset _padding = new RectOffset();
|
||||
private GridOrientation _orientation;
|
||||
[SerializeField] private float _cellSize = 32;
|
||||
[SerializeField] private RectOffset _padding = new RectOffset();
|
||||
[SerializeField] private GridOrientation _orientation;
|
||||
private int2? _hovered;
|
||||
private Color32[] _records = new Color32[0];
|
||||
private Texture2D _texture;
|
||||
private bool _dirty;
|
||||
|
|
@ -66,9 +71,13 @@ public class InventoryGrid : MaskableGraphic, ILayoutElement, ICanvasRaycastFilt
|
|||
public int Width => _width;
|
||||
public int Height => _height;
|
||||
public float CellSize => _cellSize;
|
||||
public RectOffset Padding => _padding;
|
||||
public GridOrientation Orientation => _orientation;
|
||||
public override Texture mainTexture => _texture;
|
||||
|
||||
// The hovered present cell changed: (was, now), either null for none. Resize and Clear forget the hovered cell without raising.
|
||||
public event Action<int2?, int2?> HoveredCellChanged;
|
||||
|
||||
public void Resize(int width, int height, float cellSize, RectOffset padding, GridOrientation orientation)
|
||||
{
|
||||
_cellSize = cellSize;
|
||||
|
|
@ -86,6 +95,7 @@ public class InventoryGrid : MaskableGraphic, ILayoutElement, ICanvasRaycastFilt
|
|||
_records = new Color32[_texture.width * _texture.height];
|
||||
}
|
||||
else System.Array.Clear(_records, 0, _records.Length);
|
||||
_hovered = null;
|
||||
_dirty = true;
|
||||
SetAllDirty();
|
||||
}
|
||||
|
|
@ -93,6 +103,7 @@ public class InventoryGrid : MaskableGraphic, ILayoutElement, ICanvasRaycastFilt
|
|||
public void Clear()
|
||||
{
|
||||
System.Array.Clear(_records, 0, _records.Length);
|
||||
_hovered = null;
|
||||
_dirty = true;
|
||||
}
|
||||
|
||||
|
|
@ -106,6 +117,19 @@ public class InventoryGrid : MaskableGraphic, ILayoutElement, ICanvasRaycastFilt
|
|||
_dirty = true;
|
||||
}
|
||||
|
||||
// The exact inverse of SetCell; absent outside the grid.
|
||||
public CellRecord RecordAt(int2 cell)
|
||||
{
|
||||
if (cell.x < 0 || cell.y < 0 || cell.x >= _width || cell.y >= _height) return default;
|
||||
var at = cell.y * 3 * _width + 3 * cell.x;
|
||||
var flags = _records[at + 2];
|
||||
return new CellRecord
|
||||
{
|
||||
Icon = _records[at], Background = _records[at + 1], Mask = flags.r,
|
||||
Present = (flags.g & 1) != 0, Item = (flags.g & 2) != 0, Conductivity = (byte) (flags.b & 15)
|
||||
};
|
||||
}
|
||||
|
||||
// Uploads the records when any changed. Once per frame is the contract; edit-mode callers flush by hand.
|
||||
public void Flush()
|
||||
{
|
||||
|
|
@ -278,6 +302,24 @@ public class InventoryGrid : MaskableGraphic, ILayoutElement, ICanvasRaycastFilt
|
|||
return (_records[cell.y * 3 * _width + 3 * cell.x + 2].g & 1) != 0;
|
||||
}
|
||||
|
||||
// Pointer move reaches the grid while another object owns a drag (the input module sends it to every hovered object), so the
|
||||
// hovered cell follows the pointer through a foreign drag with no polling.
|
||||
public void OnPointerEnter(PointerEventData data) => Hover(data);
|
||||
public void OnPointerMove(PointerEventData data) => Hover(data);
|
||||
|
||||
public void OnPointerExit(PointerEventData data) => SetHovered(null);
|
||||
|
||||
private void Hover(PointerEventData data) =>
|
||||
SetHovered(IsRaycastLocationValid(data.position, data.enterEventCamera) && CellAt(data.position, data.enterEventCamera, out var cell, out _) ? cell : (int2?) null);
|
||||
|
||||
private void SetHovered(int2? now)
|
||||
{
|
||||
var was = _hovered;
|
||||
if (was.HasValue == now.HasValue && (!was.HasValue || was.Value.Equals(now.Value))) return;
|
||||
_hovered = now;
|
||||
HoveredCellChanged?.Invoke(was, now);
|
||||
}
|
||||
|
||||
public float minWidth => _padding.horizontal + _width * _cellSize;
|
||||
public float preferredWidth => minWidth;
|
||||
public float flexibleWidth => -1;
|
||||
|
|
|
|||
Loading…
Reference in a new issue