
/* Generated by Interface Builder */

#import "CompView.h"

@implementation CompView

#define COPY 0
#define SOVER 1

- initFrame:(const NXRect *)theFrame
{
    [super initFrame:theFrame];
    currentPoint.x=oldPoint.x=bounds.size.width/2.0;
    currentPoint.y=oldPoint.y=bounds.size.height/2.0;
    sqSide=100.;
    compType=NX_COPY;
    [self setSqImage];			//create image of square
    bgImage=[[NXImage alloc] initSize:&bounds.size];
    backgroundGray=NX_WHITE;
    [self setBgImage];			//create background image
    return self;
}

- (float)backgroundGray
{
    return backgroundGray;
}

- takeBackgroundGrayFrom:sender
{
    backgroundGray = [sender floatValue];
    [self setBgImage];			//reset background image with new gray
    [self display];
    return self;
}

- compositeType:sender
{
    int comp;
    comp=[[sender selectedCell] tag];	// read the composite type selected
    if (comp == COPY) compType=NX_COPY;
    if (comp == SOVER) compType=NX_SOVER;
    return self;
}

- setSqImage
{   // This creates the squarewave image that is moved around
    int	i, length = sqSide/8;
	
    NXSetRect(&sqRect,0.0,0.0,3*sqSide,sqSide);        //init the bounding area
    sqImage=[[NXImage alloc] initSize:&sqRect.size];
    [sqImage lockFocus];
		[sqImage composite:NX_CLEAR fromRect:&sqRect toPoint:&sqRect.origin];
		PSsetgray(NX_BLACK);
		PSsetlinewidth(5.0);

		PSmoveto(sqRect.origin.x+2.25, sqRect.origin.y+2.25);
		for(i = 0; i<12; i++){
			PSrlineto(length, 0.0);
			PSrlineto(0.0, 8*length);
			PSrlineto(length, 0.0);
			PSrlineto(0.0, -8*length);
		}
		PSstroke();
    [sqImage unlockFocus];

/* Now set up square to move FROM in background */
    oldRect.size=sqRect.size;		//The old square is always this size
    oldRect.origin.x=currentPoint.x;	//These are used only at initialization 
    oldRect.origin.y=currentPoint.y;
    return self;
}

- setBgImage
{   //This creates the background image
    [bgImage lockFocus];
        PSsetgray(backgroundGray);
        NXRectFill(&bounds);
    [bgImage unlockFocus];
    return self;
}

- drawSelf:(const NXRect *)r :(int)c
{   //Here we just display the entire background, then the squarewave
    [bgImage composite:NX_COPY toPoint:&bounds.origin];
    [sqImage composite:compType toPoint:&currentPoint];
    return self;
}

- moveSquare
{
   [self lockFocus];
        /* First, erase by copying background rectangle on top of it */
        [bgImage composite:NX_COPY fromRect:&oldRect toPoint:&oldRect.origin];
        /* Now composite squarewave to new location */
        [sqImage composite:compType toPoint:&currentPoint];
    [self unlockFocus];
    [window flushWindow];			// flush the window buffer to screen

    return self;
}
    
- mouseDownAction:(NXPoint *)currentLocation
{	
    [self mouseDraggedAction:currentLocation];  //now do same as when dragging
    return self;
}

- mouseDraggedAction:(NXPoint *)currentLocation
{   // Move square to follow the mouse
    oldRect.origin=currentPoint;
    xOffset = sqRect.size.width/2;	// set the offsets for movement wrt the center
    yOffset = sqRect.size.height/2;
    currentPoint.x  = currentLocation->x - xOffset; // new x and y locations are center
    currentPoint.y  = currentLocation->y - yOffset; // of drawing

    [self moveSquare];
    return self;
}


@end
