| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using TMPro; |
| | | 3 | | using UnityEngine; |
| | | 4 | | using UnityEngine.UI; |
| | | 5 | | |
| | | 6 | | [RequireComponent(typeof(CanvasRenderer))] |
| | | 7 | | public class TacticalPolarGridGraphic : Graphic { |
| | | 8 | | [SerializeField] |
| | 0 | 9 | | private Color _gridColor = new Color(0.0f, 1.0f, 0.0f, 0.3f); |
| | | 10 | | |
| | | 11 | | [SerializeField] |
| | 0 | 12 | | private Color _textColor = new Color(0.2f, 0.2f, 0.2f, 0.5f); |
| | | 13 | | |
| | | 14 | | [SerializeField] |
| | 0 | 15 | | private int _numberOfBearingLines = 36; // Every 10 degrees. |
| | | 16 | | |
| | | 17 | | [SerializeField] |
| | 0 | 18 | | private int _numberOfRangeRings = 5; |
| | | 19 | | |
| | | 20 | | [SerializeField] |
| | 0 | 21 | | private float[] _rangeScales = { 100f, 1000f, 10000f, 40000f }; // meters |
| | | 22 | | |
| | | 23 | | [SerializeField] |
| | 0 | 24 | | private float _lineWidth = 2f; |
| | | 25 | | |
| | 0 | 26 | | private int _currentRangeIndex = 1; // Start with 10 km range. |
| | | 27 | | |
| | | 28 | | [SerializeField] |
| | 0 | 29 | | private GameObject _rangeText = null!; |
| | | 30 | | |
| | 0 | 31 | | private List<TextMeshProUGUI> _rangeTexts = new List<TextMeshProUGUI>(); |
| | | 32 | | |
| | 0 | 33 | | private float _currentScaleFactor = 1f; |
| | | 34 | | |
| | 0 | 35 | | public float CurrentScaleFactor => _currentScaleFactor; |
| | | 36 | | |
| | 0 | 37 | | protected override void OnEnable() { |
| | 0 | 38 | | base.OnEnable(); |
| | 0 | 39 | | if (Application.isPlaying) { |
| | 0 | 40 | | ClearRangeTexts(); |
| | 0 | 41 | | UpdateRangeTexts(); |
| | 0 | 42 | | } |
| | 0 | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | protected override void OnDestroy() { |
| | 0 | 46 | | base.OnDestroy(); |
| | 0 | 47 | | ClearRangeTexts(); |
| | 0 | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | public void CycleRangeUp() { |
| | 0 | 51 | | _currentRangeIndex = (_currentRangeIndex + 1) % _rangeScales.Length; |
| | 0 | 52 | | SetVerticesDirty(); |
| | 0 | 53 | | UpdateRangeTexts(); |
| | 0 | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | public void CycleRangeDown() { |
| | 0 | 57 | | _currentRangeIndex = (_currentRangeIndex - 1 + _rangeScales.Length) % _rangeScales.Length; |
| | 0 | 58 | | SetVerticesDirty(); |
| | 0 | 59 | | UpdateRangeTexts(); |
| | 0 | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | protected override void OnPopulateMesh(VertexHelper vh) { |
| | 0 | 63 | | vh.Clear(); |
| | | 64 | | |
| | | 65 | | // Convert to km for UI scale. |
| | 0 | 66 | | float maxRange = _rangeScales[_currentRangeIndex] / 1000f; |
| | | 67 | | |
| | | 68 | | // Get the rect dimensions. |
| | 0 | 69 | | Rect rect = GetPixelAdjustedRect(); |
| | 0 | 70 | | Vector2 center = rect.center; |
| | | 71 | | |
| | | 72 | | // Adjust scale based on rect size. |
| | 0 | 73 | | _currentScaleFactor = Mathf.Min(rect.width, rect.height) / (2 * maxRange); |
| | | 74 | | |
| | 0 | 75 | | float scaleFactor = _currentScaleFactor; |
| | | 76 | | |
| | | 77 | | // Draw the grid. |
| | 0 | 78 | | DrawRangeRings(vh, center, maxRange, scaleFactor); |
| | 0 | 79 | | DrawBearingLines(vh, center, maxRange, scaleFactor); |
| | | 80 | | |
| | | 81 | | // Only update positions of existing range texts. |
| | 0 | 82 | | UpdateRangeTextsPositions(center, maxRange, scaleFactor); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | private void DrawRangeRings(VertexHelper vh, in Vector2 center, float maxRange, |
| | 0 | 86 | | float scaleFactor) { |
| | | 87 | | // Extend the range rings to 10x the major marker range. |
| | 0 | 88 | | float extendedMaxRange = maxRange * 10; |
| | | 89 | | |
| | 0 | 90 | | for (int i = 1; i <= _numberOfRangeRings * 10; ++i) { |
| | 0 | 91 | | float radius = i * extendedMaxRange / (_numberOfRangeRings * 10); |
| | 0 | 92 | | DrawCircle(vh, center, radius * scaleFactor, 128, 1f); |
| | | 93 | | |
| | | 94 | | // Make every 10th ring thicker to indicate major markers. |
| | 0 | 95 | | if (i % 10 == 0) { |
| | 0 | 96 | | DrawCircle(vh, center, radius * scaleFactor, 128, 2f); |
| | 0 | 97 | | } |
| | 0 | 98 | | } |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | private void DrawBearingLines(VertexHelper vh, Vector2 center, float maxRange, |
| | 0 | 102 | | float scaleFactor) { |
| | | 103 | | // Extend the bearing lines to 10x the major marker range. |
| | 0 | 104 | | float extendedMaxRange = maxRange * 10; |
| | | 105 | | |
| | 0 | 106 | | float angleStep = 360f / _numberOfBearingLines; |
| | | 107 | | |
| | 0 | 108 | | for (int i = 0; i < _numberOfBearingLines; ++i) { |
| | 0 | 109 | | float angle = i * angleStep * Mathf.Deg2Rad; |
| | 0 | 110 | | Vector2 direction = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)); |
| | 0 | 111 | | DrawLine(vh, center, center + direction * extendedMaxRange * scaleFactor, _lineWidth); |
| | 0 | 112 | | } |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | private void DrawCircle(VertexHelper vh, in Vector2 center, float radius, int segments, |
| | 0 | 116 | | float widthMultiplier) { |
| | 0 | 117 | | float angleStep = 360f / segments; |
| | 0 | 118 | | Vector2 prevPoint = center + new Vector2(radius, 0); |
| | | 119 | | |
| | 0 | 120 | | for (int i = 1; i <= segments; ++i) { |
| | 0 | 121 | | float angle = i * angleStep * Mathf.Deg2Rad; |
| | 0 | 122 | | Vector2 newPoint = center + new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * radius; |
| | 0 | 123 | | DrawLine(vh, prevPoint, newPoint, _lineWidth * widthMultiplier); |
| | 0 | 124 | | prevPoint = newPoint; |
| | 0 | 125 | | } |
| | 0 | 126 | | } |
| | | 127 | | |
| | 0 | 128 | | private void DrawLine(VertexHelper vh, in Vector2 start, in Vector2 end, float thickness) { |
| | | 129 | | // Calculate the total scale factor from Canvas and RectTransform. |
| | 0 | 130 | | float totalScale = canvas.scaleFactor * transform.lossyScale.x; |
| | | 131 | | |
| | | 132 | | // Thickness adjusted for total scale to maintain constant pixel width. |
| | 0 | 133 | | float adjustedThickness = thickness / totalScale; |
| | | 134 | | |
| | 0 | 135 | | Vector2 direction = (end - start).normalized; |
| | 0 | 136 | | Vector2 perpendicular = new Vector2(-direction.y, direction.x) * adjustedThickness * 0.5f; |
| | | 137 | | |
| | 0 | 138 | | Vector2 v0 = start - perpendicular; |
| | 0 | 139 | | Vector2 v1 = start + perpendicular; |
| | 0 | 140 | | Vector2 v2 = end + perpendicular; |
| | 0 | 141 | | Vector2 v3 = end - perpendicular; |
| | | 142 | | |
| | 0 | 143 | | int index = vh.currentVertCount; |
| | | 144 | | |
| | 0 | 145 | | UIVertex vertex = UIVertex.simpleVert; |
| | 0 | 146 | | vertex.color = _gridColor; |
| | | 147 | | |
| | 0 | 148 | | vertex.position = v0; |
| | 0 | 149 | | vh.AddVert(vertex); |
| | | 150 | | |
| | 0 | 151 | | vertex.position = v1; |
| | 0 | 152 | | vh.AddVert(vertex); |
| | | 153 | | |
| | 0 | 154 | | vertex.position = v2; |
| | 0 | 155 | | vh.AddVert(vertex); |
| | | 156 | | |
| | 0 | 157 | | vertex.position = v3; |
| | 0 | 158 | | vh.AddVert(vertex); |
| | | 159 | | |
| | 0 | 160 | | vh.AddTriangle(index, index + 1, index + 2); |
| | 0 | 161 | | vh.AddTriangle(index + 2, index + 3, index); |
| | 0 | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | private void ClearRangeTexts() { |
| | 0 | 165 | | foreach (var text in _rangeTexts) { |
| | 0 | 166 | | if (text != null) { |
| | 0 | 167 | | DestroyImmediate(text.gameObject); |
| | 0 | 168 | | } |
| | 0 | 169 | | } |
| | 0 | 170 | | _rangeTexts.Clear(); |
| | 0 | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | private void UpdateRangeTexts() { |
| | 0 | 174 | | ClearRangeTexts(); |
| | | 175 | | |
| | | 176 | | // Only create new labels if they do not exist. |
| | 0 | 177 | | if (_rangeTexts.Count == 0) { |
| | 0 | 178 | | for (int i = 1; i <= _numberOfRangeRings; ++i) { |
| | 0 | 179 | | GameObject textObj = Instantiate(_rangeText, transform); |
| | 0 | 180 | | TextMeshProUGUI textComponent = textObj.GetComponent<TextMeshProUGUI>(); |
| | 0 | 181 | | textComponent.color = _textColor; |
| | 0 | 182 | | textComponent.transform.localScale = Vector3.one; |
| | 0 | 183 | | _rangeTexts.Add(textComponent); |
| | 0 | 184 | | } |
| | 0 | 185 | | } |
| | | 186 | | |
| | | 187 | | // Update the text values. |
| | 0 | 188 | | for (int i = 0; i < _rangeTexts.Count; ++i) { |
| | 0 | 189 | | _rangeTexts[i].text = GetRangeLabelText(i + 1); |
| | 0 | 190 | | } |
| | 0 | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | private void UpdateRangeTextsPositions(in Vector2 center, float maxRange, float scaleFactor) { |
| | 0 | 194 | | if (_rangeTexts.Count == 0) { |
| | 0 | 195 | | return; |
| | | 196 | | } |
| | | 197 | | |
| | 0 | 198 | | for (int i = 1; i <= _numberOfRangeRings; ++i) { |
| | 0 | 199 | | float radius = i * maxRange / _numberOfRangeRings; |
| | 0 | 200 | | float adjustedRadius = radius * scaleFactor; |
| | | 201 | | |
| | | 202 | | // Position to the right (0 degrees). |
| | 0 | 203 | | Vector2 position = center + new Vector2(adjustedRadius, 0); |
| | | 204 | | |
| | 0 | 205 | | TextMeshProUGUI textComponent = _rangeTexts[i - 1]; |
| | 0 | 206 | | if (textComponent != null) { |
| | 0 | 207 | | textComponent.rectTransform.anchoredPosition = position; |
| | | 208 | | // Adjust text scale inversely to the grid's scaleFactor to maintain constant size |
| | | 209 | | // Assuming uniform scaling for both x and y |
| | 0 | 210 | | float inverseScale = 4f / scaleFactor; |
| | | 211 | | // textComponent.transform.localScale = new Vector3(inverseScale, inverseScale, 1f); |
| | 0 | 212 | | } else { |
| | | 213 | | // Remove the object if it doesn't have a TextMeshProUGUI component |
| | 0 | 214 | | _rangeTexts.Clear(); |
| | 0 | 215 | | return; |
| | | 216 | | } |
| | 0 | 217 | | } |
| | 0 | 218 | | } |
| | | 219 | | |
| | 0 | 220 | | private string GetRangeLabelText(int ringIndex) { |
| | 0 | 221 | | float maxRange = _rangeScales[_currentRangeIndex]; |
| | 0 | 222 | | float rangeValue = ringIndex * maxRange / _numberOfRangeRings; |
| | | 223 | | |
| | 0 | 224 | | if (rangeValue >= 1000f) { |
| | 0 | 225 | | return $"{rangeValue / 1000f:F0} km"; |
| | 0 | 226 | | } else { |
| | 0 | 227 | | return $"{rangeValue:F0} m"; |
| | | 228 | | } |
| | 0 | 229 | | } |
| | | 230 | | } |