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