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

📄 modelwindow.cpp

📁 骨骼动画....把魔兽模型解出的代码..
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//+-----------------------------------------------------------------------------
//| Included files
//+-----------------------------------------------------------------------------
#include "ModelWindow.h"


//+-----------------------------------------------------------------------------
//| Global objects
//+-----------------------------------------------------------------------------
MODEL_WINDOW ModelWindow;


//+-----------------------------------------------------------------------------
//| Constructor
//+-----------------------------------------------------------------------------
MODEL_WINDOW::MODEL_WINDOW()
{
	CurrentView = INVALID_INDEX;
	ActiveLeftButtonView = INVALID_INDEX;
	ActiveRightButtonView = INVALID_INDEX;

	ViewList[VIEW_INDEX_TOP_LEFT].ViewType = VIEW_TYPE_XZ_FRONT;
	ViewList[VIEW_INDEX_TOP_RIGHT].ViewType = VIEW_TYPE_YZ_BACK;
	ViewList[VIEW_INDEX_BOTTOM_LEFT].ViewType = VIEW_TYPE_XY_FRONT;
	ViewList[VIEW_INDEX_BOTTOM_RIGHT].ViewType = VIEW_TYPE_NONE;

	PivotPoint = D3DXVECTOR3(0.0f, 0.0f, 0.0f);

	Selecting = FALSE;
	DeselectMode = FALSE;
	SelectingView = INVALID_INDEX;
	SelectionStart = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	SelectionEnd = D3DXVECTOR3(0.0f, 0.0f, 0.0f);

	WindowActive = FALSE;
}


//+-----------------------------------------------------------------------------
//| Destructor
//+-----------------------------------------------------------------------------
MODEL_WINDOW::~MODEL_WINDOW()
{
	//Empty
}


//+-----------------------------------------------------------------------------
//| Creates a new window
//+-----------------------------------------------------------------------------
BOOL MODEL_WINDOW::Create()
{
	INT i;

	FrameInfo.Title = "Model Editor";
	FrameInfo.Width = DEFAULT_MODEL_WINDOW_WIDTH;
	FrameInfo.Height = DEFAULT_MODEL_WINDOW_HEIGHT;
	FrameInfo.Menu = ::LoadMenu(GetModuleHandle(NULL), MAKEINTRESOURCE(ModelMenu));
	FrameInfo.Style &= (~WS_VISIBLE);
	FrameInfo.Style |= (WS_MAXIMIZEBOX | WS_SIZEBOX);

	if(!WINDOW_FRAME::Create()) return FALSE;

	for(i = 0; i < 4; i++)
	{
		ViewList[i].Window = CreateView();
		if(ViewList[i].Window == NULL) return FALSE;
	}

	ResizeViews();

	for(i = 0; i < 4; i++)
	{
		if(!ViewList[i].GraphicsWindow.Create(ViewList[i].Window)) return FALSE;
	}

	return TRUE;
}


//+-----------------------------------------------------------------------------
//| Destroys the window
//+-----------------------------------------------------------------------------
VOID MODEL_WINDOW::Destroy()
{
	INT i;

	CurrentView = INVALID_INDEX;

	for(i = 0; i < 4; i++)
	{
		ViewList[i].GraphicsWindow.Destroy();
		SAFE_DESTROY(ViewList[i].Window);
	}

	WINDOW_FRAME::Destroy();
}


//+-----------------------------------------------------------------------------
//| Handles the window messages
//+-----------------------------------------------------------------------------
LRESULT MODEL_WINDOW::MessageHandler(UINT Message, WPARAM W, LPARAM L)
{
	switch(Message)
	{
		case WM_PAINT:
		{
			UpdateAndRender();
			ValidateRect(Window, NULL);
			return 0;
		}

		case WM_SIZE:
		{
			WindowActive = (W != SIZE_MINIMIZED);
			ResizeViews();
			return 0;
		}

		case WM_SIZING:
		{
			ResizeViews();
			return 0;
		}

		case WM_SHOWWINDOW:
		{
			WindowActive = static_cast<BOOL>(W);
			return 0;
		}

		case WM_CLOSE:
		{
			Hide();
			return 0;
		}

		case WM_DESTROY:
		{
			return 0;
		}
	}

	return DefWindowProc(Window, Message, W, L);
}


//+-----------------------------------------------------------------------------
//| Handles the window menu messages
//+-----------------------------------------------------------------------------
LRESULT MODEL_WINDOW::MenuHandler(WORD MenuItem)
{
	switch(MenuItem)
	{
		case ModelViewNone:
		{
			if(CurrentView == INVALID_INDEX)
			{
				Error.SetMessage("No view is selected!");
				Error.DisplayMessage(Window);
				Error.ClearMessage();
				return 0;
			}

			ViewList[CurrentView].ViewType = VIEW_TYPE_NONE;

			return 0;
		}

		case ModelViewXYFront:
		{
			if(CurrentView == INVALID_INDEX)
			{
				Error.SetMessage("No view is selected!");
				Error.DisplayMessage(Window);
				Error.ClearMessage();
				return 0;
			}

			ViewList[CurrentView].ViewType = VIEW_TYPE_XY_FRONT;

			return 0;
		}

		case ModelViewXYBack:
		{
			if(CurrentView == INVALID_INDEX)
			{
				Error.SetMessage("No view is selected!");
				Error.DisplayMessage(Window);
				Error.ClearMessage();
				return 0;
			}

			ViewList[CurrentView].ViewType = VIEW_TYPE_XY_BACK;

			return 0;
		}

		case ModelViewXZFront:
		{
			if(CurrentView == INVALID_INDEX)
			{
				Error.SetMessage("No view is selected!");
				Error.DisplayMessage(Window);
				Error.ClearMessage();
				return 0;
			}

			ViewList[CurrentView].ViewType = VIEW_TYPE_XZ_FRONT;

			return 0;
		}

		case ModelViewXZBack:
		{
			if(CurrentView == INVALID_INDEX)
			{
				Error.SetMessage("No view is selected!");
				Error.DisplayMessage(Window);
				Error.ClearMessage();
				return 0;
			}

			ViewList[CurrentView].ViewType = VIEW_TYPE_XZ_BACK;

			return 0;
		}

		case ModelViewYZFront:
		{
			if(CurrentView == INVALID_INDEX)
			{
				Error.SetMessage("No view is selected!");
				Error.DisplayMessage(Window);
				Error.ClearMessage();
				return 0;
			}

			ViewList[CurrentView].ViewType = VIEW_TYPE_YZ_FRONT;

			return 0;
		}

		case ModelViewYZBack:
		{
			if(CurrentView == INVALID_INDEX)
			{
				Error.SetMessage("No view is selected!");
				Error.DisplayMessage(Window);
				Error.ClearMessage();
				return 0;
			}

			ViewList[CurrentView].ViewType = VIEW_TYPE_YZ_BACK;

			return 0;
		}

		case ModelViewResize:
		{
			SetWindowDimension(DEFAULT_MODEL_WINDOW_WIDTH, DEFAULT_MODEL_WINDOW_HEIGHT);
			ResizeViews();
			return 0;
		}

		case ModelViewResetPivotPoint:
		{
			PivotPoint = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
			return 0;
		}

		case ModelViewResetCamera:
		{
			INT i;

			for(i = 0; i < 4; i++)
			{
				ViewList[i].Position = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
				ViewList[i].Scale = 1.0f;
			}

			return 0;
		}

		case ModelSelectionAll:
		{
			SelectAllVertices();
			SelectAllFaces();
			return 0;
		}

		case ModelSelectionNone:
		{
			SelectNoVertices();
			SelectNoFaces();
			return 0;
		}

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

			return 0;
		}

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

			return 0;
		}

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

			return 0;
		}
	}

	return 0;
}


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


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


//+-----------------------------------------------------------------------------
//| Updates and renders the window
//+-----------------------------------------------------------------------------
VOID MODEL_WINDOW::UpdateAndRender()
{
	INT i;
	INT View;
	POINT MousePosition;
	POINT TempMousePosition;
	POINT LocalMousePosition;

	ModelKeyboard.Update();
	ModelMouse.Update();

	GetCursorPos(&MousePosition);
	TempMousePosition = MousePosition;
	ScreenToClient(Window, &MousePosition);
	View = GetView(MousePosition.x, MousePosition.y);

	DeselectMode = ModelKeyboard.KeyDown(KEY_LEFTSHIFT);

	if(ModelMouse.ButtonPressed(BUTTON_LEFT))
	{
		SelectView(GetView(MousePosition.x, MousePosition.y));
		ActiveLeftButtonView = View;
	}

	if(ModelMouse.ButtonReleased(BUTTON_LEFT))
	{
		ActiveLeftButtonView = INVALID_INDEX;

		if(Selecting)
		{
			Select();
			Selecting = FALSE;
			SelectingView = INVALID_INDEX;
		}
	}

	if(ModelMouse.ButtonPressed(BUTTON_RIGHT))
	{
		SelectView(GetView(MousePosition.x, MousePosition.y));
		ActiveRightButtonView = View;
	}

	if(ModelMouse.ButtonReleased(BUTTON_RIGHT))
	{
		ActiveRightButtonView = INVALID_INDEX;
	}

	if(ActiveLeftButtonView != INVALID_INDEX)
	{
		if(ModelMouse.ButtonPressed(BUTTON_LEFT))
		{
			if(ModelKeyboard.KeyDown(KEY_LEFTCTRL))
			{
				LocalMousePosition = TempMousePosition;
				ScreenToClient(ViewList[ActiveLeftButtonView].Window, &LocalMousePosition);
				PivotPoint = ScreenPositionToWorldPosition(ActiveLeftButtonView, BuildProjectionPlane(ActiveLeftButtonView, PivotPoint), LocalMousePosition.x, LocalMousePosition.y);
			}
			else if(!ModelKeyboard.KeyDown(KEY_LEFTALT))
			{
				Selecting = TRUE;
				SelectingView = ActiveLeftButtonView;
				LocalMousePosition = TempMousePosition;
				ScreenToClient(ViewList[ActiveLeftButtonView].Window, &LocalMousePosition);
				SelectionStart = ScreenPositionToWorldPosition(ActiveLeftButtonView, BuildProjectionPlane(ActiveLeftButtonView, D3DXVECTOR3(0.0f, 0.0f, 0.0f)), LocalMousePosition.x, LocalMousePosition.y);
				SelectionEnd = SelectionStart;
			}
		}
	}

	if(Selecting)
	{
		if((ActiveLeftButtonView == SelectingView) && (CurrentView == SelectingView) && !ModelKeyboard.KeyDown(KEY_LEFTALT))
		{
			LocalMousePosition = TempMousePosition;
			ScreenToClient(ViewList[ActiveLeftButtonView].Window, &LocalMousePosition);
			SelectionEnd = ScreenPositionToWorldPosition(ActiveLeftButtonView, BuildProjectionPlane(ActiveLeftButtonView, D3DXVECTOR3(0.0f, 0.0f, 0.0f)), LocalMousePosition.x, LocalMousePosition.y);
		}
		else
		{
			Selecting = FALSE;
			SelectingView = INVALID_INDEX;
		}
	}

	if(ActiveLeftButtonView != INVALID_INDEX)
	{
		if(ModelKeyboard.KeyDown(KEY_LEFTALT) && ModelMouse.ButtonDown(BUTTON_LEFT))
		{
			ViewList[ActiveLeftButtonView].Scale -= (static_cast<FLOAT>(ModelMouse.GetDY()) * CAMERA_FACTOR_DISTANCE * 10.0f * ViewList[ActiveLeftButtonView].Scale);
		}

		if(ViewList[ActiveLeftButtonView].Scale < 0.01f) ViewList[ActiveLeftButtonView].Scale = 0.01f;
		if(ViewList[ActiveLeftButtonView].Scale > 100.0f) ViewList[ActiveLeftButtonView].Scale = 100.0f;
	}

	if(CurrentView != INVALID_INDEX)
	{

⌨️ 快捷键说明

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