Skip to content

Commit 2197b0e

Browse files
jsm174freezy
authored andcommitted
units: rework itemCount/item keys to match MultiInput units
1 parent 50551b7 commit 2197b0e

19 files changed

+123
-114
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
21+
namespace VisualPinball.Unity.VisualScripting.Editor
22+
{
23+
[Descriptor(typeof(AllSwitchesEnabledEventUnit))]
24+
public class AllSwitchesEnabledEventUnitDescriptor : MultiUnitDescriptor<AllSwitchesEnabledEventUnit>
25+
{
26+
public AllSwitchesEnabledEventUnitDescriptor(AllSwitchesEnabledEventUnit target) : base(target) { }
27+
protected override string ItemLabel(int id) => $"Switch ID {id}";
28+
protected override string ItemDescription(int id) => $"Switch ID {id} to look for enabled status.";
29+
protected override string DefinedSummary() => "This node triggers an event when the last switch in the list gets enabled.";
30+
protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.SwitchEvent);
31+
}
32+
}

Editor/Descriptors/AllSwitchesEnabledEventUnitDescriptor.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/Descriptors/GetSwitchUnitDescriptor.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
// ReSharper disable UnusedType.Global
1818

19-
using System.Text.RegularExpressions;
2019
using Unity.VisualScripting;
2120
using VisualPinball.Unity.Editor;
2221
using IconSize = VisualPinball.Unity.Editor.IconSize;
@@ -44,15 +43,11 @@ protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
4443
if (port.key == nameof(GetSwitchUnit.IsEnabled)) {
4544
desc.summary = "Whether the switch is enabled.";
4645
}
47-
else {
48-
var match = new Regex("^(item)([0-9]+)$").Match(port.key);
46+
else if (int.TryParse(port.key, out int id)) {
47+
id += 1;
4948

50-
if (match.Success) {
51-
var id = int.Parse(match.Groups[2].Value) + 1;
52-
53-
desc.label = $"Switch ID {id}";
54-
desc.summary = $"Switch ID {id} to check if enabled.";
55-
}
49+
desc.label = $"Switch ID {id}";
50+
desc.summary = $"Switch ID {id} to check if enabled.";
5651
}
5752
}
5853
}

Editor/Descriptors/MultiUnitDescriptor.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,10 @@
1616

1717
// ReSharper disable UnusedType.Global
1818

19-
using System.Text.RegularExpressions;
2019
using Unity.VisualScripting;
2120

2221
namespace VisualPinball.Unity.VisualScripting.Editor
2322
{
24-
[Descriptor(typeof(AllSwitchesEnabledEventUnit))]
25-
public class AllSwitchesEnabledEventUnitDescriptor : MultiUnitDescriptor<AllSwitchesEnabledEventUnit>
26-
{
27-
public AllSwitchesEnabledEventUnitDescriptor(AllSwitchesEnabledEventUnit target) : base(target) { }
28-
protected override string ItemLabel(int id) => $"Switch ID {id}";
29-
protected override string ItemDescription(int id) => $"Switch ID {id} to look for enabled status.";
30-
protected override string DefinedSummary() => "This node triggers an event when the last switch in the list gets enabled.";
31-
protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.SwitchEvent);
32-
}
33-
3423
public abstract class MultiUnitDescriptor<TUnit> : UnitDescriptor<TUnit> where TUnit : class, IUnit
3524
{
3625
protected abstract string ItemLabel(int id);
@@ -43,9 +32,10 @@ protected MultiUnitDescriptor(TUnit target) : base(target)
4332
protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
4433
{
4534
base.DefinedPort(port, desc);
46-
var match = new Regex("^(item)([0-9]+)$").Match(port.key);
47-
if (match.Success) {
48-
var id = int.Parse(match.Groups[2].Value) + 1;
35+
36+
if (int.TryParse(port.key, out int id)) {
37+
id += 1;
38+
4939
desc.label = ItemLabel(id);
5040
desc.summary = ItemDescription(id);
5141
}

Editor/Descriptors/PulseCoilUnitDescriptor.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
// ReSharper disable UnusedType.Global
1818

19-
using System.Text.RegularExpressions;
2019
using Unity.VisualScripting;
2120
using VisualPinball.Unity.Editor;
2221

@@ -47,15 +46,11 @@ protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
4746
if (port.key == nameof(PulseCoilUnit.PulseDuration)) {
4847
desc.summary = "The time in milliseconds until the coils are disabled again.";
4948
}
50-
else {
51-
var match = new Regex("^(item)([0-9]+)$").Match(port.key);
49+
else if (int.TryParse(port.key, out int id)) {
50+
id += 1;
5251

53-
if (match.Success) {
54-
var id = int.Parse(match.Groups[2].Value) + 1;
55-
56-
desc.label = $"Coil ID {id}";
57-
desc.summary = $"Coil ID {id} of the coil to be pulsed.";
58-
}
52+
desc.label = $"Coil ID {id}";
53+
desc.summary = $"Coil ID {id} of the coil to be pulsed.";
5954
}
6055
}
6156
}

Editor/Descriptors/SetCoilUnitDescriptor.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
// ReSharper disable UnusedType.Global
1818

19-
using System.Text.RegularExpressions;
2019
using Unity.VisualScripting;
2120
using VisualPinball.Unity.Editor;
2221

@@ -47,15 +46,11 @@ protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
4746
if (port.key == nameof(SetCoilUnit.IsEnabled)) {
4847
desc.summary = "The value to assign to the coils.";
4948
}
50-
else {
51-
var match = new Regex("^(item)([0-9]+)$").Match(port.key);
49+
else if (int.TryParse(port.key, out int id)) {
50+
id += 1;
5251

53-
if (match.Success) {
54-
var id = int.Parse(match.Groups[2].Value) + 1;
55-
56-
desc.label = $"Coil ID {id}";
57-
desc.summary = $"Coil ID {id} of the coil to be set.";
58-
}
52+
desc.label = $"Coil ID {id}";
53+
desc.summary = $"Coil ID {id} of the coil to be set.";
5954
}
6055
}
6156
}

Editor/Descriptors/SwitchEnabledEventUnitDescriptor.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
// ReSharper disable UnusedType.Global
1818

19-
using System.Text.RegularExpressions;
2019
using Unity.VisualScripting;
2120

2221
namespace VisualPinball.Unity.VisualScripting.Editor
@@ -39,10 +38,8 @@ protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
3938
{
4039
base.DefinedPort(port, desc);
4140

42-
var match = new Regex("^(item)([0-9]+)$").Match(port.key);
43-
44-
if (match.Success) {
45-
var id = int.Parse(match.Groups[2].Value) + 1;
41+
if (int.TryParse(port.key, out int id)) {
42+
id += 1;
4643

4744
desc.label = $"Switch ID {id}";
4845
desc.summary = $"Switch ID {id} to look for enabled status.";

Editor/Descriptors/SwitchLampUnitDescriptor.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
// ReSharper disable UnusedType.Global
1818

19-
using System;
20-
using System.Text.RegularExpressions;
2119
using Unity.VisualScripting;
2220
using VisualPinball.Unity.Editor;
2321
using IconSize = VisualPinball.Unity.Editor.IconSize;
@@ -45,15 +43,11 @@ protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
4543
if (port.key == nameof(SwitchLampUnit.SourceValue)) {
4644
desc.summary = "Source value to use for matching";
4745
}
48-
else {
49-
var match = new Regex("^(item)([0-9]+)$").Match(port.key);
46+
else if (int.TryParse(port.key, out int id)) {
47+
id += 1;
5048

51-
if (match.Success) {
52-
var id = int.Parse(match.Groups[2].Value) + 1;
53-
54-
desc.label = $"Lamp ID {id}";
55-
desc.summary = $"Lamp ID {id} to enable if specified Value matches source Value, or disable if specified Value does not match source Value";
56-
}
49+
desc.label = $"Lamp ID {id}";
50+
desc.summary = $"Lamp ID {id} to enable if specified Value matches source Value, or disable if specified Value does not match source Value";
5751
}
5852
}
5953
}

Editor/Widgets/GetSwitchUnitWidget.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ public GetSwitchUnitWidget(FlowCanvas canvas, GetSwitchUnit unit) : base(canvas,
3535

3636
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
3737
{
38-
if (_switchIdInspectorConstructorList.Count() < unit.itemCount) {
39-
for (var index = 0; index < unit.itemCount - _switchIdInspectorConstructorList.Count(); index++) {
38+
if (_switchIdInspectorConstructorList.Count() < unit.inputCount) {
39+
for (var index = 0; index < unit.inputCount - _switchIdInspectorConstructorList.Count(); index++) {
4040
_switchIdInspectorConstructorList.Add(meta => new VariableNameInspector(meta, GetNameSuggestions));
4141
}
4242
}
4343

44-
for (var index = 0; index < unit.itemCount; index++) {
44+
for (var index = 0; index < unit.inputCount; index++) {
4545
if (unit.Items[index] == port) {
4646
VariableNameInspector switchIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
4747
InspectorProvider.instance.Renew(ref switchIdInspector, meta, _switchIdInspectorConstructorList[index]);

Editor/Widgets/PulseCoilUnitWidget.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ public PulseCoilUnitWidget(FlowCanvas canvas, PulseCoilUnit unit) : base(canvas,
3535

3636
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
3737
{
38-
if (_coilIdInspectorConstructorList.Count() < unit.itemCount) {
39-
for (var index = 0; index < unit.itemCount - _coilIdInspectorConstructorList.Count(); index++) {
38+
if (_coilIdInspectorConstructorList.Count() < unit.inputCount) {
39+
for (var index = 0; index < unit.inputCount - _coilIdInspectorConstructorList.Count(); index++) {
4040
_coilIdInspectorConstructorList.Add(meta => new VariableNameInspector(meta, GetNameSuggestions));
4141
}
4242
}
4343

44-
for (var index = 0; index < unit.itemCount; index++) {
44+
for (var index = 0; index < unit.inputCount; index++) {
4545
if (unit.Items[index] == port) {
4646
VariableNameInspector coilIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
4747
InspectorProvider.instance.Renew(ref coilIdInspector, meta, _coilIdInspectorConstructorList[index]);

0 commit comments

Comments
 (0)