Listbox : MouseReleased only works when starting clicking in the listbox

Started by r4gTime, 26 November 2020, 19:34:32

r4gTime

Hello,

I'm trying to code something where you can drag an object and drop it on a listbox, and it does something depending on the item index where the object is dropped.

Problem is, the signal ListBox::MouseReleased doesn't work if the start of your click is not in the list box.

I thought maybe it needed to be focused, so I made a MouseEntered signal for the list box and bound  it to focusing this listbox, but it's still doesn't work.

If anyone has an idea, thank you for your help !

texus

MouseReleased in ListBox wasn't made for this. It was added so that you could know when selecting an item end (since you can hold down the mouse button and move to select a different item).

Dragging an object on top of another is not really something supported in TGUI. You can drag things (e.g. the thumb of a slider), but until you drop it all events to other widgets are ignored. Since you need special code for dragging anyway, you might as well add some extra code to handle dropping it on the right item. I would just add the following code before the gui.handleEvent call to simulate a mouse down event before processing the mouse release:
Code (cpp) Select
if (dragging && (event.type == sf::Event::MouseButtonReleased)
&& (gui.getWidgetBelowMouseCursor({event.mouseButton.x, event.mouseButton.y}) == listBox))
{
    sf::Event mouseDownEvent = event;
    event.type = sf::Event::MouseButtonPressed;
    gui.handleEvent(mouseDownEvent);
}

r4gTime

Hey Texus,

I guess you meant :
Code (cpp) Select
mouseDownEvent.type = sf::Event::MouseButtonPressed;

thanks you very much for your help, it works like a charm.(except I can't use this wonderful 0-9 function and had to get another way to know if my cursor was on the widget ;) )

texus

Yes, I meant mouseDownEvent instead of event.

The getWidgetBelowMouseCursor was actually added to 0.8 as well, but its currently only in the 0.8 branch on github (which will become 0.8.9).