mirror of
https://gitea.msk.dinamika-avia.ru/Constanta-Design/MI-38.git
synced 2026-01-24 01:25:38 +03:00
154 lines
5.1 KiB
C#
154 lines
5.1 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using TMPro;
|
||
using UnityEngine.UI;
|
||
|
||
public class Indicator : MonoBehaviour
|
||
{
|
||
[SerializeField]
|
||
private string[] rangeColor; //массив строк с кратностью 5 {min на шкале (цифрами int), max на шкале (цифрами int), min на слайдере (цифрами int), max на слайдере (цифрами int), цвет периода (по названиям функций ниже)}
|
||
|
||
private SpriteRenderer fill;
|
||
private string formatText = "{0}";
|
||
private TextMeshPro _number;
|
||
private Slider mainSlider;
|
||
[SerializeField]
|
||
private bool _float = false;
|
||
Manager man;
|
||
|
||
void Awake()
|
||
{
|
||
Init();
|
||
ChangeColorIndicator(mainSlider.value);
|
||
}
|
||
void Init()
|
||
{
|
||
_number = GetComponentInChildren<TextMeshPro>();
|
||
mainSlider = GetComponent<Slider>();
|
||
fill = mainSlider.fillRect.GetComponent<SpriteRenderer>();
|
||
man = GameObject.Find("Manager").GetComponent<Manager>();
|
||
}
|
||
|
||
private void ChangeColorIndicator(float val)
|
||
{
|
||
int count = 0;
|
||
|
||
for (int i = 0; i < rangeColor.Length / 5; i++)
|
||
{
|
||
int min;
|
||
int.TryParse(rangeColor[count++], out min);
|
||
int max;
|
||
int.TryParse(rangeColor[count++], out max);
|
||
int kmin;
|
||
int.TryParse(rangeColor[count++], out kmin);
|
||
int kmax;
|
||
int.TryParse(rangeColor[count++], out kmax);
|
||
string col = rangeColor[count++];
|
||
|
||
//Debug.Log($"min = {min}, max = {max}, kmin = {kmin}, kmax = {kmax}, col = {col}");
|
||
|
||
if (val >= kmin && val <= kmax)
|
||
{
|
||
float coef = (max - min+0f) / (kmax - kmin);
|
||
float num = (val - kmin) * coef+min;
|
||
if ((val <= (kmax + 1) && val >= (kmax - 1)) || num > max) num = max;
|
||
|
||
if (_float)//Здесь меняем числа на индикаторе
|
||
{
|
||
var f = num / 10f;
|
||
_number.SetText("{0.0}", f);
|
||
|
||
}
|
||
else
|
||
{
|
||
_number.text = string.Format(formatText, (int)Mathf.Round(num));
|
||
}
|
||
|
||
if (col == "red") fill.color = _number.color = Color.red;
|
||
if (col == "green") fill.color = _number.color = Color.green;
|
||
if (col == "yellow") fill.color = _number.color = Color.yellow;
|
||
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
|
||
//public void RepaintNumberIndicator()
|
||
//{
|
||
// if (mainSlider == null) Init();
|
||
// ChangeColorIndicator(mainSlider.value);
|
||
//}
|
||
public void DragChangeIndicator()
|
||
{
|
||
if (man == null) Init();
|
||
UnityObject thisUO = man.objects.Find(this.gameObject.name);
|
||
if(thisUO != null)
|
||
thisUO.SetObjectState((int)TransformAngleToState(mainSlider.value), thisUO); //
|
||
//ChangeNumberIndicator();
|
||
}
|
||
|
||
public void setState(float newstate) // 0..100
|
||
{
|
||
if (mainSlider == null) Init();
|
||
if (newstate <0f || newstate>100f)
|
||
{
|
||
Debug.LogError("setState " + this.name + " not in 0..100 range: " + newstate);
|
||
return;
|
||
}
|
||
mainSlider.value = newstate;
|
||
ChangeColorIndicator(newstate);
|
||
}
|
||
|
||
public float TransformStateToAngle(float val)
|
||
{
|
||
float newstate = val;
|
||
|
||
int count = 0;
|
||
for (int i = 0; i < rangeColor.Length / 5; i++)
|
||
{
|
||
int min, max, kmin, kmax;
|
||
int.TryParse(rangeColor[count++], out min);
|
||
int.TryParse(rangeColor[count++], out max);
|
||
int.TryParse(rangeColor[count++], out kmin);
|
||
int.TryParse(rangeColor[count++], out kmax);
|
||
count++;
|
||
if ((val >= min && val <= max))
|
||
{
|
||
float coef = (kmax - kmin + 0f) / (max - min);
|
||
newstate = (val - min) * coef + kmin;
|
||
if ((val <= (max + 1) && val >= (max - 1)) || newstate > kmax)
|
||
newstate = kmax;
|
||
break;
|
||
}
|
||
}
|
||
return newstate;
|
||
}
|
||
|
||
public float TransformAngleToState(float val)
|
||
{
|
||
float newstate = val;
|
||
float kval = val;
|
||
|
||
int count = 0;
|
||
for (int i = 0; i < rangeColor.Length / 5; i++)
|
||
{
|
||
int min, max, kmin, kmax;
|
||
int.TryParse(rangeColor[count++], out min);
|
||
int.TryParse(rangeColor[count++], out max);
|
||
int.TryParse(rangeColor[count++], out kmin);
|
||
int.TryParse(rangeColor[count++], out kmax);
|
||
count++;
|
||
if ((val >= kmin && val <= kmax))
|
||
{
|
||
float coef = (max - min + 0f) / (kmax - kmin);
|
||
newstate = (kval - kmin) * coef + min;
|
||
if ((kval <= (kmax + 1) && kval >= (kmax - 1)) || newstate > max)
|
||
newstate = max;
|
||
break;
|
||
}
|
||
}
|
||
return newstate;
|
||
}
|
||
}
|