0 votes

I have a Slider and want to allow the user to only access a certain range of it. If the grabber exceeds a given threshold value (to the right in my case), the grabber should stop there. As long as the grabber is grabbed, it should still be able to move back to the left again.

My current solution doesn't allow that, it virtually (not actually however) releases the grab by setting MOUSE_FILTER_IGNORE as soon as the threshold is reached, and resetting it as soon as the left mouse button is released if the previous state was "dragged grabber beyond threshold".

Another problem is that when clicking and holding directly into the forbidden range, the grabber stays there until the user releases the mouse button. Then it jumps back to the threshold. Setting value = threshold already in the value_changed callback didn't work as expected however.

class_name HSliderLimit
extends HSlider   

export var threshold := 50.0
var _slider_overdragged := false

func _ready() -> void:
    connect("value_changed", self, "_on_value_changed")

func _input(event: InputEvent) -> void:
    if event is InputEventMouseButton and event.pressed == false and _slider_overdragged:
        _slider_overdragged = false
        mouse_filter = MOUSE_FILTER_STOP
        value = threshold

func _on_value_changed(value: float) -> void:
    if value > threshold:
        _slider_overdragged = true
        mouse_filter = MOUSE_FILTER_IGNORE
        #value = threshold  # doesn't work here, at least not visually for the grabber
Godot version 3.5
in Engine by (34 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.