/// <summary> /// Initialization code for the major parts of the application. /// </summary> void Start() { Screen.fullScreen = false; //set up references debugText = DebugTextInstance.GetComponent <Text>(); debugText.enabled = true; Instance = this; Popup.Instance = infoPopup; QuestionPopup.Instance = questionPopup; BitmapEncoding.PersistentPath = Application.persistentDataPath; foreach (var v in WebCamTexture.devices) { print("\"" + v.name + "\"" + (v.isFrontFacing ? " (Front facing)" : "")); } print("Persistent: " + BitmapEncoding.PersistentPath); //load persistent data Status = "Loading Digit Bitmaps"; storedBitmaps = BitmapEncoding.LoadBitmaps(); Status = "Loading Settings"; SettingManager.LoadSettings(); //start camera Status = "Looking for cameras"; try { texture = new WebCamTexture(CameraName); texture.Play(); if (WebCamTexture.devices.Length == 0) { throw new System.Exception(NoCamerasMessage); } } catch (System.Exception e) { Popup.ActivatePopup("Could not start the camera:" + System.Environment.NewLine + e.Message); CameraUI.enabled = OverlayUI.enabled = false; } textureWidth = texture.width; textureHeight = texture.height; side = Mathf.Min(textureWidth, textureHeight) - 10; //create overlay, rotate and stretch images correctly Status = "Assigning images"; Texture2D overlay = CreateOverlay(textureWidth, textureHeight, side); OverlayUI.texture = overlay; CameraUI.texture = texture; CameraUI.GetComponent <AspectRatioFitter>().aspectRatio = textureWidth / (float)textureHeight; if ((texture.videoRotationAngle + 360) % 180 == 90) { int i = textureWidth; textureWidth = textureHeight; textureHeight = i; } CameraUI.transform.parent.localEulerAngles = new Vector3(0, 0, -texture.videoRotationAngle); SudokuPanel.GetComponent <SudokuCreator>().Init(); Status = "Ready to take picture"; debugText.enabled = false; }
/// <summary> /// Loads the skill popup. /// </summary> /// <param name="headline">Headline for the popup</param> /// <param name="content">Content description</param> /// <param name="effectsList">Description of effects</param> public void LoadSkillPopup(string headline, string content, string effectsList) { skillPopup.ActivatePopup(headline, content, effectsList); }
/// <summary> /// Main program loop. /// </summary> void Update() { if (!taken) { if ((TakePictureNow && texture != null && texture.isPlaying) || Input.GetKey(KeyCode.Return) || Input.GetKey(KeyCode.Space)) { //grab the image data from the WebCamTexture taken = true; original = texture.GetPixels(); Color[] pixels = new Color[original.Length]; System.Array.Copy(original, pixels, original.Length); texture.Stop(); print(string.Format("Took {0}x{1} picture", texture.width, texture.height)); //refresh the settings and rotate the texture if it is needed Status = "Loading Settings"; SettingManager.LoadSettings(); ArrayHandling.RotateTexture(ref pixels, texture.width, texture.height, texture.videoRotationAngle); //start a new worker thread debugText.enabled = true; if (workThread != null && workThread.IsAlive) { workThread.Abort(); workThread.Join(200); } workThread = new System.Threading.Thread(() => Worker(pixels)); workThread.Start(); PicturePanel.SetActive(false); CameraUI.transform.parent.gameObject.SetActive(false); } } //check if the worker thread has completed its tasks else if (Status == StatusDone) //this works, but an enum would probably be better suited for this kind of check { Status = ""; debugText.enabled = false; FillSudoku(solvedSudoku, unsolvedSudoku); print("Displayed the result"); #if WRITE_IMAGES_TO_DISK Texture2D originalTexture = new Texture2D(texture.width, texture.height), processedTexture = new Texture2D(side, side); originalTexture.filterMode = FilterMode.Point; originalTexture.SetPixels(original); originalTexture.Apply(); processedTexture.filterMode = FilterMode.Point; processedTexture.SetPixels(debugPicture); processedTexture.Apply(); File.WriteAllBytes(Path.Combine(BitmapEncoding.PersistentPath, "Output.png"), processedTexture.EncodeToPNG()); File.WriteAllBytes(Path.Combine(BitmapEncoding.PersistentPath, "Input.png"), originalTexture.EncodeToPNG()); print("Saved images to " + BitmapEncoding.PersistentPath); #endif } //update explanatory text if it is needed if (debugText.enabled && Status != oldStatus) { if (Status.Contains(StatusError)) { debugText.text = Status; } else { debugText.text = (Status.Length != 0 ? "Please Wait" + System.Environment.NewLine + Status : ""); } oldStatus = Status; } //trigger popup if needed if (Popup.queued != null) { Popup.ActivatePopup(Popup.queued.Value.Key, Popup.queued.Value.Value); Popup.queued = null; } //if the user presses escape (back button on Android), go back/abort calculation/exit the app if (Input.GetKeyDown(KeyCode.Escape)) { var eb = PicturePanel.GetComponentsInChildren <ExtraButtons>(true)[0]; if (PicturePanel.activeInHierarchy || (debugText.enabled && workThread != null && workThread.IsAlive)) { Application.Quit(); } else if (infoPopup.gameObject.activeInHierarchy) { infoPopup.Close(); } else if (QuestionPopup.Instance.gameObject.activeInHierarchy) { QuestionPopup.Instance.Close(false); } else if (digitPrompt.gameObject.activeInHierarchy) { digitPrompt.Choose(-1); } else if (SudokuPanel.activeInHierarchy) { Reset(false); } else if (SettingManager.AdvancedSettingsPanel != null && SettingManager.AdvancedSettingsPanel.activeInHierarchy) { eb.DirectlyFromAdvanced(); } else if (eb.HelpPanel != null && eb.HelpPanel.activeInHierarchy) { eb.ReturnFromHelp(); } } }