ict.lixian.single/Assets/Scripts/ROBOT/CustomPanel.cs

833 lines
24 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Newtonsoft.Json;
using System;
using System.IO;
using UnityEngine.EventSystems;
using Unity.VisualScripting;
using DG.Tweening.Plugins.Core.PathCore;
//using static Siccity.GLTFUtility.GLTFAccessor.Sparse;
using static RobotManager;
using UnityEngine.EventSystems;
public class CustomPanel : MonoBehaviour
{
public static CustomPanel instance;
public InputField timeinput;
public List<CustomItem> items;
List<CustomItemData> datas = new List<CustomItemData>();
public Button backBtn;
public Button playBtn;
public Button saveBtn;
public Button addMons;
public Button refreshBtn;
public Button upMoveBtn;
public Button angularReadBackBtn;
public Button deleteBtn;
public Button insertBtn;
public Button downMoveBtn;
public Button handProBtn;
public Button readDeviationBtn;
public Button downloadBiasBtn;
public Button steeringBackCenterBtn;
public Button openActionFileBtn;
public Button inseriesFileBtn;
public Button singleErasureBtn;
public Button allErasureBtn;
public Button actionPlayBtn;
public Button actionStopBtn;
public Transform Content;
public static GameObject _currItem;
public static GameObject robot;
public Toggle cirToggle;
public Toggle msToggle;
public bool isPlayAction;
public Dropdown dropdown;
public Scrollbar actionContentScrollbar;
public Button ResetBtn;
public Text allTime;
// public static int[] actionArray = new int[16];
string path = Application.streamingAssetsPath + "/RobotFiles/Motions";
public static List<CustomItemData> actions;
public Button StopRunBtn;
public List<MotionGroup> all_motions;
public List<GameObject> all_Objects;
public static Sprite _currItemSprite;
public static Sprite defSprite;
Vector3 robotPos;
Quaternion robotRota;
private void Awake()
{
robot.GetComponent<Robot>().Isflag = true;
instance = this;
var str = Resources.Load<TextAsset>("stand").text;
datas = JsonConvert.DeserializeObject<List<CustomItemData>>(str);
_currItemSprite = Resources.Load<Sprite>("Prefabs/RobotUI/Select_Item");
defSprite = Resources.Load<Sprite>("Prefabs/RobotUI/defSprite");
}
private void OnEnable()
{
isPlayAction = false;
robot.SetActive(true);
Stant();
}
void Start()
{
robotPos = robot.transform.position;
robotRota = robot.transform.rotation;
all_motions = new List<MotionGroup>();
all_Objects = new List<GameObject>();
actions = new List<CustomItemData>();
backBtn.onClick.AddListener(ClosePanel);
playBtn.onClick.AddListener(PlayAction);
saveBtn.onClick.AddListener(SvaeAction);
addMons.onClick.AddListener(AddMons);
refreshBtn.onClick.AddListener(RefreshBtn);
upMoveBtn.onClick.AddListener(UpMoveActon);
deleteBtn.onClick.AddListener(DeleteAction);
insertBtn.onClick.AddListener(InsertAction);
downMoveBtn.onClick.AddListener(DownMoveAction);
openActionFileBtn.onClick.AddListener(OpenActionFile);
inseriesFileBtn.onClick.AddListener(InseriesFile);
actionPlayBtn.onClick.AddListener(ActionPlay);
singleErasureBtn.onClick.AddListener(SingleErasure);
allErasureBtn.onClick.AddListener(AllErasure);
actionStopBtn.onClick.AddListener(StopAction);
StopRunBtn.onClick.AddListener(StopRun);
ResetBtn.onClick.AddListener(ResetRobot);
cirToggle.onValueChanged.AddListener(x =>
{
if (x == false)
{
cirToggle.isOn = false;
}
});
OnRefreshOptions();
//<2F><><EFBFBD><EFBFBD>Ƶ<EFBFBD>
roudPoint = new Vector3(robot.transform.position.x, RobotManager.instance.robotTempCamera.transform.position.y, robot.transform.position.z);
}
void asd()
{
}
void Stant()
{
for (int i = 0; i < items.Count; i++)
{
items[i].GetComponent<CustomItem>().Init(datas[0], items.Count - i);
}
}
//<2F><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>״̬
private void ResetRobot()
{
Stant();
robot.transform.position = robotPos;
robot.transform.rotation = robotRota;
}
/// <summary>
/// ˢ<><CBA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
/// </summary>
private void OnRefreshOptions()
{
//dropdown.ClearOptions();
dropdown.options.Clear();
dropdown.ClearOptions();
dropdown.value = 0;
var fileNames = GetFilesNmae();
if (fileNames.Count > 0)
{
dropdown.AddOptions(fileNames);
CurAction(0);
dropdown.onValueChanged.AddListener(CurAction);
}
}
/// <summary>
/// ɾ<><C9BE>ȫ<EFBFBD><C8AB>
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void AllErasure()
{
if (Directory.Exists(path))
{
DirectoryInfo direction = new DirectoryInfo(path);
FileInfo[] files = direction.GetFiles("*.txt");
for (int i = 0; i < files.Length; i++)
{
File.Delete(path + "/" + files[i].Name);
}
OnRefreshOptions();
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD>ֹͣ
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void StopAction()
{
if (isPlayAction)
{
isPlayAction = false;
robot.GetComponent<Robot>().ClearQueue();
actionContentScrollbar.interactable = true;
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void SingleErasure()
{
if (dropdown.options.Count > 0)
{
string filepath = path + "/" + dropdown.options[dropdown.value].text;
if (File.Exists(filepath))
{
File.Delete(filepath);
OnRefreshOptions();
Debug.Log(<><C9BE><EFBFBD>ɹ<EFBFBD>");
}
}
}
List<Motion> motions;
private void CurAction(int arg0)
{
if (dropdown.options.Count > 0)
{
motions = new List<Motion>();
string name = dropdown.options[arg0].text.ToString();
string actionPath = path + "/" + name;
string str = File.ReadAllText(actionPath);
List<CustomItemData> cus = JsonConvert.DeserializeObject<List<CustomItemData>>(str);
for (int i = 0; i < cus.Count; i++)
{
RobotMotorValues values = new RobotMotorValues();
values.index = cus[i].index;
values.time = cus[i].time;
List<int> list = new List<int>();
for (int j = 0; j < cus[i].values.Count; j++)
{
list.Add(cus[i].values[j]);
}
values.values = list.ToArray();
motions.Add(new Motion(values));
}
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public void ActionPlay()
{
if (motions != null && motions.Count > 0)
{
StopRunBtn.gameObject.SetActive(false);
playBtn.gameObject.SetActive(true);
isPlayAction = true;
robot.GetComponent<Robot>().ClearQueue();
robot.GetComponent<Robot>().UpdateMotions(motions);
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD>
/// </summary>
public void PlayAction()
{
actionContentScrollbar.interactable = false;
AddMotion();
if (motion != null && motion.Count > 0)
{
isPlayAction = true;
robot.GetComponent<Robot>().ClearQueue();
float time = 0;
for (int i = 0; i < motion.Count; i++)
{
time += motion[i].duration_time;
}
allTime.text = time.ToString();
robot.GetComponent<Robot>().isPlaying = true;
StopRunBtn.gameObject.SetActive(true);
playBtn.gameObject.SetActive(false);
robot.GetComponent<Robot>().UpdateMotions(motion);
robot.GetComponent<Robot>().RefreshCur(all_Objects, actionContentScrollbar);
actionContentScrollbar.interactable = true;
}
}
/// <summary>
/// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
/// </summary>
private void StopRun()
{
playBtn.gameObject.SetActive(true);
StopRunBtn.gameObject.SetActive(false);
isPlayAction = false;
robot.GetComponent<Robot>().isPlaying = false;
robot.GetComponent<Robot>().ClearQueue();
for (int i = 0; i < items.Count; i++)
{
items[i].IsActivation(true);
}
}
public List<string> GetFilesNmae()
{
if (Directory.Exists(path))
{
DirectoryInfo direction = new DirectoryInfo(path);
FileInfo[] files = direction.GetFiles("*.txt");
List<string> names = new List<string>();
for (int i = 0; i < files.Length; i++)
{
if (!files[i].Name.EndsWith("meta"))
{
names.Add(files[i].Name);
}
}
return names;
}
return null;
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void InseriesFile()
{
if (isPlayAction == false)
{
FolderBrowserHelper.SelectFile(InserActionFile, FolderBrowserHelper.TEXTGEFILTER);
}
else
{
MyPrint();
}
}
public void InserActionFile(string path)
{
string str2 = path.Replace(@"\", "/");
int index = str2.IndexOf("\0");
string str3 = str2.Substring(0, index);
string str = File.ReadAllText(str3.ToString());
List<CustomItemData> cus = JsonConvert.DeserializeObject<List<CustomItemData>>(str);
for (int i = 0; i < cus.Count; i++)
{
CustomItemData data = new CustomItemData();
data.index = actions.Count + 1;
data.time = cus[i].time;
data.values = new List<int>();
for (int j = 0; j < cus[i].values.Count; j++)
{
data.values.Add(cus[i].values[cus[i].values.Count - 1 - j]);
}
actions.Add(data);
}
RefreshContent();
}
/// <summary>
/// <20>򿪶<EFBFBD><F2BFAAB6><EFBFBD><EFBFBD>ļ<EFBFBD>
/// </summary>
private void OpenActionFile()
{
if (isPlayAction == false)
{
FolderBrowserHelper.SelectFile(GetFiledPath, FolderBrowserHelper.TEXTGEFILTER);
}
else
{
MyPrint();
}
}
public void GetFiledPath(string path)
{
actions.Clear();
string str2 = path.Replace(@"\", "/");
int index = str2.IndexOf("\0");
string str3 = str2.Substring(0, index);
string str = File.ReadAllText(str3.ToString());
List<CustomItemData> cus = JsonConvert.DeserializeObject<List<CustomItemData>>(str);
for (int i = 0; i < cus.Count; i++)
{
CustomItemData data = new CustomItemData();
data.index = actions.Count + 1;
data.time = cus[i].time;
data.values = new List<int>();
for (int j = 0; j < cus[i].values.Count; j++)
{
data.values.Add(cus[i].values[cus[i].values.Count - 1 - j]);
}
actions.Add(data);
}
RefreshContent();
}
/// <summary>
/// ˢ<><EFBFBD><C2B6><EFBFBD>
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void RefreshBtn()
{
if (isPlayAction == false)
{
if (_currItem != null)
{
var temp = _currItem.GetComponent<MonsUIItem>();
for (int i = 0; i < actions.Count; i++)
{
if (actions[i].index == temp.index)
{
if (timeinput.text != "")
{
actions[i].time = int.Parse(timeinput.text);
}
else
{
actions[i].time = temp.data.time;
}
for (int j = 0; j < items.Count; j++)
{
actions[i].values[j] = int.Parse(items[j].value);
}
}
}
RefreshContent();
}
else
{
Debug.Log(ѡ<CEB4><D1A1>Ҫ<EFBFBD><D2AA><EFBFBD>µĶ<C2B5><C4B6><EFBFBD>");
}
}
else
{
MyPrint();
}
}
/// <summary>
/// <20><><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>
/// </summary>
public void AddMons()
{
if (isPlayAction == false)
{
CustomItemData data = new CustomItemData();
data.values = new List<int>();
data.index = actions.Count + 1;
if (msToggle.isOn)
{
if (timeinput.text != "")
{
data.time = int.Parse(timeinput.text);
}
else
{
data.time = 200;
}
}
else
{
if (timeinput.text != "")
{
data.time = int.Parse(timeinput.text) * 1000;
}
else
{
data.time = 200;
}
}
for (int i = 0; i < items.Count; i++)
{
data.values.Add(int.Parse(items[i].value));
}
actions.Add(data);
Debug.Log("<22><><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD>ɹ<EFBFBD>");
RefreshContent();
}
else
{
MyPrint();
}
}
/// <summary>
/// <20><><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>
/// </summary>
public void UpMoveActon()
{
if (isPlayAction == false)
{
if (_currItem != null)
{
var temp = _currItem.GetComponent<MonsUIItem>();
for (int i = 0; i < actions.Count; i++)
{
if (actions[i].index == temp.index)
{
if (i > 0)
{
var actiondata = actions[i];
actions[i] = actions[i - 1];
actions[i].index = actions[i].index + 1;
actions[i - 1] = actiondata;
actions[i - 1].index = actiondata.index - 1;
}
}
}
RefreshContent();
Debug.Log("<22><><EFBFBD>Ƴɹ<C6B3>");
}
else
{
Debug.Log("<22><>ѡ<EFBFBD><D1A1>Ҫ<EFBFBD><D2AA><EFBFBD>ƵĶ<C6B5><C4B6><EFBFBD>");
}
}
else
{
MyPrint();
}
}
/// <summary>
/// <20><>ʾ<EFBFBD><CABE>Ϣ
/// </summary>
public void MyPrint()
{
Debug.Log("<22><><EFBFBD><EFBFBD>ֹͣ<CDA3><D6B9><EFBFBD>е<EFBFBD><D0B5><EFBFBD>");
}
/// <summary>
/// ˢ<><CBA2><EFBFBD>б<EFBFBD>
/// </summary>
public void RefreshContent()
{
all_Objects.Clear();
for (int i = Content.childCount - 1; i >= 1; i--)
{
Destroy(Content.GetChild(i).gameObject);
}
for (int i = 0; i < actions.Count; i++)
{
GameObject item = Instantiate(Resources.Load<GameObject>("Prefabs/UIItem/MonsUIItem"), Content);
item.GetComponent<MonsUIItem>().Init(actions[i]);
all_Objects.Add(item);
}
}
/// <summary>
/// ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public void DeleteAction()
{
if (isPlayAction == false)
{
if (_currItem != null)
{
var temp = _currItem.GetComponent<MonsUIItem>();
for (int i = actions.Count - 1; i >= 0; i--)
{
if (actions[i].index == temp.index)
{
actions.Remove(actions[i]);
}
}
for (int i = 0; i < actions.Count; i++)
{
actions[i].index = i + 1;
}
Debug.Log("<22><><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD>ɹ<EFBFBD>");
RefreshContent();
}
else
{
Debug.Log("<22><>ѡ<EFBFBD><D1A1>Ҫɾ<D2AA><C9BE><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>");
}
}
else
{
MyPrint();
}
}
/// <summary>
/// <20>ر<EFBFBD><D8B1><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public void ClosePanel()
{
RobotUI.Instance.ClosePanel(RobotePanelEnum.CustomPanel.ToString());
for (int i = 0; i < robotgames.Count; i++)
{
robotgames[i].SetActive(true);
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
List<Motion> motion;
public void AddMotion()
{
if (actions.Count > 0)
{
motion = new List<Motion>();
for (int i = 0; i < actions.Count; i++)
{
RobotMotorValues values = new RobotMotorValues();
values.index = actions[i].index;
values.time = actions[i].time;
List<int> list = new List<int>();
for (int j = actions[i].values.Count - 1; j >= 0; j--)
{
list.Add(actions[i].values[j]);
}
values.values = list.ToArray();
motion.Add(new Motion(values));
}
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EBB6AF>
/// </summary>
public void InsertAction()
{
if (isPlayAction == false)
{
if (_currItem != null)
{
var temp = _currItem.GetComponent<MonsUIItem>();
for (int i = 0; i < actions.Count; i++)
{
if (actions[i].index == temp.index)
{
CustomItemData data = new CustomItemData();
data.values = new List<int>();
data.index = i;
if (timeinput.text != "")
{
data.time = int.Parse(timeinput.text);
}
else
{
data.time = 200;
}
for (int j = 0; j < items.Count; j++)
{
data.values.Add(int.Parse(items[j].value));
}
actions.Insert(temp.index, data);
}
}
for (int i = 0; i < actions.Count; i++)
{
actions[i].index = i + 1;
}
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>");
RefreshContent();
}
else
{
Debug.Log("<22><>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
}
else
{
MyPrint();
}
}
/// <summary>
/// <20><><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>
/// </summary>
public void DownMoveAction()
{
if (isPlayAction == false)
{
if (_currItem != null)
{
var temp = _currItem.GetComponent<MonsUIItem>();
for (int i = 0; i < actions.Count; i++)
{
if (actions[i].index == temp.index)
{
if (i < actions.Count - 1)
{
var actiondata = actions[i];
actions[i] = actions[i + 1];
actions[i].index = actions[i + 1].index - 1;
actions[i + 1] = actiondata;
actions[i + 1].index = actiondata.index + 1;
}
}
}
RefreshContent();
Debug.Log("<22><><EFBFBD>Ƴɹ<C6B3>");
}
else
{
Debug.Log("<22><>ѡ<EFBFBD><D1A1>Ҫ<EFBFBD><D2AA><EFBFBD>ƵĶ<C6B5><C4B6><EFBFBD>");
}
}
else
{
MyPrint();
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
/// </summary>
public void SvaeAction()
{
FolderBrowserHelper.SaveFile(path, SaveFiledPath, FolderBrowserHelper.TEXTGEFILTER);
}
public void SaveFiledPath(string path)
{
if (actions != null)
{
string path2 = path.Replace(@"\", "/");
int index = path2.IndexOf("\0");
string path3 = path2.Substring(0, index);
List<CustomItemData> items = new List<CustomItemData>();
for (int i = 0; i < actions.Count; i++)
{
CustomItemData customItemData = new CustomItemData();
customItemData.index = actions[i].index;
customItemData.time = actions[i].time;
customItemData.values = new List<int>();
for (int j = actions[i].values.Count - 1; j >= 0; j--)
{
customItemData.values.Add(actions[i].values[j]);
}
items.Add(customItemData);
}
string actionsData = JsonConvert.SerializeObject(items);
File.WriteAllText(path3, actionsData);
OnRefreshOptions();
}
}
/// <summary>
/// <20><>תֵ
/// </summary>
private float RotateValue=0;
/// <summary>
/// <20><><EFBFBD>Ƶ<EFBFBD>
/// </summary>
private Vector3 roudPoint;
void Update()
{
if (cirToggle.isOn == true && isPlayAction && robot.GetComponent<Robot>().isPlaying == false)
{
robot.GetComponent<Robot>().UpdateMotions(motion);
robot.GetComponent<Robot>().RefreshCur(all_Objects, actionContentScrollbar);
}
if (robot.GetComponent<Robot>().isPlaying == false && cirToggle.isOn == false)
{
StopRunBtn.gameObject.SetActive(false);
playBtn.gameObject.SetActive(true);
isPlayAction = false;
actionContentScrollbar.interactable = true;
}
RobotManager.instance.robotTempCamera.transform.RotateAround(roudPoint, Vector3.up, Time.deltaTime * RotateValue);
}
/// <summary>
/// ˢ<>µ<EFBFBD>ǰѡ<C7B0>е<EFBFBD>Item
/// </summary>
/// <param name="game"></param>
public static void FreshCurrItem(GameObject game)
{
if (_currItem != null)
{
//֮ǰѡ<C7B0><D1A1>״̬ȡ<CCAC><C8A1>
_currItem.GetComponent<MonsUIItem>().playBtn.gameObject.SetActive(false);
_currItem.GetComponent<Image>().sprite = defSprite;
}
//<2F><><EFBFBD>õ<EFBFBD>ǰѡ<C7B0><D1A1>״̬
_currItem = game;
_currItem.GetComponent<Image>().sprite = _currItemSprite;
_currItem.GetComponent<MonsUIItem>().playBtn.gameObject.SetActive(true);
}
private void OnDisable()
{
robot.SetActive(false);
RobotManager.instance.robotTempCamera.SetActive(false);
playBtn.onClick.RemoveAllListeners();
saveBtn.onClick.RemoveAllListeners();
addMons.onClick.RemoveAllListeners();
refreshBtn.onClick.RemoveAllListeners();
upMoveBtn.onClick.RemoveAllListeners();
deleteBtn.onClick.RemoveAllListeners();
downMoveBtn.onClick.RemoveAllListeners();
openActionFileBtn.onClick.RemoveAllListeners();
inseriesFileBtn.onClick.RemoveAllListeners();
actionPlayBtn.onClick.RemoveAllListeners();
singleErasureBtn.onClick.RemoveAllListeners();
allErasureBtn.onClick.RemoveAllListeners();
actionStopBtn.onClick.RemoveAllListeners();
StopRunBtn.onClick.RemoveAllListeners();
dropdown.onValueChanged.RemoveAllListeners();
}
/// <summary>
/// <20><>ת
/// </summary>
public void RotateLeft()
{
RotateValue = 20;
}
/// <summary>
/// <20><>ת
/// </summary>
public void RotateRight()
{
RotateValue = -20;
}
/// <summary>
/// ֹͣ<CDA3><D6B9>ת
/// </summary>
public void RotateStop()
{
RotateValue = 0;
}
}