OSDN Git Service

洋上補給の補強増設用のショートネームをつけた
[kcd/KCD.git] / KCD / FadeoutTextField.swift
1 //
2 //  FadeoutTextField.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/09/16.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 class FadeoutTextField: NSTextField {
12     
13     var middle1: CGFloat = 0.8
14     var middle2: CGFloat = 1.0
15     
16     private var maskImage: CGImage? {
17         
18         if !isCharacterProtrude() {
19             
20             return nil
21         }
22         
23         if let image = cachMaskImage {
24             
25             return image
26         }
27         
28         cachMaskImage = maskImage(middle1: middle1, middle2: middle2)
29         
30         return cachMaskImage
31     }
32     
33     private var cachMaskImage: CGImage?
34     
35     override func draw(_ dirtyRect: NSRect) {
36         
37         guard let context = NSGraphicsContext.current?.cgContext else { fatalError("Con not get current CGContext") }
38         
39         context.saveGState()
40         maskImage.map { context.clip(to: bounds, mask: $0) }
41         
42         super.draw(dirtyRect)
43         
44         context.restoreGState()
45     }
46     
47     override func resizeSubviews(withOldSize oldSize: NSSize) {
48         
49         cachMaskImage = nil
50         
51         super.resizeSubviews(withOldSize: oldSize)
52     }
53     
54 }