Beispiel #1
0
    /**
     * Generates N*N new DCT component toggles with the appropriate size, and with the correct DCT images and toggle behaviors.
     */
    private void UpdateGridCount(int N)
    {
        // ====================================== Update Grid Layout ==========================================

        // Change toggles per row to N
        dctToggleGrid.constraintCount = N;

        // Calculate size of each toggle to fit into grid
        int gridSize = toggleGridWidth / N - 3; // In-between spacing of 3.

        dctToggleGrid.cellSize = new Vector2(gridSize, gridSize);

        // ======================================== Toggle Creation ===========================================

        Transform dctToggleContainer = dctToggleGrid.GetComponent <Transform>();
        int       newChildCount      = N * N;

        // Enable prefab for cloning
        togglePrefab.gameObject.SetActive(true);

        // Remove previously created toggles
        foreach (Transform child in dctToggleContainer)
        {
            Destroy(child.gameObject);
        }

        // Get DCT matrix of size N for later DCT image generation.
        double[,] dctMatrix = GetDCTMatrix(N);

        // Create toggle and load the respective DCT component image
        int row = 0;
        int col = 0;

        for (int i = 0; i < newChildCount; i++)
        {
            Transform clone   = Instantiate(togglePrefab, dctToggleGrid.GetComponent <Transform>());
            Image     imgComp = clone.GetChild(0).GetComponent <Image>();
            clone.gameObject.name = row + " " + col;

            // Add listener to inform ImageRenderer that a DCT component is toggled/untoggled
            clone.GetComponent <Toggle>().onValueChanged.AddListener(delegate { imageRenderer.DCTCompUpdateCheck(); });

            // Add DCT component image to toggle
            LoadDCTComponentSprite(row, col, N, imgComp, dctMatrix);

            // Track 2D index
            col++;
            if (col >= N)
            {
                row++;
                col = 0;
            }
        }

        // Disable prefab after finishing
        togglePrefab.gameObject.SetActive(false);

        // Update the displayed image. Use ToggleOn to bypass child destruction delay.
        ToggleOn();
    }
Beispiel #2
0
    // ====================================================================================================================
    // =============================================== BUTTON BEHAVIOR ====================================================
    // ====================================================================================================================

    /**
     * Opens a file explorer and prompts player to select a PNG or JPG file; an error
     * is displayed if any other files are selected. The image is converted to grey-scale
     * and loaded to ImageRenderer. Message is sent to ImageRenderer to render the custom image.
     **/
    private void OpenExplorer()
    {
        errorMessage.text = "";
        string[] path = StandaloneFileBrowser.OpenFilePanel("Select a PNG or JPG File...", "", "", false);

        // Check if a valid file is selected.
        if (path.Length == 0)
        {
            return;
        }
        else if (path[0].Length < 5)
        {
            return;
        }

        // Check for proper extensions
        if (path[0].Substring(path[0].Length - 4) == ".png" || path[0].Substring(path[0].Length - 4) == ".jpg")
        {
            // Retrieve image file
            WWW       www           = new WWW("file:///" + path[0]);
            Texture2D image_Texture = www.texture;
            int       width         = image_Texture.width;
            int       height        = image_Texture.height;

            // Convert image to greyscale
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    Color rgb  = image_Texture.GetPixel(i, j);
                    float grey = (float)(0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]);
                    image_Texture.SetPixel(i, j, new Color(grey, grey, grey));
                }
            }
            // Commit changes
            image_Texture.Apply();

            // Load the grey-scale image into the image panel
            imagePanel.sprite = Sprite.Create(image_Texture, new Rect(0, 0, width, height), Vector2.zero);

            // Sent "message" to Image_Behavior
            imageRenderer.customImageRef = image_Texture;
            imageRenderer.usingCustom    = true;
            imageRenderer.forceRefresh   = true;
            imageRenderer.DCTCompUpdateCheck();
        }
        else
        {
            errorMessage.text = "PNG or JPG Only!";
        }
    }