Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 156 lines (105 sloc) 5.736 kb
8a6043f @steipete initial commit
authored
1 //
2 // SCGrfx.m
3 // TouchCustoms
4 //
5 // Created by Aleks Nesterow on 10/15/09.
6 // aleks.nesterow@gmail.com
7 //
8 // Copyright © 2009-2010 Screen Customs s.r.o.
9 // All rights reserved.
10 //
11
12 #import "SCGrfx.h"
13
14 SCRoundedRect SCRoundedRectMake(CGRect rect, CGFloat cornerRadius) {
15
16 SCRoundedRect result;
17
18 result.xLeft = CGRectGetMinX(rect);
19 result.xLeftCorner = result.xLeft + cornerRadius;
20
21 result.xRight = CGRectGetMaxX(rect);
22 result.xRightCorner = result.xRight - cornerRadius;
23
24 result.yTop = CGRectGetMinY(rect);
25 result.yTopCorner = result.yTop + cornerRadius;
26
27 result.yBottom = CGRectGetMaxY(rect);
28 result.yBottomCorner = result.yBottom - cornerRadius;
29
30 return result;
31 }
32
33 void SCContextAddRoundedRect(CGContextRef c, CGRect rect, CGFloat cornerRadius) {
34
35 SCRoundedRect roundedRect = SCRoundedRectMake(rect, cornerRadius);
36
37 /* Begin */
38 CGContextBeginPath(c);
39 CGContextMoveToPoint(c, roundedRect.xLeft, roundedRect.yTopCorner);
40
41 /* First corner */
42 CGContextAddArcToPoint(c, roundedRect.xLeft, roundedRect.yTop, roundedRect.xLeftCorner, roundedRect.yTop, cornerRadius);
43 CGContextAddLineToPoint(c, roundedRect.xRightCorner, roundedRect.yTop);
44
45 /* Second corner */
46 CGContextAddArcToPoint(c, roundedRect.xRight, roundedRect.yTop, roundedRect.xRight, roundedRect.yTopCorner, cornerRadius);
47 CGContextAddLineToPoint(c, roundedRect.xRight, roundedRect.yBottomCorner);
48
49 /* Third corner */
50 CGContextAddArcToPoint(c, roundedRect.xRight, roundedRect.yBottom, roundedRect.xRightCorner, roundedRect.yBottom, cornerRadius);
51 CGContextAddLineToPoint(c, roundedRect.xLeftCorner, roundedRect.yBottom);
52
53 /* Fourth corner */
54 CGContextAddArcToPoint(c, roundedRect.xLeft, roundedRect.yBottom, roundedRect.xLeft, roundedRect.yBottomCorner, cornerRadius);
55 CGContextAddLineToPoint(c, roundedRect.xLeft, roundedRect.yTopCorner);
56
57 /* Done */
58 CGContextClosePath(c);
59 }
60
61 void SCContextAddLeftRoundedRect(CGContextRef c, CGRect rect, CGFloat radius) {
62
63 SCRoundedRect roundedRect = SCRoundedRectMake(rect, radius);
64
65 CGContextBeginPath(c);
66 CGContextMoveToPoint(c, roundedRect.xLeft, roundedRect.yTopCorner);
67
68 CGContextAddArcToPoint(c, roundedRect.xLeft, roundedRect.yTop, roundedRect.xLeftCorner, roundedRect.yTop, radius);
69
70 CGContextAddLineToPoint(c, roundedRect.xRight, roundedRect.yTop);
71 CGContextAddLineToPoint(c, roundedRect.xRight, roundedRect.yBottom);
72 CGContextAddLineToPoint(c, roundedRect.xLeftCorner, roundedRect.yBottom);
73
74 CGContextAddArcToPoint(c, roundedRect.xLeft, roundedRect.yBottom, roundedRect.xLeft, roundedRect.yBottomCorner, radius);
75
76 CGContextClosePath(c);
77 }
78
79 void SCContextAddLeftTopRoundedRect(CGContextRef c, CGRect rect, CGFloat radius) {
80
81 SCRoundedRect roundedRect = SCRoundedRectMake(rect, radius);
82
83 CGContextBeginPath(c);
84 CGContextMoveToPoint(c, roundedRect.xLeft, roundedRect.yTopCorner);
85
86 CGContextAddArcToPoint(c, roundedRect.xLeft, roundedRect.yTop, roundedRect.xLeftCorner, roundedRect.yTop, radius);
87
88 CGContextAddLineToPoint(c, roundedRect.xRight, roundedRect.yTop);
89 CGContextAddLineToPoint(c, roundedRect.xRight, roundedRect.yBottom);
90 CGContextAddLineToPoint(c, roundedRect.xLeft, roundedRect.yBottom);
91
92 CGContextClosePath(c);
93 }
94
95 void SCContextAddLeftBottomRoundedRect(CGContextRef c, CGRect rect, CGFloat radius) {
96
97 SCRoundedRect roundedRect = SCRoundedRectMake(rect, radius);
98
99 CGContextBeginPath(c);
100 CGContextMoveToPoint(c, roundedRect.xLeft, roundedRect.yTop);
101
102 CGContextAddLineToPoint(c, roundedRect.xRight, roundedRect.yTop);
103 CGContextAddLineToPoint(c, roundedRect.xRight, roundedRect.yBottom);
104 CGContextAddLineToPoint(c, roundedRect.xLeftCorner, roundedRect.yBottom);
105
106 CGContextAddArcToPoint(c, roundedRect.xLeft, roundedRect.yBottom, roundedRect.xLeft, roundedRect.yBottomCorner, radius);
107
108 CGContextClosePath(c);
109 }
110
111 void SCContextAddRightRoundedRect(CGContextRef c, CGRect rect, CGFloat radius) {
112
113 SCRoundedRect roundedRect = SCRoundedRectMake(rect, radius);
114
115 CGContextBeginPath(c);
116 CGContextMoveToPoint(c, roundedRect.xLeft, roundedRect.yTop);
117
118 CGContextAddLineToPoint(c, roundedRect.xRightCorner, roundedRect.yTop);
119 CGContextAddArcToPoint(c, roundedRect.xRight, roundedRect.yTop, roundedRect.xRight, roundedRect.yTopCorner, radius);
120 CGContextAddLineToPoint(c, roundedRect.xRight, roundedRect.yBottomCorner);
121 CGContextAddArcToPoint(c, roundedRect.xRight, roundedRect.yBottom, roundedRect.xRightCorner, roundedRect.yBottom, radius);
122 CGContextAddLineToPoint(c, roundedRect.xLeft, roundedRect.yBottom);
123
124 CGContextClosePath(c);
125 }
126
127 void SCContextAddRightTopRoundedRect(CGContextRef c, CGRect rect, CGFloat radius) {
128
129 SCRoundedRect roundedRect = SCRoundedRectMake(rect, radius);
130
131 CGContextBeginPath(c);
132 CGContextMoveToPoint(c, roundedRect.xLeft, roundedRect.yTop);
133
134 CGContextAddLineToPoint(c, roundedRect.xRightCorner, roundedRect.yTop);
135 CGContextAddArcToPoint(c, roundedRect.xRight, roundedRect.yTop, roundedRect.xRight, roundedRect.yTopCorner, radius);
136 CGContextAddLineToPoint(c, roundedRect.xRight, roundedRect.yBottom);
137 CGContextAddLineToPoint(c, roundedRect.xLeft, roundedRect.yBottom);
138
139 CGContextClosePath(c);
140 }
141
142 void SCContextAddRightBottomRoundedRect(CGContextRef c, CGRect rect, CGFloat radius) {
143
144 SCRoundedRect roundedRect = SCRoundedRectMake(rect, radius);
145
146 CGContextBeginPath(c);
147 CGContextMoveToPoint(c, roundedRect.xLeft, roundedRect.yTop);
148
149 CGContextAddLineToPoint(c, roundedRect.xRight, roundedRect.yTop);
150 CGContextAddLineToPoint(c, roundedRect.xRight, roundedRect.yBottomCorner);
151 CGContextAddArcToPoint(c, roundedRect.xRight, roundedRect.yBottom, roundedRect.xRightCorner, roundedRect.yBottom, radius);
152 CGContextAddLineToPoint(c, roundedRect.xLeft, roundedRect.yBottom);
153
154 CGContextClosePath(c);
155 }
Something went wrong with that request. Please try again.