2
0

openxr_action_editor.cpp 7.8 KB

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