InfraredScreenPositioningView.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LineUI;
  5. using UnityEngine.UI;
  6. public class LinePosition {
  7. public int index;
  8. public List<Vector2> pos;
  9. }
  10. public class InfraredScreenPositioningView : JCUnityLib.ViewBase
  11. {
  12. [SerializeField]
  13. RectTransform pos1;
  14. [SerializeField]
  15. RectTransform pos2;
  16. [SerializeField]
  17. RectTransform pos3;
  18. [SerializeField]
  19. RectTransform pos4;
  20. //画线时候的点偏移量
  21. [SerializeField]
  22. int offset = 20;
  23. [SerializeField]
  24. Line line;
  25. List<LinePosition> oldLinePosition;
  26. Vector3 beginPos;
  27. Vector3 endPos;
  28. //相机感光部分
  29. public Slider slider;
  30. void Start()
  31. {
  32. //记录操作的位置信息
  33. oldLinePosition = new List<LinePosition>();
  34. SetLinePos();
  35. //相机感光度
  36. slider.onValueChanged.AddListener((value)=> {
  37. onSliderEvent(value);
  38. });
  39. onSetSliderValue();
  40. }
  41. public void OnClick_Back()
  42. {
  43. AudioMgr.ins.PlayBtn();
  44. ViewMgr.Instance.DestroyView<InfraredScreenPositioningView>();
  45. }
  46. #region 绘制线段部分
  47. //点击拖拽的开始位置
  48. public void onBeginPos(int index, Vector3 pos)
  49. {
  50. beginPos = pos;
  51. }
  52. public void onDragPos(int index, Vector3 pos)
  53. {
  54. //设置线段的点
  55. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  56. }
  57. //点击拖拽的结束位置
  58. public void onEndPos(int index, Vector3 pos)
  59. {
  60. endPos = pos;
  61. if (beginPos == endPos) return;
  62. //Debug.Log(index+",最后的点:" + pos);
  63. //再记录一次最后的点
  64. SetLinePos();
  65. }
  66. //同步设置图片对应的位置到line
  67. public void SetLinePos()
  68. {
  69. //记录一个操作点的操作位置
  70. AddOldLinePosition();
  71. //设置线段的点
  72. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  73. }
  74. void AddOldLinePosition()
  75. {
  76. //记录一个操作点的操作位置
  77. List<Vector2> screenPositions = new List<Vector2>();
  78. screenPositions.Add(pos1.localPosition);
  79. screenPositions.Add(pos2.localPosition);
  80. screenPositions.Add(pos3.localPosition);
  81. screenPositions.Add(pos4.localPosition);
  82. LinePosition linePosition = new LinePosition();
  83. linePosition.index = oldLinePosition.Count;
  84. linePosition.pos = screenPositions;
  85. oldLinePosition.Add(linePosition);
  86. }
  87. List<Vector2> linePosConversion(Vector3 _pos1, Vector3 _pos2, Vector3 _pos3, Vector3 _pos4)
  88. {
  89. List<Vector2> _screenPositions = new List<Vector2>();
  90. Vector2 startPos1 = new Vector2(_pos1.x - pos1.rect.width * 0.25f - offset, _pos1.y - pos1.rect.height * 0.25f - offset);
  91. Vector2 startPos2 = new Vector2(_pos2.x + pos2.rect.width * 0.25f + offset, _pos2.y - pos2.rect.height * 0.25f - offset);
  92. Vector2 startPos3 = new Vector2(_pos3.x + pos3.rect.width * 0.25f + offset, _pos3.y + pos3.rect.height * 0.25f + offset);
  93. Vector2 startPos4 = new Vector2(_pos4.x - pos4.rect.width * 0.25f - offset, _pos4.y + pos4.rect.height * 0.25f + offset);
  94. _screenPositions.Add(startPos1);
  95. _screenPositions.Add(startPos2);
  96. _screenPositions.Add(startPos3);
  97. _screenPositions.Add(startPos4);
  98. return _screenPositions;
  99. }
  100. void SetRectanglePoints(List<Vector2> screenPositions)
  101. {
  102. line.SetLine(screenPositions);
  103. }
  104. //撤回上一个元素
  105. public void onRecall()
  106. {
  107. // 获取并删除最后一个元素,并且保留一个元素
  108. if (oldLinePosition.Count > 1) // 确保列表不为空
  109. {
  110. // 获取回退的那个元素点
  111. LinePosition lastElement_second = oldLinePosition[oldLinePosition.Count - 2];
  112. // 获取最后一个元素
  113. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  114. //Debug.Log(JsonUtility.ToJson(lastElement) + " = " + oldLinePosition.Count);
  115. oldLinePosition.RemoveAt(oldLinePosition.Count - 1); // 删除最后一个元素
  116. pos1.localPosition = lastElement_second.pos[0];
  117. pos2.localPosition = lastElement_second.pos[1];
  118. pos3.localPosition = lastElement_second.pos[2];
  119. pos4.localPosition = lastElement_second.pos[3];
  120. //设置线段的点
  121. SetRectanglePoints(linePosConversion(lastElement_second.pos[0], lastElement_second.pos[1], lastElement_second.pos[2], lastElement_second.pos[3]));
  122. }
  123. }
  124. //确认修改
  125. public void onConfirmation()
  126. {
  127. if (oldLinePosition.Count > 1) // 确保列表不为空
  128. {
  129. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  130. oldLinePosition.Clear();
  131. oldLinePosition.Add(lastElement);
  132. }
  133. }
  134. //设置位置
  135. public void onReset()
  136. {
  137. oldLinePosition.Clear();
  138. int _x = 120, _y = 77;
  139. pos1.anchoredPosition = new Vector3(_x, _y, 0);
  140. pos2.anchoredPosition = new Vector3(-_x, _y, 0);
  141. pos3.anchoredPosition = new Vector3(-_x, -_y, 0);
  142. pos4.anchoredPosition = new Vector3(_x, -_y, 0);
  143. //设置一次位置
  144. SetLinePos();
  145. }
  146. #endregion
  147. #region 相机感光度
  148. public void onSetSliderValue() {
  149. slider.value = 5;
  150. }
  151. public void onSliderEvent(float value) {
  152. Debug.Log(value);
  153. }
  154. #endregion
  155. }