PointMarker.cs 9.8 KB

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