InfraredScreenPositioningView.cs 9.6 KB

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