powerfun-unity/Assets/Scripts/UI/Prefab/Panel/DeviceController.cs
2021-10-13 19:03:22 +08:00

230 lines
7.4 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Assets.Scripts.Devices.Ant;
using System;
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;
using Assets.Scripts.Devices.Ble.Devices;
using Assets.Scripts;
using System.IO;
public class DeviceController : PFUIPanel
{
private float timer = 1.0f;
private Button mReturnBtn;
private Image antStatus;
private Sprite ant0;
private Sprite ant1;
private Sprite circuloCarga;
private Sprite circuloCompleto;
RectTransform circulo;
private GameObject statusText;
private Image bleStatus;
private Sprite ble0;
private Sprite ble1;
RectTransform bleCirculo;
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");
}
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");
}
}
}
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);
}
}
}
protected override void Awake()
{
base.Awake();
#if UNITY_STANDALONE_WIN
Debug.Log("进入设备连接页面");
if (!File.Exists("C:\\Windows\\System32\\msvcp140.dll") && File.Exists("C:\\Program Files\\PowerFun\\_CommonRedist\\vcredist\\VC_redist.x64.exe"))
{
Utils.ExecFile("C:\\Program Files\\PowerFun\\_CommonRedist\\vcredist\\VC_redist.x64.exe", "/passive");
}
#endif
}
// Start is called before the first frame update
protected override void Start()
{
base.Start();
//Button button = this.transform.Find("Button").GetComponent<Button>();
//button.onClick.AddListener(() =>
//{
// UIManager.ShowHomePanel();
//});
mReturnBtn = this.transform.Find("CloseBtn").GetComponent<Button>();
UIManager.AddEvent(mReturnBtn.gameObject, EventTriggerType.PointerClick, new UnityEngine.Events.UnityAction<BaseEventData>(e =>
{
//UIManager.ShowHomePanel();
//UIManager.CloseModal();
App.MainDeviceAdapter.StopScan();
this.Close();
}));
//UIManager.AddEvent(mReturnBtn.gameObject, EventTriggerType.PointerUp, (e) =>
//{
// Debug.Log("aaaaaaaaaaaaaa");
//});
var bg = this.transform.Find("Status").Find("Bg");
statusText = this.transform.Find("Status").Find("GameObject").gameObject;
statusText.SetActive(false);
antStatus = bg.Find("AntIcon/Ant+").GetComponent<Image>();
circulo = bg.Find("AntIcon/Image").GetComponent<RectTransform>();
//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");
ant1 = Resources.Load<Sprite>("Images/ANT+_2");
circuloCarga = Resources.Load<Sprite>("Images/icon-circulo-carga");
circuloCompleto = Resources.Load<Sprite>("Images/icon-circulo-completo");
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");
base.SetRounded(bg, 64);
//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);
// //}
//});
}
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
if (timer <= 0)
{
Available = App.MainDeviceAdapter.GetState(Assets.Scripts.Devices.ConnectionInterface.ANT) == Assets.Scripts.Devices.DeviceAdapterState.On;
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());
timer = 1.0f;
}
}
public override void Show()
{
base.Show();
App.MainDeviceAdapter.ClearDevice();
#if UNITY_ANDROID || UNITY_IOS
App.MainDeviceAdapter.StartScan();
#if UNITY_ANDROID
Utils.CallAndroidMethod("OpenLocationService");
#else
//if (!App.weChatController.CheckLocation())
//{
// Utils.showToast(null, "Please turn on location service");
//}
#endif
#endif
transform.MyDOFade();
}
protected override void OnDisable()
{
}
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);
});
}
}