Skip to content

Commit 5b538ca

Browse files
committed
Add switch event unit that uses GLE.
1 parent 792e51c commit 5b538ca

15 files changed

+445
-136
lines changed

Editor/GleUnitAnalyzer.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections.Generic;
2+
using Unity.VisualScripting;
3+
using VisualPinball.Unity.VisualScripting;
4+
5+
namespace Editor
6+
{
7+
[Analyser(typeof(SwitchEventUnit))]
8+
public class GleUnitAnalyser : UnitAnalyser<SwitchEventUnit>
9+
{
10+
public GleUnitAnalyser(GraphReference reference, SwitchEventUnit target) : base(reference, target) { }
11+
12+
protected override IEnumerable<Warning> Warnings()
13+
{
14+
foreach (var baseWarning in base.Warnings())
15+
{
16+
yield return baseWarning;
17+
}
18+
19+
foreach (var warning in unit.Errors) {
20+
yield return Warning.Error(warning);
21+
}
22+
}
23+
}
24+
}

Editor/GleUnitAnalyzer.cs.meta

Lines changed: 3 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 & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,67 @@
1-
using System;
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;
218
using System.Collections.Generic;
319
using Unity.VisualScripting;
420
using VisualPinball.Unity;
521

622
namespace VisualPinball.Unity.VisualScripting
723
{
8-
[Widget(typeof(GetLampValueUnit))]
9-
public sealed class GetLampValueUnitWidget : UnitWidget<GetLampValueUnit>
10-
{
11-
public GetLampValueUnitWidget(FlowCanvas canvas, GetLampValueUnit unit) : base(canvas, unit)
12-
{
13-
lampIdInspectorConstructor = (metadata) => new VariableNameInspector(metadata, GetNameSuggestions);
14-
}
24+
[Widget(typeof(GetLampValueUnit))]
25+
public sealed class GetLampValueUnitWidget : UnitWidget<GetLampValueUnit>
26+
{
27+
public GetLampValueUnitWidget(FlowCanvas canvas, GetLampValueUnit unit) : base(canvas, unit)
28+
{
29+
lampIdInspectorConstructor = (metadata) => new VariableNameInspector(metadata, GetNameSuggestions);
30+
}
1531

16-
protected override NodeColorMix baseColor => NodeColorMix.TealReadable;
32+
protected override NodeColorMix baseColor => NodeColorMix.TealReadable;
1733

18-
private VariableNameInspector lampIdInspector;
19-
private Func<Metadata, VariableNameInspector> lampIdInspectorConstructor;
34+
private VariableNameInspector lampIdInspector;
35+
private Func<Metadata, VariableNameInspector> lampIdInspectorConstructor;
2036

21-
public override Inspector GetPortInspector(IUnitPort port, Metadata metadata)
22-
{
23-
if (port == unit.id) {
24-
InspectorProvider.instance.Renew(ref lampIdInspector, metadata, lampIdInspectorConstructor);
37+
public override Inspector GetPortInspector(IUnitPort port, Metadata metadata)
38+
{
39+
if (port == unit.id) {
40+
InspectorProvider.instance.Renew(ref lampIdInspector, metadata, lampIdInspectorConstructor);
2541

26-
return lampIdInspector;
27-
}
42+
return lampIdInspector;
43+
}
2844

29-
return base.GetPortInspector(port, metadata);
30-
}
45+
return base.GetPortInspector(port, metadata);
46+
}
3147

32-
private IEnumerable<string> GetNameSuggestions()
33-
{
34-
var list = new List<string>();
48+
private IEnumerable<string> GetNameSuggestions()
49+
{
50+
var list = new List<string>();
3551

36-
var tableComponent = TableSelector.Instance.SelectedTable;
52+
var tableComponent = TableSelector.Instance.SelectedTable;
3753

38-
if (tableComponent != null) {
39-
var gle = tableComponent.gameObject.GetComponent<IGamelogicEngine>();
54+
if (tableComponent != null) {
55+
var gle = tableComponent.gameObject.GetComponent<IGamelogicEngine>();
4056

41-
if (gle != null) {
42-
foreach (var lamp in gle.AvailableLamps) {
43-
list.Add(lamp.Id);
44-
}
45-
}
46-
}
57+
if (gle != null) {
58+
foreach (var lamp in gle.AvailableLamps) {
59+
list.Add(lamp.Id);
60+
}
61+
}
62+
}
4763

48-
return list;
49-
}
50-
}
64+
return list;
65+
}
66+
}
5167
}
Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,66 @@
1-
using System;
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;
218
using System.Collections.Generic;
319
using Unity.VisualScripting;
4-
using VisualPinball.Unity;
520

621
namespace VisualPinball.Unity.VisualScripting
722
{
8-
[Widget(typeof(LampEventUnit))]
9-
public sealed class LampEventUnitWidget : UnitWidget<LampEventUnit>
10-
{
11-
public LampEventUnitWidget(FlowCanvas canvas, LampEventUnit unit) : base(canvas, unit)
12-
{
13-
lampIdInspectorConstructor = (metadata) => new VariableNameInspector(metadata, GetNameSuggestions);
14-
}
23+
[Widget(typeof(LampEventUnit))]
24+
public sealed class LampEventUnitWidget : UnitWidget<LampEventUnit>
25+
{
26+
public LampEventUnitWidget(FlowCanvas canvas, LampEventUnit unit) : base(canvas, unit)
27+
{
28+
lampIdInspectorConstructor = (metadata) => new VariableNameInspector(metadata, GetNameSuggestions);
29+
}
1530

16-
protected override NodeColorMix baseColor => NodeColorMix.TealReadable;
31+
protected override NodeColorMix baseColor => NodeColorMix.TealReadable;
1732

18-
private VariableNameInspector lampIdInspector;
19-
private Func<Metadata, VariableNameInspector> lampIdInspectorConstructor;
33+
private VariableNameInspector lampIdInspector;
34+
private Func<Metadata, VariableNameInspector> lampIdInspectorConstructor;
2035

21-
public override Inspector GetPortInspector(IUnitPort port, Metadata metadata)
22-
{
23-
if (port == unit.id) {
24-
InspectorProvider.instance.Renew(ref lampIdInspector, metadata, lampIdInspectorConstructor);
36+
public override Inspector GetPortInspector(IUnitPort port, Metadata metadata)
37+
{
38+
if (port == unit.id) {
39+
InspectorProvider.instance.Renew(ref lampIdInspector, metadata, lampIdInspectorConstructor);
2540

26-
return lampIdInspector;
27-
}
41+
return lampIdInspector;
42+
}
2843

29-
return base.GetPortInspector(port, metadata);
30-
}
44+
return base.GetPortInspector(port, metadata);
45+
}
3146

32-
private IEnumerable<string> GetNameSuggestions()
33-
{
34-
var list = new List<string>();
47+
private IEnumerable<string> GetNameSuggestions()
48+
{
49+
var list = new List<string>();
3550

36-
var tableComponent = TableSelector.Instance.SelectedTable;
51+
var tableComponent = TableSelector.Instance.SelectedTable;
3752

38-
if (tableComponent != null) {
39-
var gle = tableComponent.gameObject.GetComponent<IGamelogicEngine>();
53+
if (tableComponent != null) {
54+
var gle = tableComponent.gameObject.GetComponent<IGamelogicEngine>();
4055

41-
if (gle != null) {
42-
foreach (var lamp in gle.AvailableLamps) {
43-
list.Add(lamp.Id);
44-
}
45-
}
46-
}
56+
if (gle != null) {
57+
foreach (var lamp in gle.AvailableLamps) {
58+
list.Add(lamp.Id);
59+
}
60+
}
61+
}
4762

48-
return list;
49-
}
50-
}
63+
return list;
64+
}
65+
}
5166
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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(SwitchEventUnit))]
25+
public sealed class SwitchEventUnitWidget : UnitWidget<SwitchEventUnit>
26+
{
27+
28+
protected override NodeColorMix baseColor => GleAvailable ? NodeColorMix.TealReadable : new NodeColorMix { red = 1f, green = 0f, blue = 0f };
29+
private bool GameObjectAvailable => reference != null && reference.gameObject != null;
30+
private bool GleAvailable => GameObjectAvailable && Gle != null;
31+
private IGamelogicEngine Gle => reference.gameObject.GetComponentInParent<IGamelogicEngine>();
32+
33+
private VariableNameInspector _lampIdInspector;
34+
private readonly Func<Metadata, VariableNameInspector> _switchIdInspectorConstructor;
35+
36+
public SwitchEventUnitWidget(FlowCanvas canvas, SwitchEventUnit unit) : base(canvas, unit)
37+
{
38+
_switchIdInspectorConstructor = meta => new VariableNameInspector(meta, GetNameSuggestions);
39+
40+
if (!GameObjectAvailable) {
41+
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.");
42+
43+
} else if (!GleAvailable) {
44+
unit.Errors.Add("No gamelogic engine found. One of the GameObject's parents must have a gamelogic engine component.");
45+
}
46+
}
47+
48+
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
49+
{
50+
if (port == unit.id) {
51+
InspectorProvider.instance.Renew(ref _lampIdInspector, meta, _switchIdInspectorConstructor);
52+
53+
return _lampIdInspector;
54+
}
55+
56+
return base.GetPortInspector(port, meta);
57+
}
58+
59+
private IEnumerable<string> GetNameSuggestions()
60+
{
61+
if (!GameObjectAvailable) {
62+
return new List<string>();
63+
}
64+
var gle = Gle;
65+
return gle == null ? new List<string>() : gle.AvailableSwitches.Select(lamp => lamp.Id).ToList();
66+
67+
}
68+
}
69+
}

Editor/Widgets/SwitchEventUnitWidget.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: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 Unity.VisualScripting;
20+
using UnityEngine;
21+
22+
namespace VisualPinball.Unity.VisualScripting
23+
{
24+
[UnitTitle("On Switch Changed")]
25+
[UnitCategory("Events\\Visual Pinball")]
26+
public class SwitchEventUnit : EventUnit<SwitchEventArgs>
27+
{
28+
[DoNotSerialize]
29+
[PortLabel("Switch ID")]
30+
public ValueInput id { get; private set; }
31+
32+
[DoNotSerialize]
33+
[PortLabel("Is Enabled")]
34+
public ValueOutput enabled { get; private set; }
35+
36+
public VisualScriptingGamelogicEngine gle { get; private set; }
37+
38+
[DoNotSerialize]
39+
public readonly List<string> Errors = new();
40+
41+
protected override bool register => true;
42+
43+
// Adding an EventHook with the name of the event to the list of visual scripting events.
44+
public override EventHook GetHook(GraphReference reference)
45+
{
46+
return new EventHook(EventNames.SwitchEvent);
47+
}
48+
49+
protected override void Definition()
50+
{
51+
base.Definition();
52+
53+
id = ValueInput(nameof(id), string.Empty);
54+
enabled = ValueOutput<bool>(nameof(enabled));
55+
}
56+
57+
public override void Instantiate(GraphReference instance)
58+
{
59+
base.Instantiate(instance);
60+
Debug.Log("Switch event instantiated.");
61+
}
62+
63+
protected override void AssignArguments(Flow flow, SwitchEventArgs args)
64+
{
65+
flow.SetValue(enabled, args.IsEnabled);
66+
}
67+
}
68+
}

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