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