Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Check if hovering over a visible part of a sprite  (Read 2878 times)

0 Members and 1 Guest are viewing this topic.

PneumaOfficial

  • Newbie
  • *
  • Posts: 1
    • View Profile
Check if hovering over a visible part of a sprite
« on: June 01, 2020, 10:16:56 am »
I'm trying to work on a ui library for my game, and I need to make sure that the buttons are not under other objects, to invoke their events. Any way I can go about this?

SuperApe

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Check if hovering over a visible part of a sprite
« Reply #1 on: June 02, 2020, 12:13:21 am »
I totally just did this for my project. A hover behavior looks nice. Maybe I can help.

I'm trying to work on a ui library for my game, and I need to make sure that the buttons are not under other objects, to invoke their events. Any way I can go about this?
What your describing sounds like "consuming click / hover events". Yeah? You want to make sure the button behavior is the only effect of the cursor, while underlying objects that may react to the cursor do not react while the button takes over. Do I have that right?

As you may already guess, hover behavior is a function of comparing the mouse position to the bounds of the button sprite. This is fairly straightforward, so I'm guessing the part you're asking about is more, "how do I stop things behind the button level from being hovered/clicked, etc?"

If that's the real question, here's how I solved that. There's more than two or three states to my clickable buttons. They (and all UI Elements in my system) have Disabled, Normal, Hover and Active. (I actually use an enum, and use this to manage drawing as well as behavior with regard to mouse / cursor input)

So, a Disabled button (or other element that takes a hover or click) simply ignores the cursor, I just bypass behavior altogether, and draw semi-transparent on draw. A Normal button, however, _can_ accept the cursor position as to switch to Hover state, a click at that point to Active state.

And then, the trick is to manage the states of the underlying buttons and hover-able objects. In other words, there will be those UI buttons that are active as within one dialog box or UI panel, while those under are switched to a Disabled state while this set of buttons is visible. (incidentally, my system also makes use of visible and active states to use in bypassing update and draw methods)

If what you're asking is, "Can SFML just detect if the sprite is drawn but covered up by another button, so it doesn't consume a hover or click?", I'm saying, the answer is likely no, and I made that behavior myself, just managing which levels of interactive buttons are Disabled, and which are not.

'Hope that helps.