void ToolGridVisibleEdit::DrawBrush(void)
{
	if (m_dwCurSelGrid == 0xFFFFFFFF) 
		return;

	render::Interface *pInterface = render::Interface::GetInstance();
	render::Interface::Layer3D *pLayer3D = render::Interface::GetInstance()->GetLayer3D();

	EditMap *pMap = CommDB::GetInstance()->GetMap();
	DWORD dwMapWidth = pMap->GetWidth();
	DWORD dwMapDepth = pMap->GetDepth();
	int iCenterX = m_dwCurSelGrid % dwMapWidth;
	int iCenterZ = m_dwCurSelGrid / dwMapWidth;

	TerrainMesh *pMesh = CommDB::GetInstance()->GetTerrainMesh();
	TerrainMesh::_tagGrid *pGrid(NULL);
	TerrainMesh::_tagVertex *pVertexArray = pMesh->GetVertexList();
	D3DXVECTOR3 pos[4];


	for(int j = - m_iBrushSize; j < m_iBrushSize + 1; j++)
	{
		for(int i = - m_iBrushSize; i < m_iBrushSize + 1;i++)
		{
			int iX = iCenterX + i;
			int iZ = iCenterZ + j;

			if (!(iX < 0 || iX >= (int)dwMapWidth || iZ < 0 || iZ >= (int)dwMapDepth))
			{
				//在地图内
				//显示格子
				DWORD dwGridIndex = iX + iZ * dwMapWidth;
				pGrid = pMesh->GetGrid(dwGridIndex);

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

				//pLayer3D->_DEBUG_DrawTriangle(&pos[0],&pos[1],&pos[2],dwMtlColor[iMtlSelIndex]);
				//pLayer3D->_DEBUG_DrawTriangle(&pos[0],&pos[2],&pos[3],dwMtlColor[iMtlSelIndex]);
				pLayer3D->_DEBUG_DrawTriangle(&pos[0],&pos[1],&pos[2],0x8080ff80);
				pLayer3D->_DEBUG_DrawTriangle(&pos[0],&pos[2],&pos[3],0x8080ff80);
				pLayer3D->_DEBUG_DrawWireQuad(&pos[0],&pos[1],&pos[2],&pos[3],0xff00ff00);
			}
		}
	}
}
void CmdMapPaintColor::Execute(void)
{
	Cmd::Execute();
	//保存颜色

	EditMap *pMap = CommDB::GetInstance()->GetMap();
	DWORD dwMapWidth = pMap->GetWidth();
	DWORD dwMapDepth = pMap->GetDepth();
	
	TerrainMesh *pMesh = CommDB::GetInstance()->GetTerrainMesh();
	TerrainMesh::_tagVertex *pTempVertex,*pVertexList = pMesh->GetVertexList();

	D3DXVECTOR3 *pCenterPosition = &pVertexList[m_dwVertexIndex].vPosition;//中心点坐标


    //求出要运算的顶点列表
	int iCenterRow = (int)(m_dwVertexIndex % (dwMapWidth + 1));
	int iCenterCol = (int)(m_dwVertexIndex / (dwMapWidth + 1));
	int iRadius = (int)m_fBrushRadius;
	for(int i = - iRadius; i <= iRadius; i++)
	{
		int iR = iCenterRow + i;
		if (iR < 0 || iR >= (int)(dwMapWidth +1)) 
		{
			continue;//越界
		}
		for(int j = -iRadius; j <=  iRadius; j++)
		{
			int iC =  iCenterCol + j;
			if (iC < 0 || iC >= (int)(dwMapDepth +1)) 
			{
				continue;//越界
			}

			DWORD dwVertexIndex = iR + iC * (int)(dwMapWidth + 1);

			pTempVertex = &pVertexList[dwVertexIndex];
			//计算顶点的距离是否在半径中
			float dx = pTempVertex->vPosition.x - pCenterPosition->x;
			float dz = pTempVertex->vPosition.z - pCenterPosition->z;

			float fDis = sqrtf(dx * dx + dz * dz);

			if (fDis <= m_fBrushRadius)
			{
				//保存要修改的定点索引表
				m_vVertexIndexArray.push_back(dwVertexIndex);
				m_vVertexColorArray.push_back(pTempVertex->cAddColor);
			}
		 }
	}

	std::vector<DWORD>::iterator it;
	//画刷类型
	if (m_dwBrushType == 0)
	{
		//铅笔。。。直接画
		for(it = m_vVertexIndexArray.begin(); it != m_vVertexIndexArray.end(); it++)
		{
			DWORD dwVertexIndex = *it;
			pTempVertex = &pVertexList[dwVertexIndex];
			DWORD &dwDestColor = pTempVertex->cAddColor;
			//混合运算
			DWORD sr = (m_dwColor & 0x00FF0000) >> 16;
			DWORD sg = (m_dwColor & 0x0000FF00) >> 8;
			DWORD sb = (m_dwColor & 0x000000FF);
			DWORD dr = (dwDestColor & 0x00FF0000) >> 16;
			DWORD dg = (dwDestColor & 0x0000FF00) >> 8;
			DWORD db = (dwDestColor & 0x000000FF);

			DWORD a = (DWORD)(m_fColorChroma * 255.0f);

			DWORD r = ((sr * a) + dr * (255 - a)) / 255;
			DWORD g = ((sg * a) + dg * (255 - a)) / 255;
			DWORD b = ((sb * a) + db * (255 - a)) / 255;

			dwDestColor = 0xFF000000 | r << 16 | g <<8 | b;
		}
	}
	else
	{
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 ToolGridVisibleEdit::PaintBrush(BOOL  bVisible)
{
	if (m_dwCurSelGrid == 0xFFFFFFFF)
		return;       
	
	EditMap *pMap = CommDB::GetInstance()->GetMap();
	DWORD dwMapWidth = pMap->GetWidth();
	DWORD dwMapDepth = pMap->GetDepth();
	int iCenterX = m_dwCurSelGrid % dwMapWidth;
	int iCenterZ = m_dwCurSelGrid / dwMapWidth;

	TerrainMesh *pMesh = CommDB::GetInstance()->GetTerrainMesh();
	TerrainMesh::_tagGrid *pGrid(NULL);
	TerrainMesh::_tagVertex *pVertexArray = pMesh->GetVertexList();
	D3DXVECTOR3 pos[4];

	BOOL bAllLike = TRUE;

	for(int j = - m_iBrushSize; j < m_iBrushSize + 1; j++)
	{
		for(int i = - m_iBrushSize; i < m_iBrushSize + 1;i++)
		{
			int iX = iCenterX + i;
			int iZ = iCenterZ + j;

			if (!(iX < 0 || iX >= (int)dwMapWidth || iZ < 0 || iZ >= (int)dwMapDepth))
			{
				//在地图内
				//显示格子
				DWORD dwGridIndex = iX + iZ * dwMapWidth;
				//检查是否全部相同
				if (pMap->GetGrid(dwGridIndex)->GetVisible() != bVisible)
				{
					bAllLike = FALSE;
					break;
				}
			}
		}
	}
	if (!bAllLike)//有不同,则涂
	{
		CmdMapFillGridVisible *pCmd = new CmdMapFillGridVisible;
		pCmd->SetVisible(bVisible);
		for(int j = - m_iBrushSize; j < m_iBrushSize + 1; j++)
		{
			for(int i = - m_iBrushSize; i < m_iBrushSize + 1;i++)
			{
				int iX = iCenterX + i;
				int iZ = iCenterZ + j;

				if (!(iX < 0 || iX >= (int)dwMapWidth || iZ < 0 || iZ >= (int)dwMapDepth))
				{
					//在地图内
					//显示格子
					DWORD dwGridIndex = iX + iZ * dwMapWidth;
					pCmd->AddGridIndex(dwGridIndex);					
				}
			}
		}
		CmdMgr::GetInstance()->Do(pCmd);
	}
}