2
0

editor_expression_evaluator.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**************************************************************************/
  2. /* editor_expression_evaluator.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 "editor_expression_evaluator.h"
  31. #include "editor/debugger/editor_debugger_inspector.h"
  32. #include "editor/debugger/script_editor_debugger.h"
  33. #include "editor/editor_string_names.h"
  34. #include "scene/gui/button.h"
  35. #include "scene/gui/check_box.h"
  36. #include "scene/gui/line_edit.h"
  37. void EditorExpressionEvaluator::on_start() {
  38. expression_input->set_editable(false);
  39. evaluate_btn->set_disabled(true);
  40. if (clear_on_run_checkbox->is_pressed()) {
  41. inspector->clear_stack_variables();
  42. }
  43. }
  44. void EditorExpressionEvaluator::set_editor_debugger(ScriptEditorDebugger *p_editor_debugger) {
  45. editor_debugger = p_editor_debugger;
  46. }
  47. void EditorExpressionEvaluator::add_value(const Array &p_array) {
  48. inspector->add_stack_variable(p_array, 0);
  49. inspector->set_v_scroll(0);
  50. inspector->set_h_scroll(0);
  51. }
  52. void EditorExpressionEvaluator::_line_edit_gui_input(const Ref<InputEvent> &p_event) {
  53. if (!expression_input->is_editing()) {
  54. return;
  55. }
  56. const Ref<InputEventKey> k = p_event;
  57. if (k.is_null() || !k->is_pressed()) {
  58. return;
  59. }
  60. if (k->is_action_pressed("ui_up", true)) {
  61. int size = expression_history.size();
  62. if (expression_index < size - 1) {
  63. expression_index++;
  64. const String &history_text = expression_history[expression_history.size() - expression_index - 1];
  65. expression_input->set_text(history_text);
  66. expression_input->set_caret_column(history_text.size());
  67. }
  68. accept_event();
  69. } else if (k->is_action_pressed("ui_down", true)) {
  70. if (expression_index > 0) {
  71. expression_index--;
  72. const String &history_text = expression_history[expression_history.size() - expression_index - 1];
  73. expression_input->set_text(history_text);
  74. expression_input->set_caret_column(history_text.size());
  75. } else if (expression_index == 0) {
  76. expression_index = -1;
  77. expression_input->clear();
  78. }
  79. accept_event();
  80. }
  81. }
  82. void EditorExpressionEvaluator::_evaluate() {
  83. const String &expression = expression_input->get_text();
  84. if (expression.is_empty()) {
  85. return;
  86. }
  87. if (!editor_debugger->is_session_active()) {
  88. return;
  89. }
  90. expression_history.erase(expression);
  91. expression_history.push_back(expression);
  92. expression_index = -1;
  93. editor_debugger->request_remote_evaluate(expression, editor_debugger->get_stack_script_frame());
  94. expression_input->clear();
  95. }
  96. void EditorExpressionEvaluator::_clear() {
  97. inspector->clear_stack_variables();
  98. }
  99. void EditorExpressionEvaluator::_remote_object_selected(ObjectID p_id) {
  100. editor_debugger->emit_signal(SNAME("remote_objects_requested"), Array{ p_id });
  101. }
  102. void EditorExpressionEvaluator::_on_expression_input_changed(const String &p_expression) {
  103. evaluate_btn->set_disabled(p_expression.is_empty());
  104. expression_index = -1;
  105. }
  106. void EditorExpressionEvaluator::_on_debugger_breaked(bool p_breaked, bool p_can_debug) {
  107. expression_input->set_editable(p_breaked);
  108. evaluate_btn->set_disabled(!p_breaked);
  109. }
  110. void EditorExpressionEvaluator::_on_debugger_clear_execution(Ref<Script> p_stack_script) {
  111. expression_input->set_editable(false);
  112. evaluate_btn->set_disabled(true);
  113. }
  114. void EditorExpressionEvaluator::_notification(int p_what) {
  115. switch (p_what) {
  116. case NOTIFICATION_READY: {
  117. EditorDebuggerNode::get_singleton()->connect("breaked", callable_mp(this, &EditorExpressionEvaluator::_on_debugger_breaked));
  118. EditorDebuggerNode::get_singleton()->connect("clear_execution", callable_mp(this, &EditorExpressionEvaluator::_on_debugger_clear_execution));
  119. } break;
  120. case NOTIFICATION_THEME_CHANGED: {
  121. expression_input->add_theme_font_override(SceneStringName(font), get_theme_font(SNAME("expression"), EditorStringName(EditorFonts)));
  122. expression_input->add_theme_font_size_override(SceneStringName(font_size), get_theme_font_size(SNAME("expression_size"), EditorStringName(EditorFonts)));
  123. } break;
  124. }
  125. }
  126. EditorExpressionEvaluator::EditorExpressionEvaluator() {
  127. set_h_size_flags(SIZE_EXPAND_FILL);
  128. HBoxContainer *hb = memnew(HBoxContainer);
  129. add_child(hb);
  130. expression_input = memnew(LineEdit);
  131. expression_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  132. expression_input->set_placeholder(TTRC("Expression to evaluate (Up/Down: Navigate history)"));
  133. expression_input->set_accessibility_name(TTRC("Expression to evaluate"));
  134. expression_input->set_clear_button_enabled(true);
  135. expression_input->set_keep_editing_on_text_submit(true);
  136. expression_input->connect(SceneStringName(gui_input), callable_mp(this, &EditorExpressionEvaluator::_line_edit_gui_input));
  137. expression_input->connect(SceneStringName(text_submitted), callable_mp(this, &EditorExpressionEvaluator::_evaluate).unbind(1));
  138. expression_input->connect(SceneStringName(text_changed), callable_mp(this, &EditorExpressionEvaluator::_on_expression_input_changed));
  139. hb->add_child(expression_input);
  140. clear_on_run_checkbox = memnew(CheckBox);
  141. clear_on_run_checkbox->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  142. clear_on_run_checkbox->set_text(TTRC("Clear on Run"));
  143. clear_on_run_checkbox->set_pressed(true);
  144. hb->add_child(clear_on_run_checkbox);
  145. evaluate_btn = memnew(Button);
  146. evaluate_btn->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  147. evaluate_btn->set_text(TTRC("Evaluate"));
  148. evaluate_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorExpressionEvaluator::_evaluate));
  149. hb->add_child(evaluate_btn);
  150. clear_btn = memnew(Button);
  151. clear_btn->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  152. clear_btn->set_text(TTRC("Clear"));
  153. clear_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorExpressionEvaluator::_clear));
  154. hb->add_child(clear_btn);
  155. inspector = memnew(EditorDebuggerInspector);
  156. inspector->set_v_size_flags(SIZE_EXPAND_FILL);
  157. inspector->set_property_name_style(EditorPropertyNameProcessor::STYLE_RAW);
  158. inspector->set_read_only(true);
  159. inspector->connect("object_selected", callable_mp(this, &EditorExpressionEvaluator::_remote_object_selected));
  160. inspector->set_use_filter(true);
  161. add_child(inspector);
  162. expression_input->set_editable(false);
  163. evaluate_btn->set_disabled(true);
  164. }