GUI events, button click when child of parent window...

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
docWild
Posts: 38
Joined: Wed Nov 30, 2011 4:29 pm

GUI events, button click when child of parent window...

Post by docWild »

I have an odd problem:

Code: Select all

    IGUIButton* button;
    m_pGUINavigationWindow = m_pGUIEnvironment->addWindow(rect<s32>(0,0,220,415),false,0,0,GUI_NAV_WINDOW);
    m_pGUINavigationWindow->setVisible(false);
    m_pGUINavigationWindow->setDrawTitlebar(false);
    m_pGUINavigationWindow->getCloseButton()->setVisible(false);
    m_pGUIEnvironment->addImage(m_pDriver->getTexture("media/navigation.png"),position2di(2,7),true,m_pGUINavigationWindow);
 
    button = m_pGUIEnvironment->addButton (rect<s32>(10,60,100,90),m_pGUINavigationWindow, GT_ALLSTOP, L"Stop", L"Auto-stop Craft");
    button->setPressedImage(m_pDriver->getTexture("media/red.png"));
    button->setImage(m_pDriver->getTexture("media/blue.png"));
    button->setUseAlphaChannel(true);
    button->setDrawBorder(0);
So i've got a button on a parent window. Here is the event receiver:

Code: Select all

bool CGameManager::OnEvent(const SEvent& event)
{
    if (!m_pDriver)
        return false;
 
    if (event.EventType == EET_KEY_INPUT_EVENT)
    {
 
        events.m_bKeys[event.KeyInput.Key] = event.KeyInput.PressedDown;
//        std::cout << "Key: " << event.KeyInput.Key << std::endl;
//            std::cout << "Char: " << char(event.KeyInput.Char) << std::endl;
 
    }
    if (event.EventType == EET_GUI_EVENT)
    {
        s32 id = event.GUIEvent.Caller->getID();
        IGUIEnvironment* env = m_pDevice->getGUIEnvironment();
 
 
 
        switch (event.GUIEvent.EventType)
        {
 
        case EGET_ELEMENT_FOCUSED:
        {
//
            env->setFocus(0);
//                return true;
            break;
        }
        case EGET_BUTTON_CLICKED:
 
        {
 
 
            events.guiButtons[id] = true;
            if(id == GT_ALLSTOP)
        {
            printf("tst\n");
        }
            env->setFocus(0);
            break;
        }
        case EGET_CHECKBOX_CHANGED:
        {
//            IGUIElement* root = env->getRootGUIElement();
//            IGUICheckBox* checkBox = (IGUICheckBox*)root->getElementFromId(id,true);
            events.checkBoxId = id;
 
            env->setFocus(0);
            break;
        }
        case EGET_LISTBOX_CHANGED:
        {
 
 
            IGUIElement* root = env->getRootGUIElement();
            IGUIListBox* lBox = (IGUIListBox*)root->getElementFromId(id,true);
            if(lBox)
            {
                events.listBoxItem = lBox->getSelected();
                events.listBoxEvent = true;
                events.listBoxText = lBox->getListItem(events.listBoxItem );
//            events.listBoxText = events.listBoxText.c_str();
                env->setFocus(0);
            }
            break;
        }
        default:
        {
 
            break;
        }
 
 
        }
    }
    if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
    {
        switch (event.MouseInput.Event)
        {
        case EMIE_LMOUSE_PRESSED_DOWN:
            events.MouseState.LeftButtonDown = true;
            break;
 
        case EMIE_LMOUSE_LEFT_UP:
            events.MouseState.LeftButtonDown = false;
            break;
 
        case EMIE_MOUSE_MOVED:
            events.MouseState.Position.X = event.MouseInput.X;
            events.MouseState.Position.Y = event.MouseInput.Y;
            break;
 
        default:
            // We won't use the wheel
 
            break;
        }
    }
    return false;
}
The problem is that clicking on my child button does not seem to raise an EGET_BUTTON_CLICKED. I can activate the button with a keyboard shortcut, and once it is active I can then click it raising the proper EGET_BUTTON_CLICKED so it seems to be a focus issue on the face of it.

Now the odd part. The above is basically a carbon copy of another window, with child buttons and they work perfectly.

Code: Select all

void Gui::targetingWindow(IVideoDriver* m_pDriver)
{
 
    IGUIButton* button;
    m_pGUITargetingWindow = m_pGUIEnvironment->addWindow(rect<s32>(0,0,220,415),false,0,0,GUI_TARG_WINDOW);
    m_pGUITargetingWindow->setVisible(false);
    m_pGUITargetingWindow->setDrawTitlebar(false);
    m_pGUITargetingWindow->getCloseButton()->setVisible(false);
    m_pGUIEnvironment->addImage(m_pDriver->getTexture("media/targeting.png"),position2di(2,7),true,m_pGUITargetingWindow);
 
    m_pGUIListBox = m_pGUIEnvironment->addListBox(rect<s32>(10,60,200,300),m_pGUITargetingWindow,GUI_TARGET_LIST,true);
    m_pGUIListBox->setAutoScrollEnabled(true);
 
 
    button = m_pGUIEnvironment->addButton (rect<s32>(10,310,100,340),m_pGUITargetingWindow, GT_TRACKTARGET, L"Track", L"Track Target");
    button->setPressedImage(m_pDriver->getTexture("media/red.png"));
    button->setImage(m_pDriver->getTexture("media/blue.png"));
    button->setUseAlphaChannel(true);
    button->setDrawBorder(0);
 
 
...
 
 
blahblahblah
I'm probably missing something stupid but if anyone has the time to take a look I'd be very much appreciative. Here is the link to the full source with codeblocks project (probably won't run on windows as is) http://www.box.com/s/6totzn31nkc4xix2fio0. The event receiver is in CGameManager and the buttons and windows are intitialised in the Gui constructor. If I convert these buttons to toolbar buttons, then they work as they should. This may be a bug.. but I'd rather assume it's my mistake. I'm using 1.72.

Many thanks.
docWild
Posts: 38
Joined: Wed Nov 30, 2011 4:29 pm

Re: GUI events, button click when child of parent window...

Post by docWild »

Umm... sorry but it isn't a problem anymore.

Turns out it was a P.I.C.N.I.C error, as suspected. Was staring at it for three hours and as soon as I gave in and asked for help it occured to me. I'd left a switch in another file which was cancelling my button press.

Hopefully I've come forward before anyone wastes their time on this.
Post Reply