openxr_action_map_editor.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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) 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_action_map_editor.h"
  31. #include "core/config/project_settings.h"
  32. #include "editor/editor_file_dialog.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_scale.h"
  35. #include "editor/editor_settings.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("_update_action_sets", &OpenXRActionMapEditor::_update_action_sets);
  40. ClassDB::bind_method("_add_interaction_profile_editor", &OpenXRActionMapEditor::_add_interaction_profile_editor);
  41. ClassDB::bind_method("_update_interaction_profiles", &OpenXRActionMapEditor::_update_interaction_profiles);
  42. ClassDB::bind_method(D_METHOD("_add_action_set", "name"), &OpenXRActionMapEditor::_add_action_set);
  43. ClassDB::bind_method(D_METHOD("_set_focus_on_action_set", "action_set"), &OpenXRActionMapEditor::_set_focus_on_action_set);
  44. ClassDB::bind_method(D_METHOD("_remove_action_set", "name"), &OpenXRActionMapEditor::_remove_action_set);
  45. }
  46. void OpenXRActionMapEditor::_notification(int p_what) {
  47. switch (p_what) {
  48. case NOTIFICATION_ENTER_TREE:
  49. case NOTIFICATION_THEME_CHANGED: {
  50. for (int i = 0; i < tabs->get_child_count(); i++) {
  51. Control *tab = static_cast<Control *>(tabs->get_child(i));
  52. if (tab) {
  53. tab->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("Tree")));
  54. }
  55. }
  56. } break;
  57. case NOTIFICATION_READY: {
  58. _update_action_sets();
  59. _update_interaction_profiles();
  60. } break;
  61. }
  62. }
  63. OpenXRActionSetEditor *OpenXRActionMapEditor::_add_action_set_editor(Ref<OpenXRActionSet> p_action_set) {
  64. ERR_FAIL_COND_V(p_action_set.is_null(), nullptr);
  65. OpenXRActionSetEditor *action_set_editor = memnew(OpenXRActionSetEditor(action_map, p_action_set));
  66. action_set_editor->connect("remove", callable_mp(this, &OpenXRActionMapEditor::_on_remove_action_set));
  67. action_set_editor->connect("action_removed", callable_mp(this, &OpenXRActionMapEditor::_on_action_removed));
  68. actionsets_vb->add_child(action_set_editor);
  69. return action_set_editor;
  70. }
  71. void OpenXRActionMapEditor::_update_action_sets() {
  72. // out with the old...
  73. while (actionsets_vb->get_child_count() > 0) {
  74. memdelete(actionsets_vb->get_child(0));
  75. }
  76. // in with the new...
  77. if (action_map.is_valid()) {
  78. Array action_sets = action_map->get_action_sets();
  79. for (int i = 0; i < action_sets.size(); i++) {
  80. Ref<OpenXRActionSet> action_set = action_sets[i];
  81. _add_action_set_editor(action_set);
  82. }
  83. }
  84. }
  85. OpenXRInteractionProfileEditorBase *OpenXRActionMapEditor::_add_interaction_profile_editor(Ref<OpenXRInteractionProfile> p_interaction_profile) {
  86. ERR_FAIL_COND_V(p_interaction_profile.is_null(), nullptr);
  87. String profile_path = p_interaction_profile->get_interaction_profile_path();
  88. // need to instance the correct editor for our profile
  89. OpenXRInteractionProfileEditorBase *new_profile_editor = nullptr;
  90. if (profile_path == "placeholder_text") {
  91. // instance specific editor for this type
  92. } else {
  93. // instance generic editor
  94. new_profile_editor = memnew(OpenXRInteractionProfileEditor(action_map, p_interaction_profile));
  95. }
  96. // now add it in..
  97. ERR_FAIL_NULL_V(new_profile_editor, nullptr);
  98. tabs->add_child(new_profile_editor);
  99. new_profile_editor->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("Tree")));
  100. tabs->set_tab_button_icon(tabs->get_tab_count() - 1, get_theme_icon(SNAME("close"), SNAME("TabBar")));
  101. interaction_profiles.push_back(new_profile_editor);
  102. return new_profile_editor;
  103. }
  104. void OpenXRActionMapEditor::_update_interaction_profiles() {
  105. // out with the old...
  106. while (interaction_profiles.size() > 0) {
  107. Node *interaction_profile = interaction_profiles[0];
  108. interaction_profiles.remove_at(0);
  109. tabs->remove_child(interaction_profile);
  110. interaction_profile->queue_delete();
  111. }
  112. // in with the new...
  113. if (action_map.is_valid()) {
  114. Array new_interaction_profiles = action_map->get_interaction_profiles();
  115. for (int i = 0; i < new_interaction_profiles.size(); i++) {
  116. Ref<OpenXRInteractionProfile> interaction_profile = new_interaction_profiles[i];
  117. _add_interaction_profile_editor(interaction_profile);
  118. }
  119. }
  120. }
  121. OpenXRActionSetEditor *OpenXRActionMapEditor::_add_action_set(String p_name) {
  122. ERR_FAIL_COND_V(action_map.is_null(), nullptr);
  123. Ref<OpenXRActionSet> new_action_set;
  124. // add our new action set
  125. new_action_set.instantiate();
  126. new_action_set->set_name(p_name);
  127. new_action_set->set_localized_name(p_name);
  128. action_map->add_action_set(new_action_set);
  129. // update our editor right away
  130. return _add_action_set_editor(new_action_set);
  131. }
  132. void OpenXRActionMapEditor::_remove_action_set(String p_name) {
  133. ERR_FAIL_COND(action_map.is_null());
  134. Ref<OpenXRActionSet> action_set = action_map->find_action_set(p_name);
  135. ERR_FAIL_COND(action_set.is_null());
  136. if (action_set->get_action_count() > 0) {
  137. // we should remove these and add to our redo/undo step before calling _remove_action_set
  138. WARN_PRINT("Action set still has associated actions before being removed!");
  139. }
  140. // now we remove it
  141. action_map->remove_action_set(action_set);
  142. }
  143. void OpenXRActionMapEditor::_on_add_action_set() {
  144. ERR_FAIL_COND(action_map.is_null());
  145. String new_name = "New";
  146. int count = 0;
  147. while (action_map->find_action_set(new_name).is_valid()) {
  148. new_name = "New_" + itos(count++);
  149. }
  150. OpenXRActionSetEditor *new_action_set_editor = _add_action_set(new_name);
  151. // Make sure our action set is the current tab
  152. tabs->set_current_tab(0);
  153. call_deferred("_set_focus_on_action_set", new_action_set_editor);
  154. }
  155. void OpenXRActionMapEditor::_set_focus_on_action_set(OpenXRActionSetEditor *p_action_set_editor) {
  156. // Scroll down to our new entry
  157. actionsets_scroll->ensure_control_visible(p_action_set_editor);
  158. // Set focus on this entry
  159. p_action_set_editor->set_focus_on_entry();
  160. }
  161. void OpenXRActionMapEditor::_on_remove_action_set(Object *p_action_set_editor) {
  162. ERR_FAIL_COND(action_map.is_null());
  163. OpenXRActionSetEditor *action_set_editor = Object::cast_to<OpenXRActionSetEditor>(p_action_set_editor);
  164. ERR_FAIL_NULL(action_set_editor);
  165. ERR_FAIL_COND(action_set_editor->get_parent() != actionsets_vb);
  166. Ref<OpenXRActionSet> action_set = action_set_editor->get_action_set();
  167. ERR_FAIL_COND(action_set.is_null());
  168. action_map->remove_action_set(action_set);
  169. actionsets_vb->remove_child(action_set_editor);
  170. action_set_editor->queue_delete();
  171. }
  172. void OpenXRActionMapEditor::_on_action_removed() {
  173. // make sure our interaction profiles are updated
  174. _update_interaction_profiles();
  175. }
  176. void OpenXRActionMapEditor::_on_add_interaction_profile() {
  177. ERR_FAIL_COND(action_map.is_null());
  178. PackedStringArray already_selected;
  179. for (int i = 0; i < action_map->get_interaction_profile_count(); i++) {
  180. already_selected.push_back(action_map->get_interaction_profile(i)->get_interaction_profile_path());
  181. }
  182. select_interaction_profile_dialog->open(already_selected);
  183. }
  184. void OpenXRActionMapEditor::_on_interaction_profile_selected(const String p_path) {
  185. ERR_FAIL_COND(action_map.is_null());
  186. Ref<OpenXRInteractionProfile> new_profile;
  187. new_profile.instantiate();
  188. new_profile->set_interaction_profile_path(p_path);
  189. action_map->add_interaction_profile(new_profile);
  190. _add_interaction_profile_editor(new_profile);
  191. tabs->set_current_tab(tabs->get_tab_count() - 1);
  192. }
  193. void OpenXRActionMapEditor::_load_action_map(const String p_path, bool p_create_new_if_missing) {
  194. action_map = ResourceLoader::load(p_path, "", ResourceFormatLoader::CACHE_MODE_IGNORE);
  195. if (action_map.is_null()) {
  196. if (p_create_new_if_missing) {
  197. action_map.instantiate();
  198. action_map->create_default_action_sets();
  199. } else {
  200. EditorNode::get_singleton()->show_warning(TTR("Invalid file, not an OpenXR action map."));
  201. edited_path = "";
  202. header_label->set_text("");
  203. return;
  204. }
  205. }
  206. edited_path = p_path;
  207. header_label->set_text(TTR("OpenXR Action map:") + " " + p_path.get_file());
  208. }
  209. void OpenXRActionMapEditor::_on_save_action_map() {
  210. Error err = ResourceSaver::save(action_map, edited_path);
  211. if (err != OK) {
  212. EditorNode::get_singleton()->show_warning(vformat(TTR("Error saving file: %s"), edited_path));
  213. return;
  214. }
  215. _update_action_sets();
  216. _update_interaction_profiles();
  217. }
  218. void OpenXRActionMapEditor::_on_reset_to_default_layout() {
  219. // create a new one
  220. action_map.unref();
  221. action_map.instantiate();
  222. action_map->create_default_action_sets();
  223. _update_action_sets();
  224. _update_interaction_profiles();
  225. }
  226. void OpenXRActionMapEditor::_on_tabs_tab_changed(int p_tab) {
  227. }
  228. void OpenXRActionMapEditor::_on_tab_button_pressed(int p_tab) {
  229. OpenXRInteractionProfileEditorBase *profile_editor = static_cast<OpenXRInteractionProfileEditorBase *>(tabs->get_tab_control(p_tab));
  230. ERR_FAIL_NULL(profile_editor);
  231. Ref<OpenXRInteractionProfile> interaction_profile = profile_editor->get_interaction_profile();
  232. ERR_FAIL_COND(interaction_profile.is_null());
  233. action_map->remove_interaction_profile(interaction_profile);
  234. tabs->remove_child(profile_editor);
  235. profile_editor->queue_delete();
  236. }
  237. void OpenXRActionMapEditor::open_action_map(String p_path) {
  238. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  239. _load_action_map(p_path);
  240. _update_action_sets();
  241. _update_interaction_profiles();
  242. }
  243. OpenXRActionMapEditor::OpenXRActionMapEditor() {
  244. set_custom_minimum_size(Size2(0.0, 300.0));
  245. top_hb = memnew(HBoxContainer);
  246. add_child(top_hb);
  247. header_label = memnew(Label);
  248. header_label->set_text(String(TTR("Action Map")));
  249. header_label->set_clip_text(true);
  250. header_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  251. top_hb->add_child(header_label);
  252. add_action_set = memnew(Button);
  253. add_action_set->set_text(TTR("Add Action Set"));
  254. add_action_set->set_tooltip_text(TTR("Add an action set."));
  255. add_action_set->connect("pressed", callable_mp(this, &OpenXRActionMapEditor::_on_add_action_set));
  256. top_hb->add_child(add_action_set);
  257. add_interaction_profile = memnew(Button);
  258. add_interaction_profile->set_text(TTR("Add profile"));
  259. add_interaction_profile->set_tooltip_text(TTR("Add an interaction profile."));
  260. add_interaction_profile->connect("pressed", callable_mp(this, &OpenXRActionMapEditor::_on_add_interaction_profile));
  261. top_hb->add_child(add_interaction_profile);
  262. VSeparator *vseparator = memnew(VSeparator);
  263. top_hb->add_child(vseparator);
  264. save_as = memnew(Button);
  265. save_as->set_text(TTR("Save"));
  266. save_as->set_tooltip_text(TTR("Save this OpenXR action map."));
  267. save_as->connect("pressed", callable_mp(this, &OpenXRActionMapEditor::_on_save_action_map));
  268. top_hb->add_child(save_as);
  269. _default = memnew(Button);
  270. _default->set_text(TTR("Reset to Default"));
  271. _default->set_tooltip_text(TTR("Reset to default OpenXR action map."));
  272. _default->connect("pressed", callable_mp(this, &OpenXRActionMapEditor::_on_reset_to_default_layout));
  273. top_hb->add_child(_default);
  274. tabs = memnew(TabContainer);
  275. tabs->set_h_size_flags(SIZE_EXPAND_FILL);
  276. tabs->set_v_size_flags(SIZE_EXPAND_FILL);
  277. tabs->set_theme_type_variation("TabContainerOdd");
  278. tabs->connect("tab_changed", callable_mp(this, &OpenXRActionMapEditor::_on_tabs_tab_changed));
  279. tabs->connect("tab_button_pressed", callable_mp(this, &OpenXRActionMapEditor::_on_tab_button_pressed));
  280. add_child(tabs);
  281. actionsets_scroll = memnew(ScrollContainer);
  282. actionsets_scroll->set_h_size_flags(SIZE_EXPAND_FILL);
  283. actionsets_scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  284. actionsets_scroll->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  285. tabs->add_child(actionsets_scroll);
  286. actionsets_scroll->set_name(TTR("Action Sets"));
  287. actionsets_vb = memnew(VBoxContainer);
  288. actionsets_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  289. actionsets_scroll->add_child(actionsets_vb);
  290. select_interaction_profile_dialog = memnew(OpenXRSelectInteractionProfileDialog);
  291. select_interaction_profile_dialog->connect("interaction_profile_selected", callable_mp(this, &OpenXRActionMapEditor::_on_interaction_profile_selected));
  292. add_child(select_interaction_profile_dialog);
  293. _load_action_map(GLOBAL_GET("xr/openxr/default_action_map"));
  294. }
  295. OpenXRActionMapEditor::~OpenXRActionMapEditor() {
  296. }