如何实现点击式用户交互模型?
上下文:我正在用 c++/OpenGL 进行简单的太阳系模拟。
研究:我尝试过搜索,但我只找到有关小部件、闪存和 HCI 内容的文章。
问题:我希望用户能够单击演员,然后取消选择或选择其他内容。也许一次选择多个演员。如果演员被毁了,我希望这个选择消失。我想让演员知道它已经被选中了。
我知道如何获取鼠标坐标,以及如何查看单击是否发生在这个演员或那个演员或最近的演员上。我没有任何经验做交互建模。我可以想到类似 CSS 的活动、悬停和按下模型,所以我猜是某种状态。但接下来我遇到了所有权问题,如果该演员被删除或需要删除会发生什么?是否应该有某种观察者?
显然我对这个问题的理解还不够,无法为我的目的尝试解决它。我没有回调或事件等方面的经验。谁能给我指出一些文章、指南或类似的帮助?
Context: I'm making a simple solar system simulation in c++/OpenGL.
Research: I've tried searching but I only ever find articles on widgets and flash and HCI stuff.
Problem: I'd like the user to be able to click on an actor, and then maybe deselect, or select something else. Maybe select multple actors at once. If the actor is destroyed, I'd like the selection to go away. I'd like the actor to know it's been selected.
I know how to get the mouse coordinates, and how to see if a click happened on this actor or that actor, or the closest actor. What I don't have any experience doing is modelling the interaction. I can think of something like CSS's model of active, hover, and pressed, so some sort of state, I guess. But then I get into the problem of ownership, and what happens if that actor is deleted or needs to be deleted? Should there be some sort of observer?
Obviously I don't understand the problem enough to try and solve it for my purposes. I have no experience with callbacks or events or whatever. Can anyone point me to some articles, guides, or similar help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您为参与者使用智能指针(例如
boost::shared_pointer
),那么您可以通过使用弱指针进行选择来轻松解决所有权问题。例如:您只需要知道从该集合中检索的任何指针都可能无效,但 Boost 几乎强制您检查这一点。
为了让演员知道它何时被选择,我只需创建一些由您的选择代码调用的虚拟成员函数,例如:
如果您还需要演员跟踪是否它被选择,那么就这样做像这样:
希望有帮助!
If you are using smart pointers for your actors (e.g.
boost::shared_pointer
) then you can solve the ownership problem easily by using weak pointers for your selection. For example:You just need to be aware that any pointer you retrieve from that set might be invalid, but Boost pretty much forces you to check that.
For the actor to know when it's selected, I would just make some virtual member functions that are called by your selection code, e.g.:
If you also need the actor to keep track of whether it's selected, do it like this:
Hope that helps!
我建议快速浏览一下 Qt。它会做你想做的一切,甚至更多。大多数情况下,你可以拿走你想要的,留下你不想要的。
它还有一个很好的方法来包装/集成 OpenGL 代码与您想要的更友好的用户界面元素。
I would suggest having a quick look at Qt. It will do all that you want and more. For the most part, you can take what you want and leave what you don't.
It also has a good way to wrap-up/integrate OpenGL code with the more friendly elements of a user interface that you're after.