System.Drawing.Drawing2D.HatchBrush.Setup C# (CSharp) Method

Setup() private method

private Setup ( Graphics graphics, bool fill ) : void
graphics Graphics
fill bool
return void
        internal override void Setup(Graphics graphics, bool fill)
        {
            // if this is the same as the last that was set then return
            if (graphics.LastBrush == this)
                return;

            // obtain our width and height so we can set the pattern rectangle
            float hatch_width = getHatchWidth (hatchStyle);
            float hatch_height = getHatchHeight (hatchStyle);

            //choose the pattern to be filled based on the currentPattern selected
            var patternSpace = CGColorSpace.CreatePattern(null);
            graphics.context.SetFillColorSpace(patternSpace);
            patternSpace.Dispose();

            // Pattern default work variables
            var patternRect = new CGRect (HALF_PIXEL_X,HALF_PIXEL_Y,hatch_width+HALF_PIXEL_X,hatch_height+HALF_PIXEL_Y);
            var patternTransform = CGAffineTransform.MakeIdentity();

            // Since all the patterns were developed with MonoMac on Mac OS the coordinate system is
            // defaulted to the lower left corner being 0,0 which means for MonoTouch and any view
            // that is flipped we need to flip it again.  Yep should have thought about it to begin with
            // will look into changing it later if need be.
            #if MONOMAC
            if (graphics.isFlipped)
                patternTransform = new CGAffineTransform(1, 0, 0, -1, 0, hatch_height);
            #endif
            #if MONOTOUCH
            if (!graphics.isFlipped)
                patternTransform = new CGAffineTransform(1, 0, 0, -1, 0, hatch_height);
            #endif

            // DrawPattern callback which will be set depending on hatch style
            CGPattern.DrawPattern drawPattern;

            switch (hatchStyle)
            {
            case HatchStyle.Horizontal:
            case HatchStyle.LightHorizontal:
            case HatchStyle.NarrowHorizontal:
            case HatchStyle.DarkHorizontal:
                drawPattern = HatchHorizontal;
                break;
            case HatchStyle.Vertical:
            case HatchStyle.LightVertical:
            case HatchStyle.NarrowVertical:
            case HatchStyle.DarkVertical:
                patternTransform = CGAffineTransform.MakeRotation(90 * (float)Math.PI / 180);
                drawPattern = HatchHorizontal;
                break;
            case HatchStyle.ForwardDiagonal:
            case HatchStyle.LightDownwardDiagonal:
            case HatchStyle.DarkDownwardDiagonal:
            case HatchStyle.WideDownwardDiagonal:
                // We will flip the x-axis here
                patternTransform = CGAffineTransform.MakeScale(-1,1);
                drawPattern = HatchUpwardDiagonal;
                break;
            case HatchStyle.BackwardDiagonal:
            case HatchStyle.LightUpwardDiagonal:
            case HatchStyle.DarkUpwardDiagonal:
            case HatchStyle.WideUpwardDiagonal:
                drawPattern = HatchUpwardDiagonal;
                break;
            case HatchStyle.LargeGrid:
            case HatchStyle.SmallGrid:
            case HatchStyle.DottedGrid:
                drawPattern = HatchGrid;
                break;
            case HatchStyle.DiagonalCross:
                drawPattern = HatchDiagonalCross;
                break;
            case HatchStyle.Percent05:
            case HatchStyle.Percent10:
            case HatchStyle.Percent20:
            case HatchStyle.Percent25:
            case HatchStyle.Percent30:
            case HatchStyle.Percent40:
            case HatchStyle.Percent50:
            case HatchStyle.Percent60:
            case HatchStyle.Percent70:
            case HatchStyle.Percent75:
            case HatchStyle.Percent80:
            case HatchStyle.Percent90:
                drawPattern = HatchPercentage;
                break;
            case HatchStyle.Sphere:
                drawPattern = HatchSphere;
                break;
            case HatchStyle.DashedDownwardDiagonal:
                patternTransform = CGAffineTransform.MakeScale(-1,1);
                drawPattern = HatchDashedDiagonal;
                break;
            case HatchStyle.DashedUpwardDiagonal:
                drawPattern = HatchDashedDiagonal;
                break;
            case HatchStyle.DashedHorizontal:
                drawPattern = HatchDashedHorizontal;
                break;
            case HatchStyle.DashedVertical:
                patternTransform = CGAffineTransform.MakeRotation(-90 * (float)Math.PI / 180);
                drawPattern = HatchDashedHorizontal;
                break;
            case HatchStyle.LargeConfetti:
            case HatchStyle.SmallConfetti:
                drawPattern = HatchConfetti;
                break;
            case HatchStyle.ZigZag:
                drawPattern = HatchZigZag;
                break;
            case HatchStyle.Wave:
                drawPattern = HatchWave;
                break;
            case HatchStyle.HorizontalBrick:
                drawPattern = HatchHorizontalBrick;
                break;
            case HatchStyle.DiagonalBrick:
                drawPattern = HatchDiagonalBrick;
                break;
            //			case HatchStyle.Weave:
            //				drawPattern = HatchWeave;
            //				break;
            case HatchStyle.Trellis:
                drawPattern = HatchTrellis;
                break;
            case HatchStyle.LargeCheckerBoard:
            case HatchStyle.SmallCheckerBoard:
                drawPattern = HatchCheckered;
                break;
            case HatchStyle.OutlinedDiamond:
                drawPattern = HatchOutlinedDiamond;
                break;
            case HatchStyle.SolidDiamond:
                drawPattern = HatchSolidDiamond;
                break;
            case HatchStyle.DottedDiamond:
                drawPattern = HatchDottedDiamond;
                break;
            case HatchStyle.Divot:
                drawPattern = HatchDivot;
                break;
            case HatchStyle.Shingle:
                drawPattern = HatchShingle;
                break;
            case HatchStyle.Plaid:
                drawPattern = HatchPlaid;
                break;
            default:
                drawPattern = DrawPolkaDotPattern;
                break;
            }

            //set the pattern as the Current Context’s fill pattern
            var pattern = new CGPattern(patternRect,
                                    patternTransform,
                                    hatch_width,hatch_height,
                                        CGPatternTiling.NoDistortion,
                                    true, drawPattern);
            //we dont need to set any color, as the pattern cell itself has chosen its own color
            graphics.context.SetFillPattern(pattern, new nfloat[] { 1 });

            graphics.LastBrush = this;
            // I am setting this to be used for Text coloring in DrawString
            graphics.lastBrushColor = foreColor;
        }