Skip to content

Commit b9da183

Browse files
committed
Abstract GLE widget and add coil set unit.
1 parent 15698f2 commit b9da183

File tree

9 files changed

+193
-16
lines changed

9 files changed

+193
-16
lines changed

Editor/Widgets/GleUnitWidget.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Visual Pinball Engine
2+
// Copyright (C) 2021 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;
18+
using System.Collections.Generic;
19+
using System.Linq;
20+
using Unity.VisualScripting;
21+
22+
namespace VisualPinball.Unity.VisualScripting
23+
{
24+
public abstract class GleUnitWidget<TUnit> : UnitWidget<TUnit> where TUnit : Unit, IGleUnit
25+
{
26+
protected override NodeColorMix baseColor => GleAvailable ? NodeColorMix.TealReadable : new NodeColorMix { red = 1f, green = 0f, blue = 0f };
27+
protected bool GameObjectAvailable => reference != null && reference.gameObject != null;
28+
protected bool GleAvailable => GameObjectAvailable && Gle != null;
29+
protected IGamelogicEngine Gle => reference.gameObject.GetComponentInParent<IGamelogicEngine>();
30+
31+
protected GleUnitWidget(FlowCanvas canvas, TUnit unit) : base(canvas, unit)
32+
{
33+
if (!GameObjectAvailable) {
34+
unit.Errors.Add("Not attached to GameObject. You need to attach this graph to a flow machine sitting on a GameObject in order to use it.");
35+
36+
} else if (!GleAvailable) {
37+
unit.Errors.Add("No gamelogic engine found. One of the GameObject's parents must have a gamelogic engine component.");
38+
}
39+
}
40+
}
41+
}
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Visual Pinball Engine
2+
// Copyright (C) 2021 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;
18+
using System.Collections.Generic;
19+
using System.Linq;
20+
using Unity.VisualScripting;
21+
22+
namespace VisualPinball.Unity.VisualScripting
23+
{
24+
[Widget(typeof(SetCoilUnit))]
25+
public sealed class SetCoilUnitWidget : GleUnitWidget<SetCoilUnit>
26+
{
27+
private VariableNameInspector _coilIdInspector;
28+
private readonly Func<Metadata, VariableNameInspector> _setCoilInspectorConstructor;
29+
30+
public SetCoilUnitWidget(FlowCanvas canvas, SetCoilUnit unit) : base(canvas, unit)
31+
{
32+
_setCoilInspectorConstructor = meta => new VariableNameInspector(meta, GetNameSuggestions);
33+
}
34+
35+
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
36+
{
37+
if (port == unit.id) {
38+
InspectorProvider.instance.Renew(ref _coilIdInspector, meta, _setCoilInspectorConstructor);
39+
return _coilIdInspector;
40+
}
41+
42+
return base.GetPortInspector(port, meta);
43+
}
44+
45+
private IEnumerable<string> GetNameSuggestions()
46+
{
47+
if (!GameObjectAvailable) {
48+
return new List<string>();
49+
}
50+
var gle = Gle;
51+
return gle == null ? new List<string>() : gle.AvailableCoils.Select(coil => coil.Id).ToList();
52+
}
53+
}
54+
}

Editor/Widgets/SetCoilUnitWidget.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/SwitchEventUnitWidget.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,14 @@
2222
namespace VisualPinball.Unity.VisualScripting
2323
{
2424
[Widget(typeof(SwitchEventUnit))]
25-
public sealed class SwitchEventUnitWidget : UnitWidget<SwitchEventUnit>
25+
public sealed class SwitchEventUnitWidget : GleUnitWidget<SwitchEventUnit>
2626
{
27-
protected override NodeColorMix baseColor => GleAvailable ? NodeColorMix.TealReadable : new NodeColorMix { red = 1f, green = 0f, blue = 0f };
28-
private bool GameObjectAvailable => reference != null && reference.gameObject != null;
29-
private bool GleAvailable => GameObjectAvailable && Gle != null;
30-
private IGamelogicEngine Gle => reference.gameObject.GetComponentInParent<IGamelogicEngine>();
31-
3227
private VariableNameInspector _lampIdInspector;
3328
private readonly Func<Metadata, VariableNameInspector> _switchIdInspectorConstructor;
3429

3530
public SwitchEventUnitWidget(FlowCanvas canvas, SwitchEventUnit unit) : base(canvas, unit)
3631
{
3732
_switchIdInspectorConstructor = meta => new VariableNameInspector(meta, GetNameSuggestions);
38-
39-
if (!GameObjectAvailable) {
40-
unit.Errors.Add("Not attached to GameObject. You need to attach this graph to a flow machine sitting on a GameObject in order to use it.");
41-
42-
} else if (!GleAvailable) {
43-
unit.Errors.Add("No gamelogic engine found. One of the GameObject's parents must have a gamelogic engine component.");
44-
}
4533
}
4634

4735
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,20 @@
1919

2020
namespace VisualPinball.Unity.VisualScripting
2121
{
22-
public abstract class GleEventUnit<TArgs> : EventUnit<TArgs>
22+
public abstract class GleEventUnit<TArgs> : EventUnit<TArgs>, IGleUnit
2323
{
2424
[DoNotSerialize]
25-
public readonly List<string> Errors = new();
25+
public List<string> Errors { get; } = new();
26+
}
27+
28+
public abstract class GleUnit : Unit, IGleUnit
29+
{
30+
[DoNotSerialize]
31+
public List<string> Errors { get; } = new();
32+
}
33+
34+
public interface IGleUnit
35+
{
36+
List<string> Errors { get; }
2637
}
2738
}

Runtime/Events/Nodes/GleUnit.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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Visual Pinball Engine
2+
// Copyright (C) 2021 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+
19+
namespace VisualPinball.Unity.VisualScripting
20+
{
21+
[UnitTitle("Set Coil")]
22+
[UnitCategory("Visual Pinball")]
23+
public class SetCoilUnit : GleUnit
24+
{
25+
[DoNotSerialize]
26+
[PortLabelHidden]
27+
public ControlInput inputTrigger;
28+
29+
[DoNotSerialize]
30+
[PortLabelHidden]
31+
public ControlOutput outputTrigger;
32+
33+
[DoNotSerialize]
34+
[PortLabel("Coil ID")]
35+
public ValueInput id { get; private set; }
36+
37+
[DoNotSerialize]
38+
[PortLabel("Value")]
39+
public ValueInput enabled { get; private set; }
40+
41+
protected override void Definition()
42+
{
43+
inputTrigger = ControlInput(nameof(inputTrigger), _ => outputTrigger);
44+
outputTrigger = ControlOutput(nameof(outputTrigger));
45+
46+
id = ValueInput<string>(nameof(id), string.Empty);
47+
enabled = ValueInput<bool>(nameof(enabled), false);
48+
}
49+
}
50+
}

Runtime/Events/Nodes/SetCoilUnit.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)