鼠标交互的设计模式 [英] Design pattern for mouse interaction

查看:229
本文介绍了鼠标交互的设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些关于一般鼠标
交互的理想设计模式的意见。

I need some opinions on what is the "ideal" design pattern for a general mouse interaction.

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

Here the simplified problem. I have a small 3d program (QT and openGL) and I use the mouse for interaction. Every interaction is normally not only a single function call, it is mostly performed by up to 3 function calls (initiate, perform, finalize). For example, camera rotation: here the initial function call will deliver the current first mouse position, whereas the performing function calls will update the camera etc.

但是,对于只有几个交互,这些(MousePressEvent,MouseReleaseEvent MouseMoveEvent或MouseWheelEvent等)中的硬编码不是一件大事,但如果我想到一个更高级的程序(例如20个或更多的交互)那么需要一个适当的设计。

However, for only a couple of interactions, hardcoding these (inside MousePressEvent, MouseReleaseEvent MouseMoveEvent or MouseWheelEvent etc) is not a big deal, but if I think about a more advanced program (e.g 20 or more interactions) then a proper design is needed.

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

Therefore, how would you design such a interactions inside QT.

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

I hope I made my problem clear enough, otherwise don't bother complain :-)

谢谢

推荐答案

我建议使用多态和工厂方法模式。这里有一个例子:

I suggest using polymorphism and the factory method pattern. Here's an example:

在我的Qt程序中,我有QGraphicsScenes和QGraphicsItems与mousePressEvent,mouseMoveEvent和mouseReleaseEvent,如下所示:

In my Qt program I have QGraphicsScenes and QGraphicsItems with mousePressEvent, mouseMoveEvent, and mouseReleaseEvent, which look like this:

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;
}

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

The idea in this particular case is that depending on where I click on CustomItem, mouse pressing, moving, and releasing will have different functionality. For example, if I click on the edge of the item, dragging will resize it, but if I click in the middle of the item, dragging will move it. DragHandler::onMouseMove and DragHandler::onMouseRelease are virtual functions that are reimplemented by subclasses to provide the specific functionality I want depending on where the mouse press occurred. There's no need for DragHandler::onMousePress because that's basically the constructor.

这当然是一个比较具体的例子,可能不完全是你想要的,但它给了你一个想法如何使用多态来清理你的鼠标处理。

This is of course a rather specific example, and probably not exactly what you want, but it gives you an idea of how you can use polymorphism to clean up your mouse handling.

这篇关于鼠标交互的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆