forked from bozidarsevo/regular-rect-sktexture-subtexture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegularRectSubtexture.swift
62 lines (53 loc) · 1.48 KB
/
RegularRectSubtexture.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import SpriteKit
extension CGRect {
func unitRectForSize(size: CGSize) -> CGRect {
if 0 > size.width || 0 > size.height {
return CGRect(x: 0, y: 0, width: 0, height: 0)
}
var x = self.origin.x
var y = self.origin.y
var width = self.size.width + x
var height = self.size.height + y
// x
if 0 > x {
width += x
x = 0
} else if size.width < x {
x = size.width
width = size.width
}
// y
if 0 > y {
height += y
y = 0
} else if size.height < y {
y = size.height
height = size.height
}
// width
if 0 > width {
width = 0
} else if size.width < width {
width = size.width
}
if width < x {
width = x
}
// height
if 0 > height {
height = 0
} else if size.height < height {
height = size.height
}
if height < y {
height = y
}
return CGRect(x: x / size.width, y: y / size.height, width: (width - x) / size.width, height: (height - y) / size.height)
}
}
extension SKTexture {
convenience init(regularRect: CGRect, inTexture texture: SKTexture) {
let rect = regularRect.unitRectForSize(texture.size())
self.init(rect: rect, inTexture: texture)
}
}