GUI Button click
GUI Button click
Hi,
I created a login form and added login and password edit boxes.
I have a couple of issues.
1. I have to check the user name and password on the editbox when I click "LOGIN" button. I dont know how to do the event receiver.
I tried this,
switch(event.GUIEvent.EventType)
{
case EGET_BUTTON_CLICKED:
{
IGUIButton* button = (IGUIButton*)event.GUIEvent.Caller;
if(button->getID() == 1)
buttonPressed = true;
}
......
But this doesnt work.
2. I added all the elements as a child to the loginForm.
But still If I move the dialog box the elements(editbox, button) disappears.
Below is my code, how I add the controls.
if(displayLoginForm)
{
loginForm = guienv->addWindow(core::rect<s32>(windowWidth/2, windowHeight/2, windowWidth/2 + 400, windowHeight/2 + 250), true, L"Login Form");
IGUIStaticText* text1 = guienv->addStaticText(L"Login Name :", core::rect<s32>(windowWidth/2 + 20, windowHeight/2 + 100, windowWidth/2 + 100, windowHeight/2 + 120));
IGUIStaticText* text2 = guienv->addStaticText(L"Password :", core::rect<s32>(windowWidth/2 + 20, windowHeight/2 + 150, windowWidth/2 + 100, windowHeight/2 + 170));
loginForm->addChild(text1);
loginForm->addChild(text2);
editBox = guienv->addEditBox(L"", core::rect<s32>(windowWidth/2 + 150, windowHeight/2 + 100, windowWidth/2 + 300, windowHeight/2 + 120),
true, 0, 1);
loginForm->addChild(editBox);
editBox = guienv->addEditBox(L"", core::rect<s32>(windowWidth/2 + 150, windowHeight/2 + 150, windowWidth/2 + 300, windowHeight/2 + 170),
true, 0, 2);
editBox->setPasswordBox(true);
loginForm->addChild(editBox);
IGUIButton* button1 = guienv->addButton(core::rect<s32>(windowWidth/2 + 150, windowHeight/2 + 180, windowWidth/2 + 200, windowHeight/2 + 200),
0, 1, L"LOGIN");
loginForm->addChild(button1);
displayLoginForm = !displayLoginForm;
}
I created a login form and added login and password edit boxes.
I have a couple of issues.
1. I have to check the user name and password on the editbox when I click "LOGIN" button. I dont know how to do the event receiver.
I tried this,
switch(event.GUIEvent.EventType)
{
case EGET_BUTTON_CLICKED:
{
IGUIButton* button = (IGUIButton*)event.GUIEvent.Caller;
if(button->getID() == 1)
buttonPressed = true;
}
......
But this doesnt work.
2. I added all the elements as a child to the loginForm.
But still If I move the dialog box the elements(editbox, button) disappears.
Below is my code, how I add the controls.
if(displayLoginForm)
{
loginForm = guienv->addWindow(core::rect<s32>(windowWidth/2, windowHeight/2, windowWidth/2 + 400, windowHeight/2 + 250), true, L"Login Form");
IGUIStaticText* text1 = guienv->addStaticText(L"Login Name :", core::rect<s32>(windowWidth/2 + 20, windowHeight/2 + 100, windowWidth/2 + 100, windowHeight/2 + 120));
IGUIStaticText* text2 = guienv->addStaticText(L"Password :", core::rect<s32>(windowWidth/2 + 20, windowHeight/2 + 150, windowWidth/2 + 100, windowHeight/2 + 170));
loginForm->addChild(text1);
loginForm->addChild(text2);
editBox = guienv->addEditBox(L"", core::rect<s32>(windowWidth/2 + 150, windowHeight/2 + 100, windowWidth/2 + 300, windowHeight/2 + 120),
true, 0, 1);
loginForm->addChild(editBox);
editBox = guienv->addEditBox(L"", core::rect<s32>(windowWidth/2 + 150, windowHeight/2 + 150, windowWidth/2 + 300, windowHeight/2 + 170),
true, 0, 2);
editBox->setPasswordBox(true);
loginForm->addChild(editBox);
IGUIButton* button1 = guienv->addButton(core::rect<s32>(windowWidth/2 + 150, windowHeight/2 + 180, windowWidth/2 + 200, windowHeight/2 + 200),
0, 1, L"LOGIN");
loginForm->addChild(button1);
displayLoginForm = !displayLoginForm;
}
1. if you add a child to the window the position of the button, box, text is relative to the parent means:
the text is 20 px right from the beginning of the window now...
2. the event receiver is correct
but you gave some elements the same id
make a enum with the elements and use them as id
something like this
notice: one id for one element!
now you can handle the event
3.
do you know that the window isnt in the middle of the screen?
but it doesn t matter
i hope i could help you
Code: Select all
IGUIStaticText* text1 = guienv->addStaticText(L"Login Name :", core::rect<s32>(20, 100, 100, 120));
loginForm->addChild(text1);
2. the event receiver is correct
but you gave some elements the same id
make a enum with the elements and use them as id
something like this
Code: Select all
enum GUI
{
GUI_LOGIN_BUTTON = 1,
GUI_LOGIN_BOX =11,
GUI_PW_BOX = 12
}
guienv->addEditBox(L"", core::rect<s32>(windowWidth/2 + 150, windowHeight/2 + 100, windowWidth/2 + 300, windowHeight/2 + 120),
true, win, GUI_LOGIN_BOX);
guienv->addButton(core::rect<s32>(windowWidth/2 + 150, windowHeight/2 + 180, windowWidth/2 + 200, windowHeight/2 + 200),
0, GUI_LOGIN_BUTTON , L"LOGIN");
now you can handle the event
Code: Select all
case EGET_BUTTON_CLICKED:
{
if(event.GUIEvent.Caller->getID() == 1) //or better button->getID() == GUI_LOGIN_BUTTON
buttonPressed = true; //or better
/*
if(guienv->getRootGUIElement()->getElementFromId(GUI_LOGIN_BOX,true)->getText()==L"loginname" && guienv->getRootGUIElement()->getElementFromId(GUI_PW_BOX,true)->getText()==L"pw")
{
//login sucess
}
else
{
//error
}
*/
}
do you know that the window isnt in the middle of the screen?
Code: Select all
core::rect<s32>(windowWidth/2-200, windowHeight/2-125, windowWidth/2 + 200, windowHeight/2 + 125)
i hope i could help you
no, not necessary !!!zerochen wrote:2. the event receiver is correct
but you gave some elements the same id
make a enum with the elements and use them as id
something like thisnotice: one id for one element!Code: Select all
enum GUI { GUI_LOGIN_BUTTON = 1, GUI_LOGIN_BOX =11, GUI_PW_BOX = 12 } guienv->addEditBox(L"", core::rect<s32>(windowWidth/2 + 150, windowHeight/2 + 100, windowWidth/2 + 300, windowHeight/2 + 120), true, win, GUI_LOGIN_BOX); guienv->addButton(core::rect<s32>(windowWidth/2 + 150, windowHeight/2 + 180, windowWidth/2 + 200, windowHeight/2 + 200), 0, GUI_LOGIN_BUTTON , L"LOGIN");
notice he has just one button with this ID and he checks if a button click event was raised and then he checks if this button has this ID, so no problem here...
but yes, it can lead to troubles, e.g. if he tries to search the other element by its ID, but even then he can check if the element type is the desired one and then it's not a problem again...
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Thanks a lot. The event is being caught it works perfectly.
But still I could not evaluate the text in the login and password box.
I tried to print what getText() returns and it always returns
loginname = 049BF9D0 and the check fails.
I entered "loginname" and "pw" in the login and password boxes resp.
cout << "loginname = " << guienv->getRootGUIElement()->getElementFromId(GUI::GUI_LOGIN_BOX,true)->getText() << endl;
if(guienv->getRootGUIElement()->getElementFromId(GUI::GUI_LOGIN_BOX, true)->getText()== L"loginname" &&
guienv->getRootGUIElement()->getElementFromId(GUI::GUI_PW_BOX, true)->getText() == L"pw")
{
But still I could not evaluate the text in the login and password box.
I tried to print what getText() returns and it always returns
loginname = 049BF9D0 and the check fails.
I entered "loginname" and "pw" in the login and password boxes resp.
cout << "loginname = " << guienv->getRootGUIElement()->getElementFromId(GUI::GUI_LOGIN_BOX,true)->getText() << endl;
if(guienv->getRootGUIElement()->getElementFromId(GUI::GUI_LOGIN_BOX, true)->getText()== L"loginname" &&
guienv->getRootGUIElement()->getElementFromId(GUI::GUI_PW_BOX, true)->getText() == L"pw")
{
In short: wchar_t* strings do not work as you think they do.
First cout can't handle them - you either have to use wcout or convert it to a char*.
And then you can't use == to compare c-strings. That just compares the pointer-addresses. Easiest way would be to convert it to some string-class like irr::stringw which offers such a comparison.
First cout can't handle them - you either have to use wcout or convert it to a char*.
And then you can't use == to compare c-strings. That just compares the pointer-addresses. Easiest way would be to convert it to some string-class like irr::stringw which offers such a comparison.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
I did the comparison like this and it works. Thanks all for the help.
stringw login = guienv->getRootGUIElement()->getElementFromId(GUI::GUI_LOGIN_BOX, true)->getText();
stringw pwd = guienv->getRootGUIElement()->getElementFromId(GUI::GUI_PW_BOX, true)->getText();
if(login == L"loginname" &&
pwd == L"pw")
{
//login sucess
cout << "login success " << endl;
loginSuccess = true;
}
else
{
cout << "login error " << endl;
loginSuccess = false;
//error
}
stringw login = guienv->getRootGUIElement()->getElementFromId(GUI::GUI_LOGIN_BOX, true)->getText();
stringw pwd = guienv->getRootGUIElement()->getElementFromId(GUI::GUI_PW_BOX, true)->getText();
if(login == L"loginname" &&
pwd == L"pw")
{
//login sucess
cout << "login success " << endl;
loginSuccess = true;
}
else
{
cout << "login error " << endl;
loginSuccess = false;
//error
}
One more hint - the forum has a code tag to mark code in your posts. Just check the buttons above the box where you enter your messages. It makes reading code a lot easier.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm