Skip to main content

History: MyGUI use delegates

Preview of version: 7

MyGUI Delegates

To use your function as delegate you must have specific signature for it. You can find event signature near event declaration or in documentation for specific widget and in WidgetEvent class documentation for common events. For example for eventMouseButtonClick :

Copy to clipboard
/** Event : Mouse button pressed and released.\n signature : void method(MyGUI::Widget* _sender) @param _sender widget that called this event */ EventHandle_WidgetVoid eventMouseButtonClick;

So your function must have only one input parameter - MyGUI::Widget* _sender.

Same class member function

Copy to clipboard
class Test { public: void mousePressed(MyGUI::Widget* _widget) { // ... } void main() { MyGUI::Button* button = mGUI->createWidget<MyGUI::Button>("Button", 10, 10, 300, 26, MyGUI::Align::Default, "Main"); button->eventMouseButtonClick = MyGUI::newDelegate(this, &Test::mousePressed); } }

Parent class member function

Copy to clipboard
class Parent { public: void mousePressed(MyGUI::Widget* _widget) { // ... } }; class Child : public Parent { void main() { MyGUI::Button* button = mGUI->createWidget<MyGUI::Button>("Button", 10, 10, 300, 26, MyGUI::Align::Default, "Main"); #ifdef OGRE_COMPILER_MSVC button->eventMouseButtonClick = MyGUI::newDelegate(static_cast<Parent *>(this), &Parent::mousePressed); #else // Visual Studio fails with error C2660: 'MyGUI::newDelegate' : function does not take 2 arguments // using this variant. button->eventMouseButtonClick = MyGUI::newDelegate(this, &Child::mousePressed); #endif } }

Member function from another class

Copy to clipboard
class Test { public: void mousePressed(MyGUI::Widget* _widget) { // ... } } class Main { public: void main() { MyGUI::Button* button = mGUI->createWidget<MyGUI::Button>("Button", 10, 10, 300, 26, MyGUI::Align::Default, "Main"); Test test; button->eventMouseButtonClick = MyGUI::newDelegate(&test, &Test::mousePressed); // or Test * test2 = new Test(/*...*/); button->eventMouseButtonClick = MyGUI::newDelegate(test2, &Test::mousePressed); } }

Global function

Copy to clipboard
void mousePressed(MyGUI::Widget* _widget) { // ... } class Main { public: void main() { MyGUI::Button* button = mGUI->createWidget<MyGUI::Button>("Button", 10, 10, 300, 26, MyGUI::Align::Default, "Main"); button->eventMouseButtonClick = MyGUI::newDelegate(mousePressed); } }

Static member function from any class (same or another)

Copy to clipboard
class Test { public: // static function static void mousePressed(MyGUI::Widget* _widget) { // ... } void main() { MyGUI::Button* button = mGUI->createWidget<MyGUI::Button>("Button", 10, 10, 300, 26, MyGUI::Align::Default, "Main"); button->eventMouseButtonClick = MyGUI::newDelegate(Test::mousePressed); } }

History

Information Version
Fri 24 of Feb, 2012 23:53 GMT-0000 Datalore fixed my own spelling mistake 9
Fri 24 of Feb, 2012 23:53 GMT-0000 Datalore fixed minor grammar mistakes 8
Sun 31 of Jul, 2011 22:46 GMT-0000 jacmoe 7
Tue 03 of May, 2011 09:56 GMT-0000 DragonM Replace ButtonPtr with Button*. ButtonPtr, like WidgetPtr, no longer exists in MyGUI. Remove old parent class member function sample. Reformat whitespace a little. 6
Wed 27 of Oct, 2010 20:49 GMT-0000 Altren change "Parent class member function" delegate 5
Tue 29 of Jun, 2010 15:58 GMT-0000 Altren 4
Sun 20 of Jun, 2010 22:26 GMT-0000 DragonM Add parent class delegate sample. 3
Mon 28 of Dec, 2009 05:41 GMT-0000 jacmoe 2
Sun 29 of Mar, 2009 13:44 GMT-0000 Beauty [[Category:GUI]] 1