Skip to content

Instantly share code, notes, and snippets.

View Shilo's full-sized avatar

Shilo Shilo

View GitHub Profile
@Shilo
Shilo / SafeAreaContainer.cs
Last active February 19, 2025 05:35
Godot 4 container that automatically sets margins based on touch screen safe area.
using Godot;
using System;
[GlobalClass]
public partial class SafeAreaContainer : MarginContainer
{
private bool _isUpdating;
private Rect2I _safeArea;
private Timer _mobileOrientationListenerTimer;
private bool _left = true;
@Shilo
Shilo / PackedSceneExtension.cs
Last active January 20, 2025 06:39
Godot 4 PackedScene extension to get export property value based on property name.
public static partial class PackedSceneExtension
{
public static Variant? GetNodePropertyValue(this PackedScene packedScene, int nodeIndex, StringName propertyName)
{
SceneState sceneState = packedScene.GetState();
int propertyCount = sceneState.GetNodePropertyCount(nodeIndex);
for (int propertyIndex = 0; propertyIndex < propertyCount; propertyIndex++)
{
if (sceneState.GetNodePropertyName(nodeIndex, propertyIndex) == propertyName)
@Shilo
Shilo / trial_2d.gd
Last active January 19, 2025 22:01
Godot 4 Trail2D.
@tool
class_name Trail2D extends Node2D
@export var duration_sec: float = 0.2:
set(value):
_line.clear_points()
_point_times.clear()
duration_sec = value
@export var width: int = 10:
@Shilo
Shilo / Node2DExtension.cs
Last active December 28, 2024 09:50
Godot 4 Node extension for getting Node2D bounding box based on descendents.
using System.Linq;
public static partial class Node2DExtension
{
public static Rect2 GetCalculatedBounds(this Node2D node2D)
{
var rect = new Rect2();
var children = node2D.GetChildren().ToList();
while (children.Count > 0)
@Shilo
Shilo / TOLTouchScreenButton.cs
Last active December 26, 2024 16:55
Godot 4 more advanced version of the `TouchScreenButton` class that allows for more customization and control over the button's behavior.
/// <summary>
/// A more advanced version of the `TouchScreenButton` class that allows
/// for more customization and control over the button's behavior.
/// Example: Unhandled input processing, consuming input, circle hit box, and visual properties.
/// Fixes: Will not react to input that is handled/stopped by nodes. Will not pass input to nodes behind it.
/// TODO: Implement `PassbyPress` property.
/// </summary>
[GlobalClass, Tool]
public partial class TOLTouchScreenButton : TouchScreenButton
{
@Shilo
Shilo / icon_decoration.shader
Last active December 24, 2024 11:49
Godot 4 shader to create icon decorations (bordered and optional circle).
shader_type canvas_item;
uniform vec3 border_color: source_color = vec3(0);
uniform float border_width: hint_range(0, 10) = 0;
uniform bool circle = false;
void fragment() {
float center = 0.5;
float border_width_uv = border_width * min(TEXTURE_PIXEL_SIZE.x, TEXTURE_PIXEL_SIZE.y);
float border_start = center - border_width_uv;
@Shilo
Shilo / inner_border.shader
Created December 24, 2024 11:09
Godot 4 simple canvas item shader to add inner border.
shader_type canvas_item;
uniform vec4 border_color: source_color = vec4(1.0);
uniform float border_width: hint_range(0, 10) = 0;
void fragment() {
float border_width_uv = border_width * max(TEXTURE_PIXEL_SIZE.x, TEXTURE_PIXEL_SIZE.y);
if (step(0.5 - border_width_uv, abs(UV.x - 0.5)) == 1.0 ||
step(0.5 - border_width_uv, abs(UV.y - 0.5)) == 1.0) {
@Shilo
Shilo / bordered_circle_mask.shader
Last active December 24, 2024 10:58
Godot 4 simple canvas item shader to create a circle mask.
shader_type canvas_item;
uniform vec4 border_color: source_color = vec4(1.0);
uniform float border_width: hint_range(0, 10) = 0;
void fragment() {
float alpha = step(length(UV - vec2(0.5, 0.5)), 0.5);
if (alpha > 0.0) {
float border_width_uv = border_width * max(TEXTURE_PIXEL_SIZE.x, TEXTURE_PIXEL_SIZE.y);
if (step(0.5 - border_width_uv, length(UV - vec2(0.5, 0.5))) == 1.0) {
@Shilo
Shilo / Region.cs
Created December 9, 2024 09:13
CollisionPolygon2D editor tool to copy-paste polygon global position
[GlobalClass, Tool]
public partial class Region : CollisionPolygon2D
{
private List<Vector2> _editorGlobalPolygon;
/// <summary>
/// When enabled, the polygon is stored as global space.
/// When disabled, the global polygon is restored as local space.
/// This allows workflow: enable property, move position, disable property.
/// </summary>
@Shilo
Shilo / GodotObjectExtension.cs
Created December 3, 2024 18:18
Godot 4 GodotObject helper methods.
public static partial class GodotObjectExtension
{
public static GodotObject NewWithScript(this GodotObject obj, Variant? script)
{
script ??= new Variant();
ulong id = obj.GetInstanceId();
obj.SetScript(script.Value);
return GodotObject.InstanceFromId(id);
}