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)
23             else { return nil }
24         
25         return NSAttributedString(string: attributes.string, attributes: attributes.attr)
26     }
27     
28     private func attribute(for type: DamageType) -> (string: String, attr: [String: Any])? {
29         
30         switch type {
31         case .none:
32             return nil
33             
34         case .slightly:
35             return ("●",
36                     [NSForegroundColorAttributeName: #colorLiteral(red: 1, green: 0.925, blue: 0, alpha: 1),
37                      NSParagraphStyleAttributeName: paragraphStyle]
38             )
39             
40         case .modest:
41             return ("●",
42                     [NSForegroundColorAttributeName: #colorLiteral(red: 1, green: 0.32, blue: 0, alpha: 1),
43                      NSParagraphStyleAttributeName: paragraphStyle]
44             )
45             
46         case .badly:
47             return ("◼︎",
48                     [NSForegroundColorAttributeName: #colorLiteral(red: 0.87, green: 0, blue: 0.036, alpha: 1),
49                      NSParagraphStyleAttributeName: paragraphStyle]
50             )
51         }
52     }
53     
54     private var paragraphStyle: NSParagraphStyle = {
55         
56         let style = NSMutableParagraphStyle()
57         style.alignment = .center
58         
59         return style
60     }()
61 }