forked from julius/silverflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSFCFObject.m
178 lines (140 loc) · 5 KB
/
SFCFObject.m
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
//
// SFCFObject.m
// SilverFlow
//
// Created by Julius Eckert on 02.02.08.
// Copyright 2008 Julius Eckert. All rights reserved.
//
#import "SFCFObject.h"
SFCFVector3f SFCFMakeVector3f(float _x, float _y, float _z) { SFCFVector3f v; v.x=_x;v.y=_y;v.z=_z; return v; }
@implementation SFCFObject
-(SFCFObject*) initWithLayer:(CALayer*)layer withImage:(NSImage*)img {
if (self = [super init]) {
parentLayer = layer;
sfImage = nil;
imageStd = img;
creationThread = [NSThread currentThread];
mainLayer1 = [CALayer layer];
mainLayer1.name = @"mainLayer";
mainLayer1.bounds = CGRectMake( 0, 0, 128, 256 );
mainLayer1.anchorPoint = CGPointMake(0.5,0);
mainLayer1.zPosition = 0;
mainLayer1.position = CGPointMake(0,0);
mainLayer1.delegate = self;
mainLayer1.opacity = 1.0;
mainLayer1.shadowOpacity = 0;
mainLayer1.shadowRadius = 10;
[parentLayer addSublayer:mainLayer1];
[self setImage:nil];
[self redraw];
}
return self;
}
-(void) setPosition:(SFCFVector3f)pos fast:(bool)aniFast {
if ((!aniFast) && (mainLayer1.position.y > 2000) && (pos.y < 2000)) {
[self setPosition:pos fast:true];
return;
}
[CATransaction flush];
[CATransaction begin];
if (aniFast == true) {
[CATransaction setValue:(id)kCFBooleanTrue
forKey:kCATransactionDisableActions];
} else {
[CATransaction setValue:[NSNumber numberWithFloat:0.3f]
forKey:kCATransactionAnimationDuration];
}
mainLayer1.position = CGPointMake(pos.x, pos.y - [mainLayer1 bounds].size.height/2);
mainLayer1.zPosition = pos.z - 100; // richtiges wert: 1000
[CATransaction commit];
}
-(SFCFVector3f) position {
return SFCFMakeVector3f(mainLayer1.position.x, mainLayer1.position.y, mainLayer1.zPosition);
}
-(void) setDist:(int)dist fast:(bool)aniFast {
[CATransaction flush];
[CATransaction begin];
if (aniFast == true) {
[CATransaction setValue:(id)kCFBooleanTrue
forKey:kCATransactionDisableActions];
} else {
[CATransaction setValue:[NSNumber numberWithFloat:0.3f]
forKey:kCATransactionAnimationDuration];
}
if (dist == 0) {
mainLayer1.transform = CATransform3DIdentity;
} else if (dist < 0) {
mainLayer1.transform = CATransform3DMakeRotation(-0.95, 0, -1, 0);
} else if (dist > 0) {
mainLayer1.transform = CATransform3DMakeRotation(0.95, 0, -1, 0);
}
[CATransaction commit];
/*/ FILTERS -- SLOW
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
forKey:kCATransactionDisableActions];
if (dist != 0) {
mainLayer1.backgroundFilters = nil;
} else {
CIFilter* filter = [CIFilter filterWithName:@"CIGaussianBlur"];
filter.name = @"myFilter";
mainLayer1.backgroundFilters = [NSArray arrayWithObject:filter];
[mainLayer1 setValue:[NSNumber numberWithInt:2] forKeyPath:@"backgroundFilters.myFilter.inputRadius"];
}
[CATransaction commit];/**/
/*
float o_alpha1 = alpha1;
alpha1 = (dist!=0)?0.6:0.0;
if (o_alpha1 != alpha1) [self redraw];/**/
}
-(void) redraw {
[CATransaction flush];
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
forKey:kCATransactionDisableActions];
[mainLayer1 setNeedsDisplay];
[CATransaction commit];
}
-(void) setImage: (NSImage*)img {
NSImage* before = sfImage;
if (sfImage == img) return;
if (sfImage != nil) [sfImage release];
if (img == nil) sfImage = nil;
else sfImage = [img retain];
//if ((sfImage != before) && (sfImage != nil)) [self redraw];
if (sfImage != before) [self redraw];
}
-(bool) hasImage { return (sfImage != nil); }
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)cgContext;
{
[NSGraphicsContext saveGraphicsState];
NSRect drawingRect = NSRectFromCGRect( CGContextGetClipBoundingBox( cgContext ) );
NSGraphicsContext* context = [NSGraphicsContext graphicsContextWithGraphicsPort:cgContext flipped:false];
[NSGraphicsContext setCurrentContext:context];
//[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
NSRect topRect, bottomRect;
NSDivideRect(drawingRect, &topRect, &bottomRect, drawingRect.size.height/2, NSMaxYEdge);
NSImage* img = imageStd;
if (sfImage) {
img = sfImage;
// Draw Main
[img compositeToPoint:topRect.origin operation:NSCompositeSourceOver];
// Draw Mirror
NSAffineTransform* xform = [NSAffineTransform transform];
[xform translateXBy:0.0 yBy:bottomRect.size.height];
[xform scaleXBy:1.0 yBy:-1.0];
[xform concat];
[img drawAtPoint:bottomRect.origin fromRect:rectFromSize([img size]) operation:NSCompositeSourceOver fraction:1.0];
NSColor* color1 = [NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.0 alpha:1.0];
NSColor* color2 = [NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.0 alpha:.76];
NSGradient* gradient = [[NSGradient alloc] initWithStartingColor:color1 endingColor:color2];
[[NSGraphicsContext currentContext] setCompositingOperation:NSCompositeSourceAtop];
[gradient drawInRect:bottomRect angle:270];
}
else
{
[[NSColor clearColor] set]; NSRectFill(drawingRect);
}/**/
[NSGraphicsContext restoreGraphicsState];
}
@end