Skip to content

Commit

Permalink
Adding some old godot utilities from some application
Browse files Browse the repository at this point in the history
  • Loading branch information
PereViader committed May 21, 2024
1 parent 0e645de commit cc45e8a
Show file tree
Hide file tree
Showing 21 changed files with 300 additions and 0 deletions.
2 changes: 2 additions & 0 deletions PereViader.Utils.Godot/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Normalize EOL for all files that Git considers text files.
* text=auto eol=lf
2 changes: 2 additions & 0 deletions PereViader.Utils.Godot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Godot 4+ specific ignores
.godot/

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions PereViader.Utils.Godot/PereViader.Utils.Godot.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Godot.NET.Sdk/4.1.1">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
<Nullable>enable</Nullable>
<WarningsAsErrors>true</WarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="PereViader.Utils.Common" Version="1.0.2" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=scripts/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
19 changes: 19 additions & 0 deletions PereViader.Utils.Godot/PereViader.Utils.Godot.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PereViader.Utils.Godot", "PereViader.Utils.Godot.csproj", "{14494561-92F7-44F4-AF11-FBF5CEE11162}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
ExportDebug|Any CPU = ExportDebug|Any CPU
ExportRelease|Any CPU = ExportRelease|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{14494561-92F7-44F4-AF11-FBF5CEE11162}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{14494561-92F7-44F4-AF11-FBF5CEE11162}.Debug|Any CPU.Build.0 = Debug|Any CPU
{14494561-92F7-44F4-AF11-FBF5CEE11162}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU
{14494561-92F7-44F4-AF11-FBF5CEE11162}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU
{14494561-92F7-44F4-AF11-FBF5CEE11162}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU
{14494561-92F7-44F4-AF11-FBF5CEE11162}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU
EndGlobalSection
EndGlobal
3 changes: 3 additions & 0 deletions PereViader.Utils.Godot/Samples/Sample.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[gd_scene format=3 uid="uid://c4s0cdav7j12w"]

[node name="Node2D" type="Node2D"]
11 changes: 11 additions & 0 deletions PereViader.Utils.Godot/Scripts/AnimationPlayerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Godot;

namespace PereViader.Utils.Godot;

public static class AnimationPlayerExtensions
{
public static void CompleteCurrentAnimation(this AnimationPlayer animationPlayer)
{
animationPlayer.Advance(double.MaxValue);
}
}
24 changes: 24 additions & 0 deletions PereViader.Utils.Godot/Scripts/AudioStreamPlayerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Threading;
using System.Threading.Tasks;
using Godot;
using PereViader.Utils.Common.Extensions;

namespace PereViader.Utils.Godot;

public static class AudioStreamPlayerExtensions
{
public static Task AwaitFinished(this AudioStreamPlayer audioStreamPlayer, CancellationToken cancellationToken)
{
TaskCompletionSource<object> tcs = new();
tcs.LinkCancellationToken(cancellationToken);

void Finished()
{
audioStreamPlayer.Finished -= Finished;
tcs.TrySetResult(default!);
}

audioStreamPlayer.Finished += Finished;
return tcs.Task;
}
}
36 changes: 36 additions & 0 deletions PereViader.Utils.Godot/Scripts/Awaiter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace PereViader.Utils.Godot;

public sealed class Awaiter<T> : IDisposable
{
private TaskCompletionSource<T>? _tcs;

public void Receive(T value)
{
var currentTcs = _tcs;
_tcs = null;
currentTcs?.TrySetResult(value);
}

public void Cancel()
{
var currentTcs = _tcs;
_tcs = null;
currentTcs?.TrySetCanceled();
}

public Task<T> AwaitNext(CancellationToken cancellationToken = default)
{
_tcs ??= new TaskCompletionSource<T>();

return _tcs.Task.WaitAsync(cancellationToken);
}

public void Dispose()
{
_tcs?.TrySetCanceled();
}
}
16 changes: 16 additions & 0 deletions PereViader.Utils.Godot/Scripts/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;

namespace PereViader.Utils.Godot;

public static class EnumerableExtensions
{
public static IEnumerable<(T value, int index)> ZipIndex<T>(this IEnumerable<T> enumerable)
{
int index = 0;
foreach (var value in enumerable)
{
yield return (value, index);
index++;
}
}
}
31 changes: 31 additions & 0 deletions PereViader.Utils.Godot/Scripts/FlowExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Threading;
using System.Threading.Tasks;

namespace PereViader.Utils.Godot;

public static class FlowExtensions
{
public delegate bool FirstTimeActionDelegate(bool isFirstTime);

public static void RepeatUntilSuccess(FirstTimeActionDelegate action)
{
var succeeded = action.Invoke(true);
while (!succeeded)
{
succeeded = action.Invoke(false);
}
}

public delegate Task<bool> FirstTimeActionDelegateAsync(bool isFirstTime, CancellationToken cancellationToken);

public static async Task RepeatUntilSuccess(FirstTimeActionDelegateAsync action, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
var succeeded = await action.Invoke(true, cancellationToken);
while (!succeeded)
{
cancellationToken.ThrowIfCancellationRequested();
succeeded = await action.Invoke(false, cancellationToken);
}
}
}
20 changes: 20 additions & 0 deletions PereViader.Utils.Godot/Scripts/SceneTreeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Threading;
using System.Threading.Tasks;
using PereViader.Utils.Common.Extensions;
using Godot;

namespace PereViader.Utils.Godot;

public static class SceneTreeExtensions
{
public static Task AwaitTime(this SceneTree sceneTree, double seconds, bool processAlways = true, bool processInPhysics = false, bool ignoreTimeScale = false, CancellationToken cancellationToken = default)
{
var timer = sceneTree
.CreateTimer(seconds, processAlways, processInPhysics, ignoreTimeScale);

var tcs = new TaskCompletionSource<object>();
timer.Timeout += () => tcs.SetResult(default!);
tcs.LinkCancellationToken(cancellationToken);
return tcs.Task;
}
}
17 changes: 17 additions & 0 deletions PereViader.Utils.Godot/Scripts/TaskExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Threading.Tasks;

namespace PereViader.Utils.Godot;

public static class TaskExtensions
{
public static async void RunAsync(this Task task)
{
try
{
await task;
}
catch (TaskCanceledException)
{
}
}
}
13 changes: 13 additions & 0 deletions PereViader.Utils.Godot/Scripts/TimerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Godot;

namespace PereViader.Utils.Godot;

public static class TimerExtensions
{
public static SignalAwaiter StartAndAwait(this Timer timer)
{
var signal = timer.ToSignal(timer, Timer.SignalName.Timeout);
timer.Start();
return signal;
}
}
1 change: 1 addition & 0 deletions PereViader.Utils.Godot/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions PereViader.Utils.Godot/icon.svg.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://c63aaqjwap5pw"
path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://icon.svg"
dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
svg/scale=1.0
editor/scale_with_editor_scale=false
editor/convert_colors_with_editor_theme=false
24 changes: 24 additions & 0 deletions PereViader.Utils.Godot/project.godot
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
; [section] ; section goes between []
; param=value ; assign values to parameters

config_version=5

[application]

config/name="PereViader.Utils.Godot"
run/main_scene="res://Samples/Sample.tscn"
config/features=PackedStringArray("4.1", "Mobile")
config/icon="res://icon.svg"

[dotnet]

project/assembly_name="PereViader.Utils.Godot"

[rendering]

renderer/rendering_method="mobile"

0 comments on commit cc45e8a

Please sign in to comment.