openxr_action_set_editor.cpp 12 KB

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