Skip to content

Commit 91541f4

Browse files
committed
Added new segments drawing file
1 parent 87c23c7 commit 91541f4

File tree

1 file changed

+266
-0
lines changed

1 file changed

+266
-0
lines changed

SnakeGame/SnakeSegments.swift

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
//
2+
// SnakeSegments.swift
3+
// SnakeGame
4+
//
5+
// Created by PavelPopov on 07.09.16.
6+
// Copyright (c) 2016 PixelCut. All rights reserved.
7+
//
8+
// Generated by PaintCode (www.paintcodeapp.com)
9+
//
10+
11+
12+
13+
import UIKit
14+
15+
public class SnakeSegments : NSObject {
16+
17+
//// Cache
18+
19+
private struct Cache {
20+
static let bodyColor: UIColor = UIColor(red: 0.000, green: 1.000, blue: 0.338, alpha: 1.000)
21+
static let foodColor: UIColor = UIColor(red: 1.000, green: 0.000, blue: 0.000, alpha: 1.000)
22+
}
23+
24+
//// Colors
25+
26+
public class var bodyColor: UIColor { return Cache.bodyColor }
27+
public class var foodColor: UIColor { return Cache.foodColor }
28+
29+
//// Drawing Methods
30+
31+
public class func drawHeadSymbol(open open: Bool = false, angle: CGFloat = 90) {
32+
//// General Declarations
33+
let context = UIGraphicsGetCurrentContext()
34+
35+
//// Color Declarations
36+
let bodyColor2 = SnakeSegments.bodyColor.colorWithShadow(0.5)
37+
38+
//// Variable Declarations
39+
let endAngle: CGFloat = open ? 125 : 90
40+
let startAngle: CGFloat = open ? 415 : 450
41+
42+
//// Oval Drawing
43+
CGContextSaveGState(context)
44+
CGContextTranslateCTM(context, 30, 30)
45+
CGContextRotateCTM(context, -angle * CGFloat(M_PI) / 180)
46+
47+
let ovalRect = CGRect(x: -25, y: -25, width: 50, height: 50)
48+
let ovalPath = UIBezierPath()
49+
ovalPath.addArcWithCenter(CGPoint(x: ovalRect.midX, y: ovalRect.midY), radius: ovalRect.width / 2, startAngle: -startAngle * CGFloat(M_PI)/180, endAngle: -endAngle * CGFloat(M_PI)/180, clockwise: true)
50+
ovalPath.addLineToPoint(CGPoint(x: ovalRect.midX, y: ovalRect.midY))
51+
ovalPath.closePath()
52+
53+
SnakeSegments.bodyColor.setFill()
54+
ovalPath.fill()
55+
bodyColor2.setStroke()
56+
ovalPath.lineWidth = 2
57+
ovalPath.stroke()
58+
59+
CGContextRestoreGState(context)
60+
}
61+
62+
public class func drawHead(open open: Bool = false, angle: CGFloat = 90, bounds: CGRect = CGRect(x: 0, y: 0, width: 60, height: 60)) {
63+
//// General Declarations
64+
let context = UIGraphicsGetCurrentContext()
65+
66+
//// Symbol Drawing
67+
let symbolRect = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width, height: bounds.size.height)
68+
CGContextSaveGState(context)
69+
UIRectClip(symbolRect)
70+
CGContextTranslateCTM(context, symbolRect.origin.x, symbolRect.origin.y)
71+
CGContextScaleCTM(context, symbolRect.size.width / 60, symbolRect.size.height / 60)
72+
73+
SnakeSegments.drawHeadSymbol(open: open, angle: angle)
74+
CGContextRestoreGState(context)
75+
}
76+
77+
public class func drawTailSymbol(angle angle: CGFloat = 90) {
78+
//// General Declarations
79+
let context = UIGraphicsGetCurrentContext()
80+
81+
//// Color Declarations
82+
let bodyColor2 = SnakeSegments.bodyColor.colorWithShadow(0.5)
83+
84+
//// Polygon Drawing
85+
CGContextSaveGState(context)
86+
CGContextTranslateCTM(context, 30, 30)
87+
CGContextRotateCTM(context, -(angle - 90) * CGFloat(M_PI) / 180)
88+
89+
let polygonPath = UIBezierPath()
90+
polygonPath.moveToPoint(CGPoint(x: 24, y: 0))
91+
polygonPath.addLineToPoint(CGPoint(x: -25, y: 20.35))
92+
polygonPath.addLineToPoint(CGPoint(x: -25, y: -20.35))
93+
polygonPath.addLineToPoint(CGPoint(x: 24, y: 0))
94+
polygonPath.closePath()
95+
SnakeSegments.bodyColor.setFill()
96+
polygonPath.fill()
97+
bodyColor2.setStroke()
98+
polygonPath.lineWidth = 2
99+
polygonPath.stroke()
100+
101+
CGContextRestoreGState(context)
102+
}
103+
104+
public class func drawTail(angle angle: CGFloat = 90, bounds: CGRect = CGRect(x: 0, y: 0, width: 60, height: 60)) {
105+
//// General Declarations
106+
let context = UIGraphicsGetCurrentContext()
107+
108+
//// Symbol Drawing
109+
let symbolRect = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width, height: bounds.size.height)
110+
CGContextSaveGState(context)
111+
UIRectClip(symbolRect)
112+
CGContextTranslateCTM(context, symbolRect.origin.x, symbolRect.origin.y)
113+
CGContextScaleCTM(context, symbolRect.size.width / 60, symbolRect.size.height / 60)
114+
115+
SnakeSegments.drawTailSymbol(angle: angle)
116+
CGContextRestoreGState(context)
117+
}
118+
119+
public class func drawFoodSymbol() {
120+
//// Color Declarations
121+
let foodColor2 = SnakeSegments.foodColor.colorWithShadow(0.5)
122+
123+
//// Oval Drawing
124+
let ovalPath = UIBezierPath(ovalInRect: CGRect(x: 18, y: 5, width: 25, height: 25))
125+
SnakeSegments.foodColor.setFill()
126+
ovalPath.fill()
127+
foodColor2.setStroke()
128+
ovalPath.lineWidth = 2
129+
ovalPath.stroke()
130+
131+
132+
//// Rectangle Drawing
133+
let rectanglePath = UIBezierPath(rect: CGRect(x: 28, y: 27, width: 4, height: 28))
134+
SnakeSegments.foodColor.setFill()
135+
rectanglePath.fill()
136+
foodColor2.setStroke()
137+
rectanglePath.lineWidth = 2
138+
rectanglePath.stroke()
139+
140+
141+
//// Oval 2 Drawing
142+
let oval2Path = UIBezierPath(ovalInRect: CGRect(x: 5, y: 17, width: 25, height: 25))
143+
SnakeSegments.foodColor.setFill()
144+
oval2Path.fill()
145+
foodColor2.setStroke()
146+
oval2Path.lineWidth = 2
147+
oval2Path.stroke()
148+
149+
150+
//// Oval 3 Drawing
151+
let oval3Path = UIBezierPath(ovalInRect: CGRect(x: 30, y: 17, width: 25, height: 25))
152+
SnakeSegments.foodColor.setFill()
153+
oval3Path.fill()
154+
foodColor2.setStroke()
155+
oval3Path.lineWidth = 2
156+
oval3Path.stroke()
157+
}
158+
159+
public class func drawFood(bounds bounds: CGRect = CGRect(x: 0, y: 0, width: 60, height: 60)) {
160+
//// General Declarations
161+
let context = UIGraphicsGetCurrentContext()
162+
163+
//// Symbol Drawing
164+
let symbolRect = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width, height: bounds.size.height)
165+
CGContextSaveGState(context)
166+
UIRectClip(symbolRect)
167+
CGContextTranslateCTM(context, symbolRect.origin.x, symbolRect.origin.y)
168+
CGContextScaleCTM(context, symbolRect.size.width / 60, symbolRect.size.height / 60)
169+
170+
SnakeSegments.drawFoodSymbol()
171+
CGContextRestoreGState(context)
172+
}
173+
174+
public class func drawBorderSymbol() {
175+
//// Color Declarations
176+
let borderColor = UIColor(red: 0.485, green: 0.485, blue: 0.485, alpha: 1.000)
177+
let borderColor2 = borderColor.colorWithShadow(0.5)
178+
179+
//// Rectangle Drawing
180+
let rectanglePath = UIBezierPath(roundedRect: CGRect(x: 5, y: 5, width: 50, height: 50), cornerRadius: 10)
181+
borderColor.setFill()
182+
rectanglePath.fill()
183+
borderColor2.setStroke()
184+
rectanglePath.lineWidth = 2
185+
rectanglePath.stroke()
186+
}
187+
188+
public class func drawBorder(bounds bounds: CGRect = CGRect(x: 0, y: 0, width: 60, height: 60)) {
189+
//// General Declarations
190+
let context = UIGraphicsGetCurrentContext()
191+
192+
//// Symbol Drawing
193+
let symbolRect = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width, height: bounds.size.height)
194+
CGContextSaveGState(context)
195+
UIRectClip(symbolRect)
196+
CGContextTranslateCTM(context, symbolRect.origin.x, symbolRect.origin.y)
197+
CGContextScaleCTM(context, symbolRect.size.width / 60, symbolRect.size.height / 60)
198+
199+
SnakeSegments.drawBorderSymbol()
200+
CGContextRestoreGState(context)
201+
}
202+
203+
public class func drawMiddleSymbol() {
204+
//// Color Declarations
205+
let bodyColor2 = SnakeSegments.bodyColor.colorWithShadow(0.5)
206+
207+
//// Oval Drawing
208+
let ovalPath = UIBezierPath(ovalInRect: CGRect(x: 5, y: 5, width: 50, height: 50))
209+
SnakeSegments.bodyColor.setFill()
210+
ovalPath.fill()
211+
bodyColor2.setStroke()
212+
ovalPath.lineWidth = 2
213+
ovalPath.stroke()
214+
}
215+
216+
public class func drawMiddle(bounds bounds: CGRect = CGRect(x: 0, y: 0, width: 60, height: 60)) {
217+
//// General Declarations
218+
let context = UIGraphicsGetCurrentContext()
219+
220+
//// Symbol Drawing
221+
let symbolRect = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width, height: bounds.size.height)
222+
CGContextSaveGState(context)
223+
UIRectClip(symbolRect)
224+
CGContextTranslateCTM(context, symbolRect.origin.x, symbolRect.origin.y)
225+
CGContextScaleCTM(context, symbolRect.size.width / 60, symbolRect.size.height / 60)
226+
227+
SnakeSegments.drawMiddleSymbol()
228+
CGContextRestoreGState(context)
229+
}
230+
231+
}
232+
233+
234+
235+
extension UIColor {
236+
func colorWithHue(newHue: CGFloat) -> UIColor {
237+
var saturation: CGFloat = 1.0, brightness: CGFloat = 1.0, alpha: CGFloat = 1.0
238+
self.getHue(nil, saturation: &saturation, brightness: &brightness, alpha: &alpha)
239+
return UIColor(hue: newHue, saturation: saturation, brightness: brightness, alpha: alpha)
240+
}
241+
func colorWithSaturation(newSaturation: CGFloat) -> UIColor {
242+
var hue: CGFloat = 1.0, brightness: CGFloat = 1.0, alpha: CGFloat = 1.0
243+
self.getHue(&hue, saturation: nil, brightness: &brightness, alpha: &alpha)
244+
return UIColor(hue: hue, saturation: newSaturation, brightness: brightness, alpha: alpha)
245+
}
246+
func colorWithBrightness(newBrightness: CGFloat) -> UIColor {
247+
var hue: CGFloat = 1.0, saturation: CGFloat = 1.0, alpha: CGFloat = 1.0
248+
self.getHue(&hue, saturation: &saturation, brightness: nil, alpha: &alpha)
249+
return UIColor(hue: hue, saturation: saturation, brightness: newBrightness, alpha: alpha)
250+
}
251+
func colorWithAlpha(newAlpha: CGFloat) -> UIColor {
252+
var hue: CGFloat = 1.0, saturation: CGFloat = 1.0, brightness: CGFloat = 1.0
253+
self.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: nil)
254+
return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: newAlpha)
255+
}
256+
func colorWithHighlight(highlight: CGFloat) -> UIColor {
257+
var red: CGFloat = 1.0, green: CGFloat = 1.0, blue: CGFloat = 1.0, alpha: CGFloat = 1.0
258+
self.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
259+
return UIColor(red: red * (1-highlight) + highlight, green: green * (1-highlight) + highlight, blue: blue * (1-highlight) + highlight, alpha: alpha * (1-highlight) + highlight)
260+
}
261+
func colorWithShadow(shadow: CGFloat) -> UIColor {
262+
var red: CGFloat = 1.0, green: CGFloat = 1.0, blue: CGFloat = 1.0, alpha: CGFloat = 1.0
263+
self.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
264+
return UIColor(red: red * (1-shadow), green: green * (1-shadow), blue: blue * (1-shadow), alpha: alpha * (1-shadow) + shadow)
265+
}
266+
}

0 commit comments

Comments
 (0)