PDA

View Full Version : Non-rectangular buttons



Daniel Benoy
08-11-2010, 07:24 PM
Hi. I just thought of a feature idea.

ClanLib has collision support which can detect whether a point is contained within an arbitrarily shaped object, and it can get the object outlines from images.

ClanLib also supports GUI push buttons which seem to support images.

Maybe CL_PushButton could be given an option to render completely as an image and ignore clicks that occur in the transparent areas of that image?

Daniel Benoy
08-11-2010, 08:08 PM
Hm... I just had another idea of how I can do this in my project without having to mess with ClanLib.

i.e.



class PushButtonLimited : public CL_PushButton
{
PushButtonLimited(parent, CL_CollisionOutline ClicksDontWorkOutsideOfThis);
on_process_message();
}

PushButtonLimited : CL_PushButton()
{
membervar_old_callback = func_process_message();
func_process_message.set(this, &PushButtonLimited::on_process_message());
}

PushButtonLimited::on_process_message()
{
if (inside outline)
{
membervar_old_callback.invoke()
}
// Otherwise, no dice!
}


Maybe that could be made generic with a template as well.

Daniel Benoy
08-11-2010, 08:47 PM
Maybe someone should add a get_outline and set_outline in CL_GUIComponent, and change CL_GUIComponent::get_component_at(point) to only return points within the component outlines :crazy:

They'd still be able to draw outside of those bounds, but as far as mouse clicks are concerned the component would end where the outline ends.

rombust
08-11-2010, 10:16 PM
Interesting idea.

Hopefully it can be implemented as an example.

I thought the GUI component could pass down mouse clicks to the parent, so you can ignore "invalid presses". But I am not sure.

Daniel Benoy
08-12-2010, 01:06 PM
That would be a good way to solve this sort of problem as well, but from what I can tell by reading the code, even if a click isn't consumed by a message handler that's just the end of it. It looks up the component by the mouse position, and returns that, then sends that component the click.

rombust
08-12-2010, 05:48 PM
Also:


ClanLib has collision support which can detect whether a point is contained within an arbitrarily shaped object, and it can get the object outlines from images.

I think it would be easier, not using ClanLib's collision.

Instead, have your own push button contain the CL_PixelBuffer of the image.
(i.e. CL_PixelBuffer pixelbuffer("myimage.png"); CL_Image image(gc, pixelbuffer));

Next translate the mouse coordinates into the x,y position inside the pixelbuffer.

Then use the following code:


CL_Colorf color = pixelbuffer.get_pixel(xpos, ypos);
if (color.a < 0.2f) // (You may need to adjust this alpha threshold)
{
ignore_mouse_press
}
else
{
button_was_pressed
}

Daniel Benoy
08-12-2010, 06:01 PM
Ah! Good advice, thanks :D