//$ Copyright 2015-22, Code Respawn Technologies Pvt Ltd - All Rights Reserved $//
using UnityEngine;
using System.Collections.Generic;
namespace DungeonArchitect.Graphs
{
///
/// The graph pin type
///
public enum GraphPinType
{
Input,
Output,
Unknown
}
///
/// The state of the mouse input on a pin
///
public enum GraphPinMouseState
{
Hover,
Clicked,
None
}
///
/// A pin is used to connect a link to a node
///
[System.Serializable]
public class GraphPin : ScriptableObject
{
GraphPinMouseState clickState = GraphPinMouseState.None;
///
/// The state of the mouse input on this pin
///
public GraphPinMouseState ClickState
{
get
{
return clickState;
}
set
{
clickState = value;
}
}
[SerializeField]
GraphPinType pinType;
///
/// The type of this pin
///
public GraphPinType PinType
{
get
{
return pinType;
}
set
{
pinType = value;
}
}
public delegate void OnPinLinksDestroyed(GraphPin pin);
///
/// Notifies whenever the pin is destroyed
///
public event OnPinLinksDestroyed PinLinksDestroyed;
///
/// The node this pin belongs to
///
[SerializeField]
GraphNode node;
///
/// The owning graph node
///
public GraphNode Node
{
get
{
return node;
}
set
{
node = value;
}
}
[SerializeField]
Vector2 position = new Vector2();
///
/// The position of the graph pin, relative to the owning node's position
///
public Vector2 Position
{
get
{
return position;
}
set
{
position = value;
}
}
///
/// The world position of the pin
///
public Vector2 WorldPosition
{
get
{
if (node != null)
{
return position + node.Position;
}
else
{
return position;
}
}
}
[SerializeField]
Rect boundsOffset = new Rect(0, 0, 20, 20);
///
/// The bounds of the pin, relative to the node's position
///
public Rect BoundsOffset
{
get
{
return boundsOffset;
}
set
{
boundsOffset = value;
}
}
[SerializeField]
Vector2 tangent = new Vector2();
///
/// The tangent of the pin. Links connected to this pin would come in or out from this direction
///
public Vector2 Tangent
{
get
{
return tangent;
}
set
{
tangent = value;
}
}
[SerializeField]
float tangentStrength = 50;
///
/// The spring strength of the link connected to this pin
///
public float TangentStrength
{
get
{
return tangentStrength;
}
set
{
tangentStrength = value;
}
}
public void OnEnable()
{
hideFlags = HideFlags.HideInHierarchy;
}
///
/// Gets all the links connected to this pin
///
/// The connected links.
public GraphLink[] GetConntectedLinks()
{
var result = new List();
if (node != null && node.Graph != null)
{
foreach (var link in node.Graph.Links)
{
if (link == null)
{
continue;
}
if (link.Input == this || link.Output == this)
{
result.Add(link);
}
}
}
return result.ToArray();
}
///
/// Checks if a point is inside the pin
///
/// The point to test in world coordinates
/// true, if inside the bounds of this pin, false otherwise
public virtual bool ContainsPoint(Vector2 worldPoint)
{
return GetWorldBounds().Contains(worldPoint);
}
///
/// Gets the bounds of the pin, in world coordinates
///
/// The bounds of the pin, in world coordinates
Rect GetWorldBounds()
{
var position = this.WorldPosition + boundsOffset.position;
var bounds = new Rect(position.x, position.y, boundsOffset.size.x, boundsOffset.size.y);
return bounds;
}
///
/// Gets the bounds of the pin, relative to the node position
///
/// The bounds of the pin, relative to the node position
public Rect GetBounds()
{
var position = this.Position + boundsOffset.position;
var bounds = new Rect(position.x, position.y, boundsOffset.size.x, boundsOffset.size.y);
return bounds;
}
public void NotifyPinLinksDestroyed()
{
if (PinLinksDestroyed != null)
{
PinLinksDestroyed(this);
}
}
bool requestLinkDeletionInitiated = false;
public bool RequestLinkDeletionInitiated
{
get { return requestLinkDeletionInitiated; }
set { requestLinkDeletionInitiated = value; }
}
}
}