/*
	plt.c
*/

#include <stdio.h>
#include <egb.h>

#include "ge.h"

/*--------------------------------------------------------*/
/*               パレットリストの描画･消去                */
/*--------------------------------------------------------*/

static	int		pltlist_preDispX,pltlist_preDispY;
static	int		pltlist_x,pltlist_y;
static	bool	pltlist_dsp = NO;

void makeupPltList()
{
	msgout("makeupPltList begin");

	int i,j,px,py;
	gwrtpage(1);
	px = dispX + pltlist_x / zoomX;
	py = dispY + pltlist_y / zoomY;
	for (i=0; i<4; i++) {
		for (j=0; j<4; j++) {
			grboxfill(px+(j*16)/zoomX,py+(i*16)/zoomY,
					  16/zoomX,16/zoomY,plt[i*4+j],DrawNORMAL);
		}
	}
	gwrtpage(0);

	msgout("makeupPltList end");
}

static char pltListScrBuf[80*80*2];

void drawPltList(int x,int y)
{
	msgout("drawPltList begin");

	int i,j,px,py;
	pltlist_dsp = YES;
	pltlist_preDispX = dispX;
	pltlist_preDispY = dispY;
	if (x != -1)
		pltlist_x = x;
	if (y != -1)
		pltlist_y = y;
	gwrtpage(1);
	px = dispX + pltlist_x / zoomX;
	py = dispY + pltlist_y / zoomY;
	grp_getblk(px,py,(16*4)/zoomX,(16*4)/zoomY,pltListScrBuf);
	gwrtpage(0);
	makeupPltList();

	msgout("drawPltList end");
}

void erasePltList()
{
	msgout("erasePltList begin");

	int px,py;
	pltlist_dsp = NO;
	gwrtpage(1);
	px = pltlist_preDispX + pltlist_x / zoomX;
	py = pltlist_preDispY + pltlist_y / zoomY;
	grp_putblk(pltListScrBuf,px,py);
	gwrtpage(0);

	msgout("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,Transparent,DrawNORMAL);
			grboxline(x-1+j*16,y-1+i*16,17,17,Black,DrawNORMAL);
		}
	}
}

bool getPltList_disp()
{
	return pltlist_dsp;
}

/*--------------------------------------------------------*/
/*                  RGB バーの描画･更新                   */
/*--------------------------------------------------------*/

static	int		rgb_x,rgb_y;

#define	_rgbBarLen	(scrmode == 10 ? 31 : 30)

void makeupRgbBar()
{
	msgout("makeupRgbBar begin");
	int a,i,t,rgb[3];  static int RGBcol[]={White,White,White};
	if (scrmode == 10) {	// ３万色モード
		t = plt[pltnum];
		rgb[0]=getR(t); rgb[1]=getG(t); rgb[2]=getB(t);
	} else {				// １６色モード
		char pal_[4+8*16];
		EGB_getPalette(1,pal_);
		rgb[0] = (pal_[4+8*pltnum+4+1] >> 4) & 0xf;
		rgb[1] = (pal_[4+8*pltnum+4+2] >> 4) & 0xf;
		rgb[2] = (pal_[4+8*pltnum+4  ] >> 4) & 0xf;
		// t = grp_getplt(pltnum);
		// rgb[0]=((t>>16)&0xff)*2; rgb[1]=((t>>8)&0xff)*2; rgb[2]=(t&0xff)*2;
	}
	for (i=0; i<3; i++) {
		int	barlen;
		a = rgb[i];
		barlen = (scrmode == 10 ? a : a*2);
		grboxfill(rgb_x+i*14,rgb_y,11,_rgbBarLen,Black,DrawNORMAL);
		if (a!=0)
			grboxfill(rgb_x+i*14,rgb_y+(_rgbBarLen-barlen),11,barlen,RGBcol[i],DrawNORMAL);
		putpict(rgb_x+i*14,rgb_y+47,(a<10 ? Pnonum : Pzero+a/10));
		putpict(rgb_x+i*14+6,rgb_y+47,(Pzero+a%10));
	}
	msgout("makeupRgbBar end");
}

void drawRgbBar(int x, int y)
	{
	msgout("drawRgbBar begin");
	int i;
	rgb_x = x;
	rgb_y = y;
	for (i=0; i<3; i++) {
		grboxfill(x+i*14,y,11,_rgbBarLen,Black,DrawNORMAL);
		putpict(x+i*14,y+32,Prgb_updown);
		putpict(x+i*14,y+54,Prgb_r+i);
		}
	makeupRgbBar();
	msgout("drawRgbBar end");
	}

/*--------------------------------------------------------*/
/*                     RGB 要素の増減                     */
/*--------------------------------------------------------*/

void touchRgbBar(int ax, int ay, int btn)
{
	msgout("touchRgbBar begin");
	int rgb[3],t,rgbx;
	rgbx = ax / 14;
	if ((ax % 14) > 11)
		return;
	if (scrmode == 10) {
		t = plt[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+7) {
			if (btn==OFFON && rgb[rgbx]<31)
				rgb[rgbx]++;
		} else if (32+7<=ay && ay<32+14) {
			if (btn==OFFON && rgb[rgbx]>0)
				rgb[rgbx]--;
		}
		plt[pltnum] = GRB(rgb[1],rgb[0],rgb[2]);
	} else {
		gwrtpage(1);
		{
			char pal_[4+8*16];
			EGB_getPalette(1,pal_);
			rgb[0] = (pal_[4+8*pltnum+4+1] >> 4) & 0xf;
			rgb[1] = (pal_[4+8*pltnum+4+2] >> 4) & 0xf;
			rgb[2] = (pal_[4+8*pltnum+4  ] >> 4) & 0xf;
			// t = grp_getplt(pltnum);
			// rgb[0]=((t>>16)&0xff)*2; rgb[1]=((t>>8)&0xff)*2; rgb[2]=(t&0xff)*2;
		}
		gwrtpage(0);

		// t = grp_getplt(pltnum);
		// rgb[0] = (t >> 16) & 0xff;
		// rgb[1] = (t >>  8) & 0xff;
		// rgb[2] =  t        & 0xff;
		if (0<=ay && ay<=30)
			rgb[rgbx]= (30-ay)/2;
		else if (32<=ay && ay<32+7) {
			if (btn==OFFON && rgb[rgbx]<15)
				rgb[rgbx]++;
		} else if (32+7<=ay && ay<32+14) {
			if (btn==OFFON && rgb[rgbx]>0)
				rgb[rgbx]--;
		}
		gwrtpage(1);
		{
			char pal_[4+8*16];
			DWORD(pal_ + 0) = 1;
			DWORD(pal_ + 4) = pltnum;
			BYTE (pal_ + 8) = rgb[2] << 4;
			BYTE (pal_ + 9) = rgb[0] << 4;
			BYTE (pal_ +10) = rgb[1] << 4;
			BYTE (pal_ +11) = 0;
			EGB_palette(_egbwork,0,pal_);
		}
		gwrtpage(0);
		// grp_setplt(pltnum, (rgb[0]<<20)|(rgb[1]<<12)|(rgb[2]<<4));
	}
	makeupPltList();
	makeupRgbBar();
	msgout("touchRgbBar end");
}

/* end of plt.c */
