#import #import "Controller.h" @implementation Controller - (void)applicationDidFinishLaunching:(NSNotification *)aNotification; { window = nil; srand48(time(NULL)); [self makeGameWindow]; } #define SLIDERHEIGHT 16 #define MARGIN 6 #define LABELWIDTH 20 - makeGameWindow { NSRect frame; NSView *view; int m = NSTitledWindowMask; int index; NSString *titles[3] = {@"x", @"y", @"z"}; ROT_ACTIVE tags[3] = { ROT_X, ROT_Y, ROT_Z }; NSBox *boardBox; NSRect contentRect; NSFont *font = [NSFont systemFontOfSize:SLIDERHEIGHT-3]; view = [[NSView alloc] initWithFrame: NSMakeRect(0, 0, DIMENSION, DIMENSION+SLIDERHEIGHT)]; rview = [[Rubik alloc] initAtPoint:NSMakePoint(0, 3*SLIDERHEIGHT+MARGIN) controller:self]; [view addSubview:rview]; for(index=0; index<3; index++){ NSTextField *textField; NSSlider *slider; textField = [[NSTextField alloc] initWithFrame: NSMakeRect(0, SLIDERHEIGHT*(2-index), LABELWIDTH, SLIDERHEIGHT)]; [textField setEditable:NO]; [textField setSelectable:NO]; [textField setBackgroundColor:[NSColor blackColor]]; [textField setTextColor:[NSColor whiteColor]]; [textField setStringValue:titles[index]]; [textField setFont:font]; [view addSubview:textField]; slider = sliders[index] = [[NSSlider alloc] initWithFrame: NSMakeRect(LABELWIDTH+3, SLIDERHEIGHT*(2-index), DIMENSION-LABELWIDTH-6, SLIDERHEIGHT)]; [slider setMinValue:0.0]; [slider setMaxValue:(2.0*M_PI)]; [slider setTarget:rview]; [slider setAction:@selector(angle:)]; [slider setTag:tags[index]]; [view addSubview:slider]; } boardBox = [[NSBox alloc] initWithFrame: NSMakeRect(0, 0, DIMENSION, DIMENSION)]; [boardBox setContentView:view]; [boardBox setContentViewMargins:NSMakeSize(MARGIN, MARGIN)]; [boardBox setTitle:@"Board"]; [boardBox setBorderType:NSGrooveBorder]; [boardBox sizeToFit]; frame = [NSWindow frameRectForContentRect:[boardBox frame] styleMask:m]; window = [[NSWindow alloc] initWithContentRect:frame styleMask:m backing:NSBackingStoreRetained defer:NO]; [window setMinSize:frame.size]; [window setTitle:@"Rubik's cube"]; [window setDelegate:self]; [window setFrame:frame display:YES]; [window setMaxSize:frame.size]; [window setContentView:boardBox]; [window setReleasedWhenClosed:YES]; // RELEASE(view); [window center]; [window orderFrontRegardless]; [window makeKeyWindow]; [window display]; return self; } - resetSliders { int s; for(s=0; s<3; s++){ [sliders[s] setFloatValue:0.0]; } return self; } - scramble:(id)sender { [rview resetCube]; [rview recomputeGeometry]; [rview scramble]; [rview setNeedsDisplay:YES]; [self resetSliders]; return self; } - restore:(id)sender; { [rview resetCube]; [rview recomputeGeometry]; [rview setNeedsDisplay:YES]; [self resetSliders]; return self; } @end