editor_expression_evaluator.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "scene/gui/button.h"
  34. #include "scene/gui/check_box.h"
  35. void EditorExpressionEvaluator::on_start() {
  36. expression_input->set_editable(false);
  37. evaluate_btn->set_disabled(true);
  38. if (clear_on_run_checkbox->is_pressed()) {
  39. inspector->clear_stack_variables();
  40. }
  41. }
  42. void EditorExpressionEvaluator::set_editor_debugger(ScriptEditorDebugger *p_editor_debugger) {
  43. editor_debugger = p_editor_debugger;
  44. }
  45. void EditorExpressionEvaluator::add_value(const Array &p_array) {
  46. inspector->add_stack_variable(p_array, 0);
  47. inspector->set_v_scroll(0);
  48. inspector->set_h_scroll(0);
  49. }
  50. void EditorExpressionEvaluator::_evaluate() {
  51. const String &expression = expression_input->get_text();
  52. if (expression.is_empty()) {
  53. return;
  54. }
  55. if (!editor_debugger->is_session_active()) {
  56. return;
  57. }
  58. editor_debugger->request_remote_evaluate(expression, editor_debugger->get_stack_script_frame());
  59. expression_input->clear();
  60. }
  61. void EditorExpressionEvaluator::_clear() {
  62. inspector->clear_stack_variables();
  63. }
  64. void EditorExpressionEvaluator::_remote_object_selected(ObjectID p_id) {
  65. editor_debugger->emit_signal(SNAME("remote_object_requested"), p_id);
  66. }
  67. void EditorExpressionEvaluator::_on_expression_input_changed(const String &p_expression) {
  68. evaluate_btn->set_disabled(p_expression.is_empty());
  69. }
  70. void EditorExpressionEvaluator::_on_debugger_breaked(bool p_breaked, bool p_can_debug) {
  71. expression_input->set_editable(p_breaked);
  72. evaluate_btn->set_disabled(!p_breaked);
  73. }
  74. void EditorExpressionEvaluator::_on_debugger_clear_execution(Ref<Script> p_stack_script) {
  75. expression_input->set_editable(false);
  76. evaluate_btn->set_disabled(true);
  77. }
  78. void EditorExpressionEvaluator::_notification(int p_what) {
  79. switch (p_what) {
  80. case NOTIFICATION_READY: {
  81. EditorDebuggerNode::get_singleton()->connect("breaked", callable_mp(this, &EditorExpressionEvaluator::_on_debugger_breaked));
  82. EditorDebuggerNode::get_singleton()->connect("clear_execution", callable_mp(this, &EditorExpressionEvaluator::_on_debugger_clear_execution));
  83. } break;
  84. }
  85. }
  86. EditorExpressionEvaluator::EditorExpressionEvaluator() {
  87. set_h_size_flags(SIZE_EXPAND_FILL);
  88. HBoxContainer *hb = memnew(HBoxContainer);
  89. add_child(hb);
  90. expression_input = memnew(LineEdit);
  91. expression_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  92. expression_input->set_placeholder(TTR("Expression to evaluate"));
  93. expression_input->set_clear_button_enabled(true);
  94. expression_input->connect(SceneStringName(text_submitted), callable_mp(this, &EditorExpressionEvaluator::_evaluate).unbind(1));
  95. expression_input->connect(SceneStringName(text_changed), callable_mp(this, &EditorExpressionEvaluator::_on_expression_input_changed));
  96. hb->add_child(expression_input);
  97. clear_on_run_checkbox = memnew(CheckBox);
  98. clear_on_run_checkbox->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  99. clear_on_run_checkbox->set_text(TTR("Clear on Run"));
  100. clear_on_run_checkbox->set_pressed(true);
  101. hb->add_child(clear_on_run_checkbox);
  102. evaluate_btn = memnew(Button);
  103. evaluate_btn->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  104. evaluate_btn->set_text(TTR("Evaluate"));
  105. evaluate_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorExpressionEvaluator::_evaluate));
  106. hb->add_child(evaluate_btn);
  107. clear_btn = memnew(Button);
  108. clear_btn->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  109. clear_btn->set_text(TTR("Clear"));
  110. clear_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorExpressionEvaluator::_clear));
  111. hb->add_child(clear_btn);
  112. inspector = memnew(EditorDebuggerInspector);
  113. inspector->set_v_size_flags(SIZE_EXPAND_FILL);
  114. inspector->set_property_name_style(EditorPropertyNameProcessor::STYLE_RAW);
  115. inspector->set_read_only(true);
  116. inspector->connect("object_selected", callable_mp(this, &EditorExpressionEvaluator::_remote_object_selected));
  117. inspector->set_use_filter(true);
  118. add_child(inspector);
  119. expression_input->set_editable(false);
  120. evaluate_btn->set_disabled(true);
  121. }