Changing cursor to I-beam when hovering over an edit box

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Reiko
Posts: 105
Joined: Sun Aug 16, 2009 7:06 am
Location: Australia

Changing cursor to I-beam when hovering over an edit box

Post by Reiko »

This will make the mouse cursor behaviour with edit boxes more like the behaviour in your operating system/browser, so when the user of your application/game mouses over an edit box, they'll be like "oh I can type here".

How it works: If the element moused over is an edit box, the cursor will change to an I-beam. If it's moved away from one, it will change back to a normal arrow.

"device" is a pointer to your Irrlicht device. I'll let you handle the event receiver having access to this pointer yourself.

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(const SEvent& event)
	{
		if (event.EventType == EET_GUI_EVENT)
		{
			if (event.GUIEvent.EventType == EGET_ELEMENT_HOVERED)
			{
				// Set cursor to an I-Beam if hovering over an edit box
				if (event.GUIEvent.Caller->getType() == EGUIET_EDIT_BOX)
				{
					device->getCursorControl()->setActiveIcon(ECI_IBEAM);
				}
			}
			else if (event.GUIEvent.EventType == EGET_ELEMENT_LEFT)
			{
				// Set cursor to normal if left an edit box
				if (event.GUIEvent.Caller->getType() == EGUIET_EDIT_BOX)
				{
					device->getCursorControl()->setActiveIcon(ECI_NORMAL);
				}
			}
		}
	}
}
Not 100% sure if this is fail safe (ie. if the event receiver will always know that an edit box was 'left'). If it isnt, just remove the EGET_ELEMENT_LEFT part and put the line with ECI_NORMAL as an 'else' after the if in EGET_ELEMENT_HOVERED. Though, this will cause the cursor to be set to normal every time you hover over any gui element.


Other cursors in the Irrlicht engine if interested:

irr::gui::ECURSOR_ICON from ICursorControl.h

Code: Select all

	//! Default icons for cursors
	enum ECURSOR_ICON
	{
		// Following cursors might be system specific, or might use an Irrlicht icon-set. No guarantees so far.
		ECI_NORMAL,		// arrow
		ECI_CROSS,		// Crosshair
		ECI_HAND, 		// Hand
		ECI_HELP,		// Arrow and question mark
		ECI_IBEAM,		// typical text-selection cursor
		ECI_NO, 		// should not click icon
		ECI_WAIT, 		// hourclass
		ECI_SIZEALL,  	// arrow in all directions
		ECI_SIZENESW,	// resizes in direction north-east or south-west
		ECI_SIZENWSE, 	// resizes in direction north-west or south-east
		ECI_SIZENS, 	// resizes in direction north or south
		ECI_SIZEWE, 	// resizes in direction west or east
		ECI_UP,			// up-arrow

		// Implementer note: Should we add system specific cursors, which use guaranteed the system icons,
		// then I would recommend using a naming scheme like ECI_W32_CROSS, ECI_X11_CROSSHAIR and adding those
		// additionally.

		ECI_COUNT		// maximal of defined cursors. Note that higher values can be created at runtime
	};
Post Reply