#import "ControllerView.h"

@implementation ControllerView

//This routine is called at app launch time when this class is unpacked from the nib.
//We get set up here.
-(void)awakeFromNib
{
    //load the images we'll use from the bundle's Resources directory
    _background = [NSImage imageNamed:@"base"];
    _cacheEmpty = [NSImage imageNamed:@"cache_empty"];
    _cacheFull = [NSImage imageNamed:@"cache_full"];
    _drawer = [NSImage imageNamed:@"drawer"];
    //[self setFrameSize:NSMakeSize(184, 400)];
    
    //tell ourselves that we need displaying
    [self setNeedsDisplay:YES];
}

- (void)resetCursorRects {
    [self addCursorRect:[self bounds] cursor:[NSCursor arrowCursor]];
}

//When it's time to draw, this routine is called.
//This view is inside the window, the window's opaqueness has been turned off,
//and the window's styleMask has been set to NSBorderlessWindowMask on creation,
//so what this view draws *is all the user sees of the window*.  The first two lines below
//then fill things with "clear" color, so that any images we draw are the custom shape of the window,
//for all practical purposes.  Furthermore, if the window's alphaValue is <1.0, drawing will use
//transparency.
-(void)drawRect:(NSRect)rect
{
/*    //erase whatever graphics were there before with clear
    [[NSColor clearColor] set];
    NSRectFill([self frame]);
    //if our window transparency is >0.7, we decide to draw the circle.  Otherwise, draw the pentagram.
    //If we called -disolveToPoint:fraction: instead of -compositeToPoint:operation:, then the image
    //could itself be drawn with less than full opaqueness, but since we're already setting the alpha
    //on the entire window, we don't bother with that here.
    //if ([[self window] alphaValue]>0.7)
    //[circleImage compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver];
    //else
    //[pentaImage compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver];
    float width = [_cacheEmpty size].width/2.0;
    [_cacheEmpty compositeToPoint:NSMakePoint(width, 0)
     fromRect:NSMakeRect(width, 0, width, width)
     operation:NSCompositeSourceOver];
    [_cacheFull compositeToPoint:NSMakePoint(0, 0)
     fromRect:NSMakeRect(0, 0, width, width)
     operation:NSCompositeSourceOver];
    [_drawer compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver];
    [_background compositeToPoint:NSMakePoint(0, 100) operation:NSCompositeSourceOver];
    //the next line resets the CoreGraphics window shadow (calculated around our custom window shape content)
    //so it's recalculated for the new shape, etc.  The API to do this was introduced in 10.2.
    if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_1)
    {
        [[self window] setHasShadow:NO];
        [[self window] setHasShadow:YES];
    }
    else
        [[self window] invalidateShadow];*/
}

- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent {
    return TRUE;
}

@end
