undo和redo操作在许多软件中都有应用,而3ds max也不例外。那么3ds max是用什么东东来实现undo和redo呢?
看了max sdk 2010的Deriving From RestoreObj后,你会恍然大悟,max是用RestoreObj来实现undo和redo的。
首先, 你要实现一个继承RestoreObj的类(DeriveRestoreObj) ,在这个类里要实现RestoreObj里的Restore和Redo函数,它们分别是用来
实现max的undo和redo操作的。
再者 , 你还要实现其他方法,比如size(),EndHold(),Description()等函数。size函数是告诉你你的RestoreObj的字节数。
EndHold()是设置一个restore的结束。
接下来,就是配合theHold来实现undo和redo操作了。这里你把theHold当做一个堆栈桶,theHold.begin()就代表开始往里面
放入操作,而紧接着的theHold.put(new DeriveRestoreObj(....))就代表这个堆栈桶要以DeriveRestoreObj里的Restore函数和
Redo函数来执行undo和redo操作。
然后,经过一段代码操作后,theHold.accept()就代表这段代码操作被压到堆栈桶里了,而theHold.cancel()就代表撤销了压入堆栈桶
的操作,即这段代码操作没有压入堆栈桶,同时撤销了theHold 堆栈桶。
最后,拿max sdk的例子给大家看看:
class NodeRest : public RestoreObj {
public:
Orient offs;
Orient redoOffs;
Node *cur;
NodeRest() { cur = NULL; }
NodeRest(Node *node) {
cur = node;
offs = node->modOffset;
}
~NodeRest() {}
void Restore(int isUndo);
void Redo();
int Size() { return 2*sizeof(Orient) + sizeof(INode*); }
void EndHold() { cur->ClearAFlag(A_HELD); }
virtual TSTR Description() { return TSTR(_T(" Node State")); }
};
void Node::HoldState() {
if (theHold.Holding() && !TestAFlag(A_HELD))
{
theHold.Put(new NodeRest(this));
SetAFlag(A_HELD);
}
}