From 2fe9d8e220b81e3a5116ec93ef46ada8035eb069 Mon Sep 17 00:00:00 2001 From: jinyang Date: Thu, 10 Jul 2025 18:02:18 +0800 Subject: [PATCH] Fix choice button color --- .../Modules/Choice/node_choice_button.gd | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/addons/dialogic/Modules/Choice/node_choice_button.gd b/addons/dialogic/Modules/Choice/node_choice_button.gd index 4a09c29ce..b652533fb 100644 --- a/addons/dialogic/Modules/Choice/node_choice_button.gd +++ b/addons/dialogic/Modules/Choice/node_choice_button.gd @@ -33,6 +33,14 @@ func _ready() -> void: add_to_group('dialogic_choice_button') shortcut_in_tooltip = false hide() + + # For players who use a mouse to make choices, mouse hover should grab focus. + # Otherwise the auto-focused button will always show a highlighted color when + # the mouse cursor is hovering on another button. + if not mouse_entered.is_connected(grab_focus): + mouse_entered.connect(grab_focus) + if not focus_entered.is_connected(_on_choice_button_focus_entred): + focus_entered.connect(_on_choice_button_focus_entred.bind(self)) ## Custom choice buttons can override this for specialized behavior when the choice button is pressed. @@ -65,3 +73,36 @@ func set_choice_text(new_text: String) -> void: text_node.text = new_text else: text = new_text + + +## For players who use many devices (mouse/keyboard/gamepad, etc) at the same time to make choices, +## a grabing-focus triggered by keyboard/gamepad should also change the mouse cursor's +## position otherwise two buttons will have highlighted color(one highlighted button +## triggered by mouse hover and another highlighted button triggered by other devices' choice). +## So this function do such thing: make sure the mouse cursor does not hover on an unfocused button. +func _on_choice_button_focus_entred(focused_button: Button): + var global_mouse_pos = get_global_mouse_position() + var focused_button_rect = focused_button.get_global_rect() + if focused_button_rect.has_point(global_mouse_pos): + return + # Only change mouse curor position when an unfocused button' rect has the cursor. + for node in get_tree().get_nodes_in_group('dialogic_choice_button'): + if node is Button: + if node != focused_button and node.get_global_rect().has_point(global_mouse_pos): + # We prefer to change only mouse_position.y or mouse_position.x to warp the + # mouse to the focused button's rect to achieve the best visual effect. + var modify_y_pos = Vector2(global_mouse_pos.x, focused_button.get_global_rect().get_center().y) + if focused_button_rect.has_point(modify_y_pos): + get_viewport().warp_mouse(modify_y_pos) + return + + var modify_x_pos = Vector2(focused_button.get_global_rect().get_center().x, global_mouse_pos.y) + if focused_button_rect.has_point(modify_x_pos): + get_viewport().warp_mouse(modify_x_pos) + return + + # Maybe the buttons are not aligned as vertically or horizontlly. + # Or perhaps the length difference between the two buttons is quite large. + # So we just make the cursor hover on the center of the focused button. + get_viewport().warp_mouse(focused_button.get_global_rect().get_center()) + return