Im trying to use custom UiViews over my cocos layers in a game I'm trying to build. When i say custom UIView i want to write and use my own drawing code inside the UIView's draw rect to create the view (using bezier paths). While i know how to add the UiView onto the screen, like so :
[[[CCDirector sharedDirector] view] addSubview:myView];
The drawing code inside my drawRect method doesn't seem to work!
This is how ive setup cocos2d:
- (void)setupCocos2D {
director = (CCDirectorIOS*) [CCDirector sharedDirector];
CCGLView *glView = [CCGLView viewWithFrame:[[UIScreen mainScreen] bounds]
pixelFormat:kEAGLColorFormatRGB565 //kEAGLColorFormatRGBA8
depthFormat:0 //GL_DEPTH_COMPONENT24_OES
preserveBackbuffer:NO
sharegroup:nil
multiSampling:YES
numberOfSamples:1024];
[director setView:glView];
[glView setMultipleTouchEnabled:NO];
[director enableRetinaDisplay:NO];
director.wantsFullScreenLayout = YES;
[director setDisplayStats:YES];
[director setDepthTest:YES];
[director setAnimationInterval:1.0/60];
}
The following is my custom drawing code inside myView's draw rect method:
- (void)drawRect:(CGRect)rect
{ //// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
UIGraphicsBeginImageContext(self.bounds.size);
context = UIGraphicsGetCurrentContext();
//// Color Declarations
UIColor* defaultMidGreen_COLOR = [UIColor colorWithRed: 0.231 green: 0.392 blue: 0.392 alpha: 1];
UIColor* panelGradient_COLOR = [UIColor colorWithRed: 0.231 green: 0.392 blue: 0.392 alpha: 1];
UIColor* default_0Opacity_COLOR = [UIColor colorWithRed: 0.231 green: 0.392 blue: 0.392 alpha: 0];
UIColor* defaultDarkGreen_60Opacity_COLOR = [UIColor colorWithRed: 0.231 green: 0.392 blue: 0.392 alpha: 0.6];
UIColor* iconPlayer_Fill_COLOR = [UIColor colorWithRed: 0.4 green: 0.8 blue: 0.769 alpha: 1];
//// Gradient Declarations
NSArray* bottom_GRADIENTColors = [NSArray arrayWithObjects:
(id)panelGradient_COLOR.CGColor,
(id)[UIColor colorWithRed: 0.231 green: 0.392 blue: 0.392 alpha: 0.5].CGColor,
(id)default_0Opacity_COLOR.CGColor, nil];
CGFloat bottom_GRADIENTLocations[] = {0, 0.55, 1};
CGGradientRef bottom_GRADIENT = CGGradientCreateWithColors(colorSpace, (CFArrayRef)bottom_GRADIENTColors, bottom_GRADIENTLocations);
{
//// panelComponent-BottomSolid Drawing
solidPaths = [[UIBezierPath alloc] init];
[solidPaths moveToPoint: CGPointMake(0, 110)];
[solidPaths addLineToPoint: CGPointMake(540, 110)];
[solidPaths addLineToPoint: CGPointMake(540, 60)];
[solidPaths addLineToPoint: CGPointMake(0, 60)];
[solidPaths addLineToPoint: CGPointMake(0, 110)];
[solidPaths closePath];
[defaultMidGreen_COLOR setFill];
[solidPaths fill];
//// panelComponent-BottomGradient Drawing
gradientPaths = [[UIBezierPath alloc] init];
[gradientPaths moveToPoint: CGPointMake(0, 2)];
[gradientPaths addLineToPoint: CGPointMake(539, 2)];
[gradientPaths addLineToPoint: CGPointMake(539, 60)];
[gradientPaths addLineToPoint: CGPointMake(0, 60)];
[gradientPaths addLineToPoint: CGPointMake(0, 2)];
[gradientPaths closePath];
CGContextSaveGState(context);
[gradientPaths addClip];
outline = [[UIBezierPath alloc ]init];
[outline moveToPoint: CGPointMake(1, 109)];
[outline addLineToPoint: CGPointMake(539, 109)];
[outline addLineToPoint: CGPointMake(539, 0)];
[outline addLineToPoint: CGPointMake(1, 0)];
[outline addLineToPoint: CGPointMake(1, 109)];
[outline closePath];
[defaultDarkGreen_60Opacity_COLOR setFill];
[iconPlayer_Fill_COLOR setStroke];
outline.lineWidth = 1;
gripLines = [[UIBezierPath alloc] init];
[gripLines moveToPoint: CGPointMake(200, 3)];
[gripLines addLineToPoint: CGPointMake(200, 2)];
[gripLines addLineToPoint: CGPointMake(340, 2)];
[gripLines addLineToPoint: CGPointMake(340, 3)];
[gripLines addLineToPoint: CGPointMake(200, 3)];
[gripLines closePath];
[iconPlayer_Fill_COLOR setFill];
[gripLines fill];
}
[solidPaths stroke];
[gripLines stroke];
[outline stroke];
//// Cleanup
UIGraphicsEndImageContext();
CGGradientRelease(bottom_GRADIENT);
CGColorSpaceRelease(colorSpace);
}
Additional Info:
- If i try to set the backgroundColor property inside draw rect, it doesnt work. However the same works if i initialize the object with the color (in the init method)
- I think it has something to with the graphics context im trying to use and a conflict between the one I'm using and the one cocos2D is using! Not sure though!
Any help is appreciated!