Skip to content

Commit

Permalink
Add a C# version of the plugin
Browse files Browse the repository at this point in the history
Add a C# version of the plugin
  • Loading branch information
patrickdown committed Nov 24, 2023
1 parent dce8720 commit 1e8d5db
Show file tree
Hide file tree
Showing 44 changed files with 1,503 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ build
.vscode
.vs

*.sln
*.csproj

# Binaries
*.o
*.os
Expand Down
4 changes: 3 additions & 1 deletion SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ if env['platform'] == 'windows':

f1 = env.Command('example.gd/addons/tiltfive/bin/libgdtiltfive{}{}'.format(env['suffix'], env['SHLIBSUFFIX']), library, Copy('$TARGET', '$SOURCE') )
f2 = env.Command('example.gd/addons/tiltfive/bin/TiltFiveNative.dll', tilt_five_library_path + '/TiltFiveNative.dll', Copy('$TARGET', '$SOURCE') )
f3 = env.Command('example.csharp/addons/tiltfive/bin/libgdtiltfive{}{}'.format(env['suffix'], env['SHLIBSUFFIX']), library, Copy('$TARGET', '$SOURCE') )
f4 = env.Command('example.csharp/addons/tiltfive/bin/TiltFiveNative.dll', tilt_five_library_path + '/TiltFiveNative.dll', Copy('$TARGET', '$SOURCE') )

env.Alias('example', [f1, f2])
env.Alias('example', [f1, f2, f3, f4])

Default(library)
50 changes: 50 additions & 0 deletions example.csharp/Controls.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[gd_scene load_steps=4 format=3 uid="uid://dnx42xctfl3mx"]

[sub_resource type="CapsuleMesh" id="4"]

[sub_resource type="BoxMesh" id="BoxMesh_dc5d6"]

[sub_resource type="SphereMesh" id="3"]

[node name="Controls" type="Node3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.0748157, -0.00207818, 0.009782)

[node name="Trigger" type="MeshInstance3D" parent="."]
transform = Transform3D(0.01, 0, 0, 0, 0.01, 0, 0, 0, 0.01, -0.000888377, -0.0415742, -0.0168045)
mesh = SubResource("4")

[node name="TriggerClick" type="MeshInstance3D" parent="."]
transform = Transform3D(0.01, 0, 0, 0, 0.01, 0, 0, 0, 0.01, -0.000888377, -0.00848173, -0.0840266)
mesh = SubResource("BoxMesh_dc5d6")

[node name="Three" type="MeshInstance3D" parent="."]
transform = Transform3D(0.01, 0, 0, 0, 0.01, 0, 0, 0, 0.01, 0, 0, 0)
mesh = SubResource("3")

[node name="One" type="MeshInstance3D" parent="."]
transform = Transform3D(0.01, 0, 0, 0, 0.01, 0, 0, 0, 0.01, -0.02, 0, 0.05)
mesh = SubResource("3")

[node name="Two" type="MeshInstance3D" parent="."]
transform = Transform3D(0.01, 0, 0, 0, 0.01, 0, 0, 0, 0.01, 0.02, 0, 0.05)
mesh = SubResource("3")

[node name="T5" type="MeshInstance3D" parent="."]
transform = Transform3D(-4.37114e-10, -0.01, 0, 0.01, -4.37114e-10, 0, 0, 0, 0.01, 0, 0, 0.08)
mesh = SubResource("4")

[node name="X" type="MeshInstance3D" parent="."]
transform = Transform3D(0.01, 0, 0, 0, 0.01, 0, 0, 0, 0.01, 0, 0, 0.15)
mesh = SubResource("3")

[node name="B" type="MeshInstance3D" parent="."]
transform = Transform3D(0.01, 0, 0, 0, 0.01, 0, 0, 0, 0.01, 0, 0, 0.21)
mesh = SubResource("3")

[node name="Y" type="MeshInstance3D" parent="."]
transform = Transform3D(0.01, 0, 0, 0, 0.01, 0, 0, 0, 0.01, 0.03, 0, 0.18)
mesh = SubResource("3")

[node name="A" type="MeshInstance3D" parent="."]
transform = Transform3D(0.01, 0, 0, 0, 0.01, 0, 0, 0, 0.01, -0.03, 0, 0.18)
mesh = SubResource("3")
30 changes: 30 additions & 0 deletions example.csharp/ExampleRig.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[gd_scene load_steps=9 format=3 uid="uid://ba8h6c1mtb3h0"]

[ext_resource type="PackedScene" uid="uid://dpbt52d0p5wjw" path="res://addons/tiltfive/scenes/T5XRRig.tscn" id="1_x7gas"]
[ext_resource type="PackedScene" uid="uid://b1cd3jc00rhal" path="res://addons/tiltfive/assets/T5GlassesModel.tscn" id="2_dp1ep"]
[ext_resource type="Script" path="res://WandControl.cs" id="2_epf7w"]
[ext_resource type="PackedScene" uid="uid://dnx42xctfl3mx" path="res://Controls.tscn" id="2_ge6xw"]
[ext_resource type="Script" path="res://addons/tiltfive/assets/AutoScale.cs" id="3_qxj3p"]
[ext_resource type="PackedScene" uid="uid://bggv3rg34lcs6" path="res://addons/tiltfive/assets/T5-wand.glb" id="6_7gqrr"]

[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_tnkdi"]
albedo_color = Color(0.580392, 0.396078, 0.278431, 1)

[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_kgxv6"]
albedo_color = Color(0.0352941, 1, 0, 1)

[node name="T5XRRig" instance=ExtResource("1_x7gas")]

[node name="T5-glasses" parent="Origin/Camera" index="0" instance=ExtResource("2_dp1ep")]
script = ExtResource("3_qxj3p")

[node name="Wand_1" parent="Origin" index="1"]
script = ExtResource("2_epf7w")
unselected = SubResource("StandardMaterial3D_tnkdi")
selected = SubResource("StandardMaterial3D_kgxv6")

[node name="Controls" parent="Origin/Wand_1" index="0" instance=ExtResource("2_ge6xw")]
transform = Transform3D(10, 0, 0, 0, 10, 0, 0, 0, 10, 0.585525, -0.00207818, 0.223126)

[node name="T5-wand" parent="Origin/Wand_1" index="1" instance=ExtResource("6_7gqrr")]
script = ExtResource("3_qxj3p")
113 changes: 113 additions & 0 deletions example.csharp/WandControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Threading;

public partial class WandControl : T5ControllerCS
{
static Dictionary<string, NodePath> buttons = new()
{
{"button_a", "Controls/A" },
{"button_b", "Controls/B" },
{"button_x", "Controls/X" },
{"button_y", "Controls/Y" },
{"button_1", "Controls/One" },
{"button_2", "Controls/Two" },
{"button_3", "Controls/Three" },
{"button_t5", "Controls/T5" },
{ "trigger_click", "Controls/TriggerClick" }

};

[Export]
public Material unselected;

[Export]
public Material selected;

Vector3 triggerPos;
Vector3 stickPos;

public override void _EnterTree()
{
base._EnterTree();

Connect("button_pressed", Callable.From<StringName>(OnButtonPressed));
Connect("button_released", Callable.From<StringName>(OnButtonReleased));
}

public override void _ExitTree()
{
Disconnect("button_pressed", Callable.From<StringName>(OnButtonPressed));
Disconnect("button_released", Callable.From<StringName>(OnButtonReleased));

base._ExitTree();
}

// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
foreach (var entry in buttons)
{
var node = GetNodeOrNull<MeshInstance3D>(entry.Value);
if (node != null)
node.MaterialOverride = unselected;
}
var triggerNode = GetNodeOrNull<MeshInstance3D>("Controls/Trigger");
if (triggerNode != null)
{
triggerNode.MaterialOverride = unselected;
triggerPos = triggerNode.Transform.Origin;
}
var stickNode = GetNodeOrNull<MeshInstance3D>("Controls/Three");
if (stickNode != null)
{
stickNode.MaterialOverride = unselected;
stickPos = stickNode.Transform.Origin;
}
}

// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
base._Process(delta);

var triggerNode = GetNodeOrNull<MeshInstance3D>("Controls/Trigger");
if(triggerNode != null)
{
var triggerValue = GetFloat("trigger") * 0.03f;
var transform = triggerNode.Transform;
transform.Origin = triggerPos + new Vector3(0, 0, triggerValue);
triggerNode.Transform = transform;
}

var stickNode = GetNodeOrNull<MeshInstance3D>("Controls/Three");
if (stickNode != null)
{
var stickValue = GetVector2("stick") * 0.03f;
var transform = stickNode.Transform;
transform.Origin = stickPos + new Vector3(stickValue.X, 0, -stickValue.Y);
stickNode.Transform = transform;
}
}

public void OnButtonPressed(StringName name)
{
if(buttons.TryGetValue(name, out var button))
{
var node = GetNodeOrNull<MeshInstance3D>(button);
if (node != null)
node.MaterialOverride = selected;
}
}

public void OnButtonReleased(StringName name)
{
if (buttons.TryGetValue(name, out var button))
{
var node = GetNodeOrNull<MeshInstance3D>(button);
if (node != null)
node.MaterialOverride = unselected;
}
}
}
Loading

0 comments on commit 1e8d5db

Please sign in to comment.