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