-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSFSelectorObject.m
More file actions
203 lines (164 loc) · 5.58 KB
/
SFSelectorObject.m
File metadata and controls
203 lines (164 loc) · 5.58 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// SFSelectorObject.m
// SilverFlow
//
// Created by Julius Eckert on 07.05.08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "SFSelectorObject.h"
@implementation SFSelectorObject
- (id)initWithLayer:(CALayer*)pLayer asSelector:(int)sel {
if (self = [super init]) {
selector = sel;
curSelector = 1;
parentLayer = pLayer;
sfImage = nil;
[self setupLayers];
[self updatePosition];
}
return self;
}
-(void) redraw {
[CATransaction flush];
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
forKey:kCATransactionDisableActions];
[mainLayer setNeedsDisplay];
[CATransaction commit];
}
- (void)setImage:(NSImage*)img {
@try {
if (sfImage) [sfImage release];
if (img) {
sfImage = [img retain];
} else sfImage = nil;
[self redraw];
}
@catch (NSException *exception) {
NSLog(@"############ setImage: Caught %@: %@", [exception name], [exception reason]);
}
}
- (void)updatePosition {
// both selectors on the right
if (curSelector == 1) {
textLayer.opacity = 1;
textLayer.anchorPoint = CGPointMake(1, 0.5);
textLayer.alignmentMode = kCAAlignmentRight;
mainLayer.transform = CATransform3DMakeScale(0.5, 0.5, 1);
if (selector == 2) {
mainLayer.position = CGPointMake([parentLayer frame].size.width+90, 130);
mainLayer.zPosition = -200;
textLayer.position = CGPointMake([parentLayer frame].size.width+35, 150);
textLayer.zPosition = -200;
}
if (selector == 3) {
mainLayer.position = CGPointMake([parentLayer frame].size.width+90, 60);
mainLayer.zPosition = -200;
textLayer.position = CGPointMake([parentLayer frame].size.width+35, 80);
textLayer.zPosition = -200;
}
}
// one in the middle, one on the right
if (curSelector == 2) {
if (selector == 2) {
mainLayer.position = CGPointMake([parentLayer frame].size.width/2, 60);
mainLayer.zPosition = -50;
mainLayer.transform = CATransform3DMakeScale(0.8, 0.8, 1);
textLayer.opacity = 0;
}
if (selector == 3) {
mainLayer.position = CGPointMake([parentLayer frame].size.width+90, 130);
mainLayer.zPosition = -200;
mainLayer.transform = CATransform3DMakeScale(0.5, 0.5, 1);
textLayer.opacity = 1;
textLayer.anchorPoint = CGPointMake(1, 0.5);
textLayer.alignmentMode = kCAAlignmentRight;
textLayer.position = CGPointMake([parentLayer frame].size.width+35, 150);
textLayer.zPosition = -200;
}
}
// one on the left, one in the middle
if (curSelector == 3) {
if (selector == 2) {
mainLayer.transform = CATransform3DMakeScale(0.5, 0.5, 1);
mainLayer.position = CGPointMake(0, 30);
mainLayer.zPosition = -100;
textLayer.opacity = 1;
textLayer.anchorPoint = CGPointMake(0, 0.5);
textLayer.alignmentMode = kCAAlignmentLeft;
textLayer.position = CGPointMake(40, 50);
textLayer.zPosition = -100;
}
if (selector == 3) {
mainLayer.position = CGPointMake([parentLayer frame].size.width/2, 60);
mainLayer.zPosition = -50;
mainLayer.transform = CATransform3DMakeScale(0.8, 0.8, 1);
textLayer.opacity = 0;
}
}
}
- (void)setText:(NSString*)text {
[CATransaction flush];
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
forKey:kCATransactionDisableActions];
textLayer.string = text;
[CATransaction commit];
}
- (void)setCurrentSelector:(int)sel {
curSelector = sel;
[self updatePosition];
}
- (void)setupLayers {
mainLayer = [CALayer layer];
mainLayer.name = @"mainLayer";
mainLayer.bounds = CGRectMake( 0, 0, 128, 128 );
mainLayer.anchorPoint = CGPointMake(0.5,0);
mainLayer.delegate = self;
mainLayer.position = CGPointMake(0,0);
mainLayer.borderWidth = 0;
[parentLayer addSublayer:mainLayer];
[mainLayer setNeedsDisplay];
textLayer = [CATextLayer layer];
textLayer.name = @"textLayer";
textLayer.string = @"Öffnen";
textLayer.font = [NSFont boldSystemFontOfSize:16];
textLayer.fontSize = 20;
textLayer.bounds = CGRectMake( 0, 0, 150, 40 );
textLayer.alignmentMode = kCAAlignmentLeft;
textLayer.anchorPoint = CGPointMake(0.5, 0.5);
textLayer.position = CGPointMake(0, -40);
textLayer.foregroundColor = CGColorCreateGenericRGB(255, 255, 255, 255);
textLayer.zPosition = 0;
textLayer.opacity = 1.0;
[textLayer setTruncationMode:kCATruncationEnd];
[parentLayer addSublayer:textLayer];
}
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)cgContext;
{
[NSGraphicsContext saveGraphicsState];
NSRect drawingRect = NSRectFromCGRect( CGContextGetClipBoundingBox( cgContext ) );
NSGraphicsContext* context = [NSGraphicsContext graphicsContextWithGraphicsPort:cgContext flipped:NO];
[NSGraphicsContext setCurrentContext:context];
NSBezierPath *cornerEraser = nil;
cornerEraser = [NSBezierPath bezierPath];
[cornerEraser appendBezierPathWithRect:drawingRect]; //RoundedRectangle:drawingRect withRadius:4];
//[cornerEraser addClip];
NSRect topRect, bottomRect;
NSDivideRect(drawingRect, &topRect, &bottomRect, NSHeight(drawingRect) /2, NSMaxYEdge);
[[NSColor colorWithCalibratedRed:0.07 green:0.07 blue:0.07 alpha:1.0] set];
NSRectFill(drawingRect);
[[NSColor colorWithCalibratedRed:0.35 green:0.36 blue:0.35 alpha:0.5] setStroke];
[cornerEraser stroke];
if (sfImage) {
NSRect imgRect = drawingRect;
imgRect.origin.x += 5;
imgRect.origin.y += 5;
imgRect.size.width -= 10;
imgRect.size.height -= 10;
[sfImage setFlipped:false];
[sfImage drawInRect:imgRect fromRect:rectFromSize([sfImage size]) operation:NSCompositeSourceOver fraction:1.0];
}
[NSGraphicsContext restoreGraphicsState];
}
@end