Creating/assigning delegate

itekimasu

10-05-2009 14:34:15

Hi guys,

I am trying to bind a delegate to a button but I get an error, and VS 2008 seems to be acting as if MyGUI::newDelegate doesn't exist.


MyGUI::ButtonPtr button = mGUI->createWidget<MyGUI::Button>("Button", 10, 10, 300, 26, MyGUI::Align::Default, "Main");
button->setCaption("exit");

button->eventMouseButtonClick = MyGUI::newDelegate(this, &UserInterface::MouseClick);


>Unknown compiler version - please run the configure tests and report the results
trunk\src\drivingsim_main\userinterface.cpp(26) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'MyGUI::delegates::IDelegate0 *' (or there is no acceptable conversion)
1> dependencies\mygui\include\mygui_delegateimplement.h(156): could be 'MyGUI::delegates::CDelegate1<TP1> &MyGUI::delegates::CDelegate1<TP1>::operator =(MyGUI::delegates::IDelegate1<TP1> *)'
1> with
1> [
1> TP1=MyGUI::WidgetPtr
1> ]
1> trunk\dependencies\mygui\include\mygui_delegateimplement.h(173): or 'MyGUI::delegates::CDelegate1<TP1> &MyGUI::delegates::CDelegate1<TP1>::operator =(const MyGUI::delegates::CDelegate1<TP1> &)'
1> with
1> [
1> TP1=MyGUI::WidgetPtr
1> ]
1> while trying to match the argument list '(MyGUI::EventHandle_WidgetVoid, MyGUI::delegates::IDelegate0 *)'

I'm a beginner at this stuff so I would appreciate any guidance.

Thanks a lot!

Five_stars

10-05-2009 15:17:03

Please, show full declaration of UserInterface::MouseClick.
Check that, it has the needed type. You can see it in declaration of eventMouseButtonClick .

my.name

10-05-2009 22:41:54

void UserInterface::MouseClick(MyGUI::WidgetPtr _sender)
{
}

?

MuLLeR

11-05-2009 05:10:50

Isn't it MyGUI::delegates::newDelegate ?

Altren

11-05-2009 16:43:50

Compiler said that you have wrong signature, Five_stars and my.name repeated it. Should I tell it once more?

About your last post - we have using MyGUI::delegates::newDelegate; in MyGUI_Prerequest.h

itekimasu

11-05-2009 17:32:59

Last post wasn't me Altren, but thanks everyone anyway, i got it. Just had to add the pointer to the parameters, as you said.

I'm trying now to set a delegate to act when a list box has a change in its item selection. I have:

// load layout
MyGUI::VectorWidgetPtr widgets = MyGUI::LayoutManager::getInstance().load("scene6.layout");

for(unsigned int i = 0; i < widgets.size(); i++) {
printf("Type of widget: %s\n", widgets[i]->getUserString("type").c_str());

if(widgets[i]->getUserString("type") == "button") {
widgets[i]->eventMouseButtonClick = MyGUI::newDelegate(this, &UserInterface::mouseClick);
}

if(widgets[i]->getUserString("type") == "tabs") {
printf("Tab found\n");
for(unsigned int k = 0; k < widgets[i]->getChildCount(); k++) {
printf("Getting tab child %d\n", k);
printf("Type of widget: %s\n", widgets[i]->getChildAt(k)->getUserString("type").c_str());
if(widgets[i]->getChildAt(k)->getUserString("type") == "list") {
MyGUI::ListPtr pList = (MyGUI::ListPtr)widgets[i]->getChildAt(k);
pList->eventListMouseItemActivate = MyGUI::newDelegate(this, &UserInterface::itemActivated);
}
}
}

//etc.


A couple of things. Firstly, when trying to print the user strings for the children of the tab, nothing is coming out (see the second printf statement in the k for loop).
Second thing, I'm not sure about setting the delegate for the list box based on item activation. I know the above assignment is not correct because I get an error.

Thank you again.

Altren

11-05-2009 18:02:12

Sorry, missed that it wasn't you.

About UserStrings - have you assigned them first?

About event - all signatures described near event declaration (and in docs) /** Event : Item was selected by mouse.\n
signature : void method(MyGUI::ListPtr _sender, size_t _index)\n
@param _sender widget that called this event
@param _index of selected item
*/
EventPair<EventHandle_WidgetSizeT, EventHandle_ListPtrSizeT> eventListMouseItemActivate;
So your event should look like
UserInterface::itemActivated(MyGUI::ListPtr _sender, size_t _index)
{
//...
}

itekimasu

11-05-2009 20:36:29

I'm setting the user string in the layout file and it prints for everything but the children of the tabs for some reason.

Ignoring the if statement for now, if I just try:


MyGUI::ListPtr pList = (MyGUI::ListPtr)widgets[0]->getChildAt(0);
pList->eventListMouseItemActivate = MyGUI::delegates::newDelegate(this, &UserInterface::itemActivated);

.....


void UserInterface::itemActivated(MyGUI::ListPtr _sender, size_t _index)
{
printf("Selected item %s in list %s\n", _sender->getItem(_index).c_str(), _sender->getName().c_str());
}


It builds but the program crashes at the delegate setting line. I know that the first widget in the vector is a tab with 2 children and the first child is a list. Any ideas?

Altren

11-05-2009 20:51:46

It crashes because pList is not valid.
And about Tab childs: tab don't have normal childs. It have tab items (sheets) and tab items have childs.

itekimasu

12-05-2009 06:42:32

Thanks for all your help Altren. I got the list responding now.

void UserInterface::loadLayout(Ogre::String _file)
{
// load layout
MyGUI::VectorWidgetPtr widgets = MyGUI::LayoutManager::getInstance().load(_file);

for(unsigned int i = 0; i < widgets.size(); i++) {
printf("Type of widget: %s\n", widgets[i]->getUserString("type").c_str());

if(widgets[i]->getUserString("type") == "button") {
widgets[i]->eventMouseButtonClick = MyGUI::delegates::newDelegate(this, &UserInterface::mouseClick);
}

if(widgets[i]->getUserString("type") == "tabs") {
MyGUI::TabPtr pTab = (MyGUI::TabPtr) widgets[i];
printf("Tab has %d sheets\n", pTab->getSheetCount());
for(unsigned int k = 0; k < pTab->getSheetCount(); k++) {
printf("Getting children of sheet %d\n", k);
for(unsigned int y = 0; y < pTab->getSheet(k)->getChildCount(); y++) {
printf("Name: %s. Type of widget: %s\n", pTab->getSheet(k)->getChildAt(y)->getName().c_str(), pTab->getSheet(k)->getChildAt(y)->getUserString("type").c_str());
if(pTab->getSheet(k)->getChildAt(y)->getUserString("type") == "list") {
MyGUI::ListPtr pList = (MyGUI::ListPtr)pTab->getSheet(k)->getChildAt(y);
pList->eventListMouseItemActivate = MyGUI::delegates::newDelegate(this, &UserInterface::itemActivated);
}
}
}
}

if(widgets[i]->getUserString("type") == "window") {

}
}
}


I'm not sure why but printing the contents of the list item seems to only print the first character.

void UserInterface::itemActivated(MyGUI::ListPtr _sender, size_t _index)
{
printf("Selected item %s in list %s\n", _sender->getItem(_index).c_str(), _sender->getName().c_str());
}

Altren

12-05-2009 14:17:30

It's probably because strings is in utf, so c_str give you multibyte string and every secongchar is '\0' for latin characters.
To print it you should convert utf to ansi string. Try MyGUI::convert::utf8_to_ansi