80 lines
2.5 KiB
C#
80 lines
2.5 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Unheimlich.UI.DetectiveBoard
|
|
{
|
|
[RequireComponent(typeof(RectTransform))]
|
|
public class ConnectionLine : MonoBehaviour
|
|
{
|
|
[Header("References")]
|
|
[SerializeField] private Image _lineImage;
|
|
[SerializeField] private RectTransform _rectTransform;
|
|
|
|
[Header("Settings")]
|
|
[SerializeField] private float _lineThickness = 3f;
|
|
[SerializeField] private Color _lineColor = Color.white;
|
|
|
|
private PostItNode _startNode;
|
|
private PostItNode _endNode;
|
|
|
|
public PostItNode StartNode => _startNode;
|
|
public PostItNode EndNode => _endNode;
|
|
|
|
private void Awake()
|
|
{
|
|
if (_rectTransform == null)
|
|
_rectTransform = GetComponent<RectTransform>();
|
|
|
|
if (_lineImage == null)
|
|
_lineImage = GetComponent<Image>();
|
|
}
|
|
|
|
public void Initialize(PostItNode startNode, PostItNode endNode, Color? color = null)
|
|
{
|
|
_startNode = startNode;
|
|
_endNode = endNode;
|
|
|
|
if (color.HasValue)
|
|
_lineColor = color.Value;
|
|
|
|
if (_lineImage != null)
|
|
_lineImage.color = _lineColor;
|
|
|
|
UpdateLine();
|
|
}
|
|
|
|
public void UpdateLine()
|
|
{
|
|
if (_startNode == null || _endNode == null || _rectTransform == null)
|
|
return;
|
|
|
|
Vector2 startPos = _startNode.ConnectionPoint.anchoredPosition;
|
|
Vector2 endPos = _endNode.ConnectionPoint.anchoredPosition;
|
|
|
|
Vector2 direction = endPos - startPos;
|
|
float distance = direction.magnitude;
|
|
|
|
_rectTransform.anchoredPosition = startPos;
|
|
_rectTransform.sizeDelta = new Vector2(distance, _lineThickness);
|
|
|
|
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
|
|
_rectTransform.rotation = Quaternion.Euler(0, 0, angle);
|
|
|
|
_rectTransform.pivot = new Vector2(0, 0.5f);
|
|
}
|
|
|
|
public void SetColor(Color color)
|
|
{
|
|
_lineColor = color;
|
|
if (_lineImage != null)
|
|
_lineImage.color = color;
|
|
}
|
|
|
|
public bool ConnectsNodes(PostItNode nodeA, PostItNode nodeB)
|
|
{
|
|
return (_startNode == nodeA && _endNode == nodeB) ||
|
|
(_startNode == nodeB && _endNode == nodeA);
|
|
}
|
|
}
|
|
}
|