Path: blob/master/editor/debugger/editor_expression_evaluator.cpp
9898 views
/**************************************************************************/1/* editor_expression_evaluator.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_expression_evaluator.h"3132#include "editor/debugger/editor_debugger_inspector.h"33#include "editor/debugger/script_editor_debugger.h"34#include "scene/gui/button.h"35#include "scene/gui/check_box.h"36#include "scene/gui/line_edit.h"3738void EditorExpressionEvaluator::on_start() {39expression_input->set_editable(false);40evaluate_btn->set_disabled(true);4142if (clear_on_run_checkbox->is_pressed()) {43inspector->clear_stack_variables();44}45}4647void EditorExpressionEvaluator::set_editor_debugger(ScriptEditorDebugger *p_editor_debugger) {48editor_debugger = p_editor_debugger;49}5051void EditorExpressionEvaluator::add_value(const Array &p_array) {52inspector->add_stack_variable(p_array, 0);53inspector->set_v_scroll(0);54inspector->set_h_scroll(0);55}5657void EditorExpressionEvaluator::_evaluate() {58const String &expression = expression_input->get_text();59if (expression.is_empty()) {60return;61}6263if (!editor_debugger->is_session_active()) {64return;65}6667editor_debugger->request_remote_evaluate(expression, editor_debugger->get_stack_script_frame());6869expression_input->clear();70}7172void EditorExpressionEvaluator::_clear() {73inspector->clear_stack_variables();74}7576void EditorExpressionEvaluator::_remote_object_selected(ObjectID p_id) {77Array arr = { p_id };78editor_debugger->emit_signal(SNAME("remote_objects_requested"), arr);79}8081void EditorExpressionEvaluator::_on_expression_input_changed(const String &p_expression) {82evaluate_btn->set_disabled(p_expression.is_empty());83}8485void EditorExpressionEvaluator::_on_debugger_breaked(bool p_breaked, bool p_can_debug) {86expression_input->set_editable(p_breaked);87evaluate_btn->set_disabled(!p_breaked);88}8990void EditorExpressionEvaluator::_on_debugger_clear_execution(Ref<Script> p_stack_script) {91expression_input->set_editable(false);92evaluate_btn->set_disabled(true);93}9495void EditorExpressionEvaluator::_notification(int p_what) {96switch (p_what) {97case NOTIFICATION_READY: {98EditorDebuggerNode::get_singleton()->connect("breaked", callable_mp(this, &EditorExpressionEvaluator::_on_debugger_breaked));99EditorDebuggerNode::get_singleton()->connect("clear_execution", callable_mp(this, &EditorExpressionEvaluator::_on_debugger_clear_execution));100} break;101}102}103104EditorExpressionEvaluator::EditorExpressionEvaluator() {105set_h_size_flags(SIZE_EXPAND_FILL);106107HBoxContainer *hb = memnew(HBoxContainer);108add_child(hb);109110expression_input = memnew(LineEdit);111expression_input->set_h_size_flags(Control::SIZE_EXPAND_FILL);112expression_input->set_placeholder(TTR("Expression to evaluate"));113expression_input->set_accessibility_name(TTRC("Expression to evaluate"));114expression_input->set_clear_button_enabled(true);115expression_input->connect(SceneStringName(text_submitted), callable_mp(this, &EditorExpressionEvaluator::_evaluate).unbind(1));116expression_input->connect(SceneStringName(text_changed), callable_mp(this, &EditorExpressionEvaluator::_on_expression_input_changed));117hb->add_child(expression_input);118119clear_on_run_checkbox = memnew(CheckBox);120clear_on_run_checkbox->set_h_size_flags(Control::SIZE_SHRINK_CENTER);121clear_on_run_checkbox->set_text(TTR("Clear on Run"));122clear_on_run_checkbox->set_pressed(true);123hb->add_child(clear_on_run_checkbox);124125evaluate_btn = memnew(Button);126evaluate_btn->set_h_size_flags(Control::SIZE_SHRINK_CENTER);127evaluate_btn->set_text(TTR("Evaluate"));128evaluate_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorExpressionEvaluator::_evaluate));129hb->add_child(evaluate_btn);130131clear_btn = memnew(Button);132clear_btn->set_h_size_flags(Control::SIZE_SHRINK_CENTER);133clear_btn->set_text(TTR("Clear"));134clear_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorExpressionEvaluator::_clear));135hb->add_child(clear_btn);136137inspector = memnew(EditorDebuggerInspector);138inspector->set_v_size_flags(SIZE_EXPAND_FILL);139inspector->set_property_name_style(EditorPropertyNameProcessor::STYLE_RAW);140inspector->set_read_only(true);141inspector->connect("object_selected", callable_mp(this, &EditorExpressionEvaluator::_remote_object_selected));142inspector->set_use_filter(true);143add_child(inspector);144145expression_input->set_editable(false);146evaluate_btn->set_disabled(true);147}148149150