/// <summary>
        /// Adds joint to this piece
        /// </summary>
        /// <param name="Joint">Joint info to add to this piece</param>
        /// <returns>Returns true if joint position provided in Joint is not already occupied returns false otherwise</returns>
        public bool AddJoint(SJointInfo Joint)
        {
            //Check if this joint type already exists
            foreach (SJointInfo item in _Joints)
                if (item.JointPosition == Joint.JointPosition)
                    return false;

            _Joints.Add(Joint);

            return true;
        }
Example #2
0
        /// <summary>
        /// Adds joint to this piece
        /// </summary>
        /// <param name="Joint">Joint info to add to this piece</param>
        /// <returns>Returns true if joint position provided in Joint is not already occupied returns false otherwise</returns>
        public bool AddJoint(SJointInfo Joint)
        {
            //Check if this joint type already exists
            foreach (SJointInfo item in _Joints)
            {
                if (item.JointPosition == Joint.JointPosition)
                {
                    return(false);
                }
            }

            _Joints.Add(Joint);

            return(true);
        }
Example #3
0
        /// <summary>
        /// Returns joint information based on joint position.
        /// </summary>
        /// <param name="Joint">Joint position to return data for</param>
        /// <param name="IsFound">an out variable, true if joint is found false otherwise</param>
        /// <returns>Returns joint information of joint at provided joint position</returns>
        public SJointInfo GetJoint(EJointPosition Joint, out bool IsFound)
        {
            SJointInfo Result = new SJointInfo();

            foreach (SJointInfo item in _Joints)
            {
                if (item.JointPosition == Joint)
                {
                    Result  = new SJointInfo(item.JointType, item.JointPosition, item.JointWidth, item.JointHeight);
                    IsFound = true;

                    return(Result);
                }
            }

            IsFound = false;

            return(Result);
        }
Example #4
0
        public SJointInfo GetJoint(EJointPosition Joint, out bool IsFound)
        {
            SJointInfo Result = new SJointInfo();

            foreach (SJointInfo item in _Joints)
            {
                if (item.JointPosition == Joint)
                {
                    Result = new SJointInfo(item.JointType, item.JointPosition, item.JointWidth, item.JointHeight);
                    IsFound = true;

                    return Result;
                }
            }

            IsFound = false;

            return Result;
        }
        /// <summary>
        /// Draws joint mask image for each side of joints for every piece which is used to later 
        /// generate puzzle image for pieces
        /// </summary>
        /// <param name="TopJointMaskImage">Output mask image for top joints</param>
        /// <param name="BotJointMaskImage">Output mask image for bottom joints</param>
        /// <param name="LeftJointMaskImage">Output mask image for left joints</param>
        /// <param name="RightJointMaskImage">Output mask image for right joints</param>
        /// <param name="JointMaskImages">User provided joint mask images to be used</param>
        /// <param name="PieceHeightWithoutJoint">Output piece image height</param>
        /// <param name="PieceWidthWithoutJoint">Output piece image width</param>
        /// <param name="PuzzleImgHeight">Height of user provided puzzle image</param>
        /// <param name="PuzzleImgWidth">Width of user provided puzzle image</param>
        /// <param name="Cols">Total columns in puzzle</param>
        /// <param name="Rows">Total rows in puzzle</param>
        /// <returns>Returns created pieces metadata created during masks creation</returns>
        private SPieceInfo[,] DrawCustomPieceJointsMask(ref Color[][] TopJointMaskImage, ref Color[][] BotJointMaskImage,
            ref Color[][] LeftJointMaskImage, ref Color[][] RightJointMaskImage, Texture2D[] JointMaskImages,
                out int PieceWidthWithoutJoint, out int PieceHeightWithoutJoint, int PuzzleImgWidth, int PuzzleImgHeight, int Rows = 5, int Cols = 5)
        {

            int[] JointMaskWidth = new int[JointMaskImages.Length];
            int[] JointMaskHeight = new int[JointMaskImages.Length];

            //Create direction wise mask images
            Texture2D[] LeftJointMask = new Texture2D[JointMaskImages.Length];
            Texture2D[] RightJointMask = new Texture2D[JointMaskImages.Length];
            Texture2D[] TopJointMask = new Texture2D[JointMaskImages.Length];
            Texture2D[] BottomJointMask = new Texture2D[JointMaskImages.Length];

            SPieceInfo[,] ResultPiecesData = new SPieceInfo[Rows, Cols];


            //Initialize pieces data
            for (int i = 0; i < Rows; i++)
                for (int j = 0; j < Cols; j++)
                    ResultPiecesData[i, j] = new SPieceInfo((i * Cols) + j);

            int PieceHeight = PuzzleImgHeight / Rows;
            int PieceWidth = PuzzleImgWidth / Cols;

            PieceWidthWithoutJoint = PieceWidth;
            PieceHeightWithoutJoint = PieceHeight;



            for (int ArrayTav = 0; ArrayTav < JointMaskImages.Length; ArrayTav++)
            {
                LeftJointMask[ArrayTav] = JointMaskImages[ArrayTav];
                BottomJointMask[ArrayTav] = HelperMethods.rotateImage(JointMaskImages[ArrayTav], 90);
                RightJointMask[ArrayTav] = HelperMethods.rotateImage(BottomJointMask[ArrayTav], 90);
                TopJointMask[ArrayTav] = HelperMethods.rotateImage(JointMaskImages[ArrayTav], 270);
                                

                #region "Resize Joint mask images for drawing inside mask image And calculate joints width and height"

                //Resize Joint mask images for drawing inside mask image
                //  Image will be resized according to piece width
                int MaskImageWidth = (int)(PieceWidth * 0.3f);
                int MaskImageHeight = (int)((float)MaskImageWidth / ((float)JointMaskImages[ArrayTav].width / (float)JointMaskImages[ArrayTav].height));

                LeftJointMask[ArrayTav] = HelperMethods.resizeImage(LeftJointMask[ArrayTav], MaskImageWidth, MaskImageHeight);
                RightJointMask[ArrayTav] = HelperMethods.resizeImage(RightJointMask[ArrayTav], MaskImageWidth, MaskImageHeight);
                TopJointMask[ArrayTav] = HelperMethods.resizeImage(TopJointMask[ArrayTav], MaskImageWidth, MaskImageHeight);
                BottomJointMask[ArrayTav] = HelperMethods.resizeImage(BottomJointMask[ArrayTav], MaskImageWidth, MaskImageHeight);


                //Calculate joints width and heights
                CalculateCustomJointDimensions(LeftJointMask[ArrayTav], out JointMaskWidth[ArrayTav], out JointMaskHeight[ArrayTav]);



                #endregion

            }




            #region "Argument Error Checking"

            //Joint mask image width and height should be same
            //Joint mask image should have only black and white pixels inside it

            if (JointMaskImages[0].width != JointMaskImages[0].height)
            {
                Debug.LogError("JointMaskImage width and height should be same");
                return null;
            }
            else
            {
                bool ErrorFound = false;  //If Non-Black or Non-White pixel found

                //Check for pixel colors in joint mask image
                for (int rowtrav = 0; rowtrav < JointMaskImages[0].height && !ErrorFound; rowtrav++)
                {
                    for (int coltrav = 0; coltrav < JointMaskImages[0].width && !ErrorFound; coltrav++)
                    {
                        Color PixelColor = JointMaskImages[0].GetPixel(coltrav, rowtrav);

                        if (PixelColor != Color.white || PixelColor != Color.black)
                        {
                            ErrorFound = true;

                            //Debug.LogError("Only white and black pixels are allowed in JointMaskImage");

                            //return null;
                        }
                    }
                }


            }

            #endregion

            TopJointMaskImage = new Color[PuzzleImgWidth][];
            BotJointMaskImage = new Color[PuzzleImgWidth][];
            LeftJointMaskImage = new Color[PuzzleImgWidth][];
            RightJointMaskImage = new Color[PuzzleImgWidth][];

            //Clear Instantiated mask image
            for (int i = 0; i < PuzzleImgWidth; i++)
            {
                TopJointMaskImage[i] = new Color[PuzzleImgHeight];
                BotJointMaskImage[i] = new Color[PuzzleImgHeight];
                LeftJointMaskImage[i] = new Color[PuzzleImgHeight];
                RightJointMaskImage[i] = new Color[PuzzleImgHeight];
            }


            //Generate random joint info And Draw joints

            Random.seed = System.DateTime.Now.Second;
            Color PieceColor = Color.black;


            for (int RowTrav = 0; RowTrav < Rows; RowTrav++)
            {

                for (int ColTrav = 0; ColTrav < Cols; ColTrav++)
                {
                    int PieceX = ColTrav * PieceWidth;
                    int PieceY = RowTrav * PieceHeight;

                    //Generate Random joint info and Draw Joints From Mask Image

                    #region "Draw right joints according to piece joint information"

                    if (ColTrav < Cols - 1)
                    {
                        int SelectedRandomJoint = Random.Range(1, JointMaskImages.Length) - 1;

                        //Create random joint information
                        int RndVal = (int)(Random.Range(1f, 18f) >= 10 ? 1 : 0);
                        ResultPiecesData[RowTrav, ColTrav].AddJoint(new SJointInfo((EJointType)RndVal, EJointPosition.Right,
                                        JointMaskWidth[SelectedRandomJoint], JointMaskHeight[SelectedRandomJoint]));


                        int JointX = PieceX + PieceWidth;
                        int JointY = PieceY + (PieceHeight / 2) - (RightJointMask[SelectedRandomJoint].height / 2);

                        bool Result = false;
                        SJointInfo RightJointInfo = ResultPiecesData[RowTrav, ColTrav].GetJoint(EJointPosition.Right, out Result);

                        if (!Result)
                        {
                            Debug.LogError("Logical error in draw joints from mask image Right Joints");
                        }
                        else
                        {
                            if (RightJointInfo.JointType == EJointType.Male)
                                drawJoint(ref RightJointMaskImage, RightJointMask[SelectedRandomJoint], PieceColor, JointX, JointY);
                        }

                    }
                    #endregion

                    #region"Draw left joints according to piece joint information"

                    if (ColTrav > 0)
                    {
                        int SelectedRandomJoint = Random.Range(1, JointMaskImages.Length) - 1;

                        //Create random joint information
                        bool Result = false;

                        SJointInfo PreviousRightJoint = ResultPiecesData[RowTrav, ColTrav - 1].GetJoint(EJointPosition.Right, out Result);

                        if (Result == false)
                        {
                            Debug.LogError("Logical error in joints information left joint");
                        }
                        else
                        {
                            SJointInfo CalcLeftJoint = new SJointInfo(PreviousRightJoint.JointType == EJointType.Female ?
                                        EJointType.Male : EJointType.Female, EJointPosition.Left,
                                        JointMaskWidth[SelectedRandomJoint], JointMaskHeight[SelectedRandomJoint]);
                            ResultPiecesData[RowTrav, ColTrav].AddJoint(CalcLeftJoint);
                        }


                        int JointX = PieceX - LeftJointMask[SelectedRandomJoint].width;
                        int JointY = PieceY + (PieceHeight / 2) - (LeftJointMask[SelectedRandomJoint].height / 2);

                        Result = false;
                        SJointInfo LeftJointInfo = ResultPiecesData[RowTrav, ColTrav].GetJoint(EJointPosition.Left, out Result);

                        if (!Result)
                        {
                            Debug.LogError("Logical error in draw joints from mask image Left Joints");
                        }
                        else
                        {
                            if (LeftJointInfo.JointType == EJointType.Male)
                                drawJoint(ref LeftJointMaskImage, LeftJointMask[SelectedRandomJoint], PieceColor, JointX, JointY);
                        }
                    }

                    #endregion

                    #region"Draw Top joints according to piece joint information"

                    if (RowTrav < Rows - 1)
                    {
                        int SelectedRandomJoint = Random.Range(1, JointMaskImages.Length) - 1;

                        //Create random joint information
                        int RndVal = (int)(Random.Range(1f, 17f) >= 10 ? 1 : 0);
                        ResultPiecesData[RowTrav, ColTrav].AddJoint(new SJointInfo((EJointType)RndVal, EJointPosition.Top,
                                        JointMaskWidth[SelectedRandomJoint], JointMaskHeight[SelectedRandomJoint]));

                        int JointX = PieceX + (PieceWidth / 2) - (TopJointMask[SelectedRandomJoint].width / 2);
                        int JointY = PieceY + PieceHeight;

                        bool Result = false;
                        SJointInfo TopJointInfo = ResultPiecesData[RowTrav, ColTrav].GetJoint(EJointPosition.Top, out Result);

                        if (!Result)
                        {
                            Debug.LogError("Logical error in draw joints from mask image Top Joints");
                        }
                        else
                        {
                            if (TopJointInfo.JointType == EJointType.Male)
                                drawJoint(ref TopJointMaskImage, TopJointMask[SelectedRandomJoint], PieceColor, JointX, JointY);
                        }

                    }

                    #endregion

                    #region"Draw Bottom joints according to piece joint information"

                    if (RowTrav > 0)
                    {
                        int SelectedRandomJoint = Random.Range(1, JointMaskImages.Length) - 1;

                        //Create random joint information
                        bool Result = false;

                        SJointInfo PreviousPieceTopJoint = ResultPiecesData[RowTrav - 1, ColTrav].GetJoint(EJointPosition.Top, out Result);

                        if (Result == false)
                        {
                            Debug.LogError("Logical error in joints information Bottom joint");
                        }
                        else
                        {
                            SJointInfo CalcBottomJoint = new SJointInfo(PreviousPieceTopJoint.JointType == EJointType.Female ?
                                        EJointType.Male : EJointType.Female, EJointPosition.Bottom,
                                        JointMaskWidth[SelectedRandomJoint], JointMaskHeight[SelectedRandomJoint]);

                            ResultPiecesData[RowTrav, ColTrav].AddJoint(CalcBottomJoint);
                        }


                        int JointX = PieceX + (PieceWidth / 2) - (BottomJointMask[SelectedRandomJoint].width / 2);
                        int JointY = PieceY - BottomJointMask[SelectedRandomJoint].height;

                        Result = false;
                        SJointInfo BottomJointInfo = ResultPiecesData[RowTrav, ColTrav].GetJoint(EJointPosition.Bottom, out Result);

                        if (!Result)
                        {
                            Debug.LogError("Logical error in draw joints from mask image Top Joints");
                        }
                        else
                        {
                            if (BottomJointInfo.JointType == EJointType.Male)
                                drawJoint(ref BotJointMaskImage, BottomJointMask[SelectedRandomJoint], PieceColor, JointX, JointY);
                        }

                    }

                    #endregion


                }

            }



            return ResultPiecesData;
        }
Example #6
0
        private SPieceInfo[,] DrawCustomPieceJointsMask(ref Color[][] Image, Texture2D[] JointMaskImage,
                out int PieceWidthWithoutJoint, out int PieceHeightWithoutJoint, int PuzzleImgWidth, int PuzzleImgHeight, int Rows = 5, int Cols = 5)
        {

            //Create direction wise mask images
            Texture2D[] LeftJointMask = new Texture2D[JointMaskImage.Length];
            int[] LeftJointMaskWidth = new int[JointMaskImage.Length];
            int[] LeftJointMaskHeight = new int[JointMaskImage.Length];

            Texture2D[] RightJointMask = new Texture2D[JointMaskImage.Length];
            int[] RightJointMaskWidth = new int[JointMaskImage.Length];
            int[] RightJointMaskHeight = new int[JointMaskImage.Length];

            Texture2D[] TopJointMask = new Texture2D[JointMaskImage.Length];
            int[] TopJointMaskWidth = new int[JointMaskImage.Length];
            int[] TopJointMaskHeight = new int[JointMaskImage.Length];

            Texture2D[] BottomJointMask = new Texture2D[JointMaskImage.Length];
            int[] BottomJointMaskWidth = new int[JointMaskImage.Length];
            int[] BottomJointMaskHeight = new int[JointMaskImage.Length];


            SPieceInfo[,] ResultPiecesData = new SPieceInfo[Rows, Cols];

            //Initialize pieces data
            for (int i = 0; i < Rows; i++)
                for (int j = 0; j < Cols; j++)
                    ResultPiecesData[i, j] = new SPieceInfo((i * Cols) + j);
            
            int PieceHeight = PuzzleImgHeight / Rows;
            int PieceWidth = PuzzleImgWidth / Cols;

            PieceWidthWithoutJoint = PieceWidth;
            PieceHeightWithoutJoint = PieceHeight;



            for (int ArrayTav = 0; ArrayTav < JointMaskImage.Length; ArrayTav++)
            {
                LeftJointMask[ArrayTav] = JointMaskImage[ArrayTav];
                RightJointMask[ArrayTav] = rotateImage(JointMaskImage[ArrayTav], 180);
                TopJointMask[ArrayTav] = rotateImage(JointMaskImage[ArrayTav], 270);
                BottomJointMask[ArrayTav] = rotateImage(JointMaskImage[ArrayTav], 90);

#region "Resize Joint mask images for drawing inside mask image And calculate joints width and height"

                //Resize Joint mask images for drawing inside mask image
                //  Image will be resized according to piece width
                int MaskImageWidth = (int)(PieceWidth * 0.3f);
                int MaskImageHeight = (int)((float)MaskImageWidth / ((float)JointMaskImage[ArrayTav].width / (float)JointMaskImage[ArrayTav].height));

                LeftJointMask[ArrayTav] = resizeImage(LeftJointMask[ArrayTav], MaskImageWidth, MaskImageHeight);
                RightJointMask[ArrayTav] = resizeImage(RightJointMask[ArrayTav], MaskImageWidth, MaskImageHeight);
                TopJointMask[ArrayTav] = resizeImage(TopJointMask[ArrayTav], MaskImageWidth, MaskImageHeight);
                BottomJointMask[ArrayTav] = resizeImage(BottomJointMask[ArrayTav], MaskImageWidth, MaskImageHeight);

                
                //Calculate joints width and heights
                CalculateCustomJointDimensions(LeftJointMask[ArrayTav], out LeftJointMaskWidth[ArrayTav], out LeftJointMaskHeight[ArrayTav]);
                
                RightJointMaskWidth[ArrayTav] = LeftJointMaskWidth[ArrayTav];
                RightJointMaskHeight[ArrayTav] = LeftJointMaskHeight[ArrayTav];

                TopJointMaskWidth[ArrayTav] = LeftJointMaskHeight[ArrayTav];
                TopJointMaskHeight[ArrayTav] = LeftJointMaskWidth[ArrayTav];

                BottomJointMaskWidth[ArrayTav] = LeftJointMaskHeight[ArrayTav];
                BottomJointMaskHeight[ArrayTav] = LeftJointMaskWidth[ArrayTav];
                
                
                /*
                //Save these image
                saveTexture2D(LeftJointMask[ArrayTav], "c:\\Images\\LeftJoint.png");
                saveTexture2D(RightJointMask[ArrayTav], "c:\\Images\\RightJointMask.png");
                saveTexture2D(TopJointMask[ArrayTav], "c:\\Images\\TopJointMask.png");
                saveTexture2D(BottomJointMask[ArrayTav], "c:\\Images\\BottomJointMask.png");
                */

#endregion

            }




#region "Argument Error Checking"

            //Joint mask image width and height should be same
            //Joint mask image should have only black and white pixels inside it

            if (JointMaskImage[0].width != JointMaskImage[0].height)
            {
                Debug.LogError("JointMaskImage width and height should be same");
                return null;
            }
            else
            {
                bool ErrorFound = false;  //If Non-Black or Non-White pixel found

                //Check for pixel colors in joint mask image
                for (int rowtrav = 0; rowtrav < JointMaskImage[0].height && !ErrorFound; rowtrav++)
                {
                    for (int coltrav = 0; coltrav < JointMaskImage[0].width && !ErrorFound; coltrav++)
                    {
                        Color PixelColor = JointMaskImage[0].GetPixel(coltrav, rowtrav);

                        if (PixelColor != Color.white || PixelColor != Color.black)
                        {
                            ErrorFound = true;

                            //Debug.LogError("Only white and black pixels are allowed in JointMaskImage");

                            //return null;
                        }
                    }
                }


            }

#endregion

            Color[][] CreatedMaskImage = new Color[PuzzleImgWidth][];
            
            //Clear Instantiated mask image
            for (int i = 0; i < PuzzleImgWidth; i++)
            {
                CreatedMaskImage[i] = new Color[PuzzleImgHeight];
                for (int j = 0; j < PuzzleImgHeight; j++)
                {
                    CreatedMaskImage[i][j] = Color.white;
                }
            }


#region "Color Pieces Alternatively And Generate random joint info And Draw joints"

            bool AlternatePiece = true;

            //if (ColorPieces)
            {
                Random.seed = System.DateTime.Now.Second;

                for (int RowTrav = 0; RowTrav < Rows; RowTrav++)
                {

                    for (int ColTrav = 0; ColTrav < Cols; ColTrav++)
                    {
                        int PieceX = ColTrav * PieceWidth;
                        int PieceY = RowTrav * PieceHeight;
                        Color PieceColor = AlternatePiece ? Color.green : Color.red;

                        for (int InternalRowTrav = PieceY; InternalRowTrav < PieceY + PieceHeight; InternalRowTrav++)
                            for (int InternalColTrav = PieceX; InternalColTrav < PieceX + PieceWidth; InternalColTrav++)
                                if (CreatedMaskImage[InternalColTrav][InternalRowTrav] == Color.white)
                                    CreatedMaskImage[InternalColTrav][InternalRowTrav] = PieceColor;


#region "Generate Random joint info and Draw Joints From Mask Image"


#region "Draw right joints according to piece joint information"

                        if (ColTrav < Cols - 1)
                        {
                            int SelectedRandomJoint = Random.Range(1, JointMaskImage.Length) - 1;

                            //Create random joint information
                            int RndVal = (int)(Random.Range(1f, 18f) >= 10 ? 1 : 0);
                            ResultPiecesData[RowTrav, ColTrav].AddJoint(new SJointInfo((EJointType)RndVal, EJointPosition.Right, 
                                            RightJointMaskWidth[SelectedRandomJoint], RightJointMaskHeight[SelectedRandomJoint]));


                            int JointX = PieceX + PieceWidth - 5;
                            int JointY = PieceY + (PieceHeight / 2) - (RightJointMask[SelectedRandomJoint].height / 2);

                            bool Result = false;
                            SJointInfo RightJointInfo = ResultPiecesData[RowTrav, ColTrav].GetJoint(EJointPosition.Right, out Result);

                            if (!Result)
                            {
                                Debug.LogError("Logical error in draw joints from mask image Right Joints");
                            }
                            else
                            {
                                if ( RightJointInfo.JointType == EJointType.Male )
                                    drawJoint(ref CreatedMaskImage, RightJointMask[SelectedRandomJoint], PieceColor, JointX, JointY);
                            }

                        }
#endregion

#region"Draw left joints according to piece joint information"

                        if (ColTrav > 0)
                        {
                            int SelectedRandomJoint = Random.Range(1, JointMaskImage.Length) - 1;

                            //Create random joint information
                            bool Result = false;

                            SJointInfo PreviousRightJoint = ResultPiecesData[RowTrav, ColTrav - 1].GetJoint(EJointPosition.Right, out Result);

                            if (Result == false)
                            {
                                Debug.LogError("Logical error in joints information left joint");
                            }
                            else
                            {
                                SJointInfo CalcLeftJoint = new SJointInfo(PreviousRightJoint.JointType == EJointType.Female ?
                                            EJointType.Male : EJointType.Female, EJointPosition.Left, 
                                            LeftJointMaskWidth[SelectedRandomJoint], LeftJointMaskHeight[SelectedRandomJoint]);
                                ResultPiecesData[RowTrav , ColTrav].AddJoint(CalcLeftJoint);
                            }


                            int JointX = PieceX - LeftJointMask[SelectedRandomJoint].width + 5;
                            int JointY = PieceY + (PieceHeight / 2) - (LeftJointMask[SelectedRandomJoint].height / 2);

                            Result = false;
                            SJointInfo LeftJointInfo = ResultPiecesData[RowTrav, ColTrav].GetJoint(EJointPosition.Left, out Result);

                            if (!Result)
                            {
                                Debug.LogError("Logical error in draw joints from mask image Left Joints");
                            }
                            else
                            {
                                if (LeftJointInfo.JointType == EJointType.Male)
                                    drawJoint(ref CreatedMaskImage, LeftJointMask[SelectedRandomJoint], PieceColor, JointX, JointY);
                            }
                        }

#endregion

#region"Draw Top joints according to piece joint information"
                        
                        if (RowTrav < Rows - 1)
                        {
                            int SelectedRandomJoint = Random.Range(1, JointMaskImage.Length) - 1;

                            //Create random joint information
                            int RndVal = (int)(Random.Range(1f, 17f) >= 10 ? 1 : 0);
                            ResultPiecesData[RowTrav, ColTrav].AddJoint(new SJointInfo((EJointType)RndVal, EJointPosition.Top,
                                            TopJointMaskWidth[SelectedRandomJoint], TopJointMaskHeight[SelectedRandomJoint] ));

                            int JointX = PieceX + (PieceWidth / 2) - (TopJointMask[SelectedRandomJoint].width / 2);
                            int JointY = PieceY + PieceHeight - 5;

                            bool Result = false;
                            SJointInfo TopJointInfo = ResultPiecesData[RowTrav, ColTrav].GetJoint(EJointPosition.Top, out Result);

                            if (!Result)
                            {
                                Debug.LogError("Logical error in draw joints from mask image Top Joints");
                            }
                            else
                            {
                                if (TopJointInfo.JointType == EJointType.Male)
                                    drawJoint(ref CreatedMaskImage, TopJointMask[SelectedRandomJoint], PieceColor, JointX, JointY);
                            }

                        }
                        
#endregion

#region"Draw Bottom joints according to piece joint information"

                        if (RowTrav > 0)
                        {
                            int SelectedRandomJoint = Random.Range(1, JointMaskImage.Length) - 1;

                            //Create random joint information
                            bool Result = false;

                            SJointInfo PreviousPieceTopJoint = ResultPiecesData[RowTrav - 1, ColTrav].GetJoint(EJointPosition.Top, out Result);

                            if (Result == false)
                            {
                                Debug.LogError("Logical error in joints information Bottom joint");
                            }
                            else
                            {
                                SJointInfo CalcBottomJoint = new SJointInfo(PreviousPieceTopJoint.JointType == EJointType.Female ?
                                            EJointType.Male : EJointType.Female, EJointPosition.Bottom,
                                            BottomJointMaskWidth[SelectedRandomJoint], BottomJointMaskHeight[SelectedRandomJoint] );

                                ResultPiecesData[RowTrav, ColTrav].AddJoint(CalcBottomJoint);
                            }


                            int JointX = PieceX + (PieceWidth / 2) - (BottomJointMask[SelectedRandomJoint].width / 2);
                            int JointY = PieceY - BottomJointMask[SelectedRandomJoint].height;

                            Result = false;
                            SJointInfo BottomJointInfo = ResultPiecesData[RowTrav, ColTrav].GetJoint(EJointPosition.Bottom, out Result);

                            if (!Result)
                            {
                                Debug.LogError("Logical error in draw joints from mask image Top Joints");
                            }
                            else
                            {
                                if (BottomJointInfo.JointType == EJointType.Male)
                                    drawJoint(ref CreatedMaskImage, BottomJointMask[SelectedRandomJoint], PieceColor, JointX, JointY);
                            }

                        }

#endregion


#endregion


                        AlternatePiece = !AlternatePiece;
                    }

                    if (Cols % 2 == 0)
                        AlternatePiece = !AlternatePiece;
                }


            }

#endregion

            //Store mask image for testing purposes
            //System.IO.File.WriteAllBytes("c:\\Images\\MaskImage.png", CreatedMaskImage.EncodeToPNG());

            Image = CreatedMaskImage;

            
            return ResultPiecesData;
        }
 /// <summary>
 /// Gets all joints information in this piece
 /// </summary>
 /// <returns>Returns array of joints information in this piece</returns>
 public SJointInfo[] GetJoints()
 {
     SJointInfo[] Result = new SJointInfo[_Joints.Count];
     _Joints.CopyTo(Result);
     return Result;
 }
Example #8
0
 /// <summary>
 /// Gets all joints information in this piece
 /// </summary>
 /// <returns>Returns array of joints information in this piece</returns>
 public SJointInfo[] GetJoints()
 {
     SJointInfo[] Result = new SJointInfo[_Joints.Count];
     _Joints.CopyTo(Result);
     return(Result);
 }
        /// <summary>
        /// Draws joint mask image for each side of joints for every piece which is used to later
        /// generate puzzle image for pieces
        /// </summary>
        /// <param name="TopJointMaskImage">Output mask image for top joints</param>
        /// <param name="BotJointMaskImage">Output mask image for bottom joints</param>
        /// <param name="LeftJointMaskImage">Output mask image for left joints</param>
        /// <param name="RightJointMaskImage">Output mask image for right joints</param>
        /// <param name="JointMaskImages">User provided joint mask images to be used</param>
        /// <param name="PieceHeightWithoutJoint">Output piece image height</param>
        /// <param name="PieceWidthWithoutJoint">Output piece image width</param>
        /// <param name="PuzzleImgHeight">Height of user provided puzzle image</param>
        /// <param name="PuzzleImgWidth">Width of user provided puzzle image</param>
        /// <param name="Cols">Total columns in puzzle</param>
        /// <param name="Rows">Total rows in puzzle</param>
        /// <returns>Returns created pieces metadata created during masks creation</returns>
        private SPieceInfo[,] DrawCustomPieceJointsMask(ref Color[][] TopJointMaskImage, ref Color[][] BotJointMaskImage,
                                                        ref Color[][] LeftJointMaskImage, ref Color[][] RightJointMaskImage, Texture2D[] JointMaskImages,
                                                        out int PieceWidthWithoutJoint, out int PieceHeightWithoutJoint, int PuzzleImgWidth, int PuzzleImgHeight, int Rows = 5, int Cols = 5)
        {
            int[] JointMaskWidth  = new int[JointMaskImages.Length];
            int[] JointMaskHeight = new int[JointMaskImages.Length];

            //Create direction wise mask images
            Texture2D[] LeftJointMask   = new Texture2D[JointMaskImages.Length];
            Texture2D[] RightJointMask  = new Texture2D[JointMaskImages.Length];
            Texture2D[] TopJointMask    = new Texture2D[JointMaskImages.Length];
            Texture2D[] BottomJointMask = new Texture2D[JointMaskImages.Length];

            SPieceInfo[,] ResultPiecesData = new SPieceInfo[Rows, Cols];


            //Initialize pieces data
            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Cols; j++)
                {
                    ResultPiecesData[i, j] = new SPieceInfo((i * Cols) + j);
                }
            }

            int PieceHeight = PuzzleImgHeight / Rows;
            int PieceWidth  = PuzzleImgWidth / Cols;

            PieceWidthWithoutJoint  = PieceWidth;
            PieceHeightWithoutJoint = PieceHeight;



            for (int ArrayTav = 0; ArrayTav < JointMaskImages.Length; ArrayTav++)
            {
                LeftJointMask[ArrayTav]   = JointMaskImages[ArrayTav];
                BottomJointMask[ArrayTav] = HelperMethods.rotateImage(JointMaskImages[ArrayTav], 90);
                RightJointMask[ArrayTav]  = HelperMethods.rotateImage(BottomJointMask[ArrayTav], 90);
                TopJointMask[ArrayTav]    = HelperMethods.rotateImage(JointMaskImages[ArrayTav], 270);


                #region "Resize Joint mask images for drawing inside mask image And calculate joints width and height"

                //Resize Joint mask images for drawing inside mask image
                //  Image will be resized according to piece width
                int MaskImageWidth  = (int)(PieceWidth * 0.3f);
                int MaskImageHeight = (int)((float)MaskImageWidth / ((float)JointMaskImages[ArrayTav].width / (float)JointMaskImages[ArrayTav].height));

                LeftJointMask[ArrayTav]   = HelperMethods.resizeImage(LeftJointMask[ArrayTav], MaskImageWidth, MaskImageHeight);
                RightJointMask[ArrayTav]  = HelperMethods.resizeImage(RightJointMask[ArrayTav], MaskImageWidth, MaskImageHeight);
                TopJointMask[ArrayTav]    = HelperMethods.resizeImage(TopJointMask[ArrayTav], MaskImageWidth, MaskImageHeight);
                BottomJointMask[ArrayTav] = HelperMethods.resizeImage(BottomJointMask[ArrayTav], MaskImageWidth, MaskImageHeight);


                //Calculate joints width and heights
                CalculateCustomJointDimensions(LeftJointMask[ArrayTav], out JointMaskWidth[ArrayTav], out JointMaskHeight[ArrayTav]);



                #endregion
            }



            #region "Argument Error Checking"

            //Joint mask image width and height should be same
            //Joint mask image should have only black and white pixels inside it

            if (JointMaskImages[0].width != JointMaskImages[0].height)
            {
                Debug.LogError("JointMaskImage width and height should be same");
                return(null);
            }
            else
            {
                bool ErrorFound = false;  //If Non-Black or Non-White pixel found

                //Check for pixel colors in joint mask image
                for (int rowtrav = 0; rowtrav < JointMaskImages[0].height && !ErrorFound; rowtrav++)
                {
                    for (int coltrav = 0; coltrav < JointMaskImages[0].width && !ErrorFound; coltrav++)
                    {
                        Color PixelColor = JointMaskImages[0].GetPixel(coltrav, rowtrav);

                        if (PixelColor != Color.white || PixelColor != Color.black)
                        {
                            ErrorFound = true;

                            //Debug.LogError("Only white and black pixels are allowed in JointMaskImage");

                            //return null;
                        }
                    }
                }
            }

            #endregion

            TopJointMaskImage   = new Color[PuzzleImgWidth][];
            BotJointMaskImage   = new Color[PuzzleImgWidth][];
            LeftJointMaskImage  = new Color[PuzzleImgWidth][];
            RightJointMaskImage = new Color[PuzzleImgWidth][];

            //Clear Instantiated mask image
            for (int i = 0; i < PuzzleImgWidth; i++)
            {
                TopJointMaskImage[i]   = new Color[PuzzleImgHeight];
                BotJointMaskImage[i]   = new Color[PuzzleImgHeight];
                LeftJointMaskImage[i]  = new Color[PuzzleImgHeight];
                RightJointMaskImage[i] = new Color[PuzzleImgHeight];
            }


            //Generate random joint info And Draw joints

            Random.seed = System.DateTime.Now.Second;
            Color PieceColor = Color.black;


            for (int RowTrav = 0; RowTrav < Rows; RowTrav++)
            {
                for (int ColTrav = 0; ColTrav < Cols; ColTrav++)
                {
                    int PieceX = ColTrav * PieceWidth;
                    int PieceY = RowTrav * PieceHeight;

                    //Generate Random joint info and Draw Joints From Mask Image

                    #region "Draw right joints according to piece joint information"

                    if (ColTrav < Cols - 1)
                    {
                        int SelectedRandomJoint = Random.Range(1, JointMaskImages.Length) - 1;

                        //Create random joint information
                        int RndVal = (int)(Random.Range(1f, 18f) >= 10 ? 1 : 0);
                        ResultPiecesData[RowTrav, ColTrav].AddJoint(new SJointInfo((EJointType)RndVal, EJointPosition.Right,
                                                                                   JointMaskWidth[SelectedRandomJoint], JointMaskHeight[SelectedRandomJoint]));


                        int JointX = PieceX + PieceWidth;
                        int JointY = PieceY + (PieceHeight / 2) - (RightJointMask[SelectedRandomJoint].height / 2);

                        bool       Result         = false;
                        SJointInfo RightJointInfo = ResultPiecesData[RowTrav, ColTrav].GetJoint(EJointPosition.Right, out Result);

                        if (!Result)
                        {
                            Debug.LogError("Logical error in draw joints from mask image Right Joints");
                        }
                        else
                        {
                            if (RightJointInfo.JointType == EJointType.Male)
                            {
                                drawJoint(ref RightJointMaskImage, RightJointMask[SelectedRandomJoint], PieceColor, JointX, JointY);
                            }
                        }
                    }
                    #endregion

                    #region "Draw left joints according to piece joint information"

                    if (ColTrav > 0)
                    {
                        int SelectedRandomJoint = Random.Range(1, JointMaskImages.Length) - 1;

                        //Create random joint information
                        bool Result = false;

                        SJointInfo PreviousRightJoint = ResultPiecesData[RowTrav, ColTrav - 1].GetJoint(EJointPosition.Right, out Result);

                        if (Result == false)
                        {
                            Debug.LogError("Logical error in joints information left joint");
                        }
                        else
                        {
                            SJointInfo CalcLeftJoint = new SJointInfo(PreviousRightJoint.JointType == EJointType.Female ?
                                                                      EJointType.Male : EJointType.Female, EJointPosition.Left,
                                                                      JointMaskWidth[SelectedRandomJoint], JointMaskHeight[SelectedRandomJoint]);
                            ResultPiecesData[RowTrav, ColTrav].AddJoint(CalcLeftJoint);
                        }


                        int JointX = PieceX - LeftJointMask[SelectedRandomJoint].width;
                        int JointY = PieceY + (PieceHeight / 2) - (LeftJointMask[SelectedRandomJoint].height / 2);

                        Result = false;
                        SJointInfo LeftJointInfo = ResultPiecesData[RowTrav, ColTrav].GetJoint(EJointPosition.Left, out Result);

                        if (!Result)
                        {
                            Debug.LogError("Logical error in draw joints from mask image Left Joints");
                        }
                        else
                        {
                            if (LeftJointInfo.JointType == EJointType.Male)
                            {
                                drawJoint(ref LeftJointMaskImage, LeftJointMask[SelectedRandomJoint], PieceColor, JointX, JointY);
                            }
                        }
                    }

                    #endregion

                    #region "Draw Top joints according to piece joint information"

                    if (RowTrav < Rows - 1)
                    {
                        int SelectedRandomJoint = Random.Range(1, JointMaskImages.Length) - 1;

                        //Create random joint information
                        int RndVal = (int)(Random.Range(1f, 17f) >= 10 ? 1 : 0);
                        ResultPiecesData[RowTrav, ColTrav].AddJoint(new SJointInfo((EJointType)RndVal, EJointPosition.Top,
                                                                                   JointMaskWidth[SelectedRandomJoint], JointMaskHeight[SelectedRandomJoint]));

                        int JointX = PieceX + (PieceWidth / 2) - (TopJointMask[SelectedRandomJoint].width / 2);
                        int JointY = PieceY + PieceHeight;

                        bool       Result       = false;
                        SJointInfo TopJointInfo = ResultPiecesData[RowTrav, ColTrav].GetJoint(EJointPosition.Top, out Result);

                        if (!Result)
                        {
                            Debug.LogError("Logical error in draw joints from mask image Top Joints");
                        }
                        else
                        {
                            if (TopJointInfo.JointType == EJointType.Male)
                            {
                                drawJoint(ref TopJointMaskImage, TopJointMask[SelectedRandomJoint], PieceColor, JointX, JointY);
                            }
                        }
                    }

                    #endregion

                    #region "Draw Bottom joints according to piece joint information"

                    if (RowTrav > 0)
                    {
                        int SelectedRandomJoint = Random.Range(1, JointMaskImages.Length) - 1;

                        //Create random joint information
                        bool Result = false;

                        SJointInfo PreviousPieceTopJoint = ResultPiecesData[RowTrav - 1, ColTrav].GetJoint(EJointPosition.Top, out Result);

                        if (Result == false)
                        {
                            Debug.LogError("Logical error in joints information Bottom joint");
                        }
                        else
                        {
                            SJointInfo CalcBottomJoint = new SJointInfo(PreviousPieceTopJoint.JointType == EJointType.Female ?
                                                                        EJointType.Male : EJointType.Female, EJointPosition.Bottom,
                                                                        JointMaskWidth[SelectedRandomJoint], JointMaskHeight[SelectedRandomJoint]);

                            ResultPiecesData[RowTrav, ColTrav].AddJoint(CalcBottomJoint);
                        }


                        int JointX = PieceX + (PieceWidth / 2) - (BottomJointMask[SelectedRandomJoint].width / 2);
                        int JointY = PieceY - BottomJointMask[SelectedRandomJoint].height;

                        Result = false;
                        SJointInfo BottomJointInfo = ResultPiecesData[RowTrav, ColTrav].GetJoint(EJointPosition.Bottom, out Result);

                        if (!Result)
                        {
                            Debug.LogError("Logical error in draw joints from mask image Top Joints");
                        }
                        else
                        {
                            if (BottomJointInfo.JointType == EJointType.Male)
                            {
                                drawJoint(ref BotJointMaskImage, BottomJointMask[SelectedRandomJoint], PieceColor, JointX, JointY);
                            }
                        }
                    }

                    #endregion
                }
            }



            return(ResultPiecesData);
        }