Skip to content

Commit 1d55635

Browse files
jsm174freezy
authored andcommitted
units: refactored SetLampComponentUnit into SetLampColorUnit
1 parent 05cb45b commit 1d55635

File tree

7 files changed

+120
-86
lines changed

7 files changed

+120
-86
lines changed

Editor/Descriptors/SetLampComponentUnitDescriptor.cs renamed to Editor/Descriptors/SetLampColorUnitDescriptor.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222

2323
namespace VisualPinball.Unity.VisualScripting.Editor
2424
{
25-
[Descriptor(typeof(SetLampComponentUnit))]
26-
public class SetLampComponentUnitDescriptor : UnitDescriptor<SetLampComponentUnit>
25+
[Descriptor(typeof(SetLampColorUnit))]
26+
public class SetLampColorUnitDescriptor : UnitDescriptor<SetLampColorUnit>
2727
{
28-
public SetLampComponentUnitDescriptor(SetLampComponentUnit target) : base(target)
28+
public SetLampColorUnitDescriptor(SetLampColorUnit target) : base(target)
2929
{
3030
}
3131

3232
protected override string DefinedSummary()
3333
{
34-
return "This node assigns a given value to a light or light group in the scene. \n\nNote that this doesn't pass through the gamelogic engine, thus no event will be triggered. However, it allows you to drive non-mapped lights as well.";
34+
return "This node assigns a given value to a lamp defined by its mapped ID. This will also trigger the lamp changed event and update the internal status.";
3535
}
3636

3737
protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.Light(IconSize.Large, IconColor.Orange));
@@ -40,18 +40,16 @@ protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
4040
{
4141
base.DefinedPort(port, desc);
4242

43-
switch (port.key) {
44-
case nameof(SetLampComponentUnit.LampComponent):
45-
desc.summary = "The light component whose value you want to change. Assigning a light group will change all lights in the group.";
46-
break;
47-
48-
case nameof(SetLampComponentUnit.Value):
49-
desc.summary = "The intensity to apply (0-1).";
50-
break;
43+
if (port.key == nameof(SetLampColorUnit.Value))
44+
{
45+
desc.summary = "The color to set the lamp";
46+
}
47+
else if (int.TryParse(port.key, out int id))
48+
{
49+
id += 1;
5150

52-
case nameof(SetLampComponentUnit.ColorChannel):
53-
desc.summary = "Which color channel to use. For non-RGB lights, use alpha.";
54-
break;
51+
desc.label = $"Lamp ID {id}";
52+
desc.summary = $"Lamp ID {id} of the lamp to set the intensity";
5553
}
5654
}
5755
}

Runtime/Nodes/Lamps/SetLampComponentUnit.cs.meta renamed to Editor/Descriptors/SetLampColorUnitDescriptor.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.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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(SetLampColorUnit))]
27+
public sealed class SetLampColorUnitWidget : GleUnitWidget<SetLampColorUnit>
28+
{
29+
private readonly List<Func<Metadata, VariableNameInspector>> _lampIdInspectorConstructorList;
30+
31+
public SetLampColorUnitWidget(FlowCanvas canvas, SetLampColorUnit unit) : base(canvas, unit)
32+
{
33+
_lampIdInspectorConstructorList = new List<Func<Metadata, VariableNameInspector>>();
34+
}
35+
36+
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
37+
{
38+
if (_lampIdInspectorConstructorList.Count() < unit.inputCount)
39+
{
40+
for (var index = 0; index < unit.inputCount - _lampIdInspectorConstructorList.Count(); index++)
41+
{
42+
_lampIdInspectorConstructorList.Add(meta => new VariableNameInspector(meta, GetNameSuggestions));
43+
}
44+
}
45+
46+
for (var index = 0; index < unit.inputCount; index++)
47+
{
48+
if (unit.multiInputs[index] == port)
49+
{
50+
VariableNameInspector lampIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
51+
InspectorProvider.instance.Renew(ref lampIdInspector, meta, _lampIdInspectorConstructorList[index]);
52+
53+
return lampIdInspector;
54+
}
55+
}
56+
57+
return base.GetPortInspector(port, meta);
58+
}
59+
60+
private IEnumerable<string> GetNameSuggestions()
61+
{
62+
return !GleAvailable
63+
? new List<string>()
64+
: Gle.RequestedLamps.Select(lamp => lamp.Id).ToList();
65+
}
66+
}
67+
}

Editor/Descriptors/SetLampComponentUnitDescriptor.cs.meta renamed to Editor/Widgets/SetLampColorUnitWidget.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.

Editor/Widgets/SetLampComponentUnitWidget.cs

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

Runtime/Nodes/Lamps/SetLampComponentUnit.cs renamed to Runtime/Nodes/Lamps/SetLampColorUnit.cs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@
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+
using System.Collections.Generic;
18+
using System.Collections.ObjectModel;
1719
using Unity.VisualScripting;
1820
using UnityEngine;
19-
using VisualPinball.Engine.Math;
2021

2122
namespace VisualPinball.Unity.VisualScripting
2223
{
23-
[UnitTitle("Set Lamp (Component, Value, Channel)")]
2424
[UnitShortTitle("Set Lamp")]
25-
[UnitSurtitle("Scene")]
25+
[UnitTitle("Set Lamp (ID, Color)")]
26+
[UnitSurtitle("Gamelogic Engine")]
2627
[UnitCategory("Visual Pinball")]
27-
public class SetLampComponentUnit : GleUnit
28+
public class SetLampColorUnit : GleUnit, IMultiInputUnit
2829
{
2930
[DoNotSerialize]
3031
[PortLabelHidden]
@@ -34,49 +35,63 @@ public class SetLampComponentUnit : GleUnit
3435
[PortLabelHidden]
3536
public ControlOutput OutputTrigger;
3637

38+
[SerializeAs(nameof(inputCount))]
39+
private int _inputCount = 1;
40+
3741
[DoNotSerialize]
38-
[PortLabel("Component")]
39-
public ValueInput LampComponent;
42+
[Inspectable, UnitHeaderInspectable("Lamp IDs")]
43+
public int inputCount
44+
{
45+
get => _inputCount;
46+
set => _inputCount = Mathf.Clamp(value, 1, 10);
47+
}
4048

4149
[DoNotSerialize]
42-
[PortLabel("Intensity")]
43-
public ValueInput Value;
50+
public ReadOnlyCollection<ValueInput> multiInputs { get; private set; }
4451

4552
[DoNotSerialize]
46-
[PortLabel("Color Channel")]
47-
public ValueInput ColorChannel;
53+
[PortLabel("Value")]
54+
public ValueInput Value { get; private set; }
4855

4956
protected override void Definition()
5057
{
5158
InputTrigger = ControlInput(nameof(InputTrigger), Process);
5259
OutputTrigger = ControlOutput(nameof(OutputTrigger));
5360

54-
LampComponent = ValueInput<MonoBehaviour>(nameof(LampComponent), null);
61+
var _multiInputs = new List<ValueInput>();
5562

56-
Value = ValueInput(nameof(Value), 0f);
57-
ColorChannel = ValueInput(nameof(ColorChannel), Engine.Math.ColorChannel.Alpha);
63+
multiInputs = _multiInputs.AsReadOnly();
64+
65+
for (var i = 0; i < inputCount; i++)
66+
{
67+
var input = ValueInput(i.ToString(), string.Empty);
68+
_multiInputs.Add(input);
69+
70+
Requirement(input, InputTrigger);
71+
}
72+
73+
Value = ValueInput(nameof(Value), Color.black);
5874

59-
Requirement(LampComponent, InputTrigger);
6075
Succession(InputTrigger, OutputTrigger);
6176
}
6277

6378
private ControlOutput Process(Flow flow)
6479
{
65-
if (!AssertPlayer(flow)) {
66-
Debug.LogError("Cannot find player.");
80+
if (!AssertGle(flow))
81+
{
82+
Debug.LogError("Cannot find GLE.");
6783
return OutputTrigger;
6884
}
6985

70-
if (flow.GetValue<MonoBehaviour>(LampComponent) is ILampDeviceComponent lamp) {
71-
var valueRaw = flow.GetValue<float>(Value);
72-
var colorChannelRaw = flow.GetValue<ColorChannel>(ColorChannel);
86+
var value = flow.GetValue<Color>(Value);
7387

74-
Player.Lamp(lamp)?.OnLamp(valueRaw, colorChannelRaw);
88+
foreach (var input in multiInputs)
89+
{
90+
var lampId = flow.GetValue<string>(input);
91+
Gle.SetLamp(lampId, value);
7592
}
7693

7794
return OutputTrigger;
7895
}
79-
80-
8196
}
8297
}

Editor/Widgets/SetLampComponentUnitWidget.cs.meta renamed to Runtime/Nodes/Lamps/SetLampColorUnit.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.

0 commit comments

Comments
 (0)