Skip to content

Commit 2fe9d8e

Browse files
committed
Fix choice button color
1 parent 667e654 commit 2fe9d8e

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

addons/dialogic/Modules/Choice/node_choice_button.gd

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ func _ready() -> void:
3333
add_to_group('dialogic_choice_button')
3434
shortcut_in_tooltip = false
3535
hide()
36+
37+
# For players who use a mouse to make choices, mouse hover should grab focus.
38+
# Otherwise the auto-focused button will always show a highlighted color when
39+
# the mouse cursor is hovering on another button.
40+
if not mouse_entered.is_connected(grab_focus):
41+
mouse_entered.connect(grab_focus)
42+
if not focus_entered.is_connected(_on_choice_button_focus_entred):
43+
focus_entered.connect(_on_choice_button_focus_entred.bind(self))
3644

3745

3846
## 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:
6573
text_node.text = new_text
6674
else:
6775
text = new_text
76+
77+
78+
## For players who use many devices (mouse/keyboard/gamepad, etc) at the same time to make choices,
79+
## a grabing-focus triggered by keyboard/gamepad should also change the mouse cursor's
80+
## position otherwise two buttons will have highlighted color(one highlighted button
81+
## triggered by mouse hover and another highlighted button triggered by other devices' choice).
82+
## So this function do such thing: make sure the mouse cursor does not hover on an unfocused button.
83+
func _on_choice_button_focus_entred(focused_button: Button):
84+
var global_mouse_pos = get_global_mouse_position()
85+
var focused_button_rect = focused_button.get_global_rect()
86+
if focused_button_rect.has_point(global_mouse_pos):
87+
return
88+
# Only change mouse curor position when an unfocused button' rect has the cursor.
89+
for node in get_tree().get_nodes_in_group('dialogic_choice_button'):
90+
if node is Button:
91+
if node != focused_button and node.get_global_rect().has_point(global_mouse_pos):
92+
# We prefer to change only mouse_position.y or mouse_position.x to warp the
93+
# mouse to the focused button's rect to achieve the best visual effect.
94+
var modify_y_pos = Vector2(global_mouse_pos.x, focused_button.get_global_rect().get_center().y)
95+
if focused_button_rect.has_point(modify_y_pos):
96+
get_viewport().warp_mouse(modify_y_pos)
97+
return
98+
99+
var modify_x_pos = Vector2(focused_button.get_global_rect().get_center().x, global_mouse_pos.y)
100+
if focused_button_rect.has_point(modify_x_pos):
101+
get_viewport().warp_mouse(modify_x_pos)
102+
return
103+
104+
# Maybe the buttons are not aligned as vertically or horizontlly.
105+
# Or perhaps the length difference between the two buttons is quite large.
106+
# So we just make the cursor hover on the center of the focused button.
107+
get_viewport().warp_mouse(focused_button.get_global_rect().get_center())
108+
return

0 commit comments

Comments
 (0)