PointMarker.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections.Generic;
  4. using System;
  5. using ZIM.Unity;
  6. using System.Linq;
  7. public class PointMarker : MonoBehaviour
  8. {
  9. //public Text instructionText; // 用于显示提示文字
  10. [Tooltip("标记页面标题提示")]
  11. [SerializeField] TextAutoLanguage2 instructionText;
  12. public Button undoButton; // 撤回按钮
  13. public Button completeButton; // 完成按钮
  14. public bool bComplete = false;// 是否完成
  15. public GameObject markerPrefab; // 用于表示标记的UI元素,可能是一个小图标(如枪)
  16. // 新增父对象,用于存放标记的物体
  17. public Transform markerParent; // 标记物体的父对象(例如Canvas中的一个空物体)
  18. public Image[] hintImages; // 依次存放左上、右上、右下、左下四个提示图片
  19. Vector2 oldPoint = Vector2.zero;
  20. private List<Vector2> markedPoints = new List<Vector2>();
  21. // { "左上", "右上", "右下", "左下" }
  22. private string[] directions = { "TipTopLeft", "TipTopRight", "TipBottomRight", "TipBottomLeft" };
  23. private int currentPointIndex = 0; // 当前需要标记的点的索引
  24. private List<GameObject> markerObjects = new List<GameObject>(); // 记录标记物体
  25. [SerializeField] InfraredScreenPositioningView infraredScreenPositioningView;
  26. [Tooltip("存在校准数据时候显示")]
  27. [SerializeField] ZIM.LineGenerator line;//开始显示屏幕线条
  28. void Start()
  29. {
  30. if (AimHandler.ins) AimHandler.ins.OnCrossBtnEvent += OnRecordInfrared;
  31. UpdateInstruction();
  32. undoButton.onClick.AddListener(UndoLastPoint);
  33. completeButton.onClick.AddListener(CompletePoint); // 初始化时隐藏完成按钮
  34. bComplete = false;
  35. // 初始化时,显示第一个提示图片,其余隐藏
  36. ShowHintImage(0);
  37. }
  38. void OnDestroy()
  39. {
  40. if (AimHandler.ins) AimHandler.ins.OnCrossBtnEvent -= OnRecordInfrared;
  41. }
  42. private float lastClickTime = 0f;
  43. private float doubleClickThreshold = 0.3f; // 双击间隔时间,单位:秒
  44. void Update()
  45. {
  46. if (Input.GetMouseButtonDown(0)) // 检测左键按下
  47. {
  48. float currentTime = Time.time;
  49. if (currentTime - lastClickTime <= doubleClickThreshold)
  50. {
  51. // 双击检测成功,执行触发逻辑
  52. if (currentPointIndex >= 4) return;
  53. var mouse = Input.mousePosition;
  54. var u = mouse.x / Screen.width;
  55. var v = mouse.y / Screen.height;
  56. u = Math.Clamp(u, 0, 1);
  57. v = Math.Clamp(v, 0, 1);
  58. Vector2 pos = new Vector2(u * ScreenLocate.Main.getUVCTexture.width, v * ScreenLocate.Main.getUVCTexture.height);
  59. //markedPoints.Add(pos);
  60. oldPoint = new Vector2(u, v);
  61. // 设置标记的位置
  62. MarkPoint(new Vector2(u, v).pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), markerParent.GetComponent<RectTransform>().rect));
  63. }
  64. lastClickTime = currentTime; // 更新上一次点击时间
  65. }
  66. //#if UNITY_EDITOR
  67. //if (Input.GetKeyDown(KeyCode.Alpha6) || Input.GetMouseButtonDown(1))
  68. //{
  69. // //Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  70. // //MarkPoint(mousePosition);
  71. // // OnRecordInfrared();
  72. // if (currentPointIndex >= 4) return;
  73. // var mouse = Input.mousePosition;
  74. // var u = mouse.x / Screen.width;
  75. // var v = mouse.y / Screen.height;
  76. // u = Math.Clamp(u, 0, 1);
  77. // v = Math.Clamp(v, 0, 1);
  78. // Vector2 pos = new Vector2(u * ScreenLocate.Main.getUVCTexture.width, v * ScreenLocate.Main.getUVCTexture.height);
  79. // markedPoints.Add(pos);
  80. // // 设置标记的位置
  81. // MarkPoint(new Vector2(u, v).pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), markerParent.GetComponent<RectTransform>().rect));
  82. //}
  83. //#endif
  84. }
  85. /// <summary>
  86. /// 记录红外点
  87. /// </summary>
  88. public void OnRecordInfrared()
  89. {
  90. if (currentPointIndex >= 4) return;
  91. var location = ScreenLocate.Main.infraredSpotBuffer.FirstOrDefault()?.CameraLocation;
  92. if (location != null)
  93. {
  94. oldPoint = location.Value;
  95. Vector2 localPosition = location.Value.pixelToLocalPosition_AnchorCenter(ScreenLocate.Main.CameraSize, infraredScreenPositioningView.Bg.rectTransform.rect);
  96. //markedPoints.Add(localPosition);
  97. MarkPoint(localPosition);
  98. }
  99. }
  100. void MarkPoint(Vector2 point)
  101. {
  102. if (!infraredScreenPositioningView.IsPointInMaskLine(point)) {
  103. Debug.Log("不在梯形内部!");
  104. instructionText.SetTextKey("TipMarkerError");
  105. return;
  106. }
  107. // 创建标记物体,并将其作为markerParent的子对象
  108. GameObject marker = Instantiate(markerPrefab, markerParent);
  109. marker.SetActive(true);
  110. marker.transform.localPosition = point;
  111. markerObjects.Add(marker);
  112. //绘制连线
  113. markedPoints.Add(point);
  114. UpdateLine();
  115. // 隐藏当前的提示图片,显示下一个提示图片
  116. currentPointIndex++;
  117. ShowHintImage(currentPointIndex);
  118. UpdateInstruction();
  119. // 如果已经标记了四个点,显示完成按钮
  120. if (currentPointIndex == 4)
  121. {
  122. Vector2[] points = {
  123. markerObjects[0].transform.localPosition,
  124. markerObjects[1].transform.localPosition,
  125. markerObjects[2].transform.localPosition,
  126. markerObjects[3].transform.localPosition,
  127. };
  128. bool result = infraredScreenPositioningView.IsValidQuadrilateral(points);
  129. Debug.Log(result ? "是有效的四边形" : "不是有效的四边形");
  130. if (result)
  131. {
  132. instructionText.SetTextKey("TipMarkComplete");
  133. //PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("TipMarkComplete"));
  134. bComplete = true;
  135. }
  136. else {
  137. //不是有效是四边形时候,回退 一个
  138. //instructionText.SetTextKey("TipMarkerError");
  139. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("TipQuadError"));
  140. UndoLastPoint();
  141. }
  142. }
  143. }
  144. void UndoLastPoint()
  145. {
  146. if (currentPointIndex > 0)
  147. {
  148. // 移除最后一个标记的点
  149. markedPoints.RemoveAt(markedPoints.Count - 1);
  150. UpdateLine();
  151. // 删除最后一个标记的物体
  152. Destroy(markerObjects[markerObjects.Count - 1]);
  153. markerObjects.RemoveAt(markerObjects.Count - 1);
  154. // 回退到上一个提示图片
  155. currentPointIndex--;
  156. ShowHintImage(currentPointIndex);
  157. UpdateInstruction();
  158. }
  159. // 隐藏完成按钮
  160. bComplete = false;
  161. }
  162. /// <summary>
  163. /// 移除全部标记,选择自动和半自动数据界面返回时候调用
  164. /// </summary>
  165. public void UndoAllPoints()
  166. {
  167. // 移除所有标记物体
  168. foreach (var marker in markerObjects)
  169. {
  170. Destroy(marker);
  171. }
  172. markerObjects.Clear();
  173. markedPoints.Clear();
  174. UpdateLine();
  175. // 重置索引
  176. currentPointIndex = 0;
  177. // 更新提示图片和说明
  178. ShowHintImage(currentPointIndex);
  179. UpdateInstruction();
  180. // 隐藏完成按钮
  181. bComplete = false;
  182. }
  183. void UpdateLine() {
  184. if (line.gameObject.activeSelf) {
  185. RectTransform rectTransform = markerParent as RectTransform;
  186. Vector2 pivot = rectTransform.pivot; // 获取父物体的 pivot 值
  187. Vector2 texSize = rectTransform.rect.size;
  188. line.Points = InfraredDemo._ins.ConvertPointsToCoordinates(markedPoints.ToArray(), texSize, pivot);
  189. }
  190. }
  191. void UpdateInstruction()
  192. {
  193. if (currentPointIndex < 4)
  194. {
  195. instructionText.textFormatArgs = new object[] { TextAutoLanguage2.GetTextByKey(directions[currentPointIndex]) };
  196. instructionText.SetTextKey("TipMiddle");
  197. }
  198. }
  199. // 显示当前需要的提示图片,并隐藏其余提示图片
  200. void ShowHintImage(int index)
  201. {
  202. for (int i = 0; i < hintImages.Length; i++)
  203. {
  204. if (i == index)
  205. hintImages[i].gameObject.SetActive(true);
  206. else
  207. hintImages[i].gameObject.SetActive(false);
  208. }
  209. }
  210. /// <summary>
  211. /// 如果没有数据时候,可能需要隐藏渲染
  212. /// </summary>
  213. /// <param name="bShow"></param>
  214. public void ShowHintImageParent(bool bShow)
  215. {
  216. for (int i = 0; i < hintImages.Length; i++)
  217. {
  218. hintImages[i].transform.parent.gameObject.SetActive(bShow);
  219. }
  220. }
  221. void CompletePoint() {
  222. if (!bComplete) {
  223. //Debug.Log("未完成");
  224. //todo,加提示
  225. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("Incomplete"));
  226. return;
  227. }
  228. //完成,下一步
  229. for (int i = 0; i < 4; i++)
  230. {
  231. int index = i;
  232. infraredScreenPositioningView.onManualNewPos(3 - index, markerObjects[index].transform.localPosition);
  233. }
  234. infraredScreenPositioningView.onFinishManualToAutomatic();
  235. ////清空数据
  236. //currentPointIndex = 0;
  237. ////清空生成的数据
  238. //markerObjects.Clear();
  239. //foreach (Transform i in markerParent.transform)
  240. // Destroy(i.gameObject);
  241. }
  242. }