/*
	ARTemis (Graphic Editor for FM-TOWNS)
	(c) MATSUUCHI Ryosuke 1992

	plt.c
*/

// #define	DEBUG

#include <stdio.h>
#include <egb.h>

#include "ge.h"
#include "plt16.h"
#include "dispman.h"
#include "mainmenu.h"


/*--------------------------------------------------------*/
/*                        ２色表示                        */
/*--------------------------------------------------------*/

static	bool 	colsdisp = NO;
static	MENU	*cols_menu = NULL;
static	int		cols_item1,cols_item2;
static	int		*cols_1,*cols_2;
static	int		colcsrpos = 0;


void cols_init(int *col1,int *col2, MENU *menu, int itemcol1, int itemcol2)
{
	cols_menu = menu;
	cols_item1 = itemcol1, cols_item2 = itemcol2;
	cols_1 = col1, cols_2 = col2;
}


bool getcolsdisp()
{
	return colsdisp;
}

void makeupCols()
/*
	ページ１の色表示のみを更新する
*/
{
	int ix,iy,ixlen,iylen;
	if (!colsdisp)
		return;
	if (!DMgetifonepage())
	{
		int zr = DMimage_getzoomrate();
		page_edit();
		menu_getbuttonxy(cols_menu, cols_item1, &ix, &iy, &ixlen, &iylen);
		grboxfill(DMgetpage1x(ix),DMgetpage1y(iy),
				  ixlen/zr,iylen/zr, *cols_1, DrawNORMAL);
		menu_getbuttonxy(cols_menu, cols_item2, &ix, &iy, &ixlen, &iylen);
		grboxfill(DMgetpage1x(ix),DMgetpage1y(iy),
				  ixlen/zr,iylen/zr, *cols_2, DrawNORMAL);
		page_menu();
	}
	else
	{
		menu_getbuttonxy(cols_menu, cols_item1, &ix, &iy, &ixlen, &iylen);
		grboxfill(ix,iy,ixlen-1,iylen-1, *cols_1, DrawNORMAL);
		menu_getbuttonxy(cols_menu, cols_item2, &ix, &iy, &ixlen, &iylen);
		grboxfill(ix,iy,ixlen-1,iylen-1, *cols_2, DrawNORMAL);
	}
}


int drawCols()
{
	int ix,iy,ixlen,iylen;
	if (!DMgetifonepage())
	{
		int zr = DMimage_getzoomrate();
		menu_getbuttonxy(cols_menu, cols_item1, &ix, &iy, &ixlen, &iylen);
		if (DMmenu2_addbox(ix,iy,ixlen,iylen) != 0)
			return -1;
		grboxfill(ix-1,iy-1,ixlen+1,iylen+1,0,DrawNORMAL);
		grboxline(ix-1,iy-1,ixlen+1,iylen+1,Black,DrawNORMAL);
		page_edit();
		grboxfill(DMgetpage1x(ix),DMgetpage1y(iy),
				  ixlen/zr,iylen/zr,*cols_1,DrawNORMAL);
		//
		page_menu();
		menu_getbuttonxy(cols_menu, cols_item2, &ix, &iy, &ixlen, &iylen);
		if (DMmenu2_addbox(ix,iy,ixlen,iylen) != 0)
			return -1;
		grboxfill(ix-1,iy-1,ixlen+1,iylen+1,0,DrawNORMAL);
		grboxline(ix-1,iy-1,ixlen+1,iylen+1,Black,DrawNORMAL);
		page_edit();
		grboxfill(DMgetpage1x(ix),DMgetpage1y(iy),
				  ixlen/zr,iylen/zr,*cols_2,DrawNORMAL);
		page_menu();
	}
	else
	{
		menu_getbuttonxy(cols_menu, cols_item1, &ix, &iy, &ixlen, &iylen);
		grboxline(ix-1,iy-1,ixlen+1,iylen+1,menu_plt(Black),DrawNORMAL);
		grboxfill(ix,iy,ixlen-1,iylen-1,*cols_1,DrawNORMAL);
		menu_getbuttonxy(cols_menu, cols_item2, &ix, &iy, &ixlen, &iylen);
		grboxline(ix-1,iy-1,ixlen+1,iylen+1,menu_plt(Black),DrawNORMAL);
		grboxfill(ix,iy,ixlen-1,iylen-1,*cols_2,DrawNORMAL);
	}
	colsdisp = YES;
	return 0;
}


void eraseCols()
{
	colsdisp = NO;
	if (!DMgetifonepage())
	{
		DMmenu2_deletebox();
		DMmenu2_deletebox();
	}
}


int cols_getcsrpos()
{
	return colcsrpos;
}

void cols_setcsrpos(int n)
{
	colcsrpos = n;
}

void drawColCsr()
{
	int ix,iy,ixlen,iylen;
	menu_getbuttonxy(cols_menu, (colcsrpos==0 ? cols_item1: cols_item2),
				   &ix, &iy, &ixlen, &iylen);
	grboxline(ix-1,iy-1,ixlen+1,iylen+1,menu_plt(White),DrawXOR);
}

/*--------------------------------------------------------*/
/*               パレットリストの描画･消去                */
/*--------------------------------------------------------*/


static bool pltlist_dsp = NO;
				// パレットリストを表示しているかどうか
static int pltlist_x,pltlist_y;
				// パレットリストを表示している位置(640*480座標)
static int pltlist_preDispX,pltlist_preDispY;
				// パレットリストを表示している時の dispX, dispY の値


void makeupPltList()
// パレットリストを更新する
{
	int zr = DMimage_getzoomrate();
	DEBUG_MSG("makeupPltList begin");
	if (!DMgetifonepage())
	{
		int i,j,px,py;
		page_edit();
		px = DMgetpage1x(pltlist_x);
		py = DMgetpage1y(pltlist_y);
		for (i=0; i<4; i++)
		{
			for (j=0; j<4; j++)
			{
				grboxfill(px+(j*16)/zr,py+(i*16)/zr,
						  16/zr,16/zr,plt_getcode(i*4+j),DrawNORMAL);
			}
		}
		page_menu();
	}
	else
	{
		int i,j,px,py;
		px = pltlist_x;
		py = pltlist_y;
		for (i=0; i<4; i++)
		{
			for (j=0; j<4; j++)
			{
				grboxfill(px+j*16,py+i*16,15,15,plt_getcode(i*4+j),DrawNORMAL);
			}
		}
	}
	DEBUG_MSG("makeupPltList end");
}


int drawPltList(int x,int y)
// パレットリストを新たに描く（menumode == 0 時、元の画像は保存）
{
	int zr = DMimage_getzoomrate();
	DEBUG_MSG("drawPltList begin");
	if (!DMgetifonepage())
	{
		int px,py;
		pltlist_dsp = YES;
		pltlist_preDispX = DMgetpage1x(x);
		pltlist_preDispY = DMgetpage1y(y);
		if (x != -1)
			pltlist_x = x;
		if (y != -1)
			pltlist_y = y;
		page_edit();
		px = DMgetpage1x(pltlist_x);
		py = DMgetpage1y(pltlist_y);
		if (DMmenu2_addbox(pltlist_x,pltlist_y,16*4,16*4) != 0)
			{ page_menu();  return -1; }
		page_menu();
		makeupPltList();
	}
	else
	{
		pltlist_dsp = YES;
		if (x != -1)
			pltlist_x = x;
		if (y != -1)
			pltlist_y = y;
		makeupPltList();
	}
	DEBUG_MSG("drawPltList end");
	return 0;
}


void erasePltList()
// パレットリストを消去する
{
	DEBUG_MSG("erasePltList begin");
	if (!DMgetifonepage())
	{
		pltlist_dsp = NO;
		DMmenu2_deletebox();
	}
	else
		pltlist_dsp = NO;
	DEBUG_MSG("erasePltList end");
}


void drawPltList_init(int x, int y)
{
	int i,j;
	for (i=0; i<4; i++)
	{
		for (j=0; j<4; j++)
		{
			grboxfill(x-1+j*16,y-1+i*16,17,17,menu_plt(Transparent),DrawNORMAL);
			grboxline(x-1+j*16,y-1+i*16,17,17,menu_plt(Black),DrawNORMAL);
		}
	}
}


bool getPltList_disp()
{
	return pltlist_dsp;
}


void	drawPltCsr(int c)
// DRAW PaLetTe CurSoR : パレットリストの中の１パレットを四角で囲む
{
	int cx,cy;
	cx = pltlist_x + (c%4)*16;
	cy = pltlist_y + (c/4)*16;
	grboxfill(cx-1,cy-1,17,17,menu_plt(White),DrawXOR);
	grboxfill(cx,cy,15,15,menu_plt(White),DrawXOR);
}


/*--------------------------------------------------------*/
/*                  RGB バーの描画･更新                   */
/*--------------------------------------------------------*/


static	int		rgb_x,rgb_y;
#define	_RGBBAR_MAXLEN	(mode == MODE16 ? 30 : 31)


void makeupRgbBar()
{
	DEBUG_MSG("makeupRgbBar begin");
	int i,rgb[3];
	static int RGBcol[]={White,White,White};
	if (mode == MODE32K)		// ３万色モード
	{
		int t;
		t = plt_getcode(pltnum);
		rgb[0]=getR(t), rgb[1]=getG(t), rgb[2]=getB(t);
	}
	else if (mode == MODE16)					// １６色モード
	{
		grb_t grb;
		grb = plt16_get(pltnum);
		rgb[0] = (grb>>4)&15,  rgb[1] = (grb>>8)&15,  rgb[2] = grb&15;
	}
	else					// ２５６色モード
		DEBUG_MSG("☆☆ makeupRgbBar : 256色未定義！");
	for (i=0; i<3; i++)
	{
		int	barlen, barx;
		int a = rgb[i];
		barx = rgb_x + i * 14;
		// 縦棒グラフを描く
		barlen = (mode == MODE16 ? a*2 : a);
		grboxfill(barx, rgb_y, 11, _RGBBAR_MAXLEN, menu_plt(Black), DrawNORMAL);
		if (a!=0)
		{
			grboxfill(barx, rgb_y + (_RGBBAR_MAXLEN - barlen), 11, barlen,
					  menu_plt(RGBcol[i]), DrawNORMAL);
		}
		// 数値を表示する
		putpict(barx  , rgb_y+32+12*2+1, Pnonum);
		putpict(barx+6, rgb_y+32+12*2+1, Pnonum);
		if (a >= 10)
			putpict(barx  , rgb_y+32+12*2+1, Pzero + a/10);
		putpict(barx+6, rgb_y+32+12*2+1, (Pzero + a % 10) );
	}
	DEBUG_MSG("makeupRgbBar end");
}


void drawRgbBar(int x, int y)
{
	DEBUG_MSG("drawRgbBar begin");
	int i;
	rgb_x = x;
	rgb_y = y;
	for (i=0; i<3; i++)
	{
		grboxfill(x+i*14, y, 11, _RGBBAR_MAXLEN, menu_plt(Black), DrawNORMAL);
		putpict(x+i*14, y+32,    Pupsw);
		putpict(x+i*14, y+32+12, Pdownsw);
		putpict(x+i*14, y+64, Prgb_r + i);
	}
	makeupRgbBar();
	DEBUG_MSG("drawRgbBar end");
}


#undef _RGBBAR_MAXLEN


/*--------------------------------------------------------*/
/*                     RGB 要素の増減                     */
/*--------------------------------------------------------*/


void touchRgbBar(int ax, int ay, int btn)
{
	DEBUG_MSG("touchRgbBar begin");
	int rgb[3],t,rgbx;
	rgbx = ax / 14;
	if ((ax % 14) > 11)
		return;
	if (mode == MODE32K)		// ３万色編集の場合
	{
		t = plt_getcode(pltnum);
		rgb[0]=getR(t), rgb[1]=getG(t), rgb[2]=getB(t);
		if (0<=ay && ay<=31)
			rgb[rgbx]= 31-ay;
		else if (32<=ay && ay<32+12)
		{
			if (btn==OFFON && rgb[rgbx]<31)
				rgb[rgbx]++;
		}
		else if (32+12<=ay && ay<32+24)
		{
			if (btn==OFFON && rgb[rgbx]>0)
				rgb[rgbx]--;
		}
		plt_setcode(pltnum, GRB(rgb[1],rgb[0],rgb[2]));
	}
	else if (mode == MODE16)	// 16色編集の場合
	{
		page_edit();
		{
			grb_t grb;
			grb = plt16_get(pltnum);
			rgb[0] = (grb>>4)&15,  rgb[1] = (grb>>8)&15,  rgb[2] = grb&15;
		}
		page_menu();
		if (0<=ay && ay<=30)
			rgb[rgbx]= (30-ay)/2;
		else if (32<=ay && ay<32+12)
		{
			if (btn==OFFON && rgb[rgbx]<15)
				rgb[rgbx]++;
		}
		else if (32+12<=ay && ay<32+24)
		{
			if (btn==OFFON && rgb[rgbx]>0)
				rgb[rgbx]--;
		}
		page_edit();
		{
			grb_t grb;
			grb = (int)rgb[1] * 256 + (int)rgb[0] * 16 + rgb[2];
			plt16_set(pltnum,grb);
		}
		page_menu();
	}
	else
		DEBUG_MSG("touchRgbBar : 256色未定義！");
	makeupPltList();
	makeupRgbBar();
	DEBUG_MSG("touchRgbBar end");
}


/* end of plt.c */
