231 lines
7.8 KiB
C#
231 lines
7.8 KiB
C#
using Assets.Scripts.Devices.Ant;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
using Assets.Scripts.Devices.Ant.Interfaces;
|
|
using Assets.Scripts.UI.Control;
|
|
using Assets.Scripts;
|
|
|
|
public class ConnectDeviceModal : PFUIPanel
|
|
{
|
|
private float timer = 1.0f;
|
|
private SensorType mSensorType = SensorType.None;
|
|
public SensorType SensorType {
|
|
get {
|
|
return mSensorType;
|
|
}
|
|
set
|
|
{
|
|
Debug.Log(value);
|
|
mSensorType = value;
|
|
var deviceInfoStr = Utils.deviceDict[value.ToString()];
|
|
if (deviceInfoStr != null)
|
|
{
|
|
var deviceInfo = deviceInfoStr.Split('|');
|
|
if (deviceInfo.Length == 2)
|
|
{
|
|
Title.text = deviceInfo[0];
|
|
Logo.sprite = Resources.Load<Sprite>(deviceInfo[1]);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
private Image Logo;
|
|
private UnityEngine.Object deviceItem;
|
|
private VerticalLayoutGroup content;
|
|
private PfUIButton connectBtn;
|
|
private Dictionary<ushort, DeviceItem> deviceList;
|
|
private Text noDevice,Title;
|
|
private RectTransform searchIconRect;
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
|
|
deviceList = new Dictionary<ushort, DeviceItem>();
|
|
|
|
var container = this.transform.Find("GameObject");
|
|
var closeBtn = container.Find("CloseBtn");
|
|
Logo = container.Find("Logo").GetComponent<Image>();
|
|
Title = container.Find("Title").GetComponent<Text>();
|
|
var panel = container.Find("Panel");
|
|
content = panel.Find("Scroll View").Find("Viewport").Find("Content").GetComponent<VerticalLayoutGroup>();
|
|
|
|
noDevice = container.Find("NoDevice").GetComponent<Text>();
|
|
|
|
searchIconRect = container.Find("SearchIcon").GetComponent<RectTransform>();
|
|
_x = searchIconRect.localPosition.x;
|
|
_y = searchIconRect.localPosition.y;
|
|
|
|
base.SetRounded(panel.GetComponent<Image>().transform, 20f);
|
|
|
|
UIManager.AddEvent(closeBtn.gameObject, EventTriggerType.PointerClick, new UnityEngine.Events.UnityAction<BaseEventData>(e =>
|
|
{
|
|
//this.gameObject.SetActive(false);
|
|
this.Close();
|
|
}));
|
|
|
|
deviceItem = Resources.Load("UI/Prefab/Device/DeviceItem");
|
|
|
|
connectBtn = container.Find("ConnectBtn").GetComponent<PfUIButton>();
|
|
|
|
UIManager.AddEvent(connectBtn.gameObject, EventTriggerType.PointerClick, new UnityEngine.Events.UnityAction<BaseEventData>(e =>
|
|
{
|
|
foreach (var item in deviceList)
|
|
{
|
|
if (item.Value.GetStatus() == false)
|
|
{
|
|
if (item.Value.DeviceInfo.Sensor == SensorType)
|
|
{
|
|
item.Value.DeviceInfo.Disconnect();
|
|
}
|
|
}
|
|
}
|
|
var dd = deviceList.Select(d => d.Value).Where(d => d.GetStatus()).FirstOrDefault();
|
|
if(dd != null)
|
|
{
|
|
dd.DeviceInfo.Connect();
|
|
}
|
|
this.Close();
|
|
}));
|
|
|
|
|
|
}
|
|
|
|
// Start is called before the first frame update
|
|
protected override void Start()
|
|
{
|
|
SearchDevice();
|
|
}
|
|
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
timer -= Time.deltaTime;
|
|
if(timer <= 0)
|
|
{
|
|
SearchDevice();
|
|
timer = 1.0f;
|
|
}
|
|
|
|
Move();
|
|
}
|
|
|
|
private void SearchDevice()
|
|
{
|
|
if (SensorType != SensorType.None)
|
|
{
|
|
//Debug.Log(AntConnector.Instance().discoveredDevices.Count(d => d.Sensor == SensorType));
|
|
|
|
var devices = new List<AbstractAntDevice>();
|
|
switch (SensorType)
|
|
{
|
|
case SensorType.Cadence:
|
|
devices = AntConnector.Instance().discoveredDevices.Where(d => d.Sensor == SensorType || (d is ICadenceDevice && d.State == DeviceState.Connected)).OrderBy(d=>d.Priority).ToList();
|
|
break;
|
|
case SensorType.HeartRate:
|
|
devices = AntConnector.Instance().discoveredDevices.Where(d => d.Sensor == SensorType || (d is IHeartRateDevice && d.State == DeviceState.Connected)).OrderBy(d => d.Priority).ToList();
|
|
break;
|
|
case SensorType.Power:
|
|
devices = AntConnector.Instance().discoveredDevices.Where(d => d.Sensor == SensorType || (d is IPowerDevice && d.State == DeviceState.Connected)).OrderBy(d=>d.Priority).ToList();
|
|
break;
|
|
case SensorType.Speed:
|
|
break;
|
|
case SensorType.SpeedCadence:
|
|
devices = AntConnector.Instance().discoveredDevices.Where(d => d.Sensor == SensorType || (d is ISpeedDevice && d.State == DeviceState.Connected)).OrderBy(d => d.Priority).ToList();
|
|
break;
|
|
case SensorType.Trainer:
|
|
devices = AntConnector.Instance().discoveredDevices.Where(d => d.Sensor == SensorType).ToList();
|
|
break;
|
|
case SensorType.VirtualPower:
|
|
break;
|
|
}
|
|
|
|
foreach (var device in devices)
|
|
{
|
|
if (deviceList.ContainsKey(device.DeviceNumber))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var gameObject = (GameObject)Instantiate(deviceItem);
|
|
gameObject.transform.SetParent(content.transform);
|
|
var deviceItemObj = gameObject.GetComponent<DeviceItem>();
|
|
deviceItemObj.DeviceInfo = device;
|
|
|
|
UIManager.AddEvent(deviceItemObj.gameObject, EventTriggerType.PointerClick, new UnityEngine.Events.UnityAction<BaseEventData>(e =>
|
|
{
|
|
deviceItemObj.Set(true);
|
|
foreach (var item in deviceList)
|
|
{
|
|
if (item.Key != deviceItemObj.DeviceInfo.DeviceNumber)
|
|
{
|
|
item.Value.Set(false);
|
|
}
|
|
}
|
|
}));
|
|
|
|
//UIManager.AddEvent(deviceItemObj.gameObject, EventTriggerType.Deselect, new UnityEngine.Events.UnityAction<BaseEventData>(e =>
|
|
//{
|
|
// //Debug.Log("de select");
|
|
// if(deviceItemObj.DeviceInfo.Sensor == SensorType)
|
|
// {
|
|
// deviceItemObj.DeviceInfo.Disconnect();
|
|
// }
|
|
//}));
|
|
|
|
deviceList.Add(device.DeviceNumber, deviceItemObj);
|
|
}
|
|
|
|
if(deviceList.All(d=>d.Value.GetStatus() == false))
|
|
{
|
|
var firstDevice = deviceList.Select(d => d.Value).Where(d => d.DeviceInfo.State == DeviceState.Connected).OrderByDescending(d => d.DeviceInfo.Priority).FirstOrDefault();
|
|
if (firstDevice != null)
|
|
{
|
|
firstDevice.Set(true);
|
|
|
|
//connectBtn.text.text = "DISCOUNECT";
|
|
noDevice.text = firstDevice.DeviceInfo.Name;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void Close()
|
|
{
|
|
base.Close();
|
|
DestroyImmediate(this.gameObject);
|
|
}
|
|
|
|
|
|
float time = 0;
|
|
float _x = 0;
|
|
float _y = 0;
|
|
void Move()
|
|
{
|
|
var hudu = (2 * Mathf.PI / 360) * 1 * time;
|
|
|
|
var x = _x + Mathf.Sin(hudu) * 8f;
|
|
var y = _y + Mathf.Cos(hudu) * 8f;
|
|
|
|
var rect = searchIconRect.localPosition;
|
|
//rect.x = x;
|
|
//rect.y = y;
|
|
rect.x = x;
|
|
rect.y = y;
|
|
searchIconRect.localPosition = rect;
|
|
|
|
time += 1.8f;
|
|
|
|
//Debug.Log(x + ", " + y);
|
|
|
|
//yield return null;
|
|
}
|
|
}
|