Skip to content

Commit 75030fa

Browse files
jsm174freezy
authored andcommitted
events: Added SwitchEnabled event
1 parent 51567f8 commit 75030fa

9 files changed

+206
-3
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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(SwitchEnabledEventUnit))]
24+
public class SwitchEnabledEventUnitDescriptor : UnitDescriptor<SwitchEnabledEventUnit>
25+
{
26+
public SwitchEnabledEventUnitDescriptor(SwitchEnabledEventUnit target) : base(target)
27+
{
28+
}
29+
30+
protected override string DefinedSummary()
31+
{
32+
return "This node triggers an event when a switch with a given ID is enabled.";
33+
}
34+
35+
protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.SwitchEvent);
36+
37+
protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
38+
{
39+
base.DefinedPort(port, desc);
40+
41+
switch (port.key) {
42+
case nameof(SwitchEnabledEventUnit.Id):
43+
desc.summary = "The ID of the switch that was enabled.";
44+
break;
45+
}
46+
}
47+
}
48+
}

Editor/Descriptors/SwitchEnabledEventUnitDescriptor.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 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(SwitchEnabledEventUnit))]
27+
public sealed class SwitchEnabledEventUnitWidget : GleUnitWidget<SwitchEnabledEventUnit>
28+
{
29+
private VariableNameInspector _switchIdInspector;
30+
private readonly Func<Metadata, VariableNameInspector> _switchIdInspectorConstructor;
31+
32+
public SwitchEnabledEventUnitWidget(FlowCanvas canvas, SwitchEnabledEventUnit unit) : base(canvas, unit)
33+
{
34+
_switchIdInspectorConstructor = meta => new VariableNameInspector(meta, GetNameSuggestions);
35+
}
36+
37+
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
38+
{
39+
if (port == unit.Id) {
40+
InspectorProvider.instance.Renew(ref _switchIdInspector, meta, _switchIdInspectorConstructor);
41+
42+
return _switchIdInspector;
43+
}
44+
45+
return base.GetPortInspector(port, meta);
46+
}
47+
48+
private IEnumerable<string> GetNameSuggestions()
49+
{
50+
if (!GameObjectAvailable) {
51+
return new List<string>();
52+
}
53+
var gle = Gle;
54+
return gle == null ? new List<string>() : gle.AvailableSwitches.Select(sw => sw.Id).ToList();
55+
}
56+
}
57+
}

Editor/Widgets/SwitchEnabledEventUnitWidget.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.

Runtime/Gamelogic/VisualScriptingEventNames.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static class VisualScriptingEventNames
2121
public const string GleStartedEvent = "GleStartedEvent";
2222
public const string LampEvent = "LampEvent";
2323
public const string SwitchEvent = "SwitchEvent";
24+
public const string SwitchEnabledEvent = "SwitchEnabledEvent";
2425
public const string CoilEvent = "CoilEvent";
2526
}
2627
}

Runtime/Gamelogic/VisualScriptingGamelogicBridge.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,13 @@ private void OnStarted(object sender, EventArgs e)
7676

7777
private static void OnSwitchChanged(object sender, SwitchEventArgs2 e)
7878
{
79-
EventBus.Trigger(VisualScriptingEventNames.SwitchEvent, new SwitchEventArgs2(e.Id, e.IsEnabled));
79+
var args = new SwitchEventArgs2(e.Id, e.IsEnabled);
80+
81+
EventBus.Trigger(VisualScriptingEventNames.SwitchEvent, args);
82+
83+
if (e.IsEnabled) {
84+
EventBus.Trigger(VisualScriptingEventNames.SwitchEnabledEvent, args);
85+
}
8086
}
8187

8288
private static void OnCoilChanged(object sender, CoilEventArgs e)

Runtime/Gamelogic/VisualScriptingGamelogicEngine.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,15 @@ public void OnInit(Player player, TableApi tableApi, BallManager ballManager)
6666

6767
public void Switch(string id, bool isClosed)
6868
{
69-
OnSwitchChanged?.Invoke(this, new SwitchEventArgs2(id, isClosed));
70-
EventBus.Trigger(VisualScriptingEventNames.SwitchEvent, new SwitchEventArgs2(id, isClosed));
69+
var args = new SwitchEventArgs2(id, isClosed);
70+
71+
OnSwitchChanged?.Invoke(this, args);
72+
73+
EventBus.Trigger(VisualScriptingEventNames.SwitchEvent, args);
74+
75+
if (isClosed) {
76+
EventBus.Trigger(VisualScriptingEventNames.SwitchEnabledEvent, args);
77+
}
7178
}
7279

7380
public void SetCoil(string id, bool isEnabled)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 Unity.VisualScripting;
18+
19+
namespace VisualPinball.Unity.VisualScripting
20+
{
21+
[UnitTitle("On Switch Enabled")]
22+
[UnitSurtitle("Gamelogic Engine")]
23+
[UnitCategory("Events\\Visual Pinball")]
24+
public class SwitchEnabledEventUnit : GleEventUnit<SwitchEventArgs2>
25+
{
26+
[DoNotSerialize]
27+
[PortLabel("Switch ID")]
28+
public ValueInput Id { get; private set; }
29+
30+
[DoNotSerialize]
31+
protected override bool register => true;
32+
33+
// Adding an EventHook with the name of the event to the list of visual scripting events.
34+
public override EventHook GetHook(GraphReference reference)
35+
{
36+
return new EventHook(VisualScriptingEventNames.SwitchEnabledEvent);
37+
}
38+
39+
protected override void Definition()
40+
{
41+
base.Definition();
42+
43+
Id = ValueInput(nameof(Id), string.Empty);
44+
}
45+
46+
protected override bool ShouldTrigger(Flow flow, SwitchEventArgs2 args)
47+
{
48+
return flow.GetValue<string>(Id) == args.Id;
49+
}
50+
}
51+
}

Runtime/Nodes/Switches/SwitchEnabledEventUnit.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)