| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using LineUI;
- using UnityEngine.UI;
- using System.Linq;
- using ZIM;
- using o0;
- using Color = UnityEngine.Color;
- using InfraredManager;
- using Org.BouncyCastle.Asn1.Crmf;
- using ZIM.Unity;
- using o0.Project;
- using UnityEngine.SceneManagement;
- public class LinePosition
- {
- public int index;
- public List<Vector3> pos;
- }
- public class InfraredScreenPositioningView : JCUnityLib.ViewBase
- {
- [SerializeField]
- RectTransform canvasRectTransform;
- [SerializeField]
- RectTransform draggableParent;
- [SerializeField]
- GameObject mask;
- [SerializeField]
- GameObject cameraLight;
- [SerializeField]
- RectTransform pointsParent;
- [SerializeField]
- RectTransform pos1;
- [SerializeField]
- RectTransform pos2;
- [SerializeField]
- RectTransform pos3;
- [SerializeField]
- RectTransform pos4;
- //画线时候的点偏移量
- float offset = 10;
- [SerializeField]
- Line line;
- List<LinePosition> oldLinePosition;
- Vector3 beginPos;
- Vector3 endPos;
- //相机感光部分
- [SerializeField]
- Slider slider;
- [SerializeField]
- RawImage rawImage;
- [SerializeField]
- RectTransform crosshair;
- [SerializeField]
- RectTransform crosshairSmall;
- [SerializeField]
- GameObject textTip1;
- [SerializeField]
- GameObject textTip2;
- [SerializeField]
- Button btnAuto;
- [SerializeField]
- Button btnHandMovement;
- [SerializeField]
- BtnRecordInfrared btnRecordInfrared;
- bool bAuto = true;
- [SerializeField] Color normalColor = Color.white;
- [SerializeField] Color highlightedColor = Color.green;
- [SerializeField] Color normalTextColor = Color.black;
- [SerializeField] Color highlightedTextColor = Color.white;
- [SerializeField] TextAutoLanguage2 markerTextAutoLanguage2;
- [SerializeField]
- GameObject LayoutStart;
- [SerializeField]
- GameObject LayoutMarker;
- [SerializeField]
- GameObject LayoutSuccessful;
- [Tooltip("选择框的Line")]
- [SerializeField]
- Sprite[] ResultLines;// 0: 选中状态, 1: 未选中状态
- [Tooltip("选择框图片")]
- [SerializeField]
- Image[] ResultImages;
- [Tooltip("选择框按钮")]
- [SerializeField]
- Button[] ResultImagesButtons;
- [SerializeField]
- Button[] ResultButtons; // 结果选择按钮
- [SerializeField]
- RawImage CameraFirstImage; //第一个结果屏幕
- [SerializeField]
- RawImage CameraSecondImage; //第二个结果屏幕
- [SerializeField] LineGenerator FirstUILineGenerator;//第一个结果屏幕线条
- [SerializeField] LineGenerator SecondUILineGenerator;//第二个结果屏幕线条
- bool doLocateAuto;
- int DefaultResolutionIndex;
- private void Awake()
- {
- offset = line.MyThickness;
- }
- void Start()
- {
- doLocateAuto = false;
- textTip1.SetActive(true);
- //设置btnAuto 高亮
- OnButtonClick(btnAuto);
- textTip2.SetActive(false);
- //动态设置marker的提示语
- if (BluetoothAim.ins!=null && markerTextAutoLanguage2 != null)
- {
- string name = "";
- bool switchValue = false;
- if (BluetoothAim.ins.isMainConnectToHOUYIPRO())
- {
- name = TextAutoLanguage2.GetTextByKey("TitleTipMarker-HOUYIPro");
- }
- else if (BluetoothAim.ins.isMainConnectToARTEMISPRO())
- {
- name = TextAutoLanguage2.GetTextByKey("TitleTipMarker-ArtemisPro");
- }
- else if (BluetoothAim.ins.isMainConnectToGun())
- {
- //name = TextAutoLanguage2.GetTextByKey("TitleTipMarker-M9");
- switchValue = true;
- }
- if (switchValue)
- {
- markerTextAutoLanguage2.SetTextKey("TitleTipMarker-M9");
- }
- else {
- markerTextAutoLanguage2.textFormatArgs = new object[] { name };
- }
- }
- //手动不高亮
- //ResetButton(btnHandMovement);
- //quadUnityVectorList.Clear();
- //quadUnityVectorList.Add(new Vector2(16.39f, 35.91f));
- //quadUnityVectorList.Add(new Vector2(233.35f, 51.08f));
- //quadUnityVectorList.Add(new Vector2(94.15f, 219.11f));
- //quadUnityVectorList.Add(new Vector2(243.12f, 237.40f));
- Debug.Log("quadUnityVectorList count:" + ScreenLocate.quadUnityVectorList.Count);
- if (ScreenLocate.quadUnityVectorList.Count == 0)
- {
- Debug.Log("GetLocalPos");
- GetLocalPos();
- }
- else
- {
- Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize;
- Debug.Log("texSize:" + texSize + " = " + canvasRectTransform.rect);
- //自动识别时候四个点
- SyncQuadUnityVectorListToPos();
- //pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- //pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- //pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- //pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- }
- //记录操作的位置信息
- oldLinePosition = new List<LinePosition>();
- SetLinePos();
- //相机感光度
- if (InfraredDemo._ins)
- {
- //重置偏移量
- InfraredDemo._ins.ResetCenterOffset();
- //重置识别点
- ScreenLocate.Main.ScreenIdentification.ClearQuadCache();
- //清除一下记录的点
- ScreenLocate.quadUnityVectorList.Clear();
- slider.onValueChanged.AddListener((value) =>
- {
- InfraredDemo._ins.onSliderEvent(value);
- });
- InfraredDemo._ins.onSetSliderValue(slider);
- offset = line.MyThickness = InfraredDemo._ins.lineWidth.Get();
- }
- else {
- //编辑器使用
- if (Application.isEditor || Application.platform == RuntimePlatform.WindowsPlayer) {
- slider.onValueChanged.AddListener((value) =>
- {
- double originalMin = 0.0;
- double originalMax = 10.0;
- double targetMin = -1;
- double targetMax = 1;
- // 计算转换后的值
- double result = InfraredDemo.MapValue(value, originalMin, originalMax, targetMin, targetMax);
- //int _current = (int)(result);
- Debug.Log("_current:" + value + " , result:" + result);
- ScreenLocate.Main.pcContrast = (float)result;
- });
- float v = ScreenLocate.Main.pcContrast;
- // 目标区间 [0, 10] 的边界值
- double targetMin = 0.0;
- double targetMax = 10.0;
- double originalMin = -1;
- double originalMax = 1;
- // 计算转换后的值
- double v2 = InfraredDemo.MapValue(v, originalMin, originalMax, targetMin, targetMax);
- Debug.Log("PC获取相机的感光度:" + v + " = " + v2);
- slider.SetValueWithoutNotify((float)v2);
- }
- }
- initSelectInfo();
- //if (ScreenLocate.Main.getUVCTexture)
- //{
- // //渲染相机画面
- // rawImage.texture = ScreenLocate.Main.getUVCTexture;
- // //Debug.Log("rawImage.texture:" + rawImage.texture.name);
- //}
- //slider.onValueChanged.AddListener((value) => {
- // //onSliderEvent(value);
- // InfraredDemo._ins.SetBrightness(value);
- //});
- //slider.value = InfraredDemo._ins.brightness.Get();
- //修改分辨率。是否清晰一点?
- //DefaultResolutionIndex = InfraredDemo._ins?.ResolutionIndex ?? 0;
- //Debug.Log("[InfraredScreenPositioningView]开始记录进入时候的分辨率:" + DefaultResolutionIndex);
- //InfraredDemo._ins?.SetResolutionNew(ScreenLocate.Main.HighScreenLocateResolutionIndex);
- //if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked)
- //{
- // SB_EventSystem.ins.AwakenSimulateMouse();
- // // Debug.Log("simulate-mouse-close");
- //}
-
- }
- private void OnDestroy()
- {
- //修改回进入手动调节页面时候的分辨率
- //InfraredDemo._ins?.SetResolutionNew(DefaultResolutionIndex);
- Debug.Log("OnDestroy*********************************AwakenSimulateMouse:" + SB_EventSystem.ins.simulateMouseIsAwaked);
- if (SB_EventSystem.ins && !SB_EventSystem.ins.simulateMouseIsAwaked)
- {
- SB_EventSystem.ins.AwakenSimulateMouse();
- }
- }
- public RawImage Bg => rawImage;
- void Update()
- {
- if (enterFromZimWebCamera && ScreenLocate.Main.DebugOnZIMDemo) {
- // ZimWebCamera场景测试
- rawImage.texture = ScreenLocate.Main.outputRawImages[7].texture;
- if (ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.HasValue)
- {
- // 检测到光点
- var posInCanvas = ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(ScreenLocate.Main.CameraSize, rawImage.rectTransform.rect);
- crosshair.gameObject.SetActive(true);
- crosshair.anchoredPosition = posInCanvas;
- }
- else
- crosshair.gameObject.SetActive(false);
- //渲染相机画面
- CameraSecondImage.texture = rawImage.texture;
- CameraFirstImage.texture = rawImage.texture;
- }
- else if (InfraredDemo.running)
- {
- //渲染相机画面
- Texture texture = InfraredDemo.infraredCameraHelper.GetCameraTexture();
- if (rawImage.texture == null || texture.GetNativeTexturePtr() != rawImage.texture.GetNativeTexturePtr())
- {
- rawImage.texture = texture;
- //渲染相机画面
- CameraSecondImage.texture = rawImage.texture;
- CameraFirstImage.texture = rawImage.texture;
- }
- if (ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.HasValue)
- {
- // 检测到光点
- var posInCanvas = ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(ScreenLocate.Main.CameraSize, rawImage.rectTransform.rect);
- crosshairSmall.gameObject.SetActive(true);
- crosshairSmall.anchoredPosition = posInCanvas;
- }
- else
- crosshairSmall.gameObject.SetActive(false);
- //rawImage.material = InfraredDemo.infraredCameraHelper.GetCameraMaterial();
- }
- }
- public void OnClick_Back()
- {
- AudioMgr.ins.PlayBtn();
- ViewMgr.Instance.DestroyView<InfraredScreenPositioningView>();
- }
- /// <summary>
- /// 进入屏幕标记
- /// </summary>
- public void OnClick_EnterMarker() {
- LayoutStart.SetActive(false);
- LayoutMarker.SetActive(true);
- }
- /// <summary>
- /// 自动识别
- /// </summary>
- public void OnClick_Auto()
- {
- bAuto = true;
- doLocateAuto = true;
- textTip1.SetActive(false);
- textTip2.SetActive(true);
- ResetButton(btnAuto);
- if (enterFromZimWebCamera)
- {
- ScreenLocate _screenLocate = FindAnyObjectByType<ScreenLocate>();
- _screenLocate.EnterScreenLocateManualAuto();
- return;
- }
- if (InfraredDemo.running)
- {
- InfraredDemo.infraredCameraHelper.EnterScreenLocateManualAuto();
- }
-
- }
- /// <summary>
- /// 切换成手动方式
- /// </summary>
- public void onHandMovement()
- {
- bAuto = false;
- doLocateAuto = false;
- draggableParent.gameObject.SetActive(true);
- pointsParent.gameObject.SetActive(false);
- mask.SetActive(false);
- cameraLight.SetActive(true);
- }
- /// <summary>
- /// 自动识别后同步
- /// </summary>
- public void SyncScreenPosition()
- {
- //Debug.Log("quadUnityVectorList[i]:" + quadUnityVectorList[i]);
- Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize;
- Debug.Log("texSize:" + texSize + " = " + canvasRectTransform.rect);
- //自动识别时候四个点
- //pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- //pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- //pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- //pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- SyncQuadUnityVectorListToPos();
- SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
- //自动识别后隐藏手动按钮
- draggableParent.gameObject.SetActive(false);
- pointsParent.gameObject.SetActive(true);
- if (ScreenLocate.Main) {
- ZIM.Unity.QuadrilateralInCamera screen = ScreenLocate.Main.ScreenIdentification.Screen.QuadInCamera;
- //设置points点
- for (int i = 0; i < 4; i++)
- {
- RectTransform t = pointsParent.GetChild(i) as RectTransform;
- t.anchoredPosition = screen.Quad[i].pixelToLocalPosition_AnchorCenter(screen.CameraSize, pointsParent.rect);
- }
- }
- //显示最后结果
- LayoutMarker.SetActive(false);
- LayoutSuccessful.SetActive(true);
- //设置两个线段
- QuadrilateralInCamera screenAuto = ScreenLocate.Main.ScreenIdentification.QuadAuto;
- QuadrilateralInCamera screenSemiAuto = ScreenLocate.Main.ScreenIdentification.QuadSemiAuto;
- var lo = new Vector2(-0.5f, -0.5f);
- if (screenAuto != null)
- {
- FirstUILineGenerator.Points = new Vector2[4] {
- 2 * (new Vector2(screenAuto.Quad[0].x/texSize.x,screenAuto.Quad[0].y/texSize.y) + lo),
- 2 * (new Vector2(screenAuto.Quad[1].x/texSize.x,screenAuto.Quad[1].y/texSize.y) + lo),
- 2 * (new Vector2(screenAuto.Quad[3].x/texSize.x,screenAuto.Quad[3].y/texSize.y) + lo),
- 2 * (new Vector2(screenAuto.Quad[2].x/texSize.x,screenAuto.Quad[2].y/texSize.y) + lo) };
- // 打印 Points 的值,合并为一个字符串
- string pointsOutput = "1 Points: ";
- for (int i = 0; i < FirstUILineGenerator.Points.Length; i++)
- {
- pointsOutput += "[" + FirstUILineGenerator.Points[i].x + ", " + FirstUILineGenerator.Points[i].y + "]";
- if (i < FirstUILineGenerator.Points.Length - 1)
- {
- pointsOutput += ", "; // 添加逗号分隔符,最后一个元素后不加
- }
- }
- Debug.Log(pointsOutput);
- // 打印 Quad 的值,合并为一个字符串
- int[] customOrder = new int[] { 0, 1, 3, 2 };
- string quadOutput = "1 Quad Points: ";
- for (int i = 0; i < customOrder.Length; i++)
- {
- quadOutput += "[" + screenAuto.Quad[customOrder[i]].x + ", " + screenAuto.Quad[customOrder[i]].y + "]";
- if (i < customOrder.Length - 1)
- {
- quadOutput += ", "; // 添加逗号分隔符,最后一个元素后不加
- }
- }
- Debug.Log(quadOutput);
- }
- else {
- Debug.LogError("screenAuto 不存在!");
- }
- if (screenSemiAuto != null)
- {
- SecondUILineGenerator.Points = new Vector2[4] {
- 2 * (new Vector2(screenSemiAuto.Quad[0].x/texSize.x,screenSemiAuto.Quad[0].y/texSize.y) + lo),
- 2 * (new Vector2(screenSemiAuto.Quad[1].x/texSize.x,screenSemiAuto.Quad[1].y/texSize.y) + lo),
- 2 * (new Vector2(screenSemiAuto.Quad[3].x/texSize.x,screenSemiAuto.Quad[3].y/texSize.y) + lo),
- 2 * (new Vector2(screenSemiAuto.Quad[2].x/texSize.x,screenSemiAuto.Quad[2].y/texSize.y) + lo) };
- // 打印 Points 的值,合并为一个字符串
- string pointsOutput = "2 Points: ";
- for (int i = 0; i < SecondUILineGenerator.Points.Length; i++)
- {
- pointsOutput += "[" + SecondUILineGenerator.Points[i].x + ", " + SecondUILineGenerator.Points[i].y + "]";
- if (i < SecondUILineGenerator.Points.Length - 1)
- {
- pointsOutput += ", "; // 添加逗号分隔符,最后一个元素后不加
- }
- }
- Debug.Log(pointsOutput);
- // 打印 Quad 的值,合并为一个字符串
- int[] customOrder = new int[] { 0, 1, 3, 2 };
- string quadOutput = "2 Quad Points: ";
- for (int i = 0; i < customOrder.Length; i++)
- {
- quadOutput += "[" + screenSemiAuto.Quad[customOrder[i]].x + ", " + screenSemiAuto.Quad[customOrder[i]].y + "]";
- if (i < customOrder.Length - 1)
- {
- quadOutput += ", "; // 添加逗号分隔符,最后一个元素后不加
- }
- }
- Debug.Log(quadOutput);
- }
- else {
- Debug.LogError("screenSemiAuto 不存在!");
- }
- }
- #region 绘制线段部分
- //点击拖拽的开始位置
- public void onBeginPos(int index, Vector3 pos)
- {
- Debug.Log("pos begin: " + 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)
- {
- Debug.Log("pos end: " + 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()
- {
- Vector3[] v = new Vector3[4];
- pos1.GetWorldCorners(v);
- Vector3[] v1 = new Vector3[4];
- pos1.GetLocalCorners(v1);
- //for (int i = 0; i < 4; i++)
- //{
- // Debug.Log(i + " =1= " + v[i]);
- // Debug.Log(i + " =2= " + v1[i]);
- //}
- //记录一个操作点的操作位置
- List<Vector3> screenPositions = new List<Vector3>();
- 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>();
- //点为负数的增大 offset,正数减少 offset
- //Vector2 startPos1 = new Vector2(_pos1.x - pos1.rect.width * 0.5f + offset, _pos1.y - pos1.rect.height * 0.5f + offset);
- //Vector2 startPos2 = new Vector2(_pos2.x + pos2.rect.width * 0.5f - offset, _pos2.y - pos2.rect.height * 0.5f + offset);
- //Vector2 startPos3 = new Vector2(_pos3.x + pos3.rect.width * 0.5f - offset, _pos3.y + pos3.rect.height * 0.5f - offset);
- //Vector2 startPos4 = new Vector2(_pos4.x - pos4.rect.width * 0.5f + offset, _pos4.y + pos4.rect.height * 0.5f - offset);
- Vector2 startPos1 = new Vector2(_pos1.x + offset, _pos1.y + offset);
- Vector2 startPos2 = new Vector2(_pos2.x - offset, _pos2.y + offset);
- Vector2 startPos3 = new Vector2(_pos3.x - offset, _pos3.y - offset);
- Vector2 startPos4 = new Vector2(_pos4.x + offset, _pos4.y - 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]));
- }
- }
- //是不是从Demo界面进入该页面的
- public bool enterFromInfraredDemo { get; set; } = false;
- //是否从测试场景进入
- public bool enterFromZimWebCamera { get; set; } = false;
- //确认修改
- public void onConfirmation()
- {
- if (enterFromZimWebCamera)
- {
- ConfirmScreenLocateManualTest();
- if (oldLinePosition.Count > 1) // 确保列表不为空
- {
- LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
- oldLinePosition.Clear();
- oldLinePosition.Add(lastElement);
- }
- SaveLocalPos();
- AudioMgr.ins.PlayBtn();
- ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
- return;
- }
- if (!ConfirmScreenLocateManual()) return;
- if (oldLinePosition.Count > 1) // 确保列表不为空
- {
- LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
- oldLinePosition.Clear();
- oldLinePosition.Add(lastElement);
- }
- if (InfraredDemo.running)
- {
- //跳转入界面
- AudioMgr.ins.PlayBtn();
- if (!enterFromInfraredDemo)
- {
- //每次初始化重置一下引导
- InfraredDemo._ins.resetInfraredPlayerPrefs();
- //GameObject connectGuidanceView = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_ConnectGuidanceView);
- //connectGuidanceView.GetComponent<ConnectGuidanceView>().showTextipInfrared();
- if (!PlayerPrefs.HasKey("hideInfraredBowAndArrow"))
- {
- //如果是红外连接成功,记录一个tag
- PlayerPrefs.SetInt("hideInfraredBowAndArrow", 1);
- }
- onEnterInfrared();
- }
- else {
- ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
- }
-
- }
- //存储一次节点
- SaveLocalPos();
- }
- //重置位置
- public void onReset()
- {
- oldLinePosition.Clear();
- // 获取屏幕的四个角的像素坐标
- Vector2 bottomLeft = new Vector2(0, 0);
- // 将屏幕像素坐标转换为 Canvas 的局部坐标
- Vector2 localBottomLeft;
- RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, bottomLeft, null, out localBottomLeft);
- // 打印结果
- Debug.Log("Local Bottom Left: " + localBottomLeft);
- //int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x) - 120), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y) - 77);
- int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x)), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y));
- pos1.anchoredPosition = new Vector3(-_x, -_y, 0); //Bottom Left
- pos2.anchoredPosition = new Vector3(_x, -_y, 0); //Bottom Right
- pos3.anchoredPosition = new Vector3(_x, _y, 0); //Top Right
- pos4.anchoredPosition = new Vector3(-_x, _y, 0); //Top Left
- //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();
- btnRecordInfrared.Reset();
- ScreenLocate.Main.ScreenIdentification.ClearQuadCache();
- if (enterFromZimWebCamera)
- {
- ScreenLocate.Main.ScreenQuad.gameObject.SetActive(false);
- ScreenLocate.Main.UILineGenerator.Points = new Vector2[0];
- }
- }
- /// <summary>
- /// 标准四个点
- /// </summary>
- /// <param name="index"></param>
- /// <param name="pos"></param>
- public void onManualNewPos(int index, Vector3 pos)
- {
- Debug.Log("pos end: " + pos);
- if (index == 0)
- pos1.localPosition = pos;
- else if (index == 1)
- pos2.localPosition = pos;
- else if (index == 2)
- pos3.localPosition = pos;
- else if (index == 3)
- pos4.localPosition = pos;
- //再记录一次最后的点
- SetLinePos();
- }
- /// <summary>
- /// 处理新流程,先记录手动,然后到自动识别处理
- /// </summary>
- public void onManualToAutomatic() {
- if (enterFromZimWebCamera)
- {
- ConfirmScreenLocateManualTest();
- if (oldLinePosition.Count > 1) // 确保列表不为空
- {
- LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
- oldLinePosition.Clear();
- oldLinePosition.Add(lastElement);
- }
- SaveLocalPos();
- //到自动
- OnClick_Auto();
- return;
- }
- if (!ConfirmScreenLocateManual()) {
- Debug.LogError("ConfirmScreenLocateManual is false!");
- return;
- }
- if (oldLinePosition.Count > 1) // 确保列表不为空
- {
- LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
- oldLinePosition.Clear();
- oldLinePosition.Add(lastElement);
- }
- //存储一次节点
- SaveLocalPos();
- bAuto = true;
- doLocateAuto = true;
- //自动校准
- InfraredDemo.infraredCameraHelper.EnterScreenLocateManualAuto();
- }
- #endregion
- List<Vector2> _locatePointList = new();
- float _texWidth;
- float _texHeight;
- void RecordLocatePoint(RectTransform p, Vector2 pivot)
- {
- Vector2 pos = JCUnityLib.RectTransformUtils.GetPositionByPivot(p, pivot);
- pos.x = Mathf.Clamp01(pos.x / Screen.width);
- pos.y = Mathf.Clamp01(pos.y / Screen.height);
- _locatePointList.Add(pos);
- }
- bool ConfirmScreenLocateManual()
- {
- if (InfraredDemo.running)
- {
- //渲染截图
- Texture2D texture2D = InfraredDemo.infraredCameraHelper.EnterScreenLocateManual();
- if (texture2D == null)
- {
- Debug.Log("EnterScreenLocateManual = null");
- InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(null);
- return false;
- }
- _locatePointList.Clear();
- _texWidth = texture2D.width;
- _texHeight = texture2D.height;
- RecordLocatePoint(pos1, new Vector2(0, 0));
- RecordLocatePoint(pos2, new Vector2(1, 0));
- RecordLocatePoint(pos3, new Vector2(1, 1));
- RecordLocatePoint(pos4, new Vector2(0, 1));
- //RecordLocatePoint(pos1, new Vector2(0.5f, 0.5f));
- //RecordLocatePoint(pos2, new Vector2(0.5f, 0.5f));
- //RecordLocatePoint(pos3, new Vector2(0.5f, 0.5f));
- //RecordLocatePoint(pos4, new Vector2(0.5f, 0.5f));
- InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(_locatePointList);
- FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, 1, 1);
- //同步数据
- ScreenLocate.quadUnityVectorList.Clear();
- ScreenLocate.quadUnityVectorList.Add(_locatePointList[0]);
- ScreenLocate.quadUnityVectorList.Add(_locatePointList[1]);
- //两个点切换,顺序不一样
- ScreenLocate.quadUnityVectorList.Add(_locatePointList[3]);
- ScreenLocate.quadUnityVectorList.Add(_locatePointList[2]);
- ScreenLocate.SaveScreenLocateVectorList();
- return true;
- }
- return false;
- }
- void ConfirmScreenLocateManualTest()
- {
- //渲染截图
- ScreenLocate _screenLocate = FindAnyObjectByType<ScreenLocate>();
- Texture2D texture2D = _screenLocate.EnterScreenLocateManual();
- if (texture2D == null)
- {
- _screenLocate.QuitScreenLocateManual(null);
- return;
- }
- _locatePointList.Clear();
- _texWidth = texture2D.width;
- _texHeight = texture2D.height;
- RecordLocatePoint(pos1, new Vector2(0, 0));
- RecordLocatePoint(pos2, new Vector2(1, 0));
- RecordLocatePoint(pos3, new Vector2(1, 1));
- RecordLocatePoint(pos4, new Vector2(0, 1));
- _screenLocate.QuitScreenLocateManual(_locatePointList);
- //FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, _texWidth, _texHeight);
- setPointsLocation(_locatePointList,pointsParent.gameObject,!bAuto);
- if (!doLocateAuto) // 设置手动定位数据
- setPointsManual(_locatePointList, GameObject.Find("WebCameraView/CameraImage0/ScreenQuad"));
- //同步数据
- ScreenLocate.quadUnityVectorList.Clear();
- ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[0].x, _locatePointList[0].y));
- ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[1].x, _locatePointList[1].y));
- //两个点切换,顺序不一样
- ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[3].x, _locatePointList[3].y));
- ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[2].x, _locatePointList[2].y));
- ScreenLocate.SaveScreenLocateVectorList();
- }
- void setPointsLocation(List<Vector2> targetList, GameObject pointsTF2, bool active = true)
- {
- //GameObject pointsTF2 = GameObject.Find("WebCameraView/CameraImage0/ScreenQuad");
- pointsTF2.SetActive(active);
- if (pointsTF2.transform.childCount == targetList.Count)
- {
- for (int i = 0; i < pointsTF2.transform.childCount; i++)
- {
- Transform pointTF = pointsTF2.transform.GetChild(i);
- Vector2 pos = targetList[i];
- pointTF.localPosition = pos.pixelToLocalPosition_AnchorCenter(Vector2.one, pointsTF2.GetComponent<RectTransform>().rect);
- pointTF.gameObject.SetActive(true);
- }
- }
-
- }
- void setPointsManual(List<Vector2> targetList, GameObject pointsTF2, bool active = true)
- {
- setPointsLocation(targetList, pointsTF2, active);
- var lo = new Vector2(-0.5f, -0.5f);
- ScreenLocate.Main.UILineGenerator.Points = new Vector2[4] {
- 2 * (targetList[0] + lo),
- 2 * (targetList[1] + lo),
- 2 * (targetList[2] + lo),
- 2 * (targetList[3] + lo) };
- }
- void SaveLocalPos()
- {
- List<Vector3> screenPositions = new List<Vector3>();
- screenPositions.Add(pos1.anchoredPosition);
- screenPositions.Add(pos2.anchoredPosition);
- screenPositions.Add(pos3.anchoredPosition);
- screenPositions.Add(pos4.anchoredPosition);
- string saveStr = string.Join(';', screenPositions.Select(v => $"{v.x},{v.y}")); //,{v.z}
- Debug.Log("Local UI Position: " + saveStr);
- PlayerPrefs.SetString("ScreenPositioningView", saveStr);
- }
- void GetLocalPos()
- {
- string posListStr = PlayerPrefs.GetString("ScreenPositioningView", "");
- if (!string.IsNullOrWhiteSpace(posListStr))
- {
- List<Vector2> posList = posListStr.Split(';')
- .Select(s =>
- {
- string[] parts = s.Split(',');
- return new Vector2(float.Parse(parts[0]), float.Parse(parts[1]));
- })
- .ToList();
- pos1.anchoredPosition = posList[0];
- pos2.anchoredPosition = posList[1];
- pos3.anchoredPosition = posList[2];
- pos4.anchoredPosition = posList[3];
- SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
- }
- }
- #region 按钮颜色切换
- private void OnButtonClick(Button button)
- {
- // 切换按钮颜色
- ColorBlock colors = button.colors;
- colors.normalColor = highlightedColor;
- colors.highlightedColor = highlightedColor;
- button.colors = colors;
- // 切换字体颜色
- Text buttonText = button.GetComponentInChildren<Text>();
- buttonText.color = highlightedTextColor;
- }
- private void ResetButton(Button button)
- {
- // 重置按钮颜色
- ColorBlock colors = button.colors;
- colors.normalColor = normalColor;
- colors.highlightedColor = normalColor;
- button.colors = colors;
- // 重置字体颜色
- Text buttonText = button.GetComponentInChildren<Text>();
- buttonText.color = normalTextColor;
- }
- #endregion
- #region 标定完成之后进入游戏界面
- void onEnterInfrared()
- {
- //添加进入射箭场景
- if (PlayerPrefs.GetInt("entry-guider-infrared-" + LoginMgr.myUserInfo.id, 0) == 0)
- {
- Debug.Log("-----进入射箭场景!");
- NewUserGuiderManager newUserGuiderManager = FindObjectOfType<NewUserGuiderManager>();
- newUserGuiderManager.curConfigKey = "开始-红外调整";
- newUserGuiderManager.isNewModule = AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.HOUYIPRO || AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.Gun || AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.ARTEMISPRO;
- //进入射箭场景
- GlobalData.pkMatchType = PKMatchType.None;
- GameMgr.gameType = 1;
- //射一箭回到连接页面,Device.view
- GameMgr.bNavBack = true;
- GameMgr.bShowDistance = false;
- AimHandler.ins.bInitOne = false;//true
- //关闭计时器
- GameMgr.turnOffTimer = true;
- //关闭左边靶子和底部速度栏
- GameMgr.HideTargetView = true;
- GameMgr.HideBillboard = true;
- GameMgr.ButtonCount = 0;
- UnityEngine.SceneManagement.SceneManager.LoadScene(
- "Game", UnityEngine.SceneManagement.LoadSceneMode.Single);
- PlayerPrefs.SetInt("entry-guider-infrared-" + LoginMgr.myUserInfo.id, 1);
- }
- else
- {
- Debug.Log("-----跳转回连接界面!");
- //如不是第一次,则应该跳转回连接界面
- ViewMgr.Instance.DestroyView<SmartArcheryView>();
- ViewManager2.HideView(ViewManager2.Path_ConnectGuidanceView);
- }
- }
- #endregion
- #region 最后选择算法处理UI
- int selected = -1;
- void initSelectInfo() {
- for (int i = 0; i < 2; i++)
- {
- int currentIndex = i; // 将 i 赋值给局部变量
- ResultImagesButtons[currentIndex].onClick.AddListener(() => SelectImage(currentIndex));
- //ResultButtons[currentIndex].onClick.AddListener(() =>
- //{
- // if (currentIndex == 0)
- // {
- // OnFirstResult();
- // }
- // else if (currentIndex == 1)
- // {
- // OnSecondResult();
- // }
- //});
- }
- //默认选择1 自动
- SelectImage(0);
- }
- // 添加一个选择函数
- void SelectImage(int selectedIndex)
- {
- //如果重复选中则跳转
- if (selected == selectedIndex) {
- if (selected == 0)
- {
- OnFirstResult();
- }
- else if (selected == 1)
- {
- OnSecondResult();
- }
- return;
- }
- // 遍历所有图片
- for (int i = 0; i < ResultImages.Length; i++)
- {
- // 如果是选中的 index,赋值为 ResultLines[0],否则赋值为 ResultLines[1]
- if (i == selectedIndex)
- {
- selected = i;
- ResultImages[i].sprite = ResultLines[0];
- ResultButtons[i].interactable = true;
- }
- else
- {
- ResultImages[i].sprite = ResultLines[1];
- ResultButtons[i].interactable = false;
- }
- }
- }
- //选择全自动结果后进入游戏
- void OnFirstResult()
- {
- ScreenLocate.Main.SelectScreenAfterLocate(ScreenLocate.ScreenIdentificationTag.Auto);
- UpdateQuadUnityVectorList();
- onCompelete();
- }
- //选择半自动结果
- void OnSecondResult()
- {
- ScreenLocate.Main.SelectScreenAfterLocate(ScreenLocate.ScreenIdentificationTag.SemiAuto);
- UpdateQuadUnityVectorList();
- onCompelete();
- }
- public void onCompelete()
- {
- if (enterFromZimWebCamera)
- {
- SaveLocalPos();
- ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
- return;
- }
- if (InfraredDemo.running)
- {
- //跳转入界面
- AudioMgr.ins.PlayBtn();
- if (!enterFromInfraredDemo)
- {
- //每次初始化重置一下引导
- InfraredDemo._ins.resetInfraredPlayerPrefs();
- //GameObject connectGuidanceView = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_ConnectGuidanceView);
- //connectGuidanceView.GetComponent<ConnectGuidanceView>().showTextipInfrared();
- if (!PlayerPrefs.HasKey("hideInfraredBowAndArrow"))
- {
- //如果是红外连接成功,记录一个tag
- PlayerPrefs.SetInt("hideInfraredBowAndArrow", 1);
- }
- onEnterInfrared();
- }
- else
- {
- ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
- }
- }
- //存储一次节点
- SaveLocalPos();
- }
- /// <summary>
- /// 选择模式后更新 quadUnityVectorList
- /// </summary>
- void UpdateQuadUnityVectorList() {
- ScreenLocate.Main.UpdateQuadUnityVectorList();
- SyncQuadUnityVectorListToPos();
- InfraredDemo._ins?.SetLocatePointsToCameraRender(ScreenLocate.quadUnityVectorList, 1, 1);
- _locatePointList.Clear();
-
- }
- void SyncQuadUnityVectorListToPos() {
- Debug.Log("[ScreenLocate] SyncQuadUnityVectorListToPos quadUnityVectorList:" + ScreenLocate.Main.PrintVector2List(ScreenLocate.quadUnityVectorList));
- pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
- }
- //这个脚本存在时候。任何切换操作都直接处理删除
- void OnSceneUnloaded(Scene scene)
- {
- ViewManager2.HideView(ViewManager2.Path_ConnectGuidanceView);
- ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
- }
- void OnEnable()
- {
- SceneManager.sceneUnloaded += OnSceneUnloaded;
- }
- void OnDisable()
- {
- SceneManager.sceneUnloaded -= OnSceneUnloaded;
- }
- #endregion
- }
|