Files
2023-06-05 23:22:41 +05:00

120 lines
4.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Zenject;
using UltraFace;
using TMPro;
public class SettingsUI : MonoBehaviour
{
[Inject] private Visualizer _visualizer;
[Inject] private DetectionSetup _detectionSetup;
[SerializeField] private bool _initOnStart;
[SerializeField] private TMP_Text _faceCounterText;
[SerializeField] private TMP_Text _minFaceHeight;
[SerializeField] private TMP_Text _maxFaceHeight;
[SerializeField] private Button _acceptButton;
[SerializeField] private Slider _maskSlider;
[SerializeField] private RectTransform _mask;
[SerializeField] private Transform _firstCamera;
[SerializeField] private Slider _firstCameraX;
[SerializeField] private Slider _firstCameraY;
[SerializeField] private Slider _firstCameraZ;
[SerializeField] private Transform _secondCamera;
[SerializeField] private Slider _secondCameraX;
[SerializeField] private Slider _secondCameraY;
[SerializeField] private Slider _secondCameraZ;
private void Start()
{
_maskSlider.minValue = _mask.rect.width - (_mask.rect.width / 2);
_maskSlider.maxValue = _mask.rect.width + (_mask.rect.width / 2);
_maskSlider.value = _mask.rect.width;
_maskSlider.onValueChanged.AddListener(value =>
{
_mask.sizeDelta = new Vector2(value, _mask.rect.height);
});
if(_initOnStart)
{
OnAcceptButtonClick(0.11f);
return;
}
_acceptButton.onClick.AddListener(() => OnAcceptButtonClick());
_firstCameraX.value = _firstCamera.localPosition.x;
_firstCameraX.minValue = _firstCamera.localPosition.x - 5f;
_firstCameraX.maxValue = _firstCamera.localPosition.x + 5f;
_firstCameraX.onValueChanged
.AddListener(value => _firstCamera.localPosition = new Vector3(value, _firstCamera.localPosition.y, _firstCamera.localPosition.z));
_firstCameraY.value = _firstCamera.localPosition.y;
_firstCameraY.minValue = _firstCamera.localPosition.y - 5f;
_firstCameraY.maxValue = _firstCamera.localPosition.y + 5f;
_firstCameraY.onValueChanged
.AddListener(value => _firstCamera.localPosition = new Vector3(_firstCamera.localPosition.x, value, _firstCamera.localPosition.z));
_firstCameraZ.value = _firstCamera.localPosition.z;
_firstCameraZ.minValue = _firstCamera.localPosition.z - 5f;
_firstCameraZ.maxValue = _firstCamera.localPosition.z + 5f;
_firstCameraZ.onValueChanged
.AddListener(value => _firstCamera.localPosition = new Vector3(_firstCamera.localPosition.x, _firstCamera.localPosition.y, value));
_secondCameraX.value = _secondCamera.localPosition.x;
_secondCameraX.minValue = _secondCamera.localPosition.x - 5f;
_secondCameraX.maxValue = _secondCamera.localPosition.x + 5f;
_secondCameraX.onValueChanged
.AddListener(value => _secondCamera.localPosition = new Vector3(value, _secondCamera.localPosition.y, _secondCamera.localPosition.z));
_secondCameraY.value = _secondCamera.localPosition.y;
_secondCameraY.minValue = _secondCamera.localPosition.y - 5f;
_secondCameraY.maxValue = _secondCamera.localPosition.y + 5f;
_secondCameraY.onValueChanged
.AddListener(value => _secondCamera.localPosition = new Vector3(_secondCamera.localPosition.x, value, _secondCamera.localPosition.z));
_secondCameraZ.value = _secondCamera.localPosition.z;
_secondCameraZ.minValue = _secondCamera.localPosition.z - 5f;
_secondCameraZ.maxValue = _secondCamera.localPosition.z + 5f;
_secondCameraZ.onValueChanged
.AddListener(value => _secondCamera.localPosition = new Vector3(_secondCamera.localPosition.x, _secondCamera.localPosition.y, value));
}
private void OnAcceptButtonClick(float min = -1f)
{
if(min == -1f) _detectionSetup.SetMinFaceHeight(float.Parse(_maxFaceHeight.text));
else _detectionSetup.SetMinFaceHeight(min);
_visualizer.Init();
FindAnyObjectByType<MirrorAnimationHandler>().Init();
Destroy(this.gameObject);
}
private void Update()
{
var faceCounter = _visualizer.faceCounter;
var minFaceHeight = _visualizer.minFaceHeight;
var maxFaceHeight = _visualizer.maxFaceHeight;
_faceCounterText.text = faceCounter.ToString();
_faceCounterText.color = faceCounter != 0 ? Color.green : Color.red;
_minFaceHeight.text = minFaceHeight.ToString("0.00");
_maxFaceHeight.text = maxFaceHeight.ToString("0.00");
}
private void OnDestroy()
{
_acceptButton.onClick.RemoveAllListeners();
}
}