PointMarker.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. //private List<Vector2> markedPoints = new List<Vector2>();
  20. // { "左上", "右上", "右下", "左下" }
  21. private string[] directions = { "TipTopLeft", "TipTopRight", "TipBottomRight", "TipBottomLeft" };
  22. private int currentPointIndex = 0; // 当前需要标记的点的索引
  23. private List<GameObject> markerObjects = new List<GameObject>(); // 记录标记物体
  24. [SerializeField] InfraredScreenPositioningView infraredScreenPositioningView;
  25. void Start()
  26. {
  27. if (AimHandler.ins) AimHandler.ins.OnCrossBtnEvent += OnRecordInfrared;
  28. UpdateInstruction();
  29. undoButton.onClick.AddListener(UndoLastPoint);
  30. completeButton.onClick.AddListener(CompletePoint); // 初始化时隐藏完成按钮
  31. bComplete = false;
  32. // 初始化时,显示第一个提示图片,其余隐藏
  33. ShowHintImage(0);
  34. }
  35. void OnDestroy()
  36. {
  37. if (AimHandler.ins) AimHandler.ins.OnCrossBtnEvent -= OnRecordInfrared;
  38. }
  39. private float lastClickTime = 0f;
  40. private float doubleClickThreshold = 0.3f; // 双击间隔时间,单位:秒
  41. void Update()
  42. {
  43. if (Input.GetMouseButtonDown(0)) // 检测左键按下
  44. {
  45. float currentTime = Time.time;
  46. if (currentTime - lastClickTime <= doubleClickThreshold)
  47. {
  48. // 双击检测成功,执行触发逻辑
  49. if (currentPointIndex >= 4) return;
  50. var mouse = Input.mousePosition;
  51. var u = mouse.x / Screen.width;
  52. var v = mouse.y / Screen.height;
  53. u = Math.Clamp(u, 0, 1);
  54. v = Math.Clamp(v, 0, 1);
  55. Vector2 pos = new Vector2(u * ScreenLocate.Main.getUVCTexture.width, v * ScreenLocate.Main.getUVCTexture.height);
  56. //markedPoints.Add(pos);
  57. // 设置标记的位置
  58. MarkPoint(new Vector2(u, v).pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), markerParent.GetComponent<RectTransform>().rect));
  59. }
  60. lastClickTime = currentTime; // 更新上一次点击时间
  61. }
  62. //#if UNITY_EDITOR
  63. //if (Input.GetKeyDown(KeyCode.Alpha6) || Input.GetMouseButtonDown(1))
  64. //{
  65. // //Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  66. // //MarkPoint(mousePosition);
  67. // // OnRecordInfrared();
  68. // if (currentPointIndex >= 4) return;
  69. // var mouse = Input.mousePosition;
  70. // var u = mouse.x / Screen.width;
  71. // var v = mouse.y / Screen.height;
  72. // u = Math.Clamp(u, 0, 1);
  73. // v = Math.Clamp(v, 0, 1);
  74. // Vector2 pos = new Vector2(u * ScreenLocate.Main.getUVCTexture.width, v * ScreenLocate.Main.getUVCTexture.height);
  75. // markedPoints.Add(pos);
  76. // // 设置标记的位置
  77. // MarkPoint(new Vector2(u, v).pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), markerParent.GetComponent<RectTransform>().rect));
  78. //}
  79. //#endif
  80. }
  81. /// <summary>
  82. /// 记录红外点
  83. /// </summary>
  84. public void OnRecordInfrared()
  85. {
  86. if (currentPointIndex >= 4) return;
  87. var location = ScreenLocate.Main.infraredSpotBuffer.FirstOrDefault()?.CameraLocation;
  88. if (location != null)
  89. {
  90. Vector2 localPosition = location.Value.pixelToLocalPosition_AnchorCenter(ScreenLocate.Main.CameraSize, infraredScreenPositioningView.Bg.rectTransform.rect);
  91. //markedPoints.Add(localPosition);
  92. MarkPoint(localPosition);
  93. }
  94. }
  95. void MarkPoint(Vector2 point)
  96. {
  97. if (!infraredScreenPositioningView.IsPointInMaskLine(point)) {
  98. Debug.Log("不在梯形内部!");
  99. instructionText.SetTextKey("TipMarkerError");
  100. return;
  101. }
  102. // 创建标记物体,并将其作为markerParent的子对象
  103. GameObject marker = Instantiate(markerPrefab, markerParent);
  104. marker.SetActive(true);
  105. marker.transform.localPosition = point;
  106. markerObjects.Add(marker);
  107. // 隐藏当前的提示图片,显示下一个提示图片
  108. currentPointIndex++;
  109. ShowHintImage(currentPointIndex);
  110. UpdateInstruction();
  111. // 如果已经标记了四个点,显示完成按钮
  112. if (currentPointIndex == 4)
  113. {
  114. Vector2[] points = {
  115. markerObjects[0].transform.localPosition,
  116. markerObjects[1].transform.localPosition,
  117. markerObjects[2].transform.localPosition,
  118. markerObjects[3].transform.localPosition,
  119. };
  120. bool result = infraredScreenPositioningView.IsValidQuadrilateral(points);
  121. Debug.Log(result ? "是有效的四边形" : "不是有效的四边形");
  122. if (result)
  123. {
  124. instructionText.SetTextKey("TipMarkComplete");
  125. //PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("TipMarkComplete"));
  126. bComplete = true;
  127. }
  128. else {
  129. //不是有效是四边形时候,回退 一个
  130. //instructionText.SetTextKey("TipMarkerError");
  131. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("TipQuadError"));
  132. UndoLastPoint();
  133. }
  134. }
  135. }
  136. void UndoLastPoint()
  137. {
  138. if (currentPointIndex > 0)
  139. {
  140. // 移除最后一个标记的点
  141. //markedPoints.RemoveAt(markedPoints.Count - 1);
  142. // 删除最后一个标记的物体
  143. Destroy(markerObjects[markerObjects.Count - 1]);
  144. markerObjects.RemoveAt(markerObjects.Count - 1);
  145. // 回退到上一个提示图片
  146. currentPointIndex--;
  147. ShowHintImage(currentPointIndex);
  148. UpdateInstruction();
  149. }
  150. // 隐藏完成按钮
  151. bComplete = false;
  152. }
  153. void UpdateInstruction()
  154. {
  155. if (currentPointIndex < 4)
  156. {
  157. instructionText.textFormatArgs = new object[] { TextAutoLanguage2.GetTextByKey(directions[currentPointIndex]) };
  158. instructionText.SetTextKey("TipMiddle");
  159. }
  160. }
  161. // 显示当前需要的提示图片,并隐藏其余提示图片
  162. void ShowHintImage(int index)
  163. {
  164. for (int i = 0; i < hintImages.Length; i++)
  165. {
  166. if (i == index)
  167. hintImages[i].gameObject.SetActive(true);
  168. else
  169. hintImages[i].gameObject.SetActive(false);
  170. }
  171. }
  172. void CompletePoint() {
  173. if (!bComplete) {
  174. //Debug.Log("未完成");
  175. //todo,加提示
  176. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("Incomplete"));
  177. return;
  178. }
  179. //完成,下一步
  180. for (int i = 0; i < 4; i++)
  181. {
  182. int index = i;
  183. infraredScreenPositioningView.onManualNewPos(3 - index, markerObjects[index].transform.localPosition);
  184. }
  185. infraredScreenPositioningView.onManualToAutomatic();
  186. ////清空数据
  187. //currentPointIndex = 0;
  188. ////清空生成的数据
  189. //markerObjects.Clear();
  190. //foreach (Transform i in markerParent.transform)
  191. // Destroy(i.gameObject);
  192. }
  193. }