openxr_binding_modifiers_dialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. void OpenXRBindingModifiersDialog::_bind_methods() {
  34. ClassDB::bind_method(D_METHOD("_do_add_binding_modifier_editor", "binding_modifier_editor"), &OpenXRBindingModifiersDialog::_do_add_binding_modifier_editor);
  35. ClassDB::bind_method(D_METHOD("_do_remove_binding_modifier_editor", "binding_modifier_editor"), &OpenXRBindingModifiersDialog::_do_remove_binding_modifier_editor);
  36. }
  37. void OpenXRBindingModifiersDialog::_notification(int p_what) {
  38. switch (p_what) {
  39. case NOTIFICATION_READY: {
  40. _create_binding_modifiers();
  41. } break;
  42. case NOTIFICATION_THEME_CHANGED: {
  43. if (binding_modifier_sc) {
  44. binding_modifier_sc->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  45. }
  46. } break;
  47. }
  48. }
  49. OpenXRBindingModifierEditor *OpenXRBindingModifiersDialog::_add_binding_modifier_editor(Ref<OpenXRBindingModifier> p_binding_modifier) {
  50. ERR_FAIL_COND_V(p_binding_modifier.is_null(), nullptr);
  51. String class_name = p_binding_modifier->get_class();
  52. ERR_FAIL_COND_V(class_name.is_empty(), nullptr);
  53. String editor_class = OpenXRActionMapEditor::get_binding_modifier_editor_class(class_name);
  54. ERR_FAIL_COND_V(editor_class.is_empty(), nullptr);
  55. OpenXRBindingModifierEditor *new_editor = nullptr;
  56. Object *obj = ClassDB::instantiate(editor_class);
  57. if (obj) {
  58. new_editor = Object::cast_to<OpenXRBindingModifierEditor>(obj);
  59. if (!new_editor) {
  60. // Not of correct type?? Free it.
  61. memfree(obj);
  62. }
  63. }
  64. ERR_FAIL_NULL_V(new_editor, nullptr);
  65. new_editor->setup(action_map, p_binding_modifier);
  66. new_editor->connect("binding_modifier_removed", callable_mp(this, &OpenXRBindingModifiersDialog::_on_remove_binding_modifier));
  67. binding_modifiers_vb->add_child(new_editor);
  68. new_editor->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  69. return new_editor;
  70. }
  71. void OpenXRBindingModifiersDialog::_create_binding_modifiers() {
  72. Array new_binding_modifiers;
  73. if (ip_binding.is_valid()) {
  74. new_binding_modifiers = ip_binding->get_binding_modifiers();
  75. } else if (interaction_profile.is_valid()) {
  76. new_binding_modifiers = interaction_profile->get_binding_modifiers();
  77. } else {
  78. ERR_FAIL_MSG("No binding nor interaction profile specified.");
  79. }
  80. for (int i = 0; i < new_binding_modifiers.size(); i++) {
  81. Ref<OpenXRBindingModifier> binding_modifier = new_binding_modifiers[i];
  82. _add_binding_modifier_editor(binding_modifier);
  83. }
  84. }
  85. void OpenXRBindingModifiersDialog::_on_add_binding_modifier() {
  86. create_dialog->popup_create(false);
  87. }
  88. void OpenXRBindingModifiersDialog::_on_remove_binding_modifier(Object *p_binding_modifier_editor) {
  89. if (ip_binding.is_valid()) {
  90. ip_binding->set_edited(true);
  91. } else if (interaction_profile.is_valid()) {
  92. interaction_profile->set_edited(true);
  93. } else {
  94. ERR_FAIL_MSG("No binding nor interaction profile specified.");
  95. }
  96. OpenXRBindingModifierEditor *binding_modifier_editor = Object::cast_to<OpenXRBindingModifierEditor>(p_binding_modifier_editor);
  97. ERR_FAIL_NULL(binding_modifier_editor);
  98. ERR_FAIL_COND(binding_modifier_editor->get_parent() != binding_modifiers_vb);
  99. undo_redo->create_action(TTR("Remove binding modifier"));
  100. undo_redo->add_do_method(this, "_do_remove_binding_modifier_editor", binding_modifier_editor);
  101. undo_redo->add_undo_method(this, "_do_add_binding_modifier_editor", binding_modifier_editor);
  102. undo_redo->commit_action(true);
  103. }
  104. void OpenXRBindingModifiersDialog::_on_dialog_created() {
  105. // Instance new binding modifier object
  106. Variant obj = create_dialog->instantiate_selected();
  107. ERR_FAIL_COND(obj.get_type() != Variant::OBJECT);
  108. Ref<OpenXRBindingModifier> new_binding_modifier = obj;
  109. ERR_FAIL_COND(new_binding_modifier.is_null());
  110. if (ip_binding.is_valid()) {
  111. // Add it to our binding.
  112. ip_binding->add_binding_modifier(new_binding_modifier);
  113. ip_binding->set_edited(true);
  114. } else if (interaction_profile.is_valid()) {
  115. // Add it to our interaction profile.
  116. interaction_profile->add_binding_modifier(new_binding_modifier);
  117. interaction_profile->set_edited(true);
  118. } else {
  119. ERR_FAIL_MSG("No binding nor interaction profile specified.");
  120. }
  121. // Create our editor for this.
  122. OpenXRBindingModifierEditor *binding_modifier_editor = _add_binding_modifier_editor(new_binding_modifier);
  123. ERR_FAIL_NULL(binding_modifier_editor);
  124. // Add undo/redo.
  125. undo_redo->create_action(TTR("Add binding modifier"));
  126. undo_redo->add_do_method(this, "_do_add_binding_modifier_editor", binding_modifier_editor);
  127. undo_redo->add_undo_method(this, "_do_remove_binding_modifier_editor", binding_modifier_editor);
  128. undo_redo->commit_action(false);
  129. }
  130. void OpenXRBindingModifiersDialog::_do_add_binding_modifier_editor(OpenXRBindingModifierEditor *p_binding_modifier_editor) {
  131. Ref<OpenXRBindingModifier> binding_modifier = p_binding_modifier_editor->get_binding_modifier();
  132. ERR_FAIL_COND(binding_modifier.is_null());
  133. if (ip_binding.is_valid()) {
  134. // Add it to our binding
  135. ip_binding->add_binding_modifier(binding_modifier);
  136. } else if (interaction_profile.is_valid()) {
  137. // Add it to our interaction profile
  138. interaction_profile->add_binding_modifier(binding_modifier);
  139. } else {
  140. ERR_FAIL_MSG("No binding nor interaction profile specified.");
  141. }
  142. binding_modifiers_vb->add_child(p_binding_modifier_editor);
  143. }
  144. void OpenXRBindingModifiersDialog::_do_remove_binding_modifier_editor(OpenXRBindingModifierEditor *p_binding_modifier_editor) {
  145. Ref<OpenXRBindingModifier> binding_modifier = p_binding_modifier_editor->get_binding_modifier();
  146. ERR_FAIL_COND(binding_modifier.is_null());
  147. if (ip_binding.is_valid()) {
  148. // Remove it from our binding.
  149. ip_binding->remove_binding_modifier(binding_modifier);
  150. } else if (interaction_profile.is_valid()) {
  151. // Removed it to from interaction profile.
  152. interaction_profile->remove_binding_modifier(binding_modifier);
  153. } else {
  154. ERR_FAIL_MSG("No binding nor interaction profile specified.");
  155. }
  156. binding_modifiers_vb->remove_child(p_binding_modifier_editor);
  157. }
  158. OpenXRBindingModifiersDialog::OpenXRBindingModifiersDialog() {
  159. undo_redo = EditorUndoRedoManager::get_singleton();
  160. set_transient(true);
  161. binding_modifier_sc = memnew(ScrollContainer);
  162. binding_modifier_sc->set_custom_minimum_size(Size2(350.0, 0.0));
  163. binding_modifier_sc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  164. binding_modifier_sc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  165. binding_modifier_sc->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  166. add_child(binding_modifier_sc);
  167. binding_modifiers_vb = memnew(VBoxContainer);
  168. binding_modifiers_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  169. binding_modifier_sc->add_child(binding_modifiers_vb);
  170. binding_warning_label = memnew(Label);
  171. binding_warning_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
  172. binding_warning_label->set_text(TTR("Note: modifiers will only be applied if supported on the host system."));
  173. binding_modifiers_vb->add_child(binding_warning_label);
  174. add_binding_modifier_btn = memnew(Button);
  175. add_binding_modifier_btn->set_text(TTR("Add binding modifier"));
  176. add_binding_modifier_btn->connect("pressed", callable_mp(this, &OpenXRBindingModifiersDialog::_on_add_binding_modifier));
  177. binding_modifiers_vb->add_child(add_binding_modifier_btn);
  178. // TODO may need to create our own dialog for this that can filter on binding modifiers recorded on interaction profiles or on individual bindings.
  179. create_dialog = memnew(CreateDialog);
  180. create_dialog->set_transient(true);
  181. create_dialog->connect("create", callable_mp(this, &OpenXRBindingModifiersDialog::_on_dialog_created));
  182. add_child(create_dialog);
  183. }
  184. void OpenXRBindingModifiersDialog::setup(Ref<OpenXRActionMap> p_action_map, Ref<OpenXRInteractionProfile> p_interaction_profile, Ref<OpenXRIPBinding> p_ip_binding) {
  185. OpenXRInteractionProfileMetadata *meta_data = OpenXRInteractionProfileMetadata::get_singleton();
  186. action_map = p_action_map;
  187. interaction_profile = p_interaction_profile;
  188. ip_binding = p_ip_binding;
  189. String profile_path = interaction_profile->get_interaction_profile_path();
  190. if (ip_binding.is_valid()) {
  191. String action_name = "unset";
  192. String path_name = "unset";
  193. Ref<OpenXRAction> action = p_ip_binding->get_action();
  194. if (action.is_valid()) {
  195. action_name = action->get_name_with_set();
  196. }
  197. const OpenXRInteractionProfileMetadata::IOPath *io_path = meta_data->get_io_path(profile_path, p_ip_binding->get_binding_path());
  198. if (io_path != nullptr) {
  199. path_name = io_path->display_name;
  200. }
  201. create_dialog->set_base_type("OpenXRActionBindingModifier");
  202. set_title(TTR("Binding modifiers for:") + " " + action_name + ": " + path_name);
  203. } else if (interaction_profile.is_valid()) {
  204. String profile_name = profile_path;
  205. const OpenXRInteractionProfileMetadata::InteractionProfile *profile_def = meta_data->get_profile(profile_path);
  206. if (profile_def != nullptr) {
  207. profile_name = profile_def->display_name;
  208. }
  209. create_dialog->set_base_type("OpenXRIPBindingModifier");
  210. set_title(TTR("Binding modifiers for:") + " " + profile_name);
  211. }
  212. }