openxr_action_set_editor.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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) 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_set_editor.h"
  31. #include "openxr_action_editor.h"
  32. void OpenXRActionSetEditor::_bind_methods() {
  33. ADD_SIGNAL(MethodInfo("remove", PropertyInfo(Variant::OBJECT, "action_set_editor")));
  34. ADD_SIGNAL(MethodInfo("action_removed"));
  35. }
  36. void OpenXRActionSetEditor::_set_fold_icon() {
  37. if (is_expanded) {
  38. fold_btn->set_icon(get_theme_icon(SNAME("GuiTreeArrowDown"), SNAME("EditorIcons")));
  39. } else {
  40. fold_btn->set_icon(get_theme_icon(SNAME("GuiTreeArrowRight"), SNAME("EditorIcons")));
  41. }
  42. }
  43. void OpenXRActionSetEditor::_theme_changed() {
  44. _set_fold_icon();
  45. add_action->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  46. rem_action_set->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  47. }
  48. void OpenXRActionSetEditor::_notification(int p_what) {
  49. switch (p_what) {
  50. case NOTIFICATION_ENTER_TREE:
  51. case NOTIFICATION_THEME_CHANGED: {
  52. _theme_changed();
  53. panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("TabContainer")));
  54. } break;
  55. }
  56. }
  57. OpenXRActionEditor *OpenXRActionSetEditor::_add_action_editor(Ref<OpenXRAction> p_action) {
  58. OpenXRActionEditor *action_editor = memnew(OpenXRActionEditor(p_action));
  59. action_editor->connect("remove", callable_mp(this, &OpenXRActionSetEditor::_on_remove_action));
  60. actions_vb->add_child(action_editor);
  61. return action_editor;
  62. }
  63. void OpenXRActionSetEditor::_update_actions() {
  64. // out with the old...
  65. while (actions_vb->get_child_count() > 0) {
  66. memdelete(actions_vb->get_child(0));
  67. }
  68. // in with the new...
  69. Array actions = action_set->get_actions();
  70. for (int i = 0; i < actions.size(); i++) {
  71. Ref<OpenXRAction> action = actions[i];
  72. _add_action_editor(action);
  73. }
  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. // TODO validate if entry is allowed
  82. // If our localized name matches our action set name, set this too
  83. if (action_set->get_name() == action_set->get_localized_name()) {
  84. action_set->set_localized_name(p_new_text);
  85. action_set_localized_name->set_text(p_new_text);
  86. }
  87. action_set->set_name(p_new_text);
  88. }
  89. void OpenXRActionSetEditor::_on_action_set_localized_name_changed(const String p_new_text) {
  90. action_set->set_localized_name(p_new_text);
  91. }
  92. void OpenXRActionSetEditor::_on_action_set_priority_changed(const String p_new_text) {
  93. int64_t value = p_new_text.to_int();
  94. action_set->set_priority(value);
  95. }
  96. void OpenXRActionSetEditor::_on_add_action() {
  97. Ref<OpenXRAction> new_action;
  98. new_action.instantiate();
  99. new_action->set_name("New");
  100. new_action->set_localized_name("New");
  101. action_set->add_action(new_action);
  102. _add_action_editor(new_action);
  103. // TODO handle focus
  104. }
  105. void OpenXRActionSetEditor::_on_remove_action_set() {
  106. emit_signal("remove", this);
  107. }
  108. void OpenXRActionSetEditor::_on_remove_action(Object *p_action_editor) {
  109. OpenXRActionEditor *action_editor = Object::cast_to<OpenXRActionEditor>(p_action_editor);
  110. ERR_FAIL_NULL(action_editor);
  111. ERR_FAIL_COND(action_editor->get_parent() != actions_vb);
  112. Ref<OpenXRAction> action = action_editor->get_action();
  113. ERR_FAIL_COND(action.is_null());
  114. // TODO add undo/redo action
  115. // TODO find where this action is used by our interaction profiles and remove it there
  116. // And remove it....
  117. action_map->remove_action(action->get_name_with_set()); // remove it from the set and any interaction profile it relates to
  118. actions_vb->remove_child(action_editor);
  119. action_editor->queue_delete();
  120. // Let action map editor know so we can update our interaction profiles
  121. emit_signal("action_removed");
  122. }
  123. void OpenXRActionSetEditor::set_focus_on_entry() {
  124. ERR_FAIL_NULL(action_set_name);
  125. action_set_name->grab_focus();
  126. }
  127. OpenXRActionSetEditor::OpenXRActionSetEditor(Ref<OpenXRActionMap> p_action_map, Ref<OpenXRActionSet> p_action_set) {
  128. action_map = p_action_map;
  129. action_set = p_action_set;
  130. set_h_size_flags(Control::SIZE_EXPAND_FILL);
  131. panel = memnew(PanelContainer);
  132. panel->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  133. add_child(panel);
  134. HBoxContainer *panel_hb = memnew(HBoxContainer);
  135. panel_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  136. panel->add_child(panel_hb);
  137. fold_btn = memnew(Button);
  138. fold_btn->set_v_size_flags(Control::SIZE_SHRINK_BEGIN);
  139. fold_btn->connect("pressed", callable_mp(this, &OpenXRActionSetEditor::_on_toggle_expand));
  140. fold_btn->set_flat(true);
  141. panel_hb->add_child(fold_btn);
  142. main_vb = memnew(VBoxContainer);
  143. main_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  144. panel_hb->add_child(main_vb);
  145. action_set_hb = memnew(HBoxContainer);
  146. action_set_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  147. main_vb->add_child(action_set_hb);
  148. action_set_name = memnew(LineEdit);
  149. action_set_name->set_text(action_set->get_name());
  150. action_set_name->set_custom_minimum_size(Size2(150.0, 0.0));
  151. action_set_name->connect("text_changed", callable_mp(this, &OpenXRActionSetEditor::_on_action_set_name_changed));
  152. action_set_hb->add_child(action_set_name);
  153. action_set_localized_name = memnew(LineEdit);
  154. action_set_localized_name->set_text(action_set->get_localized_name());
  155. action_set_localized_name->set_custom_minimum_size(Size2(150.0, 0.0));
  156. action_set_localized_name->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  157. action_set_localized_name->connect("text_changed", callable_mp(this, &OpenXRActionSetEditor::_on_action_set_localized_name_changed));
  158. action_set_hb->add_child(action_set_localized_name);
  159. action_set_priority = memnew(TextEdit);
  160. action_set_priority->set_text(itos(action_set->get_priority()));
  161. action_set_priority->set_custom_minimum_size(Size2(50.0, 0.0));
  162. action_set_priority->connect("text_changed", callable_mp(this, &OpenXRActionSetEditor::_on_action_set_priority_changed));
  163. action_set_hb->add_child(action_set_priority);
  164. add_action = memnew(Button);
  165. add_action->set_tooltip("Add Action.");
  166. add_action->connect("pressed", callable_mp(this, &OpenXRActionSetEditor::_on_add_action));
  167. add_action->set_flat(true);
  168. action_set_hb->add_child(add_action);
  169. rem_action_set = memnew(Button);
  170. rem_action_set->set_tooltip("Remove Action Set.");
  171. rem_action_set->connect("pressed", callable_mp(this, &OpenXRActionSetEditor::_on_remove_action_set));
  172. rem_action_set->set_flat(true);
  173. action_set_hb->add_child(rem_action_set);
  174. actions_vb = memnew(VBoxContainer);
  175. actions_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  176. main_vb->add_child(actions_vb);
  177. _update_actions();
  178. }