
#import "LinesView.h"
#import "Thinker.h"
#import <appkit/appkit.h>
#import <dpsclient/wraps.h>
#import <libc.h>
#import <math.h>
#import "LinesView.h"

#define RANDINT(n) (random() % (n+1))		// Return random integer 0..n

#define XVEL corners[count].xVel  // Some slimy shortcuts, asuuming we're
#define YVEL corners[count].yVel  // using "count" as corner counter.
#define XLOC corners[count].xLoc
#define YLOC corners[count].yLoc

#define MAXVEL 12		// Maximum velocity of corners

@implementation LinesView

- initFrame:(NXRect *) frameRect {
	[super initFrame: frameRect];
	drawOp = dps_ustroke;
	[self setNumberOfCorners:self];
    userPath = newUserPath();
	return self;
	}

- (int) intValue {
   return 17;
   }

- setNumberOfCorners:sender
{
    int	   count;
    int    oldNumCorners = numCorners;

  /* set the number of corners based on the "corners" slider */
    numCorners = MIN(MAXNUMCORNERS, MAX([sender intValue], MINNUMCORNERS));

  /* set the new corner starting positions & velocities */
    for (count = oldNumCorners; count < numCorners; count++) {
      XLOC = (int)(bounds.size.width) / 2;      
      YLOC = (int)(bounds.size.height) / 2;      
      XVEL = (RANDINT(4) ? 1 : -1) * (1 + RANDINT(MAXVEL/2));
      YVEL = (RANDINT(4) ? 1 : -1) * (1 + RANDINT(MAXVEL/2));
    }
    [self display];

    return self;
}


- drawSelf:(const NXRect *)rects :(int)rectCount {
    int	  count;
    
  /* fill with the background color */
	PSsetgray(NX_BLACK);
    NXRectFill(&bounds);
    
    PSsetgray(NX_WHITE);
    PSsetlinewidth(0.0);

  /* "plot" the points */
    beginUserPath(userPath, NO);
    for (count = 0; count < numCorners; count++) {
	if (count) {
	    UPlineto(userPath, XLOC, YLOC);
	} else {
	    UPmoveto(userPath, XLOC, YLOC);
		}
    }
    closePath(userPath);


/*	count=0;
*	PSnewpath();
*	PSmoveto(XLOC,YLOC);
*	for(count=1;count < numCorners; count++) {
*		PSlineto(XLOC,YLOC);
*		}
*	PSclosepath();
*	PSeofill();
*/    
  /* draw it */
    //endUserPath(userPath, dps_ustroke);
    endUserPath(userPath, drawOp);
    sendUserPath(userPath);
    
    return self;
	}

- oneStep {
    int count;
	for (count = 0; count < numCorners; count++) {
	    XLOC += XVEL;
	    YLOC += YVEL;

	  /*
	   * Detect collision with sides; if we collide, bounce back in some
	   * random fashion.
	   */
	    if (XLOC >= bounds.size.width) {
		XLOC = bounds.size.width-1;
		XVEL = -1-RANDINT(MAXVEL);
	    } else if (XLOC < bounds.origin.x) {
		XLOC = bounds.origin.x;
		XVEL = 1+RANDINT(MAXVEL);
	    }
	    if (YLOC >= bounds.size.height) {
		YLOC = bounds.size.height-1;
		YVEL = -1-RANDINT(MAXVEL);
	    } else if (YLOC < bounds.origin.y) {
		YLOC = bounds.origin.y;
		YVEL = 1+RANDINT(MAXVEL);
	    }
	}
	[self drawSelf:&bounds :1];
	return self;
	}

- free {
    freeUserPath(userPath);
	return [super free];
	}

- inspector:sender
{
	char blah[256];
	if (!linesInspector) {
		[NXBundle getPath:blah forResource:"lines" ofType:"nib" 
		          inDirectory:[sender moduleDirectory:"Lines"] 
				  withVersion:0];
		[NXApp loadNibFile:blah owner:self withNames:NO];
		}
	return linesInspector;
	}
- setDrawOp: sender {
	int newOp=[[sender selectedCell] tag];
	switch(newOp) {
		case 0:
			drawOp = dps_ustroke;
			break;
		case 1:
			drawOp = dps_ufill;
			break;
		case 2:
			drawOp = dps_ueofill;
			break;
		}
	return self;
	}
	
@end
