Skip to content

Commit 1172f2c

Browse files
committed
Refactor and document GetLampValueUnit.
1 parent fe744f7 commit 1172f2c

File tree

11 files changed

+103
-40
lines changed

11 files changed

+103
-40
lines changed
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 Unity.VisualScripting;
18+
using VisualPinball.Unity.Editor;
19+
using IconSize = VisualPinball.Unity.Editor.IconSize;
20+
21+
namespace VisualPinball.Unity.VisualScripting.Editor
22+
{
23+
[Descriptor(typeof(GetLampValueUnit))]
24+
public class GetLampValueUnitDescriptor : UnitDescriptor<GetLampValueUnit>
25+
{
26+
public GetLampValueUnitDescriptor(GetLampValueUnit target) : base(target)
27+
{
28+
}
29+
30+
protected override string DefinedSummary()
31+
{
32+
return "This node retrieves the current intensity of the lamp, as well as if the lamp is enabled or not.";
33+
}
34+
35+
protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.Light(IconSize.Large, IconColor.Orange));
36+
37+
protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
38+
{
39+
base.DefinedPort(port, desc);
40+
41+
switch (port.key) {
42+
case nameof(GetLampValueUnit.Id):
43+
desc.summary = "The ID of the lamp for which the intensity is returned.";
44+
break;
45+
case nameof(GetLampValueUnit.Value):
46+
desc.summary = "The intensity of the lamp.";
47+
break;
48+
case nameof(GetLampValueUnit.IsEnabled):
49+
desc.summary = "Whether the intensity is larger than 0.";
50+
break;
51+
}
52+
}
53+
}
54+
}

Editor/Descriptors/GetLampValueUnitDescriptor.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/GetLampValueUnitWidget.cs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,54 +14,47 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
// ReSharper disable UnusedType.Global
18+
1719
using System;
1820
using System.Collections.Generic;
21+
using System.Linq;
1922
using Unity.VisualScripting;
2023
using VisualPinball.Unity;
2124

2225
namespace VisualPinball.Unity.VisualScripting.Editor
2326
{
2427
[Widget(typeof(GetLampValueUnit))]
25-
public sealed class GetLampValueUnitWidget : UnitWidget<GetLampValueUnit>
28+
public sealed class GetLampValueUnitWidget : GleUnitWidget<GetLampValueUnit>
2629
{
2730
public GetLampValueUnitWidget(FlowCanvas canvas, GetLampValueUnit unit) : base(canvas, unit)
2831
{
29-
lampIdInspectorConstructor = (metadata) => new VariableNameInspector(metadata, GetNameSuggestions);
32+
_lampIdInspectorConstructor = meta => new VariableNameInspector(meta, GetNameSuggestions);
3033
}
3134

3235
protected override NodeColorMix baseColor => NodeColorMix.TealReadable;
3336

34-
private VariableNameInspector lampIdInspector;
35-
private Func<Metadata, VariableNameInspector> lampIdInspectorConstructor;
37+
private VariableNameInspector _lampIdInspector;
38+
private readonly Func<Metadata, VariableNameInspector> _lampIdInspectorConstructor;
3639

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

42-
return lampIdInspector;
45+
return _lampIdInspector;
4346
}
4447

45-
return base.GetPortInspector(port, metadata);
48+
return base.GetPortInspector(port, meta);
4649
}
4750

4851
private IEnumerable<string> GetNameSuggestions()
4952
{
50-
var list = new List<string>();
51-
52-
var tableComponent = TableSelector.Instance.SelectedTable;
53-
54-
if (tableComponent != null) {
55-
var gle = tableComponent.gameObject.GetComponent<IGamelogicEngine>();
56-
57-
if (gle != null) {
58-
foreach (var lamp in gle.AvailableLamps) {
59-
list.Add(lamp.Id);
60-
}
61-
}
53+
if (!GameObjectAvailable) {
54+
return new List<string>();
6255
}
63-
64-
return list;
56+
var gle = Gle;
57+
return gle == null ? new List<string>() : gle.AvailableLamps.Select(lamp => lamp.Id).ToList();
6558
}
6659
}
6760
}

Editor/Widgets/GleUnitWidget.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public abstract class GleUnitWidget<TUnit> : UnitWidget<TUnit> where TUnit : Uni
2222
{
2323
protected override NodeColorMix baseColor => GleAvailable ? NodeColorMix.TealReadable : new NodeColorMix { red = 1f, green = 0f, blue = 0f };
2424
protected bool GameObjectAvailable => reference != null && reference.gameObject != null;
25-
protected bool GleAvailable => GameObjectAvailable && Gle != null;
2625
protected IGamelogicEngine Gle => reference.gameObject.GetComponentInParent<IGamelogicEngine>();
26+
private bool GleAvailable => GameObjectAvailable && Gle != null;
2727

2828
protected GleUnitWidget(FlowCanvas canvas, TUnit unit) : base(canvas, unit)
2929
{

Editor/Widgets/LampEventUnitWidget.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
// ReSharper disable UnusedType.Global
18+
1719
using System;
1820
using System.Collections.Generic;
1921
using System.Linq;

Editor/Widgets/SetCoilUnitWidget.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
// ReSharper disable UnusedType.Global
18+
1719
using System;
1820
using System.Collections.Generic;
1921
using System.Linq;

Editor/Widgets/SetLampUnitWidget.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
// ReSharper disable UnusedType.Global
18+
1719
using System;
1820
using System.Collections.Generic;
1921
using System.Linq;

Editor/Widgets/SwitchEventUnitWidget.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
// ReSharper disable UnusedType.Global
18+
1719
using System;
1820
using System.Collections.Generic;
1921
using System.Linq;

Runtime/Nodes/Coils/SetCoilUnit.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,5 @@ private ControlOutput Process(Flow flow)
6767

6868
return OutputTrigger;
6969
}
70-
71-
7270
}
7371
}

Runtime/Nodes/Lamps/GetLampValueUnit.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,28 @@ namespace VisualPinball.Unity.VisualScripting
2020
{
2121
[UnitTitle("Get Lamp Value")]
2222
[UnitCategory("Visual Pinball")]
23-
public class GetLampValueUnit : Unit
23+
public class GetLampValueUnit : GleUnit
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("Value")]
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
private Player _player;
3838

3939
protected override void Definition()
4040
{
41-
id = ValueInput(nameof(id), string.Empty);
41+
Id = ValueInput(nameof(Id), string.Empty);
4242

43-
value = ValueOutput(nameof(value), GetValue);
44-
enabled = ValueOutput(nameof(enabled), GetEnabled);
43+
Value = ValueOutput(nameof(Value), GetValue);
44+
IsEnabled = ValueOutput(nameof(IsEnabled), GetEnabled);
4545
}
4646

4747
private float GetValue(Flow flow)
@@ -50,7 +50,7 @@ private float GetValue(Flow flow)
5050
_player = UnityEngine.Object.FindObjectOfType<Player>();
5151
}
5252

53-
var key = flow.GetValue<string>(id);
53+
var key = flow.GetValue<string>(Id);
5454
return _player.LampStatuses.ContainsKey(key) ? _player.LampStatuses[key] : 0;
5555
}
5656

@@ -60,7 +60,7 @@ private bool GetEnabled(Flow flow)
6060
_player = flow.stack.self.GetComponentInParent<Player>();
6161
}
6262

63-
var key = flow.GetValue<string>(id);
63+
var key = flow.GetValue<string>(Id);
6464
return _player.LampStatuses.ContainsKey(key) && (_player.LampStatuses[key] > 0);
6565
}
6666
}

0 commit comments

Comments
 (0)