How can I make a scrollBar react to mouse wheel ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mbenoni
:warning: Old Version Published before Godot 3 was released.

Hello everybody !

Today, another question about a thing I didn’t find anywhere : the VScrollBar interaction with the mouse wheel.

I explain myself:

I’ve the following architecture :

-Control
– Panel1
— Panel2
---- GridContainer
---- VScrollBar

My Grid Container contains some panels/Sprites, and is restricted to the Panel2 zone.
My ScrollBar, by script, display some elements in the GridContainer when his value is changed.
It functions perfectly fine with clicking on the cursor of the scrollbar and moving it by hand, but next come the problem.

The problem is, if I do a mouse wheel up or down on the scrollbar it doesn’t work.
So, my question is :

Is there any mean to detect a mouse wheel up or down in a restricted zone of my UI ? (in this case, I want my mouse wheel to be detected when my mouse is in the Panel2 zone, and so in the GridContainer and VScrollBar zone also, as they are in the zone of Panel2).

I currently can detect a mouse wheel (more or less), but it detects it ALWAYS, even if i’m not in the zone I want, and I don’t want this ^^’

If you have any idea,

Thank you !

:bust_in_silhouette: Reply From: mbenoni

Finally got the solution :

func _input(ev):
	if ev.type==InputEvent.MOUSE_BUTTON:
		var posX = ev.pos[0]
		var posY = ev.pos[1]
               #CHECK WITH COORDINATES OF PANEL
		if(posX > 20 && posX < 990 && posY > 20 && posY < 440):
			if ev.button_index == BUTTON_WHEEL_UP:
				doSth()

But it seems doSth() is called 2 times.

Change

    if ev.type==InputEvent.MOUSE_BUTTON:

into

    if ev.type==InputEvent.MOUSE_BUTTON && ev.is_pressed():

to avoid that it is called 2 times.

xyzzyx | 2017-12-08 10:50