DayByDay/Mascot/MascotView.swift
200 lines · 7825 bytes
10 symbols in this file
1//
2// MascotView.swift
3// DayByDay
4//
5
6import SwiftUI
7
8enum MascotStyle {
9 /// Shows on white and on black.
10 static let body = Color(red: 0.55, green: 0.36, blue: 0.85)
11 static let belly = Color(red: 0.93, green: 0.89, blue: 1.0)
12 static let ink = Color(red: 0.12, green: 0.08, blue: 0.25)
13 static let beak = Color(red: 1.0, green: 0.88, blue: 0.45)
14 static let eyeWhite = Color.white
15}
16
17/// The owl. Moves gently unless Reduce Motion is on, then holds the mood's
18/// still pose.
19struct MascotView: View {
20 let mood: MascotMood
21
22 @Environment(\.accessibilityReduceMotion) private var reduceMotion
23 @State private var moodStart = Date.now
24
25 var body: some View {
26 Group {
27 if reduceMotion {
28 OwlCanvas(mood: mood, pose: MascotMotion.still(mood))
29 } else {
30 TimelineView(.animation(minimumInterval: 1 / 30)) { timeline in
31 OwlCanvas(mood: mood, pose: MascotMotion.pose(mood, at: timeline.date.timeIntervalSince(moodStart)))
32 }
33 }
34 }
35 .aspectRatio(1, contentMode: .fit)
36 .onChange(of: mood) { moodStart = .now }
37 .accessibilityHidden(true)
38 }
39}
40
41/// The owl in one pose.
42struct OwlCanvas: View {
43 let mood: MascotMood
44 let pose: MascotPose
45
46 var body: some View {
47 Canvas { context, size in
48 // 110 wide, so the raised wings stay inside the frame.
49 let scale = min(size.width, size.height) / 110
50 context.translateBy(x: (size.width - 100 * scale) / 2, y: (size.height - 100 * scale) / 2)
51 context.scaleBy(x: scale, y: scale)
52 OwlDrawing(mood: mood, pose: pose).draw(in: &context)
53 }
54 }
55}
56
57/// The owl in a 100×100 space.
58private struct OwlDrawing {
59 let mood: MascotMood
60 let pose: MascotPose
61
62 private let body = GraphicsContext.Shading.color(MascotStyle.body)
63 private let ink = GraphicsContext.Shading.color(MascotStyle.ink)
64 private let beak = GraphicsContext.Shading.color(MascotStyle.beak)
65 private let white = GraphicsContext.Shading.color(MascotStyle.eyeWhite)
66
67 func draw(in context: inout GraphicsContext) {
68 context.translateBy(x: 0, y: (pose.bob + pose.hop) * 100)
69 // Lean from the feet.
70 context.translateBy(x: 50, y: 89)
71 context.rotate(by: .degrees(pose.tilt))
72 context.translateBy(x: -50, y: -89)
73
74 wing(&context, left: true, lift: mood == .cheering ? pose.wing : 0)
75 wing(&context, left: false, lift: pose.wing)
76
77 context.fill(Path(ellipseIn: CGRect(x: 20, y: 23, width: 60, height: 66)), with: body)
78 context.fill(tuft(left: true), with: body)
79 context.fill(tuft(left: false), with: body)
80
81 context.fill(Path(ellipseIn: CGRect(x: 31, y: 64, width: 38, height: 24)), with: .color(MascotStyle.belly))
82 for (x, y) in [(43.0, 70.0), (57.0, 70.0), (50.0, 77.0)] {
83 var chevron = Path()
84 chevron.move(to: CGPoint(x: x - 3, y: y))
85 chevron.addLine(to: CGPoint(x: x, y: y + 2.5))
86 chevron.addLine(to: CGPoint(x: x + 3, y: y))
87 context.stroke(chevron, with: ink, style: StrokeStyle(lineWidth: 1.6, lineCap: .round, lineJoin: .round))
88 }
89 for x in [38.0, 62.0] {
90 for dx in [-4.0, 0, 4] {
91 context.fill(Path(ellipseIn: CGRect(x: x + dx - 2.2, y: 85, width: 4.4, height: 6)), with: beak)
92 }
93 }
94
95 eye(&context, x: 37)
96 eye(&context, x: 63)
97 brows(&context)
98 beakShape(&context)
99
100 if mood == .puzzled {
101 let mark = context.resolve(Text("?").font(.custom(HeadingFont.extraBold, fixedSize: 24))
102 .foregroundStyle(MascotStyle.body))
103 context.draw(mark, at: CGPoint(x: 89, y: 22))
104 }
105 }
106
107 private func tuft(left: Bool) -> Path {
108 let f: (Double) -> Double = { left ? $0 : 100 - $0 }
109 var p = Path()
110 p.move(to: CGPoint(x: f(25), y: 42))
111 p.addQuadCurve(to: CGPoint(x: f(27), y: 13), control: CGPoint(x: f(20), y: 22))
112 p.addQuadCurve(to: CGPoint(x: f(44), y: 27), control: CGPoint(x: f(36), y: 18))
113 p.closeSubpath()
114 return p
115 }
116
117 /// Hangs from the shoulder; `lift` raises it outward.
118 private func wing(_ context: inout GraphicsContext, left: Bool, lift: Double) {
119 var c = context
120 c.translateBy(x: left ? 24 : 76, y: 52)
121 c.rotate(by: .degrees(left ? 12 + lift : -12 - lift))
122 let shape = Path(ellipseIn: CGRect(x: -7, y: -2, width: 14, height: 32))
123 c.fill(shape, with: body)
124 c.stroke(shape, with: ink, lineWidth: 1.6)
125 }
126
127 private func eye(_ context: inout GraphicsContext, x: Double) {
128 let center = CGPoint(x: x, y: 45)
129 if mood == .cheering {
130 context.fill(Path(ellipseIn: CGRect(x: x - 12, y: 33, width: 24, height: 24)), with: white)
131 var arc = Path()
132 arc.move(to: CGPoint(x: x - 7, y: 48))
133 arc.addQuadCurve(to: CGPoint(x: x + 7, y: 48), control: CGPoint(x: x, y: 36))
134 context.stroke(arc, with: ink, style: StrokeStyle(lineWidth: 3.6, lineCap: .round))
135 return
136 }
137 if pose.eyeOpen < 0.5 {
138 var lid = Path()
139 lid.move(to: CGPoint(x: x - 9, y: 46))
140 lid.addQuadCurve(to: CGPoint(x: x + 9, y: 46), control: CGPoint(x: x, y: 50))
141 context.stroke(lid, with: white, style: StrokeStyle(lineWidth: 3.6, lineCap: .round))
142 return
143 }
144 var c = context
145 c.translateBy(x: center.x, y: center.y)
146 c.scaleBy(x: 1, y: pose.eyeOpen)
147 c.fill(Path(ellipseIn: CGRect(x: -12, y: -12, width: 24, height: 24)), with: white)
148 // Pupils look a little toward the middle.
149 let dx: Double = x < 50 ? 1.5 : -1.5
150 c.fill(Path(ellipseIn: CGRect(x: dx - 6.5, y: -5.5, width: 13, height: 13)), with: ink)
151 c.fill(Path(ellipseIn: CGRect(x: dx - 3.5, y: -3.5, width: 4, height: 4)), with: white)
152 }
153
154 private func brows(_ context: inout GraphicsContext) {
155 let lines: [(CGPoint, CGPoint, CGPoint)]
156 switch mood {
157 case .asking:
158 lines = [(CGPoint(x: 28, y: 30), CGPoint(x: 37, y: 25), CGPoint(x: 45, y: 29)),
159 (CGPoint(x: 55, y: 29), CGPoint(x: 63, y: 25), CGPoint(x: 72, y: 30))]
160 case .puzzled:
161 lines = [(CGPoint(x: 28, y: 28), CGPoint(x: 36, y: 21), CGPoint(x: 45, y: 26)),
162 (CGPoint(x: 55, y: 30), CGPoint(x: 63, y: 27), CGPoint(x: 71, y: 29))]
163 default:
164 return
165 }
166 for (a, control, b) in lines {
167 var p = Path()
168 p.move(to: a)
169 p.addQuadCurve(to: b, control: control)
170 context.stroke(p, with: ink, style: StrokeStyle(lineWidth: 3.6, lineCap: .round))
171 }
172 }
173
174 private func beakShape(_ context: inout GraphicsContext) {
175 let gap = 4 * pose.beakOpen
176 if gap > 0.2 {
177 context.fill(Path(ellipseIn: CGRect(x: 45.5, y: 54, width: 9, height: 4 + gap)), with: ink)
178 }
179 var upper = Path()
180 upper.move(to: CGPoint(x: 44, y: 52))
181 upper.addLine(to: CGPoint(x: 56, y: 52))
182 upper.addLine(to: CGPoint(x: 50, y: 60))
183 upper.closeSubpath()
184 context.fill(upper, with: beak)
185 if gap > 0.2 {
186 var lower = Path()
187 lower.move(to: CGPoint(x: 46.5, y: 57 + gap))
188 lower.addLine(to: CGPoint(x: 53.5, y: 57 + gap))
189 lower.addLine(to: CGPoint(x: 50, y: 61 + gap))
190 lower.closeSubpath()
191 context.fill(lower, with: beak)
192 }
193 }
194}
195
196#Preview {
197 HStack {
198 ForEach(MascotMood.allCases, id: \.self) { MascotView(mood: $0).frame(width: 120) }
199 }
200}