OSDN Git Service

return文の前に改行を入れるようにした
[kcd/KCD.git] / KCD / DamageValueTransformer.swift
1 //
2 //  DamageValueTransformer.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/05.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 final class DamageValueTransformer: ValueTransformer {
12     
13     override class func transformedValueClass() -> AnyClass {
14         
15         return NSAttributedString.self
16     }
17     
18     override func transformedValue(_ value: Any?) -> Any? {
19         
20         guard let v = value as? Int,
21             let type = DamageType(rawValue: v),
22             let attributes = attribute(for: type) else {
23                 
24                 return nil
25         }
26         
27         return NSAttributedString(string: attributes.string, attributes: attributes.attr)
28     }
29     
30     private func attribute(for type: DamageType) -> (string: String, attr: [NSAttributedStringKey: Any])? {
31         
32         switch type {
33             
34         case .none: return nil
35             
36         case .slightly:
37             
38             return ("●",
39                     [.foregroundColor: #colorLiteral(red: 1, green: 0.925, blue: 0, alpha: 1), .paragraphStyle: paragraphStyle]
40             )
41             
42         case .modest:
43             
44             return ("●",
45                     [.foregroundColor: #colorLiteral(red: 1, green: 0.32, blue: 0, alpha: 1), .paragraphStyle: paragraphStyle]
46             )
47             
48         case .badly:
49             
50             return ("◼︎",
51                     [.foregroundColor: #colorLiteral(red: 0.87, green: 0, blue: 0.036, alpha: 1), .paragraphStyle: paragraphStyle]
52             )
53         }
54     }
55     
56     private var paragraphStyle: NSParagraphStyle = {
57         
58         let style = NSMutableParagraphStyle()
59         style.alignment = .center
60         
61         return style
62     }()
63 }