using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using sam; using System; using exprivia; using System.IO; public class HeightVariableSubsession : MonoBehaviour, IProductDataConsumer { public Text prodNameText; public Text varNameText; public Text minValText; public Text maxValText; public Text curValText; public Text latText; public Text lonText; public Text altText; public Text dateText; public string dateFormat; public Image lut; public float lutRange = 40.0f; public Color fillColor; Texture2D lutTexture; int luty; [HideInInspector] public int initiallySelectedVariableIndex = 0; [HideInInspector] public ProductData data; [HideInInspector] public ProductProfile profile; [HideInInspector] public int variableIndex; [HideInInspector] public ProductVariable prodvar; public ProductDataPoint selectedDP = null; float lastSelectedX = -1f; float lastSelectedY = -1f; List latStopList, lonStopList, altBottomList, altTopList; List durationList; void IProductDataConsumer.SetInitiallySelectedVariableIndex(int i) { initiallySelectedVariableIndex = i; } ProductData IProductDataConsumer.GetSelectedProduct() { return data; } SatelliteHeightDataConfig cfg; private void Awake() { lutTexture = lut.sprite.texture; luty = lutTexture.height >> 1; cfg = GlobalDictionary.GetValue("SatelliteDataTypeConfig") as SatelliteHeightDataConfig; lutRange = cfg.lutRange; } public virtual Color GetDataPointColor(ProductDataPoint dp) { //return Random.ColorHSV(); if (!dp.varValid[variableIndex]) { return fillColor; } float val = dp.variables[variableIndex]; //float frac = ((val - prodvar.scaledMinValue) / prodvar.scaledRange); float frac = (val + lutRange) / (2*lutRange); frac = Mathf.Clamp(frac, 0.0f, 1.0f); float tc = frac * lutTexture.width; int intCoord = (int)tc; float fracCoord = tc - intCoord; Color val1 = lutTexture.GetPixel(intCoord, luty); Color val2 = lutTexture.GetPixel(Math.Min(intCoord + 1, lutTexture.width - 1), luty); Color c = Color.Lerp(val1, val2, fracCoord); c.a = 1f; return c; } public void OnBackButtonPressed() { string previousSceneName = (string)GlobalDictionary.GetValue("DataVariableParentSceneName"); if (!string.IsNullOrEmpty(previousSceneName)) { Debug.Log($"SWITCHING BACK TO SCENE {previousSceneName}"); exprivia.ui.Utils.LoadScene(previousSceneName); } else { Debug.Log("SHOULD GO BACK TO PREVIOUS SCENE"); } } void IProductDataConsumer.SelectProduct(ProductData pd) { data = pd; profile = data.profile; variableIndex = -1; prodvar = null; prodNameText.text = data.referenceProductName; BroadcastMessage("OnProductSelected", data, SendMessageOptions.DontRequireReceiver); ProductUserData ud = data.profile.GetUserData("LatStop"); latStopList = (data.userData[ud.fieldIndex] as List); ud = data.profile.GetUserData("LonStop"); lonStopList = (data.userData[ud.fieldIndex] as List); ud = data.profile.GetUserData("AltBottom"); altBottomList = (data.userData[ud.fieldIndex] as List); ud = data.profile.GetUserData("AltTop"); altTopList = (data.userData[ud.fieldIndex] as List); ud = data.profile.GetUserData("DeltaSeconds"); durationList = (data.userData[ud.fieldIndex] as List); SelectVariable(initiallySelectedVariableIndex); } public void SelectVariable(int varInd) { if (varInd == variableIndex) return; variableIndex = varInd; prodvar = profile.variables[variableIndex]; varNameText.text = prodvar.name; minValText.text = (-lutRange).ToString() + prodvar.unit; // prodvar.ScaledMinValWithUnit(); maxValText.text = lutRange.ToString() + prodvar.unit; // prodvar.ScaledMaxValWithUnit(); BroadcastMessage("OnVariableSelected", variableIndex, SendMessageOptions.DontRequireReceiver); } public void SelectDataPoint(ProductDataPoint dp, float x, float y) { if ((selectedDP != dp) || (lastSelectedX != x) || (lastSelectedY != y)) { if (dp == null) { latText.text = "Lat: " /*+ Utils.FormatLatitude(selectedLat)*/; lonText.text = "Lon: " /*+ Utils.FormatLongitude(selectedLon)*/; altText.text = "Alt: "; curValText.text = ""; dateText.text = ""; if (selectedDP != null) BroadcastMessage("OnDataPointDeselected", SendMessageOptions.DontRequireReceiver); } else { float lonStart = dp.longitude; float lonEnd = lonStopList[dp.dataPointIndex]; if (Mathf.Abs(lonStart - lonEnd) > 180.0f) { if (lonStart < lonEnd) lonStart += 360.0f; else lonEnd += 360.0f; } float latStart = dp.latitude; float latEnd = latStopList[dp.dataPointIndex]; float altBot = altBottomList[dp.dataPointIndex]; float altTop = altTopList[dp.dataPointIndex]; short duration = durationList[dp.dataPointIndex]; float secs = (int)(duration * x); DateTime t = dp.time.AddSeconds(secs); float lat = Mathf.Lerp(latStart, latEnd, x); float lon = Mathf.Lerp(lonStart, lonEnd, x); float alt = Mathf.Lerp(altBot, altTop, y); //Debug.Log($" x:{x} lonStart: {lonStart} lonEnd: {lonEnd} lat: {lat}"); latText.text = "Lat: " + sam.Utils.FormatLatitude(lat); lonText.text = "Lon: " + sam.Utils.FormatLongitude(lon); altText.text = "Alt: " + alt.ToString("F2") + " Km"; curValText.text = dp.variables[variableIndex].ToString() + " " + prodvar.unit; dateText.text = t.ToString(dateFormat); if (selectedDP != dp) BroadcastMessage("OnDataPointSelected", dp, SendMessageOptions.DontRequireReceiver); } selectedDP = dp; lastSelectedX = x; lastSelectedY = y; } } public void OnEmailButtonPressed() { string satelliteId = (string)GlobalDictionary.GetValue("SatelliteId"); string productId = data.productName; string subject = $"ESA {satelliteId} product: {productId}"; string body = $"{satelliteId} data\n\n" + $"- Product Name:{productId}\n\n" + $"- Variable: {prodvar.name}\n\n" + $"- Date: {dateText.text}\n\n" + $"- Longitude: {lonText.text}\n\n" + $"- Latitude: {latText.text}\n\n" + $"- Value: {curValText.text}\n\n" + $"Mail from ESA {satelliteId} app\n"; Debug.Log($"SEND EMAIL! Subject: {subject} body:\n{body}"); StartCoroutine(CaptureScreenshot(subject, body)); } IEnumerator CaptureScreenshot(string subject, string body) { yield return new WaitForEndOfFrame(); string fileName = "mailScreenshot"; string filePath = Path.Combine(Application.persistentDataPath, fileName + ".png"); Texture2D screenImage = new Texture2D(Screen.width, Screen.height); //Get Image from screen screenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); screenImage.Apply(); File.WriteAllBytes(filePath, screenImage.EncodeToPNG()); Destroy(screenImage); #if UNITY_ANDROID StartCoroutine(ShareAndroidScreenshot(subject, body, filePath)); #else NativeShare shareObject = new NativeShare(); shareObject.SetTitle("Send email"); // Dialog title shareObject.SetSubject(subject); shareObject.SetText(body); shareObject.AddFile(filePath); //shareObject.SetCallback(MyCallback); shareObject.Share(); #endif } IEnumerator ShareAndroidScreenshot(string subject, string body, string filePath) { yield return new WaitForEndOfFrame(); AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject currentActivity = unity.GetStatic("currentActivity"); AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent"); AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent"); intentObject.Call("setAction", intentClass.GetStatic("ACTION_SEND")); AndroidJavaObject fileObject = new AndroidJavaObject("java.io.File", filePath); AndroidJavaClass fileProviderClass = new AndroidJavaClass("android.support.v4.content.FileProvider"); object[] providerParams = new object[3]; providerParams[0] = currentActivity; providerParams[1] = Application.identifier + ".provider"; providerParams[2] = fileObject; AndroidJavaObject uriObject = fileProviderClass.CallStatic("getUriForFile", providerParams); intentObject.Call("putExtra", intentClass.GetStatic("EXTRA_STREAM"), uriObject); intentObject.Call("setType", "image/png"); intentObject.Call("putExtra", intentClass.GetStatic("EXTRA_SUBJECT"), subject); intentObject.Call("putExtra", intentClass.GetStatic("EXTRA_TEXT"), body); intentObject.Call("addFlags", intentClass.GetStatic("FLAG_GRANT_READ_URI_PERMISSION")); AndroidJavaObject chooser = intentClass.CallStatic("createChooser", intentObject, "Send email"); currentActivity.Call("startActivity", chooser); } }