Skip to content

Commit fe744f7

Browse files
committed
Refactor and document LampEventUnit.
1 parent 33d69e4 commit fe744f7

File tree

4 files changed

+91
-36
lines changed

4 files changed

+91
-36
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.Editor
20+
{
21+
[Descriptor(typeof(LampEventUnit))]
22+
public class LampEventUnitDescriptor : UnitDescriptor<LampEventUnit>
23+
{
24+
public LampEventUnitDescriptor(LampEventUnit target) : base(target)
25+
{
26+
}
27+
28+
protected override string DefinedSummary()
29+
{
30+
return "This node triggers an event when a lamp with a given ID changes its intensity.";
31+
}
32+
33+
protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.LampEvent);
34+
35+
protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
36+
{
37+
base.DefinedPort(port, desc);
38+
39+
switch (port.key) {
40+
case nameof(LampEventUnit.Id):
41+
desc.summary = "The ID of the lamp that changed its intensity.";
42+
break;
43+
case nameof(LampEventUnit.Value):
44+
desc.summary = "The new intensity of the lamp (0-255).";
45+
break;
46+
case nameof(LampEventUnit.IsEnabled):
47+
desc.summary = "Whether the intensity is larger than 0.";
48+
break;
49+
}
50+
}
51+
}
52+
}

Editor/Descriptors/LampEventUnitDescriptor.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/LampEventUnitWidget.cs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,43 @@
1616

1717
using System;
1818
using System.Collections.Generic;
19+
using System.Linq;
1920
using Unity.VisualScripting;
2021

2122
namespace VisualPinball.Unity.VisualScripting.Editor
2223
{
2324
[Widget(typeof(LampEventUnit))]
24-
public sealed class LampEventUnitWidget : UnitWidget<LampEventUnit>
25+
public sealed class LampEventUnitWidget : GleUnitWidget<LampEventUnit>
2526
{
2627
public LampEventUnitWidget(FlowCanvas canvas, LampEventUnit unit) : base(canvas, unit)
2728
{
28-
lampIdInspectorConstructor = (metadata) => new VariableNameInspector(metadata, GetNameSuggestions);
29+
_lampIdInspectorConstructor = meta => new VariableNameInspector(meta, GetNameSuggestions);
2930
}
3031

3132
protected override NodeColorMix baseColor => NodeColorMix.TealReadable;
3233

33-
private VariableNameInspector lampIdInspector;
34-
private Func<Metadata, VariableNameInspector> lampIdInspectorConstructor;
34+
private VariableNameInspector _lampIdInspector;
35+
private readonly Func<Metadata, VariableNameInspector> _lampIdInspectorConstructor;
3536

36-
public override Inspector GetPortInspector(IUnitPort port, Metadata metadata)
37+
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
3738
{
38-
if (port == unit.id) {
39-
InspectorProvider.instance.Renew(ref lampIdInspector, metadata, lampIdInspectorConstructor);
39+
if (port == unit.Id) {
40+
InspectorProvider.instance.Renew(ref _lampIdInspector, meta, _lampIdInspectorConstructor);
4041

41-
return lampIdInspector;
42+
return _lampIdInspector;
4243
}
4344

44-
return base.GetPortInspector(port, metadata);
45+
return base.GetPortInspector(port, meta);
4546
}
4647

4748
private IEnumerable<string> GetNameSuggestions()
4849
{
49-
var list = new List<string>();
50-
51-
var tableComponent = TableSelector.Instance.SelectedTable;
52-
53-
if (tableComponent != null) {
54-
var gle = tableComponent.gameObject.GetComponent<IGamelogicEngine>();
55-
56-
if (gle != null) {
57-
foreach (var lamp in gle.AvailableLamps) {
58-
list.Add(lamp.Id);
59-
}
60-
}
50+
if (!GameObjectAvailable) {
51+
return new List<string>();
6152
}
6253

63-
return list;
54+
var gle = Gle;
55+
return gle == null ? new List<string>() : gle.AvailableLamps.Select(lamp => lamp.Id).ToList();
6456
}
6557
}
6658
}

Runtime/Nodes/Lamps/LampEventUnit.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@
1818

1919
namespace VisualPinball.Unity.VisualScripting
2020
{
21-
[UnitTitle("On Lamp Event")]
21+
[UnitTitle("On Lamp Changed")]
2222
[UnitCategory("Events\\Visual Pinball")]
23-
public sealed class LampEventUnit : EventUnit<LampEventArgs>
23+
public sealed class LampEventUnit : GleEventUnit<LampEventArgs>
2424
{
2525
[DoNotSerialize]
26-
[PortLabelHidden]
27-
public ValueInput id { get; private set; }
26+
[PortLabel("Lamp ID")]
27+
public ValueInput Id { get; private set; }
2828

2929
[DoNotSerialize]
30-
[PortLabelHidden]
31-
public ValueOutput value { get; private set; }
30+
[PortLabel("Intensity")]
31+
public ValueOutput Value { get; private set; }
3232

3333
[DoNotSerialize]
34-
[PortLabelHidden]
35-
public ValueOutput enabled { get; private set; }
34+
[PortLabel("Is Enabled")]
35+
public ValueOutput IsEnabled { get; private set; }
3636

3737
protected override bool register => true;
3838

@@ -45,21 +45,21 @@ protected override void Definition()
4545
{
4646
base.Definition();
4747

48-
id = ValueInput(nameof(id), string.Empty);
48+
Id = ValueInput(nameof(Id), string.Empty);
4949

50-
value = ValueOutput<int>(nameof(value));
51-
enabled = ValueOutput<bool>(nameof(enabled));
50+
Value = ValueOutput<int>(nameof(Value));
51+
IsEnabled = ValueOutput<bool>(nameof(IsEnabled));
5252
}
5353

5454
protected override void AssignArguments(Flow flow, LampEventArgs args)
5555
{
56-
flow.SetValue(value, args.Value);
57-
flow.SetValue(enabled, args.Value > 0);
56+
flow.SetValue(Value, args.Value);
57+
flow.SetValue(IsEnabled, args.Value > 0);
5858
}
5959

6060
protected override bool ShouldTrigger(Flow flow, LampEventArgs args)
6161
{
62-
return args.Id == flow.GetValue<string>(id);
62+
return args.Id == flow.GetValue<string>(Id);
6363
}
6464
}
6565
}

0 commit comments

Comments
 (0)