openxr_action_editor.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**************************************************************************/
  2. /* openxr_action_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_editor.h"
  31. #include "editor/editor_string_names.h"
  32. void OpenXRActionEditor::_bind_methods() {
  33. ClassDB::bind_method(D_METHOD("_do_set_name", "name"), &OpenXRActionEditor::_do_set_name);
  34. ClassDB::bind_method(D_METHOD("_do_set_localized_name", "name"), &OpenXRActionEditor::_do_set_localized_name);
  35. ClassDB::bind_method(D_METHOD("_do_set_action_type", "type"), &OpenXRActionEditor::_do_set_action_type);
  36. ADD_SIGNAL(MethodInfo("remove", PropertyInfo(Variant::OBJECT, "action_editor")));
  37. }
  38. void OpenXRActionEditor::_theme_changed() {
  39. rem_action->set_icon(get_theme_icon(SNAME("Remove"), EditorStringName(EditorIcons)));
  40. }
  41. void OpenXRActionEditor::_notification(int p_what) {
  42. switch (p_what) {
  43. case NOTIFICATION_ENTER_TREE:
  44. case NOTIFICATION_THEME_CHANGED: {
  45. _theme_changed();
  46. } break;
  47. }
  48. }
  49. void OpenXRActionEditor::_on_action_name_changed(const String p_new_text) {
  50. if (action->get_name() != p_new_text) {
  51. undo_redo->create_action(TTR("Rename Action"));
  52. undo_redo->add_do_method(this, "_do_set_name", p_new_text);
  53. undo_redo->add_undo_method(this, "_do_set_name", action->get_name());
  54. undo_redo->commit_action(false);
  55. // If our localized name matches our action name, set this too
  56. if (action->get_name() == action->get_localized_name()) {
  57. undo_redo->create_action(TTR("Rename Actions Localized name"));
  58. undo_redo->add_do_method(this, "_do_set_localized_name", p_new_text);
  59. undo_redo->add_undo_method(this, "_do_set_localized_name", action->get_localized_name());
  60. undo_redo->commit_action(false);
  61. action->set_localized_name(p_new_text);
  62. action_localized_name->set_text(p_new_text);
  63. }
  64. action->set_name(p_new_text);
  65. action->set_edited(true);
  66. }
  67. }
  68. void OpenXRActionEditor::_do_set_name(const String p_new_text) {
  69. action->set_name(p_new_text);
  70. action->set_edited(true);
  71. action_name->set_text(p_new_text);
  72. }
  73. void OpenXRActionEditor::_on_action_localized_name_changed(const String p_new_text) {
  74. if (action->get_localized_name() != p_new_text) {
  75. undo_redo->create_action(TTR("Rename Actions Localized name"));
  76. undo_redo->add_do_method(this, "_do_set_localized_name", p_new_text);
  77. undo_redo->add_undo_method(this, "_do_set_localized_name", action->get_localized_name());
  78. undo_redo->commit_action(false);
  79. action->set_localized_name(p_new_text);
  80. action->set_edited(true);
  81. }
  82. }
  83. void OpenXRActionEditor::_do_set_localized_name(const String p_new_text) {
  84. action->set_localized_name(p_new_text);
  85. action->set_edited(true);
  86. action_localized_name->set_text(p_new_text);
  87. }
  88. void OpenXRActionEditor::_on_item_selected(int p_idx) {
  89. ERR_FAIL_INDEX(p_idx, OpenXRAction::OPENXR_ACTION_MAX);
  90. OpenXRAction::ActionType action_type = OpenXRAction::ActionType(p_idx);
  91. if (action->get_action_type() != action_type) {
  92. undo_redo->create_action(TTR("Change Action Type"));
  93. undo_redo->add_do_method(this, "_do_set_action_type", action_type);
  94. undo_redo->add_undo_method(this, "_do_set_action_type", action->get_action_type());
  95. undo_redo->commit_action(false);
  96. action->set_action_type(action_type);
  97. action->set_edited(true);
  98. }
  99. }
  100. void OpenXRActionEditor::_do_set_action_type(OpenXRAction::ActionType p_action_type) {
  101. action->set_action_type(p_action_type);
  102. action->set_edited(true);
  103. action_type_button->select(int(action->get_action_type()));
  104. }
  105. void OpenXRActionEditor::_on_remove_action() {
  106. emit_signal("remove", this);
  107. }
  108. OpenXRActionEditor::OpenXRActionEditor(Ref<OpenXRAction> p_action) {
  109. undo_redo = EditorUndoRedoManager::get_singleton();
  110. action = p_action;
  111. set_h_size_flags(Control::SIZE_EXPAND_FILL);
  112. action_name = memnew(LineEdit);
  113. action_name->set_text(action->get_name());
  114. action_name->set_custom_minimum_size(Size2(150.0, 0.0));
  115. action_name->connect("text_changed", callable_mp(this, &OpenXRActionEditor::_on_action_name_changed));
  116. add_child(action_name);
  117. action_localized_name = memnew(LineEdit);
  118. action_localized_name->set_text(action->get_localized_name());
  119. action_localized_name->set_custom_minimum_size(Size2(150.0, 0.0));
  120. action_localized_name->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  121. action_localized_name->connect("text_changed", callable_mp(this, &OpenXRActionEditor::_on_action_localized_name_changed));
  122. add_child(action_localized_name);
  123. action_type_button = memnew(OptionButton);
  124. action_type_button->add_item("Bool", OpenXRAction::OPENXR_ACTION_BOOL);
  125. action_type_button->add_item("Float", OpenXRAction::OPENXR_ACTION_FLOAT);
  126. action_type_button->add_item("Vector2", OpenXRAction::OPENXR_ACTION_VECTOR2);
  127. action_type_button->add_item("Pose", OpenXRAction::OPENXR_ACTION_POSE);
  128. action_type_button->add_item("Haptic", OpenXRAction::OPENXR_ACTION_HAPTIC);
  129. action_type_button->select(int(action->get_action_type()));
  130. action_type_button->set_custom_minimum_size(Size2(100.0, 0.0));
  131. action_type_button->connect("item_selected", callable_mp(this, &OpenXRActionEditor::_on_item_selected));
  132. add_child(action_type_button);
  133. // maybe add dropdown to edit our toplevel paths, or do we deduce them from our suggested bindings?
  134. rem_action = memnew(Button);
  135. rem_action->set_tooltip_text(TTR("Remove action"));
  136. rem_action->connect("pressed", callable_mp(this, &OpenXRActionEditor::_on_remove_action));
  137. rem_action->set_flat(true);
  138. add_child(rem_action);
  139. }