From fc073f9e13bd188396f5d1237f223bbaea6da841 Mon Sep 17 00:00:00 2001 From: Russell Matney Date: Sun, 24 Mar 2024 13:33:47 -0400 Subject: [PATCH] deps: add custom runner --- addons/CustomRunner/Config.gd | 20 ++++++++ addons/CustomRunner/CustomRunner.gd | 74 +++++++++++++++++++++++++++++ addons/CustomRunner/Plugin.gd | 7 +++ addons/CustomRunner/plugin.cfg | 7 +++ plug.gd | 1 + project.godot | 4 +- 6 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 addons/CustomRunner/Config.gd create mode 100644 addons/CustomRunner/CustomRunner.gd create mode 100644 addons/CustomRunner/Plugin.gd create mode 100644 addons/CustomRunner/plugin.cfg diff --git a/addons/CustomRunner/Config.gd b/addons/CustomRunner/Config.gd new file mode 100644 index 000000000..dc1f32613 --- /dev/null +++ b/addons/CustomRunner/Config.gd @@ -0,0 +1,20 @@ +extends CustomRunner + +## The shortcut that will trigger the plugin. +func _init() -> void: + SHORTCUT = KEY_F7 + +## If true, pressing the shortcut will invoke CustomRunner for that scene. +func _can_play_scene(scene: Node) -> bool: + return true + +## Add variables that will be passed to the game. +## Variable "scene", containing currently opened scene's path, is added automatically. +func _gather_variables(scene: Node): + if scene is CanvasItem: + add_variable("mouse_pos", scene.get_local_mouse_position()) + +## Return the path of the "game" scene of your project (i.e. main gameplay scene). +## If you return empty string, the current scene will play instead. +func _get_game_scene(for_scene: Node) -> String: + return "" diff --git a/addons/CustomRunner/CustomRunner.gd b/addons/CustomRunner/CustomRunner.gd new file mode 100644 index 000000000..d5e133c3d --- /dev/null +++ b/addons/CustomRunner/CustomRunner.gd @@ -0,0 +1,74 @@ +@tool +extends Node +class_name CustomRunner + +## Custom Runner main script. You can customize it by modifying the Config.gd file. + +## The shortcut that will trigger the plugin. +var SHORTCUT = KEY_F7 + +## If true, pressing the shortcut will invoke CustomRunner for that scene. +func _can_play_scene(scene: Node) -> bool: + return true + +## Add variables that will be passed to the game. +## Variable "scene", containing currently opened scene's path, is added automatically. +func _gather_variables(scene: Node): + pass + +## Return the path of the "game" scene of your project (i.e. main gameplay scene). +## If you return empty string, the current scene will play instead. +func _get_game_scene(for_scene: Node) -> String: + return "" + +## Returns true if the game was ran via CustomRunner. +static func is_custom_running() -> bool: + return not OS.get_environment("__custom_runner_data__").is_empty() + +## Retrieves a passed variable's value. +static func get_variable(variable: String): + assert(is_custom_running(), "Can't retrieve data if not running via plugin.") + var data: Dictionary = str_to_var(OS.get_environment("__custom_runner_data__")) + return data[variable] + +var plugin: Node +var data: Dictionary +var prev_game_scene: String + +func _unhandled_key_input(event: InputEvent): + if plugin.get_editor_interface().is_playing_scene(): + return + + if event is InputEventKey and event.pressed and event.keycode == SHORTCUT: + if event.shift_pressed: + if data.is_empty() or prev_game_scene.is_empty(): + push_warning("CustomRunner: Can't do Quick Run before running mormally at least once.") + return + else: + var root: Node = plugin.get_editor_interface().get_edited_scene_root() + if not _can_play_scene(root): + push_warning("CustomRunner: Invalid scene to play.") + return + + data.clear() + add_variable("scene", root.scene_file_path) + _gather_variables(root) + + var game_scene := _get_game_scene(root) + if game_scene.is_empty(): + game_scene = root.scene_file_path + + prev_game_scene = game_scene + + OS.set_environment("__custom_runner_data__", var_to_str(data)) + plugin.get_editor_interface().play_custom_scene(prev_game_scene) + OS.set_environment("__custom_runner_data__", "") + get_viewport().set_input_as_handled() + +## Adds a variable to be passed to the running game. Use in [method _gather_variables]. +func add_variable(variable: String, value): + if value is Object: + push_error("The value can be non-Object only.") + return + + data[variable] = value diff --git a/addons/CustomRunner/Plugin.gd b/addons/CustomRunner/Plugin.gd new file mode 100644 index 000000000..c266c8831 --- /dev/null +++ b/addons/CustomRunner/Plugin.gd @@ -0,0 +1,7 @@ +@tool +extends EditorPlugin + +func _enter_tree(): + var runner := preload("Config.gd").new() + runner.plugin = self + add_child(runner) diff --git a/addons/CustomRunner/plugin.cfg b/addons/CustomRunner/plugin.cfg new file mode 100644 index 000000000..b85a66248 --- /dev/null +++ b/addons/CustomRunner/plugin.cfg @@ -0,0 +1,7 @@ +[plugin] + +name="Custom Project Runner" +description="Configure how you run your project and pass data from editor." +author="KoBeWi" +version="1.3.1" +script="Plugin.gd" diff --git a/plug.gd b/plug.gd index 228f12b32..eefd41ddb 100644 --- a/plug.gd +++ b/plug.gd @@ -11,3 +11,4 @@ func _plugging(): plug("viniciusgerevini/godot-aseprite-wizard", {include=["addons/AsepriteWizard"]}) plug("russmatney/log.gd", {include=["addons/log"]}) plug("KoBeWi/Metroidvania-System", {include=["addons/MetroidvaniaSystem"]}) + plug("KoBeWi/Godot-Custom-Runner", {include=["addons/CustomRunner"], exclude=["ExampleProject"]}) diff --git a/project.godot b/project.godot index b20f5ab7f..2b4bfeb95 100644 --- a/project.godot +++ b/project.godot @@ -39,6 +39,8 @@ Metro="*res://addons/metro/Metro.gd" Records="*res://src/dino/Records.gd" Dino="*res://src/dino/Dino.gd" MetSys="*res://addons/MetroidvaniaSystem/Nodes/Singleton.tscn" +InputHelper="*res://addons/input_helper/input_helper.gd" +SoundManager="*res://addons/sound_manager/sound_manager.gd" [display] @@ -55,7 +57,7 @@ export/convert_text_resources_to_binary=false [editor_plugins] -enabled=PackedStringArray("res://addons/AsepriteWizard/plugin.cfg", "res://addons/MetroidvaniaSystem/EditorExtension/plugin.cfg", "res://addons/MetroidvaniaSystem/plugin.cfg", "res://addons/beehive/plugin.cfg", "res://addons/brick/plugin.cfg", "res://addons/camera/plugin.cfg", "res://addons/core/plugin.cfg", "res://addons/custom-scene-launcher/plugin.cfg", "res://addons/dj/plugin.cfg", "res://addons/gdfxr/plugin.cfg", "res://addons/hood/plugin.cfg", "res://addons/hotel/plugin.cfg", "res://addons/metro/plugin.cfg", "res://addons/navi/plugin.cfg", "res://addons/pandora/plugin.cfg", "res://addons/quest/plugin.cfg", "res://addons/reptile/plugin.cfg", "res://addons/thanks/plugin.cfg", "res://addons/trolley/plugin.cfg") +enabled=PackedStringArray("res://addons/AsepriteWizard/plugin.cfg", "res://addons/CustomRunner/plugin.cfg", "res://addons/MetroidvaniaSystem/EditorExtension/plugin.cfg", "res://addons/MetroidvaniaSystem/plugin.cfg", "res://addons/beehive/plugin.cfg", "res://addons/brick/plugin.cfg", "res://addons/camera/plugin.cfg", "res://addons/core/plugin.cfg", "res://addons/custom-scene-launcher/plugin.cfg", "res://addons/dj/plugin.cfg", "res://addons/gd-plug-ui/plugin.cfg", "res://addons/gdfxr/plugin.cfg", "res://addons/hood/plugin.cfg", "res://addons/hotel/plugin.cfg", "res://addons/input_helper/plugin.cfg", "res://addons/log/plugin.cfg", "res://addons/metro/plugin.cfg", "res://addons/navi/plugin.cfg", "res://addons/pandora/plugin.cfg", "res://addons/quest/plugin.cfg", "res://addons/reptile/plugin.cfg", "res://addons/sound_manager/plugin.cfg", "res://addons/thanks/plugin.cfg", "res://addons/trolley/plugin.cfg") [gdunit4]