⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menu.cpp

📁 map editer地图编辑器
💻 CPP
字号:
//    ███████████
//    ██╭----------╮██
//    █╭╯ Menu.cpp ╰╮█
//    █|	菜单类文件	|█
//    █╰--------------╯█
//    ███████████
//				Welcome to http://witsun.myrice.com


#include "stdafx.h"
#include "Menu.h"
#include "DirectDraw.h"
#include <mmsystem.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern CDirectDraw* DirectDraw;
extern CMenu* Menu;
extern void Cleanup();
extern LPDIRECTDRAWSURFACE7 CreateBlankSurface(int nWidth, int nHeight);//创建空白页
extern bool LeftButton;
extern bool MenuMoveable;
extern int nowx,nowy,TileID;
extern void	ChangeTile();
extern HWND hWnd;
extern int Menu0,Menu1;	//the Tile in Menu's point
extern void TileMenu1();
extern bool MapMove;	//地图中的元素是否呆经过?
/////////////////////////////////////////////////////////////////

CMenu::CMenu()
{
	MenuNum=0;
}


CMenu::~CMenu()
{
	MenuNode* next;
	while(!IsEmpty())
	{
		next=top->link;

		for (int i=0;i<top->ButtonID;i++)
			SafeRelease(top->ButtonSurface[i]);
		SafeRelease(top->MenuSurface);
		SafeDelete(top);

		top= next;

		MenuNum--;
	}
}

/////////////////////////////////////////增加菜单
CMenu& CMenu::CreateMenu(const LPCTSTR menuname,
							 int x,int y,bool havebutton)
{
	MenuNum++;

	MenuNode* p=new MenuNode;
	p->ButtonID=0;
	p->MenuName =menuname;
	p->MenuX=x;
	p->MenuY=y;
	p->HaveButton=havebutton;

	int width,height;
	char* tempname = new char[strlen(menuname)+10];
	strcpy(tempname,"pic\\");
	strcat(tempname,menuname);
	strcat(tempname,".bmp");
	p->MenuSurface=BMPToSurface(tempname,width,height);
	delete tempname;

	p->MenuWidth=width;
	p->MenuHeight=height;
	p->link =top;
	top=p;

	destRect.top=top->MenuY;
	destRect.left=top->MenuX;
	destRect.right=top->MenuX+top->MenuWidth;
	destRect.bottom=top->MenuY+top->MenuHeight;

	return* this;
}

////////////////////////////////////////删除顶栈的菜单
CMenu& CMenu::DelMenu()
{
	if (!IsEmpty())
	{
		MenuNode* p=top;
		top= top->link;

		for (int i=0;i<p->ButtonID;i++)
			SafeRelease(p->ButtonSurface[i]);
		SafeRelease(p->MenuSurface);
		delete p;

		MenuNum--;
	}
	return* this;
}


/////////////////////////////////////////显示窗口
void CMenu::Show()
{

	if (!IsEmpty())
/*	{
	DirectDraw->CleanSurface();
	return;
	}
*/
	DirectDraw->lpDDSBack->Blt(&destRect,top->MenuSurface,NULL,DDBLT_WAIT,NULL);

}

///////////////////////////////////创建按钮
void CMenu::CreateButton(const LPCTSTR name,int x,int y)
{
	if(top->ButtonID<ButtonMax)
	{
		top->ButtonName[top->ButtonID]=name;
		top->ButtonX[top->ButtonID]=x;
		top->ButtonY[top->ButtonID]=y;

		int width,height;
		char* tempname = new char[strlen(top->MenuName)+strlen(name)+11];
		strcpy(tempname,"pic\\");
		strcat(tempname,top->MenuName);
		strcat(tempname,"_");
		strcat(tempname,name);
		strcat(tempname,".bmp");
		top->ButtonSurface[top->ButtonID]=BMPToSurface(tempname,
													width,height);
		delete tempname;
		top->ButtonWidth[top->ButtonID]=width;
		top->ButtonHeight[top->ButtonID]=height;

//		DirectDraw->DDSetColorKey(top->ButtonSurface[top->ButtonID],CLR_INVALID);

		top->MenuSurface->BltFast(x,y,
								top->ButtonSurface[top->ButtonID],
								NULL,
								DDBLTFAST_WAIT/*|DDBLTFAST_SRCCOLORKEY*/);

		top->ButtonID++;
	}
}


///////////////////////////////////创建Tile
void CMenu::CreateTile(LPDIRECTDRAWSURFACE7 face)
{
	static int ID=0;

	if(top->ButtonID<ButtonMax)
	{
		top->ButtonName[top->ButtonID]=(LPCTSTR)ID;
		top->ButtonX[top->ButtonID]=0;
		top->ButtonY[top->ButtonID]=0;

		top->ButtonWidth[top->ButtonID]=40;
		top->ButtonHeight[top->ButtonID]=40;

		top->ButtonSurface[top->ButtonID]=CreateBlankSurface(40, 40);
		top->ButtonSurface[top->ButtonID]->BltFast(0,0,
								face,
								NULL,
								DDBLTFAST_WAIT/*|DDBLTFAST_SRCCOLORKEY*/);

		top->ButtonID++;
		ID++;
	}
}


///////////////////////////////////////////////////加载图片的函数
LPDIRECTDRAWSURFACE7 CMenu::BMPToSurface(LPCTSTR file_name,
										 int& width,int& height)
{
	HDC hdc;
	HBITMAP bit;
	LPDIRECTDRAWSURFACE7 surf;

	// load the interface bitmap
	bit=(HBITMAP) LoadImage(NULL,file_name,IMAGE_BITMAP,0,0,
								LR_DEFAULTSIZE|LR_LOADFROMFILE);
	if (!bit)
		
		// failed to load, return failure to caller
		return NULL;

	// get bitmap dimensions
	BITMAP bitmap;
    GetObject( bit, sizeof(BITMAP), &bitmap );
	int surf_width=bitmap.bmWidth;
	int surf_height=bitmap.bmHeight;

	width=bitmap.bmWidth;
	height=bitmap.bmHeight;

	// create surface
	HRESULT ddrval;
	DDSURFACEDESC2 ddsd;
	ZeroMemory(&ddsd,sizeof(ddsd));
	ddsd.dwSize = sizeof(DDSURFACEDESC2);
	ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT ;
	ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; 
	ddsd.dwWidth = surf_width;
	ddsd.dwHeight = surf_height; 

	// attempt to create surface
	ddrval=DirectDraw->lpDD->CreateSurface(&ddsd,&surf,NULL);

	// created ok?
	if (ddrval!=DD_OK) {

		// no, release the bitmap and return failure to caller
		DeleteObject(bit);
		return NULL;

	} else {

		// yes, get a DC for the surface
		surf->GetDC(&hdc);

		// generate a compatible DC
		HDC bit_dc=CreateCompatibleDC(hdc);

		// blit the interface to the surface
		SelectObject(bit_dc,bit);
		BitBlt(hdc,0,0,surf_width,surf_height,bit_dc,0,0,SRCCOPY);

		// release the DCs
		surf->ReleaseDC(hdc);
		DeleteDC(bit_dc);

	}

	// clear bitmap 
	DeleteObject(bit);

	// return pointer to caller
	return surf;
}

//////////////////////////////////////////////////鼠标按下处理
void CMenu::MouseDown()
{
	POINT point;
	GetCursorPos(&point);

	if (!IsEmpty())//是否有菜单?
	{
		if (top->HaveButton)//是否有按钮?
		{
			for (int i=0;i<top->ButtonID;i++)//鼠标是否在有在按钮上?
			{
				if (point.x>top->MenuX+top->ButtonX[i] &&
					point.x<top->MenuX+top->ButtonX[i]+top->ButtonWidth[i] &&
					point.y>top->MenuY+top->ButtonY[i] &&
					point.y<top->MenuY+top->ButtonY[i]+top->ButtonHeight[i])
				{
					if ((strcmp(top->MenuName,"TileMenu0")==0 ||strcmp(top->MenuName,"TileMenu1")==0) && i>3)//选择Tile
					{
						TileID=i;
						return;
					}

					LPCTSTR A=top->MenuName;
					LPCTSTR B=top->ButtonName[i];
					char* c = new char[strlen(A)+strlen(B)+2];

					strcpy(c,A);
					strcat(c,"_");
					strcat(c,B);

					ButtonClick(c);

					delete c;
					return;
				}
			}
		}
		else//菜单如果没有按钮
		{
			NotButtonClick(top->MenuName);
			return;
		}
		
		if(point.x<top->MenuX ||
				point.x>top->MenuX+top->MenuWidth ||
				point.y<top->MenuY ||
				point.y>top->MenuY+top->MenuHeight)//鼠标是否在菜单外面?
		ChangeTile();//change Tile. 
	}
	else
		ChangeTile();

}

//////////////////////////////////////////////////鼠标移动处理
void CMenu::MouseMove()
{
	if (!IsEmpty() && top->HaveButton)//是否有菜单并有按钮?
	{
		POINT point;
		GetCursorPos(&point);

		for (int i=0;i<top->ButtonID;i++)
		{
			if (point.x>top->MenuX+top->ButtonX[i] &&
				point.x<top->MenuX+top->ButtonX[i]+top->ButtonWidth[i] &&
				point.y>top->MenuY+top->ButtonY[i] &&
				point.y<top->MenuY+top->ButtonY[i]+top->ButtonHeight[i])//鼠标在按钮或Tile上时
			{
				if ((strcmp(top->MenuName,"TileMenu0")==0 ||strcmp(top->MenuName,"TileMenu1")==0) && i>3)//如杲在Tile上就反回
					return;
					

				LPCTSTR A=top->MenuName;
				LPCTSTR B=top->ButtonName[i];
				char* c = new char[strlen(A)+strlen(B)+5];

				strcpy(c,A);
				strcat(c,"_");
				strcat(c,B);
				strcat(c,"_On");

				ButtonOn(i,c);

				delete c;
				return;
			}
			else//如果不在按钮或Tile上
			{
				if ((strcmp(top->MenuName,"TileMenu0")!=0 || strcmp(top->MenuName,"TileMenu1")!=0) && i<4)//是否不是Tile
				{

					DirectDraw->DDSetColorKey(top->ButtonSurface[i],CLR_INVALID);
					top->MenuSurface->BltFast(top->ButtonX[i],top->ButtonY[i],
												top->ButtonSurface[i],
												NULL,
												DDBLTFAST_WAIT/*|DDBLTFAST_SRCCOLORKEY*/);
				}
			}
		}
		
		if (point.x>top->MenuX &&
				point.x<top->MenuX+top->MenuWidth &&
				point.y>top->MenuY &&
				point.y<top->MenuY+top->MenuHeight &&
				LeftButton)
		{
			nowx=point.x;
			nowy=point.y;
			MenuMoveable=true;
			return;
		}
	}
}

void CMenu::ButtonOn(int buttonid,LPCTSTR buttonname)
{
	int width,height;
	LPDIRECTDRAWSURFACE7 tempsurface;

	char* tempname = new char[strlen(buttonname)+10];
	strcpy(tempname,"pic\\");
	strcat(tempname,buttonname);
	strcat(tempname,".bmp");
	tempsurface=BMPToSurface(tempname,
							width,height);
	delete tempname;

//	DirectDraw->DDSetColorKey(tempsurface,CLR_INVALID);

	top->MenuSurface->BltFast(top->ButtonX[buttonid],top->ButtonY[buttonid],
							tempsurface,
							NULL,
							DDBLTFAST_WAIT/*|DDBLTFAST_SRCCOLORKEY*/);


}




//		████████████████████████████
//		██╭--------------------------------------------╮██
//		█╭╯		以 下 为 鼠 标 事 件 区		  ╰╮█
//		█|											    |█
//		█|												|█
//		█|	★鼠 标 单 击 与 鼠 标 移 动★		|█
//		█╰------------------------------------------------╯█
//		████████████████████████████

//////////////////////////////////////////////////鼠标单击事情
void ButtonClick(LPCTSTR name)
{
	if		(strcmp(name,"TileMenu1_End")==0)
	{
		Menu->DelMenu();
	}

	else if (strcmp(name,"TileMenu1_Move")==0)
	{
		MapMove=!MapMove;
	}

	else if (strcmp(name,"TileMenu1_Up")==0)
	{
		Menu1-=30;
		if (Menu1<1)
			Menu1=0;

		Menu->DelMenu();
		TileMenu1();
	}

		else if (strcmp(name,"TileMenu1_Down")==0)
	{
		Menu1+=30;
		if (Menu1>540+18)
			Menu1=600-48;

		Menu->DelMenu();
		TileMenu1();
	}

	else if (strcmp(name,"Exit_Yes")==0)
	{
		DestroyWindow(hWnd);
	}
	
	else if (strcmp(name,"Exit_No")==0)
	{
		Menu->DelMenu();
	}

	else if (strcmp(name,"TileMenu0_End")==0)
	{
		Menu->DelMenu();
	}

	else if (strcmp(name,"TileMenu0_Move")==0)
	{
		MapMove=!MapMove;
	}

	else if (strcmp(name,"")==0)
	{
	}
}

void NotButtonClick(LPCTSTR name)
{
		if		(strcmp(name,"About")==0)
	{
		Menu->DelMenu();
		return;
	}
		else if (strcmp(name,"")==0)
	{
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -