Use static typing in all demos (#1063)

This leads to code that is easier to understand and runs
faster thanks to GDScript's typed instructions.

The untyped declaration warning is now enabled on all projects
where type hints were added. All projects currently run without
any untyped declration warnings.

Dodge the Creeps and Squash the Creeps demos intentionally don't
use type hints to match the documentation, where type hints haven't
been adopted yet (given its beginner focus).
This commit is contained in:
Hugo Locurcio
2024-06-01 12:12:18 +02:00
committed by GitHub
parent 8e9c180278
commit bac1e69164
498 changed files with 5218 additions and 4776 deletions

2
plugins/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
# "Silly Material" files written by the editor plugin
*.silly_mat

View File

@@ -1,12 +1,12 @@
@tool
extends Node2D
var heart = preload("res://addons/custom_node/heart.png")
func _draw():
draw_texture(heart, -heart.get_size() / 2)
const HEART_TEXTURE := preload("res://addons/custom_node/heart.png")
func _get_item_rect():
# override
return Rect2(-heart.get_size() / 2, heart.get_size())
func _draw() -> void:
draw_texture(HEART_TEXTURE, -HEART_TEXTURE.get_size() / 2)
func _get_item_rect() -> Rect2:
return Rect2(-HEART_TEXTURE.get_size() / 2, HEART_TEXTURE.get_size())

View File

@@ -1,11 +1,12 @@
@tool
extends EditorPlugin
func _enter_tree():
# When this plugin node enters tree, add the custom type
func _enter_tree() -> void:
# When this plugin node enters tree, add the custom type.
add_custom_type("Heart", "Node2D", preload("res://addons/custom_node/heart.gd"), preload("res://addons/custom_node/heart_icon.png"))
func _exit_tree():
# When the plugin node exits the tree, remove the custom type
func _exit_tree() -> void:
# When the plugin node exits the tree, remove the custom type.
remove_custom_type("Heart")

View File

@@ -12,10 +12,6 @@ size_flags_vertical = 3
[node name="PrintHello" type="Button" parent="."]
layout_mode = 2
offset_left = 460.0
offset_top = 297.0
offset_right = 691.0
offset_bottom = 351.0
text = "Print Hello
(check Output bottom panel)"
script = ExtResource("1")

View File

@@ -3,9 +3,10 @@ extends EditorPlugin
const MainPanel = preload("res://addons/main_screen/main_panel.tscn")
var main_panel_instance
var main_panel_instance: CenterContainer
func _enter_tree():
func _enter_tree() -> void:
main_panel_instance = MainPanel.instantiate()
# Add the main panel to the editor's main viewport.
get_editor_interface().get_editor_main_screen().add_child(main_panel_instance)
@@ -13,29 +14,28 @@ func _enter_tree():
_make_visible(false)
func _exit_tree():
func _exit_tree() -> void:
if main_panel_instance:
main_panel_instance.queue_free()
func _has_main_screen():
func _has_main_screen() -> bool:
return true
func _make_visible(visible):
func _make_visible(visible: bool) -> void:
if main_panel_instance:
main_panel_instance.visible = visible
# If your plugin doesn't handle any node types, you can remove this method.
func _handles(object):
func _handles(object: Object) -> bool:
return is_instance_of(object, preload("res://addons/main_screen/handled_by_main_screen.gd"))
func _get_plugin_name():
func _get_plugin_name() -> String:
return "Main Screen Plugin"
func _get_plugin_icon():
# Must return some kind of Texture2D for the icon.
func _get_plugin_icon() -> Texture2D:
return get_editor_interface().get_base_control().get_theme_icon("Node", "EditorIcons")

View File

@@ -1,5 +1,6 @@
@tool
extends Button
func _on_PrintHello_pressed():
func _on_PrintHello_pressed() -> void:
print("Hello from the main screen plugin!")

View File

@@ -3,73 +3,74 @@ extends Panel
# In this file, the word "silly" is used to make it obvious that the name is arbitrary.
var silly_material_resource = preload("res://addons/material_creator/material_resource.gd")
var editor_interface
var editor_interface: EditorInterface
func _ready():
func _ready() -> void:
# Connect all of the signals we'll need to save and load silly materials.
get_node(^"VBoxContainer/ApplyButton").pressed.connect(apply_pressed)
get_node(^"VBoxContainer/SaveButton").pressed.connect(save_pressed)
get_node(^"VBoxContainer/LoadButton").pressed.connect(load_pressed)
get_node(^"SaveMaterialDialog").file_selected.connect(save_file_selected)
get_node(^"LoadMaterialDialog").file_selected.connect(load_file_selected)
$VBoxContainer/ApplyButton.pressed.connect(apply_pressed)
$VBoxContainer/SaveButton.pressed.connect(save_pressed)
$VBoxContainer/LoadButton.pressed.connect(load_pressed)
$SaveMaterialDialog.file_selected.connect(save_file_selected)
$LoadMaterialDialog.file_selected.connect(load_file_selected)
RenderingServer.canvas_item_set_clip(get_canvas_item(), true)
func save_pressed():
get_node(^"SaveMaterialDialog").popup_centered_ratio()
func save_pressed() -> void:
$SaveMaterialDialog.popup_centered_ratio()
func load_pressed():
get_node(^"LoadMaterialDialog").popup_centered_ratio()
func load_pressed() -> void:
$LoadMaterialDialog.popup_centered_ratio()
func apply_pressed():
func apply_pressed() -> void:
# Using the passed in editor interface, get the selected nodes in the editor.
var editor_selection = editor_interface.get_selection()
var selected_nodes = editor_selection.get_selected_nodes()
var editor_selection: EditorSelection = editor_interface.get_selection()
var selected_nodes := editor_selection.get_selected_nodes()
if selected_nodes.is_empty():
push_error("Material Creator: Can't apply the material, because there are no nodes selected!")
var material = _silly_resource_from_values().make_material()
var new_material: StandardMaterial3D = _silly_resource_from_values().make_material()
# Go through the selected nodes and see if they have the "set_surface_override_material"
# function (which only MeshInstance3D has by default). If they do, then set the material
# to the silly material.
for node in selected_nodes:
if node.has_method("set_surface_override_material"):
node.set_surface_override_material(0, material)
node.set_surface_override_material(0, new_material)
func save_file_selected(path):
var silly_resource = _silly_resource_from_values()
func save_file_selected(path: String) -> bool:
var silly_resource: Variant = _silly_resource_from_values()
# Make a file, store the silly material as a JSON string.
var file = FileAccess.open(path, FileAccess.WRITE)
var file := FileAccess.open(path, FileAccess.WRITE)
file.store_string(silly_resource.make_json())
return true
func load_file_selected(path):
var SpatialMaterial_Silly = null
func load_file_selected(path: String) -> bool:
var SpatialMaterial_Silly: StandardMaterial3D = null
# Make a new silly resource (which in this case actually is a node)
# and initialize it.
var silly_resource = silly_material_resource.new()
silly_resource.init()
var silly_resource: Variant = silly_material_resource.new()
#silly_resource.init()
# If the file exists, then open it.
if FileAccess.file_exists(path):
var file = FileAccess.open(path, FileAccess.READ)
var file := FileAccess.open(path, FileAccess.READ)
# Get the JSON string and convert it into a silly material.
var json_dict_as_string = file.get_line()
var json_dict_as_string := file.get_line()
if json_dict_as_string != null:
silly_resource.from_json(json_dict_as_string)
else:
return false
get_node(^"VBoxContainer/AlbedoColorPicker").color = silly_resource.albedo_color
get_node(^"VBoxContainer/MetallicSlider").value = silly_resource.metallic_strength
get_node(^"VBoxContainer/RoughnessSlider").value = silly_resource.roughness_strength
$VBoxContainer/AlbedoColorPicker.color = silly_resource.albedo_color
$VBoxContainer/MetallicSlider.value = silly_resource.metallic_strength
$VBoxContainer/RoughnessSlider.value = silly_resource.roughness_strength
# Return `true` to indicate success.
return true
@@ -78,14 +79,14 @@ func load_file_selected(path):
return false
func _silly_resource_from_values():
func _silly_resource_from_values() -> Variant:
# Get the values from the sliders and color picker.
var color = get_node(^"VBoxContainer/AlbedoColorPicker").color
var metallic = get_node(^"VBoxContainer/MetallicSlider").value
var roughness = get_node(^"VBoxContainer/RoughnessSlider").value
var color: Color = $VBoxContainer/AlbedoColorPicker.color
var metallic: float = $VBoxContainer/MetallicSlider.value
var roughness: float = $VBoxContainer/RoughnessSlider.value
# Make a new silly resource (which in this case actually is a node) and initialize it.
var silly_resource = silly_material_resource.new()
silly_resource.init()
var silly_resource: Variant = silly_material_resource.new()
#silly_resource.init()
# Assign the values.
silly_resource.albedo_color = color

View File

@@ -3,6 +3,7 @@
[ext_resource type="Script" path="res://addons/material_creator/material_creator.gd" id="1"]
[node name="Material Creator" type="Panel"]
custom_minimum_size = Vector2(208, 0)
offset_right = 220.0
offset_bottom = 340.0
script = ExtResource("1")

View File

@@ -11,13 +11,14 @@
@tool
extends EditorPlugin
var io_material_dialog
var io_material_dialog: Panel
func _enter_tree():
func _enter_tree() -> void:
io_material_dialog = preload("res://addons/material_creator/material_dock.tscn").instantiate()
io_material_dialog.editor_interface = get_editor_interface()
add_control_to_dock(DOCK_SLOT_LEFT_UL, io_material_dialog)
func _exit_tree():
func _exit_tree() -> void:
remove_control_from_docks(io_material_dialog)

View File

@@ -1,26 +1,19 @@
@tool
extends Node
# NOTE: in theory this would extend from resource, but until saving and loading resources
# works in godot, we'll stick with extending from node
# and using JSON files to save/load data
# NOTE: In theory, this would extend from Resource, but until saving and loading resources
# works in Godot, we'll stick with extending from Node and using JSON files to save/load data.
#
# See material_import.gd for more information
# See `material_import.gd` for more information.
var albedo_color
var metallic_strength
var roughness_strength
func init():
albedo_color = Color()
metallic_strength = 0
roughness_strength = 0
var albedo_color := Color.BLACK
var metallic_strength := 0.0
var roughness_strength := 0.0
# Convert our data into an dictonary so we can convert it
# into the JSON format
func make_json():
var json_dict = {}
# into the JSON format.
func make_json() -> String:
var json_dict := {}
json_dict["albedo_color"] = {}
json_dict["albedo_color"]["r"] = albedo_color.r
@@ -30,15 +23,13 @@ func make_json():
json_dict["metallic_strength"] = metallic_strength
json_dict["roughness_strength"] = roughness_strength
return JSON.new().stringify(json_dict)
return JSON.stringify(json_dict)
# Convert the passed in string to a json dictonary, and then
# Convert the passed in string to a JSON dictonary, and then
# fill in our data.
func from_json(json_dict_as_string):
var json = JSON.new()
json.parse(json_dict_as_string)
var json_dict = json.get_data()
func from_json(json_dict_as_string: String) -> void:
var json_dict: Dictionary = JSON.parse_string(json_dict_as_string)
albedo_color.r = json_dict["albedo_color"]["r"]
albedo_color.g = json_dict["albedo_color"]["g"]
@@ -49,11 +40,11 @@ func from_json(json_dict_as_string):
# Make a StandardMaterial3D using our variables.
func make_material():
var mat = StandardMaterial3D.new()
func make_material() -> StandardMaterial3D:
var material := StandardMaterial3D.new()
mat.albedo_color = albedo_color
mat.metallic = metallic_strength
mat.roughness = roughness_strength
material.albedo_color = albedo_color
material.metallic = metallic_strength
material.roughness = roughness_strength
return mat
return material

View File

@@ -1,66 +1,72 @@
@tool
extends EditorImportPlugin
enum Presets { PRESET_DEFAULT }
enum Preset {
PRESET_DEFAULT,
}
func _get_importer_name():
func _get_importer_name() -> String:
return "demos.sillymaterial"
func _get_visible_name():
func _get_visible_name() -> String:
return "Silly Material"
func _get_recognized_extensions():
func _get_recognized_extensions() -> PackedStringArray:
return ["mtxt"]
func _get_save_extension():
func _get_save_extension() -> String:
return "res"
func _get_resource_type():
func _get_resource_type() -> String:
return "Material"
func _get_preset_count():
return Presets.size()
func _get_preset_count() -> int:
return Preset.size()
func _get_preset_name(preset):
func _get_preset_name(preset: Preset) -> String:
match preset:
Presets.PRESET_DEFAULT: return "Default"
_: return "Unknown"
Preset.PRESET_DEFAULT:
return "Default"
_:
return "Unknown"
func _get_import_options(_path, preset):
func _get_import_options(_path: String, preset: Preset) -> Array[Dictionary]:
match preset:
Presets.PRESET_DEFAULT:
Preset.PRESET_DEFAULT:
return [{
"name": "use_red_anyway",
"default_value": false
}]
_: return []
"name": "use_red_anyway",
"default_value": false,
}]
_:
return []
func _get_import_order():
func _get_import_order() -> int:
return ResourceImporter.IMPORT_ORDER_DEFAULT
func _get_option_visibility(path, option, options):
func _get_option_visibility(path: String, option: StringName, options: Dictionary) -> bool:
return true
func _import(source_file, save_path, options, r_platform_variants, r_gen_files):
var file = FileAccess.open(source_file, FileAccess.READ)
var line = file.get_line()
func _import(source_file: String, save_path: String, options: Dictionary, r_platform_variants: Array[String], r_gen_files: Array[String]) -> Error:
var file := FileAccess.open(source_file, FileAccess.READ)
var line := file.get_line()
var channels = line.split(",")
var channels := line.split(",")
if channels.size() != 3:
return ERR_PARSE_ERROR
var color = Color8(int(channels[0]), int(channels[1]), int(channels[2]))
var material = StandardMaterial3D.new()
var color := Color8(int(channels[0]), int(channels[1]), int(channels[2]))
var material := StandardMaterial3D.new()
if options.use_red_anyway:
color = Color8(255, 0, 0)

View File

@@ -1,13 +1,14 @@
@tool
extends EditorPlugin
var import_plugin
var import_plugin: EditorImportPlugin
func _enter_tree():
func _enter_tree() -> void:
import_plugin = preload("import.gd").new()
add_import_plugin(import_plugin)
func _exit_tree():
func _exit_tree() -> void:
remove_import_plugin(import_plugin)
import_plugin = null

View File

@@ -25,9 +25,13 @@ run/main_scene="res://test_scene.tscn"
config/features=PackedStringArray("4.2")
config/icon="res://icon.webp"
[debug]
gdscript/warnings/untyped_declaration=1
[editor_plugins]
enabled=PackedStringArray("res://addons/main_screen/plugin.cfg", "res://addons/material_creator/plugin.cfg", "res://addons/material_import_plugin/plugin.cfg", "res://addons/custom_node/plugin.cfg")
enabled=PackedStringArray("res://addons/custom_node/plugin.cfg", "res://addons/main_screen/plugin.cfg", "res://addons/material_creator/plugin.cfg", "res://addons/material_import_plugin/plugin.cfg")
[rendering]

View File

@@ -12,7 +12,6 @@ script = ExtResource("2")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
mesh = SubResource("1")
skeleton = NodePath("")
[node name="HandledByMainScreen" type="Node" parent="."]
script = ExtResource("1")