/***********************/ /*** Map Editor Core ***/ /***********************/ #include "stdafx.h" #include #include #include #include "GLEnabledView.h" #include "MapEdit.h" #include "MapEditDoc.h" #include "MapEditView.h" #include "MainFrm.h" #include "NewMapGUI.h" #include "AddLayerDlg.h" #include "Core.h" #include "Layer.h" #include "LayerTile.h" #include "LayerCollision.h" #include "LayerShade.h" #include "utils.h" #include "Export.h" #include "LayerList.h" const Vector3 DefaultCamPos(0.0f,0.0f,0.9f); /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ CCore::CCore() { CurrentMousePos=CPoint(0,0); MapCam=DefaultCamPos; TileCam=DefaultCamPos; SpareFlag=false; GridFlag=true; Is3dFlag=true; CurrentView=NULL; CursorPos.x=CursorPos.y=0; } /*****************************************************************************/ CCore::~CCore() { int ListSize=Layer.size(); for (int i=0; iInitSubView(this); } ActiveLayer=FindLayer(LAYER_TYPE_TILE,LAYER_SUBTYPE_ACTION); CurrentLayer=Layer[ActiveLayer]; return(TRUE); } /*****************************************************************************/ void CCore::Load(CFile *File) { int Version,i; BOOL F; File->Read(&Version,sizeof(int)); if (Version>100000) Version=1; // Check fix for changing version to int from float if (VersionRead(&MapCam,sizeof(Vector3)); File->Read(&MapCamOfs,sizeof(Vector3)); File->Read(&TileCam,sizeof(Vector3)); File->Read(&TileCamOfs,sizeof(Vector3)); File->Read(&F,sizeof(BOOL)); SpareFlag=F!=0; File->Read(&F,sizeof(BOOL)); GridFlag=F!=0; File->Read(&F,sizeof(BOOL)); Is3dFlag=F!=0; // Layers int LayerCount; File->Read(&LayerCount,sizeof(int)); File->Read(&ActiveLayer,sizeof(int)); for (i=0;iRead(&Type,sizeof(int)); switch (Type) { case LAYER_TYPE_TILE: AddLayer(new CLayerTile(File,Version)); break; case LAYER_TYPE_COLLISION: AddLayer(new CLayerCollision(File,Version)); break; case LAYER_TYPE_SHADE: AddLayer(new CLayerShade(File,Version)); break; default: ASSERT(!"poos"); } } for (i=0; iInitSubView(this); } GetTileBank()->Load(File,Version); CurrentLayer=Layer[ActiveLayer]; // Check Layers int MapWidth=ActionLayer->GetWidth(); int MapHeight=ActionLayer->GetHeight(); for (i=0;iCheckLayerSize(MapWidth,MapHeight); } } /*****************************************************************************/ void CCore::Save(CFile *File) { BOOL F; // Version 1 File->Write(&FileVersion,sizeof(int)); File->Write(&MapCam,sizeof(Vector3)); File->Write(&MapCamOfs,sizeof(Vector3)); File->Write(&TileCam,sizeof(Vector3)); File->Write(&TileCamOfs,sizeof(Vector3)); F=SpareFlag; File->Write(&F,sizeof(BOOL)); F=GridFlag; File->Write(&F,sizeof(BOOL)); F=Is3dFlag; File->Write(&F,sizeof(BOOL)); // Layers int LayerCount=Layer.size(); File->Write(&LayerCount,sizeof(int)); File->Write(&ActiveLayer,sizeof(int)); for (int i=0;iGetType(); File->Write(&Type,sizeof(int)); Layer[i]->Save(File); } GetTileBank()->Save(File); } /*****************************************************************************/ bool CCore::Question(char *Txt) { CString Str; Str.Format(Txt); int Ret=AfxMessageBox(Str,MB_YESNO , MB_ICONQUESTION); if (Ret==IDYES) return(true); return(false); } /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ void CCore::Render(bool ForceRender) { Vector3 &ThisCam=GetCam(); if (!CurrentView) { TRACE0("No View\n"); UpdateAll(); return; } if (GetTileBank()->NeedLoad()) GetTileBank()->LoadTileSets(this); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen if (CurrentLayer!=Layer[ActiveLayer] || CurrentLayer->IsUnique()) { CurrentLayer->Render(this,ThisCam,Is3dFlag); CurrentLayer->RenderGrid(this,ThisCam,true); CurrentLayer->FindCursorPos(this,GetCam(),CurrentMousePos); } else { RenderLayers(); } CurrentLayer->RenderCursor(this,ThisCam,Is3dFlag); // Get Cursor Pos LastCursorPos=CursorPos; CurrentLayer->FindCursorPos(this,GetCam(),CurrentMousePos); } /*****************************************************************************/ void CCore::RenderLayers() { Vector3 &ThisCam=GetCam(); int ListSize=Layer.size(); int StartLayer,EndLayer; StartLayer=0; EndLayer=ListSize; // while (Layer[StartLayer]->IsUnique()) StartLayer++; // if (Layer[ActiveLayer]->IsUnique()) // { // StartLayer=ActiveLayer; // EndLayer=StartLayer+1; // } for (int i=StartLayer; iIsVisible()) { Layer[i]->Render(this,ThisCam,Is3dFlag); if (GridFlag) Layer[i]->RenderGrid(this,ThisCam,i==ActiveLayer); } } } /*****************************************************************************/ /*** Control *****************************************************************/ /*****************************************************************************/ void CCore::LButtonControl(UINT nFlags, CPoint &point,bool DownFlag) { if (CurrentLayer->IsVisible()) { CurrentLayer->LButtonControl(this,nFlags,CursorPos,DownFlag); } Command(CmdMsg_ActiveBrushLeft,0,0); RedrawView(); } /*****************************************************************************/ void CCore::MButtonControl(UINT nFlags, CPoint &point,bool DownFlag) { } /*****************************************************************************/ void CCore::RButtonControl(UINT nFlags, CPoint &point,bool DownFlag) { if (CurrentLayer->IsVisible()) { CurrentLayer->RButtonControl(this,nFlags,CursorPos,DownFlag); } Command(CmdMsg_ActiveBrushRight,0,0); RedrawView(); } /*****************************************************************************/ void CCore::Zoom(float Dst) { Vector3 Ofs; Ofs.Zero(); Ofs.z=Dst; UpdateView(&Ofs); } /*****************************************************************************/ void CCore::MouseWheel(UINT nFlags, short zDelta, CPoint &pt) { if (zDelta>0) Zoom(-0.1f); else Zoom(+0.1f); } /*****************************************************************************/ void CCore::MouseMove(UINT nFlags, CPoint &point) { Vector3 Ofs; Vector3 &ThisCam=GetCam(); // check if active doc Ofs.Zero(); if (theApp.GetCurrent()!=CurrentView->GetDocument()) return; GetWorkingPath(); CurrentMousePos=point; // Handle Drag Movement if (nFlags & MK_MBUTTON || nFlags & MK_SHIFT) { float XS,YS; RECT ThisRect; float MoveSpd=GetZoomW(); if (nFlags & MK_CONTROL) { MoveSpd*=4; } CurrentView->GetWindowRect(&ThisRect); XS=ThisCam.z*MoveSpd; YS=ThisCam.z*MoveSpd; XS/=((ThisRect.right-ThisRect.left)); YS/=((ThisRect.bottom-ThisRect.top)); Ofs.x=LastMousePos.x-CurrentMousePos.x; Ofs.y=LastMousePos.y-CurrentMousePos.y; Ofs.x*=XS; Ofs.y*=YS; UpdateView(&Ofs); } else { if (CurrentLayer->IsVisible()) { CurrentLayer->MouseMove(this,nFlags,CursorPos); } // Mouse still moved, so need to redraw windows, to get CursorPos RedrawView(); } LastMousePos=CurrentMousePos; } /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ void CCore::Command(int CmdMsg,int Param0,int Param1) { bool RedrawFlag=false; switch(CmdMsg) { case CmdMsg_ToggleSubView: ToggleSubView(); break; case CmdMsg_ToggleGrid: ToggleGrid(); break; case CmdMsg_Toggle2d: Toggle2d3d(); break; case CmdMsg_ZoomIn: Zoom(-0.1f); break; case CmdMsg_ZoomOut: Zoom(+0.1f); break; case CmdMsg_ResetView: ResetView(); break; case CmdMsg_SetLayer: SetLayer(Param0); break; case CmdMsg_AddLayer: AddLayer(Param0); break; case CmdMsg_DeleteLayer: DeleteLayer(Param0); break; // Pass remaining to Active Layer default: RedrawFlag=CurrentLayer->Command(CmdMsg,this,Param0,Param1); break; } /* if (RedrawFlag) */RedrawView(); } /*****************************************************************************/ /*** Layers ******************************************************************/ /*****************************************************************************/ void CCore::UpdateParamBar() { CMainFrame *Frm=(CMainFrame*)AfxGetMainWnd(); CMultiBar *ParamBar=Frm->GetParamBar(); GUIRemoveAll(); GUIAdd(LayerList,IDD_LAYER_LIST_DIALOG); CurrentLayer->GUIInit(this); GUIUpdate(); } /*****************************************************************************/ void CCore::UpdateLayerGUI() { int ListSize=Layer.size(); if (LayerList.ListBox) { LayerList.ListBox.ResetContent(); for (int i=0; iGetName()); } // Now sets checks (silly MSoft bug!!) for (i=0; iIsVisible()); } LayerList.ListBox.SetCurSel(ActiveLayer); } } /*****************************************************************************/ void CCore::SetLayer(int NewLayer,bool Force) { int LayerCount=Layer.size()-1; int LastLayer=ActiveLayer; if (NewLayer<0) NewLayer=0; // If toggling layer, dont change the layer if ((int)LayerList.ListBox.GetCheck(NewLayer)!=(int)Layer[NewLayer]->IsVisible()) { Layer[NewLayer]->SetVisible(LayerList.ListBox.GetCheck(NewLayer)!=0); LayerList.ListBox.SetCurSel(ActiveLayer); } else { bool IsCol=Layer[NewLayer]->GetType()==LAYER_TYPE_COLLISION; GetTileBank()->SetCollision(IsCol); ActiveLayer=NewLayer; } if (LastLayer!=ActiveLayer || Force) { if (LastLayer<=LayerCount) CurrentLayer->GUIKill(this); CurrentLayer=Layer[ActiveLayer]; CurrentLayer->GUIInit(this); GUIUpdate(); } RedrawView(); } /*****************************************************************************/ int CCore::AddLayer(CLayer *NewLayer) { int ListSize=Layer.size(); int NewIdx=CLayer::GetLayerIdx(NewLayer->GetType(),NewLayer->GetSubType()); int Idx=ListSize; TRACE3("Add Layer %i %i @ %i\n",NewLayer->GetType(),NewLayer->GetSubType(),NewIdx); for (Idx=0; IdxGetType(),Layer[Idx]->GetSubType()); if (NewIdxGetType()==LAYER_TYPE_TILE && NewLayer->GetSubType()==LAYER_SUBTYPE_ACTION) ActionLayer=(CLayerTile*)NewLayer; NewLayer->InitSubView(this); return(Idx); } /*****************************************************************************/ int CCore::AddLayer(int Type, int SubType, int Width, int Height) { int Idx; switch (Type) { case LAYER_TYPE_TILE: Idx=AddLayer(new CLayerTile(SubType, Width,Height)); break; case LAYER_TYPE_COLLISION: Idx=AddLayer(new CLayerCollision(SubType, Width,Height)); break; case LAYER_TYPE_SHADE: Idx=AddLayer(new CLayerShade(SubType, Width,Height)); break; default: ASSERT(!"AddLayer - Invalid Layer Type"); break; } return(Idx); } /*****************************************************************************/ void CCore::AddLayer(int CurrentLayer) { std::vector List; CAddLayerDlg Dlg; int NewLayerId=0; int Sel; // Build Unused List Dlg.Sel=&Sel; Sel=0; for (int i=0; iGetWidth(); int Height=ActionLayer->GetHeight(); int Idx=AddLayer(CLayer::InfoTable[NewLayerId].Type,CLayer::InfoTable[NewLayerId].SubType,Width,Height); if (ActiveLayer>=Idx) ActiveLayer++; SetLayer(Idx,true); } /*****************************************************************************/ void CCore::DeleteLayer(int CurrentLayer) { if (Layer[CurrentLayer]->CanDelete()) { Layer[CurrentLayer]->GUIKill(this); delete Layer[CurrentLayer]; Layer.erase(Layer.begin() + CurrentLayer); SetLayer(CurrentLayer-1,true); TRACE1("Deleted Layer %i\n",CurrentLayer); } else { TRACE1("Cant Delete Layer %i\n",CurrentLayer); } } /*****************************************************************************/ CLayer *CCore::FindSubView(int Type) { int i,ListSize=Layer.size(); for (i=0;iGetSubViewType()==Type) { CLayer *SV=Layer[i]->GetSubView(); if (SV) return(SV); } } return(0); } /*****************************************************************************/ /*** Grid ********************************************************************/ /*****************************************************************************/ void CCore::ToggleGrid() { GridFlag=!GridFlag; RedrawView(); } /*****************************************************************************/ /*** SubView *****************************************************************/ /*****************************************************************************/ void CCore::ToggleSubView() { CLayer *LastLayer=CurrentLayer; CurrentLayer=CurrentLayer->GetSubView(); // <-- Funky toggle code of what!! if (!CurrentLayer) CurrentLayer=Layer[ActiveLayer]; if (LastLayer!=CurrentLayer) { LastLayer->GUIKill(this); CurrentLayer->GUIInit(this); GUIUpdate(); RedrawView(); } } /*****************************************************************************/ /*** Misc ********************************************************************/ /*****************************************************************************/ Vector3 &CCore::GetCam() { // if (SubViewFlag) // return(TileCam); // else return(MapCam); } /*****************************************************************************/ void CCore::SetScale() { float XS=GetZoomW(); float YS=GetZoomH(); float ZS=XS/YS; ScaleVector.x=1.0f/XS; ScaleVector.y=1.0f/XS; ScaleVector.z=1.0f/XS; } /*****************************************************************************/ float CCore::GetZoomW() { Vector3 &ThisCam=GetCam(); return((float)SCREEN_MAP_WIDTH/ThisCam.z); } /*****************************************************************************/ float CCore::GetZoomH() { Vector3 &ThisCam=GetCam(); return((float)SCREEN_MAP_HEIGHT/ThisCam.z); } /*****************************************************************************/ void CCore::ResetView() { Vector3 &ThisCam=GetCam(); ThisCam=DefaultCamPos; UpdateView(); } /*****************************************************************************/ /*** GUI *********************************************************************/ /*****************************************************************************/ void CCore::GUIAdd(CDialog &Dlg,int ID,bool Visible,bool Lock) { CMainFrame *Frm=(CMainFrame*)AfxGetMainWnd(); CMultiBar *ParamBar=Frm->GetParamBar(); ParamBar->Add(Dlg,ID,Visible,Lock); } /*****************************************************************************/ void CCore::GUIRemove(CDialog &Dlg,int ID,bool Force) { CMainFrame *Frm=(CMainFrame*)AfxGetMainWnd(); CMultiBar *ParamBar=Frm->GetParamBar(); ParamBar->Remove(Dlg,Force); } /*****************************************************************************/ void CCore::GUIRemoveAll(bool Force) { CMainFrame *Frm=(CMainFrame*)AfxGetMainWnd(); CMultiBar *ParamBar=Frm->GetParamBar(); ParamBar->RemoveAll(Force); } /*****************************************************************************/ void CCore::GUIUpdate() { CMainFrame *Frm=(CMainFrame*)AfxGetMainWnd(); CMultiBar *ParamBar=Frm->GetParamBar(); UpdateLayerGUI(); CurrentLayer->GUIUpdate(this); ParamBar->Update(); } /*****************************************************************************/ void CCore::GUIChanged() { CurrentLayer->GUIChanged(this); } /*****************************************************************************/ void CCore::UpdateAll() { UpdateParamBar(); RedrawView(); } /*****************************************************************************/ void CCore::RedrawView() { if (CurrentView) CurrentView->Invalidate(); } /*****************************************************************************/ void CCore::UpdateView(Vector3 *Ofs) { if (Ofs) { Vector3 &ThisCam=GetCam(); ThisCam.x+=Ofs->x; ThisCam.y+=Ofs->y; ThisCam.z-=Ofs->z; if (ThisCam.x<0) ThisCam.x=0; if (ThisCam.y<0) ThisCam.y=0; if (ThisCam.z<0.1) ThisCam.z=0.1f; } SetScale(); RedrawView(); } /*****************************************************************************/ void CCore::SetMapSize(int Width,int Height) { if (Width==GetMapWidth() && Height==GetMapHeight()) return; int ListSize=Layer.size(); for (int i=0; iResize(Width,Height); } RedrawView(); } /*****************************************************************************/ void CCore::Toggle2d3d() { Is3dFlag=!Is3dFlag; RedrawView(); } /*****************************************************************************/ int CCore::FindLayer(int Type,int SubType) { int ListSize=Layer.size(); for (int i=0; iGetType()==Type) if (SubType==-1 || Layer[i]->GetSubType()==SubType) return(i); } return(-1); } /*****************************************************************************/ Vector3 CCore::OffsetCam(Vector3 &Cam,float DivVal) { Vector3 ThisCam=Cam; ThisCam=Cam/DivVal; ThisCam.z=Cam.z; return(ThisCam); } /*****************************************************************************/ void CCore::Export(char *Filename) { int LayerCount=Layer.size(); char ExportName[256]; SetFileExt(Filename,ExportName,"MEX"); CExport Exp(ExportName); for (int i=0;iCanExport()) { Layer[i]->Export(this,Exp); } } Exp.ExportTiles(this); Exp.ExportStrList(this); }