PointMarker.cs 9.7 KB

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