SBSPSS/Utils/MapEdit/LayerTile.cpp

527 lines
13 KiB
C++
Raw Normal View History

2000-11-07 22:15:35 +01:00
/******************/
/*** Layer Tile ***/
/******************/
#include "stdafx.h"
2000-12-04 17:47:34 +01:00
//#include "gl3d.h"
#include <Vector3.h>
2000-11-07 22:15:35 +01:00
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glut.h>
#include "GLEnabledView.h"
2000-11-14 16:03:04 +01:00
#include "MapEdit.h"
2000-11-07 22:15:35 +01:00
#include "MapEditDoc.h"
#include "MapEditView.h"
2000-11-14 16:03:04 +01:00
#include "MainFrm.h"
2000-11-07 22:15:35 +01:00
#include "Core.h"
#include "Layer.h"
#include "LayerTile.h"
#include "Utils.h"
2000-11-24 23:34:20 +01:00
#include "Export.h"
2000-11-07 22:15:35 +01:00
2000-11-28 22:16:00 +01:00
/*****************************************************************************/
char *CLayerTile::LayerName[]=
{
"Back",
"Mid",
"Action",
"Fore",
};
2000-11-07 22:15:35 +01:00
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
// New Layer
2000-11-28 22:16:00 +01:00
CLayerTile::CLayerTile(int _SubType,int Width,int Height,float Scale,BOOL Is3d,BOOL Resizable)
2000-11-07 22:15:35 +01:00
{
2000-11-28 22:16:00 +01:00
// SetName(_Name);
SubType=_SubType;
ScaleFactor=Scale;
2000-11-20 17:21:43 +01:00
ResizeFlag=Resizable;
2000-11-07 22:15:35 +01:00
Render3dFlag=Is3d;
2000-11-21 22:27:55 +01:00
VisibleFlag=TRUE;
2000-11-15 22:22:40 +01:00
Mode=MouseModePaint;
2000-11-20 17:21:43 +01:00
2000-11-28 15:34:42 +01:00
if (ResizeFlag)
{
2000-11-30 23:17:55 +01:00
Width=TileLayerMinWidth+(Width-TileLayerMinWidth)/ScaleFactor;
Height=TileLayerMinHeight+(Height-TileLayerMinHeight)/ScaleFactor;
2000-11-28 15:34:42 +01:00
}
2000-11-30 23:17:55 +01:00
if (Width<TileLayerMinWidth) Width=TileLayerMinWidth;
if (Height<TileLayerMinHeight) Height=TileLayerMinHeight;
Map.SetSize(Width,Height,TRUE);
2000-11-07 22:15:35 +01:00
}
/*****************************************************************************/
// Load Layer
2000-11-20 21:33:42 +01:00
CLayerTile::CLayerTile(CFile *File,int Version)
2000-11-07 22:15:35 +01:00
{
2000-11-20 21:33:42 +01:00
Load(File,Version);
2000-11-07 22:15:35 +01:00
}
/*****************************************************************************/
CLayerTile::~CLayerTile()
{
}
2000-11-20 21:33:42 +01:00
/*****************************************************************************/
void CLayerTile::Load(CFile *File,float Version)
{
// Version 1
2000-11-28 22:16:00 +01:00
if (Version>=1.0)
{
File->Read(&Render3dFlag,sizeof(BOOL));
File->Read(&ScaleFactor,sizeof(float));
File->Read(&ResizeFlag,sizeof(BOOL));
File->Read(&VisibleFlag,sizeof(BOOL));
File->Read(&Mode,sizeof(MouseMode));
File->Read(&SubType,sizeof(int));
Map.Load(File,Version);
}
TRACE1("%s\t",GetName());
TRACE1("Scl:%g\t",ScaleFactor);
2000-11-28 00:07:07 +01:00
TRACE1("%i\n",VisibleFlag);
2000-11-20 21:33:42 +01:00
}
/*****************************************************************************/
void CLayerTile::Save(CFile *File)
{
// Always Save current version
File->Write(&Render3dFlag,sizeof(BOOL));
2000-11-28 22:16:00 +01:00
File->Write(&ScaleFactor,sizeof(float));
2000-11-20 21:33:42 +01:00
File->Write(&ResizeFlag,sizeof(BOOL));
2000-11-21 22:27:55 +01:00
File->Write(&VisibleFlag,sizeof(BOOL));
2000-11-20 21:33:42 +01:00
File->Write(&Mode,sizeof(MouseMode));
2000-11-28 22:16:00 +01:00
File->Write(&SubType,sizeof(SubType));
2000-11-20 21:33:42 +01:00
Map.Save(File);
}
2000-11-20 17:21:43 +01:00
/*****************************************************************************/
2000-11-30 23:17:55 +01:00
void CLayerTile::CheckLayerSize(int Width,int Height)
{
if (Resize(Width,Height))
{
CString mexstr;
mexstr.Format("%s Layer Resized to Correct Size\nPlease re-save\n", GetName());
AfxMessageBox(mexstr,MB_OK | MB_ICONEXCLAMATION);
}
}
/*****************************************************************************/
BOOL CLayerTile::Resize(int Width,int Height)
2000-11-20 17:21:43 +01:00
{
2000-11-30 23:17:55 +01:00
if (!ResizeFlag) return(FALSE); // Its a fixed size, so DONT DO IT!
2000-11-20 17:21:43 +01:00
2000-11-30 23:17:55 +01:00
int ThisWidth=Map.GetWidth();
int ThisHeight=Map.GetHeight();
Width=TileLayerMinWidth+(Width-TileLayerMinWidth)/ScaleFactor;
Height=TileLayerMinHeight+(Height-TileLayerMinHeight)/ScaleFactor;
if (ThisWidth!=Width || ThisHeight!=Height)
{
Map.Resize(Width,Height);
return(TRUE);
}
return(FALSE);
2000-11-20 17:21:43 +01:00
}
2000-11-07 22:15:35 +01:00
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
2000-12-04 17:47:34 +01:00
void CLayerTile::Render(CCore *Core,Vector3 &CamPos,BOOL Is3d)
2000-11-07 22:15:35 +01:00
{
2000-12-04 17:47:34 +01:00
Vector3 ThisCam=Core->OffsetCam(CamPos,GetScaleFactor());
2000-11-17 22:36:13 +01:00
if (Is3d && Render3dFlag)
{
glEnable(GL_DEPTH_TEST);
2000-11-22 23:08:47 +01:00
Render(Core,ThisCam,Map,TRUE);
2000-11-17 22:36:13 +01:00
glDisable(GL_DEPTH_TEST);
}
else
{
2000-11-22 23:08:47 +01:00
Render(Core,ThisCam,Map,FALSE);
2000-11-17 22:36:13 +01:00
}
2000-11-07 22:15:35 +01:00
}
/*****************************************************************************/
2000-12-04 17:47:34 +01:00
void CLayerTile::RenderCursorPaint(CCore *Core,Vector3 &CamPos,BOOL Is3d)
2000-11-17 22:36:13 +01:00
{
CTileBank &TileBank=Core->GetTileBank();
2000-12-04 17:47:34 +01:00
Vector3 ThisCam=Core->OffsetCam(CamPos,GetScaleFactor());
2000-11-17 22:36:13 +01:00
CPoint &CursPos=Core->GetCursorPos();
CMap &Brush=TileBank.GetActiveBrush();
if (!Brush.IsValid()) return;
if (CursPos.x==-1 || CursPos.y==-1) return;
ThisCam.x+=CursPos.x;
ThisCam.y-=CursPos.y;
if (Is3d && Render3dFlag)
{
glEnable(GL_DEPTH_TEST);
2000-11-22 23:08:47 +01:00
Render(Core,ThisCam,Brush,TRUE,0.5);
2000-11-17 22:36:13 +01:00
glDisable(GL_DEPTH_TEST);
}
else
{
2000-11-22 23:08:47 +01:00
Render(Core,ThisCam,Brush,FALSE,0.5);
2000-11-17 22:36:13 +01:00
}
}
/*****************************************************************************/
2000-12-04 17:47:34 +01:00
void CLayerTile::Render(CCore *Core,Vector3 &CamPos,CMap &ThisMap,BOOL Render3d,float Alpha)
2000-11-07 22:15:35 +01:00
{
2000-11-17 22:36:13 +01:00
int Width=ThisMap.GetWidth();
int Height=ThisMap.GetHeight();
if (Alpha<1)
{
2000-12-01 22:08:54 +01:00
glColor4f(1,1,1,Alpha);
2000-11-17 22:36:13 +01:00
}
else
{
2000-12-01 22:08:54 +01:00
glColor3f(1,1,1);
2000-11-17 22:36:13 +01:00
}
2000-11-07 22:15:35 +01:00
glMatrixMode(GL_MODELVIEW);
2000-11-17 22:36:13 +01:00
for (int YLoop=0; YLoop<Height; YLoop++)
2000-11-07 22:15:35 +01:00
{
2000-11-17 22:36:13 +01:00
for (int XLoop=0; XLoop<Width; XLoop++)
2000-11-07 22:15:35 +01:00
{
2000-11-17 22:36:13 +01:00
sMapElem &ThisElem=ThisMap.Get(XLoop,YLoop);
2000-11-29 18:07:57 +01:00
if (ThisElem.Tile && Core->IsTileValid(ThisElem.Set,ThisElem.Tile))
2000-11-15 22:22:40 +01:00
{ // Render Non Zero Tiles
CTile &ThisTile=Core->GetTile(ThisElem.Set,ThisElem.Tile);
glLoadIdentity(); // Slow way, but good to go for the mo
2000-11-17 22:36:13 +01:00
glTranslatef(CamPos.x+XLoop,CamPos.y-YLoop,CamPos.z);
2000-11-22 23:08:47 +01:00
ThisTile.Render(ThisElem.Flags,Render3d);
2000-11-15 22:22:40 +01:00
}
2000-11-07 22:15:35 +01:00
}
}
}
/*****************************************************************************/
2000-12-04 17:47:34 +01:00
void CLayerTile::RenderGrid(CCore *Core,Vector3 &CamPos,BOOL Active)
2000-11-07 22:15:35 +01:00
{
2000-11-17 22:36:13 +01:00
int Width=Map.GetWidth();
int Height=Map.GetHeight();
2000-12-04 17:47:34 +01:00
Vector3 ThisCam=Core->OffsetCam(CamPos,GetScaleFactor());
2000-11-07 22:15:35 +01:00
float OverVal=0.5;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
2000-11-28 00:07:07 +01:00
glTranslatef(ThisCam.x,ThisCam.y,ThisCam.z);
2000-11-07 22:15:35 +01:00
glBegin(GL_LINES);
2000-11-28 00:07:07 +01:00
if (Active)
2000-12-01 22:08:54 +01:00
glColor3f(1,1,1);
2000-11-28 00:07:07 +01:00
else
2000-12-01 22:08:54 +01:00
glColor3f(0.5,0.5,0.5);
2000-11-07 22:15:35 +01:00
2000-11-17 22:36:13 +01:00
for (int YLoop=0; YLoop<Height+1; YLoop++)
2000-11-07 22:15:35 +01:00
{
glVertex3f( 0-OverVal, -YLoop+1, 0);
2000-11-17 22:36:13 +01:00
glVertex3f( Width+OverVal, -YLoop+1, 0);
2000-11-07 22:15:35 +01:00
}
2000-11-17 22:36:13 +01:00
for (int XLoop=0; XLoop<Width+1; XLoop++)
2000-11-07 22:15:35 +01:00
{
glVertex3f( XLoop, 0+1+OverVal, 0);
2000-11-17 22:36:13 +01:00
glVertex3f( XLoop, -Height+1-OverVal, 0);
2000-11-07 22:15:35 +01:00
}
glEnd();
}
/*****************************************************************************/
2000-12-04 17:47:34 +01:00
void CLayerTile::FindCursorPos(CCore *Core,CMapEditView *View,Vector3 &CamPos,CPoint &MousePos)
2000-11-07 22:15:35 +01:00
{
GLint Viewport[4];
GLuint SelectBuffer[SELECT_BUFFER_SIZE];
int HitCount;
int TileID=0;
CPoint &CursorPos=Core->GetCursorPos();
2000-11-17 22:36:13 +01:00
int Width=Map.GetWidth();
int Height=Map.GetHeight();
2000-12-04 17:47:34 +01:00
Vector3 ThisCam=Core->OffsetCam(CamPos,GetScaleFactor());
2000-11-08 20:53:57 +01:00
2000-11-07 22:15:35 +01:00
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);
View->SetupPersMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
2000-11-28 00:07:07 +01:00
glTranslatef(ThisCam.x,ThisCam.y,ThisCam.z);
2000-11-07 22:15:35 +01:00
2000-11-17 22:36:13 +01:00
for (int YLoop=0; YLoop<Height; YLoop++)
2000-11-07 22:15:35 +01:00
{
2000-11-17 22:36:13 +01:00
for (int XLoop=0; XLoop<Width; XLoop++)
2000-11-07 22:15:35 +01:00
{
glLoadName (TileID);
glBegin (GL_QUADS);
BuildGLQuad(XLoop,XLoop+1,-YLoop,-YLoop+1,0);
glEnd();
TileID++;
}
}
HitCount= glRenderMode (GL_RENDER);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
CursorPos.x=CursorPos.y=-1;
// Process hits
GLuint *HitPtr=SelectBuffer;
if (HitCount) // Just take 1st
{
int HitID=HitPtr[3];
2000-11-17 22:36:13 +01:00
CursorPos=IDToPoint(HitID,Width);
2000-11-07 22:15:35 +01:00
}
glMatrixMode(GL_MODELVIEW); // <-- Prevent arse GL assert
}
2000-11-14 16:03:04 +01:00
/*****************************************************************************/
/*** Gui *********************************************************************/
/*****************************************************************************/
void CLayerTile::InitGUI(CCore *Core)
{
CMainFrame *Frm=(CMainFrame*)AfxGetApp()->GetMainWnd();
CMultiBar *ParamBar=Frm->GetParamBar();
2000-11-28 15:34:42 +01:00
ParamBar->Add(Frm->GetLayerTileGUI(),IDD_LAYERTILE_GUI,TRUE);
2000-11-14 16:03:04 +01:00
}
/*****************************************************************************/
void CLayerTile::UpdateGUI(CCore *Core)
{
2000-11-28 15:34:42 +01:00
CMainFrame *Frm=(CMainFrame*)AfxGetApp()->GetMainWnd();
CLayerTileGUI *Dlg=(CLayerTileGUI *)Frm->GetDialog(IDD_LAYERTILE_GUI);
2000-11-17 00:08:54 +01:00
2000-11-28 15:34:42 +01:00
if (Dlg)
2000-11-17 00:08:54 +01:00
{
2000-11-28 15:34:42 +01:00
Dlg->ResetButtons();
2000-11-17 00:08:54 +01:00
switch(Mode)
{
case MouseModePaint:
2000-11-28 15:34:42 +01:00
Dlg->SetButtonState(CLayerTileGUI::PAINT,TRUE);
2000-11-17 00:08:54 +01:00
break;
case MouseModeSelect:
2000-11-28 15:34:42 +01:00
Dlg->SetButtonState(CLayerTileGUI::SELECT,TRUE);
2000-11-17 00:08:54 +01:00
break;
default:
break;
}
}
2000-11-28 15:34:42 +01:00
Core->UpdateTileViewGUI();
2000-11-15 22:22:40 +01:00
}
/*****************************************************************************/
/*** Functions ***************************************************************/
/*****************************************************************************/
BOOL CLayerTile::SetMode(int NewMode)
{
BOOL Ret=FALSE;
2000-11-17 00:08:54 +01:00
2000-11-15 22:22:40 +01:00
// Clean up last mode
2000-11-17 00:08:54 +01:00
Ret|=ExitMode();
Mode=(MouseMode)NewMode;
Ret|=InitMode();
return(Ret);
2000-11-15 22:22:40 +01:00
}
/*****************************************************************************/
BOOL CLayerTile::InitMode()
{
2000-11-17 00:08:54 +01:00
switch(Mode)
{
case MouseModePaint:
break;
case MouseModeSelect:
break;
default:
break;
}
return(FALSE);
2000-11-15 22:22:40 +01:00
}
/*****************************************************************************/
BOOL CLayerTile::ExitMode()
{
2000-11-17 00:08:54 +01:00
switch(Mode)
{
case MouseModePaint:
break;
case MouseModeSelect:
break;
default:
break;
}
return(FALSE);
}
2000-11-15 22:22:40 +01:00
/*****************************************************************************/
BOOL CLayerTile::LButtonControl(CCore *Core,CMapEditView *View,UINT nFlags, CPoint &CursorPos,BOOL DownFlag)
{
BOOL Ret=FALSE;
CTileBank &TileBank=Core->GetTileBank();
2000-11-17 00:08:54 +01:00
switch(Mode)
{
case MouseModePaint:
if (DownFlag)
2000-11-17 22:36:13 +01:00
Ret=Paint(TileBank.GetLBrush(),CursorPos);
2000-11-17 00:08:54 +01:00
break;
case MouseModeSelect:
break;
default:
break;
}
return(Ret);
2000-11-15 22:22:40 +01:00
}
/*****************************************************************************/
BOOL CLayerTile::RButtonControl(CCore *Core,CMapEditView *View,UINT nFlags, CPoint &CursorPos,BOOL DownFlag)
{
BOOL Ret=FALSE;
CTileBank &TileBank=Core->GetTileBank();
2000-11-17 00:08:54 +01:00
switch(Mode)
{
case MouseModePaint:
if (DownFlag)
2000-11-17 22:36:13 +01:00
Ret=Paint(TileBank.GetRBrush(),CursorPos);
2000-11-17 00:08:54 +01:00
break;
case MouseModeSelect:
break;
default:
break;
}
return(Ret);
2000-11-15 22:22:40 +01:00
}
/*****************************************************************************/
BOOL CLayerTile::MouseMove(CCore *Core,CMapEditView *View,UINT nFlags, CPoint &CursorPos)
{
BOOL Ret=FALSE;
CTileBank &TileBank=Core->GetTileBank();
2000-11-17 00:08:54 +01:00
switch(Mode)
{
case MouseModePaint:
if (nFlags & MK_LBUTTON)
2000-11-17 22:36:13 +01:00
Ret=Paint(TileBank.GetLBrush(),CursorPos);
2000-11-17 00:08:54 +01:00
else
if (nFlags & MK_RBUTTON)
2000-11-17 22:36:13 +01:00
Ret=Paint(TileBank.GetRBrush(),CursorPos);
2000-11-17 00:08:54 +01:00
break;
case MouseModeSelect:
break;
default:
break;
}
return(Ret);
}
/*****************************************************************************/
2000-12-04 17:47:34 +01:00
void CLayerTile::RenderCursor(CCore *Core,Vector3 &CamPos,BOOL Is3d)
2000-11-17 00:08:54 +01:00
{
2000-11-17 22:36:13 +01:00
switch(Mode)
{
case MouseModePaint:
RenderCursorPaint(Core,CamPos,Is3d);
break;
case MouseModeSelect:
break;
default:
break;
}
}
/*****************************************************************************/
BOOL CLayerTile::MirrorX(CCore *Core)
{
CTileBank &TileBank=Core->GetTileBank();
CMap &LBrush=TileBank.GetLBrush();
CMap &RBrush=TileBank.GetRBrush();
2000-12-06 20:29:40 +01:00
LBrush.MirrorX(TILE_FLAG_MIRROR_X);
RBrush.MirrorX(TILE_FLAG_MIRROR_X);
2000-11-17 22:36:13 +01:00
2000-11-17 00:08:54 +01:00
return(TRUE);
}
/*****************************************************************************/
2000-11-17 22:36:13 +01:00
BOOL CLayerTile::MirrorY(CCore *Core)
2000-11-17 00:08:54 +01:00
{
2000-11-17 22:36:13 +01:00
CTileBank &TileBank=Core->GetTileBank();
CMap &LBrush=TileBank.GetLBrush();
CMap &RBrush=TileBank.GetRBrush();
2000-12-06 20:29:40 +01:00
LBrush.MirrorY(TILE_FLAG_MIRROR_Y);
RBrush.MirrorY(TILE_FLAG_MIRROR_Y);
2000-11-17 22:36:13 +01:00
2000-11-17 00:08:54 +01:00
return(TRUE);
2000-11-15 22:22:40 +01:00
}
/*****************************************************************************/
2000-11-17 00:08:54 +01:00
/*****************************************************************************/
/*****************************************************************************/
2000-11-17 22:36:13 +01:00
BOOL CLayerTile::Paint(CMap &Blk,CPoint &CursorPos)
2000-11-15 22:22:40 +01:00
{
if (CursorPos.y==-1 || CursorPos.y==-1) return(FALSE); // Off Map?
2000-11-17 22:36:13 +01:00
if (!Blk.IsValid()) return(FALSE); // Invalid tile?
Map.Set(CursorPos.x,CursorPos.y,Blk);
2000-11-15 22:22:40 +01:00
return(TRUE);
}
2000-11-24 23:34:20 +01:00
/*****************************************************************************/
2000-11-29 18:07:57 +01:00
void CLayerTile::Export(CCore *Core,CExport &Exp)
2000-11-24 23:34:20 +01:00
{
2000-11-30 23:17:55 +01:00
Exp.ExportLayerTile(Core,GetName(),SubType,Map);
2000-11-24 23:34:20 +01:00
}
2000-12-11 22:29:59 +01:00
/*****************************************************************************/
void CLayerTile::DeleteSet(int Set)
{
Map.DeleteSet(Set);
}
/*****************************************************************************/
void CLayerTile::RemapSet(int OrigSet,int NewSet)
{
Map.RemapSet(OrigSet,NewSet);
}