InfraredScreenPositioningView.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. void Update()
  57. {
  58. if (InfraredDemo.running)
  59. {
  60. //渲染相机画面
  61. rawImage.texture = InfraredDemo.infraredCameraHelper.GetCameraTexture();
  62. rawImage.material = InfraredDemo.infraredCameraHelper.GetCameraMaterial();
  63. }
  64. }
  65. public void OnClick_Back()
  66. {
  67. AudioMgr.ins.PlayBtn();
  68. ViewMgr.Instance.DestroyView<InfraredScreenPositioningView>();
  69. }
  70. #region 绘制线段部分
  71. //点击拖拽的开始位置
  72. public void onBeginPos(int index, Vector3 pos)
  73. {
  74. beginPos = pos;
  75. }
  76. public void onDragPos(int index, Vector3 pos)
  77. {
  78. //设置线段的点
  79. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  80. }
  81. //点击拖拽的结束位置
  82. public void onEndPos(int index, Vector3 pos)
  83. {
  84. endPos = pos;
  85. if (beginPos == endPos) return;
  86. //Debug.Log(index+",最后的点:" + pos);
  87. //再记录一次最后的点
  88. SetLinePos();
  89. }
  90. //同步设置图片对应的位置到line
  91. public void SetLinePos()
  92. {
  93. //记录一个操作点的操作位置
  94. AddOldLinePosition();
  95. //设置线段的点
  96. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  97. }
  98. void AddOldLinePosition()
  99. {
  100. //记录一个操作点的操作位置
  101. List<Vector3> screenPositions = new List<Vector3>();
  102. screenPositions.Add(pos1.localPosition);
  103. screenPositions.Add(pos2.localPosition);
  104. screenPositions.Add(pos3.localPosition);
  105. screenPositions.Add(pos4.localPosition);
  106. LinePosition linePosition = new LinePosition();
  107. linePosition.index = oldLinePosition.Count;
  108. linePosition.pos = screenPositions;
  109. oldLinePosition.Add(linePosition);
  110. }
  111. List<Vector2> linePosConversion(Vector3 _pos1, Vector3 _pos2, Vector3 _pos3, Vector3 _pos4)
  112. {
  113. List<Vector2> _screenPositions = new List<Vector2>();
  114. Vector2 startPos1 = new Vector2(_pos1.x - pos1.rect.width * 0.25f - offset, _pos1.y - pos1.rect.height * 0.25f - offset);
  115. Vector2 startPos2 = new Vector2(_pos2.x + pos2.rect.width * 0.25f + offset, _pos2.y - pos2.rect.height * 0.25f - offset);
  116. Vector2 startPos3 = new Vector2(_pos3.x + pos3.rect.width * 0.25f + offset, _pos3.y + pos3.rect.height * 0.25f + offset);
  117. Vector2 startPos4 = new Vector2(_pos4.x - pos4.rect.width * 0.25f - offset, _pos4.y + pos4.rect.height * 0.25f + offset);
  118. _screenPositions.Add(startPos1);
  119. _screenPositions.Add(startPos2);
  120. _screenPositions.Add(startPos3);
  121. _screenPositions.Add(startPos4);
  122. return _screenPositions;
  123. }
  124. void SetRectanglePoints(List<Vector2> screenPositions)
  125. {
  126. line.SetLine(screenPositions);
  127. }
  128. //撤回上一个元素
  129. public void onRecall()
  130. {
  131. // 获取并删除最后一个元素,并且保留一个元素
  132. if (oldLinePosition.Count > 1) // 确保列表不为空
  133. {
  134. // 获取回退的那个元素点
  135. LinePosition lastElement_second = oldLinePosition[oldLinePosition.Count - 2];
  136. // 获取最后一个元素
  137. //LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  138. //Debug.Log(JsonUtility.ToJson(lastElement) + " = " + oldLinePosition.Count);
  139. oldLinePosition.RemoveAt(oldLinePosition.Count - 1); // 删除最后一个元素
  140. pos1.localPosition = lastElement_second.pos[0];
  141. pos2.localPosition = lastElement_second.pos[1];
  142. pos3.localPosition = lastElement_second.pos[2];
  143. pos4.localPosition = lastElement_second.pos[3];
  144. //设置线段的点
  145. SetRectanglePoints(linePosConversion(lastElement_second.pos[0], lastElement_second.pos[1], lastElement_second.pos[2], lastElement_second.pos[3]));
  146. }
  147. }
  148. //确认修改
  149. public void onConfirmation()
  150. {
  151. if (!ConfirmScreenLocateManual()) return;
  152. if (oldLinePosition.Count > 1) // 确保列表不为空
  153. {
  154. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  155. oldLinePosition.Clear();
  156. oldLinePosition.Add(lastElement);
  157. }
  158. if (InfraredDemo.running) {
  159. //跳转入界面
  160. AudioMgr.ins.PlayBtn();
  161. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  162. ViewManager2.ShowView(ViewManager2.Path_ConnectGuidanceView);
  163. }
  164. //存储一次节点
  165. SaveLocalPos();
  166. }
  167. //设置位置
  168. public void onReset()
  169. {
  170. oldLinePosition.Clear();
  171. int _x = 120, _y = 77;
  172. pos1.anchoredPosition = new Vector3(_x, _y, 0);
  173. pos2.anchoredPosition = new Vector3(-_x, _y, 0);
  174. pos3.anchoredPosition = new Vector3(-_x, -_y, 0);
  175. pos4.anchoredPosition = new Vector3(_x, -_y, 0);
  176. //设置一次位置
  177. SetLinePos();
  178. }
  179. #endregion
  180. List<Vector2> _locatePointList = new();
  181. float _texWidth;
  182. float _texHeight;
  183. void RecordLocatePoint(RectTransform p, Vector2 pivot)
  184. {
  185. Vector2 pos = JCUnityLib.RectTransformUtils.GetPositionByPivot(p, pivot);
  186. pos.x = Mathf.Clamp01(pos.x / Screen.width) * _texWidth;
  187. pos.y = Mathf.Clamp01(pos.y / Screen.height) * _texHeight;
  188. _locatePointList.Add(pos);
  189. }
  190. bool ConfirmScreenLocateManual()
  191. {
  192. if (InfraredDemo.running)
  193. {
  194. //渲染截图
  195. Texture2D texture2D = InfraredDemo.infraredCameraHelper.EnterScreenLocateManual();
  196. if (texture2D == null)
  197. {
  198. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(null);
  199. return false;
  200. }
  201. _locatePointList.Clear();
  202. _texWidth = texture2D.width;
  203. _texHeight = texture2D.height;
  204. RecordLocatePoint(pos1, new Vector2(0, 0));
  205. RecordLocatePoint(pos2, new Vector2(1, 0));
  206. RecordLocatePoint(pos3, new Vector2(1, 1));
  207. RecordLocatePoint(pos4, new Vector2(0, 1));
  208. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(_locatePointList);
  209. FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, _texWidth, _texHeight);
  210. return true;
  211. }
  212. return false;
  213. }
  214. void SaveLocalPos() {
  215. List<Vector3> screenPositions = new List<Vector3>();
  216. screenPositions.Add(pos1.localPosition);
  217. screenPositions.Add(pos2.localPosition);
  218. screenPositions.Add(pos3.localPosition);
  219. screenPositions.Add(pos4.localPosition);
  220. string saveStr = string.Join(';', screenPositions.Select(v => $"{v.x},{v.y},{v.z}"));
  221. Debug.Log(saveStr);
  222. PlayerPrefs.SetString("ScreenPositioningView", saveStr);
  223. }
  224. void GetLocalPos() {
  225. string posListStr = PlayerPrefs.GetString("ScreenPositioningView", "");
  226. if (!string.IsNullOrWhiteSpace(posListStr))
  227. {
  228. List<Vector3> posList = posListStr.Split(';')
  229. .Select(s =>
  230. {
  231. string[] parts = s.Split(',');
  232. return new Vector3(float.Parse(parts[0]), float.Parse(parts[1]), float.Parse(parts[2]));
  233. })
  234. .ToList();
  235. pos1.localPosition = posList[0];
  236. pos2.localPosition = posList[1];
  237. pos3.localPosition = posList[2];
  238. pos4.localPosition = posList[3];
  239. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  240. }
  241. }
  242. }