Skip to content

Commit 8f614aa

Browse files
committed
eventbus: move event bus triggers to vpe project. Add GetLampValue
1 parent 9117bce commit 8f614aa

File tree

8 files changed

+138
-85
lines changed

8 files changed

+138
-85
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Unity.VisualScripting;
4+
using VisualPinball.Unity;
5+
6+
namespace VisualPinball.Unity.VisualScripting
7+
{
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+
}
15+
16+
protected override NodeColorMix baseColor => NodeColorMix.TealReadable;
17+
18+
private VariableNameInspector lampIdInspector;
19+
private Func<Metadata, VariableNameInspector> lampIdInspectorConstructor;
20+
21+
public override Inspector GetPortInspector(IUnitPort port, Metadata metadata)
22+
{
23+
if (port == unit.id)
24+
{
25+
// This feels so hacky. The real holy grail here would be to support attribute decorators like Unity does.
26+
InspectorProvider.instance.Renew(ref lampIdInspector, metadata, lampIdInspectorConstructor);
27+
28+
return lampIdInspector;
29+
}
30+
31+
return base.GetPortInspector(port, metadata);
32+
}
33+
34+
private IEnumerable<string> GetNameSuggestions()
35+
{
36+
List<string> list = new List<string>();
37+
38+
var tableComponent = TableSelector.Instance.SelectedTable;
39+
40+
if (tableComponent != null)
41+
{
42+
var gle = tableComponent.gameObject.GetComponent<IGamelogicEngine>();
43+
44+
if (gle != null)
45+
{
46+
foreach (var lamp in gle.AvailableLamps)
47+
{
48+
list.Add(lamp.Id);
49+
}
50+
}
51+
}
52+
53+
return list;
54+
}
55+
}
56+
}

Runtime/ScriptingEngine.cs.meta renamed to Editor/Widgets/GetLampValueUnitWidget.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Events/Nodes/LampEventUnit.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
namespace VisualPinball.Unity.VisualScripting
44
{
5-
public static class EventNames
6-
{
7-
public static string LampEvent = "LampEvent";
8-
}
9-
105
[UnitTitle("On Lamp Event")]
116
[UnitCategory("Events\\Visual Pinball")]
127
public sealed class LampEventUnit : EventUnit<LampEventArgs>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Unity.VisualScripting;
2+
3+
namespace VisualPinball.Unity.VisualScripting
4+
{
5+
[UnitTitle("Get Lamp Value")]
6+
[UnitCategory("Visual Pinball")]
7+
public class GetLampValueUnit : Unit
8+
{
9+
[DoNotSerialize]
10+
[PortLabelHidden]
11+
public ValueInput id { get; private set; }
12+
13+
[DoNotSerialize]
14+
[PortLabelHidden]
15+
public ValueOutput value { get; private set; }
16+
17+
[DoNotSerialize]
18+
[PortLabelHidden]
19+
public ValueOutput enabled { get; private set; }
20+
21+
private Player _player;
22+
23+
protected override void Definition()
24+
{
25+
id = ValueInput(nameof(id), string.Empty);
26+
27+
value = ValueOutput(nameof(value), GetValue);
28+
enabled = ValueOutput(nameof(enabled), GetEnabled);
29+
}
30+
31+
private float GetValue(Flow flow)
32+
{
33+
if (_player == null) {
34+
_player = UnityEngine.Object.FindObjectOfType<Player>();
35+
}
36+
37+
var key = flow.GetValue<string>(id);
38+
return _player.LampStatuses.ContainsKey(key) ? _player.LampStatuses[key] : 0;
39+
}
40+
41+
private bool GetEnabled(Flow flow)
42+
{
43+
if (_player == null) {
44+
_player = UnityEngine.Object.FindObjectOfType<Player>();
45+
}
46+
47+
var key = flow.GetValue<string>(id);
48+
return _player.LampStatuses.ContainsKey(key) ? (_player.LampStatuses[key] > 0) : false;
49+
}
50+
}
51+
}

Runtime/Lamps/Nodes/GetLampValueUnit.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/Lamps/Nodes/LampGroupSequenceUnit.cs

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,34 @@ public class LampGroupSequenceUnit : Unit
1515
public ValueInput lightGroup;
1616

1717
[DoNotSerialize]
18+
[PortLabelHidden]
1819
public ValueInput maxLights;
1920

2021
[DoNotSerialize]
2122
public ControlOutput outputTrigger;
2223

2324
private TableApi _tableApi;
24-
private int _currentIndex;
25+
private int _currentIndex = 0;
2526

2627
protected override void Definition()
2728
{
2829
inputTrigger = ControlInput("In", (flow) => Process(flow));
2930

3031
lightGroup = ValueInput<LightGroupComponent>(nameof(lightGroup));
31-
maxLights = ValueInput<int>(nameof(maxLights));
32+
maxLights = ValueInput<int>(nameof(maxLights), 1);
3233

3334
outputTrigger = ControlOutput("");
3435
}
3536

3637
private ControlOutput Process(Flow flow, bool invert = false)
3738
{
38-
39-
if (_tableApi == null)
40-
{
39+
if (_tableApi == null) {
4140
_tableApi = UnityEngine.Object.FindObjectOfType<Player>().TableApi;
4241
}
4342

4443
// GetValue throws an exception because they are not basic types
4544

46-
try
47-
{
45+
try {
4846
EnableLightGroup(flow.GetValue<LightGroupComponent>(lightGroup), flow.GetValue<int>(maxLights));
4947
}
5048
catch (Exception) { };
@@ -54,35 +52,14 @@ private ControlOutput Process(Flow flow, bool invert = false)
5452

5553
private void EnableLightGroup(LightGroupComponent lightGroupComponent, int maxLights)
5654
{
57-
int index = 0;
58-
int count = 0;
55+
var totalLights = lightGroupComponent.Lights.Count();
5956

60-
bool enabled = false;
61-
62-
var lights = lightGroupComponent.Lights.SelectMany(l => l.GetComponentsInChildren<LightComponent>());
63-
64-
foreach (var light in lights)
65-
{
66-
if (index == _currentIndex)
67-
{
68-
enabled = true;
69-
}
70-
71-
if (enabled && ++count > maxLights)
72-
{
73-
enabled = false;
74-
}
75-
76-
var lightState = _tableApi.Light(light);
77-
lightState.State = enabled ? 1 : 0;
78-
79-
index++;
57+
for (var index = 0; index < totalLights; index++ ) {
58+
_tableApi.Light(lightGroupComponent.Lights[index].GetComponentInChildren<LightComponent>()).State =
59+
(index >= (_currentIndex * maxLights) && index < ((_currentIndex + 1) * maxLights)) ? 1 : 0;
8060
}
8161

82-
_currentIndex += maxLights;
83-
84-
if (_currentIndex > lights.Count())
85-
{
62+
if (++_currentIndex >= totalLights / maxLights) {
8663
_currentIndex = 0;
8764
}
8865
}

Runtime/Lamps/Nodes/LampGroupUnit.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,39 +48,32 @@ protected override void Definition()
4848

4949
private ControlOutput Process(Flow flow, bool invert = false)
5050
{
51-
if (_tableApi == null)
52-
{
51+
if (_tableApi == null) {
5352
_tableApi = UnityEngine.Object.FindObjectOfType<Player>().TableApi;
5453
}
5554

5655
// GetValue throws an exception because they are not basic types
5756

58-
try
59-
{
60-
foreach (var lightGroupComponent in flow.GetValue<List<LightGroupComponent>>(offLightGroups))
61-
{
57+
try {
58+
foreach (var lightGroupComponent in flow.GetValue<List<LightGroupComponent>>(offLightGroups)) {
6259
EnableLightGroup(lightGroupComponent, invert);
6360
}
6461
}
6562
catch (Exception) { };
6663

67-
try
68-
{
69-
foreach (var lightGroupComponent in flow.GetValue<List<LightGroupComponent>>(onLightGroups))
70-
{
64+
try {
65+
foreach (var lightGroupComponent in flow.GetValue<List<LightGroupComponent>>(onLightGroups)) {
7166
EnableLightGroup(lightGroupComponent, !invert);
7267
}
7368
}
7469
catch (Exception) { };
7570

76-
try
77-
{
71+
try {
7872
EnableLightGroup(flow.GetValue<LightGroupComponent>(offLightGroup), invert);
7973
}
8074
catch (Exception) { };
8175

82-
try
83-
{
76+
try {
8477
EnableLightGroup(flow.GetValue<LightGroupComponent>(onLightGroup), !invert);
8578
}
8679
catch (Exception) { };
@@ -90,10 +83,8 @@ private ControlOutput Process(Flow flow, bool invert = false)
9083

9184
private void EnableLightGroup(LightGroupComponent lightGroupComponent, bool enable)
9285
{
93-
foreach (var light in lightGroupComponent.Lights.SelectMany(l => l.GetComponentsInChildren<LightComponent>()))
94-
{
95-
var lightState = _tableApi.Light(light);
96-
lightState.State = enable ? 1 : 0;
86+
foreach (var light in lightGroupComponent.Lights.SelectMany(l => l.GetComponentsInChildren<LightComponent>())) {
87+
_tableApi.Light(light).State = enable ? 1 : 0;
9788
}
9889
}
9990
}

Runtime/ScriptingEngine.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)