Health/Assets/Scripts/YogaManager.cs

169 lines
5.5 KiB
C#
Raw Normal View History

2023-11-07 13:55:35 +00:00
using NUnit.Framework.Internal;
using OpenCVForUnity.CoreModule;
using OpenCVForUnity.DnnModule;
using OpenCVForUnity.UnityUtils;
using OpenCVForUnity.UnityUtils.Helper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
2023-11-12 15:09:33 +00:00
using System.Runtime.CompilerServices;
2023-11-07 13:55:35 +00:00
using UnityEngine;
2023-11-10 07:17:23 +00:00
using UnityEngine.UI;
using Yoga;
2023-11-07 13:55:35 +00:00
public class YogaManager : MonoSingleton<YogaManager>
{
private List<Point> _points = new List<Point>();
2023-11-23 07:22:09 +00:00
private Mat _rgbaMat;
public Mat RgbaMat
{
get
{
return _rgbaMat;
}
set => _rgbaMat = value;
}
private List<Point> _currPersonPoints = new List<Point>();
public List<Point> CurrPersonPoints { get => _currPersonPoints; set => _currPersonPoints = value; }
private List<float[]> _voloResult = new List<float[]>();
public List<float[]> VoloResult { get => _voloResult; set => _voloResult = value; }
2023-11-07 13:55:35 +00:00
2023-11-12 15:09:33 +00:00
public List<Point> Points { get => _points; set => _points = value; }
2023-11-07 13:55:35 +00:00
2023-11-07 16:51:42 +00:00
private int _currentActionCount; //当前动作计数
2023-11-10 07:17:23 +00:00
public int CurrentActionCount { get => _currentActionCount; }
2023-11-07 16:51:42 +00:00
private int _currentSuccessActionCount; //当前成功动作计数
2023-11-07 17:51:34 +00:00
public int CurrentSuccessActionCount { get => _currentSuccessActionCount; }
2023-11-10 07:17:23 +00:00
public int MaxActionCount => Action.MaxActionCount;
2023-11-07 17:51:34 +00:00
2023-11-12 15:09:33 +00:00
private int _actionIndex = -1; //用户选择界面选择的动作索引
2023-11-07 16:51:42 +00:00
public int ActionIndex { get => _actionIndex; internal set => _actionIndex = value; }
private Dictionary<AvatarAction, PoseBase> _actions = new Dictionary<AvatarAction, PoseBase>();
2023-11-07 13:55:35 +00:00
2023-11-10 07:17:23 +00:00
2023-11-15 08:10:56 +00:00
public YogaData Action
{
get => _action;
set => _action = value;
}
public Estimator CurrentEstimator { get => currentEstimator; internal set => currentEstimator = value; }
2023-11-14 10:52:43 +00:00
private YogaData _action = null;
2023-11-15 08:10:56 +00:00
private Estimator currentEstimator;
2023-11-10 07:17:23 +00:00
2023-11-07 15:28:19 +00:00
public void InitData()
2023-11-07 13:55:35 +00:00
{
2023-11-07 16:51:42 +00:00
_actions[AvatarAction.HeadTurnLeft] = new HeadTurnLeft();
_actions[AvatarAction.HeadTurnRight] = new HeadTurnRight();
2023-11-10 07:17:23 +00:00
_actions[AvatarAction.HeadTurnUp] = new HeadTurnUp();
_actions[AvatarAction.HeadTurnDown] = new HeadTurnDown();
_actions[AvatarAction.HandsUp] = new HandsUp();
_actions[AvatarAction.HandsHold] = new HandsHold();
2023-11-07 13:55:35 +00:00
2023-11-07 15:28:19 +00:00
_currentActionCount = 0;
_currentSuccessActionCount = 0;
2023-11-07 17:51:34 +00:00
2023-11-10 07:17:23 +00:00
//根据用户选择的动作索引,获取相应的资源
Action = YogaDataLoader.LoadData(ActionIndex);
2023-11-14 10:52:43 +00:00
EventManager.Instance.Dispatch(YogaEventType.UpdateProgress, CurrentSuccessActionCount, CurrentActionCount);
2023-11-07 13:55:35 +00:00
}
2023-11-07 17:51:34 +00:00
private void OnEnable()
{
2023-11-12 15:09:33 +00:00
EventManager.Instance.AddEventListener(YogaEventType.ActionSuccess, OnActionSuccess);
EventManager.Instance.AddEventListener(YogaEventType.ActionFailed, OnActionFailed);
2023-11-07 17:51:34 +00:00
}
private void OnDisable()
{
2023-11-12 15:09:33 +00:00
EventManager.Instance.RemoveEventListener(YogaEventType.ActionSuccess, OnActionSuccess);
EventManager.Instance.RemoveEventListener(YogaEventType.ActionFailed, OnActionFailed);
2023-11-07 17:51:34 +00:00
}
private void OnActionSuccess()
{
_currentSuccessActionCount++;
_currentActionCount++;
2023-11-10 07:17:23 +00:00
UpdateData();
2023-11-07 17:51:34 +00:00
}
private void OnActionFailed()
{
_currentActionCount++;
2023-11-10 07:17:23 +00:00
UpdateData();
}
private void UpdateData()
{
//如果达到最大数,则停止检测,跳转到下一个动作/下一个动作引导/奖励界面
if (CurrentActionCount >= MaxActionCount)
{
//跳转到下一个动作/下一个动作引导/奖励界面
UIManager.Instance.CloseCurrent(); //关闭当前界面并停止检测
//args[0] = successCount args[1] = totalCount
UIManager.Instance.ShowPanel<ClearingSettlementUI>(false, CurrentSuccessActionCount, MaxActionCount);
}
//args[0] = successCount args[1] = excutedCount args[2] = totalCount
2023-11-12 15:09:33 +00:00
EventManager.Instance.Dispatch(YogaEventType.UpdateProgress, CurrentSuccessActionCount, CurrentActionCount, MaxActionCount);
2023-11-07 17:51:34 +00:00
}
2023-11-07 16:51:42 +00:00
public bool IsCorrectAction(List<Point> personPoints, AvatarAction actionType)
2023-11-07 13:55:35 +00:00
{
2023-11-07 16:51:42 +00:00
if (!_actions.ContainsKey(actionType))
2023-11-07 13:55:35 +00:00
{
2023-11-24 05:13:10 +00:00
LogPrint.Error("ActionType is not exist");
2023-11-07 16:51:42 +00:00
return false;
2023-11-07 13:55:35 +00:00
}
2023-11-07 16:51:42 +00:00
var result = _actions[actionType].CheckPose(personPoints);
return result;
2023-11-07 13:55:35 +00:00
}
2023-11-12 15:09:33 +00:00
public bool ActionCheckPoints(List<Point> points)
{
if (points == null || points.Count == 0)
{
2023-11-24 05:13:10 +00:00
//LogPrint.Log("ActionCheckPoints points is null");
2023-11-12 15:09:33 +00:00
return false;
}
2023-11-13 02:53:34 +00:00
2023-11-14 10:52:43 +00:00
//for (int i = 0; i < points.Count; i++)
//{
// Point p = points[i];
// if (p != new Point(-1, -1))
2023-11-24 05:13:10 +00:00
// LogPrint.Warning($"ActionPoints p({i}): {i.tagName()}, value: {p.x},{p.y}");
2023-11-14 10:52:43 +00:00
//}
2023-11-13 02:53:34 +00:00
foreach (var p in Action.MustPoints)
2023-11-12 15:09:33 +00:00
{
2023-11-13 02:53:34 +00:00
if (!p.IsValid(points)) //有一个点不符合 返回false
{
2023-11-24 05:13:10 +00:00
//LogPrint.Error($"ActionCheckPoints failed, {p} is not valid");
2023-11-12 15:09:33 +00:00
return false;
2023-11-15 08:10:56 +00:00
}
2023-11-12 15:09:33 +00:00
}
2023-11-13 06:43:34 +00:00
//不设的情况下,不检测
if (Action.AnyPoints != null && Action.AnyPoints.Count > 0)
{
foreach (var p in Action.AnyPoints)
{
if (p.IsValid(points)) //有一个点符合 返回true
{
return true;
}
}
return false; //没有一个点符合 返回false
}
2023-11-12 15:09:33 +00:00
return true;
}
2023-11-15 08:10:56 +00:00
}