Skip to content

Commit 05cb45b

Browse files
jsm174freezy
authored andcommitted
units: Add new SetSwitch and PulseSwitch units
1 parent 5234855 commit 05cb45b

12 files changed

+498
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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(PulseSwitchUnit))]
25+
public class PulseSwitchUnitDescriptor : UnitDescriptor<PulseSwitchUnit>
26+
{
27+
public PulseSwitchUnitDescriptor(PulseSwitchUnit target) : base(target)
28+
{
29+
}
30+
31+
protected override string DefinedSummary()
32+
{
33+
return "This node enables switches defined by their IDs and disables it after a given delay.";
34+
}
35+
36+
protected override EditorTexture DefinedIcon()
37+
{
38+
var texture = VisualPinball.Unity.Editor.Icons.Switch(false, 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+
if (port.key == nameof(PulseSwitchUnit.PulseDelay)) {
47+
desc.summary = "The time in milliseconds until the switches are disabled again.";
48+
}
49+
else if (int.TryParse(port.key, out int id)) {
50+
id += 1;
51+
52+
desc.label = $"Switch ID {id}";
53+
desc.summary = $"Switch ID {id} of the switch to be pulsed.";
54+
}
55+
}
56+
}
57+
}

Editor/Descriptors/PulseSwitchUnitDescriptor.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.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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(SetSwitchUnit))]
25+
public class SetSwitchUnitDescriptor : UnitDescriptor<SetSwitchUnit>
26+
{
27+
public SetSwitchUnitDescriptor(SetSwitchUnit target) : base(target)
28+
{
29+
}
30+
31+
protected override string DefinedSummary()
32+
{
33+
return "This node assigns a given value to switches defined by their IDs.";
34+
}
35+
36+
protected override EditorTexture DefinedIcon()
37+
{
38+
var texture = VisualPinball.Unity.Editor.Icons.Switch(false, 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+
if (port.key == nameof(SetSwitchUnit.IsEnabled)) {
47+
desc.summary = "The value to assign to the switches.";
48+
}
49+
else if (int.TryParse(port.key, out int id)) {
50+
id += 1;
51+
52+
desc.label = $"Switch ID {id}";
53+
desc.summary = $"Switch ID {id} of the switch to be set.";
54+
}
55+
}
56+
}
57+
}

Editor/Descriptors/SetSwitchUnitDescriptor.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.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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(PulseSwitchUnit))]
27+
public sealed class PulseSwitchUnitWidget : GleUnitWidget<PulseSwitchUnit>
28+
{
29+
private readonly List<Func<Metadata, VariableNameInspector>> _switchIdInspectorConstructorList;
30+
31+
public PulseSwitchUnitWidget(FlowCanvas canvas, PulseSwitchUnit unit) : base(canvas, unit)
32+
{
33+
_switchIdInspectorConstructorList = new List<Func<Metadata, VariableNameInspector>>();
34+
}
35+
36+
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
37+
{
38+
if (_switchIdInspectorConstructorList.Count() < unit.inputCount) {
39+
for (var index = 0; index < unit.inputCount - _switchIdInspectorConstructorList.Count(); index++) {
40+
_switchIdInspectorConstructorList.Add(meta => new VariableNameInspector(meta, GetNameSuggestions));
41+
}
42+
}
43+
44+
for (var index = 0; index < unit.inputCount; index++) {
45+
if (unit.multiInputs[index] == port) {
46+
VariableNameInspector switchIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
47+
InspectorProvider.instance.Renew(ref switchIdInspector, meta, _switchIdInspectorConstructorList[index]);
48+
49+
return switchIdInspector;
50+
}
51+
}
52+
53+
return base.GetPortInspector(port, meta);
54+
}
55+
56+
private IEnumerable<string> GetNameSuggestions()
57+
{
58+
return !GleAvailable
59+
? new List<string>()
60+
: Gle.RequestedSwitches.Select(sw => sw.Id).ToList();
61+
}
62+
}
63+
}

Editor/Widgets/PulseSwitchUnitWidget.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.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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(SetSwitchUnit))]
27+
public sealed class SetSwitchUnitWidget : GleUnitWidget<SetSwitchUnit>
28+
{
29+
private readonly List<Func<Metadata, VariableNameInspector>> _switchIdInspectorConstructorList;
30+
31+
public SetSwitchUnitWidget(FlowCanvas canvas, SetSwitchUnit unit) : base(canvas, unit)
32+
{
33+
_switchIdInspectorConstructorList = new List<Func<Metadata, VariableNameInspector>>();
34+
}
35+
36+
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
37+
{
38+
if (_switchIdInspectorConstructorList.Count() < unit.inputCount) {
39+
for (var index = 0; index < unit.inputCount - _switchIdInspectorConstructorList.Count(); index++) {
40+
_switchIdInspectorConstructorList.Add(meta => new VariableNameInspector(meta, GetNameSuggestions));
41+
}
42+
}
43+
44+
for (var index = 0; index < unit.inputCount; index++) {
45+
if (unit.multiInputs[index] == port) {
46+
VariableNameInspector switchIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
47+
InspectorProvider.instance.Renew(ref switchIdInspector, meta, _switchIdInspectorConstructorList[index]);
48+
49+
return switchIdInspector;
50+
}
51+
}
52+
53+
return base.GetPortInspector(port, meta);
54+
}
55+
56+
private IEnumerable<string> GetNameSuggestions()
57+
{
58+
return !GleAvailable
59+
? new List<string>()
60+
: Gle.RequestedSwitches.Select(sw => sw.Id).ToList();
61+
}
62+
}
63+
}

Editor/Widgets/SetSwitchUnitWidget.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.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 System.Collections.Generic;
18+
using System.Collections.ObjectModel;
19+
using Unity.VisualScripting;
20+
using UnityEngine;
21+
22+
namespace VisualPinball.Unity.VisualScripting
23+
{
24+
[UnitTitle("Pulse Switch")]
25+
[UnitSurtitle("Gamelogic Engine")]
26+
[UnitCategory("Visual Pinball")]
27+
public class PulseSwitchUnit : GleUnit, IMultiInputUnit
28+
{
29+
[DoNotSerialize]
30+
[PortLabelHidden]
31+
public ControlInput InputTrigger;
32+
33+
[DoNotSerialize]
34+
[PortLabelHidden]
35+
public ControlOutput OutputTrigger;
36+
37+
[SerializeAs(nameof(inputCount))]
38+
private int _inputCount = 1;
39+
40+
[DoNotSerialize]
41+
[Inspectable, UnitHeaderInspectable("Switch IDs")]
42+
public int inputCount
43+
{
44+
get => _inputCount;
45+
set => _inputCount = Mathf.Clamp(value, 1, 10);
46+
}
47+
48+
[DoNotSerialize]
49+
public ReadOnlyCollection<ValueInput> multiInputs { get; private set; }
50+
51+
[DoNotSerialize]
52+
[PortLabel("Delay (ms)")]
53+
public ValueInput PulseDelay { get; private set; }
54+
55+
protected override void Definition()
56+
{
57+
InputTrigger = ControlInput(nameof(InputTrigger), Process);
58+
OutputTrigger = ControlOutput(nameof(OutputTrigger));
59+
60+
var _multiInputs = new List<ValueInput>();
61+
62+
multiInputs = _multiInputs.AsReadOnly();
63+
64+
for (var i = 0; i < inputCount; i++) {
65+
var input = ValueInput(i.ToString(), string.Empty);
66+
_multiInputs.Add(input);
67+
68+
Requirement(input, InputTrigger);
69+
}
70+
71+
PulseDelay = ValueInput(nameof(PulseDelay), 250);
72+
73+
Succession(InputTrigger, OutputTrigger);
74+
}
75+
76+
private ControlOutput Process(Flow flow)
77+
{
78+
if (!AssertGle(flow)) {
79+
Debug.LogError("Cannot find GLE.");
80+
return OutputTrigger;
81+
}
82+
83+
if (!AssertPlayer(flow)) {
84+
Debug.LogError("Cannot find Player.");
85+
return OutputTrigger;
86+
}
87+
88+
var pulseDuration = flow.GetValue<int>(PulseDelay);
89+
90+
foreach (var input in multiInputs) {
91+
var switchId = flow.GetValue<string>(input);
92+
Gle.Switch(switchId, true);
93+
Player.ScheduleAction(pulseDuration, () => Gle.Switch(switchId, false));
94+
}
95+
96+
return OutputTrigger;
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)