Проблема NSColorWell и Cocos2D Layer
Я стараюсь динамически менять цвет фона на лету, используя NSColorWell. Я использую Cocos2D Framework для рабочего стола.
У меня есть GLView, который показывает окно cocos2d, и у меня есть NSColorwell в стороне. Когда я щелкаю по цвету лунки, я хочу, чтобы фон, так сказать, менял цвет на лету.
У меня есть все мои переменные в одном классе под названием настройки. И у моего AppDeletgate есть IBActions для NSColorWell. И Cocos2D GLview живет в HelloWorldLayer.
Когда я выбираю цвет в NSColorWell, я получаю исключение. Есть идеи почему?
Простите за дамп кода, но я хотел, чтобы вы все смогли точно увидеть, что я делал. Я пробовал много разных способов реализации этого.
AppDelegate.h
#import "cocos2d.h"
@interface AnimatorAppDelegate : NSObject <NSApplicationDelegate>
{
NSWindow *window_;
MacGLView *glView_;
@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet MacGLView *glView;
- (IBAction)bgColorWell:(id)sender;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "HelloWorldLayer.h"
#import "Settings.h"
@implementation AnimatorAppDelegate
@synthesize window=window_, glView=glView_;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];
[director setDisplayFPS:YES];
[director setOpenGLView:glView_];
[director setResizeMode:kCCDirectorResize_AutoScale];
[window_ setAcceptsMouseMovedEvents:NO];
[director runWithScene:[HelloWorldLayer scene]];
}
#pragma mark AppDelegate - IBActions
- (IBAction)toggleFullScreen: (id)sender
{
CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];
[director setFullScreen: ! [director isFullScreen] ];
}
- (IBAction)bgColorWell:(id)sender {
NSLog(@"Color Well %@", [sender color]);
HelloWorldLayer *layer = [[HelloWorldLayer alloc] init];
[layer setBackgroundColorForLayer:[sender color]];
}
@end
Settings.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface Settings : NSObject {
NSColor *_backgroundColor;
}
+ (Settings *) sharedSettings;
- (NSColor *) returnBackgroundColor;
- (void) setBackgroundColor : (NSColor *)c;
@end
Settings.m
#import "Settings.h"
@implementation Settings
static Settings * _sharedSettings;
- (id) init {
if (self = [super init]){
//_backgroundColor = [NSColor colorWithDeviceRed:102/255.0f green:205/255.0f blue:170/255.0f alpha:1.0];
_backgroundColor = [NSColor blueColor];
}
return self;
}
+ (Settings *) sharedSettings {
if (!_sharedSettings) {
_sharedSettings = [[Settings alloc] init];
}
return _sharedSettings;
}
- (NSColor *) returnBackgroundColor {
return _backgroundColor;
}
- (void) setBackgroundColor : (NSColor *)c {
_backgroundColor = c;
}
@end
HelloWorldLayer.h
#import "cocos2d.h"
#import "Settings.h"
// HelloWorldLayer
@interface HelloWorldLayer : CCLayer
{
}
// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
- (void) setBackgroundColorForLayer : (id) sender;
@end
HelloWorldLayer.m
// Import the interfaces
#import "AppDelegate.h"
#import "HelloWorldLayer.h"
#import "Settings.h"
// HelloWorldLayer implementation
@implementation HelloWorldLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
}
return self;
}
- (void) setBackgroundColorForLayer : (id) sender {
Settings *mySettings = [Settings sharedSettings];
float red = [mySettings returnBackgroundColor].redComponent * 255;
float green = [mySettings returnBackgroundColor].greenComponent * 255;
float blue = [mySettings returnBackgroundColor].blueComponent * 255;
CCLayerColor* colorLayer = [CCLayerColor layerWithColor:ccc4(red, green, blue, 255)];
[self addChild:colorLayer z:0];
NSLog(@"Red color: %f", red);
NSLog(@"Green color: %f", green);
NSLog(@"Blue color: %f", blue);
//[self setBackgroundColorForLayer:sender];
NSLog(@"This Background color is %@" , [mySettings returnBackgroundColor]);
}