gpt4 book ai didi

c++ - 命令模式 : how can I do continuous actions?(例如移动对象)

转载 作者:行者123 更新时间:2023-11-28 01:52:25 26 4
gpt4 key购买 nike

假设我正在编写一个程序,用户可以在其中绘制然后移动形状。 MoveCommand 看起来像这样:

class MoveCommand {
public:
MoveCommand(Shape& shape, const Vector2f& offset) :
shape(shape), offset(offset)
{ }

void execute() {
shape.move(offset);
}

void undo() {
shape.move(-offset);
}
private:
Shape& shape;
Vector2f offset;
};

这很好用,但我如何显示移动预览(当用户按住鼠标按钮时),然后仅在释放鼠标按钮时存储最终偏移量?

ShapeEditor 类是否应该移动形状,然后在释放按钮时创建 MoveCommand?如果 execute() 的代码不简单怎么办?如何避免 ShapeEditorMoveCommand 中的代码重复?

最佳答案

This works well, but how can I display preview of movement (when user holds mouse button) and then only store final offset on mouse button release?

如果我对您的理解是正确的,您希望将整个 Action 作为单个操作不可执行/可重新执行,同时在第一次以交互方式完成时为每个单独的微 Action 设置动画。

一种方法是您自己建议的方法,即仅在移动完成时才记录撤消/重做命令。正如您所指出的,这会导致一些代码重复。实际上,这不是问题,因为您始终可以分解出通用代码。

另一种方法是为每个微移动创建一个 MoveCommand,然后将命令合并作为撤消/重做堆栈的一部分。看看怎么样done in Qt .

关于c++ - 命令模式 : how can I do continuous actions?(例如移动对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42465901/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com