Bläddra i källkod

Expose methods to play scene from plugin code

(cherry picked from commit 49f6dc5004931f17a7be008b2159cd5915cd897d)
Yuri Sizov 5 år sedan
förälder
incheckning
2b4773f0cf

+ 44 - 0
doc/classes/EditorInterface.xml

@@ -72,6 +72,13 @@
 				Returns an [Array] with the file paths of the currently opened scenes.
 			</description>
 		</method>
+		<method name="get_playing_scene" qualifiers="const">
+			<return type="String">
+			</return>
+			<description>
+				Returns the name of the scene that is being played. If no scene is currently being played, returns an empty string.
+			</description>
+		</method>
 		<method name="get_resource_filesystem">
 			<return type="EditorFileSystem">
 			</return>
@@ -117,6 +124,13 @@
 				Shows the given property on the given [code]object[/code] in the Editor's Inspector dock.
 			</description>
 		</method>
+		<method name="is_playing_scene" qualifiers="const">
+			<return type="bool">
+			</return>
+			<description>
+				Returns [code]true[/code], if a scene is currently being played; [code]false[/code] otherwise. Paused scenes are considered as being played.
+			</description>
+		</method>
 		<method name="is_plugin_enabled" qualifiers="const">
 			<return type="bool">
 			</return>
@@ -146,6 +160,29 @@
 				Opens the scene at the given path.
 			</description>
 		</method>
+		<method name="play_current_scene">
+			<return type="void">
+			</return>
+			<description>
+				Plays the currently active scene.
+			</description>
+		</method>
+		<method name="play_custom_scene">
+			<return type="void">
+			</return>
+			<argument index="0" name="scene_filepath" type="String">
+			</argument>
+			<description>
+				Plays the scene specified by its filepath.
+			</description>
+		</method>
+		<method name="play_main_scene">
+			<return type="void">
+			</return>
+			<description>
+				Plays the main scene.
+			</description>
+		</method>
 		<method name="reload_scene_from_path">
 			<return type="void">
 			</return>
@@ -201,6 +238,13 @@
 				Sets the enabled status of a plugin. The plugin name is the same as its directory name.
 			</description>
 		</method>
+		<method name="stop_playing_scene">
+			<return type="void">
+			</return>
+			<description>
+				Stops the scene that is currently playing.
+			</description>
+		</method>
 	</methods>
 	<members>
 		<member name="distraction_free_mode" type="bool" setter="set_distraction_free_mode" getter="is_distraction_free_mode_enabled">

+ 28 - 9
editor/editor_node.cpp

@@ -1962,7 +1962,6 @@ void EditorNode::_run(bool p_current, const String &p_custom) {
 	play_custom_scene_button->set_pressed(false);
 	play_custom_scene_button->set_icon(gui_base->get_icon("PlayCustom", "EditorIcons"));
 
-	String main_scene;
 	String run_filename;
 	String args;
 	bool skip_breakpoints;
@@ -2387,8 +2386,7 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
 
 		} break;
 		case RUN_PLAY: {
-			_menu_option_confirm(RUN_STOP, true);
-			_run(false);
+			run_play();
 
 		} break;
 		case RUN_PLAY_CUSTOM_SCENE: {
@@ -2399,8 +2397,7 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
 				play_custom_scene_button->set_pressed(false);
 			} else {
 				String last_custom_scene = run_custom_filename;
-				_menu_option_confirm(RUN_STOP, true);
-				_run(false, last_custom_scene);
+				run_play_custom(last_custom_scene);
 			}
 
 		} break;
@@ -2439,10 +2436,7 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
 		} break;
 
 		case RUN_PLAY_SCENE: {
-
-			_save_default_environment();
-			_menu_option_confirm(RUN_STOP, true);
-			_run(true);
+			run_play_current();
 
 		} break;
 		case RUN_PLAY_NATIVE: {
@@ -4541,10 +4535,35 @@ void EditorNode::run_play() {
 	_run(false);
 }
 
+void EditorNode::run_play_current() {
+	_save_default_environment();
+	_menu_option_confirm(RUN_STOP, true);
+	_run(true);
+}
+
+void EditorNode::run_play_custom(const String &p_custom) {
+	_menu_option_confirm(RUN_STOP, true);
+	_run(false, p_custom);
+}
+
 void EditorNode::run_stop() {
 	_menu_option_confirm(RUN_STOP, false);
 }
 
+bool EditorNode::is_run_playing() const {
+	EditorRun::Status status = editor_run.get_status();
+	return (status == EditorRun::STATUS_PLAY || status == EditorRun::STATUS_PAUSED);
+}
+
+String EditorNode::get_run_playing_scene() const {
+	String run_filename = editor_run.get_running_scene();
+	if (run_filename == "" && is_run_playing()) {
+		run_filename = GLOBAL_DEF("application/run/main_scene", ""); // Must be the main scene then.
+	}
+
+	return run_filename;
+}
+
 int EditorNode::get_current_tab() {
 	return scene_tabs->get_current_tab();
 }

+ 4 - 0
editor/editor_node.h

@@ -862,7 +862,11 @@ public:
 	bool ensure_main_scene(bool p_from_native);
 
 	void run_play();
+	void run_play_current();
+	void run_play_custom(const String &p_custom);
 	void run_stop();
+	bool is_run_playing() const;
+	String get_run_playing_scene() const;
 };
 
 struct EditorProgress {

+ 30 - 0
editor/editor_plugin.cpp

@@ -184,6 +184,30 @@ void EditorInterface::reload_scene_from_path(const String &scene_path) {
 	EditorNode::get_singleton()->reload_scene(scene_path);
 }
 
+void EditorInterface::play_main_scene() {
+	EditorNode::get_singleton()->run_play();
+}
+
+void EditorInterface::play_current_scene() {
+	EditorNode::get_singleton()->run_play_current();
+}
+
+void EditorInterface::play_custom_scene(const String &scene_path) {
+	EditorNode::get_singleton()->run_play_custom(scene_path);
+}
+
+void EditorInterface::stop_playing_scene() {
+	EditorNode::get_singleton()->run_stop();
+}
+
+bool EditorInterface::is_playing_scene() const {
+	return EditorNode::get_singleton()->is_run_playing();
+}
+
+String EditorInterface::get_playing_scene() const {
+	return EditorNode::get_singleton()->get_run_playing_scene();
+}
+
 Node *EditorInterface::get_edited_scene_root() {
 	return EditorNode::get_singleton()->get_edited_scene();
 }
@@ -295,6 +319,12 @@ void EditorInterface::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("edit_resource", "resource"), &EditorInterface::edit_resource);
 	ClassDB::bind_method(D_METHOD("open_scene_from_path", "scene_filepath"), &EditorInterface::open_scene_from_path);
 	ClassDB::bind_method(D_METHOD("reload_scene_from_path", "scene_filepath"), &EditorInterface::reload_scene_from_path);
+	ClassDB::bind_method(D_METHOD("play_main_scene"), &EditorInterface::play_main_scene);
+	ClassDB::bind_method(D_METHOD("play_current_scene"), &EditorInterface::play_current_scene);
+	ClassDB::bind_method(D_METHOD("play_custom_scene", "scene_filepath"), &EditorInterface::play_custom_scene);
+	ClassDB::bind_method(D_METHOD("stop_playing_scene"), &EditorInterface::stop_playing_scene);
+	ClassDB::bind_method(D_METHOD("is_playing_scene"), &EditorInterface::is_playing_scene);
+	ClassDB::bind_method(D_METHOD("get_playing_scene"), &EditorInterface::get_playing_scene);
 	ClassDB::bind_method(D_METHOD("get_open_scenes"), &EditorInterface::get_open_scenes);
 	ClassDB::bind_method(D_METHOD("get_edited_scene_root"), &EditorInterface::get_edited_scene_root);
 	ClassDB::bind_method(D_METHOD("get_resource_previewer"), &EditorInterface::get_resource_previewer);

+ 7 - 0
editor/editor_plugin.h

@@ -73,6 +73,13 @@ public:
 	void open_scene_from_path(const String &scene_path);
 	void reload_scene_from_path(const String &scene_path);
 
+	void play_main_scene();
+	void play_current_scene();
+	void play_custom_scene(const String &scene_path);
+	void stop_playing_scene();
+	bool is_playing_scene() const;
+	String get_playing_scene() const;
+
 	Node *get_edited_scene_root();
 	Array get_open_scenes() const;
 	ScriptEditor *get_script_editor();

+ 9 - 0
editor/editor_run.cpp

@@ -38,6 +38,10 @@ EditorRun::Status EditorRun::get_status() const {
 	return status;
 }
 
+String EditorRun::get_running_scene() const {
+	return running_scene;
+}
+
 Error EditorRun::run(const String &p_scene, const String &p_custom_args, const List<String> &p_breakpoints, const bool &p_skip_breakpoints) {
 
 	List<String> args;
@@ -192,6 +196,9 @@ Error EditorRun::run(const String &p_scene, const String &p_custom_args, const L
 	ERR_FAIL_COND_V(err, err);
 
 	status = STATUS_PLAY;
+	if (p_scene != "") {
+		running_scene = p_scene;
+	}
 
 	return OK;
 }
@@ -204,6 +211,7 @@ void EditorRun::stop() {
 	}
 
 	status = STATUS_STOP;
+	running_scene = "";
 }
 
 void EditorRun::set_debug_collisions(bool p_debug) {
@@ -229,6 +237,7 @@ bool EditorRun::get_debug_navigation() const {
 EditorRun::EditorRun() {
 
 	status = STATUS_STOP;
+	running_scene = "";
 	debug_collisions = false;
 	debug_navigation = false;
 }

+ 2 - 0
editor/editor_run.h

@@ -48,9 +48,11 @@ private:
 	bool debug_collisions;
 	bool debug_navigation;
 	Status status;
+	String running_scene;
 
 public:
 	Status get_status() const;
+	String get_running_scene() const;
 	Error run(const String &p_scene, const String &p_custom_args, const List<String> &p_breakpoints, const bool &p_skip_breakpoints = false);
 	void run_native_notify() { status = STATUS_PLAY; }
 	void stop();