1
2#import <time.h>
3
4#import "Controller.h"
5
6@implementation Controller
7
8- (void)applicationDidFinishLaunching:(NSNotification *)aNotification;
9{
10    window = nil;
11
12    srand48(time(NULL));
13
14    [self makeGameWindow];
15}
16
17#define SLIDERHEIGHT 16
18#define MARGIN        6
19#define LABELWIDTH   20
20
21
22- makeGameWindow
23{
24    NSRect frame;
25    NSView *view;
26    int m = NSTitledWindowMask;
27
28    int index;
29    NSString *titles[3] = {@"x", @"y", @"z"};
30    ROT_ACTIVE tags[3] = { ROT_X, ROT_Y, ROT_Z };
31
32    NSBox *boardBox;
33    NSRect contentRect;
34    NSFont *font = [NSFont systemFontOfSize:SLIDERHEIGHT-3];
35
36    view  = [[NSView alloc]
37                initWithFrame:
38                    NSMakeRect(0, 0, DIMENSION, DIMENSION+SLIDERHEIGHT)];
39
40    rview = [[Rubik alloc]
41                initAtPoint:NSMakePoint(0, 3*SLIDERHEIGHT+MARGIN)
42                controller:self];
43    [view addSubview:rview];
44
45
46    for(index=0; index<3; index++){
47        NSTextField *textField;
48        NSSlider *slider;
49
50        textField =
51            [[NSTextField alloc]
52                initWithFrame:
53                    NSMakeRect(0, SLIDERHEIGHT*(2-index),
54                               LABELWIDTH, SLIDERHEIGHT)];
55        [textField setEditable:NO];
56        [textField setSelectable:NO];
57        [textField setBackgroundColor:[NSColor blackColor]];
58        [textField setTextColor:[NSColor whiteColor]];
59        [textField setStringValue:titles[index]];
60        [textField setFont:font];
61        [view addSubview:textField];
62
63        slider = sliders[index] =
64            [[NSSlider alloc]
65                initWithFrame:
66                    NSMakeRect(LABELWIDTH+3,
67                               SLIDERHEIGHT*(2-index),
68                               DIMENSION-LABELWIDTH-6,
69                               SLIDERHEIGHT)];
70        [slider setMinValue:0.0];
71        [slider setMaxValue:(2.0*M_PI)];
72        [slider setTarget:rview];
73        [slider setAction:@selector(angle:)];
74        [slider setTag:tags[index]];
75        [view addSubview:slider];
76    }
77
78
79    boardBox =
80        [[NSBox alloc]
81            initWithFrame:
82                NSMakeRect(0, 0, DIMENSION, DIMENSION)];
83    [boardBox setContentView:view];
84    [boardBox setContentViewMargins:NSMakeSize(MARGIN, MARGIN)];
85    [boardBox setTitle:@"Board"];
86    [boardBox setBorderType:NSGrooveBorder];
87    [boardBox sizeToFit];
88
89    frame = [NSWindow frameRectForContentRect:[boardBox frame]
90                      styleMask:m];
91
92    window = [[NSWindow alloc] initWithContentRect:frame
93                                styleMask:m
94                                backing:NSBackingStoreRetained
95                                defer:NO];
96    [window setMinSize:frame.size];
97    [window setTitle:@"Rubik's cube"];
98    [window setDelegate:self];
99
100    [window setFrame:frame display:YES];
101    [window setMaxSize:frame.size];
102
103    [window setContentView:boardBox];
104    [window setReleasedWhenClosed:YES];
105
106    // RELEASE(view);
107
108    [window center];
109    [window orderFrontRegardless];
110    [window makeKeyWindow];
111    [window display];
112
113
114    return self;
115}
116
117- resetSliders
118{
119    int s;
120
121    for(s=0; s<3; s++){
122        [sliders[s] setFloatValue:0.0];
123    }
124
125    return self;
126}
127
128
129- scramble:(id)sender
130{
131    [rview resetCube];
132    [rview recomputeGeometry];
133    [rview scramble];
134    [rview setNeedsDisplay:YES];
135
136    [self resetSliders];
137
138    return self;
139}
140
141- restore:(id)sender;
142{
143    [rview resetCube];
144    [rview recomputeGeometry];
145    [rview setNeedsDisplay:YES];
146
147    [self resetSliders];
148
149    return self;
150}
151
152
153@end
154
155
156