InfraredScreenPositioningView.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LineUI;
  5. using UnityEngine.UI;
  6. using System.Linq;
  7. public class LinePosition {
  8. public int index;
  9. public List<Vector3> pos;
  10. }
  11. public class InfraredScreenPositioningView : JCUnityLib.ViewBase
  12. {
  13. [SerializeField]
  14. RectTransform pos1;
  15. [SerializeField]
  16. RectTransform pos2;
  17. [SerializeField]
  18. RectTransform pos3;
  19. [SerializeField]
  20. RectTransform pos4;
  21. //画线时候的点偏移量
  22. [SerializeField]
  23. int offset = 20;
  24. [SerializeField]
  25. Line line;
  26. List<LinePosition> oldLinePosition;
  27. Vector3 beginPos;
  28. Vector3 endPos;
  29. //相机感光部分
  30. [SerializeField]
  31. Slider slider;
  32. [SerializeField]
  33. RawImage rawImage;
  34. private void Awake()
  35. {
  36. GetLocalPos();
  37. }
  38. void Start()
  39. {
  40. //记录操作的位置信息
  41. oldLinePosition = new List<LinePosition>();
  42. SetLinePos();
  43. //相机感光度
  44. if (InfraredDemo._ins) {
  45. slider.onValueChanged.AddListener((value) => {
  46. InfraredDemo._ins.onSliderEvent(value);
  47. });
  48. InfraredDemo._ins.onSetSliderValue(slider);
  49. }
  50. //slider.onValueChanged.AddListener((value) => {
  51. // //onSliderEvent(value);
  52. // InfraredDemo._ins.SetBrightness(value);
  53. //});
  54. //slider.value = InfraredDemo._ins.brightness.Get();
  55. //图像
  56. if (InfraredDemo.running)
  57. {
  58. //渲染截图
  59. Texture2D texture2D = InfraredDemo.infraredCameraHelper.EnterScreenLocateManual();
  60. if (texture2D == null)
  61. {
  62. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(null);
  63. return;
  64. }
  65. rawImage.texture = texture2D;
  66. rawImage.material = InfraredDemo.infraredCameraHelper.GetCameraMaterial();
  67. }
  68. }
  69. public void OnClick_Back()
  70. {
  71. AudioMgr.ins.PlayBtn();
  72. ViewMgr.Instance.DestroyView<InfraredScreenPositioningView>();
  73. }
  74. #region 绘制线段部分
  75. //点击拖拽的开始位置
  76. public void onBeginPos(int index, Vector3 pos)
  77. {
  78. beginPos = pos;
  79. }
  80. public void onDragPos(int index, Vector3 pos)
  81. {
  82. //设置线段的点
  83. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  84. }
  85. //点击拖拽的结束位置
  86. public void onEndPos(int index, Vector3 pos)
  87. {
  88. endPos = pos;
  89. if (beginPos == endPos) return;
  90. //Debug.Log(index+",最后的点:" + pos);
  91. //再记录一次最后的点
  92. SetLinePos();
  93. }
  94. //同步设置图片对应的位置到line
  95. public void SetLinePos()
  96. {
  97. //记录一个操作点的操作位置
  98. AddOldLinePosition();
  99. //设置线段的点
  100. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  101. }
  102. void AddOldLinePosition()
  103. {
  104. //记录一个操作点的操作位置
  105. List<Vector3> screenPositions = new List<Vector3>();
  106. screenPositions.Add(pos1.localPosition);
  107. screenPositions.Add(pos2.localPosition);
  108. screenPositions.Add(pos3.localPosition);
  109. screenPositions.Add(pos4.localPosition);
  110. LinePosition linePosition = new LinePosition();
  111. linePosition.index = oldLinePosition.Count;
  112. linePosition.pos = screenPositions;
  113. oldLinePosition.Add(linePosition);
  114. }
  115. List<Vector2> linePosConversion(Vector3 _pos1, Vector3 _pos2, Vector3 _pos3, Vector3 _pos4)
  116. {
  117. List<Vector2> _screenPositions = new List<Vector2>();
  118. Vector2 startPos1 = new Vector2(_pos1.x - pos1.rect.width * 0.25f - offset, _pos1.y - pos1.rect.height * 0.25f - offset);
  119. Vector2 startPos2 = new Vector2(_pos2.x + pos2.rect.width * 0.25f + offset, _pos2.y - pos2.rect.height * 0.25f - offset);
  120. Vector2 startPos3 = new Vector2(_pos3.x + pos3.rect.width * 0.25f + offset, _pos3.y + pos3.rect.height * 0.25f + offset);
  121. Vector2 startPos4 = new Vector2(_pos4.x - pos4.rect.width * 0.25f - offset, _pos4.y + pos4.rect.height * 0.25f + offset);
  122. _screenPositions.Add(startPos1);
  123. _screenPositions.Add(startPos2);
  124. _screenPositions.Add(startPos3);
  125. _screenPositions.Add(startPos4);
  126. return _screenPositions;
  127. }
  128. void SetRectanglePoints(List<Vector2> screenPositions)
  129. {
  130. line.SetLine(screenPositions);
  131. }
  132. //撤回上一个元素
  133. public void onRecall()
  134. {
  135. // 获取并删除最后一个元素,并且保留一个元素
  136. if (oldLinePosition.Count > 1) // 确保列表不为空
  137. {
  138. // 获取回退的那个元素点
  139. LinePosition lastElement_second = oldLinePosition[oldLinePosition.Count - 2];
  140. // 获取最后一个元素
  141. //LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  142. //Debug.Log(JsonUtility.ToJson(lastElement) + " = " + oldLinePosition.Count);
  143. oldLinePosition.RemoveAt(oldLinePosition.Count - 1); // 删除最后一个元素
  144. pos1.localPosition = lastElement_second.pos[0];
  145. pos2.localPosition = lastElement_second.pos[1];
  146. pos3.localPosition = lastElement_second.pos[2];
  147. pos4.localPosition = lastElement_second.pos[3];
  148. //设置线段的点
  149. SetRectanglePoints(linePosConversion(lastElement_second.pos[0], lastElement_second.pos[1], lastElement_second.pos[2], lastElement_second.pos[3]));
  150. }
  151. }
  152. //确认修改
  153. public void onConfirmation()
  154. {
  155. if (oldLinePosition.Count > 1) // 确保列表不为空
  156. {
  157. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  158. oldLinePosition.Clear();
  159. oldLinePosition.Add(lastElement);
  160. }
  161. if (InfraredDemo.running) {
  162. //跳转入界面
  163. AudioMgr.ins.PlayBtn();
  164. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  165. ViewManager2.ShowView(ViewManager2.Path_ConnectGuidanceView);
  166. }
  167. Quit();
  168. //存储一次节点
  169. SaveLocalPos();
  170. }
  171. //设置位置
  172. public void onReset()
  173. {
  174. oldLinePosition.Clear();
  175. int _x = 120, _y = 77;
  176. pos1.anchoredPosition = new Vector3(_x, _y, 0);
  177. pos2.anchoredPosition = new Vector3(-_x, _y, 0);
  178. pos3.anchoredPosition = new Vector3(-_x, -_y, 0);
  179. pos4.anchoredPosition = new Vector3(_x, -_y, 0);
  180. //设置一次位置
  181. SetLinePos();
  182. }
  183. #endregion
  184. List<Vector2> _locatePointList = new();
  185. float _texWidth;
  186. float _texHeight;
  187. void RecordLocatePoint(RectTransform p, Vector2 pivot)
  188. {
  189. Vector2 pos = JCUnityLib.RectTransformUtils.GetPositionByPivot(p, pivot);
  190. pos.x = pos.x / Screen.width * _texWidth;
  191. pos.y = pos.y / Screen.height * _texHeight;
  192. _locatePointList.Add(pos);
  193. }
  194. void Quit()
  195. {
  196. if (InfraredDemo.running && InfraredDemo.infraredCameraHelper.IsScreenLocateManualDoing())
  197. {
  198. RecordLocatePoint(pos1, new Vector2(0, 0));
  199. RecordLocatePoint(pos2, new Vector2(1, 0));
  200. RecordLocatePoint(pos3, new Vector2(1, 1));
  201. RecordLocatePoint(pos4, new Vector2(0, 1));
  202. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(_locatePointList);
  203. FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, _texWidth, _texHeight);
  204. }
  205. }
  206. void SaveLocalPos() {
  207. List<Vector3> screenPositions = new List<Vector3>();
  208. screenPositions.Add(pos1.localPosition);
  209. screenPositions.Add(pos2.localPosition);
  210. screenPositions.Add(pos3.localPosition);
  211. screenPositions.Add(pos4.localPosition);
  212. string saveStr = string.Join(';', screenPositions.Select(v => $"{v.x},{v.y},{v.z}"));
  213. Debug.Log(saveStr);
  214. PlayerPrefs.SetString("ScreenPositioningView", saveStr);
  215. }
  216. void GetLocalPos() {
  217. string posListStr = PlayerPrefs.GetString("ScreenPositioningView", "");
  218. if (!string.IsNullOrWhiteSpace(posListStr))
  219. {
  220. List<Vector3> posList = posListStr.Split(';')
  221. .Select(s =>
  222. {
  223. string[] parts = s.Split(',');
  224. return new Vector3(float.Parse(parts[0]), float.Parse(parts[1]), float.Parse(parts[2]));
  225. })
  226. .ToList();
  227. pos1.localPosition = posList[0];
  228. pos2.localPosition = posList[1];
  229. pos3.localPosition = posList[2];
  230. pos4.localPosition = posList[3];
  231. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  232. }
  233. }
  234. }