InfraredScreenPositioningView1.cs 9.6 KB

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