/
nsimage.jstalk
79 lines (55 loc) · 2.71 KB
/
nsimage.jstalk
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
function drawInImage(img, f) {
[img lockFocus]
f();
[img unlockFocus];
}
function makeButton(pressed, size, scale) {
var img = [NSImage imageWithSize:size];
drawInImage(img, function() {
var start = [NSColor colorWithCalibratedRed:0.922 green:0.365 blue:0.224 alpha:1.000];
var end = [NSColor colorWithCalibratedRed:0.899 green:0.187 blue:0.000 alpha:1.000];
var outline = [NSColor colorWithCalibratedRed:0.576 green:0.130 blue:0.019 alpha:1.000];
var whiteDrop = [NSColor colorWithCalibratedRed:1 green:1 blue:1 alpha:.2];
var pressedDrop = [NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:.03];
var radius = size.height / 2;
var width = size.width - 2;
var height = size.height - 2;
var bp = [NSBezierPath bezierPathWithRoundedRect:NSMakeRect(.5, .5, width, height) xRadius:radius yRadius:radius];
[whiteDrop set];
[bp stroke];
bp = [NSBezierPath bezierPathWithRoundedRect:NSMakeRect(.5, 1.5, width, height) xRadius:radius yRadius:radius];
var gradient = [[[NSGradient alloc] initWithStartingColor:start endingColor:end] autorelease];
var angle = pressed ? 90 : -90;
[gradient drawInBezierPath:bp angle:angle];
if (pressed) {
[pressedDrop set];
}
else {
[whiteDrop set];
}
var whiteLineStart = NSMakePoint(radius, height + .5);
var whiteLineEnd = NSMakePoint(whiteLineStart.x + (width - radius * 2), whiteLineStart.y);
whiteLineStart.x -= 2;
whiteLineEnd.x += 4;
[NSBezierPath strokeLineFromPoint:whiteLineStart toPoint:whiteLineEnd];
whiteLineStart.x += 2;
whiteLineEnd.x -= 4;
[NSBezierPath strokeLineFromPoint:whiteLineStart toPoint:whiteLineEnd];
[outline set];
[bp stroke];
var atts = {"NSFont":[NSFont systemFontOfSize:14]};
[@"Choose…" drawAtPoint:NSMakePoint(20, 5) withAttributes:atts];
});
return img;
}
var imgRect = NSMakeRect(0, 0, 100, 26);
var up = makeButton(true, imgRect.size, 2);
var down = makeButton(false, imgRect.size, 1);
var both = [NSImage imageWithSize:NSMakeSize(150, 100)];
drawInImage(both, function() {
[[NSColor grayColor] set];
[NSBezierPath fillRect:NSMakeRect(0, 0, 150, 100)];
[up drawAtPoint:NSMakePoint(25, 15) fromRect:imgRect operation:NSCompositeSourceOver fraction:1];
[down drawAtPoint:NSMakePoint(25, 45) fromRect:imgRect operation:NSCompositeSourceOver fraction:1];
});
[JSTImageTools viewNSImage:both inWindowNamed:"buttons"];