openxr_interaction_profile_editor.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**************************************************************************/
  2. /* openxr_interaction_profile_editor.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_interaction_profile_editor.h"
  31. #include "editor/editor_string_names.h"
  32. #include "scene/gui/box_container.h"
  33. #include "scene/gui/button.h"
  34. #include "scene/gui/label.h"
  35. #include "scene/gui/line_edit.h"
  36. #include "scene/gui/panel_container.h"
  37. #include "scene/gui/separator.h"
  38. #include "scene/gui/text_edit.h"
  39. ///////////////////////////////////////////////////////////////////////////
  40. // Interaction profile editor base
  41. void OpenXRInteractionProfileEditorBase::_bind_methods() {
  42. ClassDB::bind_method(D_METHOD("_add_binding", "action", "path"), &OpenXRInteractionProfileEditorBase::_add_binding);
  43. ClassDB::bind_method(D_METHOD("_remove_binding", "action", "path"), &OpenXRInteractionProfileEditorBase::_remove_binding);
  44. }
  45. void OpenXRInteractionProfileEditorBase::_notification(int p_what) {
  46. switch (p_what) {
  47. case NOTIFICATION_ENTER_TREE: {
  48. _update_interaction_profile();
  49. } break;
  50. case NOTIFICATION_THEME_CHANGED: {
  51. _theme_changed();
  52. } break;
  53. }
  54. }
  55. void OpenXRInteractionProfileEditorBase::_do_update_interaction_profile() {
  56. if (!is_dirty) {
  57. is_dirty = true;
  58. callable_mp(this, &OpenXRInteractionProfileEditorBase::_update_interaction_profile).call_deferred();
  59. }
  60. }
  61. void OpenXRInteractionProfileEditorBase::_add_binding(const String p_action, const String p_path) {
  62. ERR_FAIL_COND(action_map.is_null());
  63. ERR_FAIL_COND(interaction_profile.is_null());
  64. Ref<OpenXRAction> action = action_map->get_action(p_action);
  65. ERR_FAIL_COND(action.is_null());
  66. Ref<OpenXRIPBinding> binding = interaction_profile->get_binding_for_action(action);
  67. if (binding.is_null()) {
  68. // create a new binding
  69. binding.instantiate();
  70. binding->set_action(action);
  71. interaction_profile->add_binding(binding);
  72. interaction_profile->set_edited(true);
  73. }
  74. binding->add_path(p_path);
  75. binding->set_edited(true);
  76. // Update our toplevel paths
  77. action->set_toplevel_paths(action_map->get_top_level_paths(action));
  78. _do_update_interaction_profile();
  79. }
  80. void OpenXRInteractionProfileEditorBase::_remove_binding(const String p_action, const String p_path) {
  81. ERR_FAIL_COND(action_map.is_null());
  82. ERR_FAIL_COND(interaction_profile.is_null());
  83. Ref<OpenXRAction> action = action_map->get_action(p_action);
  84. ERR_FAIL_COND(action.is_null());
  85. Ref<OpenXRIPBinding> binding = interaction_profile->get_binding_for_action(action);
  86. if (binding.is_valid()) {
  87. binding->remove_path(p_path);
  88. binding->set_edited(true);
  89. if (binding->get_path_count() == 0) {
  90. interaction_profile->remove_binding(binding);
  91. interaction_profile->set_edited(true);
  92. }
  93. // Update our toplevel paths
  94. action->set_toplevel_paths(action_map->get_top_level_paths(action));
  95. _do_update_interaction_profile();
  96. }
  97. }
  98. void OpenXRInteractionProfileEditorBase::remove_all_bindings_for_action(Ref<OpenXRAction> p_action) {
  99. Ref<OpenXRIPBinding> binding = interaction_profile->get_binding_for_action(p_action);
  100. if (binding.is_valid()) {
  101. String action_name = p_action->get_name_with_set();
  102. // for our undo/redo we process all paths
  103. undo_redo->create_action(TTR("Remove action from interaction profile"));
  104. PackedStringArray paths = binding->get_paths();
  105. for (const String &path : paths) {
  106. undo_redo->add_do_method(this, "_remove_binding", action_name, path);
  107. undo_redo->add_undo_method(this, "_add_binding", action_name, path);
  108. }
  109. undo_redo->commit_action(false);
  110. // but we take a shortcut here :)
  111. interaction_profile->remove_binding(binding);
  112. interaction_profile->set_edited(true);
  113. // Update our toplevel paths
  114. p_action->set_toplevel_paths(action_map->get_top_level_paths(p_action));
  115. _do_update_interaction_profile();
  116. }
  117. }
  118. OpenXRInteractionProfileEditorBase::OpenXRInteractionProfileEditorBase(Ref<OpenXRActionMap> p_action_map, Ref<OpenXRInteractionProfile> p_interaction_profile) {
  119. undo_redo = EditorUndoRedoManager::get_singleton();
  120. action_map = p_action_map;
  121. interaction_profile = p_interaction_profile;
  122. String profile_path = interaction_profile->get_interaction_profile_path();
  123. String profile_name = profile_path;
  124. profile_def = OpenXRInteractionProfileMetadata::get_singleton()->get_profile(profile_path);
  125. if (profile_def != nullptr) {
  126. profile_name = profile_def->display_name;
  127. }
  128. set_name(profile_name);
  129. set_h_size_flags(SIZE_EXPAND_FILL);
  130. set_v_size_flags(SIZE_EXPAND_FILL);
  131. // Make sure it is updated when it enters the tree...
  132. is_dirty = true;
  133. }
  134. ///////////////////////////////////////////////////////////////////////////
  135. // Default interaction profile editor
  136. void OpenXRInteractionProfileEditor::select_action_for(const String p_io_path) {
  137. selecting_for_io_path = p_io_path;
  138. select_action_dialog->open();
  139. }
  140. void OpenXRInteractionProfileEditor::action_selected(const String p_action) {
  141. undo_redo->create_action(TTR("Add binding"));
  142. undo_redo->add_do_method(this, "_add_binding", p_action, selecting_for_io_path);
  143. undo_redo->add_undo_method(this, "_remove_binding", p_action, selecting_for_io_path);
  144. undo_redo->commit_action(true);
  145. selecting_for_io_path = "";
  146. }
  147. void OpenXRInteractionProfileEditor::_on_remove_pressed(const String p_action, const String p_for_io_path) {
  148. undo_redo->create_action(TTR("Remove binding"));
  149. undo_redo->add_do_method(this, "_remove_binding", p_action, p_for_io_path);
  150. undo_redo->add_undo_method(this, "_add_binding", p_action, p_for_io_path);
  151. undo_redo->commit_action(true);
  152. }
  153. void OpenXRInteractionProfileEditor::_add_io_path(VBoxContainer *p_container, const OpenXRInteractionProfileMetadata::IOPath *p_io_path) {
  154. HBoxContainer *path_hb = memnew(HBoxContainer);
  155. path_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  156. p_container->add_child(path_hb);
  157. Label *path_label = memnew(Label);
  158. path_label->set_text(p_io_path->display_name);
  159. path_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  160. path_hb->add_child(path_label);
  161. Label *type_label = memnew(Label);
  162. switch (p_io_path->action_type) {
  163. case OpenXRAction::OPENXR_ACTION_BOOL: {
  164. type_label->set_text(TTR("Boolean"));
  165. } break;
  166. case OpenXRAction::OPENXR_ACTION_FLOAT: {
  167. type_label->set_text(TTR("Float"));
  168. } break;
  169. case OpenXRAction::OPENXR_ACTION_VECTOR2: {
  170. type_label->set_text(TTR("Vector2"));
  171. } break;
  172. case OpenXRAction::OPENXR_ACTION_POSE: {
  173. type_label->set_text(TTR("Pose"));
  174. } break;
  175. case OpenXRAction::OPENXR_ACTION_HAPTIC: {
  176. type_label->set_text(TTR("Haptic"));
  177. } break;
  178. default: {
  179. type_label->set_text(TTR("Unknown"));
  180. } break;
  181. }
  182. type_label->set_custom_minimum_size(Size2(50.0, 0.0));
  183. path_hb->add_child(type_label);
  184. Button *path_add = memnew(Button);
  185. path_add->set_icon(get_theme_icon(SNAME("Add"), EditorStringName(EditorIcons)));
  186. path_add->set_flat(true);
  187. path_add->connect("pressed", callable_mp(this, &OpenXRInteractionProfileEditor::select_action_for).bind(String(p_io_path->openxr_path)));
  188. path_hb->add_child(path_add);
  189. if (interaction_profile.is_valid()) {
  190. String io_path = String(p_io_path->openxr_path);
  191. Array bindings = interaction_profile->get_bindings();
  192. for (int i = 0; i < bindings.size(); i++) {
  193. Ref<OpenXRIPBinding> binding = bindings[i];
  194. if (binding->has_path(io_path)) {
  195. Ref<OpenXRAction> action = binding->get_action();
  196. HBoxContainer *action_hb = memnew(HBoxContainer);
  197. action_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  198. p_container->add_child(action_hb);
  199. Control *indent_node = memnew(Control);
  200. indent_node->set_custom_minimum_size(Size2(10.0, 0.0));
  201. action_hb->add_child(indent_node);
  202. Label *action_label = memnew(Label);
  203. action_label->set_text(action->get_name_with_set() + ": " + action->get_localized_name());
  204. action_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  205. action_hb->add_child(action_label);
  206. Button *action_rem = memnew(Button);
  207. action_rem->set_flat(true);
  208. action_rem->set_icon(get_theme_icon(SNAME("Remove"), EditorStringName(EditorIcons)));
  209. action_rem->connect("pressed", callable_mp((OpenXRInteractionProfileEditor *)this, &OpenXRInteractionProfileEditor::_on_remove_pressed).bind(action->get_name_with_set(), String(p_io_path->openxr_path)));
  210. action_hb->add_child(action_rem);
  211. }
  212. }
  213. }
  214. }
  215. void OpenXRInteractionProfileEditor::_update_interaction_profile() {
  216. ERR_FAIL_NULL(profile_def);
  217. if (!is_dirty) {
  218. // no need to update
  219. return;
  220. }
  221. // out with the old...
  222. while (main_hb->get_child_count() > 0) {
  223. memdelete(main_hb->get_child(0));
  224. }
  225. // in with the new...
  226. // Determine toplevel paths
  227. Vector<String> top_level_paths;
  228. for (int i = 0; i < profile_def->io_paths.size(); i++) {
  229. const OpenXRInteractionProfileMetadata::IOPath *io_path = &profile_def->io_paths[i];
  230. if (!top_level_paths.has(io_path->top_level_path)) {
  231. top_level_paths.push_back(io_path->top_level_path);
  232. }
  233. }
  234. for (int i = 0; i < top_level_paths.size(); i++) {
  235. PanelContainer *panel = memnew(PanelContainer);
  236. panel->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  237. main_hb->add_child(panel);
  238. panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("TabContainer")));
  239. VBoxContainer *container = memnew(VBoxContainer);
  240. panel->add_child(container);
  241. Label *label = memnew(Label);
  242. label->set_text(OpenXRInteractionProfileMetadata::get_singleton()->get_top_level_name(top_level_paths[i]));
  243. container->add_child(label);
  244. for (int j = 0; j < profile_def->io_paths.size(); j++) {
  245. const OpenXRInteractionProfileMetadata::IOPath *io_path = &profile_def->io_paths[j];
  246. if (io_path->top_level_path == top_level_paths[i]) {
  247. _add_io_path(container, io_path);
  248. }
  249. }
  250. }
  251. // and we've updated it...
  252. is_dirty = false;
  253. }
  254. void OpenXRInteractionProfileEditor::_theme_changed() {
  255. for (int i = 0; i < main_hb->get_child_count(); i++) {
  256. Control *panel = Object::cast_to<Control>(main_hb->get_child(i));
  257. if (panel) {
  258. panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("TabContainer")));
  259. }
  260. }
  261. }
  262. OpenXRInteractionProfileEditor::OpenXRInteractionProfileEditor(Ref<OpenXRActionMap> p_action_map, Ref<OpenXRInteractionProfile> p_interaction_profile) :
  263. OpenXRInteractionProfileEditorBase(p_action_map, p_interaction_profile) {
  264. main_hb = memnew(HBoxContainer);
  265. add_child(main_hb);
  266. select_action_dialog = memnew(OpenXRSelectActionDialog(p_action_map));
  267. select_action_dialog->connect("action_selected", callable_mp(this, &OpenXRInteractionProfileEditor::action_selected));
  268. add_child(select_action_dialog);
  269. }