powerfun-unity/Assets/Scripts/UI/Prefab/Panel/DeviceController.cs

212 lines
6.7 KiB
C#
Raw Normal View History

2021-03-30 14:23:41 +08:00
using Assets.Scripts.Devices.Ant;
using System;
2021-03-29 09:10:59 +08:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using DG.Tweening;
2021-06-04 10:35:58 +08:00
using Assets.Scripts.Devices.Ble.Devices;
2021-03-29 09:10:59 +08:00
2021-03-30 14:23:41 +08:00
public class DeviceController : PFUIPanel
2021-03-29 09:10:59 +08:00
{
2021-03-30 14:23:41 +08:00
private float timer = 1.0f;
2021-04-01 14:29:58 +08:00
private Button mReturnBtn;
private Image antStatus;
private Sprite ant0;
private Sprite ant1;
2021-04-23 09:22:12 +08:00
private Sprite circuloCarga;
private Sprite circuloCompleto;
RectTransform circulo;
private GameObject statusText;
2021-06-04 10:35:58 +08:00
private Image bleStatus;
private Sprite ble0;
private Sprite ble1;
RectTransform bleCirculo;
2021-04-23 09:22:12 +08:00
private bool _available = false;
private bool Available
{
get
{
return _available;
}
set
{
if (value == _available)
return;
_available = value;
if (_available)
{
antStatus.sprite = ant1;
//circulo.gameObject.SetActive(false);
DOTween.Kill(circulo, false);
circulo.GetComponent<Image>().sprite = circuloCompleto;
ShowStatusText("ANT+ Connected");
2021-04-23 09:22:12 +08:00
}
else
{
antStatus.sprite = ant0;
//circulo.gameObject.SetActive(true);
circulo.GetComponent<Image>().sprite = circuloCarga;
circulo.DOLocalRotate(new Vector3(0, 0, 360), 1f, RotateMode.FastBeyond360).SetEase(Ease.Linear).SetLoops(-1);
ShowStatusText("ANT+ Disconnect");
2021-04-23 09:22:12 +08:00
}
}
}
2021-06-04 10:35:58 +08:00
private bool _bleAvailable = false;
private bool BleAvailable
{
get
{
return _bleAvailable;
}
set
{
if (value == _bleAvailable)
return;
_bleAvailable = value;
if (_bleAvailable)
{
bleStatus.sprite = ble1;
DOTween.Kill(bleCirculo, false);
bleCirculo.GetComponent<Image>().sprite = circuloCompleto;
}
else
{
bleStatus.sprite = ble0;
bleCirculo.GetComponent<Image>().sprite = circuloCarga;
bleCirculo.DOLocalRotate(new Vector3(0, 0, 360), 1f, RotateMode.FastBeyond360).SetEase(Ease.Linear).SetLoops(-1);
}
}
}
2021-03-30 14:23:41 +08:00
protected override void Awake()
{
2021-04-14 15:02:33 +08:00
base.Awake();
2021-06-04 10:35:58 +08:00
//Debug.Log("device awake");
2021-03-30 14:23:41 +08:00
}
2021-03-29 09:10:59 +08:00
// Start is called before the first frame update
2021-03-30 14:23:41 +08:00
protected override void Start()
2021-03-29 09:10:59 +08:00
{
2021-03-30 14:23:41 +08:00
base.Start();
2021-04-01 09:33:19 +08:00
//Button button = this.transform.Find("Button").GetComponent<Button>();
//button.onClick.AddListener(() =>
//{
// UIManager.ShowHomePanel();
//});
2021-03-30 14:23:41 +08:00
2021-04-14 15:02:33 +08:00
mReturnBtn = this.transform.Find("CloseBtn").GetComponent<Button>();
UIManager.AddEvent(mReturnBtn.gameObject, EventTriggerType.PointerClick, new UnityEngine.Events.UnityAction<BaseEventData>(e =>
2021-04-01 14:29:58 +08:00
{
2021-04-14 15:02:33 +08:00
//UIManager.ShowHomePanel();
2021-04-15 17:09:35 +08:00
//UIManager.CloseModal();
this.Close();
2021-04-01 14:29:58 +08:00
}));
2021-04-19 14:36:08 +08:00
//UIManager.AddEvent(mReturnBtn.gameObject, EventTriggerType.PointerUp, (e) =>
//{
// Debug.Log("aaaaaaaaaaaaaa");
//});
2021-06-04 10:35:58 +08:00
var bg = this.transform.Find("Status").Find("Bg");
statusText = this.transform.Find("Status").Find("GameObject").gameObject;
statusText.SetActive(false);
2021-06-04 10:35:58 +08:00
antStatus = bg.Find("AntIcon/Ant+").GetComponent<Image>();
circulo = bg.Find("AntIcon/Image").GetComponent<RectTransform>();
2021-04-23 09:22:12 +08:00
//circulo.DORotate(new Vector3(0, 0, 360), 1.5f, RotateMode.Fast).SetLoops(-1);
circulo.DOLocalRotate(new Vector3(0, 0, 360), 1f, RotateMode.FastBeyond360).SetEase(Ease.Linear).SetLoops(-1);
ant0 = Resources.Load<Sprite>("Images/ANT+_0");
2021-04-19 14:36:08 +08:00
ant1 = Resources.Load<Sprite>("Images/ANT+_2");
2021-04-23 09:22:12 +08:00
circuloCarga = Resources.Load<Sprite>("Images/icon-circulo-carga");
circuloCompleto = Resources.Load<Sprite>("Images/icon-circulo-completo");
2021-04-14 15:02:33 +08:00
2021-06-04 10:35:58 +08:00
bleStatus = bg.Find("BleIcon/Ble").GetComponent<Image>();
bleCirculo = bg.Find("BleIcon/Image").GetComponent<RectTransform>();
bleCirculo.DOLocalRotate(new Vector3(0, 0, 360), 1f, RotateMode.FastBeyond360).SetEase(Ease.Linear).SetLoops(-1);
ble0 = Resources.Load<Sprite>("Images/Bluetooth_0");
ble1 = Resources.Load<Sprite>("Images/Bluetooth_2");
2021-04-19 14:36:08 +08:00
base.SetRounded(bg, 64);
2021-06-04 10:35:58 +08:00
//var text = this.transform.Find("InputField").GetComponent<InputField>();
//var btn = this.transform.Find("Button").GetComponent<Button>();
//btn.onClick.AddListener(() =>
//{
// //Debug.Log(text.text);
// var device = App.MainDeviceAdapter.GetDevices().FirstOrDefault(d => d.State == DeviceState.Connected && d.Sensor == SensorType.HeartRate);
// if(device == null)
// {
// return;
// }
// //(device as HeartRate).GetBatteryLevel();
// //(device as Tacx).SetResistanceMode(int.Parse(text.text));
// //if (device is Tacx)
// //{
// // (device as Tacx).SetTrackResistance(double.Parse(text.text));
// //}
// //else if(device is FitDevice)
// //{
// // (device as FitDevice).SetErgMode(100* 4);
// //}
//});
2021-03-29 09:10:59 +08:00
}
2021-04-14 15:02:33 +08:00
2021-03-29 09:10:59 +08:00
// Update is called once per frame
void Update()
{
2021-03-30 14:23:41 +08:00
timer -= Time.deltaTime;
if (timer <= 0)
{
2021-05-19 14:38:48 +08:00
Available = App.MainDeviceAdapter.GetState(Assets.Scripts.Devices.ConnectionInterface.ANT) == Assets.Scripts.Devices.DeviceAdapterState.On;
2021-06-04 10:35:58 +08:00
BleAvailable = App.MainDeviceAdapter.GetState(Assets.Scripts.Devices.ConnectionInterface.BLE) == Assets.Scripts.Devices.DeviceAdapterState.On;
//Debug.Log("ble available:" + App.MainDeviceAdapter.GetState(Assets.Scripts.Devices.ConnectionInterface.BLE).ToString());
2021-03-30 14:23:41 +08:00
timer = 1.0f;
}
2021-03-29 09:10:59 +08:00
}
2021-04-01 09:33:19 +08:00
public override void Show()
{
var cg = this.GetComponent<CanvasGroup>();
cg.alpha = 0;
base.Show();
cg.DOFade(1f, 0.3f);
}
2021-04-23 09:22:12 +08:00
protected override void OnDisable()
2021-04-01 09:33:19 +08:00
{
2021-04-23 09:22:12 +08:00
2021-04-01 09:33:19 +08:00
}
private void ShowStatusText(string text)
{
statusText.transform.Find("Text").GetComponent<Text>().text = text;
statusText.GetComponent<CanvasGroup>().alpha = 0;
statusText.SetActive(true);
statusText.GetComponent<CanvasGroup>().DOFade(1f, 0.1f).OnComplete(()=> {
statusText.GetComponent<CanvasGroup>().DOFade(0f, 0.3f).SetDelay(3f);
});
}
2021-03-29 09:10:59 +08:00
}