import Cocoa
-class DamageValueTransformer: ValueTransformer {
- override class func transformedValueClass() -> Swift.AnyClass {
+final class DamageValueTransformer: ValueTransformer {
+
+ override class func transformedValueClass() -> AnyClass {
+
return NSAttributedString.self
}
+
override func transformedValue(_ value: Any?) -> Any? {
- guard let v = value as? Int, let type = DamageType(rawValue: v) else { return nil }
- let attributes: (string: String, attr: [String: Any])
+
+ guard let v = value as? Int,
+ let type = DamageType(rawValue: v),
+ let attributes = attribute(for: type) else {
+
+ return nil
+ }
+
+ return NSAttributedString(string: attributes.string, attributes: attributes.attr)
+ }
+
+ private func attribute(for type: DamageType) -> (string: String, attr: [NSAttributedStringKey: Any])? {
+
switch type {
- case .none:
- return nil
+
+ case .none: return nil
+
case .slightly:
- attributes = ("●",
- [
- NSForegroundColorAttributeName:
- NSColor(calibratedRed: 1.0, green: 0.925, blue: 0.0, alpha: 1.0),
- NSParagraphStyleAttributeName: paragraphStyle
- ]
+
+ return ("●",
+ [.foregroundColor: #colorLiteral(red: 1, green: 0.925, blue: 0, alpha: 1), .paragraphStyle: paragraphStyle]
)
+
case .modest:
- attributes = ("●",
- [
- NSForegroundColorAttributeName:
- NSColor(calibratedRed: 1.0, green: 0.392, blue: 0.0, alpha: 1.0),
- NSParagraphStyleAttributeName: paragraphStyle
- ]
+
+ return ("●",
+ [.foregroundColor: #colorLiteral(red: 1, green: 0.32, blue: 0, alpha: 1), .paragraphStyle: paragraphStyle]
)
+
case .badly:
- attributes = ("◼︎",
- [
- NSForegroundColorAttributeName:
- NSColor(calibratedRed: 0.87, green: 0.0, blue: 0.036, alpha: 1.0),
- NSParagraphStyleAttributeName: paragraphStyle
- ]
+
+ return ("◼︎",
+ [.foregroundColor: #colorLiteral(red: 0.87, green: 0, blue: 0.036, alpha: 1), .paragraphStyle: paragraphStyle]
)
}
-
- return NSAttributedString(string: attributes.string, attributes: attributes.attr)
}
- var paragraphStyle: NSParagraphStyle = {
+ private var paragraphStyle: NSParagraphStyle = {
+
let style = NSMutableParagraphStyle()
style.alignment = .center
+
return style
}()
}