fix mouse events for body
This commit is contained in:
@@ -443,6 +443,30 @@ func _input(event: InputEvent) -> void:
|
||||
}
|
||||
_execute_lua_callback(subscription, [key_info])
|
||||
|
||||
elif event is InputEventMouseButton:
|
||||
var mouse_event = event as InputEventMouseButton
|
||||
for subscription_id in event_subscriptions:
|
||||
var subscription = event_subscriptions[subscription_id]
|
||||
if subscription.element_id == "body" and subscription.connected_signal == "input":
|
||||
var should_trigger = false
|
||||
match subscription.event_name:
|
||||
"mousedown":
|
||||
should_trigger = mouse_event.pressed
|
||||
"mouseup":
|
||||
should_trigger = not mouse_event.pressed
|
||||
|
||||
if should_trigger:
|
||||
var mouse_info = {"x": 0, "y": 0, "button": mouse_event.button_index}
|
||||
var body_container = _get_body_container()
|
||||
|
||||
if body_container:
|
||||
var control = body_container as Control
|
||||
var global_pos = mouse_event.global_position
|
||||
var element_rect = control.get_global_rect()
|
||||
mouse_info["x"] = global_pos.x - element_rect.position.x
|
||||
mouse_info["y"] = global_pos.y - element_rect.position.y
|
||||
|
||||
_execute_lua_callback(subscription, [mouse_info])
|
||||
|
||||
elif event is InputEventMouseMotion:
|
||||
var mouse_event = event as InputEventMouseMotion
|
||||
@@ -477,28 +501,42 @@ func _get_element_relative_mouse_position(mouse_event: InputEvent, element_id: S
|
||||
}
|
||||
|
||||
func _handle_mousemove_event(mouse_event: InputEventMouseMotion, subscription: EventSubscription) -> void:
|
||||
# TODO: pass reference instead of hardcoded path
|
||||
var body_container = Engine.get_main_loop().current_scene.website_container
|
||||
|
||||
if body_container.get_parent() is MarginContainer:
|
||||
body_container = body_container.get_parent()
|
||||
|
||||
var body_container = _get_body_container()
|
||||
if not body_container:
|
||||
return
|
||||
|
||||
var container_rect = body_container.get_global_rect()
|
||||
var local_x = mouse_event.global_position.x - container_rect.position.x
|
||||
var local_y = mouse_event.global_position.y - container_rect.position.y
|
||||
var control = body_container as Control
|
||||
var global_pos = mouse_event.global_position
|
||||
var element_rect = control.get_global_rect()
|
||||
var local_x = global_pos.x - element_rect.position.x
|
||||
var local_y = global_pos.y - element_rect.position.y
|
||||
|
||||
# Only provide coordinates if mouse is within the container bounds
|
||||
if local_x >= 0 and local_y >= 0 and local_x <= container_rect.size.x and local_y <= container_rect.size.y:
|
||||
var mouse_info = {
|
||||
"x": local_x,
|
||||
"y": local_y,
|
||||
"deltaX": mouse_event.relative.x,
|
||||
"deltaY": mouse_event.relative.y
|
||||
}
|
||||
_execute_lua_callback(subscription, [mouse_info])
|
||||
var mouse_info = {
|
||||
"x": local_x,
|
||||
"y": local_y,
|
||||
"deltaX": mouse_event.relative.x,
|
||||
"deltaY": mouse_event.relative.y
|
||||
}
|
||||
_execute_lua_callback(subscription, [mouse_info])
|
||||
|
||||
func _get_body_container() -> Control:
|
||||
# Try to get body from DOM registry first
|
||||
var body_container = dom_parser.parse_result.dom_nodes.get("body", null)
|
||||
|
||||
# We fallback to finding the active website container, as it seems theres a bug where body can be null in this context
|
||||
if not body_container:
|
||||
var main_scene = Engine.get_main_loop().current_scene
|
||||
if main_scene and main_scene.has_method("get_active_website_container"):
|
||||
body_container = main_scene.get_active_website_container()
|
||||
else:
|
||||
body_container = Engine.get_main_loop().current_scene.website_container
|
||||
if body_container and body_container.get_parent() is MarginContainer:
|
||||
body_container = body_container.get_parent()
|
||||
|
||||
if body_container and body_container is Control:
|
||||
return body_container as Control
|
||||
|
||||
return null
|
||||
|
||||
# Input event handlers
|
||||
func _on_input_text_changed(new_text: String, subscription: EventSubscription) -> void:
|
||||
|
||||
@@ -189,6 +189,11 @@ static func connect_body_event(event_name: String, subscription, lua_api) -> boo
|
||||
subscription.connected_signal = "input_mousemove"
|
||||
subscription.connected_node = lua_api
|
||||
return true
|
||||
"mousedown", "mouseup":
|
||||
lua_api.set_process_input(true)
|
||||
subscription.connected_signal = "input"
|
||||
subscription.connected_node = lua_api
|
||||
return true
|
||||
"mouseenter", "mouseexit":
|
||||
var main_container = lua_api.dom_parser.parse_result.dom_nodes.get("body", null)
|
||||
if main_container:
|
||||
|
||||
Reference in New Issue
Block a user