Skip to content

Commit 00ca19a

Browse files
committed
Add coil pulse node.
1 parent ca12be7 commit 00ca19a

File tree

6 files changed

+221
-0
lines changed

6 files changed

+221
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Visual Pinball Engine
2+
// Copyright (C) 2022 freezy and VPE Team
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
// ReSharper disable UnusedType.Global
18+
19+
using Unity.VisualScripting;
20+
using VisualPinball.Unity.Editor;
21+
22+
namespace VisualPinball.Unity.VisualScripting.Editor
23+
{
24+
[Descriptor(typeof(PulseCoilUnit))]
25+
public class PulseCoilUnitDescriptor : UnitDescriptor<PulseCoilUnit>
26+
{
27+
public PulseCoilUnitDescriptor(PulseCoilUnit target) : base(target)
28+
{
29+
}
30+
31+
protected override string DefinedSummary()
32+
{
33+
return "This node enables a coil defined by its ID and disables it after a given delay.";
34+
}
35+
36+
protected override EditorTexture DefinedIcon()
37+
{
38+
var texture = VisualPinball.Unity.Editor.Icons.Coil(VisualPinball.Unity.Editor.IconSize.Large, IconColor.Orange);
39+
return EditorTexture.Single(texture);
40+
}
41+
42+
protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
43+
{
44+
base.DefinedPort(port, desc);
45+
46+
switch (port.key) {
47+
case nameof(PulseCoilUnit.Id):
48+
desc.summary = "The ID of the coil to be pulsed.";
49+
break;
50+
case nameof(PulseCoilUnit.PulseDuration):
51+
desc.summary = "The time in milliseconds until the coil is disabled again.";
52+
break;
53+
}
54+
}
55+
}
56+
}

Editor/Descriptors/PulseCoilUnitDescriptor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Widgets/PulseCoilUnitWidget.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Visual Pinball Engine
2+
// Copyright (C) 2022 freezy and VPE Team
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
// ReSharper disable UnusedType.Global
18+
19+
using System;
20+
using System.Collections.Generic;
21+
using System.Linq;
22+
using Unity.VisualScripting;
23+
24+
namespace VisualPinball.Unity.VisualScripting.Editor
25+
{
26+
[Widget(typeof(PulseCoilUnit))]
27+
public sealed class PulseCoilUnitWidget : GleUnitWidget<PulseCoilUnit>
28+
{
29+
private VariableNameInspector _coilIdInspector;
30+
private readonly Func<Metadata, VariableNameInspector> _setCoilInspectorConstructor;
31+
32+
public PulseCoilUnitWidget(FlowCanvas canvas, PulseCoilUnit unit) : base(canvas, unit)
33+
{
34+
_setCoilInspectorConstructor = meta => new VariableNameInspector(meta, GetNameSuggestions);
35+
}
36+
37+
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
38+
{
39+
if (port == unit.Id) {
40+
InspectorProvider.instance.Renew(ref _coilIdInspector, meta, _setCoilInspectorConstructor);
41+
return _coilIdInspector;
42+
}
43+
44+
return base.GetPortInspector(port, meta);
45+
}
46+
47+
private IEnumerable<string> GetNameSuggestions()
48+
{
49+
if (!GameObjectAvailable) {
50+
return new List<string>();
51+
}
52+
var gle = Gle;
53+
return gle == null ? new List<string>() : gle.AvailableCoils.Select(coil => coil.Id).ToList();
54+
}
55+
}
56+
}

Editor/Widgets/PulseCoilUnitWidget.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Nodes/Coils/PulseCoilUnit.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Visual Pinball Engine
2+
// Copyright (C) 2022 freezy and VPE Team
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
using Unity.VisualScripting;
18+
using UnityEngine;
19+
20+
namespace VisualPinball.Unity.VisualScripting
21+
{
22+
[UnitTitle("Pulse Coil")]
23+
[UnitSurtitle("Gamelogic Engine")]
24+
[UnitCategory("Visual Pinball")]
25+
public class PulseCoilUnit : GleUnit
26+
{
27+
[DoNotSerialize]
28+
[PortLabelHidden]
29+
public ControlInput InputTrigger;
30+
31+
[DoNotSerialize]
32+
[PortLabelHidden]
33+
public ControlOutput OutputTrigger;
34+
35+
[DoNotSerialize]
36+
[PortLabel("Coil ID")]
37+
public ValueInput Id { get; private set; }
38+
39+
[DoNotSerialize]
40+
[PortLabel("Duration (ms)")]
41+
public ValueInput PulseDuration { get; private set; }
42+
43+
protected override void Definition()
44+
{
45+
InputTrigger = ControlInput(nameof(InputTrigger), Process);
46+
OutputTrigger = ControlOutput(nameof(OutputTrigger));
47+
48+
Id = ValueInput<string>(nameof(Id), string.Empty);
49+
PulseDuration = ValueInput<int>(nameof(PulseDuration), 80);
50+
51+
Requirement(Id, InputTrigger);
52+
Succession(InputTrigger, OutputTrigger);
53+
}
54+
55+
private ControlOutput Process(Flow flow)
56+
{
57+
if (!AssertGle(flow)) {
58+
Debug.LogError("Cannot find GLE.");
59+
return OutputTrigger;
60+
}
61+
62+
if (!AssertPlayer(flow)) {
63+
Debug.LogError("Cannot find GLE.");
64+
return OutputTrigger;
65+
}
66+
67+
var id = flow.GetValue<string>(Id);
68+
var pulseDuration = flow.GetValue<int>(PulseDuration);
69+
70+
Gle.SetCoil(id, true);
71+
Player.ScheduleAction(pulseDuration, () => Gle.SetCoil(id, false));
72+
73+
return OutputTrigger;
74+
}
75+
}
76+
}

Runtime/Nodes/Coils/PulseCoilUnit.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)