MyGUI use delegates        

MyGUI Delegates

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

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

With this event, you function must have only one input parameter - MyGUI::Widget* _sender.

Same class member function

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

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

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

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)

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