openxr_action_map_editor.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /**************************************************************************/
  2. /* openxr_action_map_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_action_map_editor.h"
  31. #include "core/config/project_settings.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_scale.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/gui/editor_file_dialog.h"
  36. // TODO implement redo/undo system
  37. void OpenXRActionMapEditor::_bind_methods() {
  38. ClassDB::bind_method("_add_action_set_editor", &OpenXRActionMapEditor::_add_action_set_editor);
  39. ClassDB::bind_method("_add_interaction_profile_editor", &OpenXRActionMapEditor::_add_interaction_profile_editor);
  40. ClassDB::bind_method(D_METHOD("_add_action_set", "name"), &OpenXRActionMapEditor::_add_action_set);
  41. ClassDB::bind_method(D_METHOD("_set_focus_on_action_set", "action_set"), &OpenXRActionMapEditor::_set_focus_on_action_set);
  42. ClassDB::bind_method(D_METHOD("_remove_action_set", "name"), &OpenXRActionMapEditor::_remove_action_set);
  43. ClassDB::bind_method(D_METHOD("_do_add_action_set_editor", "action_set_editor"), &OpenXRActionMapEditor::_do_add_action_set_editor);
  44. ClassDB::bind_method(D_METHOD("_do_remove_action_set_editor", "action_set_editor"), &OpenXRActionMapEditor::_do_remove_action_set_editor);
  45. ClassDB::bind_method(D_METHOD("_do_add_interaction_profile_editor", "interaction_profile_editor"), &OpenXRActionMapEditor::_do_add_interaction_profile_editor);
  46. ClassDB::bind_method(D_METHOD("_do_remove_interaction_profile_editor", "interaction_profile_editor"), &OpenXRActionMapEditor::_do_remove_interaction_profile_editor);
  47. }
  48. void OpenXRActionMapEditor::_notification(int p_what) {
  49. switch (p_what) {
  50. case NOTIFICATION_ENTER_TREE:
  51. case NOTIFICATION_THEME_CHANGED: {
  52. for (int i = 0; i < tabs->get_child_count(); i++) {
  53. Control *tab = Object::cast_to<Control>(tabs->get_child(i));
  54. if (tab) {
  55. tab->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("Tree")));
  56. }
  57. }
  58. } break;
  59. case NOTIFICATION_READY: {
  60. _create_action_sets();
  61. _create_interaction_profiles();
  62. } break;
  63. }
  64. }
  65. OpenXRActionSetEditor *OpenXRActionMapEditor::_add_action_set_editor(Ref<OpenXRActionSet> p_action_set) {
  66. ERR_FAIL_COND_V(p_action_set.is_null(), nullptr);
  67. OpenXRActionSetEditor *action_set_editor = memnew(OpenXRActionSetEditor(action_map, p_action_set));
  68. action_set_editor->connect("remove", callable_mp(this, &OpenXRActionMapEditor::_on_remove_action_set));
  69. action_set_editor->connect("action_removed", callable_mp(this, &OpenXRActionMapEditor::_on_action_removed));
  70. actionsets_vb->add_child(action_set_editor);
  71. return action_set_editor;
  72. }
  73. void OpenXRActionMapEditor::_create_action_sets() {
  74. if (action_map.is_valid()) {
  75. Array action_sets = action_map->get_action_sets();
  76. for (int i = 0; i < action_sets.size(); i++) {
  77. Ref<OpenXRActionSet> action_set = action_sets[i];
  78. _add_action_set_editor(action_set);
  79. }
  80. }
  81. }
  82. OpenXRInteractionProfileEditorBase *OpenXRActionMapEditor::_add_interaction_profile_editor(Ref<OpenXRInteractionProfile> p_interaction_profile) {
  83. ERR_FAIL_COND_V(p_interaction_profile.is_null(), nullptr);
  84. String profile_path = p_interaction_profile->get_interaction_profile_path();
  85. // need to instance the correct editor for our profile
  86. OpenXRInteractionProfileEditorBase *new_profile_editor = nullptr;
  87. if (profile_path == "placeholder_text") {
  88. // instance specific editor for this type
  89. } else {
  90. // instance generic editor
  91. new_profile_editor = memnew(OpenXRInteractionProfileEditor(action_map, p_interaction_profile));
  92. }
  93. // now add it in..
  94. ERR_FAIL_NULL_V(new_profile_editor, nullptr);
  95. tabs->add_child(new_profile_editor);
  96. new_profile_editor->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("Tree")));
  97. tabs->set_tab_button_icon(tabs->get_tab_count() - 1, get_theme_icon(SNAME("close"), SNAME("TabBar")));
  98. return new_profile_editor;
  99. }
  100. void OpenXRActionMapEditor::_create_interaction_profiles() {
  101. if (action_map.is_valid()) {
  102. Array new_interaction_profiles = action_map->get_interaction_profiles();
  103. for (int i = 0; i < new_interaction_profiles.size(); i++) {
  104. Ref<OpenXRInteractionProfile> interaction_profile = new_interaction_profiles[i];
  105. _add_interaction_profile_editor(interaction_profile);
  106. }
  107. }
  108. }
  109. OpenXRActionSetEditor *OpenXRActionMapEditor::_add_action_set(String p_name) {
  110. ERR_FAIL_COND_V(action_map.is_null(), nullptr);
  111. Ref<OpenXRActionSet> new_action_set;
  112. // add our new action set
  113. new_action_set.instantiate();
  114. new_action_set->set_name(p_name);
  115. new_action_set->set_localized_name(p_name);
  116. action_map->add_action_set(new_action_set);
  117. action_map->set_edited(true);
  118. // update our editor right away
  119. OpenXRActionSetEditor *action_set_editor = _add_action_set_editor(new_action_set);
  120. undo_redo->create_action(TTR("Add action set"));
  121. undo_redo->add_do_method(this, "_do_add_action_set_editor", action_set_editor);
  122. undo_redo->add_undo_method(this, "_do_remove_action_set_editor", action_set_editor);
  123. undo_redo->commit_action(false);
  124. return action_set_editor;
  125. }
  126. void OpenXRActionMapEditor::_remove_action_set(String p_name) {
  127. ERR_FAIL_COND(action_map.is_null());
  128. Ref<OpenXRActionSet> action_set = action_map->find_action_set(p_name);
  129. ERR_FAIL_COND(action_set.is_null());
  130. for (int i = 0; i < actionsets_vb->get_child_count(); i++) {
  131. OpenXRActionSetEditor *action_set_editor = Object::cast_to<OpenXRActionSetEditor>(actionsets_vb->get_child(i));
  132. if (action_set_editor && action_set_editor->get_action_set() == action_set) {
  133. _on_remove_action_set(action_set_editor);
  134. }
  135. }
  136. }
  137. void OpenXRActionMapEditor::_on_add_action_set() {
  138. ERR_FAIL_COND(action_map.is_null());
  139. String new_name = "New";
  140. int count = 0;
  141. while (action_map->find_action_set(new_name).is_valid()) {
  142. new_name = "New_" + itos(count++);
  143. }
  144. OpenXRActionSetEditor *new_action_set_editor = _add_action_set(new_name);
  145. // Make sure our action set is the current tab
  146. tabs->set_current_tab(0);
  147. call_deferred("_set_focus_on_action_set", new_action_set_editor);
  148. }
  149. void OpenXRActionMapEditor::_set_focus_on_action_set(OpenXRActionSetEditor *p_action_set_editor) {
  150. // Scroll down to our new entry
  151. actionsets_scroll->ensure_control_visible(p_action_set_editor);
  152. // Set focus on this entry
  153. p_action_set_editor->set_focus_on_entry();
  154. }
  155. void OpenXRActionMapEditor::_on_remove_action_set(Object *p_action_set_editor) {
  156. ERR_FAIL_COND(action_map.is_null());
  157. OpenXRActionSetEditor *action_set_editor = Object::cast_to<OpenXRActionSetEditor>(p_action_set_editor);
  158. ERR_FAIL_NULL(action_set_editor);
  159. ERR_FAIL_COND(action_set_editor->get_parent() != actionsets_vb);
  160. Ref<OpenXRActionSet> action_set = action_set_editor->get_action_set();
  161. ERR_FAIL_COND(action_set.is_null());
  162. action_set_editor->remove_all_actions();
  163. undo_redo->create_action(TTR("Remove action set"));
  164. undo_redo->add_do_method(this, "_do_remove_action_set_editor", action_set_editor);
  165. undo_redo->add_undo_method(this, "_do_add_action_set_editor", action_set_editor);
  166. undo_redo->commit_action(true);
  167. action_map->set_edited(true);
  168. }
  169. void OpenXRActionMapEditor::_on_action_removed(Ref<OpenXRAction> p_action) {
  170. for (int i = 0; i < tabs->get_tab_count(); i++) {
  171. // First tab won't be an interaction profile editor, but being thorough..
  172. OpenXRInteractionProfileEditorBase *interaction_profile_editor = Object::cast_to<OpenXRInteractionProfileEditorBase>(tabs->get_tab_control(i));
  173. if (interaction_profile_editor) {
  174. interaction_profile_editor->remove_all_bindings_for_action(p_action);
  175. }
  176. }
  177. }
  178. void OpenXRActionMapEditor::_on_add_interaction_profile() {
  179. ERR_FAIL_COND(action_map.is_null());
  180. PackedStringArray already_selected;
  181. for (int i = 0; i < action_map->get_interaction_profile_count(); i++) {
  182. already_selected.push_back(action_map->get_interaction_profile(i)->get_interaction_profile_path());
  183. }
  184. select_interaction_profile_dialog->open(already_selected);
  185. }
  186. void OpenXRActionMapEditor::_on_interaction_profile_selected(const String p_path) {
  187. ERR_FAIL_COND(action_map.is_null());
  188. Ref<OpenXRInteractionProfile> new_profile;
  189. new_profile.instantiate();
  190. new_profile->set_interaction_profile_path(p_path);
  191. action_map->add_interaction_profile(new_profile);
  192. action_map->set_edited(true);
  193. OpenXRInteractionProfileEditorBase *interaction_profile_editor = _add_interaction_profile_editor(new_profile);
  194. undo_redo->create_action(TTR("Add interaction profile"));
  195. undo_redo->add_do_method(this, "_do_add_interaction_profile_editor", interaction_profile_editor);
  196. undo_redo->add_undo_method(this, "_do_remove_interaction_profile_editor", interaction_profile_editor);
  197. undo_redo->commit_action(false);
  198. tabs->set_current_tab(tabs->get_tab_count() - 1);
  199. }
  200. void OpenXRActionMapEditor::_load_action_map(const String p_path, bool p_create_new_if_missing) {
  201. Error err = OK;
  202. action_map = ResourceLoader::load(p_path, "", ResourceFormatLoader::CACHE_MODE_IGNORE, &err);
  203. if (err != OK) {
  204. if ((err == ERR_FILE_NOT_FOUND || err == ERR_CANT_OPEN) && p_create_new_if_missing) {
  205. action_map.instantiate();
  206. action_map->create_default_action_sets();
  207. // Save it immediately
  208. err = ResourceSaver::save(action_map, p_path);
  209. if (err != OK) {
  210. // show warning but continue
  211. EditorNode::get_singleton()->show_warning(vformat(TTR("Error saving file %s: %s"), edited_path, error_names[err]));
  212. }
  213. } else {
  214. EditorNode::get_singleton()->show_warning(vformat(TTR("Error loading %s: %s."), edited_path, error_names[err]));
  215. edited_path = "";
  216. header_label->set_text("");
  217. return;
  218. }
  219. }
  220. edited_path = p_path;
  221. header_label->set_text(TTR("OpenXR Action map:") + " " + edited_path.get_file());
  222. }
  223. void OpenXRActionMapEditor::_on_save_action_map() {
  224. Error err = ResourceSaver::save(action_map, edited_path);
  225. if (err != OK) {
  226. EditorNode::get_singleton()->show_warning(vformat(TTR("Error saving file %s: %s"), edited_path, error_names[err]));
  227. return;
  228. }
  229. // TODO should clear undo/redo history
  230. // out with the old
  231. _clear_action_map();
  232. _create_action_sets();
  233. _create_interaction_profiles();
  234. }
  235. void OpenXRActionMapEditor::_on_reset_to_default_layout() {
  236. // TODO should clear undo/redo history
  237. // out with the old
  238. _clear_action_map();
  239. // create a new one
  240. action_map.unref();
  241. action_map.instantiate();
  242. action_map->create_default_action_sets();
  243. action_map->set_edited(true);
  244. _create_action_sets();
  245. _create_interaction_profiles();
  246. }
  247. void OpenXRActionMapEditor::_on_tabs_tab_changed(int p_tab) {
  248. }
  249. void OpenXRActionMapEditor::_on_tab_button_pressed(int p_tab) {
  250. OpenXRInteractionProfileEditorBase *interaction_profile_editor = Object::cast_to<OpenXRInteractionProfileEditorBase>(tabs->get_tab_control(p_tab));
  251. ERR_FAIL_NULL(interaction_profile_editor);
  252. undo_redo->create_action(TTR("Remove interaction profile"));
  253. undo_redo->add_do_method(this, "_do_remove_interaction_profile_editor", interaction_profile_editor);
  254. undo_redo->add_undo_method(this, "_do_add_interaction_profile_editor", interaction_profile_editor);
  255. undo_redo->commit_action(true);
  256. action_map->set_edited(true);
  257. }
  258. void OpenXRActionMapEditor::_do_add_action_set_editor(OpenXRActionSetEditor *p_action_set_editor) {
  259. Ref<OpenXRActionSet> action_set = p_action_set_editor->get_action_set();
  260. ERR_FAIL_COND(action_set.is_null());
  261. action_map->add_action_set(action_set);
  262. actionsets_vb->add_child(p_action_set_editor);
  263. }
  264. void OpenXRActionMapEditor::_do_remove_action_set_editor(OpenXRActionSetEditor *p_action_set_editor) {
  265. Ref<OpenXRActionSet> action_set = p_action_set_editor->get_action_set();
  266. ERR_FAIL_COND(action_set.is_null());
  267. actionsets_vb->remove_child(p_action_set_editor);
  268. action_map->remove_action_set(action_set);
  269. }
  270. void OpenXRActionMapEditor::_do_add_interaction_profile_editor(OpenXRInteractionProfileEditorBase *p_interaction_profile_editor) {
  271. Ref<OpenXRInteractionProfile> interaction_profile = p_interaction_profile_editor->get_interaction_profile();
  272. ERR_FAIL_COND(interaction_profile.is_null());
  273. action_map->add_interaction_profile(interaction_profile);
  274. tabs->add_child(p_interaction_profile_editor);
  275. tabs->set_tab_button_icon(tabs->get_tab_count() - 1, get_theme_icon(SNAME("close"), SNAME("TabBar")));
  276. tabs->set_current_tab(tabs->get_tab_count() - 1);
  277. }
  278. void OpenXRActionMapEditor::_do_remove_interaction_profile_editor(OpenXRInteractionProfileEditorBase *p_interaction_profile_editor) {
  279. Ref<OpenXRInteractionProfile> interaction_profile = p_interaction_profile_editor->get_interaction_profile();
  280. ERR_FAIL_COND(interaction_profile.is_null());
  281. tabs->remove_child(p_interaction_profile_editor);
  282. action_map->remove_interaction_profile(interaction_profile);
  283. }
  284. void OpenXRActionMapEditor::open_action_map(String p_path) {
  285. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  286. // out with the old...
  287. _clear_action_map();
  288. // now load in our new action map
  289. _load_action_map(p_path);
  290. _create_action_sets();
  291. _create_interaction_profiles();
  292. }
  293. void OpenXRActionMapEditor::_clear_action_map() {
  294. while (actionsets_vb->get_child_count() > 0) {
  295. Node *child = actionsets_vb->get_child(0);
  296. actionsets_vb->remove_child(child);
  297. child->queue_free();
  298. }
  299. for (int i = tabs->get_tab_count() - 1; i >= 0; --i) {
  300. // First tab won't be an interaction profile editor, but being thorough..
  301. OpenXRInteractionProfileEditorBase *interaction_profile_editor = Object::cast_to<OpenXRInteractionProfileEditorBase>(tabs->get_tab_control(i));
  302. if (interaction_profile_editor) {
  303. tabs->remove_child(interaction_profile_editor);
  304. interaction_profile_editor->queue_free();
  305. }
  306. }
  307. }
  308. OpenXRActionMapEditor::OpenXRActionMapEditor() {
  309. undo_redo = EditorUndoRedoManager::get_singleton();
  310. set_custom_minimum_size(Size2(0.0, 300.0));
  311. top_hb = memnew(HBoxContainer);
  312. add_child(top_hb);
  313. header_label = memnew(Label);
  314. header_label->set_text(String(TTR("Action Map")));
  315. header_label->set_clip_text(true);
  316. header_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  317. top_hb->add_child(header_label);
  318. add_action_set = memnew(Button);
  319. add_action_set->set_text(TTR("Add Action Set"));
  320. add_action_set->set_tooltip_text(TTR("Add an action set."));
  321. add_action_set->connect("pressed", callable_mp(this, &OpenXRActionMapEditor::_on_add_action_set));
  322. top_hb->add_child(add_action_set);
  323. add_interaction_profile = memnew(Button);
  324. add_interaction_profile->set_text(TTR("Add profile"));
  325. add_interaction_profile->set_tooltip_text(TTR("Add an interaction profile."));
  326. add_interaction_profile->connect("pressed", callable_mp(this, &OpenXRActionMapEditor::_on_add_interaction_profile));
  327. top_hb->add_child(add_interaction_profile);
  328. VSeparator *vseparator = memnew(VSeparator);
  329. top_hb->add_child(vseparator);
  330. save_as = memnew(Button);
  331. save_as->set_text(TTR("Save"));
  332. save_as->set_tooltip_text(TTR("Save this OpenXR action map."));
  333. save_as->connect("pressed", callable_mp(this, &OpenXRActionMapEditor::_on_save_action_map));
  334. top_hb->add_child(save_as);
  335. _default = memnew(Button);
  336. _default->set_text(TTR("Reset to Default"));
  337. _default->set_tooltip_text(TTR("Reset to default OpenXR action map."));
  338. _default->connect("pressed", callable_mp(this, &OpenXRActionMapEditor::_on_reset_to_default_layout));
  339. top_hb->add_child(_default);
  340. tabs = memnew(TabContainer);
  341. tabs->set_h_size_flags(SIZE_EXPAND_FILL);
  342. tabs->set_v_size_flags(SIZE_EXPAND_FILL);
  343. tabs->set_theme_type_variation("TabContainerOdd");
  344. tabs->connect("tab_changed", callable_mp(this, &OpenXRActionMapEditor::_on_tabs_tab_changed));
  345. tabs->connect("tab_button_pressed", callable_mp(this, &OpenXRActionMapEditor::_on_tab_button_pressed));
  346. add_child(tabs);
  347. actionsets_scroll = memnew(ScrollContainer);
  348. actionsets_scroll->set_h_size_flags(SIZE_EXPAND_FILL);
  349. actionsets_scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  350. actionsets_scroll->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  351. tabs->add_child(actionsets_scroll);
  352. actionsets_scroll->set_name(TTR("Action Sets"));
  353. actionsets_vb = memnew(VBoxContainer);
  354. actionsets_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  355. actionsets_scroll->add_child(actionsets_vb);
  356. select_interaction_profile_dialog = memnew(OpenXRSelectInteractionProfileDialog);
  357. select_interaction_profile_dialog->connect("interaction_profile_selected", callable_mp(this, &OpenXRActionMapEditor::_on_interaction_profile_selected));
  358. add_child(select_interaction_profile_dialog);
  359. // Our Action map editor is only shown if openxr is enabled in project settings
  360. // So load our action map and if it doesn't exist, create it right away.
  361. _load_action_map(GLOBAL_GET("xr/openxr/default_action_map"), true);
  362. }
  363. OpenXRActionMapEditor::~OpenXRActionMapEditor() {
  364. }