|
| 1 | +// |
| 2 | +// KnobStyleKit.swift |
| 3 | +// UISpike |
| 4 | +// |
| 5 | +// Created by Matthew Fecher on 7/20/17. |
| 6 | +// Copyright © 2017 AudioKit. All rights reserved. |
| 7 | +// |
| 8 | +// Generated by PaintCode |
| 9 | +// http://www.paintcodeapp.com |
| 10 | +// |
| 11 | +// This code was generated by Trial version of PaintCode, therefore cannot be used for commercial purposes. |
| 12 | +// |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +import Cocoa |
| 17 | + |
| 18 | +public class KnobStyleKit : NSObject { |
| 19 | + |
| 20 | + //// Drawing Methods |
| 21 | + |
| 22 | + @objc dynamic public class func drawKnobCanvas(frame targetFrame: NSRect = NSRect(x: 0, y: 0, width: 120, height: 120), resizing: ResizingBehavior = .aspectFit, knobValue: CGFloat = 1) { |
| 23 | + //// General Declarations |
| 24 | + let context = NSGraphicsContext.current()!.cgContext |
| 25 | + |
| 26 | + //// Resize to Target Frame |
| 27 | + NSGraphicsContext.saveGraphicsState() |
| 28 | + let resizedFrame: NSRect = resizing.apply(rect: NSRect(x: 0, y: 0, width: 120, height: 120), target: targetFrame) |
| 29 | + context.translateBy(x: resizedFrame.minX, y: resizedFrame.minY) |
| 30 | + context.scaleBy(x: resizedFrame.width / 120, y: resizedFrame.height / 120) |
| 31 | + let resizedShadowScale: CGFloat = min(resizedFrame.width / 120, resizedFrame.height / 120) |
| 32 | + |
| 33 | + |
| 34 | + //// Color Declarations |
| 35 | + let black = NSColor(red: 0, green: 0, blue: 0, alpha: 1) |
| 36 | + let shadowColor = NSColor(red: 1, green: 0.596, blue: 0, alpha: 1) |
| 37 | + let knobBottom = NSColor(red: 0.18, green: 0.18, blue: 0.192, alpha: 1) |
| 38 | + let knobLight = NSColor(red: 0.498, green: 0.498, blue: 0.51, alpha: 1) |
| 39 | + let knobTop2 = NSColor(red: 0.141, green: 0.141, blue: 0.161, alpha: 1) |
| 40 | + let orange2 = NSColor(red: 0.902, green: 0.533, blue: 0.008, alpha: 1) |
| 41 | + |
| 42 | + //// Gradient Declarations |
| 43 | + let edge2 = NSGradient(colors: [knobBottom, knobBottom.blended(withFraction: 0.5, of: knobTop2)!, knobTop2, knobTop2.blended(withFraction: 0.5, of: knobLight)!, knobLight], atLocations: [0.0, 0.23, 0.41, 0.73, 1.0], colorSpace: NSColorSpace.sRGB)! |
| 44 | + let lowerKnobGradient2 = NSGradient(colors: [knobTop2, knobBottom, knobLight], atLocations: [0.0, 0.51, 1.0], colorSpace: NSColorSpace.sRGB)! |
| 45 | + |
| 46 | + //// Shadow Declarations |
| 47 | + let shadow2 = NSShadow() |
| 48 | + shadow2.shadowColor = NSColor.black.withAlphaComponent(0.46) |
| 49 | + shadow2.shadowOffset = NSSize(width: 2, height: -8) |
| 50 | + shadow2.shadowBlurRadius = 5 |
| 51 | + let shadow3 = NSShadow() |
| 52 | + shadow3.shadowColor = knobLight.withAlphaComponent(0.41 * knobLight.alphaComponent) |
| 53 | + shadow3.shadowOffset = NSSize(width: 0, height: -10) |
| 54 | + shadow3.shadowBlurRadius = 5 |
| 55 | + let shadow4 = NSShadow() |
| 56 | + shadow4.shadowColor = shadowColor |
| 57 | + shadow4.shadowOffset = NSSize(width: 0, height: 0) |
| 58 | + shadow4.shadowBlurRadius = 12 |
| 59 | + |
| 60 | + //// Variable Declarations |
| 61 | + let knobAngle: CGFloat = -240 * knobValue |
| 62 | + |
| 63 | + //// Knob |
| 64 | + //// BlackBackground Drawing |
| 65 | + let blackBackgroundPath = NSBezierPath(ovalIn: NSRect(x: 0, y: 0, width: 120, height: 120)) |
| 66 | + black.setFill() |
| 67 | + blackBackgroundPath.fill() |
| 68 | + |
| 69 | + |
| 70 | + //// GradientKnob 2 Drawing |
| 71 | + let gradientKnob2Path = NSBezierPath(ovalIn: NSRect(x: 9, y: 9, width: 102, height: 102)) |
| 72 | + lowerKnobGradient2.draw(in: gradientKnob2Path, angle: 90) |
| 73 | + |
| 74 | + |
| 75 | + //// GradientKnob Drawing |
| 76 | + let gradientKnobPath = NSBezierPath(ovalIn: NSRect(x: 15, y: 15, width: 90, height: 90)) |
| 77 | + NSGraphicsContext.saveGraphicsState() |
| 78 | + context.setShadow(offset: NSSize(width: shadow2.shadowOffset.width * resizedShadowScale, height: shadow2.shadowOffset.height * resizedShadowScale), blur: shadow2.shadowBlurRadius * resizedShadowScale, color: shadow2.shadowColor!.cgColor) |
| 79 | + context.beginTransparencyLayer(auxiliaryInfo: nil) |
| 80 | + edge2.draw(in: gradientKnobPath, angle: 90) |
| 81 | + context.endTransparencyLayer() |
| 82 | + |
| 83 | + ////// GradientKnob Inner Shadow |
| 84 | + NSGraphicsContext.saveGraphicsState() |
| 85 | + NSRectClip(gradientKnobPath.bounds) |
| 86 | + context.setShadow(offset: NSSize.zero, blur: 0, color: nil) |
| 87 | + |
| 88 | + context.setAlpha(shadow3.shadowColor!.alphaComponent) |
| 89 | + context.beginTransparencyLayer(auxiliaryInfo: nil) |
| 90 | + let gradientKnobOpaqueShadow = NSShadow() |
| 91 | + gradientKnobOpaqueShadow.shadowColor = shadow3.shadowColor!.withAlphaComponent(1) |
| 92 | + gradientKnobOpaqueShadow.shadowOffset = NSSize(width: shadow3.shadowOffset.width * resizedShadowScale, height: shadow3.shadowOffset.height * resizedShadowScale) |
| 93 | + gradientKnobOpaqueShadow.shadowBlurRadius = shadow3.shadowBlurRadius * resizedShadowScale |
| 94 | + gradientKnobOpaqueShadow.set() |
| 95 | + |
| 96 | + context.setBlendMode(.sourceOut) |
| 97 | + context.beginTransparencyLayer(auxiliaryInfo: nil) |
| 98 | + |
| 99 | + gradientKnobOpaqueShadow.shadowColor!.setFill() |
| 100 | + gradientKnobPath.fill() |
| 101 | + |
| 102 | + context.endTransparencyLayer() |
| 103 | + context.endTransparencyLayer() |
| 104 | + NSGraphicsContext.restoreGraphicsState() |
| 105 | + |
| 106 | + NSGraphicsContext.restoreGraphicsState() |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + //// IndicatorGroup |
| 111 | + //// Indicator Drawing |
| 112 | + NSGraphicsContext.saveGraphicsState() |
| 113 | + context.translateBy(x: 60, y: 60) |
| 114 | + context.rotate(by: (knobAngle - 240) * CGFloat.pi/180) |
| 115 | + |
| 116 | + let indicatorPath = NSBezierPath(rect: NSRect(x: -3, y: 29, width: 6, height: 17)) |
| 117 | + NSGraphicsContext.saveGraphicsState() |
| 118 | + context.setShadow(offset: NSSize(width: shadow4.shadowOffset.width * resizedShadowScale, height: shadow4.shadowOffset.height * resizedShadowScale), blur: shadow4.shadowBlurRadius * resizedShadowScale, color: shadow4.shadowColor!.cgColor) |
| 119 | + orange2.setFill() |
| 120 | + indicatorPath.fill() |
| 121 | + NSGraphicsContext.restoreGraphicsState() |
| 122 | + |
| 123 | + |
| 124 | + NSGraphicsContext.restoreGraphicsState() |
| 125 | + |
| 126 | + NSGraphicsContext.restoreGraphicsState() |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + @objc(KnobStyleKitResizingBehavior) |
| 134 | + public enum ResizingBehavior: Int { |
| 135 | + case aspectFit /// The content is proportionally resized to fit into the target rectangle. |
| 136 | + case aspectFill /// The content is proportionally resized to completely fill the target rectangle. |
| 137 | + case stretch /// The content is stretched to match the entire target rectangle. |
| 138 | + case center /// The content is centered in the target rectangle, but it is NOT resized. |
| 139 | + |
| 140 | + public func apply(rect: NSRect, target: NSRect) -> NSRect { |
| 141 | + if rect == target || target == NSRect.zero { |
| 142 | + return rect |
| 143 | + } |
| 144 | + |
| 145 | + var scales = NSSize.zero |
| 146 | + scales.width = abs(target.width / rect.width) |
| 147 | + scales.height = abs(target.height / rect.height) |
| 148 | + |
| 149 | + switch self { |
| 150 | + case .aspectFit: |
| 151 | + scales.width = min(scales.width, scales.height) |
| 152 | + scales.height = scales.width |
| 153 | + case .aspectFill: |
| 154 | + scales.width = max(scales.width, scales.height) |
| 155 | + scales.height = scales.width |
| 156 | + case .stretch: |
| 157 | + break |
| 158 | + case .center: |
| 159 | + scales.width = 1 |
| 160 | + scales.height = 1 |
| 161 | + } |
| 162 | + |
| 163 | + var result = rect.standardized |
| 164 | + result.size.width *= scales.width |
| 165 | + result.size.height *= scales.height |
| 166 | + result.origin.x = target.minX + (target.width - result.width) / 2 |
| 167 | + result.origin.y = target.minY + (target.height - result.height) / 2 |
| 168 | + return result |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments