OSDN Git Service

関数名を変更
[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         case .none:
34             return nil
35             
36         case .slightly:
37             return ("●",
38                     [.foregroundColor: #colorLiteral(red: 1, green: 0.925, blue: 0, alpha: 1), .paragraphStyle: paragraphStyle]
39             )
40             
41         case .modest:
42             return ("●",
43                     [.foregroundColor: #colorLiteral(red: 1, green: 0.32, blue: 0, alpha: 1), .paragraphStyle: paragraphStyle]
44             )
45             
46         case .badly:
47             return ("◼︎",
48                     [.foregroundColor: #colorLiteral(red: 0.87, green: 0, blue: 0.036, alpha: 1), .paragraphStyle: paragraphStyle]
49             )
50         }
51     }
52     
53     private var paragraphStyle: NSParagraphStyle = {
54         
55         let style = NSMutableParagraphStyle()
56         style.alignment = .center
57         
58         return style
59     }()
60 }