SBSPSS/Utils/MapEdit/LayerThing.cpp

453 lines
11 KiB
C++
Raw Normal View History

2001-03-22 21:49:58 +01:00
/*******************/
/*** Layer Thing ***/
/*******************/
2001-03-21 00:06:17 +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"
#include "Core.h"
#include "Layer.h"
2001-03-22 21:49:58 +01:00
#include "LayerThing.h"
2001-03-21 00:06:17 +01:00
#include "Utils.h"
#include "Export.h"
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
// New Layer
2001-03-26 23:29:09 +02:00
CLayerThing::CLayerThing(sLayerDef &Def)
2001-03-21 00:06:17 +01:00
{
2001-03-26 23:29:09 +02:00
InitLayer(Def);
2001-03-21 00:06:17 +01:00
}
/*****************************************************************************/
// Load Layer
2001-03-26 23:29:09 +02:00
//CLayerThing::CLayerThing(CFile *File,int Version)
//{
//Load(File,Version);
//}
2001-03-21 00:06:17 +01:00
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
CLayerThing::~CLayerThing()
2001-03-21 00:06:17 +01:00
{
2001-03-22 21:49:58 +01:00
ThingBank->CleanUp();
delete ThingBank;
2001-03-21 00:06:17 +01:00
}
2001-03-26 23:29:09 +02:00
/*****************************************************************************/
void CLayerThing::InitLayer(sLayerDef &Def)
{
Mode=MouseModeNormal;
LayerDef.Width=Width;
LayerDef.Height=Height;
ThingBank=new CElemBank(-1,-1,false,true);
CurrentThing=-1;
CurrentPoint=0;
}
2001-03-21 00:06:17 +01:00
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
void CLayerThing::Load(CFile *File,int Version)
2001-03-21 00:06:17 +01:00
{
File->Read(&Mode,sizeof(MouseMode));
}
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
void CLayerThing::Save(CFile *File)
2001-03-21 00:06:17 +01:00
{
// Always Save current version
File->Write(&Mode,sizeof(MouseMode));
}
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
void CLayerThing::InitSubView(CCore *Core)
2001-03-21 00:06:17 +01:00
{
}
2001-03-26 23:29:09 +02:00
/*****************************************************************************/
void CLayerThing::LoadThingScript(const char *Filename)
{
ThingScript.LoadAndImport(Filename);
}
2001-03-21 00:06:17 +01:00
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
void CLayerThing::Render(CCore *Core,Vector3 &CamPos,bool Is3d)
2001-03-21 00:06:17 +01:00
{
Vector3 ThisCam=Core->OffsetCam(CamPos,GetScaleFactor());
2001-03-22 21:49:58 +01:00
int i,ListSize=ThingList.size();
2001-03-21 00:06:17 +01:00
2001-03-22 21:49:58 +01:00
// Is3d&=Render3dFlag;
for (i=0; i<ListSize; i++)
2001-03-21 00:06:17 +01:00
{
2001-03-22 21:49:58 +01:00
RenderThing(Core,ThisCam,ThingList[i],Is3d,i==CurrentThing);
2001-03-21 00:06:17 +01:00
}
}
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
void CLayerThing::RenderThing(CCore *Core,Vector3 &ThisCam,sLayerThing &ThisThing,bool Render3d,bool Selected)
2001-03-21 00:06:17 +01:00
{
float ZoomW=Core->GetZoomW();
float ZoomH=Core->GetZoomH();
float ScrOfsX=(ZoomW/2);
float ScrOfsY=(ZoomH/2);
Vector3 &Scale=Core->GetScaleVector();
2001-03-22 21:49:58 +01:00
CElemBank *IconBank=Core->GetIconBank();
2001-03-21 00:06:17 +01:00
2001-03-22 21:49:58 +01:00
if (ThingBank->NeedLoad()) ThingBank->LoadAllSets(Core);
2001-03-21 00:06:17 +01:00
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glScalef(Scale.x,Scale.y,Scale.z);
2001-03-22 21:49:58 +01:00
glTranslatef(-ThisCam.x,ThisCam.y,0); // Set scroll offset
glTranslatef(-ScrOfsX,ScrOfsY,0); // Bring to top left corner
int ListSize=ThisThing.XY.size();
for (int i=0;i<ListSize; i++)
{
// Render Thing
glPushMatrix();
glTranslatef(ThisThing.XY[i].x,-ThisThing.XY[i].y,0); // Set Pos
if (Selected)
glColor4f(1,1,1,1); // Set default Color
else
glColor4f(1,1,1,0.5);
IconBank->RenderElem(0,i,0,Render3d);
if (i==0)
{
glColor4f(1,1,1,1); // Set default Color
ThingBank->RenderElem(ThisThing.Type,0,0,Render3d);
}
glPopMatrix();
}
2001-03-21 00:06:17 +01:00
glPopMatrix();
}
/*****************************************************************************/
/*** Gui *********************************************************************/
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
void CLayerThing::GUIInit(CCore *Core)
2001-03-21 00:06:17 +01:00
{
// Core->GUIAdd(GUIToolBar,IDD_TOOLBAR);
}
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
void CLayerThing::GUIKill(CCore *Core)
2001-03-21 00:06:17 +01:00
{
// Core->GUIRemove(GUIToolBar,IDD_TOOLBAR);
}
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
void CLayerThing::GUIUpdate(CCore *Core)
2001-03-21 00:06:17 +01:00
{
}
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
void CLayerThing::GUIChanged(CCore *Core)
2001-03-21 00:06:17 +01:00
{
}
/*****************************************************************************/
/*** Functions ***************************************************************/
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
bool CLayerThing::LButtonControl(CCore *Core,UINT nFlags, CPoint &CursorPos,bool DownFlag)
2001-03-21 00:06:17 +01:00
{
bool Ret=false;
2001-03-22 21:49:58 +01:00
2001-03-21 00:06:17 +01:00
switch(Mode)
{
2001-03-22 21:49:58 +01:00
case MouseModeNormal:
2001-03-21 00:06:17 +01:00
if (DownFlag)
2001-03-22 21:49:58 +01:00
{
if (CurrentThing==-1)
AddThing(CursorPos);
else
AddThingPoint(CursorPos);
}
2001-03-21 00:06:17 +01:00
break;
2001-03-22 21:49:58 +01:00
case MouseModePoints:
2001-03-21 00:06:17 +01:00
break;
default:
break;
}
2001-03-22 21:49:58 +01:00
return(true);
2001-03-21 00:06:17 +01:00
}
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
bool CLayerThing::RButtonControl(CCore *Core,UINT nFlags, CPoint &CursorPos,bool DownFlag)
2001-03-21 00:06:17 +01:00
{
bool Ret=FALSE;
2001-03-22 21:49:58 +01:00
2001-03-21 00:06:17 +01:00
switch(Mode)
{
2001-03-22 21:49:58 +01:00
case MouseModeNormal:
2001-03-21 00:06:17 +01:00
if (DownFlag)
2001-03-22 21:49:58 +01:00
{
SelectThing(CursorPos);
}
2001-03-21 00:06:17 +01:00
break;
2001-03-22 21:49:58 +01:00
case MouseModePoints:
2001-03-21 00:06:17 +01:00
break;
}
2001-03-22 21:49:58 +01:00
2001-03-21 00:06:17 +01:00
return(Ret);
}
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
bool CLayerThing::MouseMove(CCore *Core,UINT nFlags, CPoint &CursorPos)
2001-03-21 00:06:17 +01:00
{
2001-03-22 21:49:58 +01:00
bool Ret=false;
if (CurrentThing!=-1)
2001-03-21 00:06:17 +01:00
{
2001-03-22 21:49:58 +01:00
if (nFlags & MK_LBUTTON) // Drag
{
UpdatePos(CursorPos,CurrentThing,CurrentPoint,true);
Ret=true;
}
2001-03-21 00:06:17 +01:00
else
2001-03-22 21:49:58 +01:00
if (nFlags & MK_RBUTTON) // Cancel
{
CurrentThing=-1;
Ret=true;
}
2001-03-21 00:06:17 +01:00
}
return(Ret);
}
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
bool CLayerThing::Command(int CmdMsg,CCore *Core,int Param0,int Param1)
2001-03-21 00:06:17 +01:00
{
bool Ret=false;
2001-03-22 21:49:58 +01:00
/*
2001-03-21 00:06:17 +01:00
switch(CmdMsg)
{
case CmdMsg_SetMode:
2001-03-22 21:49:58 +01:00
// Mode=(MouseMode)Param0;
// Core->GUIUpdate();
// break;
// case CmdMsg_SubViewSet:
// Ret=ThingBank->Command(CmdMsg,Core,Param0,Param1);
2001-03-21 00:06:17 +01:00
break;
default:
2001-03-22 21:49:58 +01:00
break;
2001-03-21 00:06:17 +01:00
}
2001-03-22 21:49:58 +01:00
*/
2001-03-21 00:06:17 +01:00
return(Ret);
}
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
void CLayerThing::RenderCursor(CCore *Core,Vector3 &CamPos,bool Is3d)
2001-03-21 00:06:17 +01:00
{
Vector3 ThisCam=Core->OffsetCam(CamPos,GetScaleFactor());
CPoint &CursPos=Core->GetCursorPos();
Vector3 Ofs;
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;
2001-03-26 23:29:09 +02:00
Is3d&=GetRender3dFlag();
if (Is3d)
2001-03-21 00:06:17 +01:00
{
glEnable(GL_DEPTH_TEST);
// Render(Core,ThisCam,,TRUE,0.5,&Ofs);
glDisable(GL_DEPTH_TEST);
}
else
{
// Render(Core,ThisCam,Brush,FALSE,0.5,&Ofs);
}
}
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
/*****************************************************************************/
/*****************************************************************************/
int CLayerThing::CheckThing(CPoint &Pos)
2001-03-21 00:06:17 +01:00
{
2001-03-22 21:49:58 +01:00
CList<int> List;
int Idx=-1,i,ListSize=ThingList.size();
int StartIdx=0;
2001-03-21 00:06:17 +01:00
2001-03-22 21:49:58 +01:00
// Build List Of XY Matches
for (i=0; i<ListSize; i++)
{
sLayerThing &ThisThing=ThingList[i];
if (ThisThing.XY[0]==Pos)
{
if (i==CurrentThing) StartIdx=List.size();
List.push_back(i);
}
}
ListSize=List.size();
if (ListSize)
{
StartIdx=(StartIdx+1)%ListSize;
Idx=List[StartIdx];
}
return(Idx);
2001-03-21 00:06:17 +01:00
}
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
void CLayerThing::AddThing(CPoint &Pos)
{
if (Pos.x==-1 || Pos.y==-1) return; // Off Map?
CurrentThing=CheckThing(Pos);
CurrentPoint=0;
if (CurrentThing!=-1) return;
CurrentThing=ThingList.size();
ThingList.resize(CurrentThing+1);
sLayerThing &ThisThing=ThingList[CurrentThing];
ThisThing.XY.push_back(Pos);
ThisThing.Type=ThingList.size()%22;
ThisThing.SubType=0;
}
/*****************************************************************************/
void CLayerThing::SelectThing(CPoint &Pos)
{
if (Pos.x==-1 || Pos.y==-1) return; // Off Map?
CurrentThing=CheckThing(Pos);
CurrentPoint=0;
}
2001-03-26 23:29:09 +02:00
/*****************************************************************************/
2001-03-22 21:49:58 +01:00
/*****************************************************************************/
/*****************************************************************************/
int CLayerThing::CheckThingPoint(CPoint &Pos)
{
CList<int> List;
sLayerThing &ThisThing=ThingList[CurrentThing];
int Idx=-1,i,ListSize=ThisThing.XY.size();
int StartIdx=0;
// Build List Of XY Matches
for (i=0; i<ListSize; i++)
{
if (ThisThing.XY[i]==Pos)
{
if (i==CurrentThing) StartIdx=List.size();
List.push_back(i);
}
}
ListSize=List.size();
if (ListSize)
{
StartIdx=(StartIdx+1)%ListSize;
Idx=List[StartIdx];
}
return(Idx);
}
/*****************************************************************************/
void CLayerThing::AddThingPoint(CPoint &Pos)
{
if (Pos.x==-1 || Pos.y==-1) return; // Off Map?
CurrentPoint=CheckThingPoint(Pos);
if (CurrentPoint!=-1) return;
sLayerThing &ThisThing=ThingList[CurrentThing];
CurrentPoint=ThisThing.XY.size();
ThisThing.XY.resize(CurrentPoint+1);
ThisThing.XY[CurrentPoint]=Pos;
}
/*****************************************************************************/
void CLayerThing::SelectThingPoint(CPoint &Pos)
{
if (Pos.x==-1 || Pos.y==-1) return; // Off Map?
CurrentPoint=CheckThing(Pos);
}
/*****************************************************************************/
void CLayerThing::UpdatePos(CPoint &Pos,int Thing,int PosIdx,bool Recurs)
{
if (Pos.x==-1 || Pos.y==-1) return; // Off Map?
sLayerThing &ThisThing=ThingList[Thing];
CPoint dPos=Pos-ThisThing.XY[PosIdx];
int StartIdx=PosIdx,EndIdx=ThisThing.XY.size();
if (!Recurs)
{
StartIdx=PosIdx;
EndIdx=StartIdx++;
}
for (int i=StartIdx; i<EndIdx; i++)
{
ThisThing.XY[i]+=dPos;
}
}
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
void CLayerThing::Export(CCore *Core,CExport &Exp)
2001-03-21 00:06:17 +01:00
{
/*
int Width=Map.GetWidth();
int Height=Map.GetHeight();
Exp.ExportLayerHeader(LAYER_TYPE_Elem,SubType,Width,Height);
for (int Y=0; Y<Height; Y++)
{
for (int X=0; X<Width; X++)
{
sMapElem &MapElem=Map.Get(X,Y);
2001-03-22 21:49:58 +01:00
sExpLayerThing OutElem;
2001-03-21 00:06:17 +01:00
if (MapElem.Set==0 && MapElem.Elem==0)
{ // Blank
OutElem.Elem=0;
OutElem.Flags=0;
}
else
{
sExpElem OutElem;
CElem &ThisElem=ElemBank->GetElem(MapElem.Set,MapElem.Elem);
OutElem.Set=MapElem.Set;
OutElem.Elem=MapElem.Elem;
OutElem.TriStart=0;
OutElem.TriCount=0;
OutElem.XOfs=ThisElem.GetTexXOfs();
OutElem.YOfs=ThisElem.GetTexYOfs();
OutElem.Elem=Exp.AddElem(OutElem);
OutElem.Flags=MapElem.Flags;
}
2001-03-22 21:49:58 +01:00
Exp.Write(&OutElem,sizeof(sExpLayerThing));
2001-03-21 00:06:17 +01:00
}
}
*/
}