Net.Like.Xue.Tokyo/Assets/BITKit/Unity/Scripts/UX/Library/CellContainer.cs

84 lines
2.6 KiB
C#
Raw Normal View History

2025-02-24 23:03:39 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit.UX
{
public class CellContainer : VisualElement
{
2025-04-17 19:36:08 +08:00
public new class UxmlTraits : VisualElement.UxmlTraits
{
// The progress property is exposed to UXML.
private readonly UxmlIntAttributeDescription _cellSizeAttribute = new()
{
name = "CellSize"
};
// Use the Init method to assign the value of the progress UXML attribute to the C# progress property.
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
((CellContainer)ve).CellSize = _cellSizeAttribute.GetValueFromBag(bag, cc);
}
}
2025-02-24 23:03:39 +08:00
public new class UxmlFactory : UxmlFactory<CellContainer, UxmlTraits> { }
2025-04-17 19:36:08 +08:00
private bool _repaint = true;
public int CellSize
{
get => _cellSize;
set
{
if(_cellSize==value)return;
_cellSize = value;
_repaint = true;
MarkDirtyRepaint();
}
}
private int _cellSize = 64;
2025-02-24 23:03:39 +08:00
public CellContainer()
{
RegisterCallback<CustomStyleResolvedEvent>(x =>
{
2025-04-17 19:36:08 +08:00
_repaint = true;
2025-02-24 23:03:39 +08:00
});
2025-04-17 19:36:08 +08:00
RegisterCallback<GeometryChangedEvent>(x => MarkDirtyRepaint());
2025-02-24 23:03:39 +08:00
generateVisualContent += GenerateVisualContent;
}
private void GenerateVisualContent(MeshGenerationContext obj)
{
2025-04-17 19:36:08 +08:00
if(_repaint is false)return;
2025-02-24 23:03:39 +08:00
if(contentRect.height<=0 || contentRect.width<=0)return;
2025-04-17 19:36:08 +08:00
if(_cellSize is 0)return;
2025-02-24 23:03:39 +08:00
var painter = obj.painter2D;
painter.lineWidth = resolvedStyle.borderTopWidth;
painter.strokeColor = resolvedStyle.borderTopColor;
var yCount = contentRect.height / CellSize;
var xCount = contentRect.width / CellSize;
for (var x = 1; x < xCount; x++)
{
painter.BeginPath();
painter.MoveTo(new Vector2(x * CellSize, 0));
painter.LineTo(new Vector2(x * CellSize, contentRect.height));
painter.Stroke();
}
for (var y = 1; y < yCount; y++)
{
painter.BeginPath();
painter.MoveTo(new Vector2(0, y * CellSize));
painter.LineTo(new Vector2(contentRect.width, y * CellSize));
painter.Stroke();
}
}
}
}