|
| 1 | +import SpriteKit |
| 2 | +import UIKit |
| 3 | + |
| 4 | +enum ButtonTextureFactory { |
| 5 | + static func makeCircleButtonTexture( |
| 6 | + symbolName: String, |
| 7 | + side: CGFloat, |
| 8 | + circleColor: UIColor = SopaTheme.circleButtonFill, |
| 9 | + iconColor: UIColor = SopaTheme.circleButtonIcon, |
| 10 | + symbolScale: CGFloat = 0.42, |
| 11 | + symbolWeight: UIImage.SymbolWeight = .semibold |
| 12 | + ) -> SKTexture { |
| 13 | + let textureSize = CGSize(width: side, height: side) |
| 14 | + let renderer = UIGraphicsImageRenderer(size: textureSize) |
| 15 | + let image = renderer.image { _ in |
| 16 | + circleColor.setFill() |
| 17 | + UIBezierPath(ovalIn: CGRect(origin: .zero, size: textureSize)).fill() |
| 18 | + |
| 19 | + let config = UIImage.SymbolConfiguration(pointSize: side * symbolScale, weight: symbolWeight) |
| 20 | + if let symbol = UIImage(systemName: symbolName, withConfiguration: config)? |
| 21 | + .withTintColor(iconColor, renderingMode: .alwaysOriginal) { |
| 22 | + let symbolRect = CGRect( |
| 23 | + x: (side - symbol.size.width) / 2.0, |
| 24 | + y: (side - symbol.size.height) / 2.0, |
| 25 | + width: symbol.size.width, |
| 26 | + height: symbol.size.height |
| 27 | + ) |
| 28 | + symbol.draw(in: symbolRect) |
| 29 | + } |
| 30 | + } |
| 31 | + return SKTexture(image: image) |
| 32 | + } |
| 33 | + |
| 34 | + static func makeMinimalBackTexture(side: CGFloat) -> SKTexture { |
| 35 | + let textureSize = CGSize(width: side, height: side) |
| 36 | + let renderer = UIGraphicsImageRenderer(size: textureSize) |
| 37 | + let image = renderer.image { _ in |
| 38 | + let config = UIImage.SymbolConfiguration(pointSize: side * 0.72, weight: .semibold) |
| 39 | + if let symbol = UIImage(systemName: "chevron.left", withConfiguration: config)? |
| 40 | + .withTintColor(UIColor(red: 160.0 / 255.0, green: 164.0 / 255.0, blue: 170.0 / 255.0, alpha: 0.95), renderingMode: .alwaysOriginal) { |
| 41 | + let symbolRect = CGRect( |
| 42 | + x: (side - symbol.size.width) / 2.0, |
| 43 | + y: (side - symbol.size.height) / 2.0, |
| 44 | + width: symbol.size.width, |
| 45 | + height: symbol.size.height |
| 46 | + ) |
| 47 | + symbol.draw(in: symbolRect) |
| 48 | + } |
| 49 | + } |
| 50 | + return SKTexture(image: image) |
| 51 | + } |
| 52 | + |
| 53 | + static func makePageArrowTexture(imageNamed: String, side: CGFloat) -> SKTexture { |
| 54 | + let texture = SKTexture(imageNamed: imageNamed) |
| 55 | + let imageSize = texture.size() |
| 56 | + let ratio = imageSize.width / max(1, imageSize.height) |
| 57 | + let targetSize = CGSize(width: side * ratio, height: side) |
| 58 | + let renderer = UIGraphicsImageRenderer(size: targetSize) |
| 59 | + let image = renderer.image { _ in |
| 60 | + UIImage(named: imageNamed)?.draw(in: CGRect(origin: .zero, size: targetSize)) |
| 61 | + } |
| 62 | + return SKTexture(image: image) |
| 63 | + } |
| 64 | + |
| 65 | + static func makeNeonTextTexture(title: String, size: CGSize) -> SKTexture { |
| 66 | + let renderer = UIGraphicsImageRenderer(size: size) |
| 67 | + let image = renderer.image { _ in |
| 68 | + let paragraphStyle = NSMutableParagraphStyle() |
| 69 | + paragraphStyle.alignment = .center |
| 70 | + |
| 71 | + let glowShadow = NSShadow() |
| 72 | + glowShadow.shadowColor = SopaTheme.neonGlowBlue |
| 73 | + glowShadow.shadowBlurRadius = size.height * 0.22 |
| 74 | + glowShadow.shadowOffset = .zero |
| 75 | + |
| 76 | + let font = UIFont(name: "HelveticaNeue-Light", size: size.height * 0.60) |
| 77 | + ?? UIFont.systemFont(ofSize: size.height * 0.60, weight: .light) |
| 78 | + let textRect = CGRect(x: 0, y: size.height * 0.22, width: size.width, height: size.height * 0.60) |
| 79 | + |
| 80 | + let glowAttributes: [NSAttributedString.Key: Any] = [ |
| 81 | + .font: font, |
| 82 | + .foregroundColor: SopaTheme.neonBlue, |
| 83 | + .strokeColor: SopaTheme.neonBlue, |
| 84 | + .strokeWidth: -1.2, |
| 85 | + .paragraphStyle: paragraphStyle, |
| 86 | + .shadow: glowShadow |
| 87 | + ] |
| 88 | + title.draw(in: textRect, withAttributes: glowAttributes) |
| 89 | + |
| 90 | + let coreAttributes: [NSAttributedString.Key: Any] = [ |
| 91 | + .font: font, |
| 92 | + .foregroundColor: SopaTheme.neonCoreBlue, |
| 93 | + .paragraphStyle: paragraphStyle |
| 94 | + ] |
| 95 | + title.draw(in: textRect, withAttributes: coreAttributes) |
| 96 | + } |
| 97 | + return SKTexture(image: image) |
| 98 | + } |
| 99 | + |
| 100 | + static func makeSocialButtonTexture(symbolName: String, size: CGSize, fillBackground: Bool) -> SKTexture { |
| 101 | + let renderer = UIGraphicsImageRenderer(size: size) |
| 102 | + let image = renderer.image { _ in |
| 103 | + let blueColor = UIColor(red: 99.0 / 255.0, green: 177.0 / 255.0, blue: 230.0 / 255.0, alpha: 1.0) |
| 104 | + if fillBackground { |
| 105 | + let backgroundPath = UIBezierPath(roundedRect: CGRect(origin: .zero, size: size), cornerRadius: size.width * 0.18) |
| 106 | + blueColor.setFill() |
| 107 | + backgroundPath.fill() |
| 108 | + } |
| 109 | + |
| 110 | + let pointSize = fillBackground ? size.width * 0.50 : size.width * 0.75 |
| 111 | + let config = UIImage.SymbolConfiguration(pointSize: pointSize, weight: .semibold) |
| 112 | + guard let baseSymbol = UIImage(systemName: symbolName, withConfiguration: config) else { |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + let iconColor = fillBackground ? UIColor.white : blueColor |
| 117 | + let symbol = baseSymbol.withTintColor(iconColor, renderingMode: .alwaysOriginal) |
| 118 | + let symbolRect = CGRect( |
| 119 | + x: (size.width - symbol.size.width) / 2.0, |
| 120 | + y: (size.height - symbol.size.height) / 2.0, |
| 121 | + width: symbol.size.width, |
| 122 | + height: symbol.size.height |
| 123 | + ) |
| 124 | + symbol.draw(in: symbolRect) |
| 125 | + } |
| 126 | + return SKTexture(image: image) |
| 127 | + } |
| 128 | + |
| 129 | + static func makeTextButtonTexture( |
| 130 | + title: String, |
| 131 | + size: CGSize, |
| 132 | + fontName: String, |
| 133 | + textColor: UIColor, |
| 134 | + weight: UIFont.Weight = .semibold |
| 135 | + ) -> SKTexture { |
| 136 | + let renderer = UIGraphicsImageRenderer(size: size) |
| 137 | + let image = renderer.image { _ in |
| 138 | + let frame = CGRect(origin: .zero, size: size).insetBy(dx: size.width * 0.01, dy: size.height * 0.06) |
| 139 | + let backgroundPath = UIBezierPath(roundedRect: frame, cornerRadius: size.height * 0.23) |
| 140 | + UIColor(white: 1.0, alpha: 0.10).setFill() |
| 141 | + backgroundPath.fill() |
| 142 | + UIColor(white: 1.0, alpha: 0.22).setStroke() |
| 143 | + backgroundPath.lineWidth = size.height * 0.08 |
| 144 | + backgroundPath.stroke() |
| 145 | + |
| 146 | + let paragraphStyle = NSMutableParagraphStyle() |
| 147 | + paragraphStyle.alignment = .center |
| 148 | + let buttonFontSize = LabelSizing.fittedTextFontSize( |
| 149 | + text: title, |
| 150 | + fontName: fontName, |
| 151 | + preferredSize: size.height * 0.38, |
| 152 | + weight: weight, |
| 153 | + maxWidth: size.width * 0.90 |
| 154 | + ) |
| 155 | + let attributes: [NSAttributedString.Key: Any] = [ |
| 156 | + .font: UIFont(name: fontName, size: buttonFontSize) ?? UIFont.systemFont(ofSize: buttonFontSize, weight: weight), |
| 157 | + .foregroundColor: textColor, |
| 158 | + .paragraphStyle: paragraphStyle |
| 159 | + ] |
| 160 | + let textRect = CGRect(x: 0.0, y: size.height * 0.30, width: size.width, height: size.height * 0.5) |
| 161 | + title.draw(in: textRect, withAttributes: attributes) |
| 162 | + } |
| 163 | + return SKTexture(image: image) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func makeCircleButtonTexture(symbolName: String, side: CGFloat) -> SKTexture { |
| 168 | + ButtonTextureFactory.makeCircleButtonTexture(symbolName: symbolName, side: side) |
| 169 | +} |
0 commit comments