Text focus bug when disabled?

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
Quibbler
Posts: 25
Joined: Tue Feb 14, 2006 5:34 pm

Text focus bug when disabled?

Post by Quibbler »

In my gui environment I have added a static text label and immediately after creation I call setEnabled(false).

The reason I call setEnabled(false) is because I want my text to not receive focus when accidentally clicking inside the clipping rectangle.

However, even when it is disabled it receives focus when clicking on it and after that I can't do anything else (the GUI freezes).

The only workaround I have is to set the focus to the latest focused element, whenever the static text label receives focus.

What is going on here?

Below is my event receiver class with workaround code added:

Code: Select all

class CGUIEventReceiver : public IEventReceiver
{
private:
	IrrlichtDevice *device;
	CGUIGrid *the_grid;
	IGUIStaticText *debugTxt;
	IGUIStaticText *debugTxt2;
	IGUIElement *lastFocus;
	wchar_t debugBuf[100];
	wchar_t debugBuf2[100];
public:
	CGUIEventReceiver() : device(0), lastFocus(0) { }
	CGUIEventReceiver(IrrlichtDevice *d) : device(d) { }
	void setDevice(IrrlichtDevice *d) { device = d; }
	void registerGrid(CGUIGrid *g) { the_grid = g; }
	void registerDebugLabel(IGUIStaticText *t) { debugTxt = t; }
	void registerDebugLabel2(IGUIStaticText *t) { debugTxt2 = t; }


	virtual bool OnEvent(SEvent event) {
		bool eventprocessed = true;

		if (event.EventType == EET_GUI_EVENT) {
				s32 id = event.GUIEvent.Caller->getID();

				/* workaround for static text focus problem */
				if (id == 100 || id == 101) {
					if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUSED && lastFocus)
						device->getGUIEnvironment()->setFocus(lastFocus);
					swprintf(debugBuf, 50, L"Event: %d ID: %d", event.GUIEvent.EventType, id);
					debugTxt->setText(debugBuf);
					return false;
				}
				/* END workaround */
				switch(event.GUIEvent.EventType)
				{
				case EGET_SCROLL_BAR_CHANGED:
					static s32 posX, posY, dx, dy;
					if (id == 104) {
						dx = posX = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
						posX = the_grid->getSnapX()*100 - posX;
						the_grid->setOffsetX(posX % the_grid->getSnapX());
						lastFocus = device->getGUIEnvironment()->getFocus();
					}
					if (id == 105) {
						dy = posY = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
						posY = the_grid->getSnapY()*100 - posY;
						the_grid->setOffsetY(posY % the_grid->getSnapY());
						lastFocus = device->getGUIEnvironment()->getFocus();
					}
					swprintf(debugBuf, 50, L"X: %d  Y: %d", dx, dy);
					swprintf(debugBuf2, 50, L"Event: %d ID: %d", event.GUIEvent.EventType, id);
					debugTxt->setText(debugBuf);
					debugTxt2->setText(debugBuf2);
					eventprocessed = true;
				break;
				case EGET_BUTTON_CLICKED:
					eventprocessed = true;
				break;
				case EGET_ELEMENT_HOVERED:
					eventprocessed = true;
				break;
				case EGET_ELEMENT_LEFT:
					eventprocessed = true;
				break;
				case EGET_ELEMENT_FOCUS_LOST:

					eventprocessed = true;
				break;
				default:
					swprintf(debugBuf2, 50, L"Other: %d ID: %d", event.GUIEvent.EventType, id);
					debugTxt2->setText(debugBuf2);
					return false;
			   }
		}
		if (event.EventType == EET_MOUSE_INPUT_EVENT) {
			 return true;
		}

	}
};
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

a static text box can get focus ??? :shock:

I tried it with 2 static text boxes and a button and the text boxes don't get focus, neither with setEnabled(true) nor setEnabled(false) and the button works in all cases !!!
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Quibbler
Posts: 25
Joined: Tue Feb 14, 2006 5:34 pm

Post by Quibbler »

Well, at least I get the EGET_ELEMENT_FOCUSED for the ID of my text.

I am also running Irrlicht inside a Win32 window.
Could that be why I get this strange behaviour?

What version of Irrlicht are you running? I run the 1.3.1 version.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

1.3.1 has focus problems. If you are just starting with a project I would recommend using the 1.4 beta already.
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
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

I also use 1.3.1, but I realy can't reproduce this !!! :shock:
I tried this simple test code:

Code: Select all

#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

class CGUIEventReceiver : public IEventReceiver{
   virtual bool OnEvent(SEvent event) {
      if (event.EventType == EET_GUI_EVENT) {
            s32 id = event.GUIEvent.Caller->getID();
            if (id == 100 || id == 101) {
               if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUSED)
                  printf("hhhh\n");
            }
      }
      return false;
   }
};

int main(){
  CGUIEventReceiver mrc;

	IrrlichtDevice *device = createDevice(EDT_OPENGL, dimension2d<s32>(640, 480), 16, false, false, false, &mrc);
	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	IGUIEnvironment* guienv = device->getGUIEnvironment();
	device->setWindowCaption(L"Irrlicht alle Treiber (dynamic no sound)");

  guienv->addStaticText(L"hahaha", rect<s32>(10,10,200,40), true, true, 0, 100)->setEnabled(false);
  guienv->addStaticText(L"huhuhu", rect<s32>(10,50,200,80), true, true, 0, 101)->setEnabled(false);
  guienv->addButton(rect<s32>(10,100,200,140));

	// setup and run Irrlicht render loop
  smgr->addCameraSceneNode();
	while(device->run()){
		driver->beginScene(true, true, video::SColor(0,222,222,0));
		smgr->drawAll();
		guienv->drawAll();
		driver->endScene();

	}
	device->drop();
	return 0;
}
and it doesn't react on any clicks on both static text boxes and also the button works perfectly !!!
and just to be sure, do you realy add a static text box ???

but maybe this could be your problem:

Code: Select all

      if (event.EventType == EET_MOUSE_INPUT_EVENT) {
          return true;
      }
this returns true for all mouse inputs you ever do !!!
try it without this lines and return false at the end of the receiver !!!
always remember an event receiver should always return false if it didn't handle an event (what your receiver doesn't) !!! ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Acki wrote: always remember an event receiver should always return false if it didn't handle an event (what your receiver doesn't) !!! ;)
lol Acki :) (edit: just laughing because it reminds me of another thread, so no-one get confused plz ;-) )

I didn't try this case, but I know that in 1.3.1 something with the focus in statics went wrong. This can easy reproduced in example 05 when you click on the textfield a few times (maybe you need to click other elements between). It's fixed in svn since a while and I really, really wish there would've been a 1.3.2 release afterwards, as a few people stumpled upon this bug already and it's rather serious.
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
Quibbler
Posts: 25
Joined: Tue Feb 14, 2006 5:34 pm

Post by Quibbler »

I upgraded to latest beta so now it works. Thanks!
Post Reply