openxr_action_set_editor.cpp 8.4 KB

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