/*
	ARTemis (Graphic Editor for FM-TOWNS)
	(c) MATSUUCHI Ryosuke 1992,1993

	cmdline.c

	｢ライン｣コマンド
*/


#include "ge.h"
#include "imageman.h"
#include "dispman.h"
#include "mask.h"

void commandLine_sub(int type)
// type 0:line   1:boxfill
{
	int step = 0;
	int x1,y1,x2,y2;
	void drawcsr(int msx,int msy)
	{
		MOFF;
		int tx,ty;
		tx = DMimage_getx(msx);
		ty = DMimage_gety(msy);
		if (step==1)
		{
			if (type == 0)
				EIMline(x1,y1,tx,ty,white,DrawXOR);
			else
				EIMboxline(x1,y1,tx,ty,white,DrawXOR);
		}
		MON;
	}
	for(;;)
	{
		int prex,prey;
		DMdispcsr(ms.x,ms.y);
		drawcsr((prex=ms.x),(prey=ms.y));
		do
		{
			ms_get(&ms);
		} while (   ms.dx==0 && ms.dy==0 && ms.btn1==OFF && ms.btn2==OFF
				 && key_chk()==0);
		DMerasecsr();
		drawcsr(prex,prey);			// 消去
		scrollForCsr(1,1);
		if (ms.btn1==OFFON)
		{
			if (step==0) 			// 始点指定
			{
				step=1;
				x1=DMimage_getx(ms.x);
				y1=DMimage_gety(ms.y);
			}
			else if (step==1) 		// 終点指定
			{
				EIMbackup();
				x2=DMimage_getx(ms.x), y2=DMimage_gety(ms.y);
				if (type == 0)
				{
					if (!ryosuke)
						lineWithPen(x1,y1,x2,y2,forecol,getcurpen(),YES);
					else
						aaline(x1,y1,x2,y2,forecol,getcurpen(),256);
				}
				else
					EIMboxfill(x1,y1,x2,y2,forecol,blkop);
				if (type == 0)
					x1=x2, y1=y2;
				else
					step=0;
			}
		}
		if (ms.btn2==OFFON)
		{
			if (step==0)
				break;
			else if (step==1)
				step=0;
		}
	}
}


void commandLine()
{
	commandLine_sub(0);
}


void commandBoxfill()
{
	for (;;)
	{
		if (area_input(AREA_BOX) != 0)
			break;
		EIMbackup();
		int ax1,ay1,ax2,ay2;
		area_getboundxy(&ax1,&ay1,&ax2,&ay2);
		int x,y,mix;
		mix = getmixrate();
		ax1 = _lim(ax1, 0, EIMgetxsize());
		ax2 = _lim(ax2, 0, EIMgetxsize());
		ay1 = _lim(ay1, 0, EIMgetysize());
		ay2 = _lim(ay2, 0, EIMgetysize());
		for (y=ay1; y<=ay2; y++)
		{
			x = ax1 + area_chkxylen(ax1,ax2,y,YES);
			while (x<=ax2)
			{
				int l;
				if ((l = area_chkxylen(x,ax2,y,NO)) == 0)
					break;
				EIMgrayhline(x,x+l-1,y,forecol,mix,NO);
				x += l + area_chkxylen(x+l,ax2,y,YES);
			}
		}
	}
}


/* end of cmdline.c */
