如何实现一个通用按钮类,单击时执行不同的功能?
好的,我正在使用 SDL 和 C++,并开始为我的地图编辑器开发一个简单的 GUI。根据单个按钮的工作原型,我编写了一个通用 GUIObject 类和一个通用 Button 类。但我现在有一个问题,为了使该类可重用,我需要能够为每个按钮设置它调用的函数(即 Help! 打开帮助窗口,Open 打开文件对话框,它们是相同的Button类,但执行不同的功能)。
现在,我前段时间使用了一个简单的 Ruby GUI 库,它的语法使用了一些被标记为“块运算符”的东西:
示例:
@login_button = Button.new(@window, 98, 131, Network_Data::LOGIN_BUTTON){execute_login}
最后一部分 {} 是您以某种方式放置自己的函数和类的地方提取该函数并在单击时执行该函数。我还听说 Java 有类似的东西:
onClick = new Function() { }
那么我将如何在 C++ 程序(使用 SDL 库)中实现它。我不想使用任何 GUI 库,因为它们看起来太复杂,而且我很难将这些库插入到我已经构建的代码中。
Okay, I'm using SDL with C++ and I started working on a simple GUI for my Map Editor. From a working prototype of a single button, I wrote a generic GUIObject class and a generic Button class. But I have a problem now, In order for the class to be reusable, I need to be able to set for each button what function it calls (i.e. Help! opens the help window, Open opens the file dialog box, they are the same Button class, but they execute different functions).
Now, I used some time ago a simple library for Ruby GUI, and it's syntax used something that was notated there as a "block operator":
Example:
@login_button = Button.new(@window, 98, 131, Network_Data::LOGIN_BUTTON){execute_login}
The last part {} was the thing where you put your own function and the class somehow extracted that and executed that function when clicked. I also heard Java has something similar:
onClick = new Function() { }
So how would I go and implement this in a C++ program (which uses SDL libraries). I don't want to use any GUI libraries because they seem too complicated and I would have a hard time inserting those libraries in my already built code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用函数/函子/方法指针来完成此操作。 (函数应该足够并且相对简单)
您可以在按钮构造函数中传递指向回调函数的指针。
这是使用函数指针的基本示例。
您可以这样使用它:
这是一个使用可变参数模板 (C++0x) 的更复杂的示例
您可以这样使用它:
Tr
模板可以删除并替换为void
如果您从不打算使用返回 void 的函数以外的其他函数。因此
ButtonmyButton1( &myFunction1 );
将更改为ButtonmyButton1( &myFunction1 );
和
Button; myButton2( &myFunction2 );
将更改为Button<>; myButton2( &myFunction2 );
您可以使用方法(您需要注册一个指向要调用的类实例的指针以及指向该方法的指针)和函子(您需要注册指向要调用的函子实例的指针)。
根据您的要求,这里是使用方法执行此操作的同一个类。您需要找到一种方法让您的 Button 类立即接受函数、函子和方法(想想 virtual ^^)。这是 C++0x 版本
,您可以这样使用它:
如果您需要从该方法所属的类内部调用它,请使用
this
作为目标参数,而不是myC1< /代码>。
You can do it using function/functor/method pointers. (functions should be enough and relatively simple)
you can pass a pointer to the callback function in you button constructor.
Here is a basic example using function pointers.
And you can use it this way:
Here is a more complex example using variadic templates (C++0x)
And you can use it this way:
The
Tr
template can be removed and replaced byvoid
if you never plan to use others functions than functions returning void.Thus
Button<void, int, double> myButton1( &myFunction1 );
would change toButton<int, double> myButton1( &myFunction1 );
and
Button<void> myButton2( &myFunction2 );
would change toButton<> myButton2( &myFunction2 );
You can do almost the same thing with methods (you need to register a pointer to the instance of the class to call AND the pointer to the method) and with functors (you need to register a pointer to the instance of the functor to call).
On your demand here is the same class to do it with methods. It's up to you to find a way to make your Button class accept functions, functors and methods at once (think virtual ^^). This is the C++0x version
And you can use it this way:
If you need to call it from inside the class the method belong to, use
this
as target parameter instead ofmyC1
.有两种方法。您可以使用运行时多态性(
boost::function
或std::tr1::function
或std::function
),或者您可以使用模板。There are two approaches. You can use run-time polymorphism (
boost::function
orstd::tr1::function
orstd::function
) or you can use a template.您将需要按钮类来调用用户指定的函数。我建议您查看 Boost Signals 库。该库实现了信号和槽方法,有非常好的文档并且得到了很好的支持。此外,这些链接对于了解总体思路应该很有用:
希望有帮助。祝你好运!
You will need your button class to call functions specified by its users. I recommend you to take a look at Boost Signals library. That library implements signals and slots approach, has very nice documentation and is very well supported. Also, these links should be useful to get the general idea:
Hope it helps. Good luck!
它在 C++ 中的工作方式与在 Java 中使用的方式几乎相同:
但是显然您需要以下内容来定义必要的类:
It works in almost the same way in c++ as you'd use in java:
but then you'd obviously need the following to define necessary classes: