c++ GUI 事件/消息传递设计

发布于 2024-11-16 03:47:46 字数 562 浏览 0 评论 0原文

因此,我正在使用 DirectX 9 和 C++ 制作一个简单的游戏。我查看了 SDK 的 GUI,但我认为我可以实现一些更简单的东西。

我想要的只是窗口、标签、按钮、文本框和复选框。

我创建了一个基类 GUIObject,所有对象都继承自该基类。它包括尺寸和焦点等基础知识。然后派生类,例如 GUILabel,都定义了 render()、update() 等。

我的问题是如何处理事件,例如点击?我让它与 GUILabel::Click() 一起使用,根据当前实例的文本成员值定义每种可能性。感觉不对,我意识到需要单击的每个标签都必须在 GUILabel 类中定义。我想将其移至每个游戏状态的代码中。

因此,我简单地尝试让 GUILabel::Click() 将函数指针作为参数。但后来我意识到我需要将状态的类成员方法设置为静态(实际上不可能,除非其中的所有内容都是静态的,对吧?)或者也将其传递给 GUIObject 类。这是要走的路吗?

我可以在游戏状态中定义 GUILabel(或按钮,或其他什么)的派生并覆盖我需要的任何操作吗?然后对我在该状态下需要的任何控件执行此操作?

实现事件处理的正确方法是什么?

So, I'm making a simple game using DirectX 9 and C++. I looked at the SDK's GUI, but I think I can implement something simpler.

All I want are windows, labels, buttons, textboxes, and checkboxes.

I've created a base class GUIObject, that all inherit from. It includes basics like size and focus and whatnot. Then the derived classes, GUILabel for example, all define render(), update() and whatnot.

My question is how to handle events, like clicks? I had it working with GUILabel::Click() defining every possibility based on the current instance's text member value. It felt wrong and I realized that every single label that needed to be clicked would have to be defined in the GUILabel class. I'd like to move that to each game state's code.

So, I briefly tried making the GUILabel::Click() take a function pointer as an argument. But then I realized I needed to have the state's class member method as static (not really possible, unless everything in it is static as well, right?) or also pass it a GUIObject class as well. Is that the way to go?

Could I define a derivation of GUILabel (or button, or whatnot) within a game state and just override whichever actions I needed? And then do that for whatever controls I need in that state?

What is the proper way to implement event handling?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

动听の歌 2024-11-23 03:47:46

您可能需要考虑使用信号库(例如 boost::signals),以允许您灵活地定义 GUI 对象与每个回调将触发的底层事件之间的接口。对于需要 GUI 元素(例如状态指示器等)来响应底层事件的反向关系,它也可以派上用场。

You might want to look into using a signaling library such as boost::signals to allow you the flexibility of defining the interface between your GUI objects and the underlying events that each callback will trigger. It can also come in handy for the reverse relationship where you need GUI elements such as status indicators, etc. to respond to underlying events.

帅的被狗咬 2024-11-23 03:47:46

对于回调/点击事件,我会使用 boost::function/boost::bind 。
在我的 GUI 框架中,我曾经有两个不同的抽象类 InputHandler 和 Renderable 来处理触摸和渲染。这导致不需要可点击的组件(如 UILabel)不需要实现无用的方法的设计。

哈特哈,
亚历克斯

For callbacks/click events i would go with boost::function/boost::bind.
In my GUI framework I used to have two different abstract classes InputHandler and Renderable for handling touches and rendering. This leads to a design where components which don't need to be clickable (like UILabel) wont need to implement useless methods.

HTH,
Alex

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文