public override void DrawBezelWithFrame(CGRect frame, NSView controlView) { if (IdeApp.Preferences.UserInterfaceSkin == Skin.Dark) { var inset = frame.Inset(0.25f, 0.25f); var path = NSBezierPath.FromRoundedRect(inset, 3, 3); path.LineWidth = 0.5f; // The first time the view is drawn it has a filter of some sort attached so that the colours set here // are made lighter onscreen. // NSColor.FromRgba (0.244f, 0.247f, 0.245f, 1).SetStroke (); // would make the initial colour actually be .56,.56,.56 // // However after switching theme this filter is removed and the colour set here is the actual colour // displayed onscreen. Styles.DarkBorderBrokenColor.ToNSColor().SetStroke(); path.Stroke(); } else { if (controlView.Window.Screen.BackingScaleFactor == 2) { frame = new CGRect(frame.X, frame.Y + 0.5f, frame.Width, frame.Height); } base.DrawBezelWithFrame(frame, controlView); } }
public override void DrawRect(CGRect dirtyRect) { // draw our custom interface // doing anything other than adding a background created issues //double ratio = (DoubleValue - MinValue) / (MaxValue - MinValue); CGRect baseRect = new CGRect(dirtyRect.X + 4, dirtyRect.Y, dirtyRect.Width - 8, dirtyRect.Height); //dirtyRect.Height -= 10; //dirtyRect.Width -= 4; //dirtyRect.X += 2; //dirtyRect.Y += 5; //double finalWidth = ratio * (baseRect.Width); var bg = NSBezierPath.FromRoundedRect(dirtyRect, 2.5f, 2.5f); NSColor.ScrollBar.SetFill(); bg.Fill(); //CGRect active = new CGRect(baseRect.X, baseRect.Y, (nfloat)(finalWidth), dirtyRect.Height); //var fg = NSBezierPath.FromRoundedRect(active, 2.5f, 2.5f); //NSColor.Magenta.SetFill(); //fg.Fill(); base.DrawRect(dirtyRect); }
private NSImage CreateImageWithColor(string colorValue) { NSGraphicsContext.GlobalSaveGraphicsState(); CGSize size = new CGSize(12, 12); NSImage tintImage = new NSImage(size); tintImage.LockFocus(); float cornerRadius = 5f; CGRect rect = new CGRect(0, 0, 10, 10); NSBezierPath path = NSBezierPath.FromRoundedRect(rect, cornerRadius, cornerRadius); if (string.IsNullOrEmpty(colorValue)) { NSColor.Grid.Set(); path.Stroke(); } else { Utility.ColorWithHexColorValue(colorValue, 1.0f).SetFill(); path.Fill(); } tintImage.UnlockFocus(); CGContext context = NSGraphicsContext.CurrentContext.CGContext; return(tintImage); }
public override void DrawRect(CGRect dirtyRect) { if (_brushToRenderer != null) { _brushToRenderer.SetFill(); } else { _colorToRenderer.SetFill(); } var innerRect = NSBezierPath.FromRoundedRect(Bounds, 0, 0); NSBezierPath bezierPath = new NSBezierPath(); bezierPath.MoveTo(new CGPoint(innerRect.Bounds.X, innerRect.Bounds.Y + _bottomLeft)); // Bottom left (origin): bezierPath.AppendPathWithArc(new CGPoint(innerRect.Bounds.X + _bottomLeft, innerRect.Bounds.Y + _bottomLeft), _bottomLeft, (float)180.0, (float)270.0); // Bottom right: bezierPath.AppendPathWithArc(new CGPoint(innerRect.Bounds.X + innerRect.Bounds.Width - _bottomRight, innerRect.Bounds.Y + _bottomRight), _bottomRight, (float)270.0, (float)360.0); // Top right: bezierPath.AppendPathWithArc(new CGPoint(innerRect.Bounds.X + innerRect.Bounds.Width - _topRight, innerRect.Bounds.Y + innerRect.Bounds.Height - _topRight), _topRight, (float)0.0, (float)90.0); // Top left: bezierPath.AppendPathWithArc(new CGPoint(innerRect.Bounds.X + _topLeft, innerRect.Bounds.Y + innerRect.Bounds.Height - _topLeft), _topLeft, (float)90.0, (float)180.0); // Implicitly creates left edge. bezierPath.Fill(); base.DrawRect(dirtyRect); }
public override void DrawWithFrame(CGRect cellFrame, NSView inView) { if (IdeApp.Preferences.UserInterfaceTheme == Theme.Dark) { var inset = cellFrame.Inset(0.25f, 0.25f); inset = new CGRect(inset.X, inset.Y + 2, inset.Width, inset.Height - 2); var path = NSBezierPath.FromRoundedRect(inset, 3, 3); path.LineWidth = 0.5f; Styles.DarkBorderColor.ToNSColor().SetStroke(); path.Stroke(); inset = new CGRect(inset.X + 3, inset.Y, inset.Width, inset.Height); DrawInteriorWithFrame(inset, inView); path = new NSBezierPath(); // Draw the separators for (int segment = 1; segment < SegmentCount; segment++) { nfloat x = inset.X + (33 * segment); path.MoveTo(new CGPoint(x, 0)); path.LineTo(new CGPoint(x, inset.Y + inset.Height)); } path.LineWidth = 0.5f; path.Stroke(); } else { base.DrawWithFrame(cellFrame, inView); } }
public override void DrawSelection(CoreGraphics.CGRect dirtyRect) { //base.DrawRect(dirtyRect); // if (self.selectionHighlightStyle != NSTableViewSelectionHighlightStyleNone) // { // NSRect selectionRect = NSInsetRect(self.bounds, 2.5, 2.5); // [[NSColor colorWithCalibratedWhite:.65 alpha:1.0] setStroke]; // [[NSColor redColor] setFill]; // NSBezierPath* selectionPath = [NSBezierPath bezierPathWithRoundedRect: selectionRect xRadius: 6 yRadius: 6]; // [selectionPath fill]; // [selectionPath stroke]; //} if (this.SelectionHighlightStyle != NSTableViewSelectionHighlightStyle.None) { //NSColor.FromCalibratedWhite(0.65f, 1.0f).SetStroke(); NSColor.FromRgb(51, 153, 255).SetStroke(); NSColor.FromRgb(51, 153, 255).SetFill(); //NSColor.White.SetStroke(); var selectionPath = NSBezierPath.FromRoundedRect(dirtyRect, 0, 0); selectionPath.Fill(); selectionPath.Stroke(); } }
public override void DrawSelection(CGRect dirtyRect) { // Check the selectionHighlightStyle, in case it was set to None if (Frame.Top < LNRConstants.TOCITEMHEIGHT_MIN && Frame.Bottom <= LNRConstants.TOCITEMHEIGHT_MIN + 9) { return; } if (SelectionHighlightStyle != NSTableViewSelectionHighlightStyle.None) { Utility.BlendNSColor(Level, LNRConstants.TOCLEVEL_MAX).Set(); var selectionPath = NSBezierPath.FromRoundedRect(Bounds, 0, 0); selectionPath.Fill(); var cellView = (NSTableCellView)Subviews [0]; var textField = cellView.TextField; NSAttributedString attributeTitle = Utility.AttributedTitle(textField.StringValue, NSColor.White, textField.Font.FontName, (float)textField.Font.PointSize, NSTextAlignment.Left); textField.AttributedStringValue = attributeTitle; //0:color 1:title 2:button 3:boxline var button = (NSButton)cellView.Subviews [2]; if (button.Tag == 3 && !IsSubSelect) { button.Cell.Image = Utility.ImageWithFilePath("/Images/TOC/toc-doc-white.png"); } } }
/// <summary> /// Draws the focus ring mask. /// </summary> public override void DrawFocusRingMask() { var cornerRadius = this.Bounds.Height / 2.0f; var path = NSBezierPath.FromRoundedRect(this.Bounds, cornerRadius, cornerRadius); NSColor.Black.Set(); path.Fill(); }
public override void DrawRect(CGRect dirtyRect) { NSBezierPath bezierPath = NSBezierPath.FromRoundedRect(Bounds, kRoundedRadius, kRoundedRadius); bezierPath.LineWidth = 1; NSColor.White.Set(); bezierPath.Fill(); }
public override void DrawBackground(CGRect dirtyRect) { base.DrawBackground(dirtyRect); NSColor textColor; if (IsParent || IsSubSelect) { textColor = NSColor.White; } else { textColor = NSColor.Black; } var cellView = (NSTableCellView)Subviews [0]; var textField = cellView.TextField; NSAttributedString attributeTitle = Utility.AttributedTitle(textField.StringValue, textColor, textField.Font.FontName, (float)textField.Font.PointSize, NSTextAlignment.Left); textField.AttributedStringValue = attributeTitle; var button = (NSButton)cellView.Subviews [2]; if (button.Tag == 3) { if (IsSubSelect) { button.Cell.Image = Utility.ImageWithFilePath("/Images/TOC/toc-doc-white.png"); } else { button.Cell.Image = Utility.ImageWithFilePath("/Images/TOC/toc_doc.png"); } } if (IsParent) { Utility.BlendNSColor(Level, LNRConstants.TOCLEVEL_MAX).Set(); } else if (IsSubSelect) { Utility.BlendNSColor(Level, LNRConstants.TOCLEVEL_MAX).Set(); } else { NSColor.White.Set(); } NSBezierPath selectionPath = NSBezierPath.FromRoundedRect(Bounds, 0, 0); selectionPath.Fill(); }
static Func <CGRect, NSBezierPath> CreateRectFactory(CornerRadius cornerRadius) { if (cornerRadius == CornerRadius.None) { return(NSBezierPath.FromRect); } else { return(rect => NSBezierPath.FromRoundedRect(rect, (float)cornerRadius.RadiusX, (float)cornerRadius.RadiusY)); } }
public override void DrawBar(CGRect aRect, bool flipped) { // Mimick the dimensions of the original slider var originalHeight = aRect.Height; aRect.Height = 2.7f; var radius = aRect.Height / 2; aRect.Y += (originalHeight - aRect.Height) / 2; // Calc the progress percentage to know where one bar starts var progress = (float)((DoubleValue - MinValue) / (MaxValue - MinValue)); var minTrackRect = aRect; minTrackRect.Width *= progress; var maxTrackRect = aRect; maxTrackRect.X += maxTrackRect.Width * progress; maxTrackRect.Width = maxTrackRect.Width * (1 - progress); // Draw min track var minTrackPath = NSBezierPath.FromRoundedRect(minTrackRect, radius, radius); var defaultMinTrackColor = Color.Accent.ToNSColor(); if (Forms.IsMojaveOrNewer) { defaultMinTrackColor = NSColor.ControlAccentColor; } var minTrackColor = MinimumTrackColor.IsDefault ? defaultMinTrackColor : MinimumTrackColor.ToNSColor(); minTrackColor.SetFill(); minTrackPath.Fill(); var defaultMaxTrackColor = NSColor.ControlShadow; if (Forms.IsMojaveOrNewer) { defaultMaxTrackColor = NSColor.SeparatorColor; } // Draw max track var maxTrackPath = NSBezierPath.FromRoundedRect(maxTrackRect, radius, radius); var maxTrackColor = MaximumTrackColor.IsDefault ? defaultMaxTrackColor : MaximumTrackColor.ToNSColor(); maxTrackColor.SetFill(); maxTrackPath.Fill(); }
public override void DrawWithFrame(CGRect cellFrame, NSView inView) { var isPathSelectorViewResponder = inView.Window.FirstResponder is PathSelectorView; if (HasFocus && isPathSelectorViewResponder) { var focusRect = new CGRect(cellFrame.X, cellFrame.Y + 3, cellFrame.Width + 2, cellFrame.Height - 6); var path = NSBezierPath.FromRoundedRect(focusRect, 3, 3); path.LineWidth = 2f; NSColor.KeyboardFocusIndicator.SetStroke(); path.Stroke(); } base.DrawWithFrame(cellFrame, inView); }
public override void DrawSegment(nint segment, CGRect frame, NSView controlView) { var img = base.GetImageForSegment(segment); var rect = new CGRect(Math.Round(frame.X + ((frame.Width / 2) - (img.Size.Width / 2))), Math.Round(frame.Y + ((frame.Height / 2) - (img.Size.Height / 2))), img.Size.Width, img.Size.Height); img.Draw(rect); if (segment == buttonBar.focusedSegment && buttonBar.HasFocus) { var path = NSBezierPath.FromRoundedRect(frame, 3, 3); path.LineWidth = 3.5f; NSColor.KeyboardFocusIndicator.SetStroke(); path.Stroke(); } }
public override void DrawSelection(CGRect dirtyRect) { // Check the selectionHighlightStyle, in case it was set to None if (SelectionHighlightStyle != NSTableViewSelectionHighlightStyle.None) { if (SelBgColor == null) { SelBgColor = NSColor.LightGray; } SelBgColor.Set(); var selectionPath = NSBezierPath.FromRoundedRect(Bounds, 0, 0); selectionPath.Fill(); } }
/// <summary> /// We are using separate layer for higliting elements on a main view /// This method is highliting GUI element depends on current introduction step /// </summary> public void DrawLayer(NSView view, CGRect dirtyRect) { var bg = Colors.WindowBackground; NSColor fillColor = NSColor.FromRgba(bg.RedComponent, bg.GreenComponent, bg.BlueComponent, 0.4f); NSColor strokeColor = NSColor.FromRgb(50, 158, 230); nfloat strokeWidth = 2; if (__InotroductionStage != IntroductionStageEnum.Finished) { NSBezierPath fillRect = NSBezierPath.FromRect(dirtyRect); fillColor.SetFill(); fillRect.Fill(); } if (__InotroductionStage == IntroductionStageEnum.Firewall) { CGRect fwRect = __MainViewController.GetFirewallControlViewRect(); NSBezierPath firewallPath = NSBezierPath.FromRoundedRect(fwRect, fwRect.Height / 2, fwRect.Height / 2); firewallPath.LineWidth = strokeWidth; strokeColor.SetStroke(); firewallPath.Stroke(); } if (__InotroductionStage == IntroductionStageEnum.ConnectBtn) { // CONNECT BUTTON CGRect circleRect = __MainViewController.GetConnectButtonViewRect(); NSBezierPath connectBthPth = NSBezierPath.FromRoundedRect(circleRect, circleRect.Width / 2, circleRect.Height / 2); connectBthPth.LineWidth = strokeWidth; strokeColor.SetStroke(); connectBthPth.Stroke(); } if (__InotroductionStage == IntroductionStageEnum.Servers) { // SERVERS SELECTION CGRect serversRect = __MainViewController.GetServerSelectionViewRect(); serversRect = new CGRect(serversRect.X + 3, serversRect.Y + 3, serversRect.Width - 6, serversRect.Height - 6); NSBezierPath serversViewPath = NSBezierPath.FromRoundedRect(serversRect, 4, 4); serversViewPath.LineWidth = strokeWidth; strokeColor.SetStroke(); serversViewPath.Stroke(); } }
public override void DrawSelection(RectangleF dirtyRect) { // Check the selectionHighlightStyle, in case it was set to None if (Frame.Top <= LNRConstants.TOCITEMHEIGHT_MIN) { return; } if (SelectionHighlightStyle != NSTableViewSelectionHighlightStyle.None) { Utility.BlendNSColor(Level, LNRConstants.TOCLEVEL_MAX).Set(); NSBezierPath selectionPath = NSBezierPath.FromRoundedRect(Bounds, 0, 0); selectionPath.Fill(); } }
private static void DrawBackgroundInRect(CGRect rect) { var backgroundColor = NSColor.FromRgba(50, 50, 50, 240).CGColor; var borderColor = NSColor.FromRgba(100, 100, 100, 240).CGColor; nfloat radius = 6; var context = NSGraphicsContext.CurrentContext.CGContext; context.SaveState(); context.SetFillColor(backgroundColor); context.SetStrokeColor(borderColor); var path = NSBezierPath.FromRoundedRect(rect, radius, radius); path.LineWidth = 2; path.Stroke(); path.Fill(); context.RestoreState(); }
public override void DrawRect(CoreGraphics.CGRect dirtyRect) { base.DrawRect(dirtyRect); // var bounds: NSRect = self.bounds //var border: NSBezierPath = NSBezierPath(roundedRect: NSInsetRect(bounds, 0.5, 0.5), xRadius: 3, yRadius: 3) //NSColor(red: 47 / 255.0, green: 146 / 255.0, blue: 204 / 255.0, alpha: 1.0).set() //border.stroke() var bound = this.Bounds; bound.X = 0.5f; bound.Y = 0.5f; bound.Height -= 1f; bound.Width -= 1f; var selectionPath = NSBezierPath.FromRoundedRect(bound, 7, 7); NSColor.FromDeviceRgb(153, 153, 153); selectionPath.Stroke(); }
public override void DrawBackground(RectangleF dirtyRect) { if (IsParent) { Utility.BlendNSColor(Level, LNRConstants.TOCLEVEL_MAX).Set(); } else if (IsSubSelect) { //IsSubSelect = false; Utility.BlendNSColor(Level, LNRConstants.TOCLEVEL_MAX).Set(); } else { NSColor.White.Set(); } NSBezierPath selectionPath = NSBezierPath.FromRoundedRect(Bounds, 0, 0); selectionPath.Fill(); }
public override void DrawBezelWithFrame(CGRect frame, NSView controlView) { if (IdeApp.Preferences.UserInterfaceTheme == Theme.Dark) { #pragma warning disable EPS06 // Hidden struct copy operation var inset = frame.Inset(0.25f, 0.25f); #pragma warning restore EPS06 // Hidden struct copy operation var path = NSBezierPath.FromRoundedRect(inset, 3, 3); path.LineWidth = 0.5f; // The first time the view is drawn it has a filter of some sort attached so that the colours set here // are made lighter onscreen. // NSColor.FromRgba (0.244f, 0.247f, 0.245f, 1).SetStroke (); // would make the initial colour actually be .56,.56,.56 // // However after switching theme this filter is removed and the colour set here is the actual colour // displayed onscreen. // This also seems to happen in fullscreen mode and always on High Sierra if (MainToolbar.IsFullscreen || MacSystemInformation.OsVersion >= MacSystemInformation.HighSierra) { Styles.DarkBorderColor.ToNSColor().SetStroke(); } else { Styles.DarkBorderBrokenColor.ToNSColor().SetStroke(); } path.Stroke(); } else { if (controlView.Window?.Screen?.BackingScaleFactor == 2) { frame = new CGRect(frame.X, frame.Y + 0.5f, frame.Width, frame.Height); } base.DrawBezelWithFrame(frame, controlView); } }
public override void DrawWithFrame(CGRect cellFrame, NSView inView) { if (IdeApp.Preferences.UserInterfaceTheme == Theme.Dark) { var inset = cellFrame.Inset(0.25f, 0.25f); if (!ShowsFirstResponder) { var path = NSBezierPath.FromRoundedRect(inset, 3, 3); path.LineWidth = 0.5f; // Hack to make the border be the correct colour in fullscreen mode // See comment in AwesomeBar.cs for more details if (MainToolbar.IsFullscreen) { Styles.DarkBorderBrokenColor.ToNSColor().SetStroke(); } else { Styles.DarkBorderColor.ToNSColor().SetStroke(); } path.Stroke(); } // Can't just call base.DrawInteriorWithFrame because it draws the placeholder text // with a strange emboss effect when it the view is not first responder. // Again, probably because the NSSearchField handles the not first responder state itself // rather than using NSSearchFieldCell //base.DrawInteriorWithFrame (inset, inView); // So instead, draw the various extra cells and text in the correct places SearchButtonCell.DrawWithFrame(SearchButtonRectForBounds(inset), inView); if (!ShowsFirstResponder) { PlaceholderAttributedString.DrawInRect(SearchTextRectForBounds(inset)); } if (!string.IsNullOrEmpty(StringValue)) { CancelButtonCell.DrawWithFrame(CancelButtonRectForBounds(inset), inView); } } else { if (inView.Window?.Screen?.BackingScaleFactor == 2) { nfloat yOffset = 0f; nfloat hOffset = 0f; if (MacSystemInformation.OsVersion >= MacSystemInformation.ElCapitan) { if (inView.Window.IsKeyWindow) { yOffset = 0.5f; hOffset = -0.5f; } else { yOffset = 0f; hOffset = 1.0f; } } else { yOffset = 1f; hOffset = -1f; } cellFrame = new CGRect(cellFrame.X, cellFrame.Y + yOffset, cellFrame.Width, cellFrame.Height + hOffset); } else { nfloat yOffset = 0f; nfloat hOffset = 0f; cellFrame = new CGRect(cellFrame.X, cellFrame.Y + yOffset, cellFrame.Width, cellFrame.Height + hOffset); } base.DrawWithFrame(cellFrame, inView); } }
public static UIBezierPath FromRoundedRect(CGRect rect, nfloat radius) { return(new UIBezierPath(NSBezierPath.FromRoundedRect(rect, radius, radius))); }
public static void DrawUISwitch(CGRect dirtyRect, bool isEnabled, bool isSwitchOn, int BorderWidth, NSColor SwitchOnBackgroundColor, NSColor SwitchOffBackgroundColor, NSColor SwitchOffBorderColor, NSColor InternalSwitcherColor, NSColor InternalSwitcherShadowColor ) { NSGraphicsContext context = NSGraphicsContext.CurrentContext; context.CGContext.SaveState(); NSColor switchOnBackgroundColor = SwitchOnBackgroundColor; NSColor switchOffBackgroundColor = SwitchOffBackgroundColor; NSColor switchOffBorderColor = SwitchOffBorderColor; NSColor internalSwitcherColor = InternalSwitcherColor; NSColor internalSwitcherShadowColor = InternalSwitcherShadowColor; if (isEnabled == false) { // if disabled - set background color darker switchOnBackgroundColor = SetColorDarker(switchOnBackgroundColor); switchOffBackgroundColor = SetColorDarker(switchOffBackgroundColor); switchOffBorderColor = SetColorDarker(switchOffBorderColor); internalSwitcherColor = SetColorDarker(internalSwitcherColor); internalSwitcherShadowColor = SetColorDarker(internalSwitcherShadowColor); } nfloat offset = dirtyRect.Height * 0.1f; dirtyRect = new CGRect(dirtyRect.X + offset, dirtyRect.Y + offset, dirtyRect.Width - offset * 2, dirtyRect.Height - offset * 2); // set backgrund color NSColor bodyColor = (isSwitchOn)? switchOnBackgroundColor : switchOffBackgroundColor; bodyColor.SetFill(); // draw background NSBezierPath bodyPath = NSBezierPath.FromRoundedRect(dirtyRect, dirtyRect.Height / 2, dirtyRect.Height / 2); bodyPath.Fill(); // draw border if (!isSwitchOn) { bodyPath.AddClip(); bodyPath.LineWidth = BorderWidth; if (switchOffBorderColor != null) { switchOffBorderColor.SetStroke(); } bodyPath.Stroke(); } //restore \ save context status context.CGContext.RestoreState(); context.CGContext.SaveState(); // DRAW CIRCLE CGRect circleRect; if (!isSwitchOn) { circleRect = new CGRect(dirtyRect.X, dirtyRect.Y, dirtyRect.Height, dirtyRect.Height); } else { circleRect = new CGRect((dirtyRect.Width - dirtyRect.Height + dirtyRect.X), dirtyRect.Y, dirtyRect.Height, dirtyRect.Height); } // draw circle with shadow (no shadow for dark mode) if (!Colors.IsDarkMode) { CGRect circleShadowRect = new CGRect(circleRect.X + offset / 3, circleRect.Y + offset / 3, circleRect.Height - 2 * offset / 3, circleRect.Height - 2 * offset / 3); NSBezierPath circleShadowPath = NSBezierPath.FromRoundedRect(circleShadowRect, circleShadowRect.Height / 2, circleShadowRect.Height / 2); context.CGContext.SetShadow(new CGSize(offset / 3, -offset), offset * 1f, new CGColor(internalSwitcherShadowColor.RedComponent, internalSwitcherShadowColor.GreenComponent, internalSwitcherShadowColor.BlueComponent)); circleShadowPath.Fill(); } // restore context state context.CGContext.RestoreState(); // set circle color NSColor circleColor = internalSwitcherColor; circleColor.SetFill(); // draw circle without shadow to fill internal area filled by shadow NSBezierPath circlePath = NSBezierPath.FromRoundedRect(circleRect, circleRect.Height / 2, circleRect.Height / 2); circlePath.Fill(); // circle border circlePath.AddClip(); circlePath.LineWidth = 1; if (Colors.IsDarkMode) { // no border for Dark mode internalSwitcherColor.SetStroke(); } else { if (switchOffBorderColor != null) { switchOffBorderColor.SetStroke(); } } circlePath.Stroke(); }
public static NSImage ToRounded(NSImage source, nfloat rad, double cropWidthRatio, double cropHeightRatio, double borderSize, string borderHexColor) { double sourceWidth = source.CGImage.Width; double sourceHeight = source.CGImage.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) { desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); } else if (currentRatio < desiredRatio) { desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); } float cropX = (float)((sourceWidth - desiredWidth) / 2); float cropY = (float)((sourceHeight - desiredHeight) / 2); if (rad == 0) { rad = (nfloat)(Math.Min(desiredWidth, desiredHeight) / 2); } else { rad = (nfloat)(rad * (desiredWidth + desiredHeight) / 2 / 500); } var colorSpace = CGColorSpace.CreateDeviceRGB(); const int bytesPerPixel = 4; int width = (int)desiredWidth; int height = (int)desiredHeight; var bytes = new byte[width * height * bytesPerPixel]; int bytesPerRow = bytesPerPixel * width; const int bitsPerComponent = 8; using (var context = new CGBitmapContext(bytes, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big)) { var clippedRect = new CGRect(0d, 0d, desiredWidth, desiredHeight); context.BeginPath(); using (var path = NSBezierPath.FromRoundedRect(clippedRect, rad, rad)) { context.AddPath(path.ToCGPath()); context.Clip(); } var drawRect = new CGRect(-cropX, -cropY, sourceWidth, sourceHeight); context.DrawImage(drawRect, source.CGImage); if (borderSize > 0d) { borderSize = (borderSize * (desiredWidth + desiredHeight) / 2d / 1000d); var borderRect = new CGRect((0d + borderSize / 2d), (0d + borderSize / 2d), (desiredWidth - borderSize), (desiredHeight - borderSize)); context.BeginPath(); using (var path = NSBezierPath.FromRoundedRect(borderRect, rad, rad)) { context.SetStrokeColor(borderHexColor.ToUIColor().CGColor); context.SetLineWidth((nfloat)borderSize); context.AddPath(path.ToCGPath()); context.StrokePath(); } } using (var output = context.ToImage()) { return(new NSImage(output, CGSize.Empty)); } } }
public static SCNNode SCBoxNode(string title, CGRect frame, NSColor color, float cornerRadius, bool centered) { NSMutableDictionary titleAttributes = null; NSMutableDictionary centeredTitleAttributes = null; // create and extrude a bezier path to build the box var path = NSBezierPath.FromRoundedRect(frame, cornerRadius, cornerRadius); path.Flatness = 0.05f; var shape = SCNShape.Create(path, 20); shape.ChamferRadius = 0.0f; var node = SCNNode.Create(); node.Geometry = shape; // create an image and fill with the color and text var textureSize = new CGSize(); textureSize.Width = (float)Math.Ceiling((double)frame.Size.Width * 1.5); textureSize.Height = (float)Math.Ceiling((double)frame.Size.Height * 1.5); var texture = new NSImage(textureSize); texture.LockFocus(); var drawFrame = new CGRect(0, 0, textureSize.Width, textureSize.Height); nfloat hue, saturation, brightness, alpha; (color.UsingColorSpace(NSColorSpace.DeviceRGBColorSpace)).GetHsba(out hue, out saturation, out brightness, out alpha); var lightColor = NSColor.FromDeviceHsba(hue, saturation - 0.2f, brightness + 0.3f, alpha); lightColor.Set(); NSGraphics.RectFill(drawFrame); NSBezierPath fillpath = null; if (cornerRadius == 0 && centered == false) { //special case for the "labs" slide drawFrame.Offset(0, -2); fillpath = NSBezierPath.FromRoundedRect(drawFrame, cornerRadius, cornerRadius); } else { drawFrame.Inflate(-3, -3); fillpath = NSBezierPath.FromRoundedRect(drawFrame, cornerRadius, cornerRadius); } color.Set(); fillpath.Fill(); // draw the title if any if (title != null) { if (titleAttributes == null) { var paraphStyle = new NSMutableParagraphStyle(); paraphStyle.LineBreakMode = NSLineBreakMode.ByWordWrapping; paraphStyle.Alignment = NSTextAlignment.Center; paraphStyle.MinimumLineHeight = 38; paraphStyle.MaximumLineHeight = 38; var font = NSFont.FromFontName("Myriad Set Semibold", 34) != null?NSFont.FromFontName("Myriad Set Semibold", 34) : NSFont.FromFontName("Avenir Medium", 34); var shadow = new NSShadow(); shadow.ShadowOffset = new CGSize(0, -2); shadow.ShadowBlurRadius = 4; shadow.ShadowColor = NSColor.FromDeviceWhite(0.0f, 0.5f); titleAttributes = new NSMutableDictionary(); titleAttributes.SetValueForKey(font, NSAttributedString.FontAttributeName); titleAttributes.SetValueForKey(NSColor.White, NSAttributedString.ForegroundColorAttributeName); titleAttributes.SetValueForKey(shadow, NSAttributedString.ShadowAttributeName); titleAttributes.SetValueForKey(paraphStyle, NSAttributedString.ParagraphStyleAttributeName); var centeredParaphStyle = (NSMutableParagraphStyle)paraphStyle.MutableCopy(); centeredParaphStyle.Alignment = NSTextAlignment.Center; centeredTitleAttributes = new NSMutableDictionary(); centeredTitleAttributes.SetValueForKey(font, NSAttributedString.FontAttributeName); centeredTitleAttributes.SetValueForKey(NSColor.White, NSAttributedString.ForegroundColorAttributeName); centeredTitleAttributes.SetValueForKey(shadow, NSAttributedString.ShadowAttributeName); centeredTitleAttributes.SetValueForKey(paraphStyle, NSAttributedString.ParagraphStyleAttributeName); } var attrString = new NSAttributedString(title, centered ? centeredTitleAttributes : titleAttributes); var textSize = attrString.Size; //check if we need two lines to draw the text var twoLines = title.Contains("\n"); if (!twoLines) { twoLines = textSize.Width > frame.Size.Width && title.Contains(" "); } //if so, we need to adjust the size to center vertically if (twoLines) { textSize.Height += 38; } if (!centered) { drawFrame.Inflate(-15, 0); } //center vertically var dy = (drawFrame.Size.Height - textSize.Height) * 0.5f; var drawFrameHeight = drawFrame.Size.Height; drawFrame.Size = new CGSize(drawFrame.Size.Width, drawFrame.Size.Height - dy); attrString.DrawString(drawFrame); } texture.UnlockFocus(); //set the created image as the diffuse texture of our 3D box var front = SCNMaterial.Create(); front.Diffuse.Contents = texture; front.LocksAmbientWithDiffuse = true; //use a lighter color for the chamfer and sides var sides = SCNMaterial.Create(); sides.Diffuse.Contents = lightColor; node.Geometry.Materials = new SCNMaterial[] { front, sides, sides, sides, sides }; return(node); }