virtualx-engine/modules/openxr/editor/openxr_select_action_dialog.cpp
Rémi Verschelde fd6453c45e Revert "Remove NOTIFICATION_ENTER_TREE when paired with NOTIFICATION_THEME_CHANGED"
This reverts commit 4b817a565c.

Fixes #64988.
Fixes #64997.

This caused several regressions (#64988, #64997,
https://github.com/godotengine/godot/issues/64997#issuecomment-1229970605)
which point at a flaw in the current logic:

- `Control::NOTIFICATION_ENTER_TREE` triggers a *deferred* notification with
  `NOTIFCATION_THEME_CHANGED` as introduced in #62845.
- Some classes use their `THEME_CHANGED` to cache theme items in
  member variables (e.g. `style_normal`, etc.), and use those member
  variables in `ENTER_TREE`, `READY`, `DRAW`, etc. Since the `THEME_CHANGE`
  notification is now deferred, they end up accessing invalid state and this
  can lead to not applying theme properly (e.g. for EditorHelp) or crashing
  (e.g. for EditorLog or CodeEdit).

So we need to go back to the drawing board and see if `THEME_CHANGED` can be
called earlier so that the previous logic still works?

Or can we refactor all engine code to make sure that:
- `ENTER_TREE` and similar do not depend on theme properties cached in member
  variables.
- Or `THEME_CHANGE` does trigger a general UI update to make sure that any
  bad theme handling in `ENTER_TREE` and co. gets fixed when `THEME_CHANGE`
  does arrive for the first time. But that means having a temporary invalid
  (and possibly still crashing) state, and doing some computations twice
  which might be heavy (e.g. `EditorHelp::_update_doc()`).
2022-08-29 11:11:29 +02:00

133 lines
5 KiB
C++

/*************************************************************************/
/* openxr_select_action_dialog.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "openxr_select_action_dialog.h"
#include "editor/editor_node.h"
void OpenXRSelectActionDialog::_bind_methods() {
ADD_SIGNAL(MethodInfo("action_selected", PropertyInfo(Variant::STRING, "action")));
}
void OpenXRSelectActionDialog::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE:
case NOTIFICATION_THEME_CHANGED: {
scroll->add_theme_style_override("bg", get_theme_stylebox(SNAME("bg"), SNAME("Tree")));
} break;
}
}
void OpenXRSelectActionDialog::_on_select_action(const String p_action) {
if (selected_action != "") {
NodePath button_path = action_buttons[selected_action];
Button *button = static_cast<Button *>(get_node(button_path));
if (button != nullptr) {
button->set_flat(true);
}
}
selected_action = p_action;
if (selected_action != "") {
NodePath button_path = action_buttons[selected_action];
Button *button = static_cast<Button *>(get_node(button_path));
if (button != nullptr) {
button->set_flat(false);
}
}
}
void OpenXRSelectActionDialog::open() {
ERR_FAIL_COND(action_map.is_null());
// out with the old...
while (main_vb->get_child_count() > 0) {
memdelete(main_vb->get_child(0));
}
selected_action = "";
action_buttons.clear();
Array action_sets = action_map->get_action_sets();
for (int i = 0; i < action_sets.size(); i++) {
Ref<OpenXRActionSet> action_set = action_sets[i];
Label *action_set_label = memnew(Label);
action_set_label->set_text(action_set->get_localized_name());
main_vb->add_child(action_set_label);
Array actions = action_set->get_actions();
for (int j = 0; j < actions.size(); j++) {
Ref<OpenXRAction> action = actions[j];
HBoxContainer *action_hb = memnew(HBoxContainer);
main_vb->add_child(action_hb);
Control *indent_node = memnew(Control);
indent_node->set_custom_minimum_size(Size2(10.0, 0.0));
action_hb->add_child(indent_node);
Button *action_button = memnew(Button);
String action_name = action->get_name_with_set();
action_button->set_flat(true);
action_button->set_text(action->get_name() + ": " + action->get_localized_name());
action_button->connect("pressed", callable_mp(this, &OpenXRSelectActionDialog::_on_select_action).bind(action_name));
action_hb->add_child(action_button);
action_buttons[action_name] = action_button->get_path();
}
}
popup_centered();
}
void OpenXRSelectActionDialog::ok_pressed() {
if (selected_action == "") {
return;
}
emit_signal("action_selected", selected_action);
hide();
}
OpenXRSelectActionDialog::OpenXRSelectActionDialog(Ref<OpenXRActionMap> p_action_map) {
action_map = p_action_map;
set_title(TTR("Select an action"));
scroll = memnew(ScrollContainer);
scroll->set_custom_minimum_size(Size2(600.0, 400.0));
add_child(scroll);
main_vb = memnew(VBoxContainer);
main_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
scroll->add_child(main_vb);
}