Files
MI-38/Heli_with_panels/Assets/MFI/mfiArrowNumber.cs
2022-07-06 12:36:49 +03:00

112 lines
3.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
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.Events;
using TMPro;
using System;
public class mfiArrowNumber : MonoBehaviour
{
[SerializeField]
private int[] rangeColor; //массив int с кратностью 5 {min на шкале (цифрами int), max на шкале (цифрами int), min на слайдере (цифрами int), max на слайдере (цифрами int), для удобства форматирования}
private string formatText = "{0}";
private TextMeshPro _number;
private Handler _arrow;
[SerializeField]
//private bool _float = false;
void Awake()
{
Init();
ChangeNumber();
}
void Init() // так надо. Awake не всегда вызывается до начала работы с объектом
{
_number = GetComponentInParent<TextMeshPro>();
_arrow = GetComponent<Handler>();
}
private void ChangeIndicator(float val)
{
float num = TransformAngleToState(val);
if(_number != null)
_number.text = string.Format(formatText, (int)Mathf.Round(num));
}
public void ChangeNumber()
{
if (_arrow == null) Init();
ChangeIndicator(_arrow.currentAngle);
}
// На случай вращения против часовой
/* static void Swap<T>(ref T lhs, ref T rhs)
{
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}*/
public float TransformAngleToState(float val)
{
int count = 0;
for (int i = 0; i < rangeColor.Length / 5; i++)
{
int min = rangeColor[count++];
int max = rangeColor[count++];
int kmin = rangeColor[count++];
int kmax = rangeColor[count++];
count++;
//if (max < min) Swap(ref max, ref min); //На случай вращения против часовой
//Debug.Log($"min = {min}, max = {max}, kmin = {kmin}, kmax = {kmax}");
if ((val >= kmin && val <= kmax) || (count >= rangeColor.Length))
{
float coef = (max - min + 0f) / (kmax - kmin);
float num = (val - kmin) * coef + min;
if ((val <= (kmax + 1) && val >= (kmax - 1)) || num > max)
{
num = max;
}
else if (val < rangeColor[2])
{
num = rangeColor[0];
}
return num;
}
}
return val;
}
public float TransformStateToAngle(int val)
{
if (_arrow == null) Init();
float newstate = val;
int count = 0;
for (int i = 0; i < rangeColor.Length / 5; i++)
{
int min = rangeColor[count++];
int max = rangeColor[count++];
int kmin = rangeColor[count++];
int kmax = rangeColor[count++];
count++;
if ((val >= min && val <= max)) //|| (count >= rangeColor.Length)
{
float coef = (kmax - kmin + 0f) / (max - min);
newstate = (val - min) * coef + kmin;
if ((val <= (max + 1) && val >= (max - 1)) || newstate > kmax)
{
newstate = kmax;
}
else if (val < rangeColor[2])
{
newstate = rangeColor[0];
}
}
}
return newstate;
}
public void setState(float newstate) {
_arrow.currentAngle = newstate;
ChangeNumber();
}
}