openxr_action_set_editor.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**************************************************************************/
  2. /* openxr_action_set_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_set_editor.h"
  31. #include "editor/editor_string_names.h"
  32. #include "editor/gui/editor_spin_slider.h"
  33. #include "editor/themes/editor_scale.h"
  34. #include "openxr_action_editor.h"
  35. #include "scene/gui/box_container.h"
  36. #include "scene/gui/button.h"
  37. #include "scene/gui/line_edit.h"
  38. #include "scene/gui/panel_container.h"
  39. #include "scene/gui/text_edit.h"
  40. void OpenXRActionSetEditor::_bind_methods() {
  41. ClassDB::bind_method(D_METHOD("_do_set_name", "name"), &OpenXRActionSetEditor::_do_set_name);
  42. ClassDB::bind_method(D_METHOD("_do_set_localized_name", "name"), &OpenXRActionSetEditor::_do_set_localized_name);
  43. ClassDB::bind_method(D_METHOD("_do_set_priority", "value"), &OpenXRActionSetEditor::_do_set_priority);
  44. ClassDB::bind_method(D_METHOD("_do_add_action_editor", "action_editor"), &OpenXRActionSetEditor::_do_add_action_editor);
  45. ClassDB::bind_method(D_METHOD("_do_remove_action_editor", "action_editor"), &OpenXRActionSetEditor::_do_remove_action_editor);
  46. ADD_SIGNAL(MethodInfo("remove", PropertyInfo(Variant::OBJECT, "action_set_editor")));
  47. ADD_SIGNAL(MethodInfo("action_removed", PropertyInfo(Variant::OBJECT, "action")));
  48. }
  49. void OpenXRActionSetEditor::_set_fold_icon() {
  50. if (is_expanded) {
  51. fold_btn->set_button_icon(get_theme_icon(SNAME("GuiTreeArrowDown"), EditorStringName(EditorIcons)));
  52. } else {
  53. fold_btn->set_button_icon(get_theme_icon(SNAME("GuiTreeArrowRight"), EditorStringName(EditorIcons)));
  54. }
  55. }
  56. void OpenXRActionSetEditor::_theme_changed() {
  57. _set_fold_icon();
  58. add_action->set_button_icon(get_theme_icon(SNAME("Add"), EditorStringName(EditorIcons)));
  59. rem_action_set->set_button_icon(get_theme_icon(SNAME("Remove"), EditorStringName(EditorIcons)));
  60. }
  61. void OpenXRActionSetEditor::_notification(int p_what) {
  62. switch (p_what) {
  63. case NOTIFICATION_ENTER_TREE:
  64. case NOTIFICATION_THEME_CHANGED: {
  65. _theme_changed();
  66. panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("TabContainer")));
  67. } break;
  68. }
  69. }
  70. OpenXRActionEditor *OpenXRActionSetEditor::_add_action_editor(Ref<OpenXRAction> p_action) {
  71. OpenXRActionEditor *action_editor = memnew(OpenXRActionEditor(p_action));
  72. action_editor->connect("remove", callable_mp(this, &OpenXRActionSetEditor::_on_remove_action));
  73. actions_vb->add_child(action_editor);
  74. return action_editor;
  75. }
  76. void OpenXRActionSetEditor::_on_toggle_expand() {
  77. is_expanded = !is_expanded;
  78. actions_vb->set_visible(is_expanded);
  79. _set_fold_icon();
  80. }
  81. void OpenXRActionSetEditor::_on_action_set_name_changed(const String p_new_text) {
  82. if (action_set->get_name() != p_new_text) {
  83. undo_redo->create_action(TTR("Rename Action Set"));
  84. undo_redo->add_do_method(this, "_do_set_name", p_new_text);
  85. undo_redo->add_undo_method(this, "_do_set_name", action_set->get_name());
  86. undo_redo->commit_action(false);
  87. // If our localized name matches our action set name, set this too
  88. if (action_set->get_name() == action_set->get_localized_name()) {
  89. undo_redo->create_action(TTR("Rename Action Sets Localized name"));
  90. undo_redo->add_do_method(this, "_do_set_localized_name", p_new_text);
  91. undo_redo->add_undo_method(this, "_do_set_localized_name", action_set->get_localized_name());
  92. undo_redo->commit_action(false);
  93. action_set->set_localized_name(p_new_text);
  94. action_set_localized_name->set_text(p_new_text);
  95. }
  96. action_set->set_name(p_new_text);
  97. action_set->set_edited(true);
  98. }
  99. }
  100. void OpenXRActionSetEditor::_do_set_name(const String p_new_text) {
  101. action_set->set_name(p_new_text);
  102. action_set_name->set_text(p_new_text);
  103. }
  104. void OpenXRActionSetEditor::_on_action_set_localized_name_changed(const String p_new_text) {
  105. if (action_set->get_localized_name() != p_new_text) {
  106. undo_redo->create_action(TTR("Rename Action Sets Localized name"));
  107. undo_redo->add_do_method(this, "_do_set_localized_name", p_new_text);
  108. undo_redo->add_undo_method(this, "_do_set_localized_name", action_set->get_localized_name());
  109. undo_redo->commit_action(false);
  110. action_set->set_localized_name(p_new_text);
  111. action_set->set_edited(true);
  112. }
  113. }
  114. void OpenXRActionSetEditor::_do_set_localized_name(const String p_new_text) {
  115. action_set->set_localized_name(p_new_text);
  116. action_set_localized_name->set_text(p_new_text);
  117. }
  118. void OpenXRActionSetEditor::_on_action_set_priority_changed(const double p_new_value) {
  119. int64_t value = (int64_t)p_new_value;
  120. if (action_set->get_priority() != value) {
  121. undo_redo->create_action(TTR("Change Action Sets priority"));
  122. undo_redo->add_do_method(this, "_do_set_priority", value);
  123. undo_redo->add_undo_method(this, "_do_set_priority", action_set->get_priority());
  124. undo_redo->commit_action(false);
  125. action_set->set_priority(value);
  126. action_set->set_edited(true);
  127. }
  128. }
  129. void OpenXRActionSetEditor::_do_set_priority(int64_t p_value) {
  130. action_set->set_priority(p_value);
  131. action_set_priority->set_value_no_signal(p_value);
  132. }
  133. void OpenXRActionSetEditor::_on_add_action() {
  134. Ref<OpenXRAction> new_action;
  135. new_action.instantiate();
  136. new_action->set_name("New");
  137. new_action->set_localized_name("New");
  138. action_set->add_action(new_action);
  139. action_set->set_edited(true);
  140. OpenXRActionEditor *action_editor = _add_action_editor(new_action);
  141. undo_redo->create_action(TTR("Add action"));
  142. undo_redo->add_do_method(this, "_do_add_action_editor", action_editor);
  143. undo_redo->add_undo_method(this, "_do_remove_action_editor", action_editor);
  144. undo_redo->commit_action(false);
  145. // TODO handle focus
  146. }
  147. void OpenXRActionSetEditor::_on_remove_action_set() {
  148. emit_signal("remove", this);
  149. }
  150. void OpenXRActionSetEditor::_on_remove_action(Object *p_action_editor) {
  151. OpenXRActionEditor *action_editor = Object::cast_to<OpenXRActionEditor>(p_action_editor);
  152. ERR_FAIL_NULL(action_editor);
  153. ERR_FAIL_COND(action_editor->get_parent() != actions_vb);
  154. Ref<OpenXRAction> action = action_editor->get_action();
  155. ERR_FAIL_COND(action.is_null());
  156. emit_signal("action_removed", action);
  157. undo_redo->create_action(TTR("Delete action"));
  158. undo_redo->add_do_method(this, "_do_remove_action_editor", action_editor);
  159. undo_redo->add_undo_method(this, "_do_add_action_editor", action_editor);
  160. undo_redo->commit_action(true);
  161. action_set->set_edited(true);
  162. }
  163. void OpenXRActionSetEditor::_do_add_action_editor(OpenXRActionEditor *p_action_editor) {
  164. Ref<OpenXRAction> action = p_action_editor->get_action();
  165. ERR_FAIL_COND(action.is_null());
  166. action_set->add_action(action);
  167. actions_vb->add_child(p_action_editor);
  168. }
  169. void OpenXRActionSetEditor::_do_remove_action_editor(OpenXRActionEditor *p_action_editor) {
  170. Ref<OpenXRAction> action = p_action_editor->get_action();
  171. ERR_FAIL_COND(action.is_null());
  172. actions_vb->remove_child(p_action_editor);
  173. action_set->remove_action(action);
  174. }
  175. void OpenXRActionSetEditor::remove_all_actions() {
  176. for (int i = actions_vb->get_child_count(); i > 0; --i) {
  177. _on_remove_action(actions_vb->get_child(i));
  178. }
  179. }
  180. void OpenXRActionSetEditor::set_focus_on_entry() {
  181. ERR_FAIL_NULL(action_set_name);
  182. action_set_name->grab_focus();
  183. }
  184. OpenXRActionSetEditor::OpenXRActionSetEditor(Ref<OpenXRActionMap> p_action_map, Ref<OpenXRActionSet> p_action_set) {
  185. undo_redo = EditorUndoRedoManager::get_singleton();
  186. action_map = p_action_map;
  187. action_set = p_action_set;
  188. set_h_size_flags(Control::SIZE_EXPAND_FILL);
  189. panel = memnew(PanelContainer);
  190. panel->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  191. add_child(panel);
  192. HBoxContainer *panel_hb = memnew(HBoxContainer);
  193. panel_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  194. panel->add_child(panel_hb);
  195. fold_btn = memnew(Button);
  196. fold_btn->set_v_size_flags(Control::SIZE_SHRINK_BEGIN);
  197. fold_btn->connect(SceneStringName(pressed), callable_mp(this, &OpenXRActionSetEditor::_on_toggle_expand));
  198. fold_btn->set_accessibility_name(TTRC("Fold"));
  199. fold_btn->set_flat(true);
  200. panel_hb->add_child(fold_btn);
  201. main_vb = memnew(VBoxContainer);
  202. main_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  203. panel_hb->add_child(main_vb);
  204. action_set_hb = memnew(HBoxContainer);
  205. action_set_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  206. main_vb->add_child(action_set_hb);
  207. action_set_name = memnew(LineEdit);
  208. action_set_name->set_text(action_set->get_name());
  209. action_set_name->set_tooltip_text(TTR("Internal name of the action. Some XR runtimes don't allow spaces or special characters."));
  210. action_set_name->set_custom_minimum_size(Size2(150.0 * EDSCALE, 0.0));
  211. action_set_name->connect(SceneStringName(text_changed), callable_mp(this, &OpenXRActionSetEditor::_on_action_set_name_changed));
  212. action_set_name->set_accessibility_name(TTRC("Action Set Name"));
  213. action_set_hb->add_child(action_set_name);
  214. action_set_localized_name = memnew(LineEdit);
  215. action_set_localized_name->set_text(action_set->get_localized_name());
  216. action_set_localized_name->set_tooltip_text(TTR("Human-readable name of the action set. This can be displayed to end users."));
  217. action_set_localized_name->set_custom_minimum_size(Size2(150.0 * EDSCALE, 0.0));
  218. action_set_localized_name->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  219. action_set_localized_name->connect(SceneStringName(text_changed), callable_mp(this, &OpenXRActionSetEditor::_on_action_set_localized_name_changed));
  220. action_set_localized_name->set_accessibility_name(TTRC("Action Set Localized Name"));
  221. action_set_hb->add_child(action_set_localized_name);
  222. action_set_priority = memnew(EditorSpinSlider);
  223. action_set_priority->set_tooltip_text(TTR("Priority of the action set. If multiple action sets bind to the same input, the action set with the highest priority will be updated."));
  224. action_set_priority->set_editing_integer(true);
  225. action_set_priority->set_min(2147483647.0);
  226. action_set_priority->set_min(-2147483648.0);
  227. action_set_priority->set_value_no_signal(action_set->get_priority());
  228. action_set_priority->set_custom_minimum_size(Size2(75.0 * EDSCALE, 0.0));
  229. action_set_priority->connect(SceneStringName(value_changed), callable_mp(this, &OpenXRActionSetEditor::_on_action_set_priority_changed));
  230. action_set_priority->set_accessibility_name(TTRC("Action Set Priority"));
  231. action_set_hb->add_child(action_set_priority);
  232. add_action = memnew(Button);
  233. add_action->set_tooltip_text(TTR("Add action."));
  234. add_action->connect(SceneStringName(pressed), callable_mp(this, &OpenXRActionSetEditor::_on_add_action));
  235. add_action->set_accessibility_name(TTRC("Add"));
  236. add_action->set_flat(true);
  237. action_set_hb->add_child(add_action);
  238. rem_action_set = memnew(Button);
  239. rem_action_set->set_tooltip_text(TTR("Remove action set."));
  240. rem_action_set->connect(SceneStringName(pressed), callable_mp(this, &OpenXRActionSetEditor::_on_remove_action_set));
  241. rem_action_set->set_accessibility_name(TTRC("Remove"));
  242. rem_action_set->set_flat(true);
  243. action_set_hb->add_child(rem_action_set);
  244. actions_vb = memnew(VBoxContainer);
  245. actions_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  246. main_vb->add_child(actions_vb);
  247. // Add our existing actions
  248. Array actions = action_set->get_actions();
  249. for (int i = 0; i < actions.size(); i++) {
  250. Ref<OpenXRAction> action = actions[i];
  251. _add_action_editor(action);
  252. }
  253. }