DWORD ToolGridVisibleEdit::GetSelectGrid(void)
{
	ui::Manager::_tagINPUTINFO *pInputInfo = ui::Manager::GetInstance()->GetInputInfoBuffer();
	//计算选中的格子
	static GameSelectRay cSelRay;
	RECT rc;
	EditWnd *pEditWnd = CommUI::GetInstance()->GetEditWnd();
	pEditWnd->GetWndRect(&rc);
	cSelRay.CalculateRay(&rc,pEditWnd->GetCamera(),pInputInfo->ptMouse);
	//显示格子
	EditMap *pMap = CommDB::GetInstance()->GetMap();
	VisibleSet *pSet = pMap->GetVisibleSet();
	DWORD *pVIArray = pSet->GetVisibleIndexArray();
	TerrainMesh *pMesh = CommDB::GetInstance()->GetTerrainMesh();
	TerrainMesh::_tagGrid *pGrid(NULL);
	TerrainMesh::_tagVertex *pVertexArray = pMesh->GetVertexList();
	D3DXVECTOR3 pos[4];
	DWORD dwSelGrid = 0xFFFFFFFF;
	float fDistance = 100000000.0f;
	float fTempDist;

	for(DWORD n = 0; n< pSet->GetVisibleIndexCount(); n++)
	{
		DWORD dwGridIndex = pVIArray[n];
		pGrid = pMesh->GetGrid(dwGridIndex);

		for(DWORD v = 0; v < 4; v++)
		{
			pos[v] = pVertexArray[pGrid->dwVertexIndex[v]].vPosition;
		}

	    if (cSelRay.IntersectTri(&pos[0],&pos[1],&pos[2],NULL,&fTempDist))
		{
			if (fTempDist < fDistance)
			{
				fDistance = fTempDist;
				dwSelGrid = dwGridIndex;
			}
		}
		else
			if (cSelRay.IntersectTri(&pos[0],&pos[2],&pos[3],NULL,&fTempDist))
			{	 
				if (fTempDist < fDistance)
				{
					fDistance = fTempDist;
					dwSelGrid = dwGridIndex;
				}

				
			}
	}
	return dwSelGrid;
}
void ToolDoorEdit::MessageProc(BOOL bActive)
{
	//切换
	ui::Manager::_tagINPUTINFO *pInputInfo = ui::Manager::GetInstance()->GetInputInfoBuffer();

	EditWnd *pEditWnd = CommUI::GetInstance()->GetEditWnd();

	WndDoorEdit *pWndDoorEdit = pEditWnd->GetWndDoorEdit();

	int iDoorIndex = pWndDoorEdit->GetCurDoorIndex() + 1;

	int iDoorCount = pWndDoorEdit->GetDoorCount();

	if (pInputInfo->eType == UIMT_KB_KEYDOWN)
	{
		switch(pInputInfo->dwData)
		{
		case DIK_Q:
			{
				m_eEditStatus = (m_eEditStatus == DEST_SELECT)? DEST_PALACE : DEST_SELECT;
			}
			break;
		case DIK_V:
			//if (pInputInfo->byKeyBuffer[DIK_LCONTROL])
			//{
			//	if (m_eEditStatus == DEST_SELECT)
			//	{

			//	}
			//}
			break;
		case DIK_DELETE:
			if (m_eEditStatus == DEST_SELECT)
			{
				int iSelSize = (int)m_vSelGridIndexArray.size();
				if (iSelSize)
				{
					////清除所选择部分D
					CmdMapDelDoor *pCmd = new CmdMapDelDoor;
					for(int i = 0; i < iSelSize; i++)
					{
						pCmd->AddGridIndex((DWORD)m_vSelGridIndexArray[i]);
					}
					CmdMgr::GetInstance()->Do(pCmd);
				}
			}
			break;
		}
	}

	//选择
	switch(m_eEditStatus)
	{
	case DEST_SELECT:
		//选择
		if (bActive)
		{
			switch(pInputInfo->eType)
			{
			case  UIMT_MS_BTNDOWN:
				if (pInputInfo->dwData == MK_LBUTTON)
				{
					m_bDrag = TRUE;
					m_ptDragStart.x = GET_X_LPARAM(pInputInfo->lParam);
					m_ptDragStart.y = GET_Y_LPARAM(pInputInfo->lParam);

				}
				break;
			case  UIMT_MS_BTNUP:
				if (pInputInfo->dwData == MK_LBUTTON)
				{
					if (m_bDrag)
					{
						m_ptDragEnd.x = GET_X_LPARAM(pInputInfo->lParam);
						m_ptDragEnd.y = GET_Y_LPARAM(pInputInfo->lParam);
						m_bDrag = FALSE;
						//结束,开始选择//画框
						SelectFrustum cSelFrustum;
						RECT rc;
						pEditWnd->GetWndRect(&rc);
						cSelFrustum.CalculateFrustum(&rc,pEditWnd->GetCamera(),m_ptDragStart,m_ptDragEnd);
						std::vector<int>vTempArray;
						//循环可见格子
						EditMap *pMap = CommDB::GetInstance()->GetMap();
						VisibleSet *pSet = pMap->GetVisibleSet();
						DWORD *pVIArray = pSet->GetVisibleIndexArray();
						DWORD dwMapWidth = pMap->GetWidth();
						DWORD dwMapDepth = pMap->GetDepth();
						for(DWORD n = 0; n< pSet->GetVisibleIndexCount(); n++)
						{
							DWORD dwGridIndex = pVIArray[n];
							Grid *pGrid = pMap->GetGrid(dwGridIndex);
							if (pGrid->GetDoorIndex())
							{

								//计算可视格子  in  选择锥
								D3DXVECTOR3 pos[4];
								D3DXVECTOR3 vCenter;
								pMap->GetGridPosition(dwGridIndex,pos,&vCenter);
								/*float x = (float)(dwGridIndex % dwMapWidth);
								float z = (float)(dwGridIndex / dwMapWidth);

								pos[0] = D3DXVECTOR3(x,0,z);
								pos[1] = D3DXVECTOR3(x+1,0,z);
								pos[2] = D3DXVECTOR3(x,0,z+1);
								pos[3] = D3DXVECTOR3(x+1,0,z+1);*/

								
								

								if (cSelFrustum.TestPoint(&vCenter))
								{
									//这个格子被选中
									vTempArray.push_back(dwGridIndex);
								}
							}
						}

						//是否是CTRL按住
						if (pInputInfo->byKeyBuffer[DIK_LCONTROL] & 0x80)
						{
							//减模式
							std::vector<int>::iterator result;

							std::vector<int> vSelIndexArray = m_vSelGridIndexArray;

							for(std::vector<int>::iterator it = vSelIndexArray.begin();
								it != vSelIndexArray.end();it++)
							{		
								int iGridIndex = *it;

								//find 
								result = find(vTempArray.begin(),vTempArray.end(),iGridIndex);
								if  (result != vTempArray.end())
								{
									result = find(m_vSelGridIndexArray.begin(),m_vSelGridIndexArray.end(),iGridIndex);
									if (result != vTempArray.end())
									{
										//找到,删除
										m_vSelGridIndexArray.erase(result);
									}
								}
							}							
						}
						else
							if (pInputInfo->byKeyBuffer[DIK_LSHIFT] & 0x80)
							{
								//添加模式
								std::vector<int>::iterator result;
								for(DWORD n = 0; n < vTempArray.size(); n++)
								{	
									int iGridIndex = vTempArray[n];
									result = find(m_vSelGridIndexArray.begin(),m_vSelGridIndexArray.end(),iGridIndex);
									if (result == m_vSelGridIndexArray.end())
									{
										m_vSelGridIndexArray.push_back(iGridIndex);
									}
								}
							}
							else
							{
								//正常选择模式
								m_vSelGridIndexArray.clear();
								for(DWORD n = 0; n < vTempArray.size(); n++)
								{
									m_vSelGridIndexArray.push_back(vTempArray[n]);
								}
							}
					}
				}
				break;
			}

		}
		break;
	case DEST_PALACE:
		//放置
		{
			if (bActive)
			{
				if (pInputInfo->dwMouseButtonState & MK_LBUTTON)
				{
					//计算选中的格子
					GameSelectRay cSelRay;
					RECT rc;
					pEditWnd->GetWndRect(&rc);
					cSelRay.CalculateRay(&rc,pEditWnd->GetCamera(),pInputInfo->ptMouse);
					//显示格子
					EditMap *pMap = CommDB::GetInstance()->GetMap();
					VisibleSet *pSet = pMap->GetVisibleSet();
					DWORD *pVIArray = pSet->GetVisibleIndexArray();
					DWORD dwMapWidth = pMap->GetWidth();
					DWORD dwMapDepth = pMap->GetDepth();
					for(DWORD n = 0; n< pSet->GetVisibleIndexCount(); n++)
					{
						DWORD dwGridIndex = pVIArray[n];
						//计算可视格子  in  选择锥
						D3DXVECTOR3 pos[4];
						D3DXVECTOR3 vCenter;
						pMap->GetGridPosition(dwGridIndex,pos,&vCenter);
						/*float x = (float)(dwGridIndex % dwMapWidth);
						float z = (float)(dwGridIndex / dwMapWidth);*/
						

						/*pos[0] = D3DXVECTOR3(x,pGrid->,z);
						pos[1] = D3DXVECTOR3(x+1,0,z);
						pos[2] = D3DXVECTOR3(x,0,z+1);
						pos[3] = D3DXVECTOR3(x+1,0,z+1);

						D3DXVECTOR3 vCenter = (pos[0] + pos[1] + pos[2] + pos[3]) / 4.0f;*/

						if (cSelRay.IntersectTri(&pos[0],NULL,NULL) || cSelRay.IntersectTri(&pos[1],NULL,NULL))
						{
							//check grid mtl 
							//equal old?
							Grid *pGrid = pMap->GetGrid(dwGridIndex);
							if (pGrid->GetDoorIndex() != iDoorIndex)
							{
								//这个格子被选中
								CmdMapSetDoor *pCmd = new CmdMapSetDoor;
								pCmd->SetDoorIndex(dwGridIndex,iDoorIndex);
								CmdMgr::GetInstance()->Do(pCmd);
							}
							break;
						}
					}
				}
			}
		}
		break;
	}
}
void ToolDoorEdit::Render()
{
	ui::Manager::_tagINPUTINFO *pInputInfo = ui::Manager::GetInstance()->GetInputInfoBuffer();
	render::Interface *pInterface = render::Interface::GetInstance();
	render::Interface::Layer2D   *pLayer2D   = pInterface->GetLayer2D();
	render::Interface::Layer3D   *pLayer3D   = pInterface->GetLayer3D();

	EditWnd *pEditWnd = CommUI::GetInstance()->GetEditWnd();

	WndDoorEdit *pWndDoorEdit= pEditWnd->GetWndDoorEdit();
	DWORD dwSelIndex = pWndDoorEdit->GetCurDoorIndex();

	switch(m_eEditStatus)
	{
	case DEST_SELECT:
		if (m_bDrag)
		{
			//画拖动线
			pLayer2D->DrawWireQuadRect(m_ptDragStart.x,m_ptDragStart.y,pInputInfo->ptMouse.x,pInputInfo->ptMouse.y,0xffffffff);
		}
		DrawSelected();
		break;
	case DEST_PALACE:
		{
			D3DXMATRIX matTRS;

			//显示被点亮的格子
			static GameSelectRay cSelRay;
			RECT rc;
			pEditWnd->GetWndRect(&rc);
			cSelRay.CalculateRay(&rc,pEditWnd->GetCamera(),pInputInfo->ptMouse);
			//显示格子
			
			EditMap *pMap = CommDB::GetInstance()->GetMap();
			if (pMap)
			{
				VisibleSet *pSet = pMap->GetVisibleSet();
				DWORD *pVIArray = pSet->GetVisibleIndexArray();
				TerrainMesh *pMesh = CommDB::GetInstance()->GetTerrainMesh();
				TerrainMesh::_tagGrid *pGrid(NULL);
				TerrainMesh::_tagVertex *pVertexArray = pMesh->GetVertexList();
				D3DXVECTOR3 pos[4];
				D3DXVECTOR3 vCenter;

				for(DWORD n = 0; n< pSet->GetVisibleIndexCount(); n++)
				{
					DWORD dwGridIndex = pVIArray[n];

					pGrid = pMesh->GetGrid(dwGridIndex);

					for(DWORD v= 0; v < 4; v++)
					{
						pos[v] = pVertexArray[pGrid->dwVertexIndex[v]].vPosition;
					}

					vCenter = (pos[0] + pos[1] + pos[2] + pos[3]) / 4.0f;
					if (cSelRay.IntersectTri(&pos[0],&pos[1],&pos[2],NULL,NULL) || cSelRay.IntersectTri(&pos[0],&pos[2],&pos[3],NULL,NULL))
					{
						//这个格子被选中
						pLayer3D->_DEBUG_DrawWireQuad(&pos[0],&pos[1],&pos[2],&pos[3],0xffff0000);

						D3DXMatrixTranslation(&matTRS,vCenter.x,vCenter.y,vCenter.z);
						
						RnderDoorModel(&matTRS);	

						break;
					}
				}
			}
		}
		break;
	}

	_ShowStatus();
}