SBSPSS/Utils/MapEdit/TileSet.cpp

455 lines
11 KiB
C++
Raw Normal View History

2000-10-27 02:06:19 +02:00
/*********************/
/*** TileSet Stuph ***/
/*********************/
#include "stdafx.h"
2000-12-04 17:47:34 +01:00
#include <Vector3.h>
2000-10-27 02:06:19 +02:00
#include <gl\gl.h>
#include <gl\glu.h>
2000-11-14 16:03:04 +01:00
#include "GLEnabledView.h"
2000-10-27 02:06:19 +02:00
#include <Vector>
2001-02-01 23:48:22 +01:00
2000-12-04 17:47:34 +01:00
#include <GFName.hpp>
2000-10-27 02:06:19 +02:00
2000-11-06 21:24:11 +01:00
#include "Core.h"
2000-10-27 02:06:19 +02:00
#include "TileSet.h"
#include "GinTex.h"
#include "utils.h"
2000-11-14 16:03:04 +01:00
#include "MapEdit.h"
#include "MapEditDoc.h"
#include "MapEditView.h"
#include "MainFrm.h"
2001-03-13 22:00:34 +01:00
#include "GUITileBank.h"
2000-11-14 16:03:04 +01:00
2000-10-27 02:06:19 +02:00
/*****************************************************************************/
2000-11-06 21:24:11 +01:00
/*** TileBank ****************************************************************/
2000-10-27 02:06:19 +02:00
/*****************************************************************************/
2000-11-14 16:03:04 +01:00
const float TileBrowserGap=0.2f;
const float TileBrowserX0=0-TileBrowserGap/2;
const float TileBrowserX1=1+TileBrowserGap/2;
const float TileBrowserY0=0-TileBrowserGap/2;
const float TileBrowserY1=1+TileBrowserGap/2;
2000-11-06 21:24:11 +01:00
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
CTileBank::CTileBank() : CElemBank(16,16,true,false)
2000-11-06 21:24:11 +01:00
{
2000-11-17 22:36:13 +01:00
for (int i=0; i<MaxBrush; i++) Brush[i].Delete();
ActiveBrush=0;
SelStart=-1;
2000-11-06 21:24:11 +01:00
}
/*****************************************************************************/
CTileBank::~CTileBank()
{
}
2000-11-20 21:33:42 +01:00
/*****************************************************************************/
2001-03-05 21:49:46 +01:00
void CTileBank::Load(CFile *File,int Version)
2001-01-23 22:53:48 +01:00
{
2001-03-26 23:29:09 +02:00
if (Version<=4)
2001-01-23 22:53:48 +01:00
{
2001-03-22 21:49:58 +01:00
File->Read(&LayerCam,sizeof(Vector3));
2001-03-05 21:49:46 +01:00
File->Read(&CurrentSet,sizeof(int));
File->Read(&ActiveBrush,sizeof(int));
Brush[0].Load(File,Version);
Brush[1].Load(File,Version);
}
else
{
2001-03-22 21:49:58 +01:00
File->Read(&LayerCam,sizeof(Vector3));
File->Read(&ActiveBrush,sizeof(int));
Brush[0].Load(File,Version);
Brush[1].Load(File,Version);
2001-03-05 21:49:46 +01:00
}
2001-02-07 19:46:59 +01:00
2001-03-22 21:49:58 +01:00
CElemBank::Load(File,Version);
2000-11-20 21:33:42 +01:00
}
/*****************************************************************************/
void CTileBank::Save(CFile *File)
{
2001-03-05 21:49:46 +01:00
File->Write(&LayerCam,sizeof(Vector3));
2000-11-28 15:34:42 +01:00
File->Write(&ActiveBrush,sizeof(int));
Brush[0].Save(File);
Brush[1].Save(File);
2000-11-20 21:33:42 +01:00
2001-03-05 21:49:46 +01:00
CElemBank::Save(File);
2000-11-06 21:24:11 +01:00
}
2000-11-29 18:07:57 +01:00
/*****************************************************************************/
2001-03-01 18:28:20 +01:00
void CTileBank::DeleteCurrent()
2000-11-29 18:07:57 +01:00
{
2001-03-05 21:49:46 +01:00
int ListSize=GetSetCount();
2000-12-11 22:29:59 +01:00
// Remap Brushes
for (int i=0; i<MaxBrush; i++)
{
2001-03-05 21:49:46 +01:00
Brush[i].RemoveSet(CurrentSet);
2000-12-11 22:29:59 +01:00
}
for (int Set=CurrentSet; Set<ListSize; Set++)
{
for (int i=0; i<MaxBrush; i++)
{
2001-01-23 22:53:48 +01:00
Brush[i].RemapSet(Set,Set);
2000-12-11 22:29:59 +01:00
}
}
2001-03-05 21:49:46 +01:00
SetList.erase(CurrentSet);
if (CurrentSet) CurrentSet--;
2000-11-29 18:07:57 +01:00
}
2000-11-06 21:24:11 +01:00
/*****************************************************************************/
2001-03-05 21:49:46 +01:00
void CTileBank::Render(CCore *Core,Vector3 &CamPos,bool Is3d)
2000-11-06 21:24:11 +01:00
{
2001-03-05 21:49:46 +01:00
if (!GetSetCount()) return;
CElemSet &ThisSet=SetList[CurrentSet];
int ListSize=ThisSet.GetCount();
int BrowserWidth=ThisSet.GetBrowserWidth();
int TileID=0;
sMapElem ThisElem;
int SelFlag;
float Scale=CamPos.z/(float)BrowserWidth/2.0;
ThisElem.Flags=0;
ThisElem.Set=CurrentSet;
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
while(TileID!=ListSize)
{
2001-03-22 21:49:58 +01:00
CPoint Pos=GetElemPos(TileID,BrowserWidth);
2001-03-05 21:49:46 +01:00
float XPos=(float)Pos.x*(1+TileBrowserGap);
float YPos=(float)Pos.y*(1+TileBrowserGap);
glLoadIdentity();
glScalef(Scale,Scale,Scale);
glTranslatef(-CamPos.x+XPos,CamPos.y-YPos,0);
glColor3f(1,1,1);
2001-03-27 18:26:43 +02:00
RenderElem(CurrentSet,TileID,0,Is3d);
2001-03-05 21:49:46 +01:00
// Selection
ThisElem.Tile=TileID;
SelFlag=0;
if (Brush[0].DoesContainTile(ThisElem)) SelFlag|=1;
if (Brush[1].DoesContainTile(ThisElem)) SelFlag|=2;
if (SelFlag)
{
glBegin(GL_QUADS);
switch(SelFlag)
{
case 1: // L
glColor4f(1,0,0,0.5);
BuildGLQuad(TileBrowserX0,TileBrowserX1,TileBrowserY0,TileBrowserY1,0.01f);
break;
case 2: // R
glColor4f(0,0,1,0.5);
BuildGLQuad(TileBrowserX0,TileBrowserX1,TileBrowserY0,TileBrowserY1,0.01f);
break;
case 3: // LR
glColor4f(1,0,0,0.5);
BuildGLQuad(TileBrowserX0,0.5,TileBrowserY0,TileBrowserY1,0.01f);
glColor4f(0,0,1,0.5);
BuildGLQuad(0.5,TileBrowserX1,TileBrowserY0,TileBrowserY1,0.01f);
break;
}
glEnd();
}
2001-03-27 18:26:43 +02:00
2001-03-05 21:49:46 +01:00
TileID++;
}
glPopMatrix();
2000-11-06 21:24:11 +01:00
}
2000-11-14 16:03:04 +01:00
/*****************************************************************************/
2001-03-05 21:49:46 +01:00
void CTileBank::RenderCursor(CCore *Core,Vector3 &CamPos,bool Is3d)
2000-11-14 16:03:04 +01:00
{
2001-03-05 21:49:46 +01:00
if (!GetSetCount()) return;
CElemSet &ThisSet=SetList[CurrentSet];
int ListSize=ThisSet.GetCount();
int BrowserWidth=ThisSet.GetBrowserWidth();
CPoint Start,End;
int MaxTile=ListSize;
float Scale=CamPos.z/(float)BrowserWidth/2.0;
if (CursorPos<-1 || CursorPos>ListSize) return;
if (!ListSize) return;
2000-11-14 16:03:04 +01:00
2001-03-05 21:49:46 +01:00
if (SelStart==-1)
2000-11-14 16:03:04 +01:00
{
2001-03-22 21:49:58 +01:00
Start=GetElemPos(CursorPos,BrowserWidth);
2001-03-05 21:49:46 +01:00
End=Start;
2000-11-14 16:03:04 +01:00
}
2001-03-05 21:49:46 +01:00
else
2000-11-14 16:03:04 +01:00
{
2001-03-05 21:49:46 +01:00
CPoint S=IDToPoint(SelStart-1,BrowserWidth);
2001-03-22 21:49:58 +01:00
CPoint E=IDToPoint(CursorPos-1,BrowserWidth);
2001-03-05 21:49:46 +01:00
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)>=MaxTile) return; // Invalid selection
2000-11-14 16:03:04 +01:00
}
2000-11-17 22:36:13 +01:00
2001-03-05 21:49:46 +01:00
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+TileBrowserGap);
float YPos=(float)Y*(1+TileBrowserGap);
glLoadIdentity();
glScalef(Scale,Scale,Scale);
glTranslatef(-CamPos.x+XPos,CamPos.y-YPos,0);
glBegin(GL_QUADS);
glColor4f(1,1,0,0.5);
BuildGLQuad(TileBrowserX0,TileBrowserX1,TileBrowserY0,TileBrowserY1,0);
glEnd();
}
}
glPopMatrix();
2001-03-01 18:28:20 +01:00
}
/*****************************************************************************/
bool CTileBank::LButtonControl(CCore *Core,UINT nFlags, CPoint &CursorPos,bool DownFlag)
{
if (nFlags & MK_RBUTTON)
{
SelectCancel();
return(false);
}
Select(LBrush,DownFlag);
return(true);
}
/*****************************************************************************/
bool CTileBank::RButtonControl(CCore *Core,UINT nFlags, CPoint &CursorPos,bool DownFlag)
{
if (nFlags & MK_LBUTTON)
{
SelectCancel();
return(false);
}
Select(RBrush,DownFlag);
return(false);
}
/*****************************************************************************/
bool CTileBank::MouseMove(CCore *Core,UINT nFlags, CPoint &CursorPos)
{
return(false);
}
/*****************************************************************************/
bool CTileBank::Command(int CmdMsg,CCore *Core,int Param0,int Param1)
{
switch(CmdMsg)
{
case CmdMsg_SubViewLoad:
2001-03-05 21:49:46 +01:00
LoadNewSet(Core);
2001-03-06 19:39:42 +01:00
GUIUpdate(Core);
2001-03-01 18:28:20 +01:00
break;
case CmdMsg_SubViewDelete:
DeleteSet(Core);
2001-03-06 19:39:42 +01:00
GUIUpdate(Core);
2001-03-01 18:28:20 +01:00
break;
case CmdMsg_SubViewUpdate:
2001-03-05 21:49:46 +01:00
ReloadAllSets();
2001-03-01 18:28:20 +01:00
Core->GetTexCache().Purge();
2001-03-06 19:39:42 +01:00
GUIUpdate(Core);
2001-03-01 18:28:20 +01:00
break;
case CmdMsg_SubViewSet:
2001-03-13 22:00:34 +01:00
CurrentSet=GUIElemList.m_List.GetCurSel();
2001-03-06 19:39:42 +01:00
GUIUpdate(Core);
2001-03-01 18:28:20 +01:00
break;
case CmdMsg_ActiveBrushLeft:
ActiveBrush=LBrush;
break;
case CmdMsg_ActiveBrushRight:
ActiveBrush=RBrush;
default:
TRACE3("TileBank-Unhandled Command %i (%i,%i)\n",CmdMsg,Param0,Param1);
}
return(true);
}
2000-11-14 16:03:04 +01:00
/*****************************************************************************/
2000-11-15 22:22:40 +01:00
/*** Gui *********************************************************************/
2000-11-14 16:03:04 +01:00
/*****************************************************************************/
2001-02-09 22:17:01 +01:00
void CTileBank::GUIInit(CCore *Core)
2000-11-14 16:03:04 +01:00
{
2001-03-13 22:00:34 +01:00
Core->GUIAdd(GUIElemList,IDD_ELEMLIST);
Core->GUIAdd(GUITileBank,IDD_TILEBANK);
2001-02-14 23:35:47 +01:00
}
/*****************************************************************************/
void CTileBank::GUIKill(CCore *Core)
{
2001-03-13 22:00:34 +01:00
Core->GUIRemove(GUIElemList,IDD_ELEMLIST);
Core->GUIRemove(GUITileBank,IDD_TILEBANK);
2001-02-09 22:17:01 +01:00
}
2000-11-14 16:03:04 +01:00
2001-02-09 22:17:01 +01:00
/*****************************************************************************/
void CTileBank::GUIUpdate(CCore *Core)
{
2001-03-05 21:49:46 +01:00
int ListSize=GetSetCount();
2001-03-06 19:39:42 +01:00
bool IsSubView=Core->IsSubView();
2001-02-09 22:17:01 +01:00
2001-03-13 22:00:34 +01:00
if (GUIElemList.m_List)
2000-11-28 15:34:42 +01:00
{
2001-03-13 22:00:34 +01:00
GUIElemList.m_List.ResetContent();
2001-03-05 21:49:46 +01:00
if (ListSize)
2000-11-28 15:34:42 +01:00
{
2001-03-05 21:49:46 +01:00
for (int i=0; i<ListSize; i++)
2001-02-09 22:17:01 +01:00
{
2001-03-13 22:00:34 +01:00
GUIElemList.m_List.AddString(GetSetName(i));
2001-02-09 22:17:01 +01:00
}
2001-03-13 22:00:34 +01:00
GUIElemList.m_List.SetCurSel(CurrentSet);
2000-11-28 15:34:42 +01:00
}
2001-02-09 22:17:01 +01:00
else
{
2001-03-01 18:28:20 +01:00
IsSubView=FALSE;
2001-02-09 22:17:01 +01:00
}
2001-03-13 22:00:34 +01:00
GUIElemList.m_List.EnableWindow(IsSubView);
2000-11-28 15:34:42 +01:00
}
2001-03-05 21:49:46 +01:00
2000-11-14 16:03:04 +01:00
}
2001-03-01 18:28:20 +01:00
/*****************************************************************************/
void CTileBank::GUIChanged(CCore *Core)
{
}
2000-11-14 16:03:04 +01:00
/*****************************************************************************/
2000-11-15 22:22:40 +01:00
/*** Functions ***************************************************************/
2000-11-14 16:03:04 +01:00
/*****************************************************************************/
2001-03-01 18:28:20 +01:00
bool CTileBank::Select(int BrushID,bool DownFlag)
2000-11-17 22:36:13 +01:00
{
if (DownFlag && SelStart==-1)
{
2000-11-28 22:16:00 +01:00
if (CursorPos<0) return(FALSE);
2000-11-17 22:36:13 +01:00
SelStart=CursorPos;
2001-03-05 21:49:46 +01:00
TRACE1("SEL Start %i\n",CursorPos);
2000-11-28 22:16:00 +01:00
if (CursorPos==0)
{
SetBrush(GetBrush(BrushID));
SelStart=-1;
TRACE0("Selected Blank\n");
}
2000-11-17 22:36:13 +01:00
}
else
if (!DownFlag && SelStart!=-1)
{
if (CursorPos==-1) return(SelectCancel());
SetBrush(GetBrush(BrushID));
SelStart=-1;
2001-03-05 21:49:46 +01:00
TRACE1("END SEL %i\n",CursorPos);
2000-11-17 22:36:13 +01:00
}
return(TRUE);
}
/*****************************************************************************/
void CTileBank::SetBrush(CMap &ThisBrush)
2000-11-14 16:03:04 +01:00
{
2001-03-05 21:49:46 +01:00
int BW=SetList[CurrentSet].GetBrowserWidth();
2000-11-28 22:16:00 +01:00
CPoint S=IDToPoint(SelStart-1,BW);
2001-03-22 21:49:58 +01:00
CPoint E=IDToPoint(CursorPos-1,BW);
2000-11-28 22:16:00 +01:00
int Width=abs(E.x-S.x)+1;
int Height=abs(E.y-S.y)+1;
2000-11-17 22:36:13 +01:00
sMapElem ThisElem;
2001-03-05 21:49:46 +01:00
int MaxTile=SetList[CurrentSet].GetCount();
2000-11-17 22:36:13 +01:00
2000-11-28 22:16:00 +01:00
// if (PointToID(End,BW)>=MaxTile) SelectCancel(); // Invalid selection
2000-11-17 22:36:13 +01:00
ThisElem.Set=CurrentSet;
ThisElem.Flags=0;
ThisBrush.Delete();
ThisBrush.SetSize(Width,Height);
for (int Y=0; Y<Height; Y++)
{
for (int X=0; X<Width; X++)
{
2000-11-28 22:16:00 +01:00
ThisElem.Tile=SelStart+X+(Y*BW);
2001-03-05 21:49:46 +01:00
if (!IsValid(CurrentSet,ThisElem.Tile))
2000-11-29 18:07:57 +01:00
{
2001-03-05 21:49:46 +01:00
TRACE2("Not valid %i %i\n",CurrentSet,ThisElem.Tile);
2000-11-29 18:07:57 +01:00
ThisElem.Tile=-1;
}
2001-03-05 21:49:46 +01:00
ThisBrush.Set(X,Y,ThisElem,true);
2000-11-17 22:36:13 +01:00
}
2000-11-15 22:22:40 +01:00
}
2000-11-17 22:36:13 +01:00
}
2000-11-14 16:03:04 +01:00
2000-11-17 22:36:13 +01:00
/*****************************************************************************/
2001-03-01 18:28:20 +01:00
bool CTileBank::SelectCancel()
2000-11-17 22:36:13 +01:00
{
SelStart=-1;
TRACE0("Select Cancelled\n");
2000-11-15 22:22:40 +01:00
return(TRUE);
2000-11-14 16:03:04 +01:00
}
2001-03-01 18:28:20 +01:00
/*****************************************************************************/
void CTileBank::DeleteSet(CCore *Core)
{
2001-04-07 23:05:33 +02:00
if (GUIElemList.m_List.GetCurSel()==-1) return;
2001-03-01 18:28:20 +01:00
if (Core->Question("Delete Current Tile Bank\n\nAll used tiles in current set will be set to blank\nAre you sure?"))
{
int SetCount=GetSetCount();
int i,ListSize=Core->GetLayerCount();
for (i=0;i<ListSize;i++)
{
CLayerTile *ThisLayer=(CLayerTile*)Core->GetLayer(i);
if (ThisLayer->GetType()==LAYER_TYPE_TILE)
{
2001-03-05 21:49:46 +01:00
ThisLayer->RemoveSet(CurrentSet);
2001-03-01 18:28:20 +01:00
}
}
DeleteCurrent();
for (int Set=CurrentSet+1; Set<SetCount; Set++)
{
for (i=0;i<ListSize;i++)
{
CLayerTile *ThisLayer=(CLayerTile*)Core->GetLayer(i);
if (ThisLayer->GetType()==LAYER_TYPE_TILE)
{
ThisLayer->RemapSet(Set,Set-1);
}
}
}
}
CurrentSet--;
2001-04-07 23:05:33 +02:00
2001-03-01 18:28:20 +01:00
GUIUpdate(Core);
}