forked from pmougin/F-Script
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFSDemoController.m
144 lines (125 loc) · 6.13 KB
/
FSDemoController.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
/* FSDemoController.m Copyright (c) 2007 Philippe Mougin. */
/* This software is open source. See the license. */
#import "FSDemoController.h"
#import "FSNSString.h"
@implementation FSDemoController
- (IBAction)drawLine:sender
{
[self putCommand:@"p := NSBezierPath bezierPath. p moveToPoint:0<>0; lineToPoint:300<>300; stroke."];
}
- (IBAction)drawCircle:sender
{
[self putCommand:@"(NSBezierPath bezierPathWithOvalInRect:(400<>300 extent:200<>200)) stroke."];
}
- (IBAction)converter:sender
{
[self putCommand:@"\n\"--- F-SCRIPT CURRENCY CONVERTER ---\"\n"
@"\n"
@"\"--- Instantiate and configure the window ---\"\n"
@"\n"
@"window := NSWindow alloc initWithContentRect:(125<>513 extent:383<>175)\n"
@" styleMask:NSTitledWindowMask + NSClosableWindowMask\n"
@" backing:NSBackingStoreBuffered defer:NO.\n"
@"\n"
@"\"--- Create the script that will compute the currency conversion ---\"\n"
@"\n"
@"conversionScript := [(form cellAtIndex:2) setStringValue:(form cellAtIndex:0) floatValue * (form cellAtIndex:1) floatValue].\n"
@"\n"
@"\"--- Instantiate and configure the form ---\"\n"
@"\n"
@"form := NSForm alloc initWithFrame:(15<>70 extent:348<>85).\n"
@"form addEntry:@{'Exchange Rate per $1','Dollars to Convert','Amount in Other Currency'}.\n"
@"form setInterlineSpacing:9; setAutosizesCells:YES; setTarget:conversionScript; setAction:#value.\n"
@"\n"
@"\"--- Instantiate and configure the button ---\"\n"
@"\n"
@"button := NSButton alloc initWithFrame:(247<>15 extent:90<>30).\n"
@"button setBezelStyle:NSRoundedBezelStyle; setTitle:'Convert'; setTarget:conversionScript;\n"
@"setAction:#value; setKeyEquivalent:'\\r'.\n"
@"\n"
@"\"--- Instantiate and configure the decorative line ---\"\n"
@"\n"
@"line := NSBox alloc initWithFrame:(15<>59 extent:353<>2).\n"
@"\n"
@"\" ---Put the various components in the window and put the window onscreen ---\"\n"
@"\n"
@"window contentView addSubview:@{form, button, line}.\n"
@"window setTitle:'Currency Converter'; orderFront:nil.\n"];
}
- (IBAction)loadImage:sender
{
[self putCommand:@"image := CIImage imageWithContentsOfURL:(NSURL fileURLWithPath:'/Users/pmougin/Desktop/Clown Fish.png')."];
}
- (IBAction)displayImage:sender
{
[self putCommand:@"image drawInRect:(200<>150 extent:image extent extent) fromRect:image extent operation:NSCompositeSourceOver fraction:1."];
}
- (IBAction)perspective:sender
{
[self putCommand:@"perspective := CIFilter filterWithName:'CIPerspectiveTransform'.\n"
@"perspective setValue:(CIVector vectorWithX:0 Y:306) forKey:'inputTopLeft'.\n"
@"perspective setValue:(CIVector vectorWithX:490 Y:306) forKey:'inputTopRight'.\n"
@"perspective setValue:(CIVector vectorWithX:590 Y:0) forKey:'inputBottomRight'.\n"
@"perspective setValue:(CIVector vectorWithX:-100 Y: 0) forKey:'inputBottomLeft'.\n"
@"\n"
@"perspective setValue:image forKey:'inputImage'.\n"
@"image := perspective valueForKey:'outputImage'."];
}
- (IBAction)hueAdjust:sender
{
[self putCommand:@"hueAdjust := CIFilter filterWithName:'CIHueAdjust'.\n"
@"hueAdjust setValue:2.094 forKey:'inputAngle'.\n"
@"\n"
@"hueAdjust setValue:image forKey:'inputImage'.\n"
@"image := hueAdjust valueForKey:'outputImage'."];
}
- (IBAction)bump:sender
{
[self putCommand:@"bump := CIFilter filterWithName:'CIBumpDistortion'.\n"
@"bump setValue:(CIVector vectorWithX:300 Y:150) forKey:'inputCenter'.\n"
@"bump setValue:130 forKey:'inputRadius'.\n"
@"bump setValue:0.7 forKey:'inputScale'.\n"
@"\n"
@"bump setValue:image forKey:'inputImage'.\n"
@"image := bump valueForKey:'outputImage'."];
}
- (IBAction)eges:sender
{
[self putCommand:@"edges := CIFilter filterWithName:'CIEdges'.\n"
@"edges setValue:20 forKey:'inputIntensity'.\n"
@"\n"
@"edges setValue:image forKey:'inputImage'.\n"
@"image := edges valueForKey:'outputImage'.\n"];
}
- (IBAction)bumpAnimate:sender
{
[self putCommand:@"image := CIImage imageWithContentsOfURL:(NSURL fileURLWithPath:'/Users/pmougin/Desktop/Clown Fish.png').\n"
@"window := NSApplication sharedApplication keyWindow.\n"
@"destinationRect := (200<>150 extent:image extent extent).\n"
@"\n"
@"bump := CIFilter filterWithName:'CIBumpDistortion'.\n"
@"bump setValue:130 forKey:'inputRadius'.\n"
@"bump setValue:0.7 forKey:'inputScale'.\n"
@"bump setValue:image forKey:'inputImage'.\n"
@"\n"
@"0 to:600 do:\n"
@"[:i|\n"
@" bump setValue:(CIVector vectorWithX:i Y:150) forKey:'inputCenter'.\n"
@" result := bump valueForKey:'outputImage'.\n"
@" result drawInRect:destinationRect fromRect:image extent operation:NSCompositeSourceOver fraction:1.\n"
@" window flushWindow.\n"
@"].\n"];
}
- (void)putCommand:(NSString *)command
{
NSArray *fragments = [command componentsSeparatedByString:@" "];
for (unsigned int i = 0, n = [fragments count]; i < n; i++)
{
[interpreterView putCommand:[fragments objectAtIndex:i]];
[interpreterView putCommand:@" "];
[interpreterView display];
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.02]];
}
[[interpreterView window] makeKeyWindow];
}
@end