clicking a 2d image... how to?

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
unreal3149
Posts: 7
Joined: Sun Mar 02, 2008 11:22 pm

clicking a 2d image... how to?

Post by unreal3149 »

Hi. I would just like to know how to be able to click a rendered 2D image to get the window to close (for example). I'm guessing I need to create some sort of box around my texture and when the mouse clicks it something executes (like the window closing, for example). I'm new and would just like some help. Could someone post an example or something for me on how to do this? Muchly appreciated.
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

Check and see if the mouse is inside the rectangle.

To do this you'll need :
The mouse position,
the rectangle to be clicked,
an event receiver

I'm trying to help without giving away the code :P
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

How about using a button? Buttons can use textures, and an event is generated that indicates it has been clicked.

Travis
unreal3149
Posts: 7
Joined: Sun Mar 02, 2008 11:22 pm

Post by unreal3149 »

How would you go about giving a button a texture? Code is appreciated.
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Post by Ion Dune »

http://irrlicht.sourceforge.net/docu/cl ... on.html#a6

Has code plus everything else you may need regarding buttons.[/url]
unreal3149
Posts: 7
Joined: Sun Mar 02, 2008 11:22 pm

Post by unreal3149 »

monkeycracks wrote:Check and see if the mouse is inside the rectangle.

To do this you'll need :
The mouse position,
the rectangle to be clicked,
an event receiver

I'm trying to help without giving away the code :P
I can do an event receiver but what would the 'get mouse position' and see if 'mouse is in rectangle' code look like? Sorry i'm new..
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

unreal3149 wrote:
monkeycracks wrote:Check and see if the mouse is inside the rectangle.

To do this you'll need :
The mouse position,
the rectangle to be clicked,
an event receiver

I'm trying to help without giving away the code :P
I can do an event receiver but what would the 'get mouse position' and see if 'mouse is in rectangle' code look like? Sorry i'm new..
Don't recreate the wheel by coding a IGUIButton yourself. Just use the IGUIButton provided by Irrlicht. Vitek clearly, and politely, pointed that out to you, and I hope you take his advice. If you don't know what I mean, then use the following line of code and look it up in the API.

Code: Select all

IGUIButton* myButton = guienv->addButton(...)
TheQuestion = 2B || !2B
FuzzYspo0N
Posts: 914
Joined: Fri Aug 03, 2007 12:43 pm
Location: South Africa
Contact:

Post by FuzzYspo0N »

Code: Select all

bool g_sprite::isMouseInside(core::position2di mousepos)
{
	bool answer = false;

	core::rect<s32> spriteRect;
	spriteRect.UpperLeftCorner.X = this->pos.X;
	spriteRect.UpperLeftCorner.Y = this->pos.Y;
	spriteRect.LowerRightCorner.X = this->pos.X  + this->size.Width;
	spriteRect.LowerRightCorner.Y = this->pos.Y  + this->size.Height;

	answer = spriteRect.isPointInside(mousepos);

	return answer;
}
the size : comes from

Code: Select all

this->size = texture->getOriginalSize(); 
The ITexture gives you this. The position X and Y is self explanatory...

But, as the IGUIImage is an IGUIElement, it has the following :

Code: Select all

	if(event.EventType == EET_GUI_EVENT)
	{
		if(event.GUIEvent.EventType == EGET_ELEMENT_HOVERED)
		{
			if(event.GUIEvent.Caller->getID() == 1001)
			{
				//change the image to the mouse overed one? 
			}
		}
	}
This means you can put the above code inside the mouse click check!
This is easier by making a click boolean variable. Then inside THIS check you can say if clicked == true, do whatever. its harder doing code for clicks without a class!, so try getting that right.

If people are struggling with this, there is http://old.owned.co.za to look at, i released it here on the forums too, its jsut a test and has mouse over as well as a click class, its all that you need i think. hope it helps
Post Reply