using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements; namespace BITKit.UX { public class CellContainer : VisualElement { 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); } } public new class UxmlFactory : UxmlFactory { } private bool _repaint = true; public int CellSize { get => _cellSize; set { if(_cellSize==value)return; _cellSize = value; _repaint = true; MarkDirtyRepaint(); } } private int _cellSize = 64; public CellContainer() { RegisterCallback(x => { _repaint = true; }); RegisterCallback(x => MarkDirtyRepaint()); generateVisualContent += GenerateVisualContent; } private void GenerateVisualContent(MeshGenerationContext obj) { if(_repaint is false)return; if(contentRect.height<=0 || contentRect.width<=0)return; if(_cellSize is 0)return; 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(); } } } }