openxr_action_set_editor.cpp 12 KB

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