using System.Collections; using System.Collections.Generic; using UnityEngine; using exprivia.ui; using sam; class Orbit { public string productId; public SatelliteOrbitController orbitController; float distFromCenter; public Vector3[] normalizedVerts; public LineRenderer lr; public bool isIntersecting = false; public void ComputeDistanceFromCenter(Vector3 center) { distFromCenter = 100; foreach(var v in normalizedVerts) { float d = (v - center).sqrMagnitude; if (d < distFromCenter) distFromCenter = d; } //Debug.Log($"MinDist from center: {distFromCenter}"); } public bool ComputeIntersectionWithDisc(float radius) { isIntersecting = (radius >= distFromCenter); return isIntersecting; } } public class OrbitsIntersectionController : MonoBehaviour { public enum OrbitViewMode { HideAll, ShowAll, ShowSelected } public OrbitViewMode selectModeVisibility = OrbitViewMode.ShowAll; public OrbitViewMode orbitsModeVisibility = OrbitViewMode.ShowAll; OrbitViewMode curVisibility; public Canvas canvas; public Material orbitMaterial; public Color defaultColor = Color.yellow; public Color selectedColor = Color.red; public float lineWidth = 0.65f; public float estimatedProductSizeInMB = 8.9f; MultiDownloadSubsession mds; Satellite satellite; //List satControllers = new List(); List orbits = new List(); Transform orbitsParent; Vector3 curCenter; float curRadius = 0.1f; //SatelliteOrbitController selectedOrbit = null; bool ready = false; private void Awake() { mds = GetComponentInParent(); curVisibility = selectModeVisibility; orbitsParent = new GameObject($"OrbitsParent").GetComponent(); orbitsParent.SetParent(transform); if (curVisibility == OrbitViewMode.HideAll) orbitsParent.gameObject.SetActive(false); } public void OnProductListUpdated(List productNames) { orbits.Clear(); //satControllers.Clear(); satellite = new Satellite(mds.cfg.tleHistoryFilename, mds.cfg.satelliteId); int n = productNames.Count; for (int i = 0; i < n; i++) { Orbit orbit = new Orbit(); orbit.productId = (mds.cfg.productNameExtractionFunc != null)? mds.cfg.productNameExtractionFunc.ExtractProductName(productNames[i]) : productNames[i]; orbits.Add(orbit); SatelliteOrbitController soc = new GameObject($"Orbit-{i}", typeof(SatelliteOrbitController)).GetComponent(); //satControllers.Add(soc); orbit.orbitController = soc; //soc.gameObject.AddComponent(); soc.transform.SetParent(orbitsParent); soc.satellite = satellite; soc.orbitStartDateTime = mds.productDates[i].Item1.ToString("yyyy-MM-ddTHH:mm:ss"); soc.orbitEndDateTime = mds.productDates[i].Item2.ToString("yyyy-MM-ddTHH:mm:ss"); soc.width = lineWidth/*0.01f*/; soc.orbitMaterial = orbitMaterial; } } private IEnumerator Start() { while (orbits.Count == 0) yield return null; SatelliteOrbitController soc = orbits[0].orbitController; while (!ready) { yield return null; ready = soc.GetComponentInChildren() != null; } foreach (var orbit in orbits) { orbit.lr = orbit.orbitController.GetComponentInChildren(); orbit.lr.startColor = defaultColor; orbit.lr.endColor = defaultColor; orbit.normalizedVerts = new Vector3[orbit.lr.positionCount]; orbit.lr.GetPositions(orbit.normalizedVerts); for (int i=0; i < orbit.lr.positionCount; i++) { orbit.normalizedVerts[i] = orbit.normalizedVerts[i].normalized; } } OnCapCenterChanged(curCenter); estimatedProductSizeInMB = mds.cfg.estimatedProductSizeInMB; } public void OnEnterOrbitsMode((Vector3 center,float radius) cap) { curCenter = cap.center; curRadius = cap.radius; curVisibility = orbitsModeVisibility; orbitsParent.gameObject.SetActive(curVisibility != OrbitViewMode.HideAll); OnCapRadiusChanged(curRadius); } public void OnEnterSelectMode() { curVisibility = selectModeVisibility; orbitsParent.gameObject.SetActive(curVisibility != OrbitViewMode.HideAll); OnCapRadiusChanged(curRadius); } public void OnCapCenterChanged(Vector3 c) { curCenter = c; if (!ready) return; foreach (var orbit in orbits) { orbit.ComputeDistanceFromCenter(c); } OnCapRadiusChanged(curRadius); } public void OnCapRadiusChanged(float r) { curRadius = r; if (!ready) return; //Debug.Log($"curRadius={curRadius}"); Vector3 p1 = new Vector3(0.0f, 1.0f, 0.0f); Quaternion rot = Quaternion.Euler(r, 0.0f, 0.0f); Vector3 p2 = rot * p1; float d = (p2 - p1).sqrMagnitude; foreach (var orbit in orbits) { if (orbit.ComputeIntersectionWithDisc(d)) { orbit.lr.startColor = selectedColor; orbit.lr.endColor = selectedColor; orbit.orbitController.gameObject.SetActive(true); } else { orbit.lr.startColor = defaultColor; orbit.lr.endColor = defaultColor; orbit.orbitController.gameObject.SetActive(curVisibility != OrbitViewMode.ShowSelected); } } } public void OnDownloadButtonClicked() { List productIds = new List(); foreach (var orbit in orbits) { if (orbit.isIntersecting) productIds.Add(orbit.productId); } if (productIds.Count > 0) { Debug.Log($"DOWNLOAD {productIds.Count} SELECTED PRODUCTS!"); // Sprite warningIcon = Resources.Load("Icons/TransparentPixel"); int estimatedSize = Mathf.RoundToInt(estimatedProductSizeInMB * productIds.Count); PopUp.NewPopUp(canvas, null/*warningIcon*/, $"Confirm Download\n"+ $"Number of products: {productIds.Count}\n" + $"Estimated Size: {estimatedSize} MB", "Cancel", "Download", (i) => { Debug.Log($"Modal Dialog Closed with value: {i}"); if (i == 2) { Debug.Log($"SEND DOWNLOAD REQUEST FOR {productIds.Count} PRODUCTS"); DropBoxDownload.Download(productIds.ToArray()); } }); } } }