/*
 *	Written by Joe Freeman	2/91
 *
 *	Simple class that hacks the Matel PowerGlove into ImagePaint
 *
 *	This sets up  a timed entry that causes the program to poll the 
 *	current glove location.  That location is then mapped to the screen 
 *	to allow the user to paint, as if using the mouse.
 *
 */
 
/* Generated by Interface Builder */

#import "ImageWithGlove.h"
#import "PowerGlove.h"
#import <c.h>
#import <appkit/Application.h>
#import <appkit/Slider.h>
#import <dpsclient/psops.h>
#import <dpsclient/wraps.h>
#import <appkit/Button.h>

@implementation ImageWithGlove

void timeHandler(DPSTimedEntry teNumber, double now, void *userData)
{
	[(id)userData getGloveData];
}

- initFrame:(const NXRect *)theFrame
{
    [super initFrame:theFrame];
    powerGlove = [[PowerGlove alloc] init];
    [powerGlove setxHyst:0 yHyst:0 zHyst:0];

    timerNum = DPSAddTimedEntry(0.10, 
    				&timeHandler, 
				self,
				NX_BASETHRESHOLD);
    return self;
}

/* need to get the current glove position and fake like we are an event */
/* convert glove position to be full scale in this view */
- getGloveData
{
    NXEvent	temp;
    static NXEvent	last;
    float tempSize;
    
    if ([powerGlove thumb]>1){
    	[brushDisplay performClick:self];
    }else if ([powerGlove index]>1){
	tempSize = [brushSize floatValue]+ [brushSize maxValue]* 0.05;
	if (tempSize > [brushSize maxValue]) 
		tempSize = tempSize - [brushSize maxValue];
    	[brushSize setFloatValue:tempSize];
	[self drawBrushImage:self];
    }
    if ([powerGlove middle]>1 || [powerGlove ring] >1){
    	//fprintf(stderr,"%d,%d-",[powerGlove middle],[powerGlove ring]);
    	/* mouse down or mouse dragged */
	if (last.type == NX_MOUSEDOWN || last.type == NX_MOUSEDRAGGED) {
		temp.type = NX_MOUSEDRAGGED;
		temp.location.x = last.location.x + [powerGlove gloveX]/4.0;
		temp.location.y = last.location.y + [powerGlove gloveY]/4.0;
		temp.location.x = MAX (temp.location.x, bounds.origin.x);
		temp.location.x = MIN (temp.location.x, NX_MAXX(&bounds));
		temp.location.y = MAX (temp.location.y, bounds.origin.y);
		temp.location.y = MIN (temp.location.y, NX_MAXY(&bounds));
	 }else {
	 	temp.type = NX_MOUSEDOWN;
		PScurrentmouse([window windowNum], 
				&temp.location.x,&temp.location.y);
		[self convertPoint:&temp.location fromView:nil];
	}
    } else if (last.type == NX_MOUSEDOWN || last.type == NX_MOUSEDRAGGED){
    	temp.type = NX_MOUSEUP;
    	temp.location.x = last.location.x;
	temp.location.y = last.location.y;
    } else {
    	temp.type = NX_NULLEVENT;
    }
    if (temp.type == NX_MOUSEDOWN)
	[self mouseDownAction:&temp.location];
    else if (temp.type == NX_MOUSEUP)
	[self mouseUpAction:&temp.location];
    else if (temp.type == NX_MOUSEDRAGGED)
	[self mouseDraggedAction:&temp.location];
    last = temp;
    
    return self;
}



@end
