Godot Version
v4.3.stable.official [77dcf97d8]
Question
Ok, I know it seems unreasonable, but here is my case:
I use a Subviewport to display enemy when it is way out of the player’s viewing range. And I need to click the enemy in the Subviewport to lock on different parts of the enemy.
For that, I grab the event in gui_input signal of my SubViewportContainer, calculate the position and parse it.
The problem is: mouse_enter signal doesn’t seem to work on off-screen nodes(which is probably by design), and only those click related signals work(button_down, button_up …). So I can only click on the enemy, but not detecting the mouse entering.
So, is there a way to detect mouse_enter on a off-screen button?
Below is a simple demo:
The TargetBtn is placed that its bottom half in and top half out of screen. When you hover your mouse on the button in the SubViewportContainer, its upper half doesn’t respond, but lower half does.
Script:
extends Node2D
@onready var Target = $Target
@onready var TargetViewport = $SubViewportContainer/TargetViewport
@onready var TargetViewportCamera = $SubViewportContainer/TargetViewport/Camera2D
func _ready() -> void:
TargetViewport.world_2d = get_viewport().world_2d
TargetViewport.size = Vector2(200, 200)
TargetViewportCamera.global_position = $Target.global_position
func _on_sub_viewport_container_gui_input(event: InputEvent) -> void:
# Grab the event in TargetViewport and parse it in the global world
var n_pos = Vector2(event.position.x / TargetViewport.size.x, event.position.y / TargetViewport.size.y)
var target_rect_global = Rect2(Target.global_position - TargetViewport.size / 2., TargetViewport.size )
var global_pos = Vector2(target_rect_global.position + target_rect_global.size * n_pos)
var ev = event.duplicate()
ev.position = get_viewport().canvas_transform * global_pos
Input.parse_input_event(ev)