Skip to content

Commit 002a561

Browse files
committed
Add node for toggling lights.
1 parent da2db35 commit 002a561

File tree

7 files changed

+214
-1
lines changed

7 files changed

+214
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
using VisualPinball.Unity.Editor;
21+
using IconSize = VisualPinball.Unity.Editor.IconSize;
22+
23+
namespace VisualPinball.Unity.VisualScripting.Editor
24+
{
25+
[Descriptor(typeof(SetLampEnabledUnit))]
26+
public class SetLampEnabledUnitDescriptor : UnitDescriptor<SetLampEnabledUnit>
27+
{
28+
public SetLampEnabledUnitDescriptor(SetLampEnabledUnit target) : base(target)
29+
{
30+
}
31+
32+
protected override string DefinedSummary()
33+
{
34+
return "This node turns a lamp defined by its mapped ID on or off.";
35+
}
36+
37+
protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.Light(IconSize.Large, IconColor.Orange));
38+
39+
protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
40+
{
41+
base.DefinedPort(port, desc);
42+
43+
switch (port.key) {
44+
case nameof(SetLampEnabledUnit.Id):
45+
desc.summary = "The ID of the lamp to toggle.";
46+
break;
47+
case nameof(SetLampEnabledUnit.IsEnabled):
48+
desc.summary = "Whether to turn on or off.";
49+
break;
50+
}
51+
}
52+
}
53+
}

Editor/Descriptors/SetLampEnabledUnitDescriptor.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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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(SetLampEnabledUnit))]
27+
public sealed class SetLampEnabledUnitWidget : GleUnitWidget<SetLampEnabledUnit>
28+
{
29+
private VariableNameInspector _lampIdInspector;
30+
private readonly Func<Metadata, VariableNameInspector> _setLampInspectorConstructor;
31+
32+
public SetLampEnabledUnitWidget(FlowCanvas canvas, SetLampEnabledUnit unit) : base(canvas, unit)
33+
{
34+
_setLampInspectorConstructor = 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 _lampIdInspector, meta, _setLampInspectorConstructor);
41+
return _lampIdInspector;
42+
}
43+
44+
return base.GetPortInspector(port, meta);
45+
}
46+
47+
private IEnumerable<string> GetNameSuggestions()
48+
{
49+
if (!GameObjectAvailable) {
50+
return new List<string>();
51+
}
52+
var gle = Gle;
53+
return gle == null ? new List<string>() : gle.AvailableLamps.Select(lamp => lamp.Id).ToList();
54+
}
55+
}
56+
}

Editor/Widgets/SetLampEnabledUnitWidget.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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
using UnityEngine;
19+
20+
namespace VisualPinball.Unity.VisualScripting
21+
{
22+
[UnitShortTitle("Set Lamp")]
23+
[UnitTitle("Set Lamp (ID, on/off)")]
24+
[UnitSurtitle("Gamelogic Engine")]
25+
[UnitCategory("Visual Pinball")]
26+
public class SetLampEnabledUnit : GleUnit
27+
{
28+
[DoNotSerialize]
29+
[PortLabelHidden]
30+
public ControlInput InputTrigger;
31+
32+
[DoNotSerialize]
33+
[PortLabelHidden]
34+
public ControlOutput OutputTrigger;
35+
36+
[DoNotSerialize]
37+
[PortLabel("Lamp ID")]
38+
public ValueInput Id { get; private set; }
39+
40+
[DoNotSerialize]
41+
[PortLabel("Is Enabled")]
42+
public ValueInput IsEnabled { get; private set; }
43+
44+
protected override void Definition()
45+
{
46+
InputTrigger = ControlInput(nameof(InputTrigger), Process);
47+
OutputTrigger = ControlOutput(nameof(OutputTrigger));
48+
49+
Id = ValueInput(nameof(Id), string.Empty);
50+
IsEnabled = ValueInput(nameof(IsEnabled), false);
51+
52+
Requirement(Id, InputTrigger);
53+
Succession(InputTrigger, OutputTrigger);
54+
}
55+
56+
private ControlOutput Process(Flow flow)
57+
{
58+
if (!AssertGle(flow)) {
59+
Debug.LogError("Cannot find GLE.");
60+
return OutputTrigger;
61+
}
62+
63+
var id = flow.GetValue<string>(Id);
64+
var isEnabled = flow.GetValue<bool>(IsEnabled);
65+
66+
Gle.SetLamp(id, isEnabled ? 255f : 0f);
67+
68+
return OutputTrigger;
69+
}
70+
}
71+
}

Runtime/Nodes/Lamps/SetLampEnabledUnit.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/Nodes/Lamps/SetLampUnit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
namespace VisualPinball.Unity.VisualScripting
2121
{
2222
[UnitShortTitle("Set Lamp")]
23-
[UnitTitle("Set Lamp (ID, Value)")]
23+
[UnitTitle("Set Lamp (ID, Intensity)")]
2424
[UnitSurtitle("Gamelogic Engine")]
2525
[UnitCategory("Visual Pinball")]
2626
public class SetLampUnit : GleUnit

0 commit comments

Comments
 (0)