[SOLVED] Event handling from layout file

globalsinner

03-06-2010 13:18:37

Hi,

I have the following test layout file:


<?xml version="1.0" encoding="utf-8"?>
<MyGUI type="Layout">
<Widget type="Window" skin="WindowCSX" position="10 10 300 100" layer="Overlapped" name="MyMainWindow">
<Widget type="StaticText" skin="StaticText" position="5 5 150 26">
<Property key="Text_TextAlign" value="Left VCenter"/>
<Property key="Widget_Caption" value="Select:"/>
</Widget>

<Widget type="Button" skin="Button" position="160 5 107 26" name="button_OK">
<Property key="Widget_Caption" value="Ok"/>
<Property key="Event" value = "OK_Pressed"/>
</Widget>

</Widget>
</MyGUI>


As you can see the button_OK has a key of type event valued at "OK_Pressed". I want an implementation so that when the user presses the button_OK above, the function will be called with the corresponding value of the event (ie. OK_Pressed, in this case). The final layout will have multiple buttons with varying event values and i would like to call a single function for these presses with the event ID as a parameter. After looking at samples and demos, I am using the following code to load the layout and set the event handler for button press.


MyGUI::LayoutManager::getInstance().load("MyMainMenu.layout");
MyGUI::WindowPtr mainWindowX = mGUI->findWidget<MyGUI::Window>("MyMainWindow");
mainWindowX->eventWindowButtonPressed = MyGUI::newDelegate(this, &menuMgr::handlePress);


the the corresponding function is


void menuMgr::handlePress(MyGUI::WidgetPtr _widget, const std::string& _name)
{
if(_name == "OK_Pressed")
{
MyGUI::WindowPtr window = _widget->castType<MyGUI::Window>();
.....
}
}


The code runs and compiles fine, but nothing happens when I press the OK button. Only the event handler for the top-right "X" button is working which calls the function. How do I get the other buttons within the window to work in a similar way?

haibo19981984

03-06-2010 15:03:55

you should find "button_OK" ,then set the function.For example:

MyGUI::ButtonPtr button = mGUI->findWidget<MyGUI::Button>("button_OK");
button->eventMouseButtonClick = MyGUI::newDelegate(this,&MyListener::notifyButtonOK);


void notifyButtonOK(MyGUI::WidgetPtr _sender)
{
......
}

globalsinner

03-06-2010 15:12:57

you should find "button_OK" ,then set the function.For example:

MyGUI::ButtonPtr button = mGUI->findWidget<MyGUI::Button>("button_OK");
button->eventMouseButtonClick = MyGUI::newDelegate(this,&MyListener::notifyButtonOK);


void notifyButtonOK(MyGUI::WidgetPtr _sender)
{
......
}


Hey, thanks for the response. That approach would work but the problem is i'm gonna have a lot of buttons in the final window and finding and assigning handlers for each button would result in horrible code. I wanted to simply be able to handle it all in a single function with a single event handler for the window. Am I thinking in totally the wrong way here? Any ideas bout that? MyGUI is awesome, but i'm still a beginner at it.

Altren

03-06-2010 17:44:39

Subwidgets names is important and used to determine widget purpose. You must use "Button" name (where you have "button_OK") for all buttons that is part of window.
<Widget type="Button" skin="Button" position="160 5 107 26" name="Button">
<Property key="Widget_Caption" value="Ok"/>
<Property key="Event" value = "OK_Pressed"/>
</Widget>

globalsinner

04-06-2010 10:07:07

Subwidgets names is important and used to determine widget purpose. You must use "Button" name (where you have "button_OK") for all buttons that is part of window.
<Widget type="Button" skin="Button" position="160 5 107 26" name="Button">
<Property key="Widget_Caption" value="Ok"/>
<Property key="Event" value = "OK_Pressed"/>
</Widget>


Hi Altren.
Thanks a lot for the response. I was hoping that would work but it still is only taking the event for the "close" button which is part of the "WindowCSX" skin being used by the layout. Is this happening because I have specified the "Event" property key in a layout? I have added a button on the skin and that seems to work. However, the "Widget_Caption" property seems to not work in the skin files. I am hoping you will be able to shed some light into this. Thanks in advance.

Altren

06-06-2010 23:32:07

Right, I gave you a bit wrong answer - missed that you wrote about layout and not about skin.

In general skin and layout is a bit different things. We realised that they are so similar a bit too late, but we are going to make them same.
So properties for layouts doesn't work in skin and vice versa.
You should use Event in skin file and for caption you should read last post there : viewtopic.php?f=17&t=12706

globalsinner

08-06-2010 15:15:39

Hi,

Thanks for the clarification. That solved the issue I was having. It will be great if they are combined.