Files
MI-38/Heli_with_panels/Assets/MFI/mfiClockIndicator.cs
2022-07-04 13:15:35 +03:00

67 lines
2.1 KiB
C#
Raw 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;
public class mfiClockIndicator : MonoBehaviour
{
public int minutesInHour;
public int hoursInCircle;
private string formatText = "{0}";
private TextMeshPro _number;
private Handler _hArrow=null, _mArrow=null;
private int curH, curM;
void Awake()
{
Init();
//ChangeNumber();
}
void Init() // так надо. Awake не всегда вызывается до начала работы с объектом
{
_number = GetComponent<TextMeshPro>();
Handler[] handles = this.GetComponentsInChildren<Handler>();
for (int i = 0; i < handles.Length; i++)
{
if (handles[i].gameObject.name.EndsWith("_h"))
_hArrow = handles[i];
if (handles[i].gameObject.name.EndsWith("_m"))
_mArrow = handles[i];
}
curH = curM = 0;
}
/* private void ChangeIndicator(float val)
{
float num = TransformAngleToState(val);
_number.text = string.Format(formatText, (int)Mathf.Round(num));
}
public void ChangeNumber()
{
if (_hArrow == null) Init();
ChangeIndicator(_arrow.currentAngle);
}*/
public int TransformHourAngleToState(float val)
{
if (val < 0) val += 360f;
int res = (int)Mathf.Round((val / 360f) * (minutesInHour * hoursInCircle));
return res;
}
public int TransformMinutesAngleToState(float val)
{
if (val < 0) val += 360f;
int res = (int)Mathf.Round((val / 360f) * minutesInHour + curH* minutesInHour);
return res;
}
public void setState(int newstate) {
newstate %= minutesInHour * hoursInCircle;
curH = newstate / minutesInHour;
curM = newstate % minutesInHour;
_mArrow.SetCurAngle(180f * (curM / (minutesInHour / 2f)));
_hArrow.SetCurAngle(180f * (newstate / (minutesInHour * hoursInCircle / 2f)));
_number.text = string.Format(formatText, newstate);
}
}