openxr_interaction_profile_editor.cpp 12 KB

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