MyGUI use delegates        
Print

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(external link) for specific widgets and in the WidgetEvent class documentation(external link) 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); 
     } 
 
 }

 


Contributors to this page: Datalore87 points  , jacmoe133512 points  , DragonM1059 points  , Beauty10198 points  and Altren595 points  .
Page last modified on Friday 24 of February, 2012 23:53:54 UTC by Datalore87 points .


The content on this page is licensed under the terms of the Creative Commons Attribution-ShareAlike License.
As an exception, any source code contributed within the content is released into the Public Domain.