InfraredScreenPositioningView1.cs 8.0 KB

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