#include "CMapEditorDraw.h"
#include "resource.h"
#include "CMapEditor.h"
#include "CMainEditor.h"
#include "CSetSnapshot.h"

IMPLEMENT_DYNAMIC(CMapEditorDraw, CDialog)

BEGIN_MESSAGE_MAP(CMapEditorDraw, CDialog)
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
	ON_WM_MOUSEWHEEL()
	ON_WM_MOUSEMOVE()
	ON_WM_VSCROLL()
	ON_WM_HSCROLL()
END_MESSAGE_MAP()

CMapEditorDraw::CMapEditorDraw(CWnd* pParent)
	: CDialog(IDD_CMAPEDITOR_DRAW, pParent)
{
}

CMapEditorDraw::~CMapEditorDraw()
{
	if (pFont_sourcesun20)
		TTF_CloseFont(pFont_sourcesun20);
	for (auto& p : pFontMap28)
	{
		if (p.second.pTexture)
			p.second.pTexture->Release();
		if (p.second.pSRV)
			p.second.pSRV->Release();
	}
	for (auto& p : loadedTexture)
	{
		if (p.second.pSRV &&
			p.second.pSRV != tas_nullpxa.pSRV)
		{
			p.second.pSRV->Release();
			p.second.pSRV = nullptr;
		}
		if (p.second.pTexture &&
			p.second.pTexture != tas_nullpxa.pTexture)
		{
			p.second.pTexture->Release();
			p.second.pTexture = nullptr;
		}
	}
	loadedTexture.clear();
	if (tas_pxatype.pSRV)
		tas_pxatype.pSRV->Release();
	if (tas_pxatype.pTexture)
		tas_pxatype.pTexture->Release();
	if (tas_nullpxa.pSRV)
		tas_nullpxa.pSRV->Release();
	if (tas_nullpxa.pTexture)
		tas_nullpxa.pTexture->Release();
	pRenderer.ClearBuffer(&stageBuffer);
	pRenderer.ClearBuffer(&entityLight);
}

MyView* CMapEditorDraw::GetMyView()
{
	return &view;
}

extern bool g_DisableSound;
void CMapEditorDraw::Draw(bool timeCounterDraw)
{
	//如果正在播放，只接收来自timeCounterDraw的绘制
	if (*pPlaying && !timeCounterDraw)
		return;
	auto parentEditor = (CMapEditor*)pParentEditor;
	pRenderer.RenderClear();
	if (parentEditor->IsLoaded())
	{
		auto selectLayer = parentEditor->GetSelectLayer();
		view.count_w = *pMapW;
		view.count_max = *pMapH * view.count_w;
		//绘制主层透明背景
		if (parentEditor->Bk())
			view.DrawTransparentBk();
		//绘制每一个图层的内容
		pMtx->lock();
		//运行跳帧实体
		for (auto& layer : *pDrawLayer)
		{
			if (!layer->visible)
				continue;
			entityRes.SetCollisionEntityLayer(layer, GetPxa(layer->pxaName.c_str()));
			while (layer->jumpFrame_counter > 0)
			{
				g_DisableSound = true;
				entityRes.StepEntityLayerPe(layer);
				g_DisableSound = false;
				layer->jumpFrame_counter--;
			}
		}
		//绘制舞台
		DrawStage(timeCounterDraw, selectLayer);
		for (auto& layer : *pDrawLayer)
		{
			if (!layer->visible)
				continue;
			if (parentEditor->EntityShow())
				entityRes.DrawEntityLayer(layer, *pPlaying, true);
		}
		pMtx->unlock();
		//绘制地形类型
		if (selectLayer &&
			selectLayer->visible && 
			selectLayer->layerType != 2 &&
			parentEditor->Pxa())
			DrawPxa(selectLayer);
		//绘制主层网格
		if (parentEditor->Grid())
			view.DrawGrid();
		//绘制实体网格
		if (selectLayer &&
			parentEditor->Entity())
		{
			if (*pPlaying)
				entityRes.DrawEntityCbRectLayer(selectLayer);
			else
				DrawEntityGrid(selectLayer);
			DrawRecordGoal();
		}
		//绘制实体标签
		if (selectLayer && 
			parentEditor ->EntityLabel())
			DrawEntityLabel(selectLayer);
		//绘制选中层的网格以及其他
		if (selectLayer)
		{
			MyView srcView = view;
			view.offX += selectLayer->offx;
			view.offY += selectLayer->offy;
			view.DrawBorderLine();
			if (selectLayer->enableClipZone)
				DrawClipRect(selectLayer);
			view.DrawCursor();
			view = srcView;
		}
	}
	pRenderer.RenderPresent();
}

void CMapEditorDraw::ResetContext()
{
	RefreshImage();
	view.Reset();
	ResetEntityPlay();
}

void CMapEditorDraw::RefreshImage()
{
	for (auto& p : loadedTexture)
	{
		if (p.second.pSRV &&
			p.second.pSRV != tas_nullpxa.pSRV)
		{
			p.second.pSRV->Release();
			p.second.pSRV = nullptr;
		}
		if (p.second.pTexture &&
			p.second.pTexture != tas_nullpxa.pTexture)
		{
			p.second.pTexture->Release();
			p.second.pTexture = nullptr;
		}
	}
	loadedTexture.clear();
	loadedPxa.clear();
	entityRes.RefreshImage();
}

void CMapEditorDraw::ResetEntityPlay()
{
	entityRes.Reset();
	try
	{
		pMtx->lock();
		//绘制舞台
		for (auto& layer : *pDrawLayer)
			layer->jumpFrame_counter = layer->jumpFrame;
		pMtx->unlock();
	}
	catch(std::exception)
	{

	}
}

void CMapEditorDraw::SetCursorToCopyBuffSize()
{
	view.cursorW = copyBuff_w;
	view.cursorH = copyBuff_h;
}

EntityRes* CMapEditorDraw::GetEntityRes()
{
	return &entityRes;
}

void CMapEditorDraw::GetCopyBoard(CMAPEDITOR_DATATYPE_PXM** outCopyBuff, int** outCopyBuff_w, int** outCopyBuff_h)
{
	*outCopyBuff = &copyBuff;
	*outCopyBuff_w = &copyBuff_w;
	*outCopyBuff_h = &copyBuff_h;
}

void CMapEditorDraw::UpdateMapCache()
{
	if (*pPlaying)
	{
		entityRes.UpdatePathFinder();
		auto parentEditor = (CMapEditor*)pParentEditor;
		parentEditor->UpdateMapCache();
	}
}

PxaData* CMapEditorDraw::GetPxa(CString pxaName)
{
	if (loadedPxa.find(pxaName.GetString()) == loadedPxa.end())
	{
		PxaData pxaData;
		CPxaEditor::StaticLoad(pxaName, &pxaData.pxa, &pxaData.tileW, &pxaData.tileH);
		pxaData.UpdatePxaArr();
		loadedPxa.insert(std::make_pair(pxaName.GetString(), pxaData));
	}
	return &loadedPxa.at(pxaName.GetString());
}



void CMapEditorDraw::SetScreenShot()
{
	auto parentEditor = (CMapEditor*)pParentEditor;
	auto mainEditor = (CMainEditor*)parentEditor->pMainEditor;
	std::vector<char> buff;
	std::vector<wchar_t> wbuff;
	//生成一个输出文件名
	UTF16ToUTF8(parentEditor->GetMapFileName(), &buff);
	char filenameBuff[MAX_PATH];
	//取当前时间
	time_t now;
	tm local;
	// 获取当前时间
	time(&now);
	// 转换为本地时间
	localtime_s(&local, &now);
	sprintf_s(filenameBuff, "%s-%d%02d%02d-%02d%02d%02d-%d.aseprite",
		buff.data(),
		local.tm_year + 1900,
		local.tm_mon + 1,
		local.tm_mday,
		local.tm_hour,
		local.tm_min,
		local.tm_sec, clock());
	UTF8ToUTF16(filenameBuff, &wbuff);

	CSetSnapshot dlg;
	dlg.inFilename = wbuff.data();
	if (dlg.DoModal() != IDOK)
		return;
	snapshot_aseApp = dlg.outAseApp;
	snapshot_saveAs = dlg.outSaveAs;
	snapshot_path = dlg.outPath;
	UTF16ToUTF8(dlg.outFilename.GetString(), &buff);
	std::string mapfilename = buff.data();

	//准备一个和地图尺寸一样大的大贴图
	ExTextureBuffer buffer, stageBuffer, entityLight;
	if (*pMapW <= 0 || *pMapH <= 0)
		return;
	int renderSize_w = *pMapW * 16;
	int renderSize_h = *pMapH * 16;
	pRenderer.SetBufferSize(renderSize_w, renderSize_h, &buffer);
	pRenderer.SetBufferSize(renderSize_w, renderSize_h, &stageBuffer);
	pRenderer.SetBufferSize(renderSize_w, renderSize_h, &entityLight);
	std::vector<std::string> pngFiles;//记录每个文件名
	//保存最后一次用的RTV
	auto lastRTV = pRenderer.GetPenRTV();
	MyView lastView = view;
	view.offX = 0;
	view.offY = 0;
	view.scale = 1;
	view.window_w = (float)renderSize_w;
	view.window_h = (float)renderSize_h;
	MyRect rc;
	rc.Set1(0, 0, (float)renderSize_w, (float)renderSize_h);
	std::vector<char> layerNameBuff;
	//把完整的地图渲染到一个texture上
	if (parentEditor->Light())
	{
		//标记所有的灯光实体
		entityRes.ClearLightList();
		for (auto& pLayer : *pDrawLayer)
		{
			auto& layer = *pLayer;
			if (!layer.visible)
				continue;
			entityRes.MarkLightEntity(&layer);
		}
		for (auto& pLayer : *pDrawLayer)
		{
			auto& layer = *pLayer;
			UTF16ToUTF8(layer.tempLayerName.c_str(), &layerNameBuff);
			std::string fbkName = GenerateFileName(layerNameBuff.data(), false, false);
			std::string bbkName = GenerateFileName(layerNameBuff.data(), false, true);
			std::string entityName = GenerateFileName(layerNameBuff.data(), true, false);
			if (!layer.visible)
				continue;
			switch (layer.layerType)
			{
			case 0://主层
			case 1://背景层
				{
					//绘制bbk
					pRenderer.SetPenRTV(stageBuffer.pRTV);
					pRenderer.SetClipVpMVP((float)renderSize_w, (float)renderSize_h);
					pRenderer.RenderClearRTV(buffer.pRTV);
					pRenderer.RenderClearRTV(stageBuffer.pRTV);
					pRenderer.RenderClearRTV(entityLight.pRTV);
					if (parentEditor->BGround())
						DrawLayerBack(&layer);
					pRenderer.GameSetDayLight(layer.dayLight);//设置天光
					pRenderer.GameSetHSV(layer.tinyHSV[0], layer.tinyHSV[1], layer.tinyHSV[2]);
					pRenderer.SetPenRTV(entityLight.pRTV);//绘制发光实体
					pRenderer.RenderClearRTV(entityLight.pRTV, 1.0f, 1.0f, 1.0f, 0.0f);
					entityRes.DrawLightEntity(&layer, *pPlaying);
					pRenderer.SetPenRTV(buffer.pRTV);//最终输出
					auto vs = entityRes.GetVertexShader(L"sys_finalLight");
					auto ps = entityRes.GetPixelShader(L"sys_finalLight");
					pRenderer.SetTPenVertexShader(vs);
					pRenderer.SetTPenPixelShader(ps);
					pRenderer.SetTPenDraw(entityLight.pSRV, stageBuffer.pSRV);
					pRenderer.AddRenderCopy(&rc, &rc);
					pRenderer.DrawRenderCopy();
					pRenderer.SetTPenVertexShader();//复原着色器
					pRenderer.SetTPenPixelShader();
					pRenderer.MySaveTexture(buffer.pBuffer, bbkName.c_str());
					pngFiles.push_back(bbkName);
				}
				{
					//绘制实体
					pRenderer.SetPenRTV(stageBuffer.pRTV);
					pRenderer.SetClipVpMVP((float)renderSize_w, (float)renderSize_h);
					pRenderer.RenderClearRTV(buffer.pRTV);
					pRenderer.RenderClearRTV(stageBuffer.pRTV);
					pRenderer.RenderClearRTV(entityLight.pRTV);
					if (parentEditor->EntityShow())
						entityRes.DrawEntityLayer(&layer, *pPlaying);
					pRenderer.GameSetDayLight(layer.dayLight);//设置天光
					pRenderer.GameSetHSV(layer.tinyHSV[0], layer.tinyHSV[1], layer.tinyHSV[2]);
					pRenderer.SetPenRTV(entityLight.pRTV);//绘制发光实体
					pRenderer.RenderClearRTV(entityLight.pRTV, 1.0f, 1.0f, 1.0f, 0.0f);
					entityRes.DrawLightEntity(&layer, *pPlaying);
					pRenderer.SetPenRTV(buffer.pRTV);//最终输出
					auto vs = entityRes.GetVertexShader(L"sys_finalLight");
					auto ps = entityRes.GetPixelShader(L"sys_finalLight");
					pRenderer.SetTPenVertexShader(vs);
					pRenderer.SetTPenPixelShader(ps);
					pRenderer.SetTPenDraw(entityLight.pSRV, stageBuffer.pSRV);
					pRenderer.AddRenderCopy(&rc, &rc);
					pRenderer.DrawRenderCopy();
					pRenderer.SetTPenVertexShader();//复原着色器
					pRenderer.SetTPenPixelShader();
					pRenderer.MySaveTexture(buffer.pBuffer, entityName.c_str());
					pngFiles.push_back(entityName);
				}
				{
					//绘制fbk
					pRenderer.SetPenRTV(stageBuffer.pRTV);
					pRenderer.SetClipVpMVP((float)renderSize_w, (float)renderSize_h);
					pRenderer.RenderClearRTV(buffer.pRTV);
					pRenderer.RenderClearRTV(stageBuffer.pRTV);
					pRenderer.RenderClearRTV(entityLight.pRTV);
					if (parentEditor->FGround())
						DrawLayerFront(&layer);
					pRenderer.GameSetDayLight(layer.dayLight);//设置天光
					pRenderer.GameSetHSV(layer.tinyHSV[0], layer.tinyHSV[1], layer.tinyHSV[2]);
					pRenderer.SetPenRTV(entityLight.pRTV);//绘制发光实体
					pRenderer.RenderClearRTV(entityLight.pRTV, 1.0f, 1.0f, 1.0f, 0.0f);
					entityRes.DrawLightEntity(&layer, *pPlaying);
					pRenderer.SetPenRTV(buffer.pRTV);//最终输出
					auto vs = entityRes.GetVertexShader(L"sys_finalLight");
					auto ps = entityRes.GetPixelShader(L"sys_finalLight");
					pRenderer.SetTPenVertexShader(vs);
					pRenderer.SetTPenPixelShader(ps);
					pRenderer.SetTPenDraw(entityLight.pSRV, stageBuffer.pSRV);
					pRenderer.AddRenderCopy(&rc, &rc);
					pRenderer.DrawRenderCopy();
					pRenderer.SetTPenVertexShader();//复原着色器
					pRenderer.SetTPenPixelShader();
					pRenderer.MySaveTexture(buffer.pBuffer, fbkName.c_str());
					pngFiles.push_back(fbkName);
				}
				break;
			case 2://循环背景层
				{
					//绘制bbk
					pRenderer.SetPenRTV(stageBuffer.pRTV);
					pRenderer.SetClipVpMVP((float)renderSize_w, (float)renderSize_h);
					pRenderer.RenderClearRTV(buffer.pRTV);
					pRenderer.RenderClearRTV(stageBuffer.pRTV);
					pRenderer.RenderClearRTV(entityLight.pRTV);
					DrawLoopLayer(&layer, nullptr);
					pRenderer.SetClipVpMVP((float)renderSize_w, (float)renderSize_h);
					pRenderer.GameSetDayLight(layer.dayLight);//设置天光
					pRenderer.GameSetHSV(layer.tinyHSV[0], layer.tinyHSV[1], layer.tinyHSV[2]);
					pRenderer.SetPenRTV(entityLight.pRTV);//绘制发光实体
					pRenderer.RenderClearRTV(entityLight.pRTV, 1.0f, 1.0f, 1.0f, 0.0f);
					entityRes.DrawLightEntity(&layer, *pPlaying);
					pRenderer.SetPenRTV(buffer.pRTV);//最终输出
					auto vs = entityRes.GetVertexShader(L"sys_finalLight");
					auto ps = entityRes.GetPixelShader(L"sys_finalLight");
					pRenderer.SetTPenVertexShader(vs);
					pRenderer.SetTPenPixelShader(ps);
					pRenderer.SetTPenDraw(entityLight.pSRV, stageBuffer.pSRV);
					pRenderer.AddRenderCopy(&rc, &rc);
					pRenderer.DrawRenderCopy();
					pRenderer.SetTPenVertexShader();//复原着色器
					pRenderer.SetTPenPixelShader();
					pRenderer.MySaveTexture(buffer.pBuffer, bbkName.c_str());
					pngFiles.push_back(bbkName);
				}
				{
					//绘制实体
					pRenderer.SetPenRTV(stageBuffer.pRTV);
					pRenderer.SetClipVpMVP((float)renderSize_w, (float)renderSize_h);
					pRenderer.RenderClearRTV(buffer.pRTV);
					pRenderer.RenderClearRTV(stageBuffer.pRTV);
					pRenderer.RenderClearRTV(entityLight.pRTV);
					if (parentEditor->EntityShow())
						entityRes.DrawEntityLayer(&layer, *pPlaying);
					pRenderer.GameSetDayLight(layer.dayLight);//设置天光
					pRenderer.GameSetHSV(layer.tinyHSV[0], layer.tinyHSV[1], layer.tinyHSV[2]);
					pRenderer.SetPenRTV(entityLight.pRTV);//绘制发光实体
					pRenderer.RenderClearRTV(entityLight.pRTV, 1.0f, 1.0f, 1.0f, 0.0f);
					entityRes.DrawLightEntity(&layer, *pPlaying);
					pRenderer.SetPenRTV(buffer.pRTV);//最终输出
					auto vs = entityRes.GetVertexShader(L"sys_finalLight");
					auto ps = entityRes.GetPixelShader(L"sys_finalLight");
					pRenderer.SetTPenVertexShader(vs);
					pRenderer.SetTPenPixelShader(ps);
					pRenderer.SetTPenDraw(entityLight.pSRV, stageBuffer.pSRV);
					pRenderer.AddRenderCopy(&rc, &rc);
					pRenderer.DrawRenderCopy();
					pRenderer.SetTPenVertexShader();//复原着色器
					pRenderer.SetTPenPixelShader();
					pRenderer.MySaveTexture(buffer.pBuffer, entityName.c_str());
					pngFiles.push_back(entityName);
				}
				break;
			}
		}
	}
	else
	{
		pRenderer.SetPenRTV(buffer.pRTV);
		for (auto& pLayer : *pDrawLayer)
		{
			auto& layer = *pLayer;
			UTF16ToUTF8(layer.tempLayerName.c_str(), &layerNameBuff);
			std::string fbkName = GenerateFileName(layerNameBuff.data(), false, false);
			std::string bbkName = GenerateFileName(layerNameBuff.data(), false, true);
			std::string entityName = GenerateFileName(layerNameBuff.data(), true, false);
			if (!layer.visible)
				continue;
			switch (layer.layerType)
			{
			case 0://主层
			case 1://背景层
				//绘制bbk
				pRenderer.SetClipVpMVP((float)renderSize_w, (float)renderSize_h);
				pRenderer.RenderClearRTV(buffer.pRTV);
				if (parentEditor->BGround())
					DrawLayerBack(&layer);
				pRenderer.MySaveTexture(buffer.pBuffer, bbkName.c_str());
				pngFiles.push_back(bbkName);
				//绘制实体
				pRenderer.SetClipVpMVP((float)renderSize_w, (float)renderSize_h);
				pRenderer.RenderClearRTV(buffer.pRTV);
				if (parentEditor->EntityShow())
					entityRes.DrawEntityLayer(&layer, *pPlaying);
				pRenderer.MySaveTexture(buffer.pBuffer, entityName.c_str());
				pngFiles.push_back(entityName);
				//绘制fbk
				pRenderer.SetClipVpMVP((float)renderSize_w, (float)renderSize_h);
				pRenderer.RenderClearRTV(buffer.pRTV);
				if (parentEditor->FGround())
					DrawLayerFront(&layer);
				pRenderer.MySaveTexture(buffer.pBuffer, fbkName.c_str());
				pngFiles.push_back(fbkName);
				break;
			case 2://循环背景层
				pRenderer.SetClipVpMVP((float)renderSize_w, (float)renderSize_h);
				pRenderer.RenderClearRTV(buffer.pRTV);
				DrawLoopLayer(&layer, nullptr);
				pRenderer.MySaveTexture(buffer.pBuffer, bbkName.c_str());
				pngFiles.push_back(bbkName);
				//绘制实体
				pRenderer.SetClipVpMVP((float)renderSize_w, (float)renderSize_h);
				pRenderer.RenderClearRTV(buffer.pRTV);
				if (parentEditor->EntityShow())
					entityRes.DrawEntityLayer(&layer, *pPlaying);
				pRenderer.MySaveTexture(buffer.pBuffer, entityName.c_str());
				pngFiles.push_back(entityName);
				break;
			}
		}
	}
	pRenderer.ResetClipVpMVP();
	//恢复RTV
	pRenderer.SetPenRTV(lastRTV);
	view = lastView;
	//清理贴图
	pRenderer.ClearBuffer(&buffer);
	pRenderer.ClearBuffer(&stageBuffer);
	pRenderer.ClearBuffer(&entityLight);
	//用aseprite合并
	std::string asepritePath;
	UTF16ToUTF8(snapshot_aseApp.GetString(), &buff);
	asepritePath = buff.data();
	UTF16ToUTF8(mainEditor->GetBasePath().GetString(), &buff);
	std::string luaScriptPath = buff.data();
	luaScriptPath += "data\\tool\\merge_png.lua";
	char outputFile[MAX_PATH];
	UTF16ToUTF8(snapshot_saveAs.GetString(), &buff);
	sprintf_s(outputFile, "%s\\%s", buff.data(), mapfilename.c_str());
	MergePNGsWithAseprite(asepritePath, luaScriptPath, outputFile, pngFiles);
	//删除这些临时文件
	for (auto& p : pngFiles)
		remove(p.c_str());
}

std::string CMapEditorDraw::GenerateFileName(const char* layerName, bool bStageOrEntity, bool fBkOrbBk) const
{
	const char* propName = "";
	if (bStageOrEntity)
		propName = "e";
	else
		propName = fBkOrbBk ? "b" : "f";
	std::vector<char> buff;
	UTF16ToUTF8(snapshot_path.GetString(), &buff);
	char filename[MAX_PATH];
	sprintf_s(filename, "%s\\%s-%s.png",
		buff.data(),
		layerName,
		propName
		);
	return std::string(filename);
}

void CMapEditorDraw::MergePNGsWithAseprite(const std::string& asepritePath, const std::string& luaScriptPath, const std::string& outputAsepriteFile, const std::vector<std::string>& pngFiles)
{
	std::vector<wchar_t> wbuff;
	static char cmd[5000];
	ZeroMemory(cmd, sizeof(cmd));
	sprintf_s(cmd, "\"%s\" -b -script-param argNum=%d ", asepritePath.c_str(), (int)pngFiles.size());
	char buff2[MAX_PATH];
	for (int i = 0; i < pngFiles.size(); i++)
	{
		sprintf_s(buff2, "-script-param png%d=\"%s\" ", i, pngFiles[i].c_str());
		strcat_s(cmd, buff2);
	}
	sprintf_s(buff2, "-script-param output=\"%s\" ", outputAsepriteFile.c_str());
	strcat_s(cmd, buff2);

	sprintf_s(buff2, "-script \"%s\"", luaScriptPath.c_str());
	strcat_s(cmd, buff2);

	char bat_filename[MAX_PATH];
	ZeroMemory(bat_filename, sizeof(bat_filename));
	std::vector<char> buff;
	UTF16ToUTF8(snapshot_path.GetString(), &buff);
	sprintf_s(bat_filename, "%s\\OcStoreTemp.bat", buff.data());
	FILE* fp = nullptr;
	fopen_s(&fp, bat_filename, "w+");
	if (fp)
	{
		fwrite(cmd, strlen(cmd), 1, fp);
		fclose(fp);
		UTF8ToUTF16(bat_filename, &wbuff);
		_wsystem(wbuff.data());
	}
	remove(bat_filename);
}

void CMapEditorDraw::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BOOL CMapEditorDraw::OnInitDialog()
{
	CDialog::OnInitDialog();
	//初始化渲染器
	pRenderer.Init(m_hWnd);
	//初始化字体
	pFont_sourcesun20 = pRenderer.MyLoadFont("SourceHanSansSC-Normal", 20);
	view.Init(this, &pRenderer);
	view.count_w = 40;
	view.count_max = 23 * view.count_w;
	view.drawSelectedCursor = false;
	//实体播放
    entityRes.Init(&pRenderer, &view, pMapW, pMapH);
	//载入资源
	LoadResourceTexture(&pRenderer, IDB_PXATYPE, &tas_pxatype.pTexture, &tas_pxatype.pSRV);
	LoadResourceTexture(&pRenderer, IDB_NULLPXA, &tas_nullpxa.pTexture, &tas_nullpxa.pSRV);
	return 0;
}

void CMapEditorDraw::OnSize(UINT nType, int cx, int cy)
{
	CDialog::OnSize(nType, cx, cy);
	if (!view.pRenderer)
		return;
	view.window_w = (float)cx;
	view.window_h = (float)cy;
	pRenderer.OnSize(cx, cy, nType == SIZE_MINIMIZED);
	pRenderer.SetBufferSize(cx, cy, &stageBuffer, "stageBuffer");
	pRenderer.SetBufferSize(cx, cy, &entityLight, "entityLight");
}

void CMapEditorDraw::OnPaint()
{
	CPaintDC dc(this); // device context for painting

	// 不为绘图消息调用 CDialog::OnPaint()
	Draw();
}

void CMapEditorDraw::OnLButtonDown(UINT nFlags, CPoint point)
{
	//编辑地图
	auto parentEditor = (CMapEditor*)pParentEditor;
	bool bUpdateShadowTable = false;
	if (parentEditor->IsEditMapMode())
	{
		MyMapLayer* layer = parentEditor->GetSelectLayer();
		if (layer && layer->visible)
		{
			MyView& brushView = *pBrushView;
			auto& step = *layer->GetPxmStep();
			switch (parentEditor->GetPaintMode())
			{
			case 5://粘贴
				if (copyBuff.empty())
					break;
				[[fallthrough]];
			case 0://涂画
				painting = true;
				paintStartCursorPosX = view.cursorX;
				paintStartCursorPosY = view.cursorY;
				layer->undoRedo.BeginStep(CMAPEDITOR_DATATYPE_PXM_STR);
				DrawTile();
				break;
			case 1://替换
				{
					layer->undoRedo.BeginStep(CMAPEDITOR_DATATYPE_PXM_STR);
					LONG srcPxa = step[MAKELONG(view.cursorX, view.cursorY)];
					for (auto& p : step)
						if (p.second == srcPxa)
							p.second = MAKELONG(brushView.cursorX_selected, brushView.cursorY_selected);
					layer->undoRedo.EndStep();
					bUpdateShadowTable = true;
				}
				break;
			case 2://填充
				{
					layer->undoRedo.BeginStep(CMAPEDITOR_DATATYPE_PXM_STR);
					int count_h = view.count_max % view.count_w ? view.count_max / view.count_w + 1 : view.count_max / view.count_w;
					LONG srcPxa = step[MAKELONG(view.cursorX, view.cursorY)];
					std::list<int> sp;
					sp.push_back(MAKELONG(view.cursorX, view.cursorY));
					int dx = brushView.cursorW_selected - view.cursorX % brushView.cursorW_selected;
					int dy = brushView.cursorH_selected - view.cursorY % brushView.cursorH_selected;
					while (!sp.empty())
					{
						int x = LOWORD(sp.back());
						int y = HIWORD(sp.back());
						sp.pop_back();
						LONG dstPxa = MAKELONG(brushView.cursorX_selected + (x + dx) % brushView.cursorW_selected,
							brushView.cursorY_selected + (y + dy) % brushView.cursorH_selected);
						step[MAKELONG(x, y)] = dstPxa;
						//检查四周否要填充
						if (y - 1 >= 0 && step[MAKELONG(x, y - 1)] == srcPxa && srcPxa != dstPxa)//上方
							sp.push_back(MAKELONG(x, y - 1));
						if (y + 1 < count_h && step[MAKELONG(x, y + 1)] == srcPxa && srcPxa != dstPxa)//下方
							sp.push_back(MAKELONG(x, y + 1));
						if (x - 1 >= 0 && step[MAKELONG(x - 1, y)] == srcPxa && srcPxa != dstPxa)//左方
							sp.push_back(MAKELONG(x - 1, y));
						if (x + 1 < view.count_w && step[MAKELONG(x + 1, y)] == srcPxa && srcPxa != dstPxa)//右方
							sp.push_back(MAKELONG(x + 1, y));
					}
					layer->undoRedo.EndStep();
					bUpdateShadowTable = true;
				}
				break;
			case 3://矩形
			case 4://复制
				view.drawSelectedCursor = true;
				view.allowSelectOut = true;
				view.BeginSelect(point);
				break;
			}
		}
	}
	//编辑实体
	else if (parentEditor->IsEditEntityMode())
	{
		OnMouseMove(nFlags, point);
		MyMapLayer* layer = parentEditor->GetSelectLayer();
		if (layer)
		{
			auto& step_pxe = *layer->GetPxeStep();
			std::list<int> l;
			for (auto& el : step_pxe)
				if (view.cursorX == el.second.x && view.cursorY == el.second.y)
					l.push_back(el.first);
			if (l.empty())
				parentEditor->SelectOutEntity();
			else
			{
				if (l.size() == 1)//如果同坐标内只有一个实体
				{
					layer->selected_entity = l.front();
					parentEditor->SelectToEntity();
					*pDragEntity = true;
				}
				else if (layer->selected_entity >= 0)
				{
					parentEditor->SelectToEntity();
					*pDragEntity = true;
				}
				else if (layer->selected_entity < 0)//如果有多个实体
				{
					CString str;
					CMenu m;
					CPoint pt = point;
					m.CreatePopupMenu();
					for (auto& id : l)
					{
						auto iter = step_pxe.find(id);
						if (iter == step_pxe.end())
							continue;
						auto& el = iter->second;
						int entityID = iter->first;
						str.Format(L"实体 %d - %s", entityID, el.name.c_str());
						m.AppendMenu(MF_ENABLED, 2000 + entityID, str);
					}
					ClientToScreen(&pt);
					m.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
						pt.x, pt.y, parentEditor);
				}
			}
		}
	}
	//更新影表
	if (bUpdateShadowTable)
		UpdateMapCache();
	Draw();
	parentEditor->UpdateWindowTitle();
	parentEditor->UpdateMenuState();
	CDialog::OnLButtonDown(nFlags, point);
}

void CMapEditorDraw::OnLButtonUp(UINT nFlags, CPoint point)
{
	auto parentEditor = (CMapEditor*)pParentEditor;
	bool bUpdateShadowTable = false;
	bool bDraw = false;
	if (parentEditor->IsEditMapMode())
	{
		MyMapLayer* layer = parentEditor->GetSelectLayer();
		if (layer && layer->visible)
		{
			MyView& brushView = *pBrushView;
			auto& step = *layer->GetPxmStep();
			switch (parentEditor->GetPaintMode())
			{
			case 5://粘贴
				if (copyBuff.empty())
					break;
			case 0://涂画
				painting = false;
				layer->undoRedo.EndStep();
				bUpdateShadowTable = true;
				break;
			case 3://矩形
				view.drawSelectedCursor = false;
				view.allowSelectOut = false;
				view.EndSelect();
				layer->undoRedo.BeginStep(CMAPEDITOR_DATATYPE_PXM_STR);
				for (int y = 0; y < view.cursorH_selected; y++)
				{
					for (int x = 0; x < view.cursorW_selected; x++)
					{
						if (brushView.cursorW_selected == 0 || brushView.cursorH_selected == 0)
						{
							step.erase(MAKELONG(view.cursorX_selected + x, view.cursorY_selected + y));
						}
						else
						{
							//其他层
							int pxa = MAKELONG(brushView.cursorX_selected + x % brushView.cursorW_selected,
								(brushView.cursorY_selected + y % brushView.cursorH_selected));
							//不使用0号地形
							if (pxa || layer->layerType == 2)
								step[MAKELONG(view.cursorX_selected + x, view.cursorY_selected + y)] = pxa;
							else
								step.erase(MAKELONG(view.cursorX_selected + x, view.cursorY_selected + y));
						}
					}
				}
				layer->undoRedo.EndStep();
				bUpdateShadowTable = true;
				bDraw = true;
				break;
			case 4://复制
				view.drawSelectedCursor = false;
				view.allowSelectOut = false;
				view.EndSelect();
				copyBuff.clear();
				if (layer->layerType == 2)
					layer->undoRedo.BeginStep(CMAPEDITOR_DATATYPE_PXM_STR);
				for (int y = 0; y < view.cursorH_selected; y++)
				{
					for (int x = 0; x < view.cursorW_selected; x++)
					{
						if (layer->layerType == 2)//如果是循环背景层
						{
							//复制是擦除功能
							step.erase(MAKELONG(view.cursorX_selected + x, view.cursorY_selected + y));
						}
						else
						{
							LONG pxa = step[MAKELONG(view.cursorX_selected + x, view.cursorY_selected + y)];
							if (layer->layerType == 2)//如果是循环背景层
							{
								//复制0号地形
								copyBuff[MAKELONG(x, y)] = pxa;
							}
							else
							{
								//不复制0号地形
								if (pxa)
									copyBuff[MAKELONG(x, y)] = pxa;
							}
						}
					}	
				}	
				if (layer->layerType == 2)
				{
					layer->undoRedo.EndStep();
					bUpdateShadowTable = true;
					bDraw = true;
				}
				copyBuff_w = view.cursorW_selected;
				copyBuff_h = view.cursorH_selected;
				break;
			}
		}
	}
	//编辑实体
	if (*pDragEntity)
	{
		*pDragEntity = false;
		MyMapLayer* layer = parentEditor->GetSelectLayer();
		if (layer)
			layer->undoRedo.EndStep();
	}
	//更新影表
	if (bUpdateShadowTable)
		 UpdateMapCache();
	if (bDraw)
	{
		Draw();
		parentEditor->UpdateWindowTitle();
		parentEditor->UpdateMenuState();
	}
	CDialog::OnLButtonUp(nFlags, point);
}

void CMapEditorDraw::OnRButtonDown(UINT nFlags, CPoint point)
{
	auto parentEditor = (CMapEditor*)pParentEditor;
	auto layer = parentEditor->GetSelectLayer();
	if (layer)
	{
		view.offX += layer->offx;
		view.offY += layer->offy;
		view.WindowBind_OnRButtonDown(point);
		view.offX -= layer->offx;
		view.offY -= layer->offy;
	}
	else
	{
		view.WindowBind_OnRButtonDown(point);
	}
	//实体编辑
	rclick_last_pt = point;//保存右键点击坐标
	CDialog::OnRButtonDown(nFlags, point);
}


void CMapEditorDraw::OnRButtonUp(UINT nFlags, CPoint point)
{
	auto parentEditor = (CMapEditor*)pParentEditor;
	auto layer = parentEditor->GetSelectLayer();
	if (layer)
	{
		view.offX += layer->offx;
		view.offY += layer->offy;
		view.WindowBind_OnRButtonUp();
		view.offX -= layer->offx;
		view.offY -= layer->offy;
	}
	else
	{
		view.WindowBind_OnRButtonUp();
	}
	//实体编辑
	if (layer &&
		parentEditor->IsEditEntityMode() &&
		(abs(point.x - rclick_last_pt.x) <= 5 && abs(point.y - rclick_last_pt.y) <= 5))
	{
		// 加载弹出菜单
		*pDragEntity = false;
		if (layer->selected_entity < 0)
		{
			OnLButtonDown(nFlags, point);
			CMenu m, * pm;
			CPoint pt = point;
			m.LoadMenu(IDR_CMAPEDITOR_ENTITYEDIT_RMENU_NOSEL);
			pm = m.GetSubMenu(0);
			ClientToScreen(&pt);
			pm->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
				pt.x, pt.y, this);
		}
		else
		{
			//点击位置得和选择实体一致
			auto& step_pxe = *layer->GetPxeStep();
			auto& el = step_pxe.at(layer->selected_entity);
			if (el.x == view.cursorX && el.y == view.cursorY)
			{
				CMenu m, * pm;
				CPoint pt = point;
				m.LoadMenu(IDR_CMAPEDITOR_ENTITYEDIT_RMENU);
				pm = m.GetSubMenu(0);

				ClientToScreen(&pt);
				pm->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
					pt.x, pt.y, this);
			}
			else
			{
				OnLButtonDown(nFlags, point);
				*pDragEntity = false;
			}
		}
	}
	CDialog::OnRButtonUp(nFlags, point);
}

BOOL CMapEditorDraw::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
	auto parentEditor = (CMapEditor*)pParentEditor;
	auto layer = parentEditor->GetSelectLayer();
	if (layer)
	{
		view.offX += layer->offx;
		view.offY += layer->offy;
		bool bDraw = view.WindowBind_OnMouseWheel(zDelta, pt);
		view.offX -= layer->offx;
		view.offY -= layer->offy;
		if (bDraw)
		{
			Draw();
		}
	}
	else
	{
		if (view.WindowBind_OnMouseWheel(zDelta, pt))
		{
			Draw();
		}
	}
	return CDialog::OnMouseWheel(nFlags, zDelta, pt);
}

void CMapEditorDraw::OnMouseMove(UINT nFlags, CPoint point)
{
	auto parentEditor = (CMapEditor*)pParentEditor;
	auto layer = parentEditor->GetSelectLayer();
	MyView& brushView = *pBrushView;
	bool bDraw = false;
	if (layer)
	{
		//转换光标大小
		if (parentEditor->IsEditMapMode() && layer->visible)
		{
			switch (parentEditor->GetPaintMode())
			{
			case 5://粘贴
				if (view.cursorW != copyBuff_w || 
					view.cursorH != copyBuff_h)
				{
					view.cursorW = copyBuff_w;
					view.cursorH = copyBuff_h;
					bDraw = true;
				}
				break;
			case 0://涂画
				if (view.cursorW != brushView.cursorW_selected ||
					view.cursorH != brushView.cursorH_selected)
				{
					view.cursorW = brushView.cursorW_selected;
					view.cursorH = brushView.cursorH_selected;
					bDraw = true;
				}
				break;
			}
		}
		else if (parentEditor->IsEditEntityMode())
		{
			if (view.cursorW != 1 || 
				view.cursorH != 1)
			{
				view.cursorW = view.cursorH = 1;
				bDraw = true;
			}
		}
		//编辑地图
		switch (parentEditor->GetPaintMode())
		{
		case 5://粘贴
			if (copyBuff.empty())
				break;
		case 0://涂画
			if (painting)
			{
				DrawTile();
				bDraw = true;
			}	
			break;
		case 3://矩形
		case 4://复制
			view.Select(point);
			break;
		}
		view.offX += layer->offx;
		view.offY += layer->offy;
		bDraw |= view.WindowBind_OnMouseMove(point);
		view.offX -= layer->offx;
		view.offY -= layer->offy;
		//编辑实体
		if (*pDragEntity && layer->selected_entity >= 0)
		{
			auto& step_pxe = *layer->GetPxeStep();
			auto& el = step_pxe.at(layer->selected_entity);
			if (el.x != view.cursorX || el.y != view.cursorY)
			{
				layer->undoRedo.BeginStep(CMAPEDITOR_DATATYPE_PXE_STR);
				el.x = view.cursorX;
				el.y = view.cursorY;
				ResetEntityPlay();
				bDraw = true;
			}
		}
	}
	else
	{
		if (view.WindowBind_OnMouseMove(point))
		{
			bDraw = true;
		}
	}
	if (bDraw)
	{
		parentEditor->UpdateWindowTitle();
		Draw();
	}
	CDialog::OnMouseMove(nFlags, point);
}

void CMapEditorDraw::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	auto parentEditor = (CMapEditor*)pParentEditor;
	auto layer = parentEditor->GetSelectLayer();
	if (layer)
	{
		view.offX += layer->offx;
		view.offY += layer->offy;
		bool bDraw = view.WindowBind_OnVScroll(nSBCode, nPos);
		view.offX -= layer->offx;
		view.offY -= layer->offy;
		if (bDraw)
			Draw();
	}
	else
	{
		if (view.WindowBind_OnVScroll(nSBCode, nPos))
			Draw();
	}
	CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}

void CMapEditorDraw::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	auto parentEditor = (CMapEditor*)pParentEditor;
	auto layer = parentEditor->GetSelectLayer();
	if (layer)
	{
		view.offX += layer->offx;
		view.offY += layer->offy;
		bool bDraw = view.WindowBind_OnHScroll(nSBCode, nPos);
		view.offX -= layer->offx;
		view.offY -= layer->offy;
		if (bDraw)
			Draw();
	}
	else
	{
		if (view.WindowBind_OnHScroll(nSBCode, nPos))
			Draw();
	}
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

ID3D11ShaderResourceView* CMapEditorDraw::GetTexture(CString pngName)
{
	if (loadedTexture.find(pngName.GetString()) == loadedTexture.end())
	{
		//单文件名
		std::vector<char> utf8;
		CString wstr;
		wstr.Format(L"./data/pxa/%s.png", pngName.GetString());
		UTF16ToUTF8(wstr.GetString(), &utf8);

		//确认文件是否存在
		FILE* fp;
		fopen_s(&fp, utf8.data(), "rb");
		if (fp)
		{
			fclose(fp);
			MyTextureAndSRV tas;
			pRenderer.MyLoadTexture(&tas.pTexture, utf8.data());
			pRenderer.MyCreateShaderResourceView(&tas.pSRV, tas.pTexture);
			loadedTexture.insert(std::make_pair(pngName.GetString(), tas));
			return tas.pSRV;
		}
		else
		{
			//使用pxaNull
			loadedTexture.insert(std::make_pair(pngName.GetString(), tas_nullpxa));
			return tas_nullpxa.pSRV;
		}
	}
	return loadedTexture.at(pngName.GetString()).pSRV;
}

void CMapEditorDraw::DrawTile()
{
	auto parentEditor = (CMapEditor*)pParentEditor;
	MyView& brushView = *pBrushView;
	MyMapLayer* layer = parentEditor->GetSelectLayer();
	if (layer == nullptr)
		return;
	auto& step = *layer->GetPxmStep();
	view.cursorX = (view.cursorX - paintStartCursorPosX % view.cursorW) / view.cursorW * view.cursorW + paintStartCursorPosX % view.cursorW;
	view.cursorY = (view.cursorY - paintStartCursorPosY % view.cursorH) / view.cursorH * view.cursorH + paintStartCursorPosY % view.cursorH;
	switch (parentEditor->GetPaintMode())
	{
	case 0://涂画
		for (int y = 0; y < brushView.cursorH_selected; y++)
		{
			for (int x = 0; x < brushView.cursorW_selected; x++)
			{
				LONG pxa = MAKELONG(brushView.cursorX_selected + x, brushView.cursorY_selected + y);
				if (layer->layerType == 2)//如果是循环背景层
				{
					//使用0号地形
					step[MAKELONG(view.cursorX + x, view.cursorY + y)] = pxa;
				}
				else
				{
					//不使用0号地形
					if (pxa)
						step[MAKELONG(view.cursorX + x, view.cursorY + y)] = pxa;
					else
						step.erase(MAKELONG(view.cursorX + x, view.cursorY + y));
				}
			}
		}
		break;
	case 5://粘贴
		for (int y = 0; y < copyBuff_h; y++)
		{
			for (int x = 0; x < copyBuff_w; x++)
			{
				LONG pos = MAKELONG(x, y);
				auto iter = copyBuff.find(pos);
				if (iter == copyBuff.end())
					continue;
				LONG pxa = iter->second;
				if (layer->layerType == 2)//如果是循环背景层
				{
					//使用0号地形
					step[MAKELONG(view.cursorX + x, view.cursorY + y)] = pxa;
				}
				else
				{
					//不使用0号地形
					if (pxa)
						step[MAKELONG(view.cursorX + x, view.cursorY + y)] = pxa;
					else
						step.erase(MAKELONG(view.cursorX + x, view.cursorY + y));
				}
			}
		}
		break;
	}
	
}

void CMapEditorDraw::DrawLayerBack(MyMapLayer* pLayer)
{
	//绘制上限
	const int limit = 40000;
	int drawCount = 0;
	MyRect rcScreen;
	rcScreen.Set1(0, 0, view.window_w, view.window_h);
	MyView srcView = view;
	if (pLayer->layerType == 1)
	{
		view.offX += pLayer->offx;
		view.offY += pLayer->offy;
		if (*pPlaying)
		{
			float farValue = MyFar(pLayer->myFar);//计算随距离增加，眼睛中物体移动距离
			view.offX -= (1 - farValue) * view.offX;
			view.offY -= (1 - farValue) * view.offY;
		}
	}
	//设置绘制剪切区域
	if (pLayer->enableClipZone && *pPlaying)
	{
		MyRect clip;
		MyRect& clipRc = pLayer->clipZone;
		clip.left = (clipRc.left) * view.scale - floorf(view.offX * view.scale);
		clip.right = (clipRc.right) * view.scale - floorf(view.offY * view.scale);
		clip.top = (clipRc.top) * view.scale - floorf(view.offY * view.scale);
		clip.bottom = (clipRc.bottom) * view.scale - floorf(view.offY * view.scale);
		pRenderer.SetScissorRect(&clip);
	}
	pRenderer.SetTPenDraw(GetTexture(pLayer->pxaName.c_str()));
	auto& step = *pLayer->GetPxmStep();
	auto& pxaData = *GetPxa(pLayer->pxaName.c_str());
	for (auto& t : step)
	{
		//跳过0号地形
		if (t.second == 0)
			continue;
		auto iter = pxaData.pxa.find(t.second);
		auto pxaID = 0;
		if (iter != pxaData.pxa.end())
			pxaID = iter->second;
		//如果不是背景地形则跳过
		if (!IsBackPxa(pxaID))
			continue;
		float srcX = (float)LOWORD(t.second);
		float srcY = (float)HIWORD(t.second);
		float dstX = (float)LOWORD(t.first);
		float dstY = (float)HIWORD(t.first);
		MyRect src, dst;
		if (*pPlaying && IsWindPxa(pxaID))
		{
			//绘制风场地形
			switch (pxaID)
			{
			case 0x60://左
			case 0x70:
				//绘制一般地形
				src.Set1((srcX + *pWindAniIndex) * pxaData.tileW,
					(srcY + 1) * pxaData.tileH,
					(float)pxaData.tileW,
					(float)pxaData.tileH);
				break;
			case 0x61://左上
			case 0x71:
				src.Set1((srcX + *pWindAniIndex2) * pxaData.tileW,
					(srcY + 1 + *pWindAniIndex2) * pxaData.tileH,
					(float)pxaData.tileW,
					(float)pxaData.tileH);
				break;
			case 0x62://上
			case 0x72:
				src.Set1(srcX * pxaData.tileW,
					(srcY + 1 + *pWindAniIndex) * pxaData.tileH,
					(float)pxaData.tileW,
					(float)pxaData.tileH);
				break;
			case 0x63://右上
			case 0x73:
				src.Set1((srcX + 1 - *pWindAniIndex2) * pxaData.tileW,
					(srcY + 1 + *pWindAniIndex2) * pxaData.tileH,
					(float)pxaData.tileW,
					(float)pxaData.tileH);
				break;
			case 0x64://右
			case 0x74:
				src.Set1((srcX + 1 - *pWindAniIndex) * pxaData.tileW,
					(srcY + 1) * pxaData.tileH,
					(float)pxaData.tileW,
					(float)pxaData.tileH);
				break;
			case 0x65://左下
			case 0x75:
				src.Set1((srcX + *pWindAniIndex2) * pxaData.tileW,
					(srcY + 2 - *pWindAniIndex2) * pxaData.tileH,
					(float)pxaData.tileW,
					(float)pxaData.tileH);
				break;
			case 0x66://下
			case 0x76:
				src.Set1(srcX * pxaData.tileW,
					(srcY + 2 - *pWindAniIndex) * pxaData.tileH,
					(float)pxaData.tileW,
					(float)pxaData.tileH);
				break;
			case 0x67://右下
			case 0x77:
				src.Set1((srcX + 1 - *pWindAniIndex2) * pxaData.tileW,
					(srcY + 2 - *pWindAniIndex2) * pxaData.tileH,
					(float)pxaData.tileW,
					(float)pxaData.tileH);
				break;
			}
		}
		else
		{
			//绘制一般地形
			src.Set1(srcX * pxaData.tileW,
				srcY * pxaData.tileH,
				(float)pxaData.tileW,
				(float)pxaData.tileH);
		}
		dst.Set1(((dstX * view.grid_w) * view.scale - floorf(view.offX * view.scale)),
			((dstY * view.grid_h) * view.scale - floorf(view.offY * view.scale)),
			view.grid_w * view.scale,
			view.grid_h * view.scale);
		//测试绘制区域是否在屏幕内
		if (!rcScreen.IntersectRect(&dst))
			continue;
		pRenderer.AddRenderCopy(&src, &dst, pLayer->color);
		drawCount++;
		if (drawCount >= limit)
			break;
	}
	pRenderer.DrawRenderCopy();
	if (pLayer->layerType == 1)
		view = srcView;
	if (pLayer->enableClipZone && *pPlaying)
		pRenderer.SetScissorRect();
}

void CMapEditorDraw::DrawLayerFront(MyMapLayer* pLayer)
{
	//绘制上限
	const int limit = 40000;
	int drawCount = 0;
	MyRect rcScreen;
	rcScreen.Set1(0, 0, view.window_w, view.window_h);
	MyView srcView = view;
	if (pLayer->layerType == 1)
	{
		view.offX += pLayer->offx;
		view.offY += pLayer->offy;
		if (*pPlaying)
		{
			float farValue = MyFar(pLayer->myFar);//计算随距离增加，眼睛中物体移动距离
			view.offX -= (1 - farValue) * view.offX;
			view.offY -= (1 - farValue) * view.offY;
		}
	}
	//设置绘制剪切区域
	if (pLayer->enableClipZone && *pPlaying)
	{
		MyRect clip;
		MyRect& clipRc = pLayer->clipZone;
		clip.left = (clipRc.left) * view.scale - floorf(view.offX * view.scale);
		clip.right = (clipRc.right) * view.scale - floorf(view.offX * view.scale);
		clip.top = (clipRc.top) * view.scale - floorf(view.offY * view.scale);
		clip.bottom = (clipRc.bottom) * view.scale - floorf(view.offY * view.scale);
		pRenderer.SetScissorRect(&clip);
	}
	pRenderer.SetTPenDraw(GetTexture(pLayer->pxaName.c_str()));
	auto& step = *pLayer->GetPxmStep();
	auto& pxaData = *GetPxa(pLayer->pxaName.c_str());
	for (auto& t : step)
	{
		//跳过0号地形
		if (t.second == 0)
			continue;
		auto iter = pxaData.pxa.find(t.second);
		auto pxaID = 0;
		if (iter != pxaData.pxa.end())
			pxaID = iter->second;
		//如果是背景地形则跳过
		if (IsBackPxa(pxaID))
			continue;
		float srcX = (float)LOWORD(t.second);
		float srcY = (float)HIWORD(t.second);
		float dstX = (float)LOWORD(t.first);
		float dstY = (float)HIWORD(t.first);
		MyRect src, dst;
		src.Set1(srcX * pxaData.tileW,
			srcY * pxaData.tileH,
			(float)pxaData.tileW,
			(float)pxaData.tileH);
		dst.Set1(((dstX * view.grid_w) * view.scale - floorf(view.offX * view.scale)),
			((dstY * view.grid_h) * view.scale - floorf(view.offY * view.scale)),
			view.grid_w * view.scale,
			view.grid_h * view.scale);
		//测试绘制区域是否在屏幕内
		if (!rcScreen.IntersectRect(&dst))
			continue;
		pRenderer.AddRenderCopy(&src, &dst, pLayer->color);
		drawCount++;
		if (drawCount >= limit)
			break;
	}
	pRenderer.DrawRenderCopy();
	if (pLayer->layerType == 1)
		view = srcView;
	if (pLayer->enableClipZone && *pPlaying)
		pRenderer.SetScissorRect();
}

void CMapEditorDraw::DrawLoopLayer(MyMapLayer* pLayer, MyMapLayer* selectLayer)
{
	auto& step = *pLayer->GetPxmStep();
	auto& pxaData = *GetPxa(pLayer->pxaName.c_str());
	//设置绘制剪切区域
	MyRect clip;
	if (pLayer->enableClipZone && *pPlaying)
	{
		MyView srcView = view;
		view.offX += pLayer->offx;
		view.offY += pLayer->offy;
		float farValue = MyFar(pLayer->myFar);//计算随距离增加，眼睛中物体移动距离
		view.offX -= (1 - farValue) * view.offX;
		view.offY -= (1 - farValue) * view.offY;
		MyRect& clipRc = pLayer->clipZone;
		clip.left = (clipRc.left) * view.scale - floorf(view.offX * view.scale);
		clip.right = (clipRc.right) * view.scale - floorf(view.offX * view.scale);
		clip.top = (clipRc.top) * view.scale - floorf(view.offY * view.scale);
		clip.bottom = (clipRc.bottom) * view.scale - floorf(view.offY * view.scale);
		view = srcView;
	}
	else
	{
		clip.Set1(-view.offX * view.scale,
			-view.offY * view.scale,
			view.grid_w * view.count_w * view.scale + 1,
			view.grid_h * (view.count_max / view.count_w) * view.scale + 1);
	}
	//设置剪切矩形
	if (*pPlaying || pLayer != selectLayer)
	{
		//循环层
		int minXIndex = INT_MAX;
		int minYIndex = INT_MAX;
		int maxXIndex = INT_MIN;
		int maxYIndex = INT_MIN;
		//取瓦片范围
		for (auto& t : step)
		{
			int srcX = (int)LOWORD(t.first);
			int srcY = (int)HIWORD(t.first);
			if (srcX < minXIndex)
				minXIndex = srcX;
			if (srcY < minYIndex)
				minYIndex = srcY;
			if (srcX > maxXIndex)
				maxXIndex = srcX;
			if (srcY > maxYIndex)
				maxYIndex = srcY;
		}
		if (minXIndex == INT_MAX ||
			minYIndex == INT_MAX ||
			maxXIndex == INT_MIN ||
			maxYIndex == INT_MIN)
			return;//不绘制这一层
		//正在播放的
		pRenderer.SetScissorRect(&clip);
		if (pLayer->loopH && pLayer->loopV)
		{
			//绘制循环瓦片
			int tw = maxXIndex + 1 - minXIndex;
			int th = maxYIndex + 1 - minYIndex;
			float bk_w = tw * view.grid_w;
			float bk_h = th * view.grid_h;
			float farValue = MyFar(pLayer->myFar);//计算随距离增加，眼睛中物体移动距离
			MyView srcView = view;
			float srcLoopOffx = pLayer->speedH * (float)(*pTimeCounter) * farValue;
			float srcLoopOffy = pLayer->speedV * (float)(*pTimeCounter) * farValue;
			view.offX -= srcLoopOffx - floorf(srcLoopOffx / bk_w) * bk_w;
			view.offY -= srcLoopOffy - floorf(srcLoopOffy / bk_h) * bk_h;
			if (*pPlaying)
			{
				//加上镜头远景移动
				view.offX -= (1 - farValue) * srcView.offX;
				view.offY -= (1 - farValue) * srcView.offY;
			}
			pRenderer.SetTPenDraw(GetTexture(pLayer->pxaName.c_str()));

			MyRect rc;
			MyViewDrawFor vFor;
			vFor.Init(&view);
			vFor.DrawForYBegin();
			vFor._y -= th;
			vFor.rc_draw_y -= vFor.rc_draw_h * th;
			for (; vFor.DrawForY(); vFor.DrawForYInc())
			{
				vFor.DrawForXBegin();
				vFor._x -= tw;
				vFor.rc_draw_x -= vFor.rc_draw_w * tw;
				for (; vFor.DrawForX(&rc); vFor.DrawForXInc())
				{
					int& x = vFor._x;
					int& y = vFor._y;
					if (y * view.count_w + x >= view.count_max)
						goto CMapEditorDraw_DrawLoopLayer_DrawHV_End;
					int dxSrc = (x - minXIndex) % tw;
					int dySrc = (y - minYIndex) % th;
					if (dxSrc < 0)
						dxSrc = tw - abs(dxSrc) % tw;
					if (dySrc < 0)
						dySrc = th - abs(dySrc) % th;
					int XIndexSrc = dxSrc + minXIndex;
					int YIndexSrc = dySrc + minYIndex;
					auto iter = step.find(MAKELONG(XIndexSrc, YIndexSrc));
					if (iter == step.end())
						continue;
					float srcX = (float)LOWORD(iter->second);
					float srcY = (float)HIWORD(iter->second);
					MyRect src;
					src.Set1(srcX * pxaData.tileW,
						srcY * pxaData.tileH,
						(float)pxaData.tileW,
						(float)pxaData.tileH);
					if (step.find(MAKELONG(x, y)) == step.end() && pLayer == selectLayer)
					{
						MyRGBA myRGBA;
						myRGBA.Set(pLayer->color);
						myRGBA.a = 0x80;
						DWORD c = myRGBA.Get();
						pRenderer.AddRenderCopy(&src, &rc, c);
					}
					else
						pRenderer.AddRenderCopy(&src, &rc, pLayer->color);
				}
			}
		CMapEditorDraw_DrawLoopLayer_DrawHV_End:
			pRenderer.DrawRenderCopy();
			view = srcView;
		}
		else if (pLayer->loopH)//只水平循环
		{
			//绘制循环瓦片
			int tw = maxXIndex + 1 - minXIndex;
			int th = maxYIndex + 1 - minYIndex;
			float bk_w = tw * view.grid_w;
			float bk_h = th * view.grid_h;
			float farValue = MyFar(pLayer->myFar);//计算随距离增加，眼睛中物体移动距离
			MyView srcView = view;
			float srcLoopOffx = pLayer->speedH * (float)(*pTimeCounter) * farValue;
			float srcLoopOffy = pLayer->speedV * (float)(*pTimeCounter) * farValue;
			view.offX -= srcLoopOffx - floorf(srcLoopOffx / bk_w) * bk_w;
			view.offY -= srcLoopOffy - floorf(srcLoopOffy / bk_h) * bk_h;
			if (*pPlaying)
			{
				//加上镜头远景移动
				view.offX -= (1 - farValue) * srcView.offX;
				view.offY -= (1 - farValue) * srcView.offY;
			}
			pRenderer.SetTPenDraw(GetTexture(pLayer->pxaName.c_str()));

			MyRect rc;
			MyViewDrawFor vFor;
			vFor.Init(&view);
			vFor.DrawForYBegin();
			vFor._y = minYIndex;
			vFor.rc_draw_y = (vFor._y * view.grid_h) * view.scale - floorf(view.offY * view.scale);
			for (; vFor._y <= maxYIndex; vFor.DrawForYInc())
			{
				vFor.DrawForXBegin();
				vFor._x -= tw;
				vFor.rc_draw_x -= vFor.rc_draw_w * tw;
				for (; vFor.DrawForX(&rc); vFor.DrawForXInc())
				{
					int& x = vFor._x;
					int& y = vFor._y;
					if (y * view.count_w + x >= view.count_max)
						goto CMapEditorDraw_DrawLoopLayer_DrawH_End;
					int dxSrc = (x - minXIndex) % tw;
					int dySrc = (y - minYIndex) % th;
					if (dxSrc < 0)
						dxSrc = tw - abs(dxSrc) % tw;
					if (dySrc < 0)
						dySrc = th - abs(dySrc) % th;
					int XIndexSrc = dxSrc + minXIndex;
					int YIndexSrc = dySrc + minYIndex;
					auto iter = step.find(MAKELONG(XIndexSrc, YIndexSrc));
					if (iter == step.end())
						continue;
					float srcX = (float)LOWORD(iter->second);
					float srcY = (float)HIWORD(iter->second);
					MyRect src;
					src.Set1(srcX * pxaData.tileW,
						srcY * pxaData.tileH,
						(float)pxaData.tileW,
						(float)pxaData.tileH);
					if (step.find(MAKELONG(x, y)) == step.end() && pLayer == selectLayer)
					{
						MyRGBA myRGBA;
						myRGBA.Set(pLayer->color);
						myRGBA.a = 0x80;
						DWORD c = myRGBA.Get();
						pRenderer.AddRenderCopy(&src, &rc, c);
					}
					else
						pRenderer.AddRenderCopy(&src, &rc, pLayer->color);
				}
			}
		CMapEditorDraw_DrawLoopLayer_DrawH_End:
			pRenderer.DrawRenderCopy();
			view = srcView;
		}
		else if (pLayer->loopV)//只垂直循环
		{
			//绘制循环瓦片
			int tw = maxXIndex + 1 - minXIndex;
			int th = maxYIndex + 1 - minYIndex;
			float bk_w = tw * view.grid_w;
			float bk_h = th * view.grid_h;
			float farValue = MyFar(pLayer->myFar);//计算随距离增加，眼睛中物体移动距离
			MyView srcView = view;
			float srcLoopOffx = pLayer->speedH * (float)(*pTimeCounter) * farValue;
			float srcLoopOffy = pLayer->speedV * (float)(*pTimeCounter) * farValue;
			view.offX -= srcLoopOffx - floorf(srcLoopOffx / bk_w) * bk_w;
			view.offY -= srcLoopOffy - floorf(srcLoopOffy / bk_h) * bk_h;
			if (*pPlaying)
			{
				//加上镜头远景移动
				view.offX -= (1 - farValue) * srcView.offX;
				view.offY -= (1 - farValue) * srcView.offY;
			}
			pRenderer.SetTPenDraw(GetTexture(pLayer->pxaName.c_str()));

			MyRect rc;
			MyViewDrawFor vFor;
			vFor.Init(&view);
			vFor.DrawForYBegin();
			vFor._y -= th;
			vFor.rc_draw_y -= vFor.rc_draw_h * th;
			for (; vFor.DrawForY(); vFor.DrawForYInc())
			{
				vFor.DrawForXBegin();
				vFor._x = minXIndex;
				vFor.rc_draw_x = (vFor._x * view.grid_w) * view.scale - floorf(view.offX * view.scale);
				for (; vFor._x <= maxXIndex; vFor.DrawForXInc())
				{
					rc.Set1(vFor.rc_draw_x, vFor.rc_draw_y, vFor.rc_draw_w, vFor.rc_draw_h);
					int& x = vFor._x;
					int& y = vFor._y;
					if (y * view.count_w + x >= view.count_max)
						goto CMapEditorDraw_DrawLoopLayer_DrawV_End;
					int dxSrc = (x - minXIndex) % tw;
					int dySrc = (y - minYIndex) % th;
					if (dxSrc < 0)
						dxSrc = tw - abs(dxSrc) % tw;
					if (dySrc < 0)
						dySrc = th - abs(dySrc) % th;
					int XIndexSrc = dxSrc + minXIndex;
					int YIndexSrc = dySrc + minYIndex;
					auto iter = step.find(MAKELONG(XIndexSrc, YIndexSrc));
					if (iter == step.end())
						continue;
					float srcX = (float)LOWORD(iter->second);
					float srcY = (float)HIWORD(iter->second);
					MyRect src;
					src.Set1(srcX * pxaData.tileW,
						srcY * pxaData.tileH,
						(float)pxaData.tileW,
						(float)pxaData.tileH);
					if (step.find(MAKELONG(x, y)) == step.end() && pLayer == selectLayer)
					{
						MyRGBA myRGBA;
						myRGBA.Set(pLayer->color);
						myRGBA.a = 0x80;
						DWORD c = myRGBA.Get();
						pRenderer.AddRenderCopy(&src, &rc, c);
					}
					else
						pRenderer.AddRenderCopy(&src, &rc, pLayer->color);
				}
			}
		CMapEditorDraw_DrawLoopLayer_DrawV_End:
			pRenderer.DrawRenderCopy();
			view = srcView;
		}
		else
		{
			//不循环
			float farValue = MyFar(pLayer->myFar);//计算随距离增加，眼睛中物体移动距离
			MyView srcView = view;
			if (*pPlaying)
			{
				//加上镜头远景移动
				view.offX -= (1 - farValue) * srcView.offX;
				view.offY -= (1 - farValue) * srcView.offY;
			}
			pRenderer.SetTPenDraw(GetTexture(pLayer->pxaName.c_str()));
			auto& step = *pLayer->GetPxmStep();
			auto& pxaData = *GetPxa(pLayer->pxaName.c_str());
			for (auto& t : step)
			{
				float srcX = (float)LOWORD(t.second);
				float srcY = (float)HIWORD(t.second);
				float dstX = (float)LOWORD(t.first);
				float dstY = (float)HIWORD(t.first);
				MyRect src, dst;
				src.Set1(srcX * pxaData.tileW,
					srcY * pxaData.tileH,
					(float)pxaData.tileW,
					(float)pxaData.tileH);
				dst.Set1(((dstX * view.grid_w) * view.scale - floorf(view.offX * view.scale)),
					((dstY * view.grid_h) * view.scale - floorf(view.offY * view.scale)),
					view.grid_w * view.scale,
					view.grid_h * view.scale);
				//测试绘制区域是否在屏幕内
				pRenderer.AddRenderCopy(&src, &dst, pLayer->color);
			}
			pRenderer.DrawRenderCopy();
			view = srcView;
		}
		pRenderer.SetScissorRect();
	}
	else
	{
		//正在编辑时的绘制
		pRenderer.SetTPenDraw(GetTexture(pLayer->pxaName.c_str()));
		auto& step = *pLayer->GetPxmStep();
		auto& pxaData = *GetPxa(pLayer->pxaName.c_str());
		for (auto& t : step)
		{
			float srcX = (float)LOWORD(t.second);
			float srcY = (float)HIWORD(t.second);
			float dstX = (float)LOWORD(t.first);
			float dstY = (float)HIWORD(t.first);
			MyRect src, dst;
			src.Set1(srcX * pxaData.tileW,
				srcY * pxaData.tileH,
				(float)pxaData.tileW,
				(float)pxaData.tileH);
			dst.Set1(((dstX * view.grid_w) * view.scale - floorf(view.offX * view.scale)),
				((dstY * view.grid_h) * view.scale - floorf(view.offY * view.scale)),
				view.grid_w * view.scale,
				view.grid_h * view.scale);
			//测试绘制区域是否在屏幕内
			pRenderer.AddRenderCopy(&src, &dst, pLayer->color);
		}
		pRenderer.DrawRenderCopy();
	}
}

void CMapEditorDraw::DrawPxa(MyMapLayer* pLayer)
{
	//绘制上限
	const int limit = 40000;
	int drawCount = 0;
	MyRect rcScreen;
	rcScreen.Set1(0, 0, view.window_w, view.window_h);
	pRenderer.SetTPenDraw(tas_pxatype.pSRV);
	MyView srcView = view;
	if (pLayer->layerType == 1)
	{
		view.offX += pLayer->offx;
		view.offY += pLayer->offy;
		if (*pPlaying)
		{
			float farValue = MyFar(pLayer->myFar);//计算随距离增加，眼睛中物体移动距离
			view.offX -= (1 - farValue) * view.offX;
			view.offY -= (1 - farValue) * view.offY;
		}
	}
	auto& step = *pLayer->GetPxmStep();
	auto& pxaData = *GetPxa(pLayer->pxaName.c_str());
	for (auto& t : step)
	{
		auto iter = pxaData.pxa.find(t.second);
		if (iter == pxaData.pxa.end())
			continue;
		auto pxaID = iter->second;
		float srcX = (float)(pxaID % 16);
		float srcY = (float)(pxaID / 16);
		float dstX = (float)LOWORD(t.first);
		float dstY = (float)HIWORD(t.first);
		MyRect src, dst;
		src.Set1(srcX * pxaData.tileW,
			srcY * pxaData.tileH,
			(float)pxaData.tileW,
			(float)pxaData.tileH);
		dst.Set1(((dstX * view.grid_w) * view.scale - floorf(view.offX * view.scale)),
			((dstY * view.grid_h) * view.scale - floorf(view.offY * view.scale)),
			view.grid_w * view.scale,
			view.grid_h * view.scale);
		//测试绘制区域是否在屏幕内
		if (!rcScreen.IntersectRect(&dst))
			continue;
		pRenderer.AddRenderCopy(&src, &dst, RGBA(0xff, 0xff, 0xff, 0x80));
		drawCount++;
		if (drawCount >= limit)
			break;
	}
	pRenderer.DrawRenderCopy();
	if (pLayer->layerType == 1)
		view = srcView;
}

bool CMapEditorDraw::IsBackPxa(int pxaID)
{
	return pxaID == 0 || pxaID == 0x40 || IsWindPxa(pxaID);
}

bool CMapEditorDraw::IsWindPxa(int pxaID)
{
	return (pxaID >= 0x60 && pxaID <= 0x67) || (pxaID >= 0x70 && pxaID <= 0x77);
}

void CMapEditorDraw::DrawEntityGrid(MyMapLayer* pLayer)
{
	auto& step_pxe = *pLayer->GetPxeStep();
	pRenderer.SetPenDraw();
	MyView srcView = view;
	if (pLayer->layerType == 1)
	{
		view.offX += pLayer->offx;
		view.offY += pLayer->offy;
		if (*pPlaying)
		{
			float farValue = MyFar(pLayer->myFar);//计算随距离增加，眼睛中物体移动距离
			view.offX -= (1 - farValue) * view.offX;
			view.offY -= (1 - farValue) * view.offY;
		}
	}
	MyRect rc;
	for (auto& p : step_pxe)
	{
		auto& el = p.second;
		rc.Set1((el.x * view.grid_w) * view.scale - floorf(view.offX * view.scale),
			(el.y * view.grid_h) * view.scale - floorf(view.offY * view.scale),
			view.grid_w * view.scale,
			view.grid_h * view.scale);
		if (p.first == pLayer->selected_entity)
			pRenderer.AddRect(&rc, RGBA(0xff, 0, 0xff, 0xff));//选中的实体用紫色
		else
			pRenderer.AddRect(&rc, RGBA(0, 0xff, 0, 0xff));//未选中用绿色
	}
	pRenderer.DrawLine();
	if (pLayer->layerType == 1)
		view = srcView;
}

void CMapEditorDraw::DrawEntityLabel(MyMapLayer* pLayer)
{
	auto& step_pxe = *pLayer->GetPxeStep();
	MyView srcView = view;
	if (pLayer->layerType == 1)
	{
		view.offX += pLayer->offx;
		view.offY += pLayer->offy;
		if (*pPlaying)
		{
			float farValue = MyFar(pLayer->myFar);//计算随距离增加，眼睛中物体移动距离
			view.offX -= (1 - farValue) * view.offX;
			view.offY -= (1 - farValue) * view.offY;
		}
	}
	float offx, nextX, nextY;
	MyRect rc;
	for (auto& p : step_pxe)
	{
		auto& el = p.second;
		nextX = (el.x * view.grid_w + 1) * view.scale - floorf(view.offX * view.scale);
		nextY = (el.y * view.grid_h) * view.scale - floorf(view.offY * view.scale);
		offx = nextX;
		pRenderer.DrawText_Unicode(&pFontMap28, pFont_sourcesun20, el.name.c_str(), offx, &nextX, &nextY, RGBA(0xff, 0xff, 0xff, 0xff), view.scale/4);
	}
	if (pLayer->layerType == 1)
		view = srcView;
}

void CMapEditorDraw::DrawClipRect(MyMapLayer* pLayer)
{
	MyRect& clipRc = pLayer->clipZone;
	MyView srcView = view;
	if (*pPlaying)
	{
		float farValue = MyFar(pLayer->myFar);//计算随距离增加，眼睛中物体移动距离
		view.offX -= (1 - farValue) * view.offX;
		view.offY -= (1 - farValue) * view.offY;
	}
	MyRect rc;
	rc.left = (clipRc.left) * view.scale - floorf(view.offX * view.scale);
	rc.right = (clipRc.right) * view.scale - floorf(view.offX * view.scale);
	rc.top = (clipRc.top) * view.scale - floorf(view.offY * view.scale);
	rc.bottom = (clipRc.bottom) * view.scale - floorf(view.offY * view.scale);
	pRenderer.SetPenDraw();
	pRenderer.AddRect(&rc, RGBA(0xff, 0, 0, 0xff));
	pRenderer.DrawLine();
	view = srcView;
}

void CMapEditorDraw::DrawStage(bool timeCounterDraw, MyMapLayer* selectLayer)
{
	pRenderer.GameSetScale(view.scale);
	pRenderer.GameSetOffset(view.offX, view.offY);
	auto parentEditor = (CMapEditor*)pParentEditor;
	if (parentEditor->Light())
	{
		//播放模式，绘制所有光影
		auto lastRTV = pRenderer.GetPenRTV();
		/*----------------------------标记所有的灯光实体--------------------------*/
		entityRes.ClearLightList();
		for (auto& pLayer : *pDrawLayer)
		{
			auto& layer = *pLayer;
			if (!layer.visible)
				continue;
			entityRes.MarkLightEntity(&layer);
		}
		/*----------------------------分层绘制--------------------------*/
		for (auto& pLayer : *pDrawLayer)
		{
			auto& layer = *pLayer;
			if (!layer.visible)
				continue;
			if (layer.dayLight == 1)//优化，天光为1时不需要渲染光影
			{
				if (timeCounterDraw)
				{
					entityRes.SetCollisionEntityLayer(&layer, GetPxa(layer.pxaName.c_str()));
					entityRes.StepEntityLayer(&layer);
					entityRes.DeltaTimeEntityLayer(&layer, 1);
				}
				switch (layer.layerType)
				{
				case 0://主层
				case 1://背景层
					if (parentEditor->BGround())
						DrawLayerBack(&layer);
					if (parentEditor->EntityShow())
						entityRes.DrawEntityLayer(&layer, *pPlaying);
					if (parentEditor->FGround())
						DrawLayerFront(&layer);
					break;
				case 2://循环背景层
					DrawLoopLayer(&layer, selectLayer);
					if (parentEditor->EntityShow())
						entityRes.DrawEntityLayer(&layer, *pPlaying);
					break;
				}
				continue;
			}
			/*-------------------------绘制舞台到stageBuffer--------------------------*/
			pRenderer.SetPenRTV(stageBuffer.pRTV);
			pRenderer.RenderClearRTV(stageBuffer.pRTV);
			if (timeCounterDraw)
			{
				entityRes.SetCollisionEntityLayer(&layer, GetPxa(layer.pxaName.c_str()));
				entityRes.StepEntityLayer(&layer);
				entityRes.DeltaTimeEntityLayer(&layer, 1);
			}
			switch (layer.layerType)
			{
			case 0://主层
			case 1://背景层
				if (parentEditor->BGround())
					DrawLayerBack(&layer);
				if (parentEditor->EntityShow())
					entityRes.DrawEntityLayer(&layer, *pPlaying);
				if (parentEditor->FGround())
					DrawLayerFront(&layer);
				break;
			case 2://循环背景层
				DrawLoopLayer(&layer, selectLayer);
				if (parentEditor->EntityShow())
					entityRes.DrawEntityLayer(&layer, *pPlaying);
				break;
			}
			/*-------------------------设置天光--------------------------*/
			pRenderer.GameSetDayLight(layer.dayLight);
			pRenderer.GameSetHSV(layer.tinyHSV[0], layer.tinyHSV[1], layer.tinyHSV[2]);
			/*-------------------------绘制发光实体--------------------------*/
			//绘制发光实体到entityLight
			//pRenderer.SetBlendMode_Add();
			pRenderer.SetPenRTV(entityLight.pRTV);
			pRenderer.RenderClearRTV(entityLight.pRTV, 1.0f, 1.0f, 1.0f, 0.0f);
			entityRes.DrawLightEntity(&layer, *pPlaying);
			//pRenderer.SetBlendMode_Default();
			/*-------------------------最终输出--------------------------*/
			//entityLight、stageBuffer
			pRenderer.SetPenRTV(lastRTV);
			auto vs = entityRes.GetVertexShader(L"sys_finalLight");
			auto ps = entityRes.GetPixelShader(L"sys_finalLight");
			pRenderer.SetTPenVertexShader(vs);
			pRenderer.SetTPenPixelShader(ps);
			pRenderer.SetTPenDraw(entityLight.pSRV, stageBuffer.pSRV);
			pRenderer.AddRenderCopy();
			pRenderer.DrawRenderCopy();
			//复原着色器
			pRenderer.SetTPenVertexShader();
			pRenderer.SetTPenPixelShader();
		}
	}
	else
	{
		//通常模式，不绘制光影
		for (auto& pLayer : *pDrawLayer)
		{
			auto& layer = *pLayer;
			if (!layer.visible)
				continue;
			if (timeCounterDraw)
			{
				entityRes.SetCollisionEntityLayer(&layer, GetPxa(layer.pxaName.c_str()));
				entityRes.StepEntityLayer(&layer);
				entityRes.DeltaTimeEntityLayer(&layer, 1);
			}
			switch (layer.layerType)
			{
			case 0://主层
			case 1://背景层
				if (parentEditor->BGround())
					DrawLayerBack(&layer);
				if (parentEditor->EntityShow())
					entityRes.DrawEntityLayer(&layer, *pPlaying);
				if (parentEditor->FGround())
					DrawLayerFront(&layer);
				break;
			case 2://循环背景层
				DrawLoopLayer(&layer, selectLayer);
				if (parentEditor->EntityShow())
					entityRes.DrawEntityLayer(&layer, *pPlaying);
				break;
			}
		}
	}
}

void CMapEditorDraw::DrawRecordGoal()
{
	auto parentEditor = (CMapEditor*)pParentEditor;

	DWORD c = RGBA(0xff, 0xff, 0, 0xff);
	pRenderer.SetPenDraw();
	float arrow_x = (*pRecordEndPos_x * view.grid_w) * view.scale - floorf(view.offX * view.scale);
	float arrow_y = (*pRecordEndPos_y * view.grid_h) * view.scale - floorf(view.offY * view.scale);

	MyDX11PenVertex v0, v1;
	v0.Set(arrow_x + (view.grid_w / 2 - 1.5f) * view.scale,
		arrow_y + 3 * view.scale,
		0, c);
	v1.Set(arrow_x +(view.grid_w / 2 + 1.5f) * view.scale,
		arrow_y + 3 * view.scale,
		0, c);
	pRenderer.AddLine(v0, v1);
	MyDX11PenVertex v2;
	v2.Set(arrow_x + (view.grid_w / 2 + 1.5f) * view.scale,
		arrow_y + 9 * view.scale,
		0, c);
	pRenderer.AddLine(v1, v2);
	MyDX11PenVertex v3;
	v3.Set(arrow_x + (view.grid_w / 2 + 4) * view.scale,
		arrow_y + 9 * view.scale,
		0, c);
	pRenderer.AddLine(v2, v3);
	MyDX11PenVertex v4;
	v4.Set(arrow_x + (view.grid_w / 2) * view.scale,
		arrow_y + 14 * view.scale,
		0, c);
	pRenderer.AddLine(v3, v4);
	MyDX11PenVertex v5;
	v5.Set(arrow_x + (view.grid_w / 2 - 4) * view.scale,
		arrow_y + 9 * view.scale,
		0, c);
	pRenderer.AddLine(v4, v5);
	MyDX11PenVertex v6;
	v6.Set(arrow_x + (view.grid_w / 2 - 1.5f) * view.scale,
		arrow_y + 9 * view.scale,
		0, c);
	pRenderer.AddLine(v5, v6);
	MyDX11PenVertex v7;
	v7.Set(arrow_x + (view.grid_w / 2 - 1.5f) * view.scale,
		arrow_y + 3 * view.scale,
		0, c);
	pRenderer.AddLine(v6, v7);
	pRenderer.DrawLine();
}
