Skip to content

Fix auto selected choice button color won't change to normal when mouse hover on another button #2625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions addons/dialogic/Modules/Choice/node_choice_button.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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