mirror of
https://gitea.msk.dinamika-avia.ru/Constanta-Design/MI-38.git
synced 2026-01-24 02:15:38 +03:00
308 lines
12 KiB
C#
308 lines
12 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class Connection : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
|
|
{
|
|
public GameObject[] connects;
|
|
[ContextMenuItem("Button", "OnButton")]
|
|
[ContextMenuItem("ButtonCheckable", "OnButtonCheckable")]
|
|
[ContextMenuItem("ButtonCheckable3", "OnButtonCheckable3")]
|
|
[ContextMenuItem("Switch", "OnSwitch")]
|
|
[ContextMenuItem("Handler", "OnHandler")]
|
|
[ContextMenuItem("HideCollider", "OnHideCollider")]
|
|
[ContextMenuItem("ScaleToScaleX", "OnScaleToScaleX")]
|
|
public string typeThis;// = null;//"Button". "ButtonCheckable", "ButtonCheckable3", "Switch" (êðûøêà âûêëþ÷àåò òóìáëåðû), "Handler", "HideCollider", "ScaleToScaleX"(115), CoverStateToCoverState 0701 0702,
|
|
void OnButton() => typeThis = "Button";
|
|
void OnButtonCheckable() => typeThis = "ButtonCheckable";
|
|
void OnButtonCheckable3() => typeThis = "ButtonCheckable3";
|
|
void OnSwitch() => typeThis = "Switch";
|
|
void OnHandler() => typeThis = "Handler";
|
|
void OnHideCollider() => typeThis = "HideCollider";
|
|
void OnScaleToScaleX() => typeThis = "ScaleToScaleX";
|
|
[SerializeField] private int index=0;
|
|
|
|
private float d;//
|
|
private DragCollider _drag;
|
|
private Vector2 xy;
|
|
private bool _delta = false;
|
|
private PointerEventData data;
|
|
Manager man;
|
|
|
|
private void Start()
|
|
{
|
|
man = GameObject.Find("Manager").GetComponent<Manager>();
|
|
xy = new Vector2(this.gameObject.transform.localPosition.x, this.gameObject.transform.localPosition.y);
|
|
|
|
for (int i = 0; i < connects.Length; i++)
|
|
{
|
|
switch (typeThis)
|
|
{
|
|
case "Handler":
|
|
connects[i].gameObject.GetComponent<PolygonCollider2D>().enabled = !connects[i].gameObject.GetComponent<PolygonCollider2D>().enabled;
|
|
break;
|
|
case "ScaleToScaleX":
|
|
_drag = connects[i].gameObject.transform.GetComponent<DragCollider>();
|
|
ref readonly string typeDrag = ref _drag.typeDrag;
|
|
if (typeDrag == "x")
|
|
{
|
|
d = connects[i].gameObject.transform.localPosition.x - xy.x;
|
|
}
|
|
else
|
|
{
|
|
d = connects[i].gameObject.transform.localPosition.y - xy.y;
|
|
}
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
private void OnMouseEnter()
|
|
{
|
|
for (int i = 0; i < connects.Length; i++)
|
|
{
|
|
switch (typeThis)
|
|
{
|
|
case "HideCollider":
|
|
if (connects[i].gameObject.GetComponent<Collider2D>() != null)
|
|
{
|
|
connects[i].gameObject.GetComponent<Collider2D>().enabled = false;
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
private void OnMouseExit()
|
|
{
|
|
for (int i = 0; i < connects.Length; i++)
|
|
{
|
|
switch (typeThis)
|
|
{
|
|
case "HideCollider":
|
|
if (connects[i].gameObject.GetComponent<Collider2D>() != null)
|
|
{
|
|
connects[i].gameObject.GetComponent<Collider2D>().enabled = true;
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
void OnMouseDown()
|
|
{
|
|
if(man == null) man = GameObject.Find("Manager").GetComponent<Manager>();
|
|
if (man.mode == "scedit" || man.mode == "play")
|
|
return;
|
|
xy = new Vector2(this.gameObject.transform.localPosition.x, this.gameObject.transform.localPosition.y);
|
|
for (int i = 0; i < connects.Length; i++)
|
|
{
|
|
switch (typeThis)
|
|
{
|
|
case "Button":
|
|
connects[i].gameObject.GetComponent<myButton>().OnDown();
|
|
break;
|
|
case "ButtonCheckable":
|
|
connects[i].gameObject.GetComponent<ButtonCheckable>().OnCheck();
|
|
break;
|
|
case "ButtonCheckable3":
|
|
connects[i].gameObject.GetComponent<ButtonCheckable3>().EnableSprite(index);
|
|
break;
|
|
case "Switch":
|
|
connects[i].gameObject.GetComponent<Switch>().setState(0);
|
|
Transform[] btnList = connects[i].gameObject.GetComponentsInChildren<Transform>();
|
|
string currentSw3Dname = connects[i].name.Substring(0, connects[i].name.Length) + "_3D";
|
|
GameObject currentSw3D = GameObject.Find(currentSw3Dname);
|
|
//Debug.Log($"currentSw3Dname {currentSw3Dname}");
|
|
Switch3DRotate sw3Drotate = currentSw3D.GetComponent<Switch3DRotate>();
|
|
sw3Drotate.RotateVertikalSwitch(2);
|
|
break;
|
|
case "Handler":
|
|
connects[i].gameObject.GetComponent<PolygonCollider2D>().enabled=!connects[i].gameObject.GetComponent<PolygonCollider2D>().enabled;
|
|
break;
|
|
case "CoverStateToCoverState":
|
|
//Debug.Log($"connects[i].gameObject.GetComponent<CoverState>() {connects[i].gameObject.GetComponent<CoverState>() != null}");
|
|
connects[i].gameObject.GetComponent<CoverState>().switchOn();
|
|
break;
|
|
case "ScaleToScaleX":
|
|
ref readonly string typeDrag = ref _drag.typeDrag;
|
|
ref readonly bool _delta2 = ref _drag._delta;
|
|
_delta = _delta2;
|
|
|
|
if (typeDrag == "x" && !_delta)
|
|
{
|
|
d = connects[i].gameObject.transform.localPosition.x - xy.x;
|
|
}
|
|
else if (typeDrag == "y" && !_delta)
|
|
{
|
|
d = connects[i].gameObject.transform.localPosition.y - xy.y;
|
|
//Debug.Log($"d = {d}");
|
|
}
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
void OnMouseUp()
|
|
{
|
|
if (man.mode == "scedit" || man.mode == "play")
|
|
return;
|
|
for (int i = 0; i < connects.Length; i++)
|
|
{
|
|
switch (typeThis)
|
|
{
|
|
case "Button":
|
|
connects[i].gameObject.GetComponent<myButton>().OnUp();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
{
|
|
if (man.mode == "scedit" || man.mode == "play")
|
|
return;
|
|
xy = new Vector2(this.gameObject.transform.localPosition.x, this.gameObject.transform.localPosition.y);
|
|
|
|
for (int i = 0; i < connects.Length; i++)
|
|
{
|
|
switch (typeThis)
|
|
{
|
|
case "ScaleToScaleX":
|
|
ref readonly string typeDrag = ref _drag.typeDrag;
|
|
ref readonly bool _delta2 = ref _drag._delta;
|
|
_delta = _delta2;
|
|
|
|
if (typeDrag == "x" && !_delta)
|
|
{
|
|
d = connects[i].gameObject.transform.localPosition.x - xy.x;
|
|
}
|
|
else if (typeDrag == "y" && !_delta)
|
|
{
|
|
d = connects[i].gameObject.transform.localPosition.y - xy.y;
|
|
}
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if (man.mode == "scedit" || man.mode == "play")
|
|
return;
|
|
for (int i = 0; i < connects.Length; i++)
|
|
{
|
|
switch (typeThis)
|
|
{
|
|
case "ScaleToScaleX":
|
|
if (connects[i].gameObject.transform.GetComponent<IndexH>() != null)
|
|
{
|
|
GameObject frame = connects[i].gameObject.transform.GetComponent<IndexH>().frame;
|
|
if (frame.GetComponent<ScaleToText>() != null || frame.GetComponent<ScaleToText4>() != null)
|
|
{
|
|
bool point = (frame.GetComponent<ScaleToText>() != null) ? frame.GetComponent<ScaleToText>().point : frame.GetComponent<ScaleToText4>().point;
|
|
bool isDrag = this.GetComponent<DragCollider>().isDrag;
|
|
if (point || isDrag)
|
|
{
|
|
OnDrag(data);
|
|
//Debug.Log($"point in connection = {point}");
|
|
}
|
|
else
|
|
{
|
|
OnBeginDrag(data);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
if (man.mode == "scedit" || man.mode == "play")
|
|
return;
|
|
for (int i = 0; i < connects.Length; i++)
|
|
{
|
|
switch (typeThis)
|
|
{
|
|
case "ScaleToScaleX":
|
|
ref readonly bool clamp = ref _drag.clamp;
|
|
ref readonly string typeDrag = ref _drag.typeDrag;
|
|
ref readonly float dragMin = ref _drag.dragMin;
|
|
ref readonly float dragMax = ref _drag.dragMax;
|
|
|
|
/*GameObject frame = connects[i].gameObject.transform.GetComponent<IndexH>().frame;
|
|
bool point = (frame.GetComponent<ScaleToText4>()!=null) ? frame.GetComponent<ScaleToText4>().point:false;*/
|
|
//ref readonly Vector2 dd = ref _drag.dd;
|
|
ref bool _delta2 = ref _drag._delta;
|
|
|
|
//Debug.Log($"OnDrag in connection = true");
|
|
if (clamp && typeDrag == "x")
|
|
{
|
|
//Debug.Log($"clamp && typeDrag == x");
|
|
if (this.gameObject.transform.localPosition.x + d >= dragMin && this.gameObject.transform.localPosition.x + d <= dragMax)
|
|
{
|
|
/*if(point) d = -2532;
|
|
Debug.Log($" point = {point}");
|
|
Debug.Log($" d = {d}");*/
|
|
_delta = false;
|
|
connects[i].gameObject.transform.localPosition = new Vector3(this.gameObject.transform.localPosition.x + d, connects[i].gameObject.transform.localPosition.y, connects[i].gameObject.transform.localPosition.z);
|
|
//Debug.Log($" connects[i].gameObject.transform.localPosition = {connects[i].gameObject.transform.localPosition}");
|
|
}
|
|
else
|
|
{
|
|
_delta = true;
|
|
}
|
|
|
|
}
|
|
else if (clamp && typeDrag == "y")
|
|
{
|
|
if (this.gameObject.transform.localPosition.y + d >= dragMin && this.gameObject.transform.localPosition.y + d <= dragMax)
|
|
{
|
|
_delta = false;
|
|
connects[i].gameObject.transform.localPosition = new Vector3(connects[i].gameObject.transform.localPosition.x, this.gameObject.transform.localPosition.y + d, connects[i].gameObject.transform.localPosition.z);
|
|
}
|
|
else
|
|
{
|
|
_delta = true;
|
|
}
|
|
|
|
}
|
|
_delta2 = _delta;
|
|
xy = new Vector2(this.gameObject.transform.localPosition.x, this.gameObject.transform.localPosition.y);
|
|
//connects[i].gameObject.transform.localPosition = new Vector3(this.gameObject.transform.localPosition.x + d, connects[i].gameObject.transform.localPosition.y, connects[i].gameObject.transform.localPosition.z);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
|
|
}
|
|
}
|