//	Copyright 1992  Thomas A. Dilligan
//		All Rights Reserved
//
// For best results, set tabstop=4

#import "GenericShaderInspector.h"

const char *shaderTypes[]={"Unknown","Point","Color","Scalar","String","Surface Shader","Light Shader",
	               "Displacement Shader","Volume Shader","Transformation Shader","Imager Shader"};

@implementation GenericShaderInspector

- browserAction:sender;
{
	int selected;
	int seltype;
	char str[255];
	NXColor color;
	float floatValue;
	RtPoint point;
	char *string;
	selected=[[browser matrixInColumn:0] selectedRow];
	if(selected==[shader shaderArgCount])
	{
			[stringValue setEnabled:NO];
			[colorValue setColor:[shader color]];
			[colorValue setEnabled:YES];
			return self;
	}
	
	seltype=[shader shaderArgType:[shader shaderArgNameAt:selected]];
	switch(seltype)
	{
		case SLO_TYPE_UNKNOWN:
			[stringValue setEnabled:NO];
			[stringValue setStringValue:"Unknown"];
			[colorValue setEnabled:NO];
		break;
		case SLO_TYPE_POINT:
			[shader getShaderArg:[shader shaderArgNameAt:selected] pointValue:&point];
			sprintf(str,"«%3.3f %3.3f %3.3f»",point[0],point[1],point[2]);
			[stringValue setEnabled:YES];
			[stringValue setStringValue:str];
			[colorValue setEnabled:NO];
		break;
		case SLO_TYPE_COLOR:
			[shader getShaderArg:[shader shaderArgNameAt:selected] colorValue:&color];
			[stringValue setEnabled:NO];
			[colorValue setColor:color];
			[colorValue setEnabled:YES];
		break;
		case SLO_TYPE_SCALAR:
			[shader getShaderArg:[shader shaderArgNameAt:selected] floatValue:&floatValue];
			[stringValue setEnabled:YES];
			[stringValue setFloatValue:floatValue];
			[colorValue setEnabled:NO];
		case SLO_TYPE_STRING:
			[shader getShaderArg:[shader shaderArgNameAt:selected] stringValue:&string];
			[stringValue setEnabled:YES];
			[stringValue setStringValue:string];
			[colorValue setEnabled:NO];
		break;
		case SLO_TYPE_SURFACE:
		case SLO_TYPE_LIGHT:
		case SLO_TYPE_DISPLACEMENT:
		case SLO_TYPE_VOLUME:
		case SLO_TYPE_TRANSFORMATION:
		case SLO_TYPE_IMAGER:
			[stringValue setEnabled:NO];
			[stringValue setStringValue:"Unable to Change"];
			[colorValue setEnabled:NO];
		break;
	}
	return self;
}

- buttonsAction:sender;
{
	if(![[sender selectedCell] tag])
		[shader initWithShader:[shader shader]];
	[NXApp stopModal:[[sender selectedCell] tag]];
	return self;
}

- colorValueAction:sender;
{
	if([[browser matrixInColumn:0] selectedRow]==[shader shaderArgCount])
		[shader setColor:[sender color]];
	else
		if([shader shaderArgType:[shader shaderArgNameAt:[[browser matrixInColumn:0] selectedRow]]]==SLO_TYPE_COLOR)
			[shader setShaderArg:[shader shaderArgNameAt:[[browser matrixInColumn:0] selectedRow]] colorValue:[colorValue color]];
	return self;
}

- stringValueAction:sender;
{
	int selected;
	int seltype;
	float floatValue;
	RtPoint point;
	char *string;
	char *tmp;
	selected=[[browser matrixInColumn:0] selectedRow];
	seltype=[shader shaderArgType:[shader shaderArgNameAt:selected]];
	switch(seltype)
	{
		default:
			NXRunAlertPanel("Whoops!","Non fatal error #1\nPlease notify developer.",NULL,NULL,NULL);
		break;
		case SLO_TYPE_POINT:
			tmp=string=(char *)[stringValue stringValue];
			for(;*tmp; tmp++)
				if((*tmp<'0' || *tmp>'9') && *tmp !='.')
					*tmp=' ';
			if(sscanf(string,"%f %f %f",&point[0],&point[1],&point[2])==3)
				[shader setShaderArg:[shader shaderArgNameAt:[[browser matrixInColumn:0] selectedRow]] pointValue:point];			
			else
				NXRunAlertPanel("Whoops!","Unable to read in point.",NULL,NULL,NULL);
		break;
		case SLO_TYPE_SCALAR:
			if(sscanf([stringValue stringValue],"%f",&floatValue)==1)
				[shader setShaderArg:[shader shaderArgNameAt:[[browser matrixInColumn:0] selectedRow]] floatValue:floatValue];			
			else
				NXRunAlertPanel("Whoops!","Unable to read in scalar.\n",NULL,NULL,NULL);
		break;
		case SLO_TYPE_STRING:
				[shader setShaderArg:[shader shaderArgNameAt:[[browser matrixInColumn:0] selectedRow]] stringValue:[stringValue stringValue]];			
		break;
	}
	return self;
}

- (int)browser:sender getNumRowsInColumn:(int)column;
{
		return [shader shaderArgCount]+1;
}

- browser:sender loadCell:cell atRow:(int)row inColumn:(int)column;
{
	char txt[256];
	[cell setLeaf:YES];
	[cell setTarget:self];
	[cell setAction:@selector(browserAction:)];
	[cell setTag:row];
	if(row < [shader shaderArgCount])
	{
		sprintf(txt,"%s (%s)",[shader shaderArgNameAt:row],shaderTypes[[shader shaderArgType:[shader shaderArgNameAt:row]]]);
		[cell setStringValue:txt];
	}
	else
		[cell setStringValue:"shadercolor (Color)"];
	return self;
}

- showSelfAndInspect:aShader;
{
	shader=aShader;

	[browser setDelegate:self];
	[browser setTarget:self];
	[browser setAction:@selector(browserAction:)];
	[buttons setTarget:self];
	[buttons setAction:@selector(buttonsAction:)];
	[colorValue setTarget:self];
	[colorValue setAction:@selector(colorValueAction:)];
	[stringValue setTarget:self];
	[stringValue setAction:@selector(stringValueAction:)];
	[stringValue setEnabled:NO];
	[colorValue setEnabled:NO];
	[colorValue setColor:NX_COLORWHITE];
	[stringValue setStringValue:" "];
	[browser loadColumnZero];
	[window orderFront:self];
	[NXApp runModalFor:window];
	[window close];
	return self;
}
@end
