/***********************************************************************
*Copyright (c)2012 , 天际边工作室
*All rights reserved.
**
文件名称：	Menu.cpp
*摘要：		游戏主地图类
**
*修改历史：
*	版本    日期     作者     改动内容和原因
*   ----------------------------------------------------
*	1.0    2012-10-5  wstjb     基本的功能完成
***********************************************************************/

#include "Menu.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

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 color;
extern  int flag;

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 byte resByte[2731][16384];//存储lib资源
extern int resByteLen[2731];
extern int resPos[2731][6];//存储lib地址和编号 第六位为长度
extern int resCount;

extern char menu_main[4][5];
extern char menu_s1[2][5];// = {"状态","穿戴"};
extern char menu_s3[2][5];// = {"使用","装备"};
extern char menu_s4[4][9];// = {"读入进度","存储进度","游戏设置","结束游戏"};
extern char menu_s5[2][7];// = {"音乐开","音乐关"};
extern char menu_s6[5][5];// = {"围攻","道具","防御","逃跑","状态"};
extern char menu_s7[3][5];// = {"装备","投掷","使用"};
extern char menu_money[7];

extern int GMCUsed;
/***********************************************************************
 * 说明:     Map类初始化函数
 * 输入参数: int num——用于设定君主等信息，由君主选择传值过来
 * 返回值  : 无
 * 功能	   : 初始化
 * 修改历史:
 *               姓名            日期             说明
 *             ------          ----------      -------------
 *             wstjb          2012-10-5       完成基本功能
***********************************************************************/
void Menu::Init(int x,int y,int u,int v,int menuno,int money)
{
	//nameNum = 0;
	keyflag = 0;
	stat= 0;
	ux=x;
	uy=y;
	posx=u;
	posy=v;
	type = 0;
	if(uy < 0)
	{
		type = 1;
		maxline = -uy+1;
	}
	uy = abs(uy);
	this->menuno = menuno;
	this->money = money;
	//memset(name,0,sizeof(name));
}

/***********************************************************************
 * 说明:     Map类显示函数
 * 输入参数: WPARAM KeyCode——按键信息，用于控制地图移动与对应的显示
 * 返回值  : 无
 * 功能	   : 显示地图及相关界面
 * 修改历史:
 *               姓名            日期             说明
 *             ------          ----------      -------------
 *             wstjb          2012-10-5       完成基本功能
***********************************************************************/
void Menu::Show(WPARAM KeyCode)
{
	HPEN hpen=CreatePen(PS_SOLID,1, RGB(0,0,0));
	HPEN oldhpen=(HPEN)SelectObject(hdcTmp,hpen);

	if(menuno != 6 && menuno != 7 && menuno != 8)
	{
		BitBlt(hdcTmp,9,3,92,19,hdcMem,0,0,WHITENESS);

		if(GMCUsed == 1)
		{
			HBRUSH hbrush = CreateSolidBrush(RGB(24,132,96));
			HBRUSH holdbrush = (HBRUSH)SelectObject(hdcTmp,hbrush);
			Rectangle(hdcTmp,10,4,100,23); 
			SelectObject(hdcTmp,holdbrush);
			DeleteObject(hbrush);
		}
		else
		{
			Rectangle(hdcTmp,10,4,100,23); 
		}

		
		
		ShowText(menu_money,12,6);
		char tmp[10];
		sprintf_s(tmp,"%d",money);
		ShowText(tmp,50,6);
	}
	if(type == 1) 
	{
		BitBlt(hdcTmp,posx-1,posy-1,ux*16+5+2,uy*16+5+8+2,hdcMem,0,0,WHITENESS);
		if(GMCUsed == 1)
		{
			HBRUSH hbrush = CreateSolidBrush(RGB(24,132,96));
			HBRUSH holdbrush = (HBRUSH)SelectObject(hdcTmp,hbrush);
			Rectangle(hdcTmp,posx,posy,posx+ux*16+5,posy+uy*16+5+8); 
			SelectObject(hdcTmp,holdbrush);
			DeleteObject(hbrush);
		}
		else
		{
			Rectangle(hdcTmp,posx,posy,posx+ux*16+5,posy+uy*16+5+8); 
		}
	}
	else
	{
		BitBlt(hdcTmp,posx-1,posy-1,ux*16+5+2,uy*16+5+2,hdcMem,0,0,WHITENESS);
		if(GMCUsed == 1)
		{
			HBRUSH hbrush = CreateSolidBrush(RGB(24,132,96));
			HBRUSH holdbrush = (HBRUSH)SelectObject(hdcTmp,hbrush);
			Rectangle(hdcTmp,posx,posy,posx+ux*16+5,posy+uy*16+5); 
			SelectObject(hdcTmp,holdbrush);
			DeleteObject(hbrush);
		}
		else
		{
			Rectangle(hdcTmp,posx,posy,posx+ux*16+5,posy+uy*16+5); 
		}
		

	}
	if(menuno == 0)
	{
		
		ShowText(menu_main[0],posx+2,posy+2);
		ShowText(menu_main[1],posx+2,posy+2+16);
		ShowText(menu_main[2],posx+2,posy+2+32);
		ShowText(menu_main[3],posx+2,posy+2+48);
		
	}
	if(menuno == 1)
	{
		ShowText(menu_s1[0],posx+2,posy+2);
		ShowText(menu_s1[1],posx+2,posy+2+16);
	}
	if(menuno == 3)
	{
		ShowText(menu_s3[0],posx+2,posy+2);
		ShowText(menu_s3[1],posx+2,posy+2+16);
	}
	if(menuno == 2 || menuno == 8)
	{
		ShowText(name[0],posx+2,posy+2);
		if(nameNum > 1)ShowText(name[1],posx+2,posy+2+16);
		if(nameNum > 2)ShowText(name[2],posx+2,posy+2+32);
	}
	if(menuno == 4 && (( stat < 3 && KeyCode == KeyDown0)||(stat == 0 && KeyCode == KeyUp0)))
	{
		ShowText(menu_s4[0],posx+2,posy+2);
		ShowText(menu_s4[1],posx+2,posy+2+16);
		ShowText(menu_s4[2],posx+2,posy+2+32);
		MoveToEx(hdcTmp,posx+30,posy+2+50,NULL);
		LineTo(hdcTmp,posx+30+10,posy+2+50);
		MoveToEx(hdcTmp,posx+31,posy+2+51,NULL);
		LineTo(hdcTmp,posx+31+8,posy+2+51);
		MoveToEx(hdcTmp,posx+32,posy+2+52,NULL);
		LineTo(hdcTmp,posx+32+6,posy+2+52);
		MoveToEx(hdcTmp,posx+33,posy+2+53,NULL);
		LineTo(hdcTmp,posx+33+4,posy+2+53);
		MoveToEx(hdcTmp,posx+34,posy+2+54,NULL);
		LineTo(hdcTmp,posx+34+2,posy+2+54);

		
	}
	if(menuno == 4 && ((stat > 0 && KeyCode == KeyUp0)||(stat == 3 && KeyCode == KeyDown0)))
	{
		ShowText(menu_s4[1],posx+2,posy+2+8);
		ShowText(menu_s4[2],posx+2,posy+2+24);
		ShowText(menu_s4[3],posx+2,posy+2+40);

		MoveToEx(hdcTmp,posx+30,posy+2+6,NULL);
		LineTo(hdcTmp,posx+30+10,posy+2+6);
		MoveToEx(hdcTmp,posx+31,posy+2+5,NULL);
		LineTo(hdcTmp,posx+31+8,posy+2+5);
		MoveToEx(hdcTmp,posx+32,posy+2+4,NULL);
		LineTo(hdcTmp,posx+32+6,posy+2+4);
		MoveToEx(hdcTmp,posx+33,posy+2+3,NULL);
		LineTo(hdcTmp,posx+33+4,posy+2+3);
		MoveToEx(hdcTmp,posx+34,posy+2+2,NULL);
		LineTo(hdcTmp,posx+34+2,posy+2+2);
	}
	if(menuno == 5)
	{

		ShowText(menu_s5[0],posx+2,posy+2);
		ShowText(menu_s5[1],posx+2,posy+2+16);
	}
	if(menuno == 6)
	{
		ShowText(menu_s6[0],posx+2,posy+2);
		ShowText(menu_s6[1],posx+2,posy+2+16);
		ShowText(menu_s6[2],posx+2,posy+2+32);
		ShowText(menu_s6[3],posx+2,posy+2+48);
		ShowText(menu_s6[4],posx+2,posy+2+64);
	}
	if(menuno == 7)
	{
		ShowText(menu_s7[0],posx+2,posy+2);
		ShowText(menu_s7[1],posx+2,posy+2+16);
		ShowText(menu_s7[2],posx+2,posy+2+32);
	}

	SelectObject(hdcTmp,oldhpen);
	DeleteObject(hpen);
	if(menuno == 4 && ((stat > 0 && KeyCode == KeyUp0)||(stat == 3 && KeyCode == KeyDown0)))
	{
		BitBlt(hdcTmp,posx+2,posy+2+8+(stat-1)*16,16 * ux + 1 ,16,hdcTmp,posx+2,posy+2+8+(stat-1)*16,DSTINVERT);
	}
	else
	{
		BitBlt(hdcTmp,posx+2,posy+2+stat*16,16 * ux + 1 ,16,hdcTmp,posx+2,posy+2+stat*16,DSTINVERT);
	}
}

/***********************************************************************
 * 说明:     Map类按键函数
 * 输入参数: WPARAM KeyCode——按键信息，用于确定当前的城市
 * 返回值  : 无
 * 功能	   : 处理当前类的按键信息
 * 修改历史:
 *               姓名            日期             说明
 *             ------          ----------      -------------
 *             wstjb          2012-10-5       完成基本功能
 *			   wstjb			2012-10-20		增加功能
***********************************************************************/
int Menu::KeyDown(WPARAM KeyCode)
{
	wlogfile(1, __FUNCTION__ ,"type=",type);
	wlogfile(1, __FUNCTION__ ,"KeyCode=",KeyCode);
	if(type == 0)
	{
		if(KeyCode == KeyEsc)
		{
			return -1;
			
		}
		else if(KeyCode == KeyReturn)
		{
			return 1;
		}
		else if(KeyCode == KeyUp0)
		{
			//int *p = NULL;
	//*p=1; 
			stat --;
			if(stat <0)
			{
				if(menuno == 6)
				{
					stat = 0;
				}
				else
				{
					stat = uy-1;
				}
			}
			Show(KeyCode);
		}
		else if(KeyCode == KeyDown0)
		{
			stat ++;
			if(stat >(uy -1))
			{
				if(menuno == 6)
				{
					stat = uy-1;
				}
				else
				{
					stat = 0;
				}
			}
			Show(KeyCode);
		}
	}
	else
	{
		if(KeyCode == KeyEsc)
		{
			return -1;
			
		}
		else if(KeyCode == KeyReturn)
		{
			return 1;
		}
		else if(KeyCode == KeyUp0)
		{
			stat --;
			if(stat <0)stat = maxline-1;
			Show(KeyCode);
		}
		else if(KeyCode == KeyDown0)
		{
			stat ++;
			if(stat >(maxline -1))stat = 0;
			Show(KeyCode);
		}
	}
	
	return 0;
}


void Menu::SetName(char *name,int idx)
{
	for(int i = 0;i<12;i++)
	{
		this->name[idx][i] = name[i];
	}
	nameNum++;
}