OSDN Git Service

HMSuppliesCellクラスをSwiftで書き換え
[kcd/KCD.git] / KCD / HMSuppliesCell.swift
1 //
2 //  HMSuppliesCell.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2014/12/29.
6 //  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 class HMSuppliesCell: NSCell
12 {
13         var redColor: NSColor {
14                 return NSColor(calibratedWhite: 0.790, alpha: 1.000)
15         }
16         var orangeColor: NSColor {
17                 return NSColor(calibratedWhite: 0.700, alpha: 1.000)
18         }
19         var yellowColor: NSColor {
20                 return NSColor(calibratedWhite: 0.550, alpha: 1.000)
21         }
22         var greenColor: NSColor {
23                 return NSColor(calibratedWhite: 0.390, alpha: 1.000)
24         }
25         var shipStatus: HMKCShipObject?
26         let numberOfCell = 10
27         func numberOfColoredCell(value: NSNumber?, maxValue: NSNumber?) -> Int {
28                 if value == nil { return 0 }
29                 if value!.integerValue >= maxValue!.integerValue { return 10 }
30                 let result: Double = ceil(value!.doubleValue / maxValue!.doubleValue * Double(numberOfCell))
31                 if result > 9 { return 9 }
32                 return Int(result)
33         }
34         var numberOfFuelColoredCell: Int {
35                 return numberOfColoredCell(shipStatus?.fuel, maxValue: shipStatus?.maxFuel)
36         }
37         var numberOfBullColoredCell: Int {
38                 return numberOfColoredCell(shipStatus?.bull, maxValue: shipStatus?.maxBull)
39         }
40         func statusColor(value: NSNumber?, maxValue: NSNumber?) -> NSColor {
41                 var color = greenColor
42                 switch numberOfColoredCell(value, maxValue: maxValue) {
43                 case 1, 2, 3:
44                         color = redColor
45                 case 4, 5, 6, 7:
46                         color = orangeColor
47                 case 8, 9:
48                         color = yellowColor
49                 case 10:
50                         color = greenColor
51                 default:
52                         println(__FUNCTION__, " unknown type")
53                 }
54                 return color
55         }
56         var fuelStatusColor: NSColor {
57                 return statusColor(shipStatus?.fuel, maxValue: shipStatus?.maxFuel)
58         }
59         var bullStatusColor: NSColor {
60                 return statusColor(shipStatus?.bull, maxValue: shipStatus?.maxBull)
61         }
62         
63         func drawBackground(frame: NSRect) {
64                 NSColor(calibratedWhite: 0.632, alpha: 1.000).set()
65                 NSBezierPath(rect: frame).fill()
66         }
67         func drawFuelInterior(frame: NSRect) {
68                 var cellRect = NSRect()
69                 cellRect.origin = NSZeroPoint
70                 cellRect.size.width = (frame.size.width - CGFloat(numberOfCell) - 1.0) / CGFloat(numberOfCell)
71                 cellRect.size.height = (frame.size.height - 3.0) / 2.0
72                 cellRect.origin.y = cellRect.size.height + 2
73                 
74                 fuelStatusColor.set()
75                 for i in 0..<numberOfFuelColoredCell {
76                         cellRect.origin.x = CGFloat(1 + i) + cellRect.size.width * CGFloat(i)
77                         NSBezierPath(rect: cellRect).fill()
78                 }
79                 NSColor(calibratedWhite: 0.948, alpha: 1.000).set()
80                 for i in numberOfFuelColoredCell..<10 {
81                         cellRect.origin.x = CGFloat(1 + i) + cellRect.size.width * CGFloat(i)
82                         NSBezierPath(rect: cellRect).fill()
83                 }
84         }
85         func drawBullInterior(frame: NSRect) {
86                 var cellRect = NSRect()
87                 cellRect.origin = NSZeroPoint
88                 cellRect.size.width = (frame.size.width - CGFloat(numberOfCell) - 1.0) / CGFloat(numberOfCell)
89                 cellRect.size.height = (frame.size.height - 3.0) / 2.0
90                 cellRect.origin.y = 1
91                 
92                 bullStatusColor.set()
93                 for i in 0..<numberOfBullColoredCell {
94                         cellRect.origin.x = CGFloat(1 + i) + cellRect.size.width * CGFloat(i)
95                         NSBezierPath(rect: cellRect).fill()
96                 }
97                 NSColor(calibratedWhite: 0.948, alpha: 1.000).set()
98                 for i in numberOfBullColoredCell..<10 {
99                         cellRect.origin.x = CGFloat(1 + i) + cellRect.size.width * CGFloat(i)
100                         NSBezierPath(rect: cellRect).fill()
101                 }
102         }
103         override func drawInteriorWithFrame(cellFrame: NSRect, inView controlView: NSView) {
104                 drawBackground(cellFrame)
105                 drawFuelInterior(cellFrame)
106                 drawBullInterior(cellFrame)
107         }
108 }