Path: blob/master/editor/run/run_instances_dialog.cpp
20785 views
/**************************************************************************/1/* run_instances_dialog.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 "run_instances_dialog.h"3132#include "core/config/project_settings.h"33#include "editor/settings/editor_settings.h"34#include "editor/themes/editor_scale.h"35#include "scene/gui/check_box.h"36#include "scene/gui/grid_container.h"37#include "scene/gui/label.h"38#include "scene/gui/line_edit.h"39#include "scene/gui/margin_container.h"40#include "scene/gui/popup_menu.h"41#include "scene/gui/separator.h"42#include "scene/gui/spin_box.h"43#include "scene/gui/tree.h"44#include "scene/main/timer.h"4546void RunInstancesDialog::_fetch_main_args() {47if (!main_args_edit->has_focus()) { // Only set the text if the user is not currently editing it.48main_args_edit->set_text(GLOBAL_GET("editor/run/main_run_args"));49}50}5152void RunInstancesDialog::_start_main_timer() {53main_apply_timer->start();54}5556void RunInstancesDialog::_start_instance_timer() {57instance_apply_timer->start();58}5960void RunInstancesDialog::_refresh_argument_count() {61instance_tree->clear();62instance_tree->create_item(); // Root.6364while (instance_count->get_value() > stored_data.size()) {65stored_data.append(Dictionary());66}6768instances_data.resize(instance_count->get_value());69InstanceData *instances_write = instances_data.ptrw();7071for (int i = 0; i < instances_data.size(); i++) {72InstanceData instance;73const Dictionary &instance_data = stored_data[i];7475_create_instance(instance, instance_data, i + 1);76instances_write[i] = instance;77}78}7980void RunInstancesDialog::_create_instance(InstanceData &p_instance, const Dictionary &p_data, int p_idx) {81TreeItem *instance_item = instance_tree->create_item();82p_instance.item = instance_item;8384instance_item->set_cell_mode(COLUMN_OVERRIDE_ARGS, TreeItem::CELL_MODE_CHECK);85instance_item->set_editable(COLUMN_OVERRIDE_ARGS, true);86instance_item->set_text(COLUMN_OVERRIDE_ARGS, TTR("Enabled"));87instance_item->set_checked(COLUMN_OVERRIDE_ARGS, p_data.get("override_args", false));8889instance_item->set_editable(COLUMN_LAUNCH_ARGUMENTS, true);90instance_item->set_text(COLUMN_LAUNCH_ARGUMENTS, p_data.get("arguments", String()));9192instance_item->set_cell_mode(COLUMN_OVERRIDE_FEATURES, TreeItem::CELL_MODE_CHECK);93instance_item->set_editable(COLUMN_OVERRIDE_FEATURES, true);94instance_item->set_text(COLUMN_OVERRIDE_FEATURES, TTR("Enabled"));95instance_item->set_checked(COLUMN_OVERRIDE_FEATURES, p_data.get("override_features", false));9697instance_item->set_editable(COLUMN_FEATURE_TAGS, true);98instance_item->set_text(COLUMN_FEATURE_TAGS, p_data.get("features", String()));99}100101void RunInstancesDialog::_save_main_args() {102ProjectSettings::get_singleton()->set_setting("editor/run/main_run_args", main_args_edit->get_text());103ProjectSettings::get_singleton()->save();104EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_main_feature_tags", main_features_edit->get_text());105EditorSettings::get_singleton()->set_project_metadata("debug_options", "multiple_instances_enabled", enable_multiple_instances_checkbox->is_pressed());106}107108void RunInstancesDialog::_save_arguments() {109for (int i = 0; i < instances_data.size(); i++) {110const InstanceData &instance = instances_data[i];111Dictionary dict;112dict["override_args"] = instance.overrides_run_args();113dict["arguments"] = instance.get_launch_arguments();114dict["override_features"] = instance.overrides_features();115dict["features"] = instance.get_feature_tags();116stored_data[i] = dict;117}118EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_instances_config", stored_data);119EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_instance_count", instance_count->get_value());120}121122Vector<String> RunInstancesDialog::_split_cmdline_args(const String &p_arg_string) const {123Vector<String> split_args;124int arg_start = 0;125bool is_quoted = false;126char32_t quote_char = '-';127char32_t arg_char;128int arg_length;129for (int i = 0; i < p_arg_string.length(); i++) {130arg_char = p_arg_string[i];131if (arg_char == '\"' || arg_char == '\'') {132if (i == 0 || p_arg_string[i - 1] != '\\') {133if (is_quoted) {134if (arg_char == quote_char) {135is_quoted = false;136quote_char = '-';137}138} else {139is_quoted = true;140quote_char = arg_char;141}142}143} else if (!is_quoted && arg_char == ' ') {144arg_length = i - arg_start;145if (arg_length > 0) {146split_args.push_back(p_arg_string.substr(arg_start, arg_length));147}148arg_start = i + 1;149}150}151arg_length = p_arg_string.length() - arg_start;152if (arg_length > 0) {153split_args.push_back(p_arg_string.substr(arg_start, arg_length));154}155return split_args;156}157158void RunInstancesDialog::_instance_menu_id_pressed(int p_option) {159switch (p_option) {160case CLEAR_ITEM: {161int item_to_clear = popup_menu->get_item_metadata(0);162if (item_to_clear >= 0 && item_to_clear < stored_data.size()) {163stored_data[item_to_clear] = Dictionary();164}165} break;166case CLEAR_ALL: {167stored_data.clear();168stored_data.resize(instance_count->get_value());169} break;170}171172_start_instance_timer();173_refresh_argument_count();174}175176void RunInstancesDialog::_instance_tree_rmb(const Vector2 &p_pos, MouseButton p_button) {177if (p_button != MouseButton::RIGHT) {178return;179}180181popup_menu->clear();182popup_menu->add_item(TTR("Clear"), CLEAR_ITEM);183184TreeItem *item = instance_tree->get_item_at_position(p_pos);185if (item) {186popup_menu->set_item_metadata(0, item->get_index());187} else {188popup_menu->set_item_disabled(0, true);189}190191popup_menu->add_item(TTR("Clear All"), CLEAR_ALL);192popup_menu->set_position(instance_tree->get_screen_position() + p_pos);193popup_menu->reset_size();194popup_menu->popup();195}196197void RunInstancesDialog::popup_dialog() {198popup_centered_clamped(Size2(1200, 600) * EDSCALE, 0.8);199}200201int RunInstancesDialog::get_instance_count() const {202if (enable_multiple_instances_checkbox->is_pressed()) {203return instance_count->get_value();204} else {205return 1;206}207}208209void RunInstancesDialog::get_argument_list_for_instance(int p_idx, List<String> &r_list) const {210bool override_args = instances_data[p_idx].overrides_run_args();211bool use_multiple_instances = enable_multiple_instances_checkbox->is_pressed();212String raw_custom_args;213214if (use_multiple_instances) {215if (override_args) {216raw_custom_args = instances_data[p_idx].get_launch_arguments();217} else {218raw_custom_args = main_args_edit->get_text() + " " + instances_data[p_idx].get_launch_arguments();219}220} else {221raw_custom_args = main_args_edit->get_text();222}223224String exec = OS::get_singleton()->get_executable_path();225226if (!raw_custom_args.is_empty()) {227// Allow the user to specify a command to run, similar to Steam's launch options.228// In this case, Godot will no longer be run directly; it's up to the underlying command229// to run it. For instance, this can be used on Linux to force a running project230// to use Optimus using `prime-run` or similar.231// Example: `prime-run %command% --time-scale 0.5`232const int placeholder_pos = raw_custom_args.find("%command%");233234Vector<String> custom_args;235236if (placeholder_pos != -1) {237// Prepend executable-specific custom arguments.238// If nothing is placed before `%command%`, behave as if no placeholder was specified.239Vector<String> exec_args = _split_cmdline_args(raw_custom_args.substr(0, placeholder_pos));240if (exec_args.size() > 0) {241exec = exec_args[0];242exec_args.remove_at(0);243244// Append the Godot executable name before we append executable arguments245// (since the order is reversed when using `push_front()`).246r_list.push_front(OS::get_singleton()->get_executable_path());247}248249for (int i = exec_args.size() - 1; i >= 0; i--) {250// Iterate backwards as we're pushing items in the reverse order.251r_list.push_front(exec_args[i].replace(" ", "%20"));252}253254// Append Godot-specific custom arguments.255custom_args = _split_cmdline_args(raw_custom_args.substr(placeholder_pos + String("%command%").size()));256for (int i = 0; i < custom_args.size(); i++) {257r_list.push_back(custom_args[i].replace(" ", "%20"));258}259} else {260// Append Godot-specific custom arguments.261custom_args = _split_cmdline_args(raw_custom_args);262for (int i = 0; i < custom_args.size(); i++) {263r_list.push_back(custom_args[i].replace(" ", "%20"));264}265}266}267}268269void RunInstancesDialog::apply_custom_features(int p_instance_idx) {270const InstanceData &instance = instances_data[p_instance_idx];271272String raw_text;273if (enable_multiple_instances_checkbox->is_pressed()) {274if (instance.overrides_features()) {275raw_text = instance.get_feature_tags();276} else {277raw_text = main_features_edit->get_text() + "," + instance.get_feature_tags();278}279} else {280raw_text = main_features_edit->get_text();281}282283const Vector<String> raw_list = raw_text.split(",");284Vector<String> stripped_features;285286for (int i = 0; i < raw_list.size(); i++) {287String f = raw_list[i].strip_edges();288if (!f.is_empty()) {289stripped_features.push_back(f);290}291}292OS::get_singleton()->set_environment("GODOT_EDITOR_CUSTOM_FEATURES", String(",").join(stripped_features));293}294295RunInstancesDialog::RunInstancesDialog() {296singleton = this;297set_title(TTR("Run Instances"));298299main_apply_timer = memnew(Timer);300main_apply_timer->set_wait_time(0.5);301main_apply_timer->set_one_shot(true);302add_child(main_apply_timer);303main_apply_timer->connect("timeout", callable_mp(this, &RunInstancesDialog::_save_main_args));304305instance_apply_timer = memnew(Timer);306instance_apply_timer->set_wait_time(0.5);307instance_apply_timer->set_one_shot(true);308add_child(instance_apply_timer);309instance_apply_timer->connect("timeout", callable_mp(this, &RunInstancesDialog::_save_arguments));310311VBoxContainer *main_vb = memnew(VBoxContainer);312add_child(main_vb);313314GridContainer *main_gc = memnew(GridContainer);315main_gc->set_columns(2);316main_vb->add_child(main_gc);317318{319Label *l = memnew(Label);320l->set_text(TTR("Main Run Args:"));321main_gc->add_child(l);322}323324{325Label *l = memnew(Label);326l->set_text(TTR("Main Feature Tags:"));327main_gc->add_child(l);328}329330stored_data = TypedArray<Dictionary>(EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_instances_config", TypedArray<Dictionary>()));331332main_args_edit = memnew(LineEdit);333main_args_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);334main_args_edit->set_placeholder(TTR("Space-separated arguments, example: host player1 blue"));335main_args_edit->set_accessibility_name(TTRC("Main Run Args:"));336main_gc->add_child(main_args_edit);337_fetch_main_args();338ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &RunInstancesDialog::_fetch_main_args));339main_args_edit->connect(SceneStringName(text_changed), callable_mp(this, &RunInstancesDialog::_start_main_timer).unbind(1));340341main_features_edit = memnew(LineEdit);342main_features_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);343main_features_edit->set_placeholder(TTR("Comma-separated tags, example: demo, steam, event"));344main_features_edit->set_text(EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_main_feature_tags", ""));345main_features_edit->set_accessibility_name(TTRC("Main Feature Tags:"));346main_gc->add_child(main_features_edit);347main_features_edit->connect(SceneStringName(text_changed), callable_mp(this, &RunInstancesDialog::_start_main_timer).unbind(1));348349main_vb->add_child(memnew(HSeparator));350351HBoxContainer *instance_hb = memnew(HBoxContainer);352instance_hb->set_alignment(BoxContainer::ALIGNMENT_CENTER);353main_vb->add_child(instance_hb);354355enable_multiple_instances_checkbox = memnew(CheckBox);356enable_multiple_instances_checkbox->set_text(TTRC("Enable Multiple Instances"));357enable_multiple_instances_checkbox->set_pressed(EditorSettings::get_singleton()->get_project_metadata("debug_options", "multiple_instances_enabled", false));358instance_hb->add_child(enable_multiple_instances_checkbox);359enable_multiple_instances_checkbox->connect(SceneStringName(pressed), callable_mp(this, &RunInstancesDialog::_start_main_timer));360361instance_count = memnew(SpinBox);362instance_count->set_min(1);363instance_count->set_max(20);364instance_count->set_value(EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_instance_count", stored_data.size()));365instance_count->set_accessibility_name(TTRC("Number of Instances"));366367instance_hb->add_child(instance_count);368instance_count->connect(SceneStringName(value_changed), callable_mp(this, &RunInstancesDialog::_start_instance_timer).unbind(1));369instance_count->connect(SceneStringName(value_changed), callable_mp(this, &RunInstancesDialog::_refresh_argument_count).unbind(1));370enable_multiple_instances_checkbox->connect(SceneStringName(toggled), callable_mp(instance_count, &SpinBox::set_editable));371instance_count->set_editable(enable_multiple_instances_checkbox->is_pressed());372373MarginContainer *mc = memnew(MarginContainer);374mc->set_theme_type_variation("NoBorderHorizontalWindow");375mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);376main_vb->add_child(mc);377378instance_tree = memnew(Tree);379instance_tree->set_h_scroll_enabled(false);380instance_tree->set_theme_type_variation("TreeTable");381instance_tree->set_hide_folding(true);382instance_tree->set_columns(4);383instance_tree->set_column_titles_visible(true);384instance_tree->set_column_title(COLUMN_OVERRIDE_ARGS, TTR("Override Main Run Args"));385instance_tree->set_column_expand(COLUMN_OVERRIDE_ARGS, false);386instance_tree->set_column_title(COLUMN_LAUNCH_ARGUMENTS, TTR("Launch Arguments"));387instance_tree->set_column_title(COLUMN_OVERRIDE_FEATURES, TTR("Override Main Tags"));388instance_tree->set_column_expand(COLUMN_OVERRIDE_FEATURES, false);389instance_tree->set_column_title(COLUMN_FEATURE_TAGS, TTR("Feature Tags"));390instance_tree->set_hide_root(true);391instance_tree->set_allow_rmb_select(true);392instance_tree->set_scroll_hint_mode(Tree::SCROLL_HINT_MODE_BOTTOM);393394popup_menu = memnew(PopupMenu);395popup_menu->connect(SceneStringName(id_pressed), callable_mp(this, &RunInstancesDialog::_instance_menu_id_pressed));396instance_tree->add_child(popup_menu);397398instance_tree->connect("item_mouse_selected", callable_mp(this, &RunInstancesDialog::_instance_tree_rmb));399instance_tree->connect("empty_clicked", callable_mp(this, &RunInstancesDialog::_instance_tree_rmb));400mc->add_child(instance_tree);401402_refresh_argument_count();403instance_tree->connect("item_edited", callable_mp(this, &RunInstancesDialog::_start_instance_timer));404}405406bool RunInstancesDialog::InstanceData::overrides_run_args() const {407return item->is_checked(COLUMN_OVERRIDE_ARGS);408}409410String RunInstancesDialog::InstanceData::get_launch_arguments() const {411return item->get_text(COLUMN_LAUNCH_ARGUMENTS);412}413414bool RunInstancesDialog::InstanceData::overrides_features() const {415return item->is_checked(COLUMN_OVERRIDE_FEATURES);416}417418String RunInstancesDialog::InstanceData::get_feature_tags() const {419return item->get_text(COLUMN_FEATURE_TAGS);420}421422423