SBSPSS/Utils/MapEdit/LayerItem.cpp

386 lines
9.2 KiB
C++
Raw Normal View History

2001-03-13 15:37:04 +01:00
/******************/
2001-03-13 22:00:34 +01:00
/*** Layer Item ***/
2001-03-13 15:37:04 +01:00
/******************/
#include "stdafx.h"
#include <Vector3.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include "GLEnabledView.h"
#include "MapEdit.h"
#include "MapEditDoc.h"
#include "MapEditView.h"
#include "MainFrm.h"
2001-03-13 22:00:34 +01:00
//#include "ItemSet.h"
2001-03-13 15:37:04 +01:00
#include "Core.h"
#include "Layer.h"
2001-03-13 22:00:34 +01:00
#include "LayerItem.h"
2001-03-13 15:37:04 +01:00
#include "Utils.h"
2001-03-13 22:00:34 +01:00
//#include "Select.h"
2001-03-13 15:37:04 +01:00
#include "Export.h"
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
// New Layer
2001-03-13 22:00:34 +01:00
CLayerItem::CLayerItem(int _SubType,int _Width,int _Height)
2001-03-13 15:37:04 +01:00
{
SetDefaultParams();
Mode=MouseModePaint;
}
/*****************************************************************************/
// Load Layer
2001-03-13 22:00:34 +01:00
CLayerItem::CLayerItem(CFile *File,int Version)
2001-03-13 15:37:04 +01:00
{
Load(File,Version);
}
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
CLayerItem::~CLayerItem()
2001-03-13 15:37:04 +01:00
{
2001-03-13 22:00:34 +01:00
ElemBank->CleanUp();
delete ElemBank;
2001-03-13 15:37:04 +01:00
}
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
void CLayerItem::Load(CFile *File,int Version)
2001-03-13 15:37:04 +01:00
{
File->Read(&VisibleFlag,sizeof(BOOL));
File->Read(&Mode,sizeof(MouseMode));
2001-03-13 22:00:34 +01:00
2001-03-13 15:37:04 +01:00
TRACE1("%s\t",GetName());
}
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
void CLayerItem::Save(CFile *File)
2001-03-13 15:37:04 +01:00
{
// Always Save current version
File->Write(&VisibleFlag,sizeof(BOOL));
File->Write(&Mode,sizeof(MouseMode));
}
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
void CLayerItem::InitSubView(CCore *Core)
2001-03-13 15:37:04 +01:00
{
2001-03-13 22:00:34 +01:00
ElemBank=new CElemStore;
SubView=ElemBank;
2001-03-13 15:37:04 +01:00
}
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
void CLayerItem::Render(CCore *Core,Vector3 &CamPos,bool Is3d)
2001-03-13 15:37:04 +01:00
{
2001-03-13 22:00:34 +01:00
/*
2001-03-13 15:37:04 +01:00
Vector3 ThisCam=Core->OffsetCam(CamPos,GetScaleFactor());
Is3d&=Render3dFlag;
Render(Core,ThisCam,Map,Is3d);
2001-03-13 22:00:34 +01:00
*/
2001-03-13 15:37:04 +01:00
}
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
void CLayerItem::RenderCursorPaint(CCore *Core,Vector3 &CamPos,bool Is3d)
2001-03-13 15:37:04 +01:00
{
2001-03-13 22:00:34 +01:00
/*
2001-03-13 15:37:04 +01:00
Vector3 ThisCam=Core->OffsetCam(CamPos,GetScaleFactor());
CPoint &CursPos=Core->GetCursorPos();
2001-03-13 22:00:34 +01:00
CMap &Brush=ElemBank->GetActiveBrush();
2001-03-13 15:37:04 +01:00
Vector3 Ofs;
if (!Brush.IsValid()) return;
if (CursPos.x<0 || CursPos.y<0) return;
Ofs.x=-(CursPos.x-(int)ThisCam.x);
Ofs.y=-(CursPos.y-(int)ThisCam.y);
ThisCam.x-=(int)ThisCam.x;
ThisCam.y-=(int)ThisCam.y;
if (Is3d && Render3dFlag)
{
glEnable(GL_DEPTH_TEST);
Render(Core,ThisCam,Brush,TRUE,0.5,&Ofs);
glDisable(GL_DEPTH_TEST);
}
else
{
Render(Core,ThisCam,Brush,FALSE,0.5,&Ofs);
}
2001-03-13 22:00:34 +01:00
*/
2001-03-13 15:37:04 +01:00
}
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
void CLayerItem::Render(CCore *Core,Vector3 &ThisCam,CMap &ThisMap,bool Render3d,float Alpha,Vector3 *Ofs)
2001-03-13 15:37:04 +01:00
{
2001-03-13 22:00:34 +01:00
/*
2001-03-13 15:37:04 +01:00
int MapWidth=ThisMap.GetWidth();
int MapHeight=ThisMap.GetHeight();
float ZoomW=Core->GetZoomW();
float ZoomH=Core->GetZoomH();
float ScrOfsX=(ZoomW/2);
float ScrOfsY=(ZoomH/2);
Vector3 &Scale=Core->GetScaleVector();
bool WrapMap=SubType==LAYER_SUBTYPE_BACK;
int StartX=(int)ThisCam.x;
int StartY=(int)ThisCam.y;
float ShiftX=ThisCam.x - (int)ThisCam.x;
float ShiftY=ThisCam.y - (int)ThisCam.y;
2001-03-13 22:00:34 +01:00
if (ElemBank->NeedLoad()) ElemBank->LoadAllSets(Core);
2001-03-13 15:37:04 +01:00
if (StartX<0) StartX=0;
if (StartY<0) StartY=0;
int DrawW=ZoomW+8;
int DrawH=ZoomH+8;
if (StartX+DrawW>MapWidth) DrawW=MapWidth-StartX;
if (StartY+DrawH>MapHeight) DrawH=MapHeight-StartY;
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glScalef(Scale.x,Scale.y,Scale.z);
glTranslatef(-ShiftX,ShiftY,0); // Set scroll offset
glTranslatef(-ScrOfsX,ScrOfsY,0); // Bring to top left corner
if (Ofs)
{
glTranslatef(-Ofs->x,Ofs->y,0); // Set scroll offset
}
if (WrapMap)
{
DrawW=MapWidth;
DrawH=MapHeight;
}
for (int YLoop=0; YLoop<DrawH; YLoop++)
{
for (int XLoop=0; XLoop<DrawW; XLoop++)
{
int XPos=StartX+XLoop;
int YPos=StartY+YLoop;
if (WrapMap)
{
XPos%=MapWidth;
YPos%=MapHeight;
}
sMapElem &ThisElem=ThisMap.Get(XPos,YPos);
2001-03-13 22:00:34 +01:00
if (ThisElem.Item>0)
{ // Render Non Zero and Valid Items
2001-03-13 15:37:04 +01:00
glColor4f(1,1,1,Alpha); // Set default Color
2001-03-13 22:00:34 +01:00
ElemBank->RenderItem(ThisElem.Set,ThisElem.Item,ThisElem.Flags,Render3d);
2001-03-13 15:37:04 +01:00
}
glTranslatef(1.0f,0,0); // Next X
}
glTranslatef(-DrawW,-1,0); // Next y, rewind to start X
}
glPopMatrix();
2001-03-13 22:00:34 +01:00
*/
2001-03-13 15:37:04 +01:00
}
/*****************************************************************************/
/*** Gui *********************************************************************/
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
void CLayerItem::GUIInit(CCore *Core)
2001-03-13 15:37:04 +01:00
{
2001-03-13 22:00:34 +01:00
ElemBank->GUIInit(Core);
Core->GUIAdd(GUIToolBar,IDD_LAYERTILE_TOOLBAR);
2001-03-13 15:37:04 +01:00
}
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
void CLayerItem::GUIKill(CCore *Core)
2001-03-13 15:37:04 +01:00
{
2001-03-13 22:00:34 +01:00
ElemBank->GUIKill(Core);
Core->GUIRemove(GUIToolBar,IDD_LAYERTILE_TOOLBAR);
2001-03-13 15:37:04 +01:00
}
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
void CLayerItem::GUIUpdate(CCore *Core)
2001-03-13 15:37:04 +01:00
{
2001-03-13 22:00:34 +01:00
GUIToolBar.ResetButtons();
2001-03-13 15:37:04 +01:00
switch(Mode)
{
case MouseModePaint:
2001-03-13 22:00:34 +01:00
GUIToolBar.SetButtonState(CGUIToolBar::PAINT,true);
2001-03-13 15:37:04 +01:00
break;
case MouseModeSelect:
2001-03-13 22:00:34 +01:00
GUIToolBar.SetButtonState(CGUIToolBar::SELECT,true);
2001-03-13 15:37:04 +01:00
break;
default:
break;
}
2001-03-13 22:00:34 +01:00
ElemBank->GUIUpdate(Core);
2001-03-13 15:37:04 +01:00
}
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
void CLayerItem::GUIChanged(CCore *Core)
2001-03-13 15:37:04 +01:00
{
}
/*****************************************************************************/
/*** Functions ***************************************************************/
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
bool CLayerItem::LButtonControl(CCore *Core,UINT nFlags, CPoint &CursorPos,bool DownFlag)
2001-03-13 15:37:04 +01:00
{
bool Ret=false;
2001-03-13 22:00:34 +01:00
/*
2001-03-13 15:37:04 +01:00
switch(Mode)
{
case MouseModePaint:
if (DownFlag)
2001-03-13 22:00:34 +01:00
Ret=Paint(ElemBank->GetLBrush(),CursorPos);
2001-03-13 15:37:04 +01:00
break;
case MouseModeSelect:
Ret=Selection.Handle(CursorPos,nFlags);
if (Selection.HasSelection())
{
TRACE0("LMB Selection\n");
}
break;
default:
break;
}
2001-03-13 22:00:34 +01:00
*/
2001-03-13 15:37:04 +01:00
return(Ret);
}
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
bool CLayerItem::RButtonControl(CCore *Core,UINT nFlags, CPoint &CursorPos,bool DownFlag)
2001-03-13 15:37:04 +01:00
{
bool Ret=FALSE;
2001-03-13 22:00:34 +01:00
/*
2001-03-13 15:37:04 +01:00
switch(Mode)
{
case MouseModePaint:
if (DownFlag)
2001-03-13 22:00:34 +01:00
Ret=Paint(ElemBank->GetRBrush(),CursorPos);
2001-03-13 15:37:04 +01:00
break;
case MouseModeSelect:
Ret=Selection.Handle(CursorPos,nFlags);
if (Selection.HasSelection())
{
TRACE0("RMB Selection\n");
}
break;
default:
break;
}
2001-03-13 22:00:34 +01:00
*/
2001-03-13 15:37:04 +01:00
return(Ret);
}
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
bool CLayerItem::MouseMove(CCore *Core,UINT nFlags, CPoint &CursorPos)
2001-03-13 15:37:04 +01:00
{
bool Ret=FALSE;
2001-03-13 22:00:34 +01:00
/*
2001-03-13 15:37:04 +01:00
switch(Mode)
{
case MouseModePaint:
if (nFlags & MK_LBUTTON)
2001-03-13 22:00:34 +01:00
Ret=Paint(ElemBank->GetLBrush(),CursorPos);
2001-03-13 15:37:04 +01:00
else
if (nFlags & MK_RBUTTON)
2001-03-13 22:00:34 +01:00
Ret=Paint(ElemBank->GetRBrush(),CursorPos);
2001-03-13 15:37:04 +01:00
break;
case MouseModeSelect:
Ret=Selection.Handle(CursorPos,nFlags);
break;
default:
break;
}
2001-03-13 22:00:34 +01:00
*/
2001-03-13 15:37:04 +01:00
return(Ret);
}
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
bool CLayerItem::Command(int CmdMsg,CCore *Core,int Param0,int Param1)
2001-03-13 15:37:04 +01:00
{
bool Ret=false;
2001-03-13 22:00:34 +01:00
2001-03-13 15:37:04 +01:00
switch(CmdMsg)
{
case CmdMsg_SetMode:
Mode=(MouseMode)Param0;
Core->GUIUpdate();
break;
case CmdMsg_SubViewSet:
2001-03-13 22:00:34 +01:00
Ret=ElemBank->Command(CmdMsg,Core,Param0,Param1);
2001-03-13 15:37:04 +01:00
break;
default:
2001-03-13 22:00:34 +01:00
TRACE3("LayerItem-Unhandled Command %i (%i,%i)\n",CmdMsg,Param0,Param1);
2001-03-13 15:37:04 +01:00
}
2001-03-13 22:00:34 +01:00
return(Ret);
2001-03-13 15:37:04 +01:00
}
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
void CLayerItem::RenderCursor(CCore *Core,Vector3 &CamPos,bool Is3d)
2001-03-13 15:37:04 +01:00
{
2001-03-13 22:00:34 +01:00
RenderCursorPaint(Core,CamPos,Is3d);
2001-03-13 15:37:04 +01:00
}
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
bool CLayerItem::Paint(CMap &Blk,CPoint &CursorPos)
2001-03-13 15:37:04 +01:00
{
2001-03-13 22:00:34 +01:00
if (CursorPos.x==-1 || CursorPos.y==-1) return(false); // Off Map?
if (!Blk.IsValid()) return(false); // Invalid Item?
/*
2001-03-13 15:37:04 +01:00
Map.Set(CursorPos.x,CursorPos.y,Blk);
2001-03-13 22:00:34 +01:00
*/
return(true);
2001-03-13 15:37:04 +01:00
}
/*****************************************************************************/
2001-03-13 22:00:34 +01:00
void CLayerItem::Export(CCore *Core,CExport &Exp)
2001-03-13 15:37:04 +01:00
{
2001-03-13 22:00:34 +01:00
/*
2001-03-13 15:37:04 +01:00
int Width=Map.GetWidth();
int Height=Map.GetHeight();
2001-03-13 22:00:34 +01:00
Exp.ExportLayerHeader(LAYER_TYPE_Item,SubType,Width,Height);
2001-03-13 15:37:04 +01:00
for (int Y=0; Y<Height; Y++)
{
for (int X=0; X<Width; X++)
{
sMapElem &MapElem=Map.Get(X,Y);
2001-03-13 22:00:34 +01:00
sExpLayerItem OutElem;
2001-03-13 15:37:04 +01:00
2001-03-13 22:00:34 +01:00
if (MapElem.Set==0 && MapElem.Item==0)
2001-03-13 15:37:04 +01:00
{ // Blank
2001-03-13 22:00:34 +01:00
OutElem.Item=0;
2001-03-13 15:37:04 +01:00
OutElem.Flags=0;
}
else
{
2001-03-13 22:00:34 +01:00
sExpItem OutItem;
CElem &ThisItem=ElemBank->GetItem(MapElem.Set,MapElem.Item);
OutItem.Set=MapElem.Set;
OutItem.Item=MapElem.Item;
OutItem.TriStart=0;
OutItem.TriCount=0;
OutItem.XOfs=ThisItem.GetTexXOfs();
OutItem.YOfs=ThisItem.GetTexYOfs();
OutElem.Item=Exp.AddItem(OutItem);
2001-03-13 15:37:04 +01:00
OutElem.Flags=MapElem.Flags;
}
2001-03-13 22:00:34 +01:00
Exp.Write(&OutElem,sizeof(sExpLayerItem));
2001-03-13 15:37:04 +01:00
}
}
2001-03-13 22:00:34 +01:00
*/
2001-03-13 15:37:04 +01:00
}