鼠标交互的设计模式



我需要一些关于普通鼠标的"理想"设计模式的意见互动。

这里是简化的问题。我有一个小型3D程序(QT和openGL),并且我使用鼠标进行交互。每次互动通常不仅仅是单个函数调用,它主要由最多 3 个函数调用(启动、执行、完成)执行。例如,相机旋转:此处初始函数调用将提供当前的第一个鼠标位置,而执行函数调用将更新相机等。

但是,对于仅几个交互,请对这些交互进行硬编码(在MousePressEvent,MouseReleaseEvent MouseMoveEvent或MouseWheelEvent等内部)没什么大不了的,但是如果我考虑一个更高级的程序(例如20个或更多的交互),那么需要一个适当的设计。

因此,您将如何在QT中设计这样的交互。

我希望我把我的问题说得足够清楚,否则不要抱怨:-)

谢谢

我建议使用多态性和工厂方法模式。下面是一个示例:

在我的Qt程序中,我有QGraphicsScenes和QGraphicsItems,带有mousePressEvent,mouseMoveEvent和mouseReleaseEvent,它们看起来像这样:

void CustomItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
  // call factory method, which returns a subclass depending on where click occurred
  dragHandler = DragHandler::createDragHandler(event /* and other relevant stuff */);
}
void CustomItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
  dragHandler->onMouseMove(event);
}
void CustomItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
  dragHandler->onMouseRelease(event);
  delete dragHandler;
}

这种特殊情况下的想法是,根据我单击自定义项的位置,鼠标按下、移动和释放将具有不同的功能。例如,如果我单击项目的边缘,拖动将调整其大小,但如果我单击项目的中间,拖动将移动它。DragHandler::onMouseMove 和 DragHandler::onMouseRelease 是由子类重新实现的虚拟函数,以根据鼠标按下发生的位置提供我想要的特定功能。不需要 DragHandler::onMousePress,因为它基本上是构造函数。

这当然是一个相当具体的例子,可能不完全是你想要的,但它让你知道如何使用多态性来清理鼠标处理。

Qt使这变得非常简单。

与其使用您过去编写的所有switch mouse_mode:内容,只需让每个鼠标事件处理程序函数发出一个信号,即 mouseDown/mouseUp/mousePosition 并使用信号/插槽将它们路由到适当的模型函数。

然后,您可以通过将不同的插槽连接/断开到鼠标中发送的信号来适应鼠标的不同用途(选择、旋转、编辑等)。事件()

我发现Apple的UIGestureRecognizer设计非常好且可扩展。

这个想法是将手势(或交互)的识别与将触发的动作分离。

您需要实现一个基本或抽象的 GestureRecognizer 类,该类能够根据事件 MousePressEvent、MouseReleaseEvent MouseMoveEvent 或 MouseWheelEvent 等识别特定的交互或手势。

例如,您非常基本的类是这样的:(对不起,我可怜的 semi c++ 伪代码......最近我不太使用它)

class Recognizer {
int state; // ex: 0:possible, 1:began, 2:changed, 3:ended/recognized 4:cancelled
protected:
void setTarget(void &theTarget); // or even better a touple, target/method. In this case target is assumed to have a method gestureHandle(Recognizer *r);
virtual void mouserPress() = 0;
virtual void mouserRelease() = 0;
virtual void mouserMove() = 0;
virtual void mouserWheel() = 0;
...
}

如果您想检测用鼠标滑动

class SwipeRecognizer : Recognizer {
int direction; // ex: 0:left2right 1:bottom2top 2:...
private:
void mouserPress() {
    state = 0; // possible. You don't know yet is the mouse is going to swipe, simple click, long press, etc.
    // save some values so you can calculate the direction of the swipe later 
    target.gestureHandle(this);
};
void mouserMove() {
    if (state == 0) {
        state = 1; // it was possible now you know the swipe began!
        direction = ... // calculate the swipe direction here
    } else if (state == 1 || state == 2) {// state is began or changed
        state = 2; // changed ... which means is still mouse dragging
        // probably you want to make more checks here like you are still swiping in the same direction you started, maybe velocity thresholds, if any of your conditions are not met you should cancel the gesture recognizer by setting its state to 4
    }
    target.gestureHandler(this);
};
void mouserRelease() {
    if (state == 2) { // is swipping
        state = 3; // swipe ended
    } else {
        state = 4; // it was not swiping so simple cancel the tracking
    }
    target.gestureHandler(this);
};
void mouserWheel() {
    // if this method is called then this is definitely not a swipe right?
    state = 4; // cancelled
    target.gestureHandler(this);
}

只需确保在事件发生时调用上述方法,并且它们应该在需要时调用目标。

这就是目标在我看来的样子:

class Target {
...
void gestureHandler(Recognizer *r) {
    if (r->state == 2) {
        // Is swipping: move the opengl camera using some parameter your recognizer class brings
    } else if (r->state == 3) {
        // ended: stop moving the opengl camera
    } else if (r->state == 4) {
        // Cancelled, maybe restore camera to original position?
    }
}

UIGestureRecognizer的实现非常好,将允许为同一识别器和多个识别器注册多个目标/方法到同一视图。UIGestureRecognizers 有一个委托对象,用于获取有关其他手势识别器的信息,例如,如果可以同时检测到两个手势,或者检测到另一个手势必须立即失败,等等。

某些手势识别器

比其他手势识别器需要更多的覆盖,但最大的优点是它们的输出是相同的:通知当前状态(和其他信息)的处理程序方法。

我认为值得一看

希望对:)有所帮助

最新更新