diff --git a/README.md b/README.md index 3c22d27..6eefc4e 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,10 @@ extension. ## List of plugins -| Plugin | Short description | Depends on | -| ------------------- | ------------------------------------------------ | -------------------------------------------- | -| GDScript Transpiler | Adds an editor interface for transpiling scripts | `gdscript_transpiler` module (experimental). | +| Plugin | Short description | Depends on | +| -------------------- | ------------------------------------------------- | ------------------------------------------- | +| `GridLayer` | Displays infinite grid at run-time. | `GridRect` class | +| `GDScriptTranspiler` | Adds an editor interface for transpiling scripts. | `gdscript_transpiler` module (experimental) | ## Compatibility diff --git a/addons/grid_layer/grid_layer.gd b/addons/grid_layer/grid_layer.gd new file mode 100644 index 0000000..145e906 --- /dev/null +++ b/addons/grid_layer/grid_layer.gd @@ -0,0 +1,62 @@ +class_name GridLayer, "icon_grid_layer.svg" extends CanvasLayer +tool + +# This class allows to display infinite grid at run-time, similarly to how +# editor displays grid with snapping is enabled in canvas editor. +# For example, when using Camera2D, the grid's origin offset and scale are +# going to be updated automatically. + +func _init(): + # Do not show grid on top, since we have transparent grid by default. + layer = -1 + + +func _ready(): + if not Engine.editor_hint: + return + + # Instantiate a default GridRect with transparent background. + var grid = GridRect.new() + add_child(grid) + if get_parent(): + if get_parent().filename: + grid.owner = get_parent() + else: + grid.owner = get_parent().owner + + grid.origin_axes_visible = true + + grid.anchor_right = 1.0 + grid.anchor_bottom = 1.0 + grid.margin_right = 0.0 + grid.margin_bottom = 0.0 + + var bc = Color() + bc.a = 0.0 # Transparent by default. + grid.set("custom_colors/background", bc) + # Use default colors from GraphEdit. + grid.set("custom_colors/line_cell", grid.get_color("grid_minor", "GraphEdit")) + grid.set("custom_colors/line_division", grid.get_color("grid_major", "GraphEdit")) + + +func _process(_delta): + for idx in get_child_count(): + var grid = get_child(idx) + if not grid is GridRect: + continue + + grid.origin_offset = get_viewport().canvas_transform.origin + grid.origin_scale = get_viewport().canvas_transform.get_scale() + + +func _get_configuration_warning(): + if not has_grid(): + return "GridLayer must have at least one GridRect added as a child." + return "" + + +func has_grid(): + for idx in get_child_count(): + if get_child(idx) is GridRect: + return true + return false diff --git a/addons/grid_layer/grid_layer_plugin.gd b/addons/grid_layer/grid_layer_plugin.gd new file mode 100644 index 0000000..212c53f --- /dev/null +++ b/addons/grid_layer/grid_layer_plugin.gd @@ -0,0 +1,2 @@ +tool +extends EditorPlugin diff --git a/addons/grid_layer/icon_grid_layer.svg b/addons/grid_layer/icon_grid_layer.svg new file mode 100644 index 0000000..67c0778 --- /dev/null +++ b/addons/grid_layer/icon_grid_layer.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/addons/grid_layer/icon_grid_layer.svg.import b/addons/grid_layer/icon_grid_layer.svg.import new file mode 100644 index 0000000..6c64485 --- /dev/null +++ b/addons/grid_layer/icon_grid_layer.svg.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/icon_grid_layer.svg-4d36d2a91fa21a54de7ea7a412dae37f.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://addons/grid_layer/icon_grid_layer.svg" +dest_files=[ "res://.import/icon_grid_layer.svg-4d36d2a91fa21a54de7ea7a412dae37f.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/addons/grid_layer/plugin.cfg b/addons/grid_layer/plugin.cfg new file mode 100644 index 0000000..07eb911 --- /dev/null +++ b/addons/grid_layer/plugin.cfg @@ -0,0 +1,7 @@ +[plugin] + +name="GridLayer" +description="Display infinite grid at run-time" +author="Goost" +version="0.1" +script="grid_layer_plugin.gd" diff --git a/project.godot b/project.godot index 5a06979..4a88b2e 100644 --- a/project.godot +++ b/project.godot @@ -13,9 +13,15 @@ _global_script_classes=[ { "class": "GoostImageTiling", "language": "GDScript", "path": "res://ports/image_tiling.gd" +}, { +"base": "CanvasLayer", +"class": "GridLayer", +"language": "GDScript", +"path": "res://addons/grid_layer/grid_layer.gd" } ] _global_script_class_icons={ -"GoostImageTiling": "" +"GoostImageTiling": "", +"GridLayer": "res://addons/grid_layer/icon_grid_layer.svg" } [application] @@ -25,4 +31,4 @@ config/icon="res://icon.png" [editor_plugins] -enabled=PoolStringArray( "gdscript_transpiler" ) +enabled=PoolStringArray( "res://addons/grid_layer/plugin.cfg" )