/************************/ /*** Elem Sotre Stuph ***/ /************************/ #include "stdafx.h" #include #include #include #include "GLEnabledView.h" #include #include #include "Core.h" #include "ElemStore.h" #include "GinTex.h" #include "utils.h" #include "MapEdit.h" #include "MapEditDoc.h" #include "MapEditView.h" #include "MainFrm.h" #include "LayerTileGui.h" /*****************************************************************************/ const float ElemBrowserGap=0.2f; const float ElemBrowserX0=0-ElemBrowserGap/2; const float ElemBrowserX1=1+ElemBrowserGap/2; const float ElemBrowserY0=0-ElemBrowserGap/2; const float ElemBrowserY1=1+ElemBrowserGap/2; /*****************************************************************************/ CElemStore::CElemStore() { LoadFlag=false; CurrentSet=0; LastSet=0; for (int i=0; iGetFilePath(); GString FilePath; char FixPath[1024]; FilePath=RootPath.Drive(); FilePath+=RootPath.Dir(); FilePath.Append('\\'); FilePath.Upper(); File->Read(&ListSize,sizeof(int)); File->Read(&CurrentSet,sizeof(int)); File->Read(&ActiveBrush,sizeof(int)); Brush[0].Load(File,Version); Brush[1].Load(File,Version); if (Version<2) { CurrentSet++; } // New Style rel storage for (int i=0;iRead(&c,1); FullName.Append(c); } FullName.Upper(); GFName::makeabsolute(FilePath,FullName,FixPath); FullName=FixPath; _fullpath( FixPath, FullName, 1024); for (int z=0; zRead(&LayerCam,sizeof(Vector3)); File->Read(&CurrentSet,sizeof(int)); File->Read(&ActiveBrush,sizeof(int)); Brush[0].Load(File,Version); Brush[1].Load(File,Version); CElemBank::Load(File,Version); } } /*****************************************************************************/ void CElemStore::Save(CFile *File) { int ListSize=GetSetCount(); GFName RootPath=File->GetFilePath(); GString SavePath; SavePath=RootPath.Drive(); SavePath+=RootPath.Dir(); SavePath.Append('\\'); SavePath.Upper(); File->Write(&LayerCam,sizeof(Vector3)); File->Write(&CurrentSet,sizeof(int)); File->Write(&ActiveBrush,sizeof(int)); Brush[0].Save(File); Brush[1].Save(File); CElemBank::Save(File); } /*****************************************************************************/ void CElemStore::RenderElem(int Set,int Elem,int Flags,bool Is3d) { if (IsValid(Set,Elem)) { SetList[Set].RenderElem(Elem,Flags,Is3d); } else { SetList[0].RenderInvalid(); } } /*****************************************************************************/ void CElemStore::DeleteCurrent() { int ListSize=GetSetCount(); // Remap Brushes for (int i=0; iListSize) return; if (!ListSize) return; if (SelStart==-1) { Start=GetElemPos(CursorPos,BrowserWidth); End=Start; } else { CPoint S=IDToPoint(SelStart-1,BrowserWidth); CPoint E=IDToPoint(SelEnd-1,BrowserWidth); Start=CPoint( min(S.x,E.x), min(S.y,E.y)); End=CPoint( max(S.x,E.x), max(S.y,E.y)); if (PointToID(End,BrowserWidth)>=MaxElem) return; // Invalid selection } glMatrixMode(GL_MODELVIEW); glPushMatrix(); for (int Y=Start.y; Y<=End.y; Y++) { for (int X=Start.x; X<=End.x; X++) { float XPos=(float)X*(1+ElemBrowserGap); float YPos=(float)Y*(1+ElemBrowserGap); glLoadIdentity(); glScalef(Scale,Scale,Scale); glTranslatef(-CamPos.x+XPos,CamPos.y-YPos,0); glBegin(GL_QUADS); glColor4f(1,1,0,0.5); BuildGLQuad(ElemBrowserX0,ElemBrowserX1,ElemBrowserY0,ElemBrowserY1,0); glEnd(); } } glPopMatrix(); } /*****************************************************************************/ void CElemStore::RenderGrid(CCore *Core,Vector3 &CamPos,bool Active) { if (!GetSetCount()) return; CElemSet &ThisSet=SetList[CurrentSet]; int ListSize=ThisSet.GetCount(); int BrowserWidth=ThisSet.GetBrowserWidth(); int ElemID=1; // Dont bother with blank, its sorted float Scale=CamPos.z/(float)BrowserWidth/2.0; if (!ListSize) return; glMatrixMode(GL_MODELVIEW); glPushMatrix(); while(ElemID!=ListSize) { CPoint Pos=GetElemPos(ElemID,BrowserWidth); float XPos=(float)Pos.x*(1+ElemBrowserGap); float YPos=(float)Pos.y*(1+ElemBrowserGap); glLoadIdentity(); glScalef(Scale,Scale,Scale); glTranslatef(-CamPos.x+XPos,CamPos.y-YPos,0); glBegin(GL_LINES); glColor3f(1,1,1); glVertex3f( ElemBrowserX0,ElemBrowserY0,0); glVertex3f( ElemBrowserX1,ElemBrowserY0,0); glVertex3f( ElemBrowserX0,ElemBrowserY1,0); glVertex3f( ElemBrowserX1,ElemBrowserY1,0); glVertex3f( ElemBrowserX0,ElemBrowserY0,0); glVertex3f( ElemBrowserX0,ElemBrowserY1,0); glVertex3f( ElemBrowserX1,ElemBrowserY0,0); glVertex3f( ElemBrowserX1,ElemBrowserY1,0); glEnd(); ElemID++; } glPopMatrix(); } /*****************************************************************************/ void CElemStore::FindCursorPos(CCore *Core,Vector3 &CamPos,CPoint &MousePos) { if (!GetSetCount()) return; CElemSet &ThisSet=SetList[CurrentSet]; int ListSize=ThisSet.GetCount(); int BrowserWidth=ThisSet.GetBrowserWidth(); GLint Viewport[4]; GLuint SelectBuffer[SELECT_BUFFER_SIZE]; int HitCount; int ElemID=0; float Scale=CamPos.z/(float)BrowserWidth/2.0; if (!ListSize) return; glGetIntegerv(GL_VIEWPORT, Viewport); glSelectBuffer (SELECT_BUFFER_SIZE, SelectBuffer ); glRenderMode (GL_SELECT); glInitNames(); glPushName(-1); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPickMatrix( MousePos.x ,(Viewport[3]-MousePos.y),5.0,5.0,Viewport); Core->GetView()->SetupPersMatrix(); glMatrixMode(GL_MODELVIEW); glPushMatrix(); while(ElemID!=ListSize) { CPoint Pos=GetElemPos(ElemID,BrowserWidth); float XPos=(float)Pos.x*(1+ElemBrowserGap); float YPos=(float)Pos.y*(1+ElemBrowserGap); glLoadIdentity(); glScalef(Scale,Scale,Scale); glTranslatef(-CamPos.x+XPos,CamPos.y-YPos,0); glLoadName (ElemID); glBegin (GL_QUADS); BuildGLQuad(ElemBrowserX0,ElemBrowserX1,ElemBrowserY0,ElemBrowserY1,0); glEnd(); ElemID++; } HitCount= glRenderMode (GL_RENDER); glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); // Process hits GLuint *HitPtr=SelectBuffer; ElemID=-2; if (HitCount) // Just take 1st { ElemID=HitPtr[3]; } glMatrixMode(GL_MODELVIEW); // <-- Prevent arse GL assert CursorPos=ElemID; SelEnd=CursorPos; } /*****************************************************************************/ bool CElemStore::LButtonControl(CCore *Core,UINT nFlags, CPoint &CursorPos,bool DownFlag) { if (nFlags & MK_RBUTTON) { SelectCancel(); return(false); } Select(LBrush,DownFlag); return(true); } /*****************************************************************************/ bool CElemStore::RButtonControl(CCore *Core,UINT nFlags, CPoint &CursorPos,bool DownFlag) { if (nFlags & MK_LBUTTON) { SelectCancel(); return(false); } Select(RBrush,DownFlag); return(false); } /*****************************************************************************/ bool CElemStore::MouseMove(CCore *Core,UINT nFlags, CPoint &CursorPos) { return(false); } /*****************************************************************************/ bool CElemStore::Command(int CmdMsg,CCore *Core,int Param0,int Param1) { switch(CmdMsg) { case CmdMsg_SubViewLoad: LoadNewSet(Core); GUIUpdate(Core); break; case CmdMsg_SubViewDelete: DeleteSet(Core); GUIUpdate(Core); break; case CmdMsg_SubViewUpdate: ReloadAllSets(); Core->GetTexCache().Purge(); GUIUpdate(Core); break; case CmdMsg_SubViewSet: CurrentSet=ElemStoreGUI.m_List.GetCurSel(); GUIUpdate(Core); break; case CmdMsg_ActiveBrushLeft: ActiveBrush=LBrush; break; case CmdMsg_ActiveBrushRight: ActiveBrush=RBrush; default: TRACE3("ElemStore-Unhandled Command %i (%i,%i)\n",CmdMsg,Param0,Param1); } return(true); } /*****************************************************************************/ /*** Gui *********************************************************************/ /*****************************************************************************/ void CElemStore::GUIInit(CCore *Core) { Core->GUIAdd(ElemStoreGUI,IDD_LAYERTILE_GUI); } /*****************************************************************************/ void CElemStore::GUIKill(CCore *Core) { Core->GUIRemove(ElemStoreGUI,IDD_LAYERTILE_GUI); } /*****************************************************************************/ void CElemStore::GUIUpdate(CCore *Core) { int ListSize=GetSetCount(); bool IsSubView=Core->IsSubView(); if (ElemStoreGUI.m_List) { ElemStoreGUI.m_List.ResetContent(); if (ListSize) { for (int i=0; i=MaxElem) SelectCancel(); // Invalid selection ThisElem.Set=CurrentSet; ThisElem.Flags=0; ThisBrush.Delete(); ThisBrush.SetSize(Width,Height); for (int Y=0; YQuestion("Delete Current Elem Bank\n\nAll used Elems in current set will be set to blank\nAre you sure?")) { int SetCount=GetSetCount(); int i,ListSize=Core->GetLayerCount(); for (i=0;iGetLayer(i); if (ThisLayer->GetType()==LAYER_TYPE_TILE) { ThisLayer->RemoveSet(CurrentSet); } } DeleteCurrent(); for (int Set=CurrentSet+1; SetGetLayer(i); if (ThisLayer->GetType()==LAYER_TYPE_TILE) { ThisLayer->RemapSet(Set,Set-1); } } } } CurrentSet--; GUIUpdate(Core); }