Page 1 of 1

GUI Button click

Posted: Thu Sep 03, 2009 5:51 pm
by kparamas
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;
}

Posted: Fri Sep 04, 2009 12:04 am
by zerochen
1. if you add a child to the window the position of the button, box, text is relative to the parent means:

Code: Select all

IGUIStaticText* text1 = guienv->addStaticText(L"Login Name :", core::rect<s32>(20, 100, 100, 120));
loginForm->addChild(text1); 
 
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

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: one id for one element!
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
}   
*/
} 
3.
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)
but it doesn t matter


i hope i could help you

Posted: Fri Sep 04, 2009 1:36 am
by Acki
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 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"); 
notice: one id for one element!
no, not necessary !!! ;)
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... ;)

Posted: Mon Sep 07, 2009 12:46 am
by kparamas
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")
{

Posted: Mon Sep 07, 2009 1:34 am
by CuteAlien
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.

Posted: Mon Sep 07, 2009 1:55 am
by Seven
possibly something like this

cout << "loginname = " << stringc(guienv->getRootGUIElement()->getElementFromId(GUI::GUI_LOGIN_BOX,true)->getText()).c_str() << endl;

Posted: Tue Sep 08, 2009 3:02 am
by kparamas
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
}

Posted: Tue Sep 08, 2009 4:32 pm
by CuteAlien
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.

Posted: Wed Sep 09, 2009 10:33 pm
by kparamas
Thanks. I ll put my code in tag in future.