From 958e91df7551b3c198e31866ab394d3060887979 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Tue, 7 Jul 2020 00:09:37 +0200 Subject: [PATCH] Make computer UI disappear when clicking elsewhere --- Actors/Objects/Computer/Computer.gd | 16 ++++++++++++---- Actors/Objects/Computer/Computer.tscn | 2 ++ Actors/Objects/Computer/UI/ControlComp.tscn | 6 ++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Actors/Objects/Computer/Computer.gd b/Actors/Objects/Computer/Computer.gd index 5057454..dd9481e 100644 --- a/Actors/Objects/Computer/Computer.gd +++ b/Actors/Objects/Computer/Computer.gd @@ -44,14 +44,22 @@ func set_direction(dir): $computer.region_rect.position = Vector2(32, 0) $computer/screen.region_rect.position = screen_region_offset + Vector2(64, 0) -func open_controls(): +func manage_controls(show: bool): match computer_type: ComputerType.ShipCommand: - $Control/ControlComp.visible = true + $Control/ControlComp.visible = show + +func _input(event): + if event is InputEventMouseButton and event.pressed and not is_inside: + manage_controls(false) func _input_event(viewport, event, shape_idx): if Engine.editor_hint: return if event is InputEventMouseButton and event.pressed: - print(event) - open_controls() + manage_controls(true) + +var is_inside = false +func _ui_focus_changed(entered): + print(entered) + is_inside = entered diff --git a/Actors/Objects/Computer/Computer.tscn b/Actors/Objects/Computer/Computer.tscn index 0902b82..99ed114 100644 --- a/Actors/Objects/Computer/Computer.tscn +++ b/Actors/Objects/Computer/Computer.tscn @@ -76,3 +76,5 @@ margin_top = -78.3109 margin_right = 166.851 margin_bottom = 53.6891 rect_scale = Vector2( 0.5, 0.5 ) +[connection signal="mouse_entered" from="Control/ControlComp" to="." method="_ui_focus_changed" binds= [ true ]] +[connection signal="mouse_exited" from="Control/ControlComp" to="." method="_ui_focus_changed" binds= [ false ]] diff --git a/Actors/Objects/Computer/UI/ControlComp.tscn b/Actors/Objects/Computer/UI/ControlComp.tscn index ac3de95..b7419e2 100644 --- a/Actors/Objects/Computer/UI/ControlComp.tscn +++ b/Actors/Objects/Computer/UI/ControlComp.tscn @@ -7,6 +7,7 @@ margin_left = -100.0 margin_top = -150.0 margin_right = 100.0 margin_bottom = -20.0 +mouse_filter = 1 script = ExtResource( 1 ) [node name="Panel" type="Panel" parent="."] @@ -15,6 +16,7 @@ margin_top = 90.0 margin_right = 140.0 margin_bottom = 130.0 rect_rotation = 45.0 +mouse_filter = 1 __meta__ = { "_edit_use_anchors_": false } @@ -47,12 +49,14 @@ __meta__ = { margin_top = 5.0 margin_right = 180.0 margin_bottom = 19.0 +mouse_filter = 1 text = "Ship velocity" [node name="HSlider" type="HSlider" parent="Container/VelocityBox"] margin_top = 23.0 margin_right = 180.0 margin_bottom = 39.0 +mouse_filter = 1 max_value = 1000.0 value = 1000.0 tick_count = 10 @@ -73,12 +77,14 @@ __meta__ = { margin_top = 5.0 margin_right = 180.0 margin_bottom = 19.0 +mouse_filter = 1 text = "Ship direction" [node name="HSlider" type="HSlider" parent="Container/DirectionBox"] margin_top = 23.0 margin_right = 180.0 margin_bottom = 39.0 +mouse_filter = 1 max_value = 360.0 tick_count = 18 ticks_on_borders = true