Repository Public
1//2// Auto-Ka iOS v23// File: AutoKa/Design/Brand/Portiere.swift4// Description: le symbole « Portière » — géométrie du système de marque (grille 64 × 64), formes SwiftUI,5// projection du battant qui pivote vers soi sur ses charnières avant, et son dessin (tôle, vitre,6// poignée, lignes de caisse, chant, ombre propre).7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10//1112import SwiftUI1314/// Géométrie du symbole. Source : brand/README.md, section 2. Le battant occupe une boîte de 41 × 44,5 unités ;15/// les coordonnées ci-dessous sont relatives à son coin haut gauche (symbole : décalage de 12 ; 7).16enum PortiereGeometry {17 static let leafSize = CGSize(width: 41, height: 44.5)18 /// rapport largeur / hauteur du battant (0,921)19 static let ratio: CGFloat = 41 / 44.520 /// battant : bas avant, pied du montant, tête du montant, haut arrière, bas arrière21 static let leaf: [CGPoint] = [.init(x: 0, y: 44.5), .init(x: 0, y: 21.2), .init(x: 11.6, y: 0), .init(x: 41, y: 0), .init(x: 41, y: 44.5)]22 static let leafRadii: [CGFloat] = [2.5, 2, 2.5, 3, 2.5]23 /// vitre, en réserve ; son bord avant est parallèle au montant24 static let glass: [CGPoint] = [.init(x: 8.4, y: 19.75), .init(x: 15.9, y: 5.75), .init(x: 36, y: 5.75), .init(x: 36, y: 19.75)]25 static let glassRadii: [CGFloat] = [1.2, 1.2, 1.5, 1.2]26 /// poignée : capsule27 static let handle = CGRect(x: 26.5, y: 24.5, width: 9.5, height: 3)28 /// lignes de caisse du battant animé29 static let bodyLines: [CGFloat] = [23.3, 37.4]30 /// variante micro (16 à 24 pt), dans la grille 64 : pas de poignée, seuil plus épais31 static let microLeaf: [CGPoint] = [.init(x: 10.5, y: 50.5), .init(x: 10.5, y: 27.5), .init(x: 22.5, y: 5.5), .init(x: 54.5, y: 5.5), .init(x: 54.5, y: 50.5)]32 static let microGlass: [CGPoint] = [.init(x: 21, y: 27.5), .init(x: 28.4, y: 13.5), .init(x: 47.5, y: 13.5), .init(x: 47.5, y: 27.5)]33 /// angle d'ouverture complète du geste34 static let openAngle: Double = 763536 /// Polygone aux coins arrondis, échantillonné : `samples` points par sommet (nombre constant, donc deux37 /// polygones de même nombre de sommets s'interpolent point à point).38 static func rounded(_ v: [CGPoint], _ radii: [CGFloat], samples: Int = 7) -> [CGPoint] {39 var out: [CGPoint] = []40 out.reserveCapacity(v.count * samples)41 let n = v.count42 for i in 0..<n {43 let p = v[i], a = v[(i + n - 1) % n], b = v[(i + 1) % n]44 let da = CGPoint(x: a.x - p.x, y: a.y - p.y), db = CGPoint(x: b.x - p.x, y: b.y - p.y)45 let la = max(hypot(da.x, da.y), 1e-6), lb = max(hypot(db.x, db.y), 1e-6)46 let ua = CGPoint(x: da.x / la, y: da.y / la), ub = CGPoint(x: db.x / lb, y: db.y / lb)47 let cosA = max(-1, min(1, ua.x * ub.x + ua.y * ub.y))48 let half = acos(cosA) / 249 // distance du sommet aux points de tangence ; bornée à la moitié des côtés50 let t = min(radii[i] / max(tan(half), 1e-4), la / 2, lb / 2)51 let t1 = CGPoint(x: p.x + ua.x * t, y: p.y + ua.y * t), t2 = CGPoint(x: p.x + ub.x * t, y: p.y + ub.y * t)52 for s in 0..<samples {53 let u = CGFloat(s) / CGFloat(samples - 1), w = 1 - u54 out.append(CGPoint(x: w * w * t1.x + 2 * w * u * p.x + u * u * t2.x, y: w * w * t1.y + 2 * w * u * p.y + u * u * t2.y))55 }56 }57 return out58 }5960 static func path(_ pts: [CGPoint]) -> Path {61 var p = Path()62 guard let f = pts.first else { return p }63 p.move(to: f)64 for q in pts.dropFirst() { p.addLine(to: q) }65 p.closeSubpath()66 return p67 }6869 /// Boîte du battant (toute la hauteur, largeur 0,921 H) centrée dans le cadre d'une photo.70 static func leafBox(in rect: CGRect) -> CGRect {71 let w = min(rect.height * ratio, rect.width)72 return CGRect(x: rect.midX - w / 2, y: rect.minY, width: w, height: rect.height)73 }74}7576/// Embrasure de la photo : interpole entre le rectangle arrondi de la photo (k = 0) et la forme de la portière77/// (k = 1). Animable.78struct PortiereShape: Shape {79 var k: CGFloat80 var cornerRadius: CGFloat = Theme.radiusPhoto8182 var animatableData: CGFloat {83 get { k }84 set { k = newValue }85 }8687 static func points(in rect: CGRect, k: CGFloat, cornerRadius: CGFloat) -> [CGPoint] {88 let box = PortiereGeometry.leafBox(in: rect)89 let unit = box.height / PortiereGeometry.leafSize.height90 let door = PortiereGeometry.leaf.map { CGPoint(x: box.minX + $0.x * unit, y: box.minY + $0.y * unit) }91 // mêmes cinq sommets pour le rectangle : le pied du montant est sur son bord, la tête dans son coin92 let frame = [CGPoint(x: rect.minX, y: rect.maxY), CGPoint(x: rect.minX, y: rect.minY + rect.height * 0.476),93 CGPoint(x: rect.minX, y: rect.minY), CGPoint(x: rect.maxX, y: rect.minY), CGPoint(x: rect.maxX, y: rect.maxY)]94 let frameRadii: [CGFloat] = [cornerRadius, 0, cornerRadius, cornerRadius, cornerRadius]95 let kk = max(0, min(1, k))96 let v = zip(frame, door).map { CGPoint(x: $0.x + ($1.x - $0.x) * kk, y: $0.y + ($1.y - $0.y) * kk) }97 let r = zip(frameRadii, PortiereGeometry.leafRadii).map { $0 + ($1 * unit - $0) * kk }98 return PortiereGeometry.rounded(v, r)99 }100101 func path(in rect: CGRect) -> Path {102 PortiereGeometry.path(PortiereShape.points(in: rect, k: k, cornerRadius: cornerRadius))103 }104}105106/// Le battant du symbole, à plat : tôle pleine, vitre et poignée en réserve.107struct PortiereLeafShape: Shape {108 var micro = false109110 func path(in rect: CGRect) -> Path {111 // dessiné dans la grille 64 × 64 du symbole112 let s = min(rect.width, rect.height) / 64113 let o = CGPoint(x: rect.midX - 32 * s, y: rect.midY - 32 * s)114 func map(_ pts: [CGPoint], offset: CGPoint) -> [CGPoint] { pts.map { CGPoint(x: o.x + ($0.x + offset.x) * s, y: o.y + ($0.y + offset.y) * s) } }115 var p = Path()116 if micro {117 p.addPath(PortiereGeometry.path(PortiereGeometry.rounded(map(PortiereGeometry.microLeaf, offset: .zero), [2.5, 2, 2.5, 3, 2.5].map { $0 * s })))118 p.addPath(PortiereGeometry.path(PortiereGeometry.rounded(map(PortiereGeometry.microGlass, offset: .zero), [1.2, 1.2, 1.5, 1.2].map { $0 * s })))119 } else {120 let off = CGPoint(x: 12, y: 7)121 p.addPath(PortiereGeometry.path(PortiereGeometry.rounded(map(PortiereGeometry.leaf, offset: off), PortiereGeometry.leafRadii.map { $0 * s })))122 p.addPath(PortiereGeometry.path(PortiereGeometry.rounded(map(PortiereGeometry.glass, offset: off), PortiereGeometry.glassRadii.map { $0 * s })))123 let h = PortiereGeometry.handle124 p.addRoundedRect(in: CGRect(x: o.x + (h.minX + 12) * s, y: o.y + (h.minY + 7) * s, width: h.width * s, height: h.height * s),125 cornerSize: CGSize(width: 1.5 * s, height: 1.5 * s))126 }127 return p128 }129}130131/// Le seuil (bas de caisse) du symbole.132struct PortiereSillShape: Shape {133 var micro = false134 func path(in rect: CGRect) -> Path {135 let s = min(rect.width, rect.height) / 64136 let o = CGPoint(x: rect.midX - 32 * s, y: rect.midY - 32 * s)137 let (x0, x1, y, w): (CGFloat, CGFloat, CGFloat, CGFloat) = micro ? (7, 58, 57, 5) : (6, 58, 56, 3.5)138 var p = Path()139 p.move(to: CGPoint(x: o.x + x0 * s, y: o.y + y * s))140 p.addLine(to: CGPoint(x: o.x + x1 * s, y: o.y + y * s))141 return p.strokedPath(StrokeStyle(lineWidth: w * s, lineCap: .round))142 }143}144145/// Couleurs du battant, résolues pour le thème courant.146struct DoorPalette {147 var door: Color = Theme.door148 var glass: Color = Theme.doorGlass149 var edge: Color = Theme.doorEdge150 var handle: Color = Theme.doorHandle151 var sill: Color = Theme.sill152 var shade: Color = Theme.doorShade153}154155/// Dessin du battant en perspective dans un `GraphicsContext`. Fonction pure de l'angle : le geste d'ouverture,156/// l'indicateur de chargement et les états vides s'en servent.157enum DoorPainter {158 /// Projection d'un point du battant.159 /// - Parameters:160 /// - u, v: position sur le battant, en unités de la boîte (0…41 ; 0…44,5), charnières en u = 0161 /// - inset: profondeur le long de la normale intérieure (chant), en points162 /// - box: boîte de l'embrasure à l'écran ; l'œil est face à son centre, à 3,2 largeurs163 /// - angle: ouverture en radians ; le bord arrière vient vers l'œil164 static func project(u: CGFloat, v: CGFloat, inset: CGFloat = 0, box: CGRect, angle: CGFloat) -> CGPoint {165 let unit = box.height / PortiereGeometry.leafSize.height166 let d = 3.2 * box.width167 let x3 = u * unit * cos(angle) + inset * sin(angle)168 let z = u * unit * sin(angle) - inset * cos(angle)169 let s = d / max(d - z, d * 0.2)170 let wx = box.minX + x3, wy = box.minY + v * unit171 return CGPoint(x: box.midX + (wx - box.midX) * s, y: box.midY + (wy - box.midY) * s)172 }173174 private static func projected(_ pts: [CGPoint], box: CGRect, angle: CGFloat) -> Path {175 PortiereGeometry.path(pts.map { project(u: $0.x, v: $0.y, box: box, angle: angle) })176 }177178 /// - Parameters:179 /// - detailed: lignes de caisse, cuvette de poignée et liseré clair (battant du geste) ; sinon l'aplat du symbole180 /// - opacity: opacité de tout le battant (apparition, retrait)181 static func draw(_ ctx: inout GraphicsContext, box: CGRect, degrees: Double, palette: DoorPalette = DoorPalette(),182 detailed: Bool = true, opacity: Double = 1) {183 guard opacity > 0.001 else { return }184 let angle = CGFloat(degrees * .pi / 180)185 let unit = box.height / PortiereGeometry.leafSize.height186 var g = ctx187 g.opacity = opacity188189 let leafPts = PortiereGeometry.rounded(PortiereGeometry.leaf, PortiereGeometry.leafRadii)190 let glassPts = PortiereGeometry.rounded(PortiereGeometry.glass, PortiereGeometry.glassRadii)191 let leaf = projected(leafPts, box: box, angle: angle)192 let glass = projected(glassPts, box: box, angle: angle)193194 // chant : bande perpendiculaire au bord arrière, visible dès que le battant s'ouvre195 if degrees > 0.5 {196 let depth = 0.05 * box.width197 var chant = Path()198 let top: CGFloat = 1.2, bottom: CGFloat = PortiereGeometry.leafSize.height - 1.0199 chant.move(to: project(u: 41, v: top, box: box, angle: angle))200 chant.addLine(to: project(u: 41, v: top, inset: depth, box: box, angle: angle))201 chant.addLine(to: project(u: 41, v: bottom, inset: depth, box: box, angle: angle))202 chant.addLine(to: project(u: 41, v: bottom, box: box, angle: angle))203 chant.closeSubpath()204 g.fill(chant, with: .color(palette.edge))205 g.stroke(chant, with: .color(palette.shade.opacity(0.35)), lineWidth: 0.5)206 }207208 // tôle, vitre en réserve209 var sheet = leaf210 sheet.addPath(glass)211 g.fill(sheet, with: .color(palette.door), style: FillStyle(eoFill: true))212 // vitre : teinte à plat, on voit le véhicule au travers ; aucun flou213 g.fill(glass, with: .color(palette.glass))214215 if detailed {216 // lignes de caisse217 for y in PortiereGeometry.bodyLines {218 var l = Path()219 l.move(to: project(u: 0.9, v: y, box: box, angle: angle))220 l.addLine(to: project(u: 40.1, v: y, box: box, angle: angle))221 g.stroke(l, with: .color(palette.edge), lineWidth: max(1, 0.3 * unit))222 }223 // poignée : capsule claire dans sa cuvette224 let h = PortiereGeometry.handle225 let cup = PortiereGeometry.rounded(226 [CGPoint(x: h.minX - 1.3, y: h.maxY + 1.3), CGPoint(x: h.minX - 1.3, y: h.minY - 1.3),227 CGPoint(x: h.maxX + 1.3, y: h.minY - 1.3), CGPoint(x: h.maxX + 1.3, y: h.maxY + 1.3)], [2.8, 2.8, 2.8, 2.8])228 g.fill(projected(cup, box: box, angle: angle), with: .color(palette.edge))229 let grip = PortiereGeometry.rounded(230 [CGPoint(x: h.minX, y: h.maxY), CGPoint(x: h.minX, y: h.minY), CGPoint(x: h.maxX, y: h.minY), CGPoint(x: h.maxX, y: h.maxY)],231 [1.5, 1.5, 1.5, 1.5])232 g.fill(projected(grip, box: box, angle: angle), with: .color(palette.handle))233 // liseré clair : le battant reste net sur n'importe quelle photo234 g.stroke(leaf, with: .color(palette.handle), lineWidth: max(1, 0.34 * unit))235 g.stroke(glass, with: .color(palette.handle), lineWidth: max(1, 0.34 * unit))236 } else {237 let h = PortiereGeometry.handle238 let grip = PortiereGeometry.rounded(239 [CGPoint(x: h.minX, y: h.maxY), CGPoint(x: h.minX, y: h.minY), CGPoint(x: h.maxX, y: h.minY), CGPoint(x: h.maxX, y: h.maxY)],240 [1.5, 1.5, 1.5, 1.5])241 g.fill(projected(grip, box: box, angle: angle), with: .color(palette.handle))242 }243244 // ombre propre : le battant s'assombrit en tournant245 let shade = 0.36 * sin(Double(angle))246 if shade > 0.004 { g.fill(leaf, with: .color(palette.shade.opacity(shade))) }247 }248249 /// Seuil sous l'embrasure : barre arrondie, 1,28 × la largeur du battant, tracée du centre.250 static func drawSill(_ ctx: inout GraphicsContext, box: CGRect, progress: Double, color: Color, opacity: Double = 1) {251 guard progress > 0.001, opacity > 0.001 else { return }252 let w = box.width * 1.28 * progress253 let h = max(5, box.height * 0.017)254 let r = CGRect(x: box.midX - w / 2, y: box.maxY + box.height * 0.03, width: w, height: h)255 ctx.fill(Path(roundedRect: r, cornerRadius: h / 2), with: .color(color.opacity(opacity)))256 }257}258