| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using LineUI;
- using UnityEngine.UI;
- public class InfraredScreenPositioningView1 : MonoBehaviour
- {
- [SerializeField]
- RectTransform pos1;
- [SerializeField]
- RectTransform pos2;
- [SerializeField]
- RectTransform pos3;
- [SerializeField]
- RectTransform pos4;
- //画线时候的点偏移量
- [SerializeField]
- int offset = 20;
- [SerializeField]
- Line line;
- List<LinePosition> oldLinePosition;
- Vector3 beginPos;
- Vector3 endPos;
- //相机感光部分
- public Slider slider;
- void Start()
- {
- //记录操作的位置信息
- oldLinePosition = new List<LinePosition>();
- SetLinePos();
- //相机感光度
- slider.onValueChanged.AddListener((value)=> {
- onSliderEvent(value);
- });
- onSetSliderValue();
- }
- public void OnClick_Back()
- {
- AudioMgr.ins.PlayBtn();
- ViewMgr.Instance.DestroyView<InfraredScreenPositioningView>();
- }
- #region 绘制线段部分
- //点击拖拽的开始位置
- public void onBeginPos(int index, Vector3 pos)
- {
- beginPos = pos;
- }
- public void onDragPos(int index, Vector3 pos)
- {
- //设置线段的点
- SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
- }
- //点击拖拽的结束位置
- public void onEndPos(int index, Vector3 pos)
- {
- endPos = pos;
- if (beginPos == endPos) return;
- //Debug.Log(index+",最后的点:" + pos);
- //再记录一次最后的点
- SetLinePos();
- }
- //同步设置图片对应的位置到line
- public void SetLinePos()
- {
- //记录一个操作点的操作位置
- AddOldLinePosition();
- //设置线段的点
- SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
- }
- void AddOldLinePosition()
- {
- //记录一个操作点的操作位置
- List<Vector2> screenPositions = new List<Vector2>();
- screenPositions.Add(pos1.localPosition);
- screenPositions.Add(pos2.localPosition);
- screenPositions.Add(pos3.localPosition);
- screenPositions.Add(pos4.localPosition);
- LinePosition linePosition = new LinePosition();
- linePosition.index = oldLinePosition.Count;
- linePosition.pos = screenPositions;
- oldLinePosition.Add(linePosition);
- }
- List<Vector2> linePosConversion(Vector3 _pos1, Vector3 _pos2, Vector3 _pos3, Vector3 _pos4)
- {
-
- List<Vector2> _screenPositions = new List<Vector2>();
- Vector2 startPos1 = new Vector2(_pos1.x - pos1.rect.width * 0.25f - offset, _pos1.y - pos1.rect.height * 0.25f - offset);
- Vector2 startPos2 = new Vector2(_pos2.x + pos2.rect.width * 0.25f + offset, _pos2.y - pos2.rect.height * 0.25f - offset);
- Vector2 startPos3 = new Vector2(_pos3.x + pos3.rect.width * 0.25f + offset, _pos3.y + pos3.rect.height * 0.25f + offset);
- Vector2 startPos4 = new Vector2(_pos4.x - pos4.rect.width * 0.25f - offset, _pos4.y + pos4.rect.height * 0.25f + offset);
- _screenPositions.Add(startPos1);
- _screenPositions.Add(startPos2);
- _screenPositions.Add(startPos3);
- _screenPositions.Add(startPos4);
- return _screenPositions;
- }
- void SetRectanglePoints(List<Vector2> screenPositions)
- {
- line.SetLine(screenPositions);
- }
- //撤回上一个元素
- public void onRecall()
- {
- // 获取并删除最后一个元素,并且保留一个元素
- if (oldLinePosition.Count > 1) // 确保列表不为空
- {
- // 获取回退的那个元素点
- LinePosition lastElement_second = oldLinePosition[oldLinePosition.Count - 2];
- // 获取最后一个元素
- LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
- //Debug.Log(JsonUtility.ToJson(lastElement) + " = " + oldLinePosition.Count);
- oldLinePosition.RemoveAt(oldLinePosition.Count - 1); // 删除最后一个元素
- pos1.localPosition = lastElement_second.pos[0];
- pos2.localPosition = lastElement_second.pos[1];
- pos3.localPosition = lastElement_second.pos[2];
- pos4.localPosition = lastElement_second.pos[3];
- //设置线段的点
- SetRectanglePoints(linePosConversion(lastElement_second.pos[0], lastElement_second.pos[1], lastElement_second.pos[2], lastElement_second.pos[3]));
- }
- }
- //确认修改
- public void onConfirmation()
- {
- if (oldLinePosition.Count > 1) // 确保列表不为空
- {
- LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
- oldLinePosition.Clear();
- oldLinePosition.Add(lastElement);
- }
- //跳转入界面
- AudioMgr.ins.PlayBtn();
- //ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
- //ViewManager2.ShowView(ViewManager2.Path_ConnectGuidanceView);
- Destroy(gameObject);
- Quit();
- }
- //设置位置
- public void onReset()
- {
- oldLinePosition.Clear();
- int _x = 120, _y = 77;
- pos1.anchoredPosition = new Vector3(_x, _y, 0);
- pos2.anchoredPosition = new Vector3(-_x, _y, 0);
- pos3.anchoredPosition = new Vector3(-_x, -_y, 0);
- pos4.anchoredPosition = new Vector3(_x, -_y, 0);
- //设置一次位置
- SetLinePos();
- }
- #endregion
- #region 相机感光度
- public void onSetSliderValue() {
- if (InfraredDemo.running)
- {
- float v = InfraredDemo.infraredCameraHelper.GetBrightness();
- slider.SetValueWithoutNotify(v);
- }
- else slider.SetValueWithoutNotify(5);
- }
- public void onSliderEvent(float value) {
- Debug.Log(value);
- //修改亮度时,调试界面的亮度也应该一起修改
- if (InfraredDemo.running)
- {
- Slider slider = FindObjectOfType<InfraredDemo>()
- .transform.Find("InfraredCamera/Layout/SliderBrightness")
- .GetComponent<Slider>();
- slider.value = value;
- }
- }
- #endregion
- public static void Enter(Texture2D texture2D)
- {
- InfraredScreenPositioningView1 view = FindObjectOfType<InfraredScreenPositioningView1>();
- if (view) return;
- GameObject prefab = Resources.Load<GameObject>("InfraredScreenPositioningView 1");
- GameObject o = Instantiate(prefab);
- if (InfraredDemo.running)
- {
- view = o.GetComponent<InfraredScreenPositioningView1>();
- Transform bg = o.transform.Find("ScreenPositioningView/BG");
- DestroyImmediate(bg.GetComponent<Image>());
- RawImage rawImage = bg.gameObject.AddComponent<RawImage>();
- rawImage.texture = texture2D;
- rawImage.material = InfraredDemo.infraredCameraHelper.GetCameraMaterial();
- view._texWidth = texture2D.width;
- view._texHeight = texture2D.height;
- }
- }
- List<Vector2> _locatePointList = new();
- float _texWidth;
- float _texHeight;
- void RecordLocatePoint(RectTransform p, Vector2 pivot)
- {
- Vector2 pos = JCUnityLib.RectTransformUtils.GetPositionByPivot(p, pivot);
- pos.x = pos.x / Screen.width * _texWidth;
- pos.y = pos.y / Screen.height * _texHeight;
- _locatePointList.Add(pos);
- }
- void Quit()
- {
- if (InfraredDemo.running && InfraredDemo.infraredCameraHelper.IsScreenLocateManualDoing())
- {
- RecordLocatePoint(pos1, new Vector2(0, 0));
- RecordLocatePoint(pos2, new Vector2(1, 0));
- RecordLocatePoint(pos3, new Vector2(1, 1));
- RecordLocatePoint(pos4, new Vector2(0, 1));
- InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(_locatePointList);
- FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, _texWidth, _texHeight);
- }
- }
- }
|