GUI Button click

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
kparamas
Posts: 17
Joined: Fri Jun 26, 2009 10:00 pm

GUI Button click

Post 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;
}
zerochen
Posts: 273
Joined: Wed Jan 07, 2009 1:17 am
Location: Germany

Post 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
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post 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... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
kparamas
Posts: 17
Joined: Fri Jun 26, 2009 10:00 pm

Post 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")
{
CuteAlien
Admin
Posts: 9716
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post 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.
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
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

possibly something like this

cout << "loginname = " << stringc(guienv->getRootGUIElement()->getElementFromId(GUI::GUI_LOGIN_BOX,true)->getText()).c_str() << endl;
kparamas
Posts: 17
Joined: Fri Jun 26, 2009 10:00 pm

Post 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
}
CuteAlien
Admin
Posts: 9716
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post 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.
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
kparamas
Posts: 17
Joined: Fri Jun 26, 2009 10:00 pm

Post by kparamas »

Thanks. I ll put my code in tag in future.
Post Reply