mainwindow.cpp

来自「骨骼动画....把魔兽模型解出的代码..」· C++ 代码 · 共 1,323 行 · 第 1/3 页

CPP
1,323
字号
		case MainWindowsGlobalSequenceManager:
		{
			GlobalSequenceManagerWindow.Show();
			GlobalSequenceManagerWindow.Restore();
			GlobalSequenceManagerWindow.Activate();
			return 0;
		}

		case MainWindowsMpqBrowser:
		{
			MpqWindow.Show();
			MpqWindow.Restore();
			MpqWindow.Activate();
			return 0;
		}

		case MainExtrasColoredText:
		{
			ColoredTextDialog.Display(Window);
			return 0;
		}

		case MainExtrasLoadingScreen:
		{
			std::string TextureFileName;

			if(!LoadingScreenDialog.Display(Window, TextureFileName)) return 0;

			TextureFileName = Common.GetFileName(TextureFileName);

			if(!SaveFileIfNeccessary(Cancel))
			{
				Error.DisplayMessage(Window);
				Error.ClearMessage();
				return 0;
			}

			if(Cancel) return 0;

			if(!NewLoadingScreen(TextureFileName))
			{
				Error.DisplayMessage(Window);
				Error.ClearMessage();
				return 0;
			}

			return 0;
		}

		case MainHelpHelp:
		{
			if(!DisplayHelp())
			{
				Error.DisplayMessage(Window);
				Error.ClearMessage();
				return 0;
			}

			return 0;
		}

		case MainHelpAbout:
		{
			MessageBoxEx(Window, EDITOR_HELP_MESSAGE.c_str(), EDITOR_HELP_TITLE.c_str(), MB_ICONINFORMATION, CURRENT_LANGUAGE);
			return 0;
		}

		case MainTeamColorRed:
		case MainTeamColorBlue:
		case MainTeamColorTeal:
		case MainTeamColorPurple:
		case MainTeamColorYellow:
		case MainTeamColorOrange:
		case MainTeamColorGreen:
		case MainTeamColorPink:
		case MainTeamColorGray:
		case MainTeamColorLightBlue:
		case MainTeamColorDarkGreen:
		case MainTeamColorBrown:
		case MainTeamColorBlack:
		{
			CheckMenuItem(TeamColor.GetCurrentTeamColorId(), FALSE);
			TeamColor.SetCurrentTeamColorId(MenuItem);
			CheckMenuItem(TeamColor.GetCurrentTeamColorId(), TRUE);
			return 0;
		}
	}

	if(MenuItem < ModelHistory.GetMinMenuItemId()) return 0;
	if(MenuItem > ModelHistory.GetMaxMenuItemId()) return 0;

	if(!SaveFileIfNeccessary(Cancel))
	{
		Error.DisplayMessage(Window);
		Error.ClearMessage();
		return 0;
	}

	if(Cancel) return 0;

	if(!CloseFile(TRUE)) return FALSE;

	FileName = ModelHistory.GetFileName(MenuItem);
	Common.SetCurrentDirectory(Common.GetPath(FileName));

	if(!LoadByForce(FileName))
	{
		Error.DisplayMessage(Window);
		Error.ClearMessage();
		return 0;
	}

	return 0;
}


//+-----------------------------------------------------------------------------
//| Handles the window control messages
//+-----------------------------------------------------------------------------
LRESULT MAIN_WINDOW::ControlHandler(HWND Control, WORD Code)
{
	return 0;
}


//+-----------------------------------------------------------------------------
//| Handles the window notify messages
//+-----------------------------------------------------------------------------
LRESULT MAIN_WINDOW::NotifyHandler(HWND Control, UINT Code, NMHDR* Header)
{
	return 0;
}


//+-----------------------------------------------------------------------------
//| Returns the history menu
//+-----------------------------------------------------------------------------
HMENU MAIN_WINDOW::GetHistoryMenu()
{
	HMENU Menu;
	HMENU SubMenu;

	Menu = ::GetMenu(Window);
	if(Menu == NULL) return NULL;

	SubMenu = ::GetSubMenu(Menu, 1);
	if(SubMenu == NULL) return NULL;

	return SubMenu;
}


//+-----------------------------------------------------------------------------
//| Updates and renders the window
//+-----------------------------------------------------------------------------
VOID MAIN_WINDOW::UpdateAndRender(INT TimeDifference)
{
	if(TimeDifference > 0)
	{
		Keyboard.Update();
		Mouse.Update();

		Camera.Update(Window);
		Graphics.SetCamera(Camera);

		if(Keyboard.KeyDown(KEY_LEFTCTRL))
		{
			if(Mouse.ButtonPressed(BUTTON_LEFT))
			{
				POINT Point;
				BOOL InsideScreen;
				MODEL_GEOSET* Geoset;
				MODEL_MATERIAL* Material;
				D3DXVECTOR3 RayPosition;
				D3DXVECTOR3 RayDirection;

				GetCursorPos(&Point);
				ScreenToClient(Window, &Point);

				InsideScreen = FALSE;
				while(TRUE)
				{
					if(Point.x < 1) break;
					if(Point.y < 1) break;
					if(Point.x >= (GetWidth() - 1)) break;
					if(Point.y >= (GetHeight() - 1)) break;

					InsideScreen = TRUE;
					break;
				}

				if(InsideScreen)
				{
					Graphics.BuildRay(Point, GetWidth(), GetHeight(), RayPosition, RayDirection);

					Geoset = Model.GetIntersectedGeoset(RayPosition, RayDirection);
					if(Geoset != NULL)
					{
						GeosetManagerWindow.SelectGeoset(Geoset);
						FlashingGeoset.SetGeoset(Geoset);

						if(Geoset->MaterialNode.IsAttached())
						{
							Material = Geoset->MaterialNode.GetObjectData();
							MaterialManagerWindow.SelectMaterial(Material);
						}
						else
						{
							MaterialManagerWindow.SelectMaterial(NULL);
						}
					}
					else
					{
						GeosetManagerWindow.SelectGeoset(NULL);
						MaterialManagerWindow.SelectMaterial(NULL);
					}
				}
			}
		}
	}

	if(!WindowActive) return;

	if(Graphics.BeginRender(GraphicsWindow))
	{
		Render(TimeDifference);
		Graphics.EndRender();
	}
}


//+-----------------------------------------------------------------------------
//| Checks if a model is loaded
//+-----------------------------------------------------------------------------
BOOL MAIN_WINDOW::ModelIsLoaded()
{
	return ModelLoaded;
}


//+-----------------------------------------------------------------------------
//| Makes the model unsaved
//+-----------------------------------------------------------------------------
VOID MAIN_WINDOW::MakeModelUnsaved()
{
	ModelSaved = FALSE;

	UpdateWindowStatus(TRUE);
}


//+-----------------------------------------------------------------------------
//| Makes the model never saved
//+-----------------------------------------------------------------------------
VOID MAIN_WINDOW::MakeModelNeverSaved()
{
	ModelNeverSaved = TRUE;

	UpdateWindowStatus(TRUE);
}


//+-----------------------------------------------------------------------------
//| Loads a model, texture or MPQ archive by force
//+-----------------------------------------------------------------------------
BOOL MAIN_WINDOW::LoadByForce(CONST std::string& FileName)
{
	std::string Extention;
	std::string RealFileName;

	RealFileName = Common.GetFileName(FileName);

	Extention = Common.LowerCase(Common.GetExtention(FileName));
	if(MpqWindow.IsMpqExtention(Extention))
	{
		if(!MpqWindow.OpenMpqByName(RealFileName)) return FALSE;

		MpqWindow.Show();
		MpqWindow.Restore();
		MpqWindow.Activate();

		return TRUE;
	}
	else if(ResourceLoader.IsModelExtention(Extention))
	{
		BUFFER Buffer;

		if(!FileLoader.LoadFromFile(RealFileName, Buffer))
		{
			Error.SetMessage("Unable to load \"" + FileName + "\", file does not exist!");
			return FALSE;
		}

		if(!LoadModelFromBuffer(RealFileName, Buffer)) return FALSE;

		return TRUE;
	}
	else if(ResourceLoader.IsTextureExtention(Extention))
	{
		BUFFER Buffer;

		if(!FileLoader.LoadFromFile(RealFileName, Buffer))
		{
			Error.SetMessage("Unable to load \"" + FileName + "\", file does not exist!");
			return FALSE;
		}

		if(!LoadTextureFromBuffer(RealFileName, Buffer)) return FALSE;

		return TRUE;
	}

	Error.SetMessage("Unable to load \"" + FileName + "\", unknown extention!");
	return FALSE;
}


//+-----------------------------------------------------------------------------
//| Loads a file from a buffer
//+-----------------------------------------------------------------------------
BOOL MAIN_WINDOW::LoadFileFromBuffer(CONST std::string& FileName, BUFFER& Buffer, BOOL SkipSaveTest)
{
	BOOL Cancel;
	std::string Extention;

	if(!SkipSaveTest)
	{
		if(!SaveFileIfNeccessary(Cancel)) return FALSE;
		if(Cancel) return TRUE;
	}

	if(!CloseFile(TRUE)) return FALSE;

	Extention = Common.LowerCase(Common.GetExtention(FileName));
	if(ResourceLoader.IsModelExtention(Extention))
	{
		if(!LoadModelFromBuffer(FileName, Buffer))
		{
			CloseFile();
			return FALSE;
		}

		return TRUE;
	}
	else if(ResourceLoader.IsTextureExtention(Extention))
	{
		if(!LoadTextureFromBuffer(FileName, Buffer))
		{
			CloseFile();
			return FALSE;
		}

		return TRUE;
	}

	Error.SetMessage("Unable to load \"" + FileName + "\", unknown extention!");
	return FALSE;
}


//+-----------------------------------------------------------------------------
//| Loads a model from a buffer
//+-----------------------------------------------------------------------------
BOOL MAIN_WINDOW::LoadModelFromBuffer(CONST std::string& FileName, BUFFER& Buffer)
{
	FLOAT Distance;

	if(!ResourceLoader.LoadModel(Model, FileName, Buffer)) return FALSE;

	Model.CalculateBoundsRadius();
	Distance = std::max(Model.GetBoundsRadius(), Model.Data().Info.Extent.Radius);
	Camera.SetDefaultTarget(Model.GetBoundsCenter());
	Camera.SetDefaultDistance(Distance * CAMERA_FACTOR_MODEL);

	ModelLoaded = TRUE;
	ModelSaved = TRUE;
	ModelNeverSaved = FALSE;

	ModelFileName = Common.GetFileName(FileName);

	Camera.Reset(Window);
	UpdateWindowStatus(TRUE);

	return TRUE;
}


//+-----------------------------------------------------------------------------
//| Loads a texture from a buffer
//+-----------------------------------------------------------------------------
BOOL MAIN_WINDOW::LoadTextureFromBuffer(CONST std::string& FileName, BUFFER& Buffer)
{
	TEXTURE* Texture;

	if(!TextureManager.Load(FileName)) return FALSE;

	Texture = TextureManager.GetTexture(FileName);
	if(Texture == NULL) return FALSE;

	if(!Model.CreateTextureModel(FileName, Texture->GetWidth(), Texture->GetHeight(), Texture->GetRealWidth(), Texture->GetRealHeight())) return FALSE;

	Model.CalculateBoundsRadius();
	Camera.SetDefaultPitch(0);
	Camera.SetDefaultTarget(Model.GetBoundsCenter());
	Camera.SetDefaultDistance(Model.GetBoundsRadius() * CAMERA_FACTOR_MODEL);

	ModelLoaded = TRUE;
	ModelSaved = TRUE;
	ModelNeverSaved = TRUE;

	ModelFileName = FileName + ".mdl";

	Camera.Reset(Window);
	UpdateWindowStatus(TRUE);

	return TRUE;
}


//+-----------------------------------------------------------------------------
//| Registers the desired extentions
//+-----------------------------------------------------------------------------
BOOL MAIN_WINDOW::RegisterExtentions(REGISTER_INFO& RegisterInfo)
{
	std::string Program;
	std::string ImageIconPath;
	std::string ModelIconPath;
	std::string ArchiveIconPath;

	Program = Common.GetProgramFileName();
	ImageIconPath = Program + ",4";
	ModelIconPath = Program + ",5";
	ArchiveIconPath = Program + ",3";

	if(RegisterInfo.Mdl)
	{
		if(!Register.RegisterExtention("mdl", "Warcraft 3 Model", ModelIconPath.c_str())) return FALSE;
	}

	if(RegisterInfo.Mdx)
	{

⌨️ 快捷键说明

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