public void Initialize()
        {
            descriptor = GlobalManager.MLevel.GetDescriptor(GlobalManager.MStorage.GalleryLevel);
            //int width = descriptor.init.width;
            //int height = descriptor.init.height;
            float finalRatioXY = descriptor.init.finalXYScale;
            float screenAspect = Camera.main.aspect;

            // load STRESS and SMOOTH textures
            string    stressImage = StressImageCreator.StressedFinalImageFile(descriptor.init.id);
            Texture2D stressTex   = new Texture2D(2, 2, TextureFormat.RGB24, false);

            stressTex.LoadImage(System.IO.File.ReadAllBytes(stressImage));
            imageParams[0].winImage.texture = stressTex;
            Texture2D smoothTex = Resources.Load <Texture2D>(LevelDataManager.SmoothImageResource(descriptor.init.id));

            imageParams[1].winImage.texture = smoothTex;

            // set up scaling and UV rectangles
            CanvasScaler canvasScaler = GetComponent <CanvasScaler>();
            // the size of the canvas excluding margins
            Rect sourceCanvasRect = new Rect(0, 0, canvasScaler.referenceResolution.x * (1 - marginRatio), canvasScaler.referenceResolution.y * (1 - marginRatio));

            sourceCanvasSize = new Vector2(sourceCanvasRect.width, sourceCanvasRect.height);
            // SourceCanvas matches screen by width, so the final height scaling should be adjusted by:
            float heightAdjustFactor = sourceCanvasRect.width / sourceCanvasRect.height / screenAspect;

            for (int i = 0; i < imageParams.Length; i++)
            {
                imageParams[i].imageTransform.localPosition = imageParams[i].originalPosition;
                Vector2 canvasToImageRatio = new Vector2(
                    sourceCanvasRect.width / imageParams[i].originalRect.width,
                    sourceCanvasRect.height * heightAdjustFactor / imageParams[i].originalRect.height
                    );
                imageParams[i].sourceScale = new Vector2(
                    finalRatioXY > screenAspect ? canvasToImageRatio.y * finalRatioXY : canvasToImageRatio.x,
                    finalRatioXY > screenAspect ? canvasToImageRatio.y : canvasToImageRatio.x / finalRatioXY
                    );
                imageParams[i].effectiveSize             = Vector2.Scale(new Vector2(imageParams[i].originalRect.width, imageParams[i].originalRect.height), imageParams[i].sourceScale);
                imageParams[i].imageTransform.localScale = imageParams[i].sourceScale;
                currentScale = new FloatRange
                {
                    min = scaleRange.min,
                    max = scaleRange.min
                };
                // UV-coordinates of the image
                imageParams[i].winImage.uvRect = new Rect(0, 0, 1, 1);
                // positions
                //imageParams[i].originalPosition = imageParams[i].imageTransform.localPosition;
                imageParams[i].movePosition = new Vector2[2]
                {
                    imageParams[i].originalPosition,
                    imageParams[i].originalPosition
                };
            }
            effectFinished = false;
            GenerateNewStep(true);
        }
        protected override void AwakeInit()
        {
            descriptor = GlobalManager.MLevel.GetDescriptor(GlobalManager.MStorage.SelectedLevel);
            float puzzleRatioXY = (float)descriptor.init.width / (float)descriptor.init.height;
            float screenAspect  = Camera.main.aspect;

            // prepare STRESS texture
            string    stressImage = StressImageCreator.StressedFinalImageFile(descriptor.init.id);
            Texture2D tex         = new Texture2D(2, 2, TextureFormat.RGB24, false);

            tex.LoadImage(System.IO.File.ReadAllBytes(stressImage));
            Vector2 texFactor = new Vector2
            {
                x = descriptor.init.finalXYScale > puzzleRatioXY ? puzzleRatioXY / descriptor.init.finalXYScale : 1f,
                y = puzzleRatioXY > descriptor.init.finalXYScale ? descriptor.init.finalXYScale / puzzleRatioXY : 1f
            };

            sourceImage.texture = tex;
            CanvasScaler canvasScaler = GetComponent <CanvasScaler>();
            // the size of the canvas excluding margins
            Rect sourceCanvasRect = new Rect(0, 0, canvasScaler.referenceResolution.x * (1 - marginRatio), canvasScaler.referenceResolution.y * (1 - marginRatio));
            // SourceCanvas matches screen by width, so the final scaling should be multiplied by:
            float         heightAdjustFactor = sourceCanvasRect.width / sourceCanvasRect.height / screenAspect;
            RectTransform sourceTransform    = sourceImage.GetComponent <RectTransform>();
            // the original size of the source image (square)
            Rect    sourceRect         = sourceTransform.rect;
            Vector2 canvasToImageRatio = new Vector2(
                sourceCanvasRect.width / sourceRect.width,
                sourceCanvasRect.height * heightAdjustFactor / sourceRect.height
                );
            Vector3 sourceRectScale = new Vector3(
                puzzleRatioXY > screenAspect ? canvasToImageRatio.x : canvasToImageRatio.y * puzzleRatioXY,
                puzzleRatioXY > screenAspect ? canvasToImageRatio.x / puzzleRatioXY : canvasToImageRatio.y,
                1
                );

            //Debug.Log("Image sizes: canvas: " + sourceCanvasRect.ToString() + ", image: " + sourceRect.ToString() + ", image/canvas: " + canvasToImageRatio.ToString() + ", scale: " + sourceRectScale.ToString());
            sourceTransform.localScale = sourceRectScale;
            // UV-coordinates of the image
            Rect uvRect = new Rect(
                (1f - texFactor.x) / 2,
                (1f - texFactor.y) / 2,
                texFactor.x,
                texFactor.y
                );

            sourceImage.uvRect = uvRect;

            // button text
            sourceText.text = GlobalManager.MLanguage.Entry(GetSourceTextId());

            // message handling
            registrator.Add(new MessageRegistrationTuple {
                type = InstantMessageType.PuzzleViewSourceImage, handler = OnPuzzleViewSourceImage
            });
            sourceButton.interactable = true;
        }
Example #3
0
        void SetStressedImage()
        {
            string    stressImage = StressImageCreator.StressedFinalImageFile(storyMessages[frameId].pictureId);
            Texture2D stressTex   = new Texture2D(2, 2, TextureFormat.RGB24, false);

            stressTex.LoadImage(System.IO.File.ReadAllBytes(stressImage));
            stressedStoryImage.image.texture = stressTex;
            SetImageSize(stressedStoryImage);
        }
        private void Awake()
        {
            descriptor = GlobalManager.MLevel.GetDescriptor(GlobalManager.MStorage.SelectedLevel);
            int   width         = descriptor.init.width;
            int   height        = descriptor.init.height;
            float puzzleRatioXY = (float)width / (float)height;

            // prepare one STRESS texture for all tiles
            string    stressImage = StressImageCreator.StressedFinalImageFile(descriptor.init.id);
            Texture2D tex         = new Texture2D(2, 2, TextureFormat.RGB24, false);

            tex.LoadImage(System.IO.File.ReadAllBytes(stressImage));
            // a special factor used in later calculations
            Vector2 texFactor = new Vector2
            {
                x = descriptor.init.finalXYScale > puzzleRatioXY ? puzzleRatioXY / descriptor.init.finalXYScale : 1f,
                y = puzzleRatioXY > descriptor.init.finalXYScale ? descriptor.init.finalXYScale / puzzleRatioXY : 1f
            };
            Vector2 texScale = new Vector2
            {
                x = texFactor.x / (float)width,
                y = texFactor.y / (float)height
            };
            Vector2 texOffsetStart = new Vector2
            {
                x = (1f - texFactor.x) / 2,
                y = (1f - texFactor.y) / 2 + texFactor.y * (float)(height - 1) / (float)height
            };
            Vector2 texOffsetStep = new Vector2
            {
                x = texFactor.x / (float)width,
                y = -texFactor.y / (float)height
            };

            // create buttons
            buttons = new ButtonCluster[width - 1, height - 1];
            float   buttonStartX   = -(width - 2) * TileSize / 2;
            Vector3 buttonPosition = new Vector3(buttonStartX, (height - 2) * TileSize / 2, neutralButtonZ);

            for (int y = 0; y < height - 1; y++)
            {
                for (int x = 0; x < width - 1; x++)
                {
                    GameObject button = (GameObject)Instantiate(buttonPrefab);
                    initialButtonRotation     = button.transform.localRotation;
                    button.transform.position = buttonPosition;
                    button.GetComponent <PuzzleButtonController>().Init(new Vector2Int(x, y), neutralButtonZ, pressedButtonZ, releasedButtonZ);
                    buttons[x, y] = new ButtonCluster
                    {
                        button = button,
                        tiles  = new GameObject[4]
                    };
                    buttonPosition.x += TileSize;
                }
                buttonPosition.x  = buttonStartX;
                buttonPosition.y -= TileSize;
            }

            // create tiles
            tiles = new GameObject[width, height];
            Vector2 texOffset = texOffsetStart;

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    GameObject tile = (GameObject)Instantiate(tilePrefab);
                    initialTileRotation = tile.transform.localRotation;
                    // downscale the tile a bit
                    Vector3 tileScale = tile.transform.localScale;
                    tileScale *= 1 - tileGap / 2;
                    tile.transform.localScale = tileScale;
                    // set up tile's texture and its parameters
                    MeshRenderer meshRenderer = tile.GetComponent <MeshRenderer>();
                    Material[]   materials    = meshRenderer.materials;
                    materials[0].SetTexture("_MainTex", tex);
                    materials[0].SetTextureScale("_MainTex", texScale);
                    materials[0].SetTextureOffset("_MainTex", texOffset);
                    meshRenderer.materials = materials;
                    tile.GetComponent <TileFlasher>().Init(new Vector2Int(x, y));
                    tiles[x, y]  = tile;
                    texOffset.x += texOffsetStep.x;
                }
                texOffset.x  = texOffsetStart.x;
                texOffset.y += texOffsetStep.y;
            }
        }