/*

	digit.c

dgt1.bat = cl /AL digit.c sketmast mmserial vimage maputila graphlib


*/
#include <stdio.h>
#include <string.h>
#include "font.h"
#include "nomouse.h"

#define MAX_TX 18288
#define MAX_PTS  500
#define MAX_X 640
#define MAX_Y 480

int Mouse=0;
int VideoType='X';
struct board
{
	int row,col,color;
}B_Id;
int ScreenXs=640,ScreenYs=480;
unsigned char Buffer[12][MAX_X];
struct   Color 
{ 
	unsigned char  r, g, b; 
}lut[256],lutm[256],lutc[256],luts[256]; 
struct Color Vga[16]=
{
	{   0,  0,  0},
	{ 255,  0,  0},	/* red */
	{ 255,128,  0},	/* orange */
	{ 160,160,  0},	/* yellow */
	{   0,200,  0},	/* green */
 	{  0,120,  0},		/* d_green */
	{  0, 72,  0},		/* vd_green */
	{   0,  0,255},	/* blue */
	{   0,200,200},	/* cyan */
	{   0,100,100},	/* d_cyan */
	{  46, 46, 46},	/* d_grey */
	{ 128,128,128},	/* m_grey */
	{ 191,191,191},	/* l_grey */
	{ 255,255,255},	/* white */
	{ 130,111, 93},	/* l_buff */
	{  96, 28, 14}		/* buff */
};

unsigned char B_Buff[50][MAX_X];

void set_lut();
void digit_lines_xy();
double dget_num();
int DisplayOn(){};
int DisplayOff(){};

main(argc,argv)

int argc;
char *argv[];

{
	int i,j,k;
	char ans;
	int line;

	init_board(0,argv[1]);
	for(i=1;i<argc;i++)
	{
		if(strcmpi(argv[i],"/v")==0)
			VideoType='V';
		if(strcmpi(argv[i],"/e")==0)
			VideoType='E';
	}
	do
	{
		printf("\n\n\n");
	printf("              /\\                1 = red    \n");
	printf("            / 1  \\              2 = orange \n");
	printf("          / ENTER  \\            3 = yellow \n");
	printf("        /     &      \\          4 = green  \n");
	printf("      / \\ SAME COLOR  /\\        5 = d_green\n");
	printf("    /  2  \\[yellow] /  4 \\      6 = vd_green\n");
	printf("  /         \\     /        \\    7 = blue   \n");
	printf("/             \\ /            \\  8 = cyan   \n");
	printf("\\    CLOSE    / \\     END    /  9 = d_cyan \n");
	printf("  \\ [white] /  3  \\[green] /   10 = d_grey \n");
	printf("    \\     /         \\    /     11 = m_grey \n");
	printf("      \\ /     NEW     \\/       12 = l_grey \n");
	printf("        \\    COLOR   /         13 = white  \n");
	printf("          \\ [blue] /           14 = l_buff \n");
	printf("            \\    /             15 = buff   \n");
	printf("              \\/                       \n");
	printf("\n   l -- plot lines and make an '___.xyc' file -- north up\n");
	printf("   L -- plot lines and make an '___.xyc' file -- north to right\n");
		printf("\n   x -- exit program\n");

		ans=getch();
		if(ans=='l')
			digit_lines_xy(1);
		if(ans=='L')
			digit_lines_xy(2);
	}while(ans!='x');
}


/**************************************************************************
**
**   iop = 1 north up  -- x/y
**   iop = 2 north to right y/-x
**
************************************************************************* */

void digit_lines_xy(iop)

int iop;

{
	int i,j,k;
	FILE *fp,*fpin;
	double lat,lon,x,y;
	int ix,iy,ixo,iyo,ret,lx,ly,lx1,ly1,ix1,iy1;
	char filename[100],string[40],res='q',string2[40],cval;
	float scale=1016.0,fx,fy,tfx,tfy;
	double dval;
	int color,end=0,first=1;
	long offset=-1;

	printf("Do you want to open a new file?  (y or n)\n\n");
	if(getch()=='y')	
	{
		printf("Give output file name.  (type will be made '____.xyc'");
		scanf("%s",filename);
		strcat(filename,".xyc");
		fp=fopen(filename,"wb");
		i=1;
		do
		{
			printf("give Lat and Lon for control point #%d.\n",i);
			printf("      (x to exit control points)    ");
			scanf("%s",string);
			if(string[0]!='x')
			{
				sscanf(string,"%lf",&lat);
				scanf("%lf",&lon);
				Video_On();
				set_lut('c');
				map_a_point(&ix,&iy,res,&lx,&ly,13);
				Video_Off();
				if(iop==1)
					printf("%7.4lf %8.4lf\n%5d %5d\n",lat,lon,lx,ly);
				if(iop==2)
					printf("%7.4lf %8.4lf\n%5d %5d\n",lat,lon,ly,MAX_TX-lx);
				if(iop==1)
					fprintf(fp,"%7.4lf %8.4lf\n%5d %5d\n",lat,lon,lx,ly);
				if(iop==2)
					fprintf(fp,"%7.4lf %8.4lf\n%5d %5d\n",lat,lon,ly,MAX_TX-lx);
			}
			i+=1;
		}while(string[0]!='x');
		fprintf(fp,"*\n");
		Video_On();
		set_lut('c');
	}
	else
	{
		if(get_file_name("*.xyc",filename)<0)
			exit(0);
		fpin=fopen(filename,"rt");
		if(!fpin)
		{
			printf("Could not open '%s'\n\n",filename);
			exit(0);
		}
		fp=fopen("update.xyc","wt");
		if(!fp)
		{
			printf("Could not open 'update.xyc'\n\n");
			exit(0);
		}
		printf("Updated output will go in 'update.xyc'\n\n");
		printf("Hit any key to continue.\n\n");
		getch();
/*
		set_a_point(&ix,&iy,res,&lx,&ly);
		getch();
*/
		Video_On();
		set_lut('c');
		while((cval=fgetc(fpin))!='*')
			fprintf(fp,"%c",cval);
		fprintf(fp,"*\n");
		i=0;
		fscanf(fpin,"%d",&color);
		fprintf(fp,"%6d",color);
		do
		{
			fscanf(fpin,"%s%s",string,string2);
			sscanf(string,"%f",&fx);
			sscanf(string2,"%f",&fy);
			if(i%4==0)
				fprintf(fp,"\n");
			else
				fprintf(fp,"  ");
			fprintf(fp,"%5.0f %5.0f",fx,fy);
			if(iop==2)
			{
				tfx=fx;tfy=fy;
				fx=MAX_TX-tfy;
				fy=tfx;
			}
			ix=fx/29.0+5;
			iy=fy/29.0+30;
			if(i>0)
				plotln(0,ix,iy,ixo,iyo,color);
			ixo=ix;
			iyo=iy;
			i+=1;
			if(string2[strlen(string2)-1]=='/')
			{
				fprintf(fp,"//\n");
				i=0;
				if(fscanf(fpin,"%d",&color)<1)
					end=1;
				else
					fprintf(fp,"%6d",color);
			}
		}while(end==0);
		fclose(fpin);
	}
	color=dval=dget_num("Give line value.","   Enter 0 to exit",
					5,430,350,46,1,1,34);
	do
	{
		if(first==0&&(ret=map_a_point(&ix,&iy,res,&lx,&ly,13))!=1)
			dval=dget_num("Give line value.","   Enter 0 to exit",
					5,430,350,46,1,1,34);
		color=dval;
		first=0;
		if(dval>0)
		{
			fprintf(fp,"%6.0lf",dval);
			i=0;
			while((ret=map_a_point(&ix,&iy,res,&lx,&ly,13))==1)
			{
				if(i==0)
				{
					lx1=lx;
					ly1=ly;
					ix1=ix;
					iy1=iy;
				}
				if(i%4==0)
					fprintf(fp,"\n");
				else
					fprintf(fp,"  ");
				if(iop==1)
					fprintf(fp,"%5d %5d",lx,ly);
				if(iop==2)
					fprintf(fp,"%5d %5d",ly,MAX_TX-lx);
				if(i++>0)
					plotln(3,ix,iy,ixo,iyo,color);
				else
					plotpt(3,ix,iy,12);
				ixo=ix;iyo=iy;
			}
			if(ret==2)
			{
				if(i%4==0)
				{
					if(iop==1)
						fprintf(fp,"\n%5d %5d//\n",lx1,ly1);
					if(iop==2)
						fprintf(fp,"\n%5d %5d//\n",ly1,MAX_TX-lx1);
				}
				else
				{
					if(iop==1)
						fprintf(fp,"  %5d %5d//\n",lx1,ly1);
					if(iop==2)
						fprintf(fp,"  %5d %5d//\n",ly1,MAX_TX-lx1);
				}
				plotln(3,ix1,iy1,ixo,iyo,color);
			}
			else
				fprintf(fp,"//\n");
		}
	}while(dval>0.0);
	Video_Off();
	fclose(fp);
}

/***********************************************************************
**
**
**
***
************************************************************************/

int Video_Off()

{
		SetVideoMode(0,&B_Id);
}


/**************************************************************************
**
**
**
************************************************************************* */

int Video_On()

{
	int i,ret,row;

	i=GetVideoBoardID();
	if(i==0)
		VideoType='E';
	else if(i==1&&(VideoType=='X'||VideoType=='Y'))
		VideoType='V';
	if(VideoType=='V')
	{
		row=SetVideoMode(0x12,&B_Id);
		if(B_Id.row!=480||row!=480||B_Id.color!=16||B_Id.col!=640)
			VideoType='E';
	}
	if(VideoType=='E')
		row=SetVideoMode(0x10,&B_Id);
	if(VideoType=='X')
	{
		SetVideoMode(480,&B_Id);
		if(B_Id.row==480)
			set_lut('c');
		else
		{
			printf("Computer will not support enough colors.\n\n");
			exit(0);
		}
	}
}


/********************************************************************
**
**	type  g -- grey all lut[] files
**			G -- grey only lut[]
**			i -- initial header map
**			r -- reset lut[] to lutc[]
**			p -- positive (0-127) magenta, negative (128-255) green
**
********************************************************************* */

void set_lut(type)

char type;

{
	int k,hue,brite,j;
	int color[3];
	double dbrite,dval;
	float val,minval;

	for(k=0;k<16;k++)
	{
		lutc[k].r=Vga[k].r;
		lutc[k].g=Vga[k].g;
		lutc[k].b=Vga[k].b;
	}
	WritePalette(lutc); 
}


/********************************************************************
**
**
**
********************************************************************* */

double dget_num(string1,string2,xc,yc,xs,ys,save,remove,numenu)

int xc,yc,xs,ys,save,remove,numenu;
char *string1,*string2;

{
	int j,k;
	int i=yc+1;
	int ans;
	int size=2;
	int txc=xc,tyc=yc+31,txs=7,tys=15;
	char string[40],ltr[2];
	unsigned char buff[640];
	double number;

	ltr[1]='\0';

	if(save==1)
		save_box(3,0,B_Buff,xc,yc,xs,ys);
	plot_font_h(12,5,i,string1,size,Font);
	i+=30/size;
	plot_font_h(12,5,i,string2,size,Font);
	i+=30/size;
	k=0;
	do
	{	
		ans=getch();
		if(ans==0)
		{
			if((ans=getch())==59)help(65);
			if(ans==60)help(65);
			ans=getch();
		}
		paint_box(0,0,buff,txc,tyc,txs,tys);
		if((ans>='0'&&ans<='9')||ans=='-'||ans=='.')
		{
			string[k]=ans;
			ltr[0]=ans;
			plot_font_h(12,txc,i,ltr,size,Font);
			k+=1;
			txc+=7;
		}
		if(ans==8&&k>0)
		{
			string[k]='\0';
			k-=1;
			txc-=7;
			paint_box(0,0,buff,txc,tyc,txs,tys);
		}
	}while(ans!=13);
	string[k]='\0';

	sscanf(string,"%lf",&number);

	if(remove==1)
		unsave_box(3,0,B_Buff,xc,yc,xs,ys);
	return(number);
}
                                                                                                              