OSDN Git Service

Doutakuを導入
[kcd/KCD.git] / KCD / Graphics.swift
index b601d82..8a6f485 100644 (file)
@@ -8,32 +8,65 @@
 
 import Cocoa
 
-func polygon(_ point: () -> [NSPoint]) -> NSBezierPath? {
-    let points = point()
-    let count = points.count
-    guard count > 2 else { return nil }
-    let path = NSBezierPath()
-    path.move(to: points[0])
-    points[1..<count].forEach { path.line(to: $0) }
-    path.close()
-    
-    return path
-}
-func polyline(_ point: () -> [NSPoint]) -> NSBezierPath? {
-    let points = point()
-    let count = points.count
-    guard count > 1 else { return nil }
-    let path = NSBezierPath()
-    path.move(to: points[0])
-    points[1..<count].forEach { path.line(to: $0) }
-    
-    return path
-}
-func multiline(_ lines: () -> [(NSPoint, NSPoint)]) -> NSBezierPath? {
-    let path = NSBezierPath()
-    lines().forEach {
-        path.move(to: $0.0)
-        path.line(to: $0.1)
+struct Polygon {
+    
+    private let bezierPath: NSBezierPath
+    
+    init() {
+        
+        bezierPath = NSBezierPath()
+    }
+    
+    init(lineWidth: CGFloat) {
+        
+        self.init()
+        bezierPath.lineWidth = lineWidth
+    }
+    
+    init(point: NSPoint) {
+        
+        self.init()
+        bezierPath.move(to: point)
+    }
+    
+    private init(path: NSBezierPath) {
+        
+        bezierPath = path
+    }
+    
+    var lineWidth: CGFloat {
+        get { return bezierPath.lineWidth }
+        set { bezierPath.lineWidth = lineWidth }
+    }
+    
+    func line(to point: NSPoint) -> Polygon {
+        
+        bezierPath.line(to: point)
+        
+        return Polygon(path: bezierPath)
+    }
+    
+    func move(to point: NSPoint) -> Polygon {
+        
+        bezierPath.move(to: point)
+        
+        return Polygon(path: bezierPath)
+    }
+    
+    func close() -> Polygon {
+        
+        bezierPath.close()
+        
+        return Polygon(path: bezierPath)
+    }
+    
+    func stroke() {
+        
+        bezierPath.stroke()
+    }
+    
+    func fill() {
+        
+        bezierPath.fill()
     }
-    return path
 }