Merge pull request #64960 from YeldhamDev/more_hl_stuff

This commit is contained in:
Rémi Verschelde 2022-08-29 08:05:33 +02:00 committed by GitHub
commit f647292df5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 13 deletions

View File

@ -337,6 +337,7 @@
<param index="0" name="index" type="int" /> <param index="0" name="index" type="int" />
<description> <description>
Sets the currently focused item as the given [param index]. Sets the currently focused item as the given [param index].
Passing [code]-1[/code] as the index makes so that no item is focused.
</description> </description>
</method> </method>
<method name="set_item_accelerator"> <method name="set_item_accelerator">

View File

@ -65,8 +65,9 @@ void BaseButton::gui_input(const Ref<InputEvent> &p_event) {
bool button_masked = mouse_button.is_valid() && (mouse_button_to_mask(mouse_button->get_button_index()) & button_mask) != MouseButton::NONE; bool button_masked = mouse_button.is_valid() && (mouse_button_to_mask(mouse_button->get_button_index()) & button_mask) != MouseButton::NONE;
if (button_masked || ui_accept) { if (button_masked || ui_accept) {
was_mouse_pressed = button_masked; was_mouse_pressed = button_masked;
on_action_event(p_event); on_action_event(p_event);
was_mouse_pressed = false;
return; return;
} }

View File

@ -103,9 +103,14 @@ void MenuButton::pressed() {
popup->set_position(gp); popup->set_position(gp);
popup->set_parent_rect(Rect2(Point2(gp - popup->get_position()), size)); popup->set_parent_rect(Rect2(Point2(gp - popup->get_position()), size));
// If not triggered by the mouse, start the popup with its first item selected. // If not triggered by the mouse, start the popup with its first enabled item focused.
if (popup->get_item_count() > 0 && !_was_pressed_by_mouse()) { if (!_was_pressed_by_mouse()) {
popup->set_current_index(0); for (int i = 0; i < popup->get_item_count(); i++) {
if (!popup->is_item_disabled(i)) {
popup->set_current_index(i);
break;
}
}
} }
popup->popup(); popup->popup();
@ -161,7 +166,10 @@ void MenuButton::_notification(int p_what) {
if (menu_btn_other && menu_btn_other != this && menu_btn_other->is_switch_on_hover() && !menu_btn_other->is_disabled() && if (menu_btn_other && menu_btn_other != this && menu_btn_other->is_switch_on_hover() && !menu_btn_other->is_disabled() &&
(get_parent()->is_ancestor_of(menu_btn_other) || menu_btn_other->get_parent()->is_ancestor_of(popup))) { (get_parent()->is_ancestor_of(menu_btn_other) || menu_btn_other->get_parent()->is_ancestor_of(popup))) {
popup->hide(); popup->hide();
menu_btn_other->pressed(); menu_btn_other->pressed();
// As the popup wasn't triggered by a mouse click, the item focus needs to be removed manually.
menu_btn_other->get_popup()->set_current_index(-1);
} }
} break; } break;
} }

View File

@ -207,12 +207,24 @@ void OptionButton::pressed() {
popup->set_position(get_screen_position() + Size2(0, size.height * get_global_transform().get_scale().y)); popup->set_position(get_screen_position() + Size2(0, size.height * get_global_transform().get_scale().y));
popup->set_size(Size2(size.width, 0)); popup->set_size(Size2(size.width, 0));
// If not triggered by the mouse, start the popup with the checked item selected. // If not triggered by the mouse, start the popup with the checked item (or the first enabled one) focused.
if (popup->get_item_count() > 0) { if (current != NONE_SELECTED && !popup->is_item_disabled(current)) {
if (!_was_pressed_by_mouse()) { if (!_was_pressed_by_mouse()) {
popup->set_current_index(current > -1 ? current : 0); popup->set_current_index(current);
} else { } else {
popup->scroll_to_item(current > -1 ? current : 0); popup->scroll_to_item(current);
}
} else {
for (int i = 0; i < popup->get_item_count(); i++) {
if (!popup->is_item_disabled(i)) {
if (!_was_pressed_by_mouse()) {
popup->set_current_index(i);
} else {
popup->scroll_to_item(i);
}
break;
}
} }
} }

View File

@ -216,9 +216,14 @@ void PopupMenu::_activate_submenu(int p_over, bool p_by_keyboard) {
submenu_pum->activated_by_keyboard = p_by_keyboard; submenu_pum->activated_by_keyboard = p_by_keyboard;
// If not triggered by the mouse, start the popup with its first item selected. // If not triggered by the mouse, start the popup with its first enabled item focused.
if (submenu_pum->get_item_count() > 0 && p_by_keyboard) { if (p_by_keyboard) {
submenu_pum->set_current_index(0); for (int i = 0; i < submenu_pum->get_item_count(); i++) {
if (!submenu_pum->is_item_disabled(i)) {
submenu_pum->set_current_index(i);
break;
}
}
} }
submenu_pum->popup(); submenu_pum->popup();
@ -1534,14 +1539,19 @@ bool PopupMenu::is_item_shortcut_disabled(int p_idx) const {
} }
void PopupMenu::set_current_index(int p_idx) { void PopupMenu::set_current_index(int p_idx) {
if (p_idx != -1) {
ERR_FAIL_INDEX(p_idx, items.size()); ERR_FAIL_INDEX(p_idx, items.size());
}
if (mouse_over == p_idx) { if (mouse_over == p_idx) {
return; return;
} }
mouse_over = p_idx; mouse_over = p_idx;
if (mouse_over != -1) {
scroll_to_item(mouse_over); scroll_to_item(mouse_over);
}
control->update(); control->update();
} }