I have a button that I long press it inorder to activate a function, and i want to activate this funciton when the mouse leave the button erea (will holding the mouse). holding the mouse doesn't trigger the _on_mouse_exited signal of the button, so i want to add a area2D on top of the button in order to trigger a _on_mouse_exited signal will holding the mouse key. But the button and the area2D are interfere with each other...

is there a way to work around it?

thx

  • SnapCracklins replied to this.
  • You can do something like this without an area.

    extends Node2D
    
    
    var but:Button
    var but_rect:Rect2
    var follower:Sprite
    var lab:Label
    var tim:Timer
    
    
    func _input(event: InputEvent) -> void:
    	if event is InputEventMouseMotion \
    	and but.pressed \
    	and not but_rect.has_point(event.position):
    		if not follower:
    			follower = Sprite.new()
    			follower.texture = preload('res://icon.png')
    			add_child(follower)
    		follower.position = event.position
    	elif event is InputEventMouseButton \
    	and not event.is_pressed() and follower:
    		follower.queue_free()
    		follower = null
    
    
    func _ready() -> void:
    	but = Button.new()
    	but.rect_size = Vector2(100, 40)
    	but.rect_position = Vector2.ONE * 100
    	but_rect = Rect2(but.rect_position, but.rect_size)
    	add_child(but)
    	but.connect('pressed', self, 'but_press')
    
    	lab = Label.new()
    	lab.rect_scale = Vector2.ONE * 5
    	lab.rect_position = Vector2(100, 200)
    	add_child(lab)
    
    	tim = Timer.new()
    	tim.one_shot = true
    	tim.wait_time = 1
    	add_child(tim)
    	tim.connect('timeout', self, 'tim_out')
    
    
    func but_press():
    	lab.text = 'pressed'
    	tim.start()
    
    
    func tim_out():
    	lab.text = ''

    CertfidBabon

    var _can_activate = false
    
    func _on_Button_button_up():
            _can_activate = false
    
    func _on_Button_button_down():
           _can_activate = true
    
    func _holdMouseFunction():
           #### insert function here
    
    func _on_Area_2D_mouse_exited():
          if Input.is_action_pressed("mouse_right") and _can_activate:
                _holdMouseFunction()

      SnapCracklins
      the "func _on_Area_2D_mouse_exited()" will not get called, because the button and the area2D are located at the same spase, and it create same interference with how the mouse event work.
      i have read that by tageling the "input-pickable" on and off, i can make the eara2d mouse event work. but then I break the button.

      You can do something like this without an area.

      extends Node2D
      
      
      var but:Button
      var but_rect:Rect2
      var follower:Sprite
      var lab:Label
      var tim:Timer
      
      
      func _input(event: InputEvent) -> void:
      	if event is InputEventMouseMotion \
      	and but.pressed \
      	and not but_rect.has_point(event.position):
      		if not follower:
      			follower = Sprite.new()
      			follower.texture = preload('res://icon.png')
      			add_child(follower)
      		follower.position = event.position
      	elif event is InputEventMouseButton \
      	and not event.is_pressed() and follower:
      		follower.queue_free()
      		follower = null
      
      
      func _ready() -> void:
      	but = Button.new()
      	but.rect_size = Vector2(100, 40)
      	but.rect_position = Vector2.ONE * 100
      	but_rect = Rect2(but.rect_position, but.rect_size)
      	add_child(but)
      	but.connect('pressed', self, 'but_press')
      
      	lab = Label.new()
      	lab.rect_scale = Vector2.ONE * 5
      	lab.rect_position = Vector2(100, 200)
      	add_child(lab)
      
      	tim = Timer.new()
      	tim.one_shot = true
      	tim.wait_time = 1
      	add_child(tim)
      	tim.connect('timeout', self, 'tim_out')
      
      
      func but_press():
      	lab.text = 'pressed'
      	tim.start()
      
      
      func tim_out():
      	lab.text = ''
        11 days later

        duane it's seems a bit wasteful to run a check every time the mouse move, but it only for the UI so i think it's fine 🙂

        thx

        you could also use a timer or count msecs/usecs and make a check every n units of time for if mouse was just moved.