

#include "Operate.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "PublicFun.h"

extern  PAINTSTRUCT ps;
extern 	HDC hdc;
extern 	HBITMAP hbmp; 
extern 	HBITMAP hbmp2; 
extern 	BITMAP bmp;
extern 	HDC hdcMem;
extern 	HDC hdcTmp;
extern 	RECT rect;
extern 	HWND hwnd;

extern int KeyReturn;
extern int KeyEsc;
extern int KeyHome;
extern int KeySpace;
extern int KeyLeft0;
extern int KeyUp0;
extern int KeyRight0;
extern int KeyDown0;
extern int color;
extern HPEN t_hpen;
extern HBRUSH t_hbrush;

using namespace std;


void Operate::Init(int style,int max)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	//1士兵2金钱3粮食 -1买入卖出选择
	this->style=style;
	this->max =max;
	current=0;
	if(style==-1)
	{
		x=max;
	}
	else
	{
		x=0;
	}
	xmax=0;
	double tmp=max;
	while(tmp>=1)
	{
		tmp=tmp/10;
		xmax++;
	}
	xmax--;

}

int Operate::KeyDown(WPARAM KeyCode)
{
	wlogfile(1,__FUNCTION__,"开始",KeyCode);
	if(style==-1)
	{
		if(KeyCode==KeyUp0||KeyCode==KeyDown0)
		{

			if(x==0)
			{
				x=1;
			}
			else if(x==1)
			{
				x=0;
			}
			Show();
			return 0;
		}
		else if(KeyCode==KeyReturn)
		{
			return x+10;
		}
		else if(KeyCode==KeyEsc)
		{
			return -1;
		}
		else if(KeyCode==KeyLeft0||KeyCode==KeyRight0)
		{
			return 0;
		}

	}

	int degree=0;
	switch(xmax)
	{
	case 0:
		degree=1;
		break;
	case 1:
		degree=10;
		break;
	case 2:
		degree=100;
		break;
	case 3:
		degree=1000;
		break;
	case 4:
		degree=10000;
		break;
	}
	if(KeyCode==KeyUp0)
	{
		switch(x)
		{
		case 0:
			current=current+degree;
			if(current>max)
			{
				current=current-degree;
			}
			break;
		case 1:
			current=current+degree/10;
			if(current>max)
			{
				current=current-degree/10;
			}
			break;
		case 2:
			current=current+degree/100;
			if(current>max)
			{
				current=current-degree/100;
			}
			break;
		case 3:
			current=current+degree/1000;
			if(current>max)
			{
				current=current-degree/1000;
			}
			break;
		case 4:
			current=current+degree/10000;
			if(current>max)
			{
				current=current-degree/10000;
			}
			break;
		}
		Show();
		return 0;
	}
	else if(KeyCode==KeyDown0)
	{
		switch(x)
		{
		case 0:
			current=current-degree;
			if(current<0)
			{
				current=current+degree;
			}
			break;
		case 1:
			current=current-degree/10;
			if(current<0)
			{
				current=current+degree/10;
			}
			break;
		case 2:
			current=current-degree/100;
			if(current<0)
			{
				current=current+degree/100;
			}
			break;
		case 3:
			current=current-degree/1000;
			if(current<0)
			{
				current=current+degree/1000;
			}
			break;
		case 4:
			current=current-degree/10000;
			if(current<0)
			{
				current=current+degree/10000;
			}
			break;
		}
		Show();
		return 0;
	}
	else if(KeyCode==KeyReturn)
	{

		return current+10;
	}
	else if(KeyCode==KeyEsc)
	{
		return -1;
	}
	else if(KeyCode==KeyRight0)
	{
		x++;
		if(x>xmax)x=xmax;
		Show();
		return 0;
	}
	else if(KeyCode==KeyLeft0)
	{
		x--;
		if(x<0)x=0;
		Show();
		return 0;
	}
	else
	{
		return 0;
	}

}

void Operate::Show()
{
	wlogfile(1,__FUNCTION__,"开始",0);
	if(style==-1)
	{

		MySetShowStyle();
		HPEN hpen=t_hpen;
		HPEN oldhpen=(HPEN)SelectObject(hdcTmp,hpen);

		HBRUSH hbrush = t_hbrush;
		HBRUSH hbrold = (HBRUSH)SelectObject(hdcTmp, hbrush); 

		Rectangle(hdcTmp,400,100,530,230); 
		SelectObject(hdcTmp, hbrold); 
		DeleteObject(hbrush);
		SelectObject(hdcTmp,oldhpen);
		DeleteObject(hpen);
		

		ShowText(GetStrTextSpilted("MENU_TSTR",0,2),405,105);
		ShowText(GetStrTextSpilted("MENU_TSTR",1,2),405,165);
		if(x==0)
			BitBlt(hdcTmp,405,105,120,60,hdcTmp,405,105,DSTINVERT);
		else if(x==1)
			BitBlt(hdcTmp,405,165,120,60,hdcTmp,405,165,DSTINVERT);
		return;
	}
	char str[20];
	if(color==1)
	{

		HPEN hpen=CreatePen(PS_SOLID,5, RGB(180,106,41));
		HPEN oldhpen=(HPEN)SelectObject(hdcTmp,hpen);
		HBRUSH hbrush = CreateSolidBrush(RGB(198,151,81)); 
		HBRUSH hbrold = (HBRUSH)SelectObject(hdcTmp, hbrush); 

		Rectangle(hdcTmp,200,200,600,400); 	
		SelectObject(hdcTmp, hbrold); 
		DeleteObject(hbrush);
		SelectObject(hdcTmp,oldhpen);
		DeleteObject(hpen);
	}
	else
	{
		HPEN hpen=CreatePen(PS_SOLID,5, RGB(0,0,0));
		HPEN oldhpen=(HPEN)SelectObject(hdcTmp,hpen);	
		Rectangle(hdcTmp,200,200,600,400); 	
		SelectObject(hdcTmp,oldhpen);
		DeleteObject(hpen);
	}

	if(style==1)
	{
		ShowText(GetStrText("ATRR_STR62"),250,220);
	}
	else if(style==2)
	{
		ShowText(GetStrText("ATRR_STR61"),250,220);
	}
	else if(style==3)
	{
		ShowText(GetStrText("ATRR_STR60"),250,220);
	}
	ShowText(GetStrText("ATRR_STR63"),210,280);
	ShowText(GetStrText("ATRR_STR64"),210,340);
	sprintf_s(str,"%d",max);
	ShowText(str,450,340);
	if(max>9999)
	{
		//ShowText("00000",450,280);
		sprintf_s(str,"%d",current);
		if(current>9999)
		{
			ShowText(str,450,280);
		}
		else if(current>999)
		{
			ShowText("0",450,280);
			ShowText(str,476,280);
		}
		else if(current>99)
		{
			ShowText("00",450,280);
			ShowText(str,502,280);
		}
		else if(current>9)
		{
			ShowText("000",450,280);
			ShowText(str,528,280);
		}
		else
		{
			ShowText("0000",450,280);
			ShowText(str,554,280);
		}
	}
	else if(max>999)
	{
		//ShowText("0000",450,280);
		sprintf_s(str,"%d",current);

		if(current>999)
		{
			ShowText(str,450,280);
		}
		else if(current>99)
		{
			ShowText("0",450,280);
			ShowText(str,476,280);
		}
		else if(current>9)
		{
			ShowText("00",450,280);
			ShowText(str,502,280);
		}
		else 
		{
			ShowText("000",450,280);
			ShowText(str,528,280);
		}
	}
	else if(max>99)
	{
		//ShowText("000",450,280);
		sprintf_s(str,"%d",current);
		if(current>99)
		{
			ShowText(str,450,280);
		}
		else if(current>9)
		{
			ShowText("0",450,280);
			ShowText(str,476,280);
		}
		else 
		{
			ShowText("00",450,280);
			ShowText(str,502,280);
		}
	}
	else if(max>9)
	{
		//ShowText("00",450,280);
		sprintf_s(str,"%d",current);
		if(current>9)
		{
			ShowText(str,450,280);
		}
		else
		{
			ShowText("0",450,280);
			ShowText(str,476,280);
		}
	}
	else 
	{
		//ShowText("0",450,280);
		sprintf_s(str,"%d",current);
		ShowText(str,450,280);
	}
	BitBlt(hdcTmp,450+x*26,280,26,60,hdcTmp,450+x*26,280,DSTINVERT);


}

