openxr_binding_modifiers_dialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**************************************************************************/
  2. /* openxr_binding_modifiers_dialog.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "openxr_binding_modifiers_dialog.h"
  31. #include "../action_map/openxr_interaction_profile_metadata.h"
  32. #include "openxr_action_map_editor.h"
  33. #include "editor/themes/editor_scale.h"
  34. void OpenXRBindingModifiersDialog::_bind_methods() {
  35. ClassDB::bind_method(D_METHOD("_do_add_binding_modifier_editor", "binding_modifier_editor"), &OpenXRBindingModifiersDialog::_do_add_binding_modifier_editor);
  36. ClassDB::bind_method(D_METHOD("_do_remove_binding_modifier_editor", "binding_modifier_editor"), &OpenXRBindingModifiersDialog::_do_remove_binding_modifier_editor);
  37. }
  38. void OpenXRBindingModifiersDialog::_notification(int p_what) {
  39. switch (p_what) {
  40. case NOTIFICATION_READY: {
  41. _create_binding_modifiers();
  42. } break;
  43. case NOTIFICATION_THEME_CHANGED: {
  44. if (binding_modifier_sc) {
  45. binding_modifier_sc->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  46. }
  47. } break;
  48. }
  49. }
  50. OpenXRBindingModifierEditor *OpenXRBindingModifiersDialog::_add_binding_modifier_editor(Ref<OpenXRBindingModifier> p_binding_modifier) {
  51. ERR_FAIL_COND_V(p_binding_modifier.is_null(), nullptr);
  52. String class_name = p_binding_modifier->get_class();
  53. ERR_FAIL_COND_V(class_name.is_empty(), nullptr);
  54. String editor_class = OpenXRActionMapEditor::get_binding_modifier_editor_class(class_name);
  55. ERR_FAIL_COND_V(editor_class.is_empty(), nullptr);
  56. OpenXRBindingModifierEditor *new_editor = nullptr;
  57. Object *obj = ClassDB::instantiate(editor_class);
  58. if (obj) {
  59. new_editor = Object::cast_to<OpenXRBindingModifierEditor>(obj);
  60. if (!new_editor) {
  61. // Not of correct type?? Free it.
  62. memfree(obj);
  63. }
  64. }
  65. ERR_FAIL_NULL_V(new_editor, nullptr);
  66. new_editor->setup(action_map, p_binding_modifier);
  67. new_editor->connect("binding_modifier_removed", callable_mp(this, &OpenXRBindingModifiersDialog::_on_remove_binding_modifier));
  68. binding_modifiers_vb->add_child(new_editor);
  69. new_editor->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  70. return new_editor;
  71. }
  72. void OpenXRBindingModifiersDialog::_create_binding_modifiers() {
  73. Array new_binding_modifiers;
  74. if (ip_binding.is_valid()) {
  75. new_binding_modifiers = ip_binding->get_binding_modifiers();
  76. } else if (interaction_profile.is_valid()) {
  77. new_binding_modifiers = interaction_profile->get_binding_modifiers();
  78. } else {
  79. ERR_FAIL_MSG("No binding nor interaction profile specified.");
  80. }
  81. for (int i = 0; i < new_binding_modifiers.size(); i++) {
  82. Ref<OpenXRBindingModifier> binding_modifier = new_binding_modifiers[i];
  83. _add_binding_modifier_editor(binding_modifier);
  84. }
  85. }
  86. void OpenXRBindingModifiersDialog::_on_add_binding_modifier() {
  87. create_dialog->popup_create(false);
  88. }
  89. void OpenXRBindingModifiersDialog::_on_remove_binding_modifier(Object *p_binding_modifier_editor) {
  90. if (ip_binding.is_valid()) {
  91. ip_binding->set_edited(true);
  92. } else if (interaction_profile.is_valid()) {
  93. interaction_profile->set_edited(true);
  94. } else {
  95. ERR_FAIL_MSG("No binding nor interaction profile specified.");
  96. }
  97. OpenXRBindingModifierEditor *binding_modifier_editor = Object::cast_to<OpenXRBindingModifierEditor>(p_binding_modifier_editor);
  98. ERR_FAIL_NULL(binding_modifier_editor);
  99. ERR_FAIL_COND(binding_modifier_editor->get_parent() != binding_modifiers_vb);
  100. undo_redo->create_action(TTR("Remove binding modifier"));
  101. undo_redo->add_do_method(this, "_do_remove_binding_modifier_editor", binding_modifier_editor);
  102. undo_redo->add_undo_method(this, "_do_add_binding_modifier_editor", binding_modifier_editor);
  103. undo_redo->commit_action(true);
  104. }
  105. void OpenXRBindingModifiersDialog::_on_dialog_created() {
  106. // Instance new binding modifier object
  107. Variant obj = create_dialog->instantiate_selected();
  108. ERR_FAIL_COND(obj.get_type() != Variant::OBJECT);
  109. Ref<OpenXRBindingModifier> new_binding_modifier = obj;
  110. ERR_FAIL_COND(new_binding_modifier.is_null());
  111. if (ip_binding.is_valid()) {
  112. // Add it to our binding.
  113. ip_binding->add_binding_modifier(new_binding_modifier);
  114. ip_binding->set_edited(true);
  115. } else if (interaction_profile.is_valid()) {
  116. // Add it to our interaction profile.
  117. interaction_profile->add_binding_modifier(new_binding_modifier);
  118. interaction_profile->set_edited(true);
  119. } else {
  120. ERR_FAIL_MSG("No binding nor interaction profile specified.");
  121. }
  122. // Create our editor for this.
  123. OpenXRBindingModifierEditor *binding_modifier_editor = _add_binding_modifier_editor(new_binding_modifier);
  124. ERR_FAIL_NULL(binding_modifier_editor);
  125. // Add undo/redo.
  126. undo_redo->create_action(TTR("Add binding modifier"));
  127. undo_redo->add_do_method(this, "_do_add_binding_modifier_editor", binding_modifier_editor);
  128. undo_redo->add_undo_method(this, "_do_remove_binding_modifier_editor", binding_modifier_editor);
  129. undo_redo->commit_action(false);
  130. }
  131. void OpenXRBindingModifiersDialog::_do_add_binding_modifier_editor(OpenXRBindingModifierEditor *p_binding_modifier_editor) {
  132. Ref<OpenXRBindingModifier> binding_modifier = p_binding_modifier_editor->get_binding_modifier();
  133. ERR_FAIL_COND(binding_modifier.is_null());
  134. if (ip_binding.is_valid()) {
  135. // Add it to our binding
  136. ip_binding->add_binding_modifier(binding_modifier);
  137. } else if (interaction_profile.is_valid()) {
  138. // Add it to our interaction profile
  139. interaction_profile->add_binding_modifier(binding_modifier);
  140. } else {
  141. ERR_FAIL_MSG("No binding nor interaction profile specified.");
  142. }
  143. binding_modifiers_vb->add_child(p_binding_modifier_editor);
  144. }
  145. void OpenXRBindingModifiersDialog::_do_remove_binding_modifier_editor(OpenXRBindingModifierEditor *p_binding_modifier_editor) {
  146. Ref<OpenXRBindingModifier> binding_modifier = p_binding_modifier_editor->get_binding_modifier();
  147. ERR_FAIL_COND(binding_modifier.is_null());
  148. if (ip_binding.is_valid()) {
  149. // Remove it from our binding.
  150. ip_binding->remove_binding_modifier(binding_modifier);
  151. } else if (interaction_profile.is_valid()) {
  152. // Removed it to from interaction profile.
  153. interaction_profile->remove_binding_modifier(binding_modifier);
  154. } else {
  155. ERR_FAIL_MSG("No binding nor interaction profile specified.");
  156. }
  157. binding_modifiers_vb->remove_child(p_binding_modifier_editor);
  158. }
  159. OpenXRBindingModifiersDialog::OpenXRBindingModifiersDialog() {
  160. undo_redo = EditorUndoRedoManager::get_singleton();
  161. set_transient(true);
  162. binding_modifier_sc = memnew(ScrollContainer);
  163. binding_modifier_sc->set_custom_minimum_size(Size2(350.0 * EDSCALE, 0.0));
  164. binding_modifier_sc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  165. binding_modifier_sc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  166. binding_modifier_sc->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  167. add_child(binding_modifier_sc);
  168. binding_modifiers_vb = memnew(VBoxContainer);
  169. binding_modifiers_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  170. binding_modifier_sc->add_child(binding_modifiers_vb);
  171. binding_warning_label = memnew(Label);
  172. binding_warning_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  173. binding_warning_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
  174. binding_warning_label->set_text(TTR("Note: modifiers will only be applied if supported on the host system."));
  175. binding_modifiers_vb->add_child(binding_warning_label);
  176. add_binding_modifier_btn = memnew(Button);
  177. add_binding_modifier_btn->set_text(TTR("Add binding modifier"));
  178. add_binding_modifier_btn->connect("pressed", callable_mp(this, &OpenXRBindingModifiersDialog::_on_add_binding_modifier));
  179. binding_modifiers_vb->add_child(add_binding_modifier_btn);
  180. // TODO may need to create our own dialog for this that can filter on binding modifiers recorded on interaction profiles or on individual bindings.
  181. create_dialog = memnew(CreateDialog);
  182. create_dialog->set_transient(true);
  183. create_dialog->connect("create", callable_mp(this, &OpenXRBindingModifiersDialog::_on_dialog_created));
  184. add_child(create_dialog);
  185. }
  186. void OpenXRBindingModifiersDialog::setup(Ref<OpenXRActionMap> p_action_map, Ref<OpenXRInteractionProfile> p_interaction_profile, Ref<OpenXRIPBinding> p_ip_binding) {
  187. OpenXRInteractionProfileMetadata *meta_data = OpenXRInteractionProfileMetadata::get_singleton();
  188. action_map = p_action_map;
  189. interaction_profile = p_interaction_profile;
  190. ip_binding = p_ip_binding;
  191. String profile_path = interaction_profile->get_interaction_profile_path();
  192. if (ip_binding.is_valid()) {
  193. String action_name = "unset";
  194. String path_name = "unset";
  195. Ref<OpenXRAction> action = p_ip_binding->get_action();
  196. if (action.is_valid()) {
  197. action_name = action->get_name_with_set();
  198. }
  199. const OpenXRInteractionProfileMetadata::IOPath *io_path = meta_data->get_io_path(profile_path, p_ip_binding->get_binding_path());
  200. if (io_path != nullptr) {
  201. path_name = io_path->display_name;
  202. }
  203. create_dialog->set_base_type("OpenXRActionBindingModifier");
  204. set_title(TTR("Binding modifiers for:") + " " + action_name + ": " + path_name);
  205. } else if (interaction_profile.is_valid()) {
  206. String profile_name = profile_path;
  207. const OpenXRInteractionProfileMetadata::InteractionProfile *profile_def = meta_data->get_profile(profile_path);
  208. if (profile_def != nullptr) {
  209. profile_name = profile_def->display_name;
  210. }
  211. create_dialog->set_base_type("OpenXRIPBindingModifier");
  212. set_title(TTR("Binding modifiers for:") + " " + profile_name);
  213. }
  214. }