📄 undo.cpp
字号:
Undo_EndBrushList
=============
*/
void Undo_EndBrushList(brush_t *brushlist)
{
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;
}
for (brush_t* pBrush = brushlist->next; pBrush != NULL && pBrush != brushlist; pBrush=pBrush->next)
{
pBrush->undoId = g_lastundo->id;
}
}
/*
=============
Undo_AddEntity
=============
*/
void Undo_AddEntity(entity_t *entity)
{
entity_t* pClone;
if (!g_lastundo)
{
Sys_Printf("Undo_AddEntity: no last undo.\n");
return;
}
//if the entity is already in the undo
if (Undo_EntityInUndo(g_lastundo, entity))
return;
//clone the entity
pClone = Entity_Clone(entity);
//NOTE: Entity_Clone adds the entity to the entity list
// so we remove it from that list here
Entity_RemoveFromList(pClone);
//save the old undo ID for previous undos
pClone->undoId = entity->undoId;
//save the entity ID (we need a full clone)
pClone->entityId = entity->entityId;
//
Entity_AddToList(pClone, &g_lastundo->entitylist);
//
g_undoMemorySize += Entity_MemorySize(pClone);
}
/*
=============
Undo_EndEntity
=============
*/
void Undo_EndEntity(entity_t *entity)
{
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;
}
if (entity == world_entity)
{
//Sys_Printf("Undo_AddEntity: undo on world entity.\n");
//NOTE: we never delete the world entity when undoing an operation
// we only transfer the epairs
return;
}
entity->undoId = g_lastundo->id;
}
/*
=============
Undo_End
=============
*/
void Undo_End(void)
{
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;
}
g_lastundo->done = true;
//undo memory size is bound to a max
while (g_undoMemorySize > g_undoMaxMemorySize)
{
//always keep one undo
if (g_undolist == g_lastundo) break;
Undo_FreeFirstUndo();
}
//
//Sys_Printf("undo size = %d, undo memory = %d\n", g_undoSize, g_undoMemorySize);
}
/*
=============
Undo_Undo
=============
*/
void Undo_Undo(void)
{
undo_t *undo, *redo;
brush_t *pBrush, *pNextBrush;
entity_t *pEntity, *pNextEntity, *pUndoEntity;
if (!g_lastundo)
{
Sys_Printf("Nothing left to undo.\n");
return;
}
if (!g_lastundo->done)
{
Sys_Printf("Undo_Undo: WARNING: last undo not yet finished!\n");
}
// get the last undo
undo = g_lastundo;
if (g_lastundo->prev) g_lastundo->prev->next = NULL;
else g_undolist = NULL;
g_lastundo = g_lastundo->prev;
//allocate a new redo
redo = (undo_t *) malloc(sizeof(undo_t));
if (!redo) return;
memset(redo, 0, sizeof(undo_t));
redo->brushlist.next = &redo->brushlist;
redo->brushlist.prev = &redo->brushlist;
redo->entitylist.next = &redo->entitylist;
redo->entitylist.prev = &redo->entitylist;
if (g_lastredo) g_lastredo->next = redo;
else g_redolist = redo;
redo->prev = g_lastredo;
redo->next = NULL;
g_lastredo = redo;
redo->time = Sys_DoubleTime();
redo->id = g_redoId++;
redo->done = true;
redo->operation = undo->operation;
//reset the redo IDs of all brushes using the new ID
for (pBrush = active_brushes.next; pBrush != NULL && pBrush != &active_brushes; pBrush = pBrush->next)
{
if (pBrush->redoId == redo->id)
{
pBrush->redoId = 0;
}
}
for (pBrush = selected_brushes.next; pBrush != NULL && pBrush != &selected_brushes; pBrush = pBrush->next)
{
if (pBrush->redoId == redo->id)
{
pBrush->redoId = 0;
}
}
//reset the redo IDs of all entities using thew new ID
for (pEntity = entities.next; pEntity != NULL && pEntity != &entities; pEntity = pEntity->next)
{
if (pEntity->redoId == redo->id)
{
pEntity->redoId = 0;
}
}
// remove current selection
Select_Deselect();
// move "created" brushes to the redo
for (pBrush = active_brushes.next; pBrush != NULL && pBrush != &active_brushes; pBrush=pNextBrush)
{
pNextBrush = pBrush->next;
if (pBrush->undoId == undo->id)
{
//Brush_Free(pBrush);
//move the brush to the redo
Brush_RemoveFromList(pBrush);
Brush_AddToList(pBrush, &redo->brushlist);
//make sure the ID of the owner is stored
pBrush->ownerId = pBrush->owner->entityId;
//unlink the brush from the owner entity
Entity_UnlinkBrush(pBrush);
}
}
// move "created" entities to the redo
for (pEntity = entities.next; pEntity != NULL && pEntity != &entities; pEntity = pNextEntity)
{
pNextEntity = pEntity->next;
if (pEntity->undoId == undo->id)
{
// check if this entity is in the undo
for (pUndoEntity = undo->entitylist.next; pUndoEntity != NULL && pUndoEntity != &undo->entitylist; pUndoEntity = pUndoEntity->next)
{
// move brushes to the undo entity
if (pUndoEntity->entityId == pEntity->entityId)
{
pUndoEntity->brushes.next = pEntity->brushes.next;
pUndoEntity->brushes.prev = pEntity->brushes.prev;
pEntity->brushes.next = &pEntity->brushes;
pEntity->brushes.prev = &pEntity->brushes;
}
}
//
//Entity_Free(pEntity);
//move the entity to the redo
Entity_RemoveFromList(pEntity);
Entity_AddToList(pEntity, &redo->entitylist);
}
}
// add the undo entities back into the entity list
for (pEntity = undo->entitylist.next; pEntity != NULL && pEntity != &undo->entitylist; pEntity = undo->entitylist.next)
{
g_undoMemorySize -= Entity_MemorySize(pEntity);
//if this is the world entity
if (pEntity->entityId == world_entity->entityId)
{
//free the epairs of the world entity
Entity_FreeEpairs(world_entity);
//set back the original epairs
world_entity->epairs = pEntity->epairs;
// unhook the epairs and free the world_entity clone that stored the epairs
pEntity->epairs = NULL;
Entity_Free(pEntity);
}
else
{
Entity_RemoveFromList(pEntity);
Entity_AddToList(pEntity, &entities);
pEntity->redoId = redo->id;
}
}
// add the undo brushes back into the selected brushes
for (pBrush = undo->brushlist.next; pBrush != NULL && pBrush != &undo->brushlist; pBrush = undo->brushlist.next)
{
g_undoMemorySize -= Brush_MemorySize(pBrush);
Brush_RemoveFromList(pBrush);
Brush_AddToList(pBrush, &active_brushes);
for (pEntity = entities.next; pEntity != NULL && pEntity != &entities; pEntity = pNextEntity)
{
if (pEntity->entityId == pBrush->ownerId)
{
Entity_LinkBrush(pEntity, pBrush);
break;
}
}
//if the brush is not linked then it should be linked into the world entity
if (pEntity == NULL || pEntity == &entities)
{
Entity_LinkBrush(world_entity, pBrush);
}
//build the brush
//Brush_Build(pBrush);
Select_Brush(pBrush);
pBrush->redoId = redo->id;
}
//
Sys_Printf("%s undone.\n", undo->operation);
// free the undo
g_undoMemorySize -= sizeof(undo_t);
free(undo);
g_undoSize--;
g_undoId--;
if (g_undoId <= 0) g_undoId = 2 * g_undoMaxSize;
//
g_bScreenUpdates = true;
Sys_UpdateWindows(W_ALL);
}
/*
=============
Undo_Redo
=============
*/
void Undo_Redo(void)
{
undo_t *redo;
brush_t *pBrush, *pNextBrush;
entity_t *pEntity, *pNextEntity, *pRedoEntity;
if (!g_lastredo)
{
Sys_Printf("Nothing left to redo.\n");
return;
}
if (g_lastundo)
{
if (!g_lastundo->done)
{
Sys_Printf("WARNING: last undo not finished.\n");
}
}
// get the last redo
redo = g_lastredo;
if (g_lastredo->prev) g_lastredo->prev->next = NULL;
else g_redolist = NULL;
g_lastredo = g_lastredo->prev;
//
Undo_GeneralStart(redo->operation);
// remove current selection
Select_Deselect();
// move "created" brushes back to the last undo
for (pBrush = active_brushes.next; pBrush != NULL && pBrush != &active_brushes; pBrush = pNextBrush)
{
pNextBrush = pBrush->next;
if (pBrush->redoId == redo->id)
{
//move the brush to the undo
Brush_RemoveFromList(pBrush);
Brush_AddToList(pBrush, &g_lastundo->brushlist);
g_undoMemorySize += Brush_MemorySize(pBrush);
pBrush->ownerId = pBrush->owner->entityId;
Entity_UnlinkBrush(pBrush);
}
}
// move "created" entities back to the last undo
for (pEntity = entities.next; pEntity != NULL && pEntity != &entities; pEntity = pNextEntity)
{
pNextEntity = pEntity->next;
if (pEntity->redoId == redo->id)
{
// check if this entity is in the redo
for (pRedoEntity = redo->entitylist.next; pRedoEntity != NULL && pRedoEntity != &redo->entitylist; pRedoEntity = pRedoEntity->next)
{
// move brushes to the redo entity
if (pRedoEntity->entityId == pEntity->entityId)
{
pRedoEntity->brushes.next = pEntity->brushes.next;
pRedoEntity->brushes.prev = pEntity->brushes.prev;
pEntity->brushes.next = &pEntity->brushes;
pEntity->brushes.prev = &pEntity->brushes;
}
}
//
//Entity_Free(pEntity);
//move the entity to the redo
Entity_RemoveFromList(pEntity);
Entity_AddToList(pEntity, &g_lastundo->entitylist);
g_undoMemorySize += Entity_MemorySize(pEntity);
}
}
// add the undo entities back into the entity list
for (pEntity = redo->entitylist.next; pEntity != NULL && pEntity != &redo->entitylist; pEntity = redo->entitylist.next)
{
//if this is the world entity
if (pEntity->entityId == world_entity->entityId)
{
//free the epairs of the world entity
Entity_FreeEpairs(world_entity);
//set back the original epairs
world_entity->epairs = pEntity->epairs;
//free the world_entity clone that stored the epairs
Entity_Free(pEntity);
}
else
{
Entity_RemoveFromList(pEntity);
Entity_AddToList(pEntity, &entities);
}
}
// add the redo brushes back into the selected brushes
for (pBrush = redo->brushlist.next; pBrush != NULL && pBrush != &redo->brushlist; pBrush = redo->brushlist.next)
{
Brush_RemoveFromList(pBrush);
Brush_AddToList(pBrush, &active_brushes);
for (pEntity = entities.next; pEntity != NULL && pEntity != &entities; pEntity = pNextEntity)
{
if (pEntity->entityId == pBrush->ownerId)
{
Entity_LinkBrush(pEntity, pBrush);
break;
}
}
//if the brush is not linked then it should be linked into the world entity
if (pEntity == NULL || pEntity == &entities)
{
Entity_LinkBrush(world_entity, pBrush);
}
//build the brush
//Brush_Build(pBrush);
Select_Brush(pBrush);
}
//
Undo_End();
//
Sys_Printf("%s redone.\n", redo->operation);
//
g_redoId--;
// free the undo
free(redo);
//
g_bScreenUpdates = true;
Sys_UpdateWindows(W_ALL);
}
/*
=============
Undo_RedoAvailable
=============
*/
int Undo_RedoAvailable(void)
{
if (g_lastredo) return true;
return false;
}
/*
=============
Undo_UndoAvailable
=============
*/
int Undo_UndoAvailable(void)
{
if (g_lastundo)
{
if (g_lastundo->done)
return true;
}
return false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -