SPB Git

Repository Public License

LunarMark.swift 8.1 KB 162 lines swift

1import SwiftUI23/// Geometry of the ORIX Navigator mark on a 100-unit grid: a core, an orbital ring around it, and a navigation4/// needle across the core. `scripts/make-icon.swift` draws the app icon from the same numbers.5public enum LunarMarkGeometry {6    public static let gridSize: CGFloat = 1007    public static let center = CGPoint(x: 50, y: 50)8    public static let coreRadius: CGFloat = 199    /// Semi-axes of the orbital ring before tilting.10    public static let ringSemiMajor: CGFloat = 4511    public static let ringSemiMinor: CGFloat = 15.512    /// Tilt of the ring (radians, y-down): the ring runs from the upper left to the lower right.13    public static let ringTilt: CGFloat = 24 * .pi / 18014    /// The needle points to the upper right, across the ring.15    public static let needleAngle: CGFloat = -52 * .pi / 18016    public static let needleLength: CGFloat = 15.517    public static let needleHalfWidth: CGFloat = 4.818    /// Position of the satellite on the ring (ellipse parameter, radians).19    public static let satelliteParameter: CGFloat = -0.6220    public static let satelliteRadius: CGFloat = 3.42122    /// A point of the tilted ring for ellipse parameter `t`.23    public static func ringPoint(_ t: CGFloat) -> CGPoint {24        let x = ringSemiMajor * cos(t), y = ringSemiMinor * sin(t)25        return CGPoint(x: center.x + x * cos(ringTilt) - y * sin(ringTilt),26                       y: center.y + x * sin(ringTilt) + y * cos(ringTilt))27    }2829    /// The ring between two ellipse parameters. `0…π` is the half passing in front of the core.30    public static func ringArc(from start: CGFloat, to end: CGFloat) -> Path {31        var path = Path()32        let steps = 7233        for step in 0...steps {34            let point = ringPoint(start + (end - start) * CGFloat(step) / CGFloat(steps))35            if step == 0 { path.move(to: point) } else { path.addLine(to: point) }36        }37        return path38    }3940    /// One half of the needle: `forward == true` is the pointing half.41    public static func needleHalf(forward: Bool) -> Path {42        let direction = CGPoint(x: cos(needleAngle), y: sin(needleAngle))43        let normal = CGPoint(x: -direction.y, y: direction.x)44        let sign: CGFloat = forward ? 1 : -145        var path = Path()46        path.move(to: CGPoint(x: center.x + direction.x * needleLength * sign,47                              y: center.y + direction.y * needleLength * sign))48        path.addLine(to: CGPoint(x: center.x + normal.x * needleHalfWidth, y: center.y + normal.y * needleHalfWidth))49        path.addLine(to: CGPoint(x: center.x - normal.x * needleHalfWidth, y: center.y - normal.y * needleHalfWidth))50        path.closeSubpath()51        return path52    }5354    public static var corePath: Path {55        Path(ellipseIn: CGRect(x: center.x - coreRadius, y: center.y - coreRadius, width: coreRadius * 2,56                               height: coreRadius * 2))57    }58}5960/// The mark as a single-colour `Shape`: ring, core outline and needle. Use it where a template image is needed.61public struct LunarMarkShape: Shape {62    public init() {}6364    public func path(in rect: CGRect) -> Path {65        let side = min(rect.width, rect.height)66        let scale = side / LunarMarkGeometry.gridSize67        let transform = CGAffineTransform(translationX: rect.midX - side / 2, y: rect.midY - side / 2)68            .scaledBy(x: scale, y: scale)69        // The ring is drawn only where it clears the core, so the mark stays legible at 16 pt: two arcs70        // embracing a solid core whose needle is knocked out.71        let clearance = LunarMarkGeometry.coreRadius + 572        var ring = Path()73        var drawing = false74        let steps = 18075        for step in 0...steps {76            let point = LunarMarkGeometry.ringPoint(2 * .pi * CGFloat(step) / CGFloat(steps))77            let distance = hypot(point.x - LunarMarkGeometry.center.x, point.y - LunarMarkGeometry.center.y)78            if distance >= clearance {79                if drawing { ring.addLine(to: point) } else { ring.move(to: point) }80                drawing = true81            } else {82                drawing = false83            }84        }85        var path = ring.strokedPath(StrokeStyle(lineWidth: 5, lineCap: .round, lineJoin: .round))86        var needle = LunarMarkGeometry.needleHalf(forward: true)87        needle.addPath(LunarMarkGeometry.needleHalf(forward: false))88        path.addPath(LunarMarkGeometry.corePath.subtracting(needle))89        let satellite = LunarMarkGeometry.ringPoint(LunarMarkGeometry.satelliteParameter)90        let r = LunarMarkGeometry.satelliteRadius + 191        path.addPath(Path(ellipseIn: CGRect(x: satellite.x - r, y: satellite.y - r, width: r * 2, height: r * 2)))92        return path.applying(transform)93    }94}9596/// The ORIX Navigator mark in full colour, drawn from the current theme: a luminous core, a thin orbital ring that97/// passes behind and in front of it, a satellite, and the navigation needle.98public struct LunarMarkView: View {99    @Environment(\.lunarTheme) private var theme100    private let size: CGFloat101    private let showsGlow: Bool102103    public init(size: CGFloat = 96, showsGlow: Bool = true) {104        self.size = size105        self.showsGlow = showsGlow106    }107108    public var body: some View {109        Canvas { context, canvasSize in110            let scale = min(canvasSize.width, canvasSize.height) / LunarMarkGeometry.gridSize111            context.scaleBy(x: scale, y: scale)112            let g = LunarMarkGeometry.self113            let accent = theme.accent.color, second = theme.accentSecondary.color114            let third = theme.color(for: .auroraMint).color115116            if showsGlow {117                let halo = CGRect(x: 2, y: 2, width: 96, height: 96)118                context.fill(Path(ellipseIn: halo), with: .radialGradient(119                    Gradient(colors: [theme.glow.color, theme.glow.withAlpha(0).color]),120                    center: g.center, startRadius: 12, endRadius: 48))121            }122123            let ringShading = GraphicsContext.Shading.linearGradient(124                Gradient(colors: [second, accent, third]),125                startPoint: g.ringPoint(.pi), endPoint: g.ringPoint(0))126            let ringStyle = StrokeStyle(lineWidth: 1.9, lineCap: .round)127            // Far half of the ring, dimmed, drawn before the core so the core hides it.128            var far = context129            far.opacity = 0.55130            far.stroke(g.ringArc(from: .pi, to: 2 * .pi), with: ringShading, style: ringStyle)131132            let core = g.corePath133            let coreTop = theme.isDark ? theme.elevatedSurface.mixed(with: theme.accent, 0.42)134                                       : theme.accent.mixed(with: .white, 0.18)135            let coreBottom = theme.isDark ? theme.windowBackground.mixed(with: theme.accentSecondary, 0.30)136                                          : theme.accent.mixed(with: theme.accentSecondary, 0.55)137            context.fill(core, with: .linearGradient(138                Gradient(colors: [coreTop.color, coreBottom.color]),139                startPoint: CGPoint(x: 38, y: 30), endPoint: CGPoint(x: 62, y: 70)))140            context.stroke(core, with: .linearGradient(141                Gradient(colors: [Color.white.opacity(0.75), accent.opacity(0.25)]),142                startPoint: CGPoint(x: 40, y: 31), endPoint: CGPoint(x: 60, y: 69)), lineWidth: 0.9)143144            let needleLight = theme.isDark ? theme.textPrimary : LunarRGBA.white145            context.fill(g.needleHalf(forward: true), with: .color(needleLight.color))146            context.fill(g.needleHalf(forward: false), with: .color(needleLight.withAlpha(0.38).color))147148            // Near half of the ring, over the core.149            context.stroke(g.ringArc(from: 0, to: .pi), with: ringShading, style: ringStyle)150151            let satellite = g.ringPoint(g.satelliteParameter)152            let r = g.satelliteRadius153            context.fill(Path(ellipseIn: CGRect(x: satellite.x - r, y: satellite.y - r, width: r * 2, height: r * 2)),154                         with: .color(third))155        }156        .frame(width: size, height: size)157        .accessibilityElement()158        .accessibilityLabel("ORIX Navigator")159        .accessibilityAddTraits(.isImage)160    }161}162
Files main
Menu

Theme