2
0

openxr_action_map_editor.cpp 14 KB

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