@@ -19,34 +19,21 @@ import UIKit
19
19
20
20
import MaterialComponents. MaterialTypography
21
21
22
- fileprivate extension MDCTypography {
23
-
24
- static func boldFont( withSize size: CGFloat ) -> UIFont {
25
- // This font should load unless something is wrong with Material's fonts.
26
- return MDCTypography . fontLoader ( ) . boldFont!( ofSize: size)
27
- }
22
+ fileprivate extension ArduinoTypography {
28
23
29
24
static var fontName : String {
30
25
// Arbitrary size to get the font name.
31
- return boldFont ( withSize : 10 ) . fontName
26
+ return monoBoldFont ( forSize : 10 ) . fontName
32
27
}
33
28
34
29
}
35
30
36
- // swiftlint:disable type_body_length
37
31
/// Animation view for the pitch sensor.
38
32
class PitchSensorAnimationView : SensorAnimationView {
39
33
40
34
// MARK: - Nested
41
35
42
36
private enum Metrics {
43
- static let backgroundColorValueBlueHigh : CGFloat = 175
44
- static let backgroundColorValueBlueLow : CGFloat = 248
45
- static let backgroundColorValueGreenHigh : CGFloat = 97
46
- static let backgroundColorValueGreenLow : CGFloat = 202
47
- static let backgroundColorValueRedHigh : CGFloat = 13
48
- static let backgroundColorValueRedLow : CGFloat = 113
49
- static let backgroundRadiusRatio : CGFloat = 0.38
50
37
static let dotAngleTop = CGFloat ( Double . pi / 2 )
51
38
static let dotEllipseRadiusRatio : CGFloat = 0.44
52
39
static let dotRadiusRatio : CGFloat = 0.05
@@ -55,21 +42,16 @@ class PitchSensorAnimationView: SensorAnimationView {
55
42
static let noteFontSizeRatio : CGFloat = 0.48
56
43
static let noteRatioX : CGFloat = 0.42
57
44
static let noteRatioY : CGFloat = 0.46
58
- static let noteLeftTextColor =
59
- UIColor ( red: 238 / 255 , green: 238 / 255 , blue: 238 / 255 , alpha: 1 ) . cgColor
60
- static let noteRightTextColor =
61
- UIColor ( red: 220 / 255 , green: 220 / 255 , blue: 220 / 255 , alpha: 1 ) . cgColor
45
+ static let noteTextColor = UIColor . white. cgColor
62
46
static let numberOfPianoKeys = 88
63
47
static let octaveFontSizeRatio : CGFloat = 0.2
64
48
static let octaveRatioX : CGFloat = 0.67
65
49
static let octaveRatioY : CGFloat = 0.67
66
- static let octaveTextColor =
67
- UIColor ( red: 220 / 255 , green: 220 / 255 , blue: 220 / 255 , alpha: 1 ) . cgColor
68
- static let signFontSizeRatio : CGFloat = 0.25
69
- static let signRatioX : CGFloat = 0.62
50
+ static let octaveTextColor = UIColor . white. cgColor
51
+ static let signFontSizeRatio : CGFloat = 0.3
52
+ static let signRatioX : CGFloat = 0.68
70
53
static let signRatioY : CGFloat = 0.43
71
- static let signTextColor =
72
- UIColor ( red: 220 / 255 , green: 220 / 255 , blue: 220 / 255 , alpha: 1 ) . cgColor
54
+ static let signTextColor = UIColor . white. cgColor
73
55
static let textShadowColor = UIColor ( white: 0 , alpha: 0.61 ) . cgColor
74
56
static let textShadowOffset = CGSize ( width: 1 , height: 2 )
75
57
static let textShadowRadius : CGFloat = 4
@@ -91,8 +73,7 @@ class PitchSensorAnimationView: SensorAnimationView {
91
73
92
74
private let backgroundShapeLayer = CAShapeLayer ( )
93
75
private let dotShapeLayer = CAShapeLayer ( )
94
- private let noteLeftTextLayer = CATextLayer ( )
95
- private let noteRightTextLayer = CATextLayer ( )
76
+ private let noteTextLayer = CATextLayer ( )
96
77
private let signTextLayer = CATextLayer ( )
97
78
private let octaveTextLayer = CATextLayer ( )
98
79
@@ -201,14 +182,6 @@ class PitchSensorAnimationView: SensorAnimationView {
201
182
override func layoutSubviews( ) {
202
183
super. layoutSubviews ( )
203
184
204
- // Background shape layer.
205
- let backgroundShapeRadius = bounds. width * Metrics. backgroundRadiusRatio
206
- let backgroundShapeRect = CGRect ( x: bounds. midX - backgroundShapeRadius,
207
- y: bounds. midY - backgroundShapeRadius,
208
- width: backgroundShapeRadius * 2 ,
209
- height: backgroundShapeRadius * 2 )
210
- backgroundShapeLayer. path = UIBezierPath ( ovalIn: backgroundShapeRect) . cgPath
211
-
212
185
// Dot shape layer will be at a point on an invisible ellipse.
213
186
let ellipseRadius = bounds. width * Metrics. dotEllipseRadiusRatio
214
187
let dotX = bounds. midX + ellipseRadius * cos( angleOfDot)
@@ -224,12 +197,11 @@ class PitchSensorAnimationView: SensorAnimationView {
224
197
if let musicalNote = musicalNote {
225
198
// Letter
226
199
let noteFontSize = floor ( bounds. height * Metrics. noteFontSizeRatio)
227
- noteLeftTextLayer. fontSize = noteFontSize
228
- noteRightTextLayer. fontSize = noteFontSize
200
+ noteTextLayer. fontSize = noteFontSize
229
201
let noteSize = musicalNote. letter. boundingRect (
230
202
with: . zero,
231
203
options: [ ] ,
232
- attributes: [ NSAttributedString . Key. font: MDCTypography . boldFont ( withSize : noteFontSize) ] ,
204
+ attributes: [ NSAttributedString . Key. font: ArduinoTypography . monoBoldFont ( forSize : noteFontSize) ] ,
233
205
context: nil ) . size
234
206
var noteX : CGFloat
235
207
var noteY : CGFloat
@@ -242,18 +214,14 @@ class PitchSensorAnimationView: SensorAnimationView {
242
214
}
243
215
244
216
// The note layer on the left is half width.
245
- noteLeftTextLayer . frame = CGRect ( x: floor ( noteX) ,
217
+ noteTextLayer . frame = CGRect ( x: floor ( noteX) ,
246
218
y: floor ( noteY) ,
247
- width: ceil ( noteSize. width/ 2 ) ,
219
+ width: ceil ( noteSize. width) ,
248
220
height: ceil ( noteSize. height) )
249
- noteRightTextLayer. frame = CGRect ( x: floor ( noteX) ,
250
- y: floor ( noteY) ,
251
- width: ceil ( noteSize. width) ,
252
- height: ceil ( noteSize. height) )
253
-
221
+
254
222
// Sign.
255
223
signTextLayer. fontSize = floor ( bounds. height * Metrics. signFontSizeRatio)
256
- let signFont = MDCTypography . boldFont ( withSize : signTextLayer. fontSize)
224
+ let signFont = ArduinoTypography . monoBoldFont ( forSize : signTextLayer. fontSize)
257
225
let signSize = musicalNote. sign. boundingRect (
258
226
with: . zero,
259
227
options: [ ] ,
@@ -267,7 +235,7 @@ class PitchSensorAnimationView: SensorAnimationView {
267
235
268
236
// Octave
269
237
octaveTextLayer. fontSize = floor ( bounds. height * Metrics. octaveFontSizeRatio)
270
- let octaveFont = MDCTypography . boldFont ( withSize : octaveTextLayer. fontSize)
238
+ let octaveFont = ArduinoTypography . monoBoldFont ( forSize : octaveTextLayer. fontSize)
271
239
let octaveSize =
272
240
musicalNote. octave. boundingRect ( with: . zero,
273
241
options: [ ] ,
@@ -287,17 +255,13 @@ class PitchSensorAnimationView: SensorAnimationView {
287
255
// Store the level.
288
256
self . level = level
289
257
290
- // Set the fill color of the background shape layer.
291
- backgroundShapeLayer. fillColor = backgroundColor ( forLevel: level) . cgColor
292
-
293
258
// The the angle of the dot.
294
259
let difference = differenceBetween ( pitch: value, andLevel: level)
295
260
angleOfDot = CGFloat ( ( 1 - 2 * difference) * ( Double . pi / 2 ) )
296
261
297
262
// Set the musical note letter, sign and octave.
298
263
let musicalNote = musicalNotes [ level]
299
- noteLeftTextLayer. string = musicalNote. letter
300
- noteRightTextLayer. string = musicalNote. letter
264
+ noteTextLayer. string = musicalNote. letter
301
265
signTextLayer. string = musicalNote. sign
302
266
octaveTextLayer. string = musicalNote. octave
303
267
@@ -327,29 +291,6 @@ class PitchSensorAnimationView: SensorAnimationView {
327
291
328
292
// MARK: - Private
329
293
330
- private func backgroundColor( forLevel level: Int ) -> UIColor {
331
- if level == 0 {
332
- return UIColor ( red: Metrics . backgroundColorValueRedLow / 255 ,
333
- green: Metrics . backgroundColorValueGreenLow / 255 ,
334
- blue: Metrics . backgroundColorValueBlueLow / 255 , alpha: 1 )
335
- } else if level == noteFrequencies. endIndex - 1 {
336
- return UIColor ( red: Metrics . backgroundColorValueRedHigh / 255 ,
337
- green: Metrics . backgroundColorValueGreenHigh / 255 ,
338
- blue: Metrics . backgroundColorValueBlueHigh / 255 , alpha: 1 )
339
- } else {
340
- let red = round ( Metrics . backgroundColorValueRedLow +
341
- ( Metrics . backgroundColorValueRedHigh - Metrics. backgroundColorValueRedLow) *
342
- CGFloat( level) / CGFloat( noteFrequencies. endIndex - 1 ) )
343
- let green = round ( Metrics . backgroundColorValueGreenLow +
344
- ( Metrics . backgroundColorValueGreenHigh - Metrics. backgroundColorValueGreenLow) *
345
- CGFloat( level) / CGFloat( noteFrequencies. endIndex - 1 ) )
346
- let blue = round ( Metrics . backgroundColorValueBlueLow +
347
- ( Metrics . backgroundColorValueBlueHigh - Metrics. backgroundColorValueBlueLow) *
348
- CGFloat( level) / CGFloat( noteFrequencies. endIndex - 1 ) )
349
- return UIColor ( red: red / 255 , green: green / 255 , blue: blue / 255 , alpha: 1 )
350
- }
351
- }
352
-
353
294
private func configureView( ) {
354
295
isAccessibilityElement = true
355
296
@@ -359,31 +300,24 @@ class PitchSensorAnimationView: SensorAnimationView {
359
300
imageView. frame = bounds
360
301
addSubview ( imageView)
361
302
362
- // Background shape layer.
363
- backgroundShapeLayer. fillColor = backgroundColor ( forLevel: 0 ) . cgColor
364
- layer. addSublayer ( backgroundShapeLayer)
365
-
366
303
// Dot shape layer.
367
- dotShapeLayer. fillColor = UIColor . red . cgColor
304
+ dotShapeLayer. fillColor = ArduinoColorPalette . warningColor . cgColor
368
305
layer. addSublayer ( dotShapeLayer)
369
306
370
307
// Text layer common configuration.
371
- [ noteRightTextLayer , noteLeftTextLayer , signTextLayer, octaveTextLayer] . forEach {
308
+ [ noteTextLayer , signTextLayer, octaveTextLayer] . forEach {
372
309
$0. alignmentMode = . center
373
310
$0. contentsScale = UIScreen . main. scale
374
- $0. font = CGFont ( MDCTypography . fontName as CFString )
311
+ $0. font = CGFont ( ArduinoTypography . fontName as CFString )
375
312
$0. shadowColor = Metrics . textShadowColor
376
313
$0. shadowOffset = Metrics . textShadowOffset
377
314
$0. shadowRadius = Metrics . textShadowRadius
378
315
layer. addSublayer ( $0)
379
316
}
380
317
381
- // Right note.
382
- noteRightTextLayer. foregroundColor = Metrics . noteRightTextColor
383
-
384
318
// Left note.
385
- noteLeftTextLayer . foregroundColor = Metrics . noteLeftTextColor
386
- noteLeftTextLayer . masksToBounds = true
319
+ noteTextLayer . foregroundColor = Metrics . noteTextColor
320
+ noteTextLayer . masksToBounds = true
387
321
388
322
// Sign.
389
323
signTextLayer. foregroundColor = Metrics . signTextColor
0 commit comments