Skip to content

Commit e5616a6

Browse files
committed
Refactor multi widgets to inherit from GleMultiUnitWidget.
1 parent 643f86c commit e5616a6

13 files changed

+78
-358
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Unity.VisualScripting;
4+
5+
namespace VisualPinball.Unity.VisualScripting.Editor
6+
{
7+
[Widget(typeof(AllSwitchesEnabledEventUnit))]
8+
public sealed class AllSwitchesEnabledEventUnitWidget : GleMultiUnitWidget<AllSwitchesEnabledEventUnit>
9+
{
10+
public AllSwitchesEnabledEventUnitWidget(FlowCanvas canvas, AllSwitchesEnabledEventUnit unit) : base(canvas, unit)
11+
{
12+
}
13+
protected override IEnumerable<string> IdSuggestions(IGamelogicEngine gle) => gle.RequestedSwitches.Select(sw => sw.Id);
14+
}
15+
}

Editor/Widgets/AllSwitchesEnabledEventUnitWidget.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/GleMultiUnitWidget.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@
2121

2222
namespace VisualPinball.Unity.VisualScripting.Editor
2323
{
24-
[Widget(typeof(AllSwitchesEnabledEventUnit))]
25-
public sealed class AllSwitchesEnabledEventUnitWidget : GleMultiUnitWidget<AllSwitchesEnabledEventUnit>
26-
{
27-
public AllSwitchesEnabledEventUnitWidget(FlowCanvas canvas, AllSwitchesEnabledEventUnit unit) : base(canvas, unit)
28-
{
29-
}
30-
protected override IEnumerable<string> IdSuggestions(IGamelogicEngine gle) => gle.RequestedSwitches.Select(sw => sw.Id);
31-
}
32-
3324
public abstract class GleMultiUnitWidget<TUnit> : GleUnitWidget<TUnit> where TUnit : Unit, IGleUnit, IMultiInputUnit
3425
{
3526
protected abstract IEnumerable<string> IdSuggestions(IGamelogicEngine gle);
Lines changed: 34 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,34 @@
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(LampEventUnit))]
27-
public sealed class LampEventUnitWidget : GleUnitWidget<LampEventUnit>
28-
{
29-
private readonly List<Func<Metadata, VariableNameInspector>> _lampIdInspectorConstructorList;
30-
31-
public LampEventUnitWidget(FlowCanvas canvas, LampEventUnit unit) : base(canvas, unit)
32-
{
33-
_lampIdInspectorConstructorList = new List<Func<Metadata, VariableNameInspector>>();
34-
}
35-
36-
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
37-
{
38-
if (_lampIdInspectorConstructorList.Count() < unit.inputCount)
39-
{
40-
for (var index = 0; index < unit.inputCount - _lampIdInspectorConstructorList.Count(); index++)
41-
{
42-
_lampIdInspectorConstructorList.Add(meta => new VariableNameInspector(meta, GetNameSuggestions));
43-
}
44-
}
45-
46-
for (var index = 0; index < unit.inputCount; index++)
47-
{
48-
if (unit.multiInputs[index] == port)
49-
{
50-
VariableNameInspector lampIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
51-
InspectorProvider.instance.Renew(ref lampIdInspector, meta, _lampIdInspectorConstructorList[index]);
52-
53-
return lampIdInspector;
54-
}
55-
}
56-
57-
return base.GetPortInspector(port, meta);
58-
}
59-
60-
private IEnumerable<string> GetNameSuggestions()
61-
{
62-
return !GleAvailable
63-
? new List<string>()
64-
: Gle.RequestedLamps.Select(lamp => lamp.Id).ToList();
65-
}
66-
}
67-
}
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.Collections.Generic;
20+
using System.Linq;
21+
using Unity.VisualScripting;
22+
23+
namespace VisualPinball.Unity.VisualScripting.Editor
24+
{
25+
[Widget(typeof(LampEventUnit))]
26+
public sealed class LampEventUnitWidget : GleMultiUnitWidget<LampEventUnit>
27+
{
28+
public LampEventUnitWidget(FlowCanvas canvas, LampEventUnit unit) : base(canvas, unit)
29+
{
30+
}
31+
32+
protected override IEnumerable<string> IdSuggestions(IGamelogicEngine gle) => gle.RequestedLamps.Select(lamp => lamp.Id);
33+
}
34+
}

Editor/Widgets/PulseCoilUnitWidget.cs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,12 @@
2424
namespace VisualPinball.Unity.VisualScripting.Editor
2525
{
2626
[Widget(typeof(PulseCoilUnit))]
27-
public sealed class PulseCoilUnitWidget : GleUnitWidget<PulseCoilUnit>
27+
public sealed class PulseCoilUnitWidget : GleMultiUnitWidget<PulseCoilUnit>
2828
{
29-
private readonly List<Func<Metadata, VariableNameInspector>> _coilIdInspectorConstructorList;
30-
3129
public PulseCoilUnitWidget(FlowCanvas canvas, PulseCoilUnit unit) : base(canvas, unit)
3230
{
33-
_coilIdInspectorConstructorList = new List<Func<Metadata, VariableNameInspector>>();
3431
}
3532

36-
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
37-
{
38-
if (_coilIdInspectorConstructorList.Count() < unit.inputCount) {
39-
for (var index = 0; index < unit.inputCount - _coilIdInspectorConstructorList.Count(); index++) {
40-
_coilIdInspectorConstructorList.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 coilIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
47-
InspectorProvider.instance.Renew(ref coilIdInspector, meta, _coilIdInspectorConstructorList[index]);
48-
49-
return coilIdInspector;
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.RequestedCoils.Select(coil => coil.Id).ToList();
61-
}
33+
protected override IEnumerable<string> IdSuggestions(IGamelogicEngine gle) => gle.RequestedCoils.Select(coil => coil.Id);
6234
}
6335
}

Editor/Widgets/PulseSwitchUnitWidget.cs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,19 @@
1616

1717
// ReSharper disable UnusedType.Global
1818

19-
using System;
2019
using System.Collections.Generic;
2120
using System.Linq;
2221
using Unity.VisualScripting;
2322

2423
namespace VisualPinball.Unity.VisualScripting.Editor
2524
{
2625
[Widget(typeof(PulseSwitchUnit))]
27-
public sealed class PulseSwitchUnitWidget : GleUnitWidget<PulseSwitchUnit>
26+
public sealed class PulseSwitchUnitWidget : GleMultiUnitWidget<PulseSwitchUnit>
2827
{
29-
private readonly List<Func<Metadata, VariableNameInspector>> _switchIdInspectorConstructorList;
30-
3128
public PulseSwitchUnitWidget(FlowCanvas canvas, PulseSwitchUnit unit) : base(canvas, unit)
3229
{
33-
_switchIdInspectorConstructorList = new List<Func<Metadata, VariableNameInspector>>();
3430
}
3531

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-
}
32+
protected override IEnumerable<string> IdSuggestions(IGamelogicEngine gle) => gle.RequestedSwitches.Select(sw => sw.Id);
6233
}
6334
}

Editor/Widgets/SetCoilUnitWidget.cs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,19 @@
1616

1717
// ReSharper disable UnusedType.Global
1818

19-
using System;
2019
using System.Collections.Generic;
2120
using System.Linq;
2221
using Unity.VisualScripting;
2322

2423
namespace VisualPinball.Unity.VisualScripting.Editor
2524
{
2625
[Widget(typeof(SetCoilUnit))]
27-
public sealed class SetCoilUnitWidget : GleUnitWidget<SetCoilUnit>
26+
public sealed class SetCoilUnitWidget : GleMultiUnitWidget<SetCoilUnit>
2827
{
29-
private readonly List<Func<Metadata, VariableNameInspector>> _coilIdInspectorConstructorList;
30-
3128
public SetCoilUnitWidget(FlowCanvas canvas, SetCoilUnit unit) : base(canvas, unit)
3229
{
33-
_coilIdInspectorConstructorList = new List<Func<Metadata, VariableNameInspector>>();
3430
}
3531

36-
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
37-
{
38-
if (_coilIdInspectorConstructorList.Count() < unit.inputCount) {
39-
for (var index = 0; index < unit.inputCount - _coilIdInspectorConstructorList.Count(); index++) {
40-
_coilIdInspectorConstructorList.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 coilIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
47-
InspectorProvider.instance.Renew(ref coilIdInspector, meta, _coilIdInspectorConstructorList[index]);
48-
49-
return coilIdInspector;
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.RequestedCoils.Select(coil => coil.Id).ToList();
61-
}
32+
protected override IEnumerable<string> IdSuggestions(IGamelogicEngine gle) => gle.RequestedCoils.Select(coil => coil.Id);
6233
}
6334
}

Editor/Widgets/SetLampColorUnitWidget.cs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,52 +16,20 @@
1616

1717
// ReSharper disable UnusedType.Global
1818

19-
using System;
2019
using System.Collections.Generic;
2120
using System.Linq;
2221
using Unity.VisualScripting;
2322

2423
namespace VisualPinball.Unity.VisualScripting.Editor
2524
{
2625
[Widget(typeof(SetLampColorUnit))]
27-
public sealed class SetLampColorUnitWidget : GleUnitWidget<SetLampColorUnit>
26+
public sealed class SetLampColorUnitWidget : GleMultiUnitWidget<SetLampColorUnit>
2827
{
29-
private readonly List<Func<Metadata, VariableNameInspector>> _lampIdInspectorConstructorList;
30-
3128
public SetLampColorUnitWidget(FlowCanvas canvas, SetLampColorUnit unit) : base(canvas, unit)
3229
{
33-
_lampIdInspectorConstructorList = new List<Func<Metadata, VariableNameInspector>>();
3430
}
3531

36-
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
37-
{
38-
if (_lampIdInspectorConstructorList.Count() < unit.inputCount)
39-
{
40-
for (var index = 0; index < unit.inputCount - _lampIdInspectorConstructorList.Count(); index++)
41-
{
42-
_lampIdInspectorConstructorList.Add(meta => new VariableNameInspector(meta, GetNameSuggestions));
43-
}
44-
}
45-
46-
for (var index = 0; index < unit.inputCount; index++)
47-
{
48-
if (unit.multiInputs[index] == port)
49-
{
50-
VariableNameInspector lampIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
51-
InspectorProvider.instance.Renew(ref lampIdInspector, meta, _lampIdInspectorConstructorList[index]);
52-
53-
return lampIdInspector;
54-
}
55-
}
32+
protected override IEnumerable<string> IdSuggestions(IGamelogicEngine gle) => gle.RequestedLamps.Select(lamp => lamp.Id);
5633

57-
return base.GetPortInspector(port, meta);
58-
}
59-
60-
private IEnumerable<string> GetNameSuggestions()
61-
{
62-
return !GleAvailable
63-
? new List<string>()
64-
: Gle.RequestedLamps.Select(lamp => lamp.Id).ToList();
65-
}
6634
}
6735
}

Editor/Widgets/SetLampEnabledUnitWidget.cs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,19 @@
1616

1717
// ReSharper disable UnusedType.Global
1818

19-
using System;
2019
using System.Collections.Generic;
2120
using System.Linq;
2221
using Unity.VisualScripting;
2322

2423
namespace VisualPinball.Unity.VisualScripting.Editor
2524
{
2625
[Widget(typeof(SetLampEnabledUnit))]
27-
public sealed class SetLampEnabledUnitWidget : GleUnitWidget<SetLampEnabledUnit>
26+
public sealed class SetLampEnabledUnitWidget : GleMultiUnitWidget<SetLampEnabledUnit>
2827
{
29-
private readonly List<Func<Metadata, VariableNameInspector>> _lampIdInspectorConstructorList;
30-
3128
public SetLampEnabledUnitWidget(FlowCanvas canvas, SetLampEnabledUnit unit) : base(canvas, unit)
3229
{
33-
_lampIdInspectorConstructorList = new List<Func<Metadata, VariableNameInspector>>();
3430
}
3531

36-
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
37-
{
38-
if (_lampIdInspectorConstructorList.Count() < unit.inputCount) {
39-
for (var index = 0; index < unit.inputCount - _lampIdInspectorConstructorList.Count(); index++) {
40-
_lampIdInspectorConstructorList.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 lampIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
47-
InspectorProvider.instance.Renew(ref lampIdInspector, meta, _lampIdInspectorConstructorList[index]);
48-
49-
return lampIdInspector;
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.RequestedLamps.Select(lamp => lamp.Id).ToList();
61-
}
32+
protected override IEnumerable<string> IdSuggestions(IGamelogicEngine gle) => gle.RequestedLamps.Select(lamp => lamp.Id);
6233
}
6334
}

0 commit comments

Comments
 (0)