Non-rectangular buttons

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Deldrith
Prole
Posts: 5
Joined: Fri Apr 08, 2016 6:48 pm

Non-rectangular buttons

Post by Deldrith » Sun May 01, 2016 12:29 am

What I am attempting to do is make it so you can interact with an object when the mouse is over its image. For example, if there was a saw in the map, when a player put their mouse over it it would display "saw" and they could interact with it by clicking on it. This would be quite easy to do if I were only working with circle or rectangles or other easily definably shaped objects. But what I want to do here is ignore cases when you are mousing over pixels of the image that are transparent, and only have the interaction with visible pixels. Is there a way to do this other than making a ridiculous image size by image size array for every single intractable object in the game?

User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: Non-rectangular buttons

Post by Kingdaro » Sun May 01, 2016 1:59 am

The second best way would probably be to use a bunch of smaller simple shapes, or a polygon. mlib does what you're probably looking for.

rhughes
Prole
Posts: 7
Joined: Thu Apr 28, 2016 3:12 am

Re: Non-rectangular buttons

Post by rhughes » Sun May 01, 2016 5:05 am

Here's what I would do.

First, I would have every object set up as a rectangle. When the mouse is at a certain position, I would get all the objects that I could be pointing at by testing to see if the cursor is in the rectangle. I would then sort these objects by their depth. Next, I would test the pixel value for each object's image at the location of the cursor. If it isn't transparent, I am on the object. If it is transparent, then I can proceed to the next object.

We can ignore the depth completely if we avoid having overlapping objects.

The question we should ask first is: 'What kind of resolution do we need here?' Do we need to know the exact pixel we're pointing at? Can we get away with a 'good enough' approach? Is a simple rectangle really not going to suffice?

Deldrith
Prole
Posts: 5
Joined: Fri Apr 08, 2016 6:48 pm

Re: Non-rectangular buttons

Post by Deldrith » Sun May 01, 2016 6:03 am

rhughes wrote:Here's what I would do.

First, I would have every object set up as a rectangle. When the mouse is at a certain position, I would get all the objects that I could be pointing at by testing to see if the cursor is in the rectangle. I would then sort these objects by their depth. Next, I would test the pixel value for each object's image at the location of the cursor. If it isn't transparent, I am on the object. If it is transparent, then I can proceed to the next object.

We can ignore the depth completely if we avoid having overlapping objects.

The question we should ask first is: 'What kind of resolution do we need here?' Do we need to know the exact pixel we're pointing at? Can we get away with a 'good enough' approach? Is a simple rectangle really not going to suffice?
There could be instances where there are multiple sprites close together or overlapping and you'd pretty much need pixel perfect accuracy to select one, unless it was done with layers or something, but that is unwieldy. How do you test pixel values for the images? I was looking at the documentation but did not see anything like that.

User avatar
deströyer
Prole
Posts: 27
Joined: Thu Jun 27, 2013 7:59 pm
Contact:

Re: Non-rectangular buttons

Post by deströyer » Sun May 01, 2016 6:27 am

https://love2d.org/wiki/ImageData:getPixel

Get the image you're pointing to and check the alpha-component of the pixel where your mouse is pointing in that image. It totally works, I've used it for an adventure-type game (that I didn't finish).

And as @rhughes suggests, you need to keep track of which order they are in / how they are layered in the scene.

Deldrith
Prole
Posts: 5
Joined: Fri Apr 08, 2016 6:48 pm

Re: Non-rectangular buttons

Post by Deldrith » Sun May 01, 2016 2:53 pm

deströyer wrote:https://love2d.org/wiki/ImageData:getPixel

Get the image you're pointing to and check the alpha-component of the pixel where your mouse is pointing in that image. It totally works, I've used it for an adventure-type game (that I didn't finish).

And as @rhughes suggests, you need to keep track of which order they are in / how they are layered in the scene.
Thank you guys very much. I had no idea this existed and couldn't find it. This does just what I want it to do.

User avatar
CaptainMaelstrom
Party member
Posts: 157
Joined: Sat Jan 05, 2013 10:38 pm

Re: Non-rectangular buttons

Post by CaptainMaelstrom » Mon May 02, 2016 11:04 pm

This all being said... whenever I see hotspots/buttons/ui interaction implemented this way in a game or program it tears me up. I feel like, as a user, I shouldn't have to aim directly onto the button I want to click, I should just have to be close.

Think about it like this: Imagine if in order to highlight text to copy it, you had to put your cursor exactly on a pixel of the character you wanted to select. It would be extremely frustrating to try and highlight several phrases in succession.

User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: Non-rectangular buttons

Post by Kingdaro » Mon May 02, 2016 11:51 pm

CaptainMaelstrom wrote:This all being said... whenever I see hotspots/buttons/ui interaction implemented this way in a game or program it tears me up. I feel like, as a user, I shouldn't have to aim directly onto the button I want to click, I should just have to be close.

Think about it like this: Imagine if in order to highlight text to copy it, you had to put your cursor exactly on a pixel of the character you wanted to select. It would be extremely frustrating to try and highlight several phrases in succession.
This is a pretty good point here. Highlighting elements based on proximity may be the best solution overall, all things considered.


Deldrith
Prole
Posts: 5
Joined: Fri Apr 08, 2016 6:48 pm

Re: Non-rectangular buttons

Post by Deldrith » Wed May 04, 2016 8:17 pm

CaptainMaelstrom wrote:This all being said... whenever I see hotspots/buttons/ui interaction implemented this way in a game or program it tears me up. I feel like, as a user, I shouldn't have to aim directly onto the button I want to click, I should just have to be close.

Think about it like this: Imagine if in order to highlight text to copy it, you had to put your cursor exactly on a pixel of the character you wanted to select. It would be extremely frustrating to try and highlight several phrases in succession.
I was aiming for a space station 13 style interaction, but maybe there is a better way to do it. This is definitely possible, but maybe it can be more user friendly? I'll give it some thought. Not sure though based on how objects can overlap and all that.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 11 guests