⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 undo.cpp

📁 quake3工具源码。包括生成bsp文件
💻 CPP
📖 第 1 页 / 共 2 页
字号:

/*

  QERadiant Undo/Redo


basic setup:

<-g_undolist---------g_lastundo> <---map data---> <-g_lastredo---------g_redolist->


  undo/redo on the world_entity is special, only the epair changes are remembered
  and the world entity never gets deleted.

  FIXME: maybe reset the Undo system at map load
		 maybe also reset the entityId at map load
*/

#include "stdafx.h"
#include "Radiant.h"
#include "qe3.h"

typedef struct undo_s
{
	double time;				//time operation was performed
	int id;						//every undo has an unique id
	int done;					//true when undo is build
	char *operation;			//name of the operation
	brush_t brushlist;			//deleted brushes
	entity_t entitylist;		//deleted entities
	struct undo_s *prev, *next;	//next and prev undo in list
} undo_t;

undo_t *g_undolist;						//first undo in the list
undo_t *g_lastundo;						//last undo in the list
undo_t *g_redolist;						//first redo in the list
undo_t *g_lastredo;						//last undo in list
int g_undoMaxSize = 64;					//maximum number of undos
int g_undoSize = 0;						//number of undos in the list
int g_undoMaxMemorySize = 2*1024*1024;	//maximum undo memory (default 2 MB)
int g_undoMemorySize = 0;				//memory size of undo buffer
int g_undoId = 1;						//current undo ID (zero is invalid id)
int g_redoId = 1;						//current redo ID (zero is invalid id)


/*
=============
Undo_MemorySize
=============
*/
int Undo_MemorySize(void)
{
	/*
	int size;
	undo_t *undo;
	brush_t *pBrush;
	entity_t *pEntity;

	size = 0;
	for (undo = g_undolist; undo; undo = undo->next)
	{
		for (pBrush = undo->brushlist.next ; pBrush != NULL && pBrush != &undo->brushlist ; pBrush = pBrush->next)
		{
			size += Brush_MemorySize(pBrush);
		}
		for (pEntity = undo->entitylist.next; pEntity != NULL && pEntity != &undo->entitylist; pEntity = pEntity->next)
		{
			size += Entity_MemorySize(pEntity);
		}
		size += sizeof(undo_t);
	}
	return size;
	*/
	return g_undoMemorySize;
}

/*
=============
Undo_ClearRedo
=============
*/
void Undo_ClearRedo(void)
{
	undo_t *redo, *nextredo;
	brush_t *pBrush, *pNextBrush;
	entity_t *pEntity, *pNextEntity;

	for (redo = g_redolist; redo; redo = nextredo)
	{
		nextredo = redo->next;
		for (pBrush = redo->brushlist.next ; pBrush != NULL && pBrush != &redo->brushlist ; pBrush = pNextBrush)
		{
			pNextBrush = pBrush->next;
			Brush_Free(pBrush);
		}
		for (pEntity = redo->entitylist.next; pEntity != NULL && pEntity != &redo->entitylist; pEntity = pNextEntity)
		{
			pNextEntity = pEntity->next;
			Entity_Free(pEntity);
		}
		free(redo);
	}
	g_redolist = NULL;
	g_lastredo = NULL;
	g_redoId = 1;
}

/*
=============
Undo_Clear

  Clears the undo buffer.
=============
*/
void Undo_Clear(void)
{
	undo_t *undo, *nextundo;
	brush_t *pBrush, *pNextBrush;
	entity_t *pEntity, *pNextEntity;

	Undo_ClearRedo();
	for (undo = g_undolist; undo; undo = nextundo)
	{
		nextundo = undo->next;
		for (pBrush = undo->brushlist.next ; pBrush != NULL && pBrush != &undo->brushlist ; pBrush = pNextBrush)
		{
			pNextBrush = pBrush->next;
			g_undoMemorySize -= Brush_MemorySize(pBrush);
			Brush_Free(pBrush);
		}
		for (pEntity = undo->entitylist.next; pEntity != NULL && pEntity != &undo->entitylist; pEntity = pNextEntity)
		{
			pNextEntity = pEntity->next;
			g_undoMemorySize -= Entity_MemorySize(pEntity);
			Entity_Free(pEntity);
		}
		g_undoMemorySize -= sizeof(undo_t);
		free(undo);
	}
	g_undolist = NULL;
	g_lastundo = NULL;
	g_undoSize = 0;
	g_undoMemorySize = 0;
	g_undoId = 1;
}

/*
=============
Undo_SetMaxSize
=============
*/
void Undo_SetMaxSize(int size)
{
	Undo_Clear();
	if (size < 1) g_undoMaxSize = 1;
	else g_undoMaxSize = size;
}

/*
=============
Undo_GetMaxSize
=============
*/
int Undo_GetMaxSize(void)
{
	return g_undoMaxSize;
}

/*
=============
Undo_SetMaxMemorySize
=============
*/
void Undo_SetMaxMemorySize(int size)
{
	Undo_Clear();
	if (size < 1024) g_undoMaxMemorySize = 1024;
	else g_undoMaxMemorySize = size;
}

/*
=============
Undo_GetMaxMemorySize
=============
*/
int Undo_GetMaxMemorySize(void)
{
	return g_undoMaxMemorySize;
}

/*
=============
Undo_FreeFirstUndo
=============
*/
void Undo_FreeFirstUndo(void)
{
	undo_t *undo;
	brush_t *pBrush, *pNextBrush;
	entity_t *pEntity, *pNextEntity;

	//remove the oldest undo from the undo buffer
	undo = g_undolist;
	g_undolist = g_undolist->next;
	g_undolist->prev = NULL;
	//
	for (pBrush = undo->brushlist.next ; pBrush != NULL && pBrush != &undo->brushlist ; pBrush = pNextBrush)
	{
		pNextBrush = pBrush->next;
		g_undoMemorySize -= Brush_MemorySize(pBrush);
		Brush_Free(pBrush);
	}
	for (pEntity = undo->entitylist.next; pEntity != NULL && pEntity != &undo->entitylist; pEntity = pNextEntity)
	{
		pNextEntity = pEntity->next;
		g_undoMemorySize -= Entity_MemorySize(pEntity);
		Entity_Free(pEntity);
	}
	g_undoMemorySize -= sizeof(undo_t);
	free(undo);
	g_undoSize--;
}

/*
=============
Undo_GeneralStart
=============
*/
void Undo_GeneralStart(char *operation)
{
	undo_t *undo;
	brush_t *pBrush;
	entity_t *pEntity;


	if (g_lastundo)
	{
		if (!g_lastundo->done)
		{
			Sys_Printf("Undo_Start: WARNING last undo not finished.\n");
		}
	}

	undo = (undo_t *) malloc(sizeof(undo_t));
	if (!undo) return;
	memset(undo, 0, sizeof(undo_t));
	undo->brushlist.next = &undo->brushlist;
	undo->brushlist.prev = &undo->brushlist;
	undo->entitylist.next = &undo->entitylist;
	undo->entitylist.prev = &undo->entitylist;
	if (g_lastundo) g_lastundo->next = undo;
	else g_undolist = undo;
	undo->prev = g_lastundo;
	undo->next = NULL;
	g_lastundo = undo;
	
	undo->time = Sys_DoubleTime();
	//
	if (g_undoId > g_undoMaxSize * 2) g_undoId = 1;
	if (g_undoId <= 0) g_undoId = 1;
	undo->id = g_undoId++;
	undo->done = false;
	undo->operation = operation;
	//reset the undo IDs of all brushes using the new ID
	for (pBrush = active_brushes.next; pBrush != NULL && pBrush != &active_brushes; pBrush = pBrush->next)
	{
		if (pBrush->undoId == undo->id)
		{
			pBrush->undoId = 0;
		}
	}
	for (pBrush = selected_brushes.next; pBrush != NULL && pBrush != &selected_brushes; pBrush = pBrush->next)
	{
		if (pBrush->undoId == undo->id)
		{
			pBrush->undoId = 0;
		}
	}
	//reset the undo IDs of all entities using thew new ID
	for (pEntity = entities.next; pEntity != NULL && pEntity != &entities; pEntity = pEntity->next)
	{
		if (pEntity->undoId == undo->id)
		{
			pEntity->undoId = 0;
		}
	}
	g_undoMemorySize += sizeof(undo_t);
	g_undoSize++;
	//undo buffer is bound to a max
	if (g_undoSize > g_undoMaxSize)
	{
		Undo_FreeFirstUndo();
	}
}

/*
=============
Undo_BrushInUndo
=============
*/
int Undo_BrushInUndo(undo_t *undo, brush_t *brush)
{
	brush_t *b;

	for (b = undo->brushlist.next; b != &undo->brushlist; b = b->next)
	{
		if (b == brush) return true;
	}
	return false;
}

/*
=============
Undo_EntityInUndo
=============
*/
int Undo_EntityInUndo(undo_t *undo, entity_t *ent)
{
	entity_t *e;

	for (e = undo->entitylist.next; e != &undo->entitylist; e = e->next)
	{
		if (e == ent) return true;
	}
	return false;
}

/*
=============
Undo_Start
=============
*/
void Undo_Start(char *operation)
{
	Undo_ClearRedo();
	Undo_GeneralStart(operation);
}

/*
=============
Undo_AddBrush
=============
*/
void Undo_AddBrush(brush_t *pBrush)
{
	if (!g_lastundo)
	{
		Sys_Printf("Undo_AddBrushList: no last undo.\n");
		return;
	}
	if (g_lastundo->entitylist.next != &g_lastundo->entitylist)
	{
		Sys_Printf("Undo_AddBrushList: WARNING adding brushes after entity.\n");
	}
	//if the brush is already in the undo
	if (Undo_BrushInUndo(g_lastundo, pBrush))
		return;
	//clone the brush
	brush_t* pClone = Brush_FullClone(pBrush);
	//save the ID of the owner entity
	pClone->ownerId = pBrush->owner->entityId;
	//save the old undo ID for previous undos
	pClone->undoId = pBrush->undoId;
	Brush_AddToList (pClone, &g_lastundo->brushlist);
	//
	g_undoMemorySize += Brush_MemorySize(pClone);
}

/*
=============
Undo_AddBrushList
=============
*/
void Undo_AddBrushList(brush_t *brushlist)
{
	brush_t *pBrush;

	if (!g_lastundo)
	{
		Sys_Printf("Undo_AddBrushList: no last undo.\n");
		return;
	}
	if (g_lastundo->entitylist.next != &g_lastundo->entitylist)
	{
		Sys_Printf("Undo_AddBrushList: WARNING adding brushes after entity.\n");
	}
	//copy the brushes to the undo
	for (pBrush = brushlist->next ; pBrush != NULL && pBrush != brushlist; pBrush=pBrush->next)
	{
		//if the brush is already in the undo
		if (Undo_BrushInUndo(g_lastundo, pBrush))
			continue;
		// do we need to store this brush's entity in the undo?
		// if it's a fixed size entity, the brush that reprents it is not really relevant, it's used for selecting and moving around
		// what we want to store for undo is the owner entity, epairs and origin/angle stuff
		//++timo FIXME: if the entity is not fixed size I don't know, so I don't do it yet
		if (pBrush->owner->eclass->fixedsize == 1)
			Undo_AddEntity( pBrush->owner );
		//clone the brush
		brush_t* pClone = Brush_FullClone(pBrush);
		//save the ID of the owner entity
		pClone->ownerId = pBrush->owner->entityId;
		//save the old undo ID from previous undos
		pClone->undoId = pBrush->undoId;
		Brush_AddToList (pClone, &g_lastundo->brushlist);
		//
		g_undoMemorySize += Brush_MemorySize(pClone);
	}
}

/*
=============
Undo_EndBrush
=============
*/
void Undo_EndBrush(brush_t *pBrush)
{
	if (!g_lastundo)
	{
		//Sys_Printf("Undo_End: no last undo.\n");
		return;
	}
	if (g_lastundo->done)
	{
		//Sys_Printf("Undo_End: last undo already finished.\n");
		return;
	}
	pBrush->undoId = g_lastundo->id;
}

/*
=============

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -