public void Rotate(object backend, double angle) { NSAffineTransform t = new NSAffineTransform(); t.RotateByDegrees((float)angle); t.Concat(); }
public StretchImage GetFlippedImage() { NSAffineTransform t = new NSAffineTransform(); StretchImage image = (StretchImage)this.Copy(); CGSize dimensions = image.Size; nint scaleX = 1; nint scaleY = 1; nfloat drawPointX = 0; nfloat drawPointY = 0; if (EndPoint.X < StartPoint.X) { scaleX = -1; drawPointX = scaleX * dimensions.Width; } if (EndPoint.Y > StartPoint.Y) { scaleY = -1; drawPointY = scaleY * dimensions.Height; } image.LockFocus(); t.Scale(scaleX, scaleY); t.Concat(); image.Draw(new CGPoint(drawPointX, drawPointY), new CGRect(0, 0, dimensions.Width, dimensions.Height), NSCompositingOperation.SourceOver, 1); image.UnlockFocus(); return(image); }
public void ResetTransform(object backend) { GetContext(backend); NSAffineTransform t = new NSAffineTransform(); t.Set(); }
public void Translate(object backend, double tx, double ty) { NSAffineTransform t = new NSAffineTransform(); t.Translate((float)tx, (float)ty); t.Concat(); }
public override void DrawBezelWithFrameInView(NSRect aFrame, NSView aControlView) { NSAffineTransform xform = null; if (aControlView.IsFlipped) { // save graphics state for restoring later NSGraphicsContext.CurrentContext.SaveGraphicsState(); xform = new NSAffineTransform(); xform.TranslateXByYBy(0.0f, aFrame.Height); xform.ScaleXByYBy(1.0f, -1.0f); xform.Concat(); } if (IsHighlighted) { iDrawer.DrawDown(aFrame); } else { iDrawer.DrawHover(aFrame); } if (xform != null) { xform.Release(); NSGraphicsContext.CurrentContext.RestoreGraphicsState(); } // draw the text after any transforms are applied iDrawer.DrawText(aFrame); }
public static NSFont CreateFont(FontFamilyHandler familyHandler, float size, NSFontTraitMask traits, int weight = 5) { var font = NSFontManager.SharedFontManager.FontWithFamily(familyHandler.MacName, traits, weight, size); if (font == null) { if (traits.HasFlag(NSFontTraitMask.Italic)) { // fake italics by transforming the font const float kRotationForItalicText = 14.0f; var fontTransform = new NSAffineTransform(); fontTransform.Scale(size); var italicTransform = new NSAffineTransform(); italicTransform.TransformStruct = Matrix.FromSkew(0, kRotationForItalicText).ToCG(); fontTransform.AppendTransform(italicTransform); traits &= ~NSFontTraitMask.Italic; font = NSFontManager.SharedFontManager.FontWithFamily(familyHandler.MacName, traits, 5, size); if (font != null) { font = NSFont.FromDescription(font.FontDescriptor, fontTransform); } } } return(font); }
public void Scale(object backend, double scaleX, double scaleY) { GetContext(backend); NSAffineTransform t = new NSAffineTransform(); t.Scale((float)scaleX, (float)scaleY); t.Concat(); }
public CIImage OutputImage() { var inputImage = ValueForKey(new NSString("inputImage")); if (inputImage == null) { return(null); } // Monochrome var monochromeFilter = CIFilter.FromName("CIColorMatrix"); monochromeFilter.SetDefaults(); monochromeFilter.SetValueForKey(CIVector.Create(0, 0, 0), new NSString("inputRVector")); monochromeFilter.SetValueForKey(CIVector.Create(0, 0, 0.4f), new NSString("inputGVector")); monochromeFilter.SetValueForKey(CIVector.Create(0, 0, 1), new NSString("inputBVector")); monochromeFilter.SetValueForKey(CIVector.Create(0, 0, 1), new NSString("inputAVector")); monochromeFilter.SetValueForKey(inputImage, new NSString("inputImage")); var glowImage = (CIImage)monochromeFilter.ValueForKey(new NSString("outputImage")); // Scale var centerX = CenterX; var centerY = CenterY; if (centerX > 0) { var transform = new NSAffineTransform(); transform.Translate(centerX, centerY); transform.Scale(1.2f); transform.Translate(-centerX, -centerY); var affineTransformFilter = CIFilter.FromName("CIAffineTransform"); affineTransformFilter.SetDefaults(); affineTransformFilter.SetValueForKey(transform, new NSString("inputTransform")); affineTransformFilter.SetValueForKey(glowImage, new NSString("inputImage")); glowImage = (CIImage)affineTransformFilter.ValueForKey(new NSString("outputImage")); } // Blur var gaussianBlurFilter = CIFilter.FromName("CIGaussianBlur"); gaussianBlurFilter.SetDefaults(); gaussianBlurFilter.SetValueForKey(glowImage, new NSString("inputImage")); gaussianBlurFilter.SetValueForKey(InputRadius != null ? InputRadius : new NSNumber(10.0f), new NSString("inputRadius")); glowImage = (CIImage)gaussianBlurFilter.ValueForKey(new NSString("outputImage")); // Blend var blendFilter = CIFilter.FromName("CISourceOverCompositing"); blendFilter.SetDefaults(); blendFilter.SetValueForKey(glowImage, new NSString("inputBackgroundImage")); blendFilter.SetValueForKey(blendFilter, new NSString("inputImage")); glowImage = (CIImage)blendFilter.ValueForKey(new NSString("outputImage")); return(glowImage); }
public static void Colourize(NSView control, Color color, Action drawAction) { var size = control.Frame.Size; if (size.Width <= 0 || size.Height <= 0) { return; } var image = new NSImage(size); image.LockFocusFlipped(control.IsFlipped); drawAction(); image.UnlockFocus(); var ciImage = CIImage.FromCGImage(image.CGImage); SD.SizeF realSize; if (control.RespondsToSelector(selConvertSizeToBacking)) { realSize = control.ConvertSizeToBacking(size); } else { realSize = control.ConvertSizeToBase(size); } if (control.IsFlipped) { var affineTransform = new NSAffineTransform(); affineTransform.Translate(0, realSize.Height); affineTransform.Scale(1, -1); var filter1 = new CIAffineTransform(); filter1.Image = ciImage; filter1.SetValueForKey(affineTransform, CIInputTransform); ciImage = filter1.ValueForKey(CIOutputImage) as CIImage; } var filter2 = new CIColorControls(); filter2.SetDefaults(); filter2.Image = ciImage; filter2.Saturation = 0.0f; ciImage = filter2.ValueForKey(CIOutputImage) as CIImage; var filter3 = new CIColorMatrix(); filter3.SetDefaults(); filter3.Image = ciImage; filter3.RVector = new CIVector(0, color.R, 0); filter3.GVector = new CIVector(color.G, 0, 0); filter3.BVector = new CIVector(0, 0, color.B); ciImage = filter3.ValueForKey(CIOutputImage) as CIImage; ciImage.Draw(new SD.RectangleF(SD.PointF.Empty, size), new SD.RectangleF(SD.PointF.Empty, realSize), NSCompositingOperation.SourceOver, 1); }
private bool DrawingHandler(CGRect dstRect) { BaseImage.Draw(dstRect); NSAffineTransform transform = new NSAffineTransform(); transform.Scale(DrawingScale); transform.Concat(); foreach (TextField.DrawingItem item in DrawingItems) { item.Draw(); } return(true); }
public static void Colourize(NSView control, Color color, Action drawAction) { var size = control.Frame.Size; var image = new NSImage(size); image.LockFocusFlipped(control.IsFlipped); drawAction(); image.UnlockFocus(); var ciImage = CIImage.FromData(image.AsTiff()); if (control.IsFlipped) { var realSize = control.ConvertSizeToBase(size); var affineTransform = new NSAffineTransform(); affineTransform.Translate(0, realSize.Height); affineTransform.Scale(1, -1); var filter1 = CIFilter.FromName("CIAffineTransform"); filter1.SetValueForKey(ciImage, CIInputImage); filter1.SetValueForKey(affineTransform, CIInputTransform); ciImage = filter1.ValueForKey(CIOutputImage) as CIImage; } var filter2 = CIFilter.FromName("CIColorControls"); filter2.SetDefaults(); filter2.SetValueForKey(ciImage, CIInputImage); filter2.SetValueForKey(new NSNumber(0.0f), CIInputSaturation); ciImage = filter2.ValueForKey(CIOutputImage) as CIImage; var filter3 = CIFilter.FromName("CIColorMatrix"); filter3.SetDefaults(); filter3.SetValueForKey(ciImage, CIInputImage); filter3.SetValueForKey(new CIVector(0, color.R, 0), CIInputRVector); filter3.SetValueForKey(new CIVector(color.G, 0, 0), CIInputGVector); filter3.SetValueForKey(new CIVector(0, 0, color.B), CIInputBVector); ciImage = filter3.ValueForKey(CIOutputImage) as CIImage; image = new NSImage(size); var rep = NSCIImageRep.FromCIImage(ciImage); image.AddRepresentation(rep); image.Draw(SD.PointF.Empty, new SD.RectangleF(SD.PointF.Empty, size), NSCompositingOperation.SourceOver, 1); /* Use this when implemented in maccore: * ciImage.Draw (SD.PointF.Empty, new SD.RectangleF (SD.PointF.Empty, size), NSCompositingOperation.SourceOver, 1); */ }
public override void DrawRect(CoreGraphics.CGRect dirtyRect) { NSBezierPath path = NSBezierPath.FromRect(new CoreGraphics.CGRect(-200, -200, 200, 200)); var rotation = new NSAffineTransform(); rotation.RotateByDegrees((System.nfloat) 30); var transtation = new NSAffineTransform(); transtation.Translate(250, 300); rotation.AppendTransform(transtation); path.TransformUsingAffineTransform(rotation); DrawPath(path); }
public static void Colourize(NSView control, Color color, Action drawAction) { var size = control.Frame.Size; if (size.Width <= 0 || size.Height <= 0) return; var image = new NSImage(size); image.LockFocusFlipped(control.IsFlipped); drawAction(); image.UnlockFocus(); var ciImage = CIImage.FromCGImage(image.CGImage); SD.SizeF realSize; if (control.RespondsToSelector(selConvertSizeToBacking)) realSize = control.ConvertSizeToBacking(size); else realSize = control.ConvertSizeToBase(size); if (control.IsFlipped) { var affineTransform = new NSAffineTransform(); affineTransform.Translate(0, realSize.Height); affineTransform.Scale(1, -1); var filter1 = new CIAffineTransform(); filter1.Image = ciImage; filter1.SetValueForKey(affineTransform, CIInputTransform); ciImage = filter1.ValueForKey(CIOutputImage) as CIImage; } var filter2 = new CIColorControls(); filter2.SetDefaults(); filter2.Image = ciImage; filter2.Saturation = 0.0f; ciImage = filter2.ValueForKey(CIOutputImage) as CIImage; var filter3 = new CIColorMatrix(); filter3.SetDefaults(); filter3.Image = ciImage; filter3.RVector = new CIVector(0, color.R, 0); filter3.GVector = new CIVector(color.G, 0, 0); filter3.BVector = new CIVector(0, 0, color.B); ciImage = filter3.ValueForKey(CIOutputImage) as CIImage; ciImage.Draw(new SD.RectangleF(SD.PointF.Empty, size), new SD.RectangleF(SD.PointF.Empty, realSize), NSCompositingOperation.SourceOver, 1); }
NSImage MakeRotatedCopy(NSImage original, float degrees) { var copy = new NSImage(original.Size); copy.LockFocus(); try { var rot = new NSAffineTransform(); rot.Translate(original.Size.Width / 2, original.Size.Height / 2); rot.RotateByDegrees(degrees); rot.Translate(-original.Size.Width / 2, -original.Size.Height / 2); rot.Concat(); original.Draw(PointF.Empty, RectangleF.Empty, NSCompositingOperation.Copy, 1); } finally { copy.UnlockFocus(); } return(copy); }
public static void Colourize (NSView control, Color color, Action drawAction) { var size = control.Frame.Size; var image = new NSImage (size); image.LockFocusFlipped (control.IsFlipped); drawAction (); image.UnlockFocus (); var ciImage = CIImage.FromData (image.AsTiff ()); if (control.IsFlipped) { var realSize = control.ConvertSizeToBase (size); var affineTransform = new NSAffineTransform (); affineTransform.Translate (0, realSize.Height); affineTransform.Scale (1, -1); var filter1 = CIFilter.FromName ("CIAffineTransform"); filter1.SetValueForKey (ciImage, CIInputImage); filter1.SetValueForKey (affineTransform, CIInputTransform); ciImage = filter1.ValueForKey (CIOutputImage) as CIImage; } var filter2 = CIFilter.FromName ("CIColorControls"); filter2.SetDefaults (); filter2.SetValueForKey (ciImage, CIInputImage); filter2.SetValueForKey (new NSNumber (0.0f), CIInputSaturation); ciImage = filter2.ValueForKey (CIOutputImage) as CIImage; var filter3 = CIFilter.FromName ("CIColorMatrix"); filter3.SetDefaults (); filter3.SetValueForKey (ciImage, CIInputImage); filter3.SetValueForKey (new CIVector (0, color.R, 0), CIInputRVector); filter3.SetValueForKey (new CIVector (color.G, 0, 0), CIInputGVector); filter3.SetValueForKey (new CIVector (0, 0, color.B), CIInputBVector); ciImage = filter3.ValueForKey (CIOutputImage) as CIImage; image = new NSImage (size); var rep = NSCIImageRep.FromCIImage (ciImage); image.AddRepresentation (rep); image.Draw (SD.PointF.Empty, new SD.RectangleF (SD.PointF.Empty, size), NSCompositingOperation.SourceOver, 1); /* Use this when implemented in maccore: ciImage.Draw (SD.PointF.Empty, new SD.RectangleF (SD.PointF.Empty, size), NSCompositingOperation.SourceOver, 1); */ }
public static XIR.Image RemoteRepresentation(this NSBezierPath nsbezierpath) { nfloat width = 1f; nfloat height = 1f; // We check here for the element count. If it is zero an error is being thrown if you access ControlPointBounds. if (nsbezierpath.ElementCount > 0) { // let's make sure we leave a little room for the line width drawing as well by adding the LineWidth of the // bezier path. width = nsbezierpath.ControlPointBounds.Width + nsbezierpath.LineWidth * 2; height = nsbezierpath.ControlPointBounds.Height + nsbezierpath.LineWidth * 2; } else { return(new NSImage(new CGSize(width, height)).RemoteRepresentation()); } var nsimage = new NSImage(new CGSize(width, height)); nsimage.LockFocus(); var transform = new NSAffineTransform(); // We need to offset the image a little, specifically by the line width, so it will not be cut off var offsetXZero = -nsbezierpath.ControlPointBounds.X; var offsetYZero = -nsbezierpath.ControlPointBounds.Y; transform.Translate(offsetXZero + nsbezierpath.LineWidth / 2, offsetYZero + nsbezierpath.LineWidth / 2); nsbezierpath.TransformUsingAffineTransform(transform); brush.SetFill(); nsbezierpath.Fill(); pen.SetStroke(); nsbezierpath.Stroke(); nsimage.UnlockFocus(); return(nsimage.RemoteRepresentation()); }
public CIImage OutputImage () { var inputImage = ValueForKey (new NSString ("inputImage")); if (inputImage == null) return null; // Monochrome var monochromeFilter = CIFilter.FromName ("CIColorMatrix"); monochromeFilter.SetDefaults (); monochromeFilter.SetValueForKey (CIVector.Create (0, 0, 0), new NSString ("inputRVector")); monochromeFilter.SetValueForKey (CIVector.Create (0, 0, 0.4f), new NSString ("inputGVector")); monochromeFilter.SetValueForKey (CIVector.Create (0, 0, 1), new NSString ("inputBVector")); monochromeFilter.SetValueForKey (CIVector.Create (0, 0, 1), new NSString ("inputAVector")); monochromeFilter.SetValueForKey (inputImage, new NSString ("inputImage")); var glowImage = (CIImage)monochromeFilter.ValueForKey (new NSString ("outputImage")); // Scale var centerX = CenterX; var centerY = CenterY; if (centerX > 0) { var transform = new NSAffineTransform (); transform.Translate (centerX, centerY); transform.Scale (1.2f); transform.Translate (-centerX, -centerY); var affineTransformFilter = CIFilter.FromName ("CIAffineTransform"); affineTransformFilter.SetDefaults (); affineTransformFilter.SetValueForKey (transform, new NSString ("inputTransform")); affineTransformFilter.SetValueForKey (glowImage, new NSString ("inputImage")); glowImage = (CIImage)affineTransformFilter.ValueForKey (new NSString ("outputImage")); } // Blur var gaussianBlurFilter = CIFilter.FromName ("CIGaussianBlur"); gaussianBlurFilter.SetDefaults (); gaussianBlurFilter.SetValueForKey (glowImage, new NSString ("inputImage")); gaussianBlurFilter.SetValueForKey (InputRadius != null ? InputRadius : new NSNumber (10.0f), new NSString ("inputRadius")); glowImage = (CIImage)gaussianBlurFilter.ValueForKey (new NSString ("outputImage")); // Blend var blendFilter = CIFilter.FromName ("CISourceOverCompositing"); blendFilter.SetDefaults (); blendFilter.SetValueForKey (glowImage, new NSString ("inputBackgroundImage")); blendFilter.SetValueForKey (blendFilter, new NSString ("inputImage")); glowImage = (CIImage)blendFilter.ValueForKey (new NSString ("outputImage")); return glowImage; }
public void Translate(object backend, double tx, double ty) { GetContext (backend); NSAffineTransform t = new NSAffineTransform (); t.Translate ((float)tx, (float)ty); t.Concat (); }
public void Rotate(object backend, double angle) { GetContext (backend); NSAffineTransform t = new NSAffineTransform (); t.RotateByDegrees ((float)angle); t.Concat (); }
public void ResetTransform(object backend) { GetContext (backend); NSAffineTransform t = new NSAffineTransform (); t.Set (); }
public override void DrawRect(RectangleF dirtyRect) { var context = NSGraphicsContext.CurrentContext.GraphicsPort; var innerBoxShadowColor = NSColor.FromCalibratedRgba (0.896f, 0.896f, 0.896f, 1.0f); var darkHighlight = NSColor.FromCalibratedRgba (0.931f, 0.931f, 0.931f, 1.0f); var boxBackgroundColor = NSColor.FromCalibratedRgba (1.0f, 1.0f, 1.0f, 0.5f); var headingGradient = new NSGradient ( new NSColor[] { NSColor.White, darkHighlight }, new float[] { 0.50f, 1.0f }); var shadow = new NSShadow (); shadow.ShadowColor = innerBoxShadowColor; shadow.ShadowOffset = new SizeF (0.1f, 0.1f); shadow.ShadowBlurRadius = 8.0f; var headerShadow = new NSShadow (); headerShadow.ShadowColor = innerBoxShadowColor; headerShadow.ShadowOffset = new SizeF(1.1f, -1.1f); headerShadow.ShadowBlurRadius = 5.0f; var drawFrame = Bounds; // Main Box Drawing var mainBoxRect = new RectangleF(drawFrame.X + 4.5f, (float)(drawFrame.Y + Math.Floor((drawFrame.Height - 36.0f) * 0.02976f) + 0.5f), drawFrame.Width - 9.0f, (float)(drawFrame.Height - 36.5f - Math.Floor((drawFrame.Height - 36) * 0.02976f))); var mainBoxCornerRadius = 4.0f; var mainBoxInnerRect = new RectangleF(mainBoxRect.X, mainBoxRect.Y, mainBoxRect.Width, mainBoxRect.Height); mainBoxInnerRect.Offset (mainBoxCornerRadius, mainBoxCornerRadius); var mainBoxPath = new NSBezierPath (); mainBoxPath.AppendPathWithArc (new PointF (mainBoxInnerRect.X, mainBoxInnerRect.Y), mainBoxCornerRadius, 180.0f, 270.0f); mainBoxPath.AppendPathWithArc (new PointF (mainBoxInnerRect.Width, mainBoxInnerRect.Y), mainBoxCornerRadius, 270.0f, 360.0f); mainBoxPath.LineTo (new PointF (mainBoxRect.Right, mainBoxRect.Bottom)); mainBoxPath.LineTo (new PointF (mainBoxRect.X, mainBoxRect.Bottom)); mainBoxPath.ClosePath (); boxBackgroundColor.SetFill (); mainBoxPath.Fill (); // Main Box Inner Shadow var mainBoxBorderRect = mainBoxPath.Bounds; mainBoxBorderRect.Inflate(shadow.ShadowBlurRadius, shadow.ShadowBlurRadius); mainBoxBorderRect.Offset(-shadow.ShadowOffset.Width, -shadow.ShadowOffset.Height); mainBoxBorderRect = RectangleF.Union(mainBoxBorderRect, mainBoxPath.Bounds); mainBoxBorderRect.Inflate(1, 1); var mainBoxNegativePath = NSBezierPath.FromRect (mainBoxBorderRect); mainBoxNegativePath.AppendPath (mainBoxPath); mainBoxNegativePath.WindingRule = NSWindingRule.EvenOdd; context.SaveState (); NSShadow shadowWithOffset = (NSShadow)shadow.Copy (); var xOffset = shadowWithOffset.ShadowOffset.Width + Math.Round (mainBoxBorderRect.Size.Width); var yOffset = shadowWithOffset.ShadowOffset.Height; shadowWithOffset.ShadowOffset = new SizeF ((float)(xOffset + (xOffset >= 0 ? 0.1f : -0.1f)), (float)(yOffset + (yOffset >= 0 ? 0.1f : -0.1f))); shadowWithOffset.Set (); NSColor.Gray.SetFill (); mainBoxPath.AddClip (); var transform = new NSAffineTransform (); transform.Translate (-(float)Math.Round (mainBoxBorderRect.Size.Width), 0); transform.TransformBezierPath (mainBoxNegativePath).Fill (); context.RestoreState (); NSColor.Gray.SetStroke (); mainBoxPath.LineWidth = 0.5f; mainBoxPath.Stroke (); // Box Head Drawing var headingBoxCornerRadius = 4.0f; var headingBoxRect = new RectangleF (drawFrame.X + 4.5f, drawFrame.Y + drawFrame.Height - 36.0f, drawFrame.Width - 9.0f, 25.0f); var headingBoxInnerRect = new RectangleF (headingBoxRect.X, headingBoxRect.Y, headingBoxRect.Width, headingBoxRect.Height); headingBoxInnerRect.Offset (headingBoxCornerRadius, headingBoxCornerRadius); var headingBoxPath = new NSBezierPath (); headingBoxPath.MoveTo (new PointF (headingBoxRect.X, headingBoxRect.Y)); headingBoxPath.LineTo (new PointF (headingBoxRect.Right, headingBoxRect.Y)); headingBoxPath.AppendPathWithArc (new PointF (headingBoxInnerRect.Width, headingBoxInnerRect.Bottom), headingBoxCornerRadius, 0.0f, 90.0f); headingBoxPath.AppendPathWithArc (new PointF (headingBoxInnerRect.X, headingBoxInnerRect.Bottom), headingBoxCornerRadius, 90.0f, 180.0f); headingBoxPath.ClosePath (); context.SaveState (); headerShadow.Set (); context.BeginTransparencyLayer (null); headingGradient.DrawInBezierPath (headingBoxPath, -90); context.EndTransparencyLayer (); context.RestoreState (); NSColor.Gray.SetStroke (); headingBoxPath.LineWidth = 0.5f; headingBoxPath.Stroke (); }
public void Scale(object backend, double scaleX, double scaleY) { GetContext (backend); NSAffineTransform t = new NSAffineTransform (); t.Scale ((float)scaleX, (float)scaleY); t.Concat (); }
public override void DrawRect(NSRect aRect) { // calculate the number of tick intervals since the hourglass was shown NSDate dateNow = NSDate.DateWithTimeIntervalSinceNow(0); double timeElapsed = dateNow.TimeIntervalSinceReferenceDate - iTimeShown; int numIntervals = (int)(timeElapsed / 0.06); // save graphics state for restoring later NSGraphicsContext context = NSGraphicsContext.CurrentContext; context.SaveGraphicsState(); // apply transforms to rotate the foreground image NSImage bkgd = Properties.Resources.IconHourglass; NSImage frgd = Properties.Resources.IconHourglass2; NSAffineTransform xform1 = new NSAffineTransform(); xform1.TranslateXByYBy(Bounds.Width * 0.5f, Bounds.Height * 0.5f); xform1.Concat(); NSAffineTransform xform2 = new NSAffineTransform(); xform2.RotateByDegrees(-45.0f * (numIntervals % 8)); xform2.Concat(); bool scale = Bounds.Width < frgd.Size.width || Bounds.Height < frgd.Size.height; NSAffineTransform xform3 = null; if (scale) { float scaleX = Bounds.Width / frgd.Size.width; float scaleY = Bounds.Height / frgd.Size.height; float ratio = scaleX > scaleY ? scaleX : scaleY; xform3 = new NSAffineTransform(); xform3.ScaleBy(ratio); xform3.Concat(); } NSAffineTransform xform4 = new NSAffineTransform(); xform4.TranslateXByYBy(-frgd.Size.width * 0.5f, -frgd.Size.height * 0.5f); xform4.Concat(); // draw background and foreground bkgd.DrawAtPointFromRectOperationFraction(Bounds.origin, NSRect.NSZeroRect, NSCompositingOperation.NSCompositeSourceOver, 1.0f); frgd.DrawAtPointFromRectOperationFraction(Bounds.origin, NSRect.NSZeroRect, NSCompositingOperation.NSCompositeSourceOver, 1.0f); // clean up xform1.Release(); xform2.Release(); if (scale) { xform3.Release(); } xform4.Release(); context.RestoreGraphicsState(); // schedule another timer event this.PerformSelectorWithObjectAfterDelay(ObjectiveCRuntime.Selector("timerUpdate"), null, 0.06); }
public override void DrawImage(NSImage image, System.Drawing.RectangleF frame, NSView controlView) { var context = NSGraphicsContext.CurrentContext.GraphicsPort; var darkHighlight = NSColor.FromCalibratedRgba (0.931f, 0.931f, 0.931f, 1.0f); var innerBoxShadowColor = NSColor.FromCalibratedRgba (0.896f, 0.896f, 0.896f, 1.0f); var switchInnerFillEnabledColor1 = NSColor.FromCalibratedRgba (0.215f, 0.447f, 0.668f, 1.0f); var switchInnerFillEnabledColor2 = NSColor.FromCalibratedHsba (switchInnerFillEnabledColor1.HueComponent, switchInnerFillEnabledColor1.SaturationComponent, 1, switchInnerFillEnabledColor1.AlphaComponent); var switchInnerFillGradient = new NSGradient (NSColor.DarkGray, NSColor.Gray); var buttonFillGradientColors = new NSColor[] { innerBoxShadowColor, NSColor.FromCalibratedRgba(0.948f, 0.948f, 0.948f, 1.0f), NSColor.White }; var buttonFillGradientPositions = new float[] { 0.0f, 0.25f, 0.51f }; var switchButtonFillGradient = new NSGradient (buttonFillGradientColors, buttonFillGradientPositions); var switchInnerFillEnabledGradient = new NSGradient (new NSColor[] { switchInnerFillEnabledColor1, switchInnerFillEnabledColor2 }, new float[] { 0.0f, 1.0f}); var switchBoxInnerShadow = new NSShadow (); switchBoxInnerShadow.ShadowColor = NSColor.Black; switchBoxInnerShadow.ShadowOffset = new SizeF (0.1f, 0.1f); switchBoxInnerShadow.ShadowBlurRadius = 5; var switchButtonShadow = new NSShadow (); switchButtonShadow.ShadowColor = NSColor.DarkGray; switchButtonShadow.ShadowOffset = new SizeF (0.1f, 0.1f); switchButtonShadow.ShadowBlurRadius = 2; var switchFrame = new RectangleF(frame.X, frame.Y, 69, 28); var switchBoxPath = NSBezierPath.FromRoundedRect (new RectangleF (switchFrame.X + 12.5f, switchFrame.Y + switchFrame.Height - 23.5f, 49, 20), 9, 9); var gradientToUse = IntValue > 0 ? switchInnerFillEnabledGradient : switchInnerFillGradient; gradientToUse.DrawInBezierPath (switchBoxPath, -90); var switchBoxBorderRect = switchBoxPath.Bounds; switchBoxBorderRect.Inflate (switchBoxInnerShadow.ShadowBlurRadius, switchBoxInnerShadow.ShadowBlurRadius); switchBoxBorderRect.Offset (-switchBoxInnerShadow.ShadowOffset.Width, -switchBoxInnerShadow.ShadowOffset.Height); switchBoxBorderRect = RectangleF.Union (switchBoxBorderRect, switchBoxPath.Bounds); switchBoxBorderRect.Inflate (1, 1); var switchBoxNegativePath = NSBezierPath.FromRoundedRect (switchBoxBorderRect, 0, 0); switchBoxNegativePath.AppendPath (switchBoxPath); switchBoxNegativePath.WindingRule = NSWindingRule.EvenOdd; context.SaveState (); var switchBoxInnerShadowOffset = new SizeF(0.1f, -1.1f); var xOffset = switchBoxInnerShadowOffset.Width + (float)Math.Round(switchBoxBorderRect.Width); var yOffset = switchBoxInnerShadowOffset.Height; NSShadow switchBoxInnerShadowWithOffset = (NSShadow)switchBoxInnerShadow.Copy (); switchBoxInnerShadowWithOffset.ShadowOffset = new SizeF (xOffset + (xOffset >= 0 ? 0.1f : -0.1f), yOffset + (yOffset >= 0 ? 0.1f : -0.1f)); switchBoxInnerShadowWithOffset.Set (); NSColor.Gray.SetFill (); switchBoxPath.AddClip (); var transform = new NSAffineTransform (); transform.Translate (-(float)Math.Round (switchBoxBorderRect.Width), 0); transform.TransformBezierPath (switchBoxNegativePath).Fill (); context.RestoreState (); NSColor.WindowFrame.SetStroke (); switchBoxPath.LineWidth = 0.5f; switchBoxPath.Stroke (); // Switch label drawing var labelPosition = IntValue > 0 ? 19 : 32; var switchLabelRect = new RectangleF (switchFrame.X + labelPosition, switchFrame.Y + switchFrame.Height - 20, 22, 15); NSMutableParagraphStyle switchLabelStyle = (NSMutableParagraphStyle)NSMutableParagraphStyle.DefaultParagraphStyle.MutableCopy (); switchLabelStyle.Alignment = NSTextAlignment.Center; var switchLabelFontAttributes = NSDictionary.FromObjectsAndKeys ( new NSObject[] { NSFont.FromFontName("Helvetica-Bold", NSFont.SmallSystemFontSize), darkHighlight, switchLabelStyle }, new NSObject[] { NSAttributedString.FontAttributeName, NSAttributedString.ForegroundColorAttributeName, NSAttributedString.ParagraphStyleAttributeName }); var labelText = IntValue > 0 ? "On" : "Off"; new NSString (labelText).DrawString (switchLabelRect, switchLabelFontAttributes); // Switch Button var position = this.IntValue > 0 ? 43.0f : 10.0f; var switchButtonPath = NSBezierPath.FromOvalInRect (new RectangleF (switchFrame.X + position, switchFrame.Y + switchFrame.Height - 24, 21, 21)); context.SaveState (); switchButtonShadow.Set (); context.BeginTransparencyLayer (null); switchButtonFillGradient.DrawInBezierPath (switchButtonPath, 135.0f); context.EndTransparencyLayer (); context.RestoreState (); context.SaveState (); switchButtonShadow.Set (); NSColor.DarkGray.SetStroke (); switchButtonPath.LineWidth = 0.5f; switchButtonPath.Stroke (); context.RestoreState (); }
public override void DrawRect(CGRect dirtyRect) { base.DrawRect(dirtyRect); var attributes = new NSStringAttributes { ForegroundColor = NSColor.Blue, Font = NSFont.FromFontName("Andale Mono", 14) }; ProcessTextFormat(attributes); NSColor.Black.SetFill(); NSBezierPath.FillRect(dirtyRect); lock (Terminal) { int row = ViewTop; float verticalOffset = -row * (float)CharacterHeight; var lines = Terminal.ViewPort.GetLines(ViewTop, Rows); foreach (var line in lines) { if (line == null) { row++; continue; } int column = 0; var xform = new NSAffineTransform(); xform.Scale((nfloat)(line.DoubleWidth ? 2.0 : 1.0), (nfloat)((line.DoubleHeightTop || line.DoubleHeightBottom) ? 2.0 : 1.0)); xform.Concat(); var spanStart = 0; while (column < line.Count) { bool selected = TextSelection == null ? false : TextSelection.Within(column, row); var backgroundColor = GetBackgroundColor(line[column].Attributes, selected); if (column < (line.Count - 1) && GetBackgroundColor(line[column + 1].Attributes, TextSelection == null ? false : TextSelection.Within(column + 1, row)) == backgroundColor) { column++; continue; } var rect = new CGRect( spanStart * CharacterWidth, ((row - (line.DoubleHeightBottom ? 1 : 0)) * CharacterHeight + verticalOffset) * (line.DoubleHeightBottom | line.DoubleHeightTop ? 0.5 : 1.0), ((column - spanStart + 1) * CharacterWidth) + 0.9, CharacterHeight + 0.9 ); backgroundColor.SetFill(); NSBezierPath.FillRect(rect); column++; spanStart = column; } xform.Invert(); xform.Concat(); row++; } row = ViewTop; foreach (var line in lines) { if (line == null) { row++; continue; } int column = 0; var xform = new NSAffineTransform(); xform.Scale((nfloat)(line.DoubleWidth ? 2.0 : 1.0), (nfloat)((line.DoubleHeightTop || line.DoubleHeightBottom) ? 2.0 : 1.0)); xform.Concat(); var spanStart = 0; string toDisplay = string.Empty; while (column < line.Count) { bool selected = TextSelection == null ? false : TextSelection.Within(column, row); var foregroundColor = GetForegroundColor(line[column].Attributes, selected); toDisplay += line[column].Char.ToString() + line[column].CombiningCharacters; if ( column < (line.Count - 1) && GetForegroundColor(line[column + 1].Attributes, TextSelection == null ? false : TextSelection.Within(column + 1, row)) == foregroundColor && line[column + 1].Attributes.Underscore == line[column].Attributes.Underscore && line[column + 1].Attributes.Reverse == line[column].Attributes.Reverse && line[column + 1].Attributes.Bright == line[column].Attributes.Bright ) { column++; continue; } var textPosition = new CGPoint( spanStart * CharacterWidth, ((row - (line.DoubleHeightBottom ? 1 : 0)) * CharacterHeight + verticalOffset + attributes.Font.Descender) * (line.DoubleHeightBottom | line.DoubleHeightTop ? 0.5 : 1.0) ); attributes.ForegroundColor = foregroundColor; attributes.UnderlineStyle = line[column].Attributes.Underscore ? 1 : 0; var nsStr = new NSString(toDisplay); nsStr.DrawAtPoint(textPosition, attributes.Dictionary); column++; spanStart = column; toDisplay = ""; } xform.Invert(); xform.Concat(); row++; } if (Terminal.CursorState.ShowCursor) { var cursorY = Terminal.ViewPort.TopRow - ViewTop + Terminal.CursorState.CurrentRow; var cursorRect = new CGRect( Terminal.CursorState.CurrentColumn * CharacterWidth, cursorY * CharacterHeight, CharacterWidth + 0.9, CharacterHeight + 0.9 ); var path = NSBezierPath.FromRect(cursorRect); GetForegroundColor(Terminal.CursorState.Attributes, false).Set(); path.Stroke(); } } }
internal CCTexture2D CreateTextSprite(string text, CCFontDefinition textDefinition) { if (string.IsNullOrEmpty(text)) return new CCTexture2D(); int imageWidth; int imageHeight; var textDef = textDefinition; var contentScaleFactorWidth = CCLabel.DefaultTexelToContentSizeRatios.Width; var contentScaleFactorHeight = CCLabel.DefaultTexelToContentSizeRatios.Height; textDef.FontSize *= contentScaleFactorWidth; textDef.Dimensions.Width *= contentScaleFactorWidth; textDef.Dimensions.Height *= contentScaleFactorHeight; //bool hasPremultipliedAlpha; // font NSFont font = null; var ext = System.IO.Path.GetExtension(textDef.FontName); if (!String.IsNullOrEmpty(ext) && ext.ToLower() == ".ttf") { try { textDef.FontName = LoadFontFile(textDef.FontName); font = NSFont.FromFontName(textDef.FontName, textDef.FontSize); } catch { CCLog.Log(".ttf {0} file not found or can not be loaded.", textDef.FontName); } } else { // font font = NSFontManager.SharedFontManager.FontWithFamily(textDef.FontName, NSFontTraitMask.Unbold | NSFontTraitMask.Unitalic, 0, textDef.FontSize); } if (font == null) { font = NSFontManager.SharedFontManager.FontWithFamily("Arial", NSFontTraitMask.Unbold | NSFontTraitMask.Unitalic, 0, textDef.FontSize); CCLog.Log("{0} not found. Defaulting to Arial.", textDef.FontName); } // color var foregroundColor = NSColor.White; // alignment var horizontalAlignment = textDef.Alignment; var verticleAlignement = textDef.LineAlignment; var textAlign = (CCTextAlignment.Right == horizontalAlignment) ? NSTextAlignment.Right : (CCTextAlignment.Center == horizontalAlignment) ? NSTextAlignment.Center : NSTextAlignment.Left; // LineBreak var lineBreak = (CCLabelLineBreak.Character == textDef.LineBreak) ? NSLineBreakMode.CharWrapping : (CCLabelLineBreak.Word == textDef.LineBreak) ? NSLineBreakMode.ByWordWrapping : NSLineBreakMode.Clipping; var nsparagraphStyle = new NSMutableParagraphStyle(); nsparagraphStyle.SetParagraphStyle(NSMutableParagraphStyle.DefaultParagraphStyle); nsparagraphStyle.LineBreakMode = lineBreak; nsparagraphStyle.Alignment = textAlign; // Create a new attributed string definition var nsAttributes = new NSStringAttributes (); // Font attribute nsAttributes.Font = font; nsAttributes.ForegroundColor = foregroundColor; nsAttributes.ParagraphStyle = nsparagraphStyle; var stringWithAttributes = new NSAttributedString(text, nsAttributes); var realDimensions = stringWithAttributes.Size; // Mac crashes if the width or height is 0 if (realDimensions == SizeF.Empty) { CCLog.Log("Native string:", "Dimensions of native NSAttributedString can not be 0,0"); return new CCTexture2D(); } var dimensions = new SizeF(textDef.Dimensions.Width, textDef.Dimensions.Height); var layoutAvailable = true; // // * Note * This seems to only effect Mac because iOS works fine without this work around. // Right Alignment BoundingRectWithSize does not seem to be working correctly when the following conditions are set: // 1) Alignment Right // 2) No dimensions // 3) There are new line characters embedded in the string. // // So we set alignment to Left, calculate our bounds and then restore alignement afterwards before drawing. // if (dimensions.Width <= 0) { dimensions.Width = 8388608; layoutAvailable = false; // Set our alignment variables to left - see notes above. nsparagraphStyle.Alignment = NSTextAlignment.Left; stringWithAttributes.Dispose(); stringWithAttributes = null; stringWithAttributes = new NSAttributedString(text, nsAttributes); } if (dimensions.Height <= 0) { dimensions.Height = 8388608; layoutAvailable = false; } // Calculate our bounding rectangle var boundingRect = stringWithAttributes.BoundingRectWithSize(new SizeF((int)dimensions.Width, (int)dimensions.Height), NSStringDrawingOptions.UsesLineFragmentOrigin); if (!layoutAvailable) { if (dimensions.Width == 8388608) { dimensions.Width = boundingRect.Width; // Restore our alignment before drawing - see notes above. nsparagraphStyle.Alignment = textAlign; stringWithAttributes.Dispose(); stringWithAttributes = null; stringWithAttributes = new NSAttributedString(text, nsAttributes); } if (dimensions.Height == 8388608) { dimensions.Height = boundingRect.Height; } } imageWidth = (int)dimensions.Width; imageHeight = (int)dimensions.Height; // Alignment var xOffset = 0.0f; switch (textAlign) { case NSTextAlignment.Left: xOffset = 0; break; case NSTextAlignment.Center: xOffset = (dimensions.Width-boundingRect.Width)/2.0f; break; case NSTextAlignment.Right: xOffset = dimensions.Width-boundingRect.Width; break; default: break; } // Line alignment var yOffset = (CCVerticalTextAlignment.Top == verticleAlignement || boundingRect.Height >= dimensions.Height) ? (dimensions.Height - boundingRect.Height) // align to top : (CCVerticalTextAlignment.Bottom == verticleAlignement) ? 0 // align to bottom : (imageHeight - boundingRect.Height) / 2.0f; // align to center //Find the rect that the string will draw into inside the dimensions var drawRect = new RectangleF(xOffset , yOffset , boundingRect.Width , boundingRect.Height); NSImage image = null; try { //Set antialias or not NSGraphicsContext.CurrentContext.ShouldAntialias = textDef.isShouldAntialias; image = new NSImage(new SizeF(imageWidth, imageHeight)); image.LockFocus(); // set a default transform var transform = new NSAffineTransform(); transform.Set(); stringWithAttributes.DrawInRect(drawRect); image.UnlockFocus(); // We will use Texture2D from stream here instead of CCTexture2D stream. var tex = Texture2D.FromStream(CCDrawManager.SharedDrawManager.XnaGraphicsDevice, image); // Debugging purposes // var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // var fileName = Path.Combine(path, "Label3.png"); // using (var stream = new FileStream(fileName, FileMode.Create, FileAccess.Write)) // { // tex.SaveAsPng(stream, imageWidth, imageHeight); // } // Create our texture of the label string. var texture = new CCTexture2D(tex); return texture; } catch (Exception exc) { CCLog.Log ("CCLabel: Error creating native label:{0}\n{1}", exc.Message, exc.StackTrace); } finally { // clean up the resources if (image != null) { image.Dispose (); image = null; } if (stringWithAttributes != null) { stringWithAttributes.Dispose (); stringWithAttributes = null; } } return new CCTexture2D (); }
NSImage MakeRotatedCopy (NSImage original, float degrees) { var copy = new NSImage (original.Size); copy.LockFocus (); try { var rot = new NSAffineTransform (); rot.Translate (original.Size.Width / 2, original.Size.Height / 2); rot.RotateByDegrees (degrees); rot.Translate (-original.Size.Width / 2, -original.Size.Height / 2); rot.Concat (); original.Draw (PointF.Empty, RectangleF.Empty, NSCompositingOperation.Copy, 1); } finally { copy.UnlockFocus (); } return copy; }
public static NSFont FromDescription(NSFontDescriptor fontDescriptor, NSAffineTransform textTransform) { var ptr = _FromDescription(fontDescriptor, textTransform); return(ptr == IntPtr.Zero ? null : new NSFont(ptr)); }
internal CCTexture2D CreateTextSprite(string text, CCFontDefinition textDefinition) { if (string.IsNullOrEmpty(text)) { return(new CCTexture2D()); } int imageWidth; int imageHeight; var textDef = textDefinition; var contentScaleFactorWidth = CCLabel.DefaultTexelToContentSizeRatios.Width; var contentScaleFactorHeight = CCLabel.DefaultTexelToContentSizeRatios.Height; textDef.FontSize *= contentScaleFactorWidth; textDef.Dimensions.Width *= contentScaleFactorWidth; textDef.Dimensions.Height *= contentScaleFactorHeight; //bool hasPremultipliedAlpha; // font NSFont font = null; var ext = System.IO.Path.GetExtension(textDef.FontName); if (!String.IsNullOrEmpty(ext) && ext.ToLower() == ".ttf") { try { textDef.FontName = LoadFontFile(textDef.FontName); font = NSFont.FromFontName(textDef.FontName, textDef.FontSize); } catch { CCLog.Log(".ttf {0} file not found or can not be loaded.", textDef.FontName); } } else { // font font = NSFontManager.SharedFontManager.FontWithFamily(textDef.FontName, NSFontTraitMask.Unbold | NSFontTraitMask.Unitalic, 0, textDef.FontSize); } if (font == null) { font = NSFontManager.SharedFontManager.FontWithFamily("Arial", NSFontTraitMask.Unbold | NSFontTraitMask.Unitalic, 0, textDef.FontSize); CCLog.Log("{0} not found. Defaulting to Arial.", textDef.FontName); } // color var foregroundColor = NSColor.White; // alignment var horizontalAlignment = textDef.Alignment; var verticleAlignement = textDef.LineAlignment; var textAlign = (CCTextAlignment.Right == horizontalAlignment) ? NSTextAlignment.Right : (CCTextAlignment.Center == horizontalAlignment) ? NSTextAlignment.Center : NSTextAlignment.Left; // LineBreak var lineBreak = (CCLabelLineBreak.Character == textDef.LineBreak) ? NSLineBreakMode.CharWrapping : (CCLabelLineBreak.Word == textDef.LineBreak) ? NSLineBreakMode.ByWordWrapping : NSLineBreakMode.Clipping; var nsparagraphStyle = new NSMutableParagraphStyle(); nsparagraphStyle.SetParagraphStyle(NSMutableParagraphStyle.DefaultParagraphStyle); nsparagraphStyle.LineBreakMode = lineBreak; nsparagraphStyle.Alignment = textAlign; // Create a new attributed string definition var nsAttributes = new NSStringAttributes(); // Font attribute nsAttributes.Font = font; nsAttributes.ForegroundColor = foregroundColor; nsAttributes.ParagraphStyle = nsparagraphStyle; var stringWithAttributes = new NSAttributedString(text, nsAttributes); var realDimensions = stringWithAttributes.Size; // Mac crashes if the width or height is 0 if (realDimensions == SizeF.Empty) { CCLog.Log("Native string:", "Dimensions of native NSAttributedString can not be 0,0"); return(new CCTexture2D()); } var dimensions = new SizeF(textDef.Dimensions.Width, textDef.Dimensions.Height); var layoutAvailable = true; // // * Note * This seems to only effect Mac because iOS works fine without this work around. // Right Alignment BoundingRectWithSize does not seem to be working correctly when the following conditions are set: // 1) Alignment Right // 2) No dimensions // 3) There are new line characters embedded in the string. // // So we set alignment to Left, calculate our bounds and then restore alignement afterwards before drawing. // if (dimensions.Width <= 0) { dimensions.Width = 8388608; layoutAvailable = false; // Set our alignment variables to left - see notes above. nsparagraphStyle.Alignment = NSTextAlignment.Left; stringWithAttributes.Dispose(); stringWithAttributes = null; stringWithAttributes = new NSAttributedString(text, nsAttributes); } if (dimensions.Height <= 0) { dimensions.Height = 8388608; layoutAvailable = false; } // Calculate our bounding rectangle var boundingRect = stringWithAttributes.BoundingRectWithSize(new SizeF((int)dimensions.Width, (int)dimensions.Height), NSStringDrawingOptions.UsesLineFragmentOrigin); if (!layoutAvailable) { if (dimensions.Width == 8388608) { dimensions.Width = boundingRect.Width; // Restore our alignment before drawing - see notes above. nsparagraphStyle.Alignment = textAlign; stringWithAttributes.Dispose(); stringWithAttributes = null; stringWithAttributes = new NSAttributedString(text, nsAttributes); } if (dimensions.Height == 8388608) { dimensions.Height = boundingRect.Height; } } imageWidth = (int)dimensions.Width; imageHeight = (int)dimensions.Height; // Alignment var xOffset = 0.0f; switch (textAlign) { case NSTextAlignment.Left: xOffset = 0; break; case NSTextAlignment.Center: xOffset = (dimensions.Width - boundingRect.Width) / 2.0f; break; case NSTextAlignment.Right: xOffset = dimensions.Width - boundingRect.Width; break; default: break; } // Line alignment var yOffset = (CCVerticalTextAlignment.Top == verticleAlignement || boundingRect.Height >= dimensions.Height) ? (dimensions.Height - boundingRect.Height) // align to top : (CCVerticalTextAlignment.Bottom == verticleAlignement) ? 0 // align to bottom : (imageHeight - boundingRect.Height) / 2.0f; // align to center //Find the rect that the string will draw into inside the dimensions var drawRect = new RectangleF(xOffset , yOffset , boundingRect.Width , boundingRect.Height); NSImage image = null; try { //Set antialias or not NSGraphicsContext.CurrentContext.ShouldAntialias = textDef.isShouldAntialias; image = new NSImage(new SizeF(imageWidth, imageHeight)); image.LockFocus(); // set a default transform var transform = new NSAffineTransform(); transform.Set(); stringWithAttributes.DrawInRect(drawRect); image.UnlockFocus(); // We will use Texture2D from stream here instead of CCTexture2D stream. var tex = Texture2D.FromStream(CCDrawManager.SharedDrawManager.XnaGraphicsDevice, image); // Debugging purposes // var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // var fileName = Path.Combine(path, "Label3.png"); // using (var stream = new FileStream(fileName, FileMode.Create, FileAccess.Write)) // { // tex.SaveAsPng(stream, imageWidth, imageHeight); // } // Create our texture of the label string. var texture = new CCTexture2D(tex); return(texture); } catch (Exception exc) { CCLog.Log("CCLabel: Error creating native label:{0}\n{1}", exc.Message, exc.StackTrace); } finally { // clean up the resources if (image != null) { image.Dispose(); image = null; } if (stringWithAttributes != null) { stringWithAttributes.Dispose(); stringWithAttributes = null; } } return(new CCTexture2D()); }
public StretchImage GetFlippedImage() { NSAffineTransform t = new NSAffineTransform(); StretchImage image = (StretchImage)this.Copy(); CGSize dimensions = image.Size; nint scaleX = 1; nint scaleY = 1; nfloat drawPointX = 0; nfloat drawPointY = 0; if (EndPoint.X < StartPoint.X) { scaleX = -1; drawPointX = scaleX * dimensions.Width; } if (EndPoint.Y > StartPoint.Y) { scaleY = -1; drawPointY = scaleY * dimensions.Height; } image.LockFocus(); t.Scale(scaleX, scaleY); t.Concat(); image.Draw(new CGPoint(drawPointX, drawPointY), new CGRect(0, 0, dimensions.Width, dimensions.Height), NSCompositingOperation.SourceOver, 1); image.UnlockFocus(); return image; }
public static XIR.Image RemoteRepresentation(this NSLineCapStyle obj) { // Customize the line cap style for the new object. var aPath = new NSBezierPath(); var lineWidth = 16; var sampleWidth = 100; // First we draw the presentation line aPath.LineWidth = lineWidth; aPath.MoveTo(new CGPoint(lineWidth, lineWidth)); aPath.LineTo(new CGPoint(lineWidth + sampleWidth, lineWidth)); switch ((NSLineCapStyle)obj) { case NSLineCapStyle.Square: aPath.LineCapStyle = NSLineCapStyle.Square; break; case NSLineCapStyle.Butt: aPath.LineCapStyle = NSLineCapStyle.Butt; break; case NSLineCapStyle.Round: aPath.LineCapStyle = NSLineCapStyle.Round; break; } // let's make sure we leave a little room for the line width drawing as well by adding the lineWidth as well var width = aPath.ControlPointBounds.Right + lineWidth; var height = aPath.ControlPointBounds.Bottom + lineWidth; var nsimage = new NSImage(new CGSize(width, height)); nsimage.LockFocus(); // We need to offset the image a little so it will not be cut off var transform = new NSAffineTransform(); transform.Translate(aPath.LineWidth / 2, aPath.LineWidth / 2); aPath.TransformUsingAffineTransform(transform); brush.Set(); aPath.Stroke(); // Second, we draw the inset line to demonstrate the bounds aPath.RemoveAllPoints(); lineWidth += lineWidth / 2; aPath.LineWidth = 2; aPath.MoveTo(new CGPoint(lineWidth, lineWidth)); aPath.LineTo(new CGPoint(lineWidth + sampleWidth, lineWidth)); pen.Set(); aPath.Stroke(); // Third, we draw the inset line endings which are two circles aPath.RemoveAllPoints(); var circleWidth = 2; aPath.LineWidth = circleWidth; aPath.LineCapStyle = NSLineCapStyle.Butt; aPath.AppendPathWithOvalInRect(new CGRect(lineWidth - (int)(circleWidth / 2), lineWidth - (int)(circleWidth / 2), circleWidth, circleWidth)); aPath.AppendPathWithOvalInRect(new CGRect(lineWidth + sampleWidth - (int)(circleWidth / 2), lineWidth - (int)(circleWidth / 2), circleWidth, circleWidth)); pen.Set(); aPath.Stroke(); nsimage.UnlockFocus(); return(nsimage.RemoteRepresentation()); }