Files
2022-07-04 13:15:35 +03:00

291 lines
26 KiB
C#
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using TMPro;
public class Handler : MonoBehaviour
{
private Vector3 mousePosition;
//если oneClamp==true то начало ограничения endDragMin, а конец ограничения endDragMax; если oneClamp==false то начало движения объекта endDragMin, а конец движения endDragMax
public float endDragMin = 0; //ограничения для Drag
public float endDragMax = 0; //ограничения для Drag
//***********************************************************************************************************************************************************
public float defaultAngle = 0; // угол устанавливаемый по дефолту. угол 0 по часам (значения могут быть от -180 до 180 или 0-360)
public bool oneClamp = true; //true если ограничение не проходит через самую нижнюю точку (6 часов)
public float currentAngle;
public rotate3DPedals connector;
public rotate3DPedals connector2;
public rotate3DPedals connector3;
public rotate3DPedals connector4;
public rotate3DPedals connector5;
public rotate3DPedals connector6;
// Данные четыре слота резервируем отдельно для вращения ручек по горизонтали и вертикали, используя отдельные коннекторы
public rotate3DPedals connector7; // Горизонтальный
public rotate3DPedals connector8; //Вертикальный
public rotate3DPedals connector9; // Горизонтальный
public rotate3DPedals connector10; //Вертикальный
// Отдельный слот для вращения одиночных
public rotate3DPedals connector11; //Горизонтальный
public rotate3DPedals connector12; // Вертикальный
// Слот для панелей 0701,0702
public rotate3DPedals connector13; // Вертикальный 0701 или 0702
public rotate3DPedals connector14; // Вертикальный 0701 или 0702
public rotate3DPedals connector15; // Вертикальный 0701 или 0702
public rotate3DPedals connector16; // Вертикальный 0701 или 0702
// Отдельный слот для вращения 2D объектов, работает в педалях
public GameObject connected2d;
//
private float rot_z; // deltaAngle
Camera cam;//камера для канваса с панелями
private Vector2 a; // Vector2 deltaPosition
[HideInInspector]
public bool drag = false;//работает ли в данный момент вращение (пока используется только в 115 панели)
Manager man;
public bool isStarted = false;
mfiArrowNumber arrowScript;
Indicator indicatorScript;
mfiClockIndicator clockScript;
public void Awake()
{
transform.localRotation = Quaternion.Euler(0f, 0f, -defaultAngle);
//cam = GameObject.Find("Camera").GetComponent<Camera>();
man = GameObject.Find("Manager").GetComponent<Manager>();
cam = man.cam;
if (man.mode == "free")
currentAngle = defaultAngle;
arrowScript = this.gameObject.GetComponentInChildren<mfiArrowNumber>();
indicatorScript = this.gameObject.GetComponentInChildren<Indicator>();
clockScript = this.gameObject.GetComponentInParent<mfiClockIndicator>();
isStarted = true;
}
void OnMouseDown()
{
if (man.mode == "scedit" || man.mode == "play")
return;
drag = true;
mousePosition = cam.ScreenToWorldPoint(Input.mousePosition);
a = new Vector2(mousePosition.x - transform.position.x, mousePosition.y - transform.position.y);
rot_z = -Mathf.Atan2(a.x, a.y) * Mathf.Rad2Deg;
if (!Mathf.Approximately(transform.localRotation.eulerAngles.z, 0))
{
float Z;
if (transform.localRotation.eulerAngles.z < 180)
{
Z = -transform.localRotation.eulerAngles.z;
}
else if (transform.localRotation.eulerAngles.z < 360)
{
Z = 360 - transform.localRotation.eulerAngles.z;
}
else
{
Z = -transform.localRotation.eulerAngles.z % 360;
}
//float Z = (transform.localRotation.eulerAngles.z < 180) ? -transform.localRotation.eulerAngles.z : (360 - transform.localRotation.eulerAngles.z);
rot_z = Z + rot_z;
}
}
void OnMouseDrag()
{
if (drag)
{
mousePosition = cam.ScreenToWorldPoint(Input.mousePosition);
Vector3 difference = mousePosition - transform.position;
float rotation_z = (Mathf.Atan2(difference.x, difference.y) * Mathf.Rad2Deg);
rotation_z += rot_z;
//*********************если есть ограничения***********
if (endDragMin != 0 || endDragMax != 0)
{
if (oneClamp && (rotation_z > endDragMin && rotation_z < endDragMax))
{
if(rotation_z % endDragMin < rotation_z % endDragMax)
rotation_z = endDragMin;
else
rotation_z = endDragMax;
/* зачем такие сложности? из-за этого стрелки и скакали в запрещенном интервале
if (transform.localRotation.eulerAngles.z % endDragMin < 1)
{
if (transform.localRotation.eulerAngles.z < endDragMin)
{
//transform.localRotation = Quaternion.Euler(0f, 0f, -endDragMin);
rotation_z = endDragMin;
}
}
else if (transform.localRotation.eulerAngles.z % endDragMax < 1)
{
if (transform.localRotation.eulerAngles.z > endDragMax)
{
//transform.localRotation = Quaternion.Euler(0f, 0f, -endDragMax);
rotation_z = endDragMax;
}
}*/
//drag = false;
}
else
{
if (!oneClamp && (rotation_z < endDragMin || rotation_z > endDragMax))
{
//drag = false;
rotation_z = currentAngle;
}
else
{
//transform.localRotation = Quaternion.Euler(0f, 0f, -rotation_z); // это не нужно - в конце вызывается SetObjectState, она вызывает перерисовку
}
}
}
//**************************если нет ограничений**********************
else
{
//transform.localRotation = Quaternion.Euler(0f, 0f, -rotation_z);
}
currentAngle = rotation_z; //Debug.Log("угол поворота " + rotation_z);
//GetComponent<mfiArrowNumber>()?.ChangeNumber();
/*
// Интеграция с 3D педалями
if (connector != null && connector2 != null && connector3 != null && connector4 != null && connector5 != null && connector6 != null)
connectWithPedals();
// Используем отдельный коннектор 7 и 8 для поворота ручек, соединяя соответствующий слот с поворачиваемой ручкой.
// Перетаскиваем в слот 7 ручку,которую будем поворачивать по горизонтали. В 8 слот по вертикали
// Интеграция С панелью 0801-0802
if (connector7 != null && connector9 != null) connectWith080Xhorizontal();
if (connector8 != null && connector10 != null) connectWith080Xvertical();
// Одиночные интеграции по горизонтали
if (connector11 != null ) connectWithLeverOneHorizontal();
// Одиночные интеграции по вертикали
if (connector12 != null) connectWithLeverOneVertical();
// Интеграция С панелью 0701-0702 по вертикали
if (connector13 != null && connector14 != null && connector15 != null && connector16 != null) connectWith070Xvertical();
// else
// if (connected2d != null)
// connected2d.transform.rotation = Quaternion.Euler(0f, 0f, -rotation_z);
*/
UnityObject curUO = man.objects.FindHandler(this.gameObject.name);
float newState = (int)currentAngle;
//int umin = curUO.stateInRange.umin;
//int umax = curUO.stateInRange.umax;
//int newState = ((int)currentAngle + 360) % 360;
//int umin = (curUO.stateInRange.umin + 360) % 360;
//int umax = (curUO.stateInRange.umax + 360) % 360;
//if (umax <= umin) umax += 360;
if(curUO.type == "clockIndicator" && clockScript != null)
{
if (this.name.EndsWith("_h"))
newState = clockScript.TransformHourAngleToState(currentAngle);
if (this.name.EndsWith("_m"))
newState = clockScript.TransformMinutesAngleToState(currentAngle);
}
else
if (curUO.type == "arrowIndicator")
newState = arrowScript.TransformAngleToState(newState);
else
if (curUO.type == "indicator")
newState = indicatorScript.TransformAngleToState(newState);
else
if (curUO.stateInRange != null && (curUO.stateInRange.max - curUO.stateInRange.min) != 0 && (curUO.stateInRange.umax - curUO.stateInRange.umin) != 0)
{
//if (newState < umin) newState = umin;
//if (newState > umax) newState = umax;
newState = curUO.stateInRange.min + (int)(((float)(newState - curUO.stateInRange.umin) / ((float)curUO.stateInRange.umax - (float)curUO.stateInRange.umin)) * ((float)curUO.stateInRange.max - (float)curUO.stateInRange.min));
}
curUO.SetObjectState((int)Mathf.Round(newState));
rotation_z = currentAngle;
if (curUO.stateInRange == null)
transform.localRotation = Quaternion.Euler(0f, 0f, -rotation_z); // для индикаторов и др. объектов, не имеющих stateInRange
}
}
private void OnMouseUp()
{
drag = false;
}
public void SetCurAngle(float newang)
{
if (man == null) man = GameObject.Find("Manager").GetComponent<Manager>();
if (man.mode != "free")
{
currentAngle = newang;
transform.localRotation = Quaternion.Euler(0f, 0f, -currentAngle);
}
else
{
currentAngle = newang;
//Debug.Log(currentAngle);
transform.localRotation = Quaternion.Euler(0f, 0f, -currentAngle);
}
/* currentAngle = newang;
transform.localRotation = Quaternion.Euler(0f, 0f, currentAngle);*/
//if (this.gameObject == null)
// Debug.Log("");
//UnityObject uo = man.objects.Find(this.gameObject.name);
//if(uo != null && uo.name == "Handle3_0701")
// Debug.Log(uo.dim + " " +uo.name + " SetCurAngle isStarted = " + isStarted + " angle = "+newang);
//if (uo != null && uo.name == "Handle3_0702")
// Debug.Log(uo.dim + " " + uo.name + " SetCurAngle isStarted = " + isStarted + " angle = " + newang);
}
public void connectWithPedals()
{
connector.RotatePedals(currentAngle);
connector2.RotatePedals(currentAngle);
connector3.RotatePedals(currentAngle);
connector4.RotatePedals(currentAngle);
connector5.RotatePedals(currentAngle);
connector6.RotatePedals(currentAngle);
man.cur090XangleHor = currentAngle;
//Debug.Log("connectWithPedals currentAngle = " + currentAngle);
}
public void connectWith080Xhorizontal()
{
connector7.RotateLeverHorizontal(currentAngle); //transform.localRotation.eulerAngles.z
connector9.RotateLeverHorizontal(currentAngle);
man.cur080XangleHor = currentAngle;
//Debug.Log("connectWith080Xhorizontal currentAngle = " + currentAngle);
}
public void connectWith080Xvertical()
{
connector8.RotateLeverVertical(currentAngle);
connector10.RotateLeverVertical(currentAngle);
man.cur080XangleVert = currentAngle;
//Debug.Log("connectWith080Xvertical currentAngle = " + currentAngle);
}
// Слот одиночных ручек
public void connectWithLeverOneHorizontal()
{
connector11.RotateLeverHorizontal(currentAngle);
}
public void connectWithLeverOneVertical()
{
connector12.RotateLeverOneVertical(currentAngle);
}
public void connectWith070Xvertical()
{
connector13.RotateLeverOneVertical(currentAngle);
connector14.RotateLeverOneVertical(currentAngle);
connector15.RotateLeverOneVertical(currentAngle);
connector16.RotateLeverOneVertical(currentAngle);
man.cur070XangleVert = currentAngle;
//Debug.Log("connectWith070Xvertical currentAngle = " + currentAngle);
}
}