public void DrawSingleTouchLine(UStep tmp)
 {
     if (tmp.lineEgdeSize == 0)
     {
         if (tmp.isPatternLine)
         {
             VectorBrushesTools.DrawPatternCircle((int)tmp.drawCoordinates[0].x, (int)tmp.drawCoordinates[0].y, paintEngine);
             for (int i = 1; i < tmp.drawCoordinates.Count; i++)
             {
                 BitmapBrushesTools.DrawLineBrush(tmp.drawCoordinates[i - 1], tmp.drawCoordinates[i], paintEngine.brushSize * 2, true, paintEngine.patternBrushBytes, paintEngine);
                 VectorBrushesTools.DrawPatternCircle((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
             }
         }
         else
         {
             VectorBrushesTools.DrawCircle((int)tmp.drawCoordinates[0].x, (int)tmp.drawCoordinates[0].y, paintEngine);
             for (int i = 1; i < tmp.drawCoordinates.Count; i++)
             {
                 BitmapBrushesTools.DrawLineBrush(tmp.drawCoordinates[i - 1], tmp.drawCoordinates[i], paintEngine.brushSize * 2, false, null, paintEngine);
                 VectorBrushesTools.DrawCircle((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
             }
         }
     }
     else
     {
         if (tmp.isPatternLine)
         {
             BitmapBrushesTools.DrawPatternCircle((int)tmp.drawCoordinates[0].x, (int)tmp.drawCoordinates[0].y, paintEngine.customBrushBytes, paintEngine.brushSize + paintEngine.lineEdgeSize, paintEngine);
             VectorBrushesTools.DrawPatternCircle((int)tmp.drawCoordinates[0].x, (int)tmp.drawCoordinates[0].y, paintEngine);
             for (int i = 1; i < tmp.drawCoordinates.Count; i++)
             {
                 //									paintEngine.DrawLineBrush(tmp.drawCoordinates[i-1],tmp.drawCoordinates[i],paintEngine.brushSize*2,true,paintEngine.patternBrushBytes);
                 //									paintEngine.DrawPatternCircle((int)tmp.drawCoordinates[i].x,(int)tmp.drawCoordinates[i].y);
                 BitmapBrushesTools.DrawPatternCircle((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine.customBrushBytes, paintEngine.brushSize + paintEngine.lineEdgeSize, paintEngine);
                 BitmapBrushesTools.DrawLineBrush(tmp.drawCoordinates[i - 1], tmp.drawCoordinates[i], paintEngine.brushSize * 2 + paintEngine.lineEdgeSize * 2, true, paintEngine.customBrushBytes, paintEngine);
                 //
                 if (i > 1)
                 {
                     BitmapBrushesTools.DrawLineBrush(tmp.drawCoordinates[i - 2], tmp.drawCoordinates[i - 1], paintEngine.brushSize * 2 + paintEngine.lineEdgeSize * 2, true, paintEngine.customBrushBytes, paintEngine);
                     BitmapBrushesTools.DrawLineBrush(tmp.drawCoordinates[i - 2], tmp.drawCoordinates[i - 1], paintEngine.brushSize * 2, true, paintEngine.patternBrushBytes, paintEngine);
                     VectorBrushesTools.DrawPatternCircle((int)tmp.drawCoordinates[i - 2].x, (int)tmp.drawCoordinates[i - 2].y, paintEngine);
                 }
             }
         }
         else
         {
             VectorBrushesTools.DrawCircle((int)tmp.drawCoordinates[0].x, (int)tmp.drawCoordinates[0].y, paintEngine);
             for (int i = 1; i < tmp.drawCoordinates.Count; i++)
             {
                 BitmapBrushesTools.DrawLineBrush(tmp.drawCoordinates[i - 1], tmp.drawCoordinates[i], paintEngine.brushSize * 2, false, null, paintEngine);
                 VectorBrushesTools.DrawCircle((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
             }
         }
     }
 }
        /// <summary>
        /// Redo operacija.
        /// </summary>
        public void RedoLastStep()
        {
            {
                //Debug.Log ("Redo step" + redoSteps.Count.ToString ());
                if (!doingWork && redoSteps.Count > 0)
                {
                    doingWork = true;
                    UStep step = redoSteps.Pop();

                    switch (step.type)
                    {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    {                             //BitmapDraw
                        UStep settings = new UStep();
                        settings.SetStepPropertiesFromEngine(paintEngine);


                        if (step.type > -1 || step.type < 5)
                        {
                            Debug.Log("Redo STEP EXEC");
                            step.SetPropertiesFromStep(paintEngine);
                            if (step.type == 0)
                            {
                                switch (step.brushMode)
                                {
                                case BrushProperties.Default:
                                    Debug.Log("DEF-R MODE");
                                    paintEngine.ReadCurrentCustomBrush();
                                    for (int i = 0; i < step.drawCoordinates.Count; i++)
                                    {
                                        BitmapBrushesTools.DrawCustomBrush2((int)step.drawCoordinates [i].x, (int)step.drawCoordinates [i].y, paintEngine);
                                    }
                                    break;

                                case BrushProperties.Simple:
                                    //FIX REDO
                                    paintEngine.ReadCurrentCustomBrush();
                                    for (int i = 0; i < step.drawCoordinates.Count; i++)
                                    {
                                        BitmapBrushesTools.DrawCustomBrush2((int)step.drawCoordinates [i].x, (int)step.drawCoordinates [i].y, paintEngine);
                                    }
                                    break;

                                case BrushProperties.Pattern:
                                    paintEngine.ReadCurrentCustomBrush();
                                    paintEngine.ReadCurrentCustomPattern(step.patternTexture);
                                    for (int i = 0; i < step.drawCoordinates.Count; i++)
                                    {
                                        BitmapBrushesTools.DrawCustomBrush2((int)step.drawCoordinates [i].x, (int)step.drawCoordinates [i].y, paintEngine);
                                    }
                                    break;

                                default:
                                    break;
                                }
                            }
                            else
                            if (step.type == 2)
                            {
                                for (int i = 0; i < step.drawCoordinates.Count; i++)
                                {
                                    if (paintEngine.useSmartFloodFill)
                                    {
                                        FloodFillTools.FloodFillAutoMaskWithThreshold((int)step.drawCoordinates[i].x, (int)step.drawCoordinates[i].y, paintEngine);
                                    }
                                    else
                                    {
                                        if (step.useTreshold)
                                        {
                                            if (step.useMaskLayerOnly)
                                            {
                                                FloodFillTools.FloodFillMaskOnlyWithThreshold((int)step.drawCoordinates[i].x, (int)step.drawCoordinates[i].y, paintEngine);
                                            }
                                            else
                                            {
                                                FloodFillTools.FloodFillWithTreshold((int)step.drawCoordinates[i].x, (int)step.drawCoordinates[i].y, paintEngine);
                                            }
                                        }
                                        else
                                        {
                                            if (step.useMaskLayerOnly)
                                            {
                                                FloodFillTools.FloodFillMaskOnly((int)step.drawCoordinates[i].x, (int)step.drawCoordinates[i].y, paintEngine);
                                            }
                                            else
                                            {
                                                FloodFillTools.FloodFill((int)step.drawCoordinates[i].x, (int)step.drawCoordinates[i].y, paintEngine);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            if (step.type == 1)
                            {
                                if (step.brushMode == BrushProperties.Default)
                                {
                                    if (step.vectorBrushType == VectorBrush.Circle)
                                    {
                                        for (int i = 0; i < step.drawCoordinates.Count; i++)
                                        {
                                            VectorBrushesTools.DrawCircle((int)step.drawCoordinates[i].x, (int)step.drawCoordinates[i].y, paintEngine);
                                        }
                                    }
                                    else
                                    {
                                        for (int i = 0; i < step.drawCoordinates.Count; i++)
                                        {
                                            VectorBrushesTools.DrawRectangle((int)step.drawCoordinates[i].x, (int)step.drawCoordinates[i].y, paintEngine);
                                        }
                                    }
                                }
                                else
                                if (step.brushMode == BrushProperties.Pattern)
                                {
                                    paintEngine.ReadCurrentCustomPattern(step.patternTexture);
                                    if (step.vectorBrushType == VectorBrush.Circle)
                                    {
                                        for (int i = 0; i < step.drawCoordinates.Count; i++)
                                        {
                                            VectorBrushesTools.DrawPatternCircle((int)step.drawCoordinates[i].x, (int)step.drawCoordinates[i].y, paintEngine);
                                        }
                                    }
                                    else
                                    {
                                        for (int i = 0; i < step.drawCoordinates.Count; i++)
                                        {
                                            VectorBrushesTools.DrawPatternRectangle((int)step.drawCoordinates[i].x, (int)step.drawCoordinates[i].y, paintEngine);
                                        }
                                    }
                                }
                            }
                            else if (step.type == 4)
                            {
                                if (paintEngine.multitouchEnabled)
                                {
                                    DrawMultiTouchLine(step);
                                }
                                else
                                {
                                    DrawSingleTouchLine(step);
                                }
                            }

                            paintEngine.tex.LoadRawTextureData(paintEngine.pixels);
                            paintEngine.tex.Apply();
                            //return it to stack
                            steps.Push(step);
                        }
                        else
                        {
                            steps.Push(step);
                        }

                        settings.SetPropertiesFromStep(paintEngine);
                    }
                    break;

                    default:
                        break;
                    }


                    //steps.Push (step);
                    doingWork = false;
                }
            }
        }
        /// <summary>
        /// Reiscrtavanje svih koraka.
        /// Ne ekstenduj ovu funkciju [INTERNAL];
        /// </summary>
        void UndoRedrawSteps()
        {
            Stack <UStep> tmpStack = new Stack <UStep>();
            //save previous settings
            UStep settings = new UStep();

            settings.SetStepPropertiesFromEngine(paintEngine);
            //get all steps from stack bottom to top
            while (steps.Count != 0)
            {
                tmpStack.Push(steps.Pop());
            }
            //redraw all the steps in order of drawing

            while (tmpStack.Count != 0)
            {
                UStep tmp = tmpStack.Pop();
                //skip possible non-drawing steps
                if (tmp.type >= 0 || tmp.type < 5)
                {
                    Debug.Log("UNDO STEP EXEC");
                    //setup engine in order to read step
                    tmp.SetPropertiesFromStep(paintEngine);
                    //bitmap based brushes redraw
                    if (tmp.type == 0)
                    {
                        switch (tmp.brushMode)
                        {
                        case BrushProperties.Default:
                            Debug.Log("DEF MODE");
                            paintEngine.ReadCurrentCustomBrush();
                            for (int i = 0; i < tmp.drawCoordinates.Count; i++)
                            {
                                BitmapBrushesTools.DrawCustomBrush2((int)tmp.drawCoordinates [i].x, (int)tmp.drawCoordinates [i].y, paintEngine);
                            }
                            break;

                        case BrushProperties.Simple:
                            //FIX UNDO
                            paintEngine.ReadCurrentCustomBrush();
                            for (int i = 0; i < tmp.drawCoordinates.Count; i++)
                            {
                                BitmapBrushesTools.DrawCustomBrush2((int)tmp.drawCoordinates [i].x, (int)tmp.drawCoordinates [i].y, paintEngine);
                            }
                            break;

                        case BrushProperties.Pattern:
                            paintEngine.ReadCurrentCustomBrush();
                            paintEngine.ReadCurrentCustomPattern(tmp.patternTexture);
                            for (int i = 0; i < tmp.drawCoordinates.Count; i++)
                            {
                                BitmapBrushesTools.DrawCustomBrush2((int)tmp.drawCoordinates [i].x, (int)tmp.drawCoordinates [i].y, paintEngine);
                            }
                            break;

                        default:
                            break;
                        }
                    }
                    else
                    if (tmp.type == 2)
                    {
                        for (int i = 0; i < tmp.drawCoordinates.Count; i++)
                        {
                            if (paintEngine.useSmartFloodFill)
                            {
                                FloodFillTools.FloodFillAutoMaskWithThreshold((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                            }
                            else
                            {
                                if (tmp.useTreshold)
                                {
                                    if (tmp.useMaskLayerOnly)
                                    {
                                        FloodFillTools.FloodFillMaskOnlyWithThreshold((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                    }
                                    else
                                    {
                                        FloodFillTools.FloodFillWithTreshold((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                    }
                                }
                                else
                                {
                                    if (tmp.useMaskLayerOnly)
                                    {
                                        FloodFillTools.FloodFillMaskOnly((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                    }
                                    else
                                    {
                                        FloodFillTools.FloodFill((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                    }
                                }
                            }
                        }
                    }
                    else
                    if (tmp.type == 1)
                    {
                        if (tmp.brushMode == BrushProperties.Default)
                        {
                            if (tmp.vectorBrushType == VectorBrush.Circle)
                            {
                                for (int i = 0; i < tmp.drawCoordinates.Count; i++)
                                {
                                    VectorBrushesTools.DrawCircle((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                }
                            }
                            else
                            {
                                for (int i = 0; i < tmp.drawCoordinates.Count; i++)
                                {
                                    VectorBrushesTools.DrawRectangle((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                }
                            }
                        }
                        else
                        if (tmp.brushMode == BrushProperties.Pattern)
                        {
                            paintEngine.ReadCurrentCustomPattern(tmp.patternTexture);
                            if (tmp.vectorBrushType == VectorBrush.Circle)
                            {
                                for (int i = 0; i < tmp.drawCoordinates.Count; i++)
                                {
                                    VectorBrushesTools.DrawPatternCircle((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                }
                            }
                            else
                            {
                                for (int i = 0; i < tmp.drawCoordinates.Count; i++)
                                {
                                    VectorBrushesTools.DrawPatternRectangle((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                }
                            }
                        }
                    }
                    else if (tmp.type == 4)
                    {
                        if (paintEngine.multitouchEnabled)
                        {
                            DrawMultiTouchLine(tmp);
                        }
                        else
                        {
                            DrawSingleTouchLine(tmp);
                        }
                    }
                    //return it to stack
                    steps.Push(tmp);
                }
                else
                {
                    steps.Push(tmp);
                }
            }
            //load and apply changes
            paintEngine.tex.LoadRawTextureData(paintEngine.pixels);
            paintEngine.tex.Apply();
            //restore modes
            settings.SetPropertiesFromStep(paintEngine);
        }
 public void DrawMultiTouchLine(UStep tmp)
 {
     if (tmp.lineEgdeSize == 0)
     {
         Debug.Log("DRAW MULTI TOUCH LINE UNDO MANAGER!");
         if (tmp.isPatternLine)
         {
             for (int j = 0; j < tmp.touchCoordinates.Count; j++)
             {
                 VectorBrushesTools.DrawPatternCircle((int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[0]].x, (int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[0]].y, paintEngine);
                 for (int i = 1; i < tmp.touchCoordinates[j].coordinatesIndex.Count; i++)
                 {
                     BitmapBrushesTools.DrawLineBrush(tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i - 1]], tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i]], paintEngine.brushSize * 2, true, paintEngine.patternBrushBytes, paintEngine);
                     VectorBrushesTools.DrawPatternCircle((int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i]].x, (int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i]].y, paintEngine);
                 }
             }
         }
         else
         {
             for (int j = 0; j < tmp.touchCoordinates.Count; j++)
             {
                 VectorBrushesTools.DrawCircle((int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[0]].x, (int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[0]].y, paintEngine);
                 for (int i = 1; i < tmp.touchCoordinates[j].coordinatesIndex.Count; i++)
                 {
                     BitmapBrushesTools.DrawLineBrush(tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i - 1]], tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i]], paintEngine.brushSize * 2, false, null, paintEngine);
                     VectorBrushesTools.DrawCircle((int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i]].x, (int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i]].y, paintEngine);
                 }
             }
         }
     }
     else
     {
         if (tmp.isPatternLine)
         {
             for (int j = 0; j < tmp.touchCoordinates.Count; j++)
             {
                 BitmapBrushesTools.DrawPatternCircle((int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[0]].x, (int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[0]].y, paintEngine.customBrushBytes, paintEngine.brushSize + paintEngine.lineEdgeSize, paintEngine);
                 VectorBrushesTools.DrawPatternCircle((int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[0]].x, (int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[0]].y, paintEngine);
                 for (int i = 1; i < tmp.touchCoordinates[j].coordinatesIndex.Count; i++)
                 {
                     BitmapBrushesTools.DrawPatternCircle((int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i]].x, (int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i]].y, paintEngine.customBrushBytes, paintEngine.brushSize + paintEngine.lineEdgeSize, paintEngine);
                     BitmapBrushesTools.DrawLineBrush(tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i - 1]], tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i]], paintEngine.brushSize * 2 + paintEngine.lineEdgeSize * 2, true, paintEngine.customBrushBytes, paintEngine);
                     //
                     if (i > 1)
                     {
                         BitmapBrushesTools.DrawLineBrush(tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i - 2]], tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i - 1]], paintEngine.brushSize * 2 + paintEngine.lineEdgeSize * 2, true, paintEngine.customBrushBytes, paintEngine);
                         BitmapBrushesTools.DrawLineBrush(tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i - 2]], tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i - 1]], paintEngine.brushSize * 2, true, paintEngine.patternBrushBytes, paintEngine);
                         VectorBrushesTools.DrawPatternCircle((int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i - 2]].x, (int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i - 2]].y, paintEngine);
                     }
                 }
             }
         }
         else
         {
             for (int j = 0; j < tmp.touchCoordinates.Count; j++)
             {
                 VectorBrushesTools.DrawCircle((int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[0]].x, (int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[0]].y, paintEngine);
                 for (int i = 1; i < tmp.touchCoordinates[j].coordinatesIndex.Count; i++)
                 {
                     BitmapBrushesTools.DrawLineBrush(tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i - 1]], tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i]], paintEngine.brushSize * 2, false, null, paintEngine);
                     VectorBrushesTools.DrawCircle((int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i]].x, (int)tmp.drawCoordinates[tmp.touchCoordinates[j].coordinatesIndex[i]].y, paintEngine);
                 }
             }
         }
     }
 }
        //	public void RetriveCachedState()
        //	{
        //
        //	}
        /// <summary>
        /// Reiscrtavanje svih koraka.
        /// Ne ekstenduj ovu funkciju [INTERNAL];
        /// </summary>
        void UndoRedrawSteps()
        {
            Stack <UStep> tmpStack = new Stack <UStep>();
            //save previous settings
            UStep settings = new UStep();

            settings.SetStepPropertiesFromEngine(paintEngine);
            //get all steps from stack bottom to top
//		while(steps.Count!=0)
//		{
//			tmpStack.Push(steps.Pop());
//		}
//		numberOfStepsSinceLastSnapshot=0;
//		numberOfStepsSinceLastSnapshot=(steps.Count+1)%snapshotFrequency;
//			if(totalSnaps>0 && numberOfStepsSinceLastSnapshot!=0)
//			{
////				Debug.Log ("sdfsdfsdfsdfdsfd");
//
//				LoadSnapshot();
//			}
//			else
//			{
////				Debug.Log ("asdsadsa");
//				if(undoSnapshots.Count>0)
//					ClearSnapshot();
//				if(totalSnaps>0)
//					LoadSnapshot();
//				else
//					paintEngine.CopyUndoPixels(paintEngine.pixels);//<---?
//
//				numberOfStepsSinceLastSnapshot=(steps.Count<=snapshotFrequency-1)?(steps.Count):snapshotFrequency-1;
////				Debug.Log ("NUM OF SNAPS "+numberOfStepsSinceLastSnapshot);
//
//			}
            if (numberOfStepsSinceLastSnapshot != 0)
            {
//			numberOfStepsSinceLastSnapshot--;

                while (numberOfStepsSinceLastSnapshot > 0 && steps.Count > 0)
                {
                    tmpStack.Push(steps.Pop());
                    numberOfStepsSinceLastSnapshot--;
                    Debug.Log("Add to tmp stack!");
                }
                Debug.Log("UNDO WITH SC");
            }
            else
            {
                Debug.Log("NO UNDO WITH SC");
                if (steps.Count > 0)
                {
                    tmpStack.Push(steps.Pop());
                }
                if (undoSnapshots.Count > 0)
                {
                    ClearSnapshot();
                }
                if (undoSnapshots.Count > 0)
                {
                    LoadSnapshot();
                }
                numberOfStepsSinceLastSnapshot = (steps.Count <= snapshotFrequency - 1) ? steps.Count : snapshotFrequency - 1;
                while (numberOfStepsSinceLastSnapshot > 0 && steps.Count > 0)
                {
                    tmpStack.Push(steps.Pop());
                    numberOfStepsSinceLastSnapshot--;
                }
            }

            //redraw all the steps in order of drawing
            Debug.Log("TMP COUNT " + tmpStack.Count);
            while (tmpStack.Count != 0)
            {
                UStep tmp = tmpStack.Pop();
                //skip possible non-drawing steps
                if (tmp.type >= 0 || tmp.type < 5)
                {
//				Debug.Log ("UNDO STEP EXEC");
                    //setup engine in order to read step
                    //TODO  compare current and step settings, if they are same than read current custom brush load can be avoided
                    bool sameBrush = false;
                    if (tmp.brushTexture == paintEngine.customBrush)
                    {
                        sameBrush = true;
                    }
                    #if UNITY_EDITOR
                    Debug.Log("Same brush in step " + sameBrush);
                    #endif
                    tmp.SetPropertiesFromStep(paintEngine);

                    //
                    //bitmap based brushes redraw
                    if (tmp.type == 0)
                    {
                        switch (tmp.brushMode)
                        {
                        case BrushProperties.Default:
//						Debug.Log ("DEF MODE");
                            if (!sameBrush)
                            {
                                paintEngine.ReadCurrentCustomBrush();
                            }
                            for (int i = 0; i < tmp.drawCoordinates.Count; i++)
                            {
                                BitmapBrushesTools.DrawCustomBrush2((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                if (i > 0)
                                {
                                    BitmapBrushesTools.DrawLineWithBrush(new Vector2((int)tmp.drawCoordinates[i - 1].x, (int)tmp.drawCoordinates[i - 1].y), new Vector2((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y), paintEngine);
                                }
                            }
                            break;

                        case BrushProperties.Simple:
                            //FIXME UNDO
                            if (!sameBrush)
                            {
                                paintEngine.ReadCurrentCustomBrush();
                            }
                            for (int i = 0; i < tmp.drawCoordinates.Count; i++)
                            {
                                BitmapBrushesTools.DrawCustomBrush2((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                if (i >= 0)
                                {
                                    BitmapBrushesTools.DrawLineWithBrush(new Vector2((int)tmp.drawCoordinates[i - 1].x, (int)tmp.drawCoordinates[i - 1].y), new Vector2((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y), paintEngine);
                                }
                            }
                            break;

                        case BrushProperties.Pattern:
                            if (!sameBrush)
                            {
                                paintEngine.ReadCurrentCustomBrush();
                            }
                            paintEngine.ReadCurrentCustomPattern(tmp.patternTexture);
                            for (int i = 0; i < tmp.drawCoordinates.Count; i++)
                            {
                                BitmapBrushesTools.DrawCustomBrush2((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                            }
                            break;

                        default:
                            break;
                        }
                    }
                    else if (tmp.type == 2)
                    {
                        for (int i = 0; i < tmp.drawCoordinates.Count; i++)
                        {
                            if (paintEngine.useSmartFloodFill)
                            {
                                FloodFillTools.FloodFillAutoMaskWithThreshold((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                            }
                            else
                            {
                                if (tmp.useTreshold)
                                {
                                    if (tmp.useMaskLayerOnly)
                                    {
                                        FloodFillTools.FloodFillMaskOnlyWithThreshold((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                    }
                                    else
                                    {
                                        FloodFillTools.FloodFillWithTreshold((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                    }
                                }
                                else
                                {
                                    if (tmp.useMaskLayerOnly)
                                    {
                                        FloodFillTools.FloodFillMaskOnly((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                    }
                                    else
                                    {
                                        FloodFillTools.FloodFill((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                    }
                                }
                            }
                        }
                    }
                    else if (tmp.type == 1)
                    {
                        if (tmp.brushMode == BrushProperties.Default)
                        {
                            if (tmp.vectorBrushType == VectorBrush.Circle)
                            {
                                for (int i = 0; i < tmp.drawCoordinates.Count; i++)
                                {
                                    VectorBrushesTools.DrawCircle((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                }
                            }
                            else
                            {
                                for (int i = 0; i < tmp.drawCoordinates.Count; i++)
                                {
                                    VectorBrushesTools.DrawRectangle((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                }
                            }
                        }
                        else if (tmp.brushMode == BrushProperties.Pattern)
                        {
                            paintEngine.ReadCurrentCustomPattern(tmp.patternTexture);
                            if (tmp.vectorBrushType == VectorBrush.Circle)
                            {
                                for (int i = 0; i < tmp.drawCoordinates.Count; i++)
                                {
                                    VectorBrushesTools.DrawPatternCircle((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                }
                            }
                            else
                            {
                                for (int i = 0; i < tmp.drawCoordinates.Count; i++)
                                {
                                    VectorBrushesTools.DrawPatternRectangle((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                                }
                            }
                        }
                    }
                    else if (tmp.type == 4)
                    {
                        if (paintEngine.multitouchEnabled)
                        {
                            DrawMultiTouchLine(tmp);
                        }
                        else
                        {
                            DrawSingleTouchLine(tmp);
                        }
                    }
                    else if (tmp.type == 3) // FIXME Cackano! Privremeno za StampBrush
                    {
//						Debug.Log ("STAMP MODE");
                        if (!sameBrush)
                        {
                            paintEngine.ReadCurrentCustomBrush();
                        }
                        for (int i = 0; i < tmp.drawCoordinates.Count; i++)
                        {
                            BitmapBrushesTools.DrawCustomBrush2((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y, paintEngine);
                            if (i > 0)
                            {
                                BitmapBrushesTools.DrawStampLineWithBrush(new Vector2((int)tmp.drawCoordinates[i - 1].x, (int)tmp.drawCoordinates[i - 1].y), new Vector2((int)tmp.drawCoordinates[i].x, (int)tmp.drawCoordinates[i].y), paintEngine);
                            }
                        }
                    }
                    //return it to stack
                    steps.Push(tmp);
                }
                else
                {
                    steps.Push(tmp);
                }
            }
            //load and apply changes
            paintEngine.tex.LoadRawTextureData(paintEngine.pixels);
            paintEngine.tex.Apply();
            //restore modes
            bool sameBrushUsed = false;
            if (settings.brushTexture == paintEngine.customBrush)
            {
                sameBrushUsed = true;
            }
            settings.SetPropertiesFromStep(paintEngine);
            if (paintEngine.drawMode == DrawMode.Stamp || paintEngine.drawMode == DrawMode.CustomBrush) //FIXME Cackano!
            {
                if (!sameBrushUsed)
                {
                    paintEngine.ReadCurrentCustomBrush();
                }
            }
        }