Skip to content

Commit 643f86c

Browse files
jsm174freezy
authored andcommitted
misc: refactor SetLightSequenceUnit to LampSequenceUnit
1 parent 1d55635 commit 643f86c

9 files changed

+270
-205
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
21+
namespace VisualPinball.Unity.VisualScripting.Editor
22+
{
23+
[Descriptor(typeof(LampSequenceUnit))]
24+
public class LampSequenceUnitDescriptor : UnitDescriptor<LampSequenceUnit>
25+
{
26+
public LampSequenceUnitDescriptor(LampSequenceUnit target) : base(target)
27+
{
28+
}
29+
30+
protected override string DefinedSummary()
31+
{
32+
return "This node turns on/off lamps in a sequence defined by lamp ids.";
33+
}
34+
35+
protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.LampSequence);
36+
37+
protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
38+
{
39+
base.DefinedPort(port, desc);
40+
41+
if (port.key == nameof(LampSequenceUnit.Step)) {
42+
desc.summary = "How position is increased and to how many lights the intensity is applied to.";
43+
}
44+
else if (port.key == nameof(LampSequenceUnit.Value)) {
45+
desc.summary = "The intensity to apply to the current step (0-1).";
46+
}
47+
else if (int.TryParse(port.key, out int id)) {
48+
id += 1;
49+
50+
desc.label = $"Lamp ID {id}";
51+
desc.summary = $"Lamp ID {id} to cycle through.";
52+
}
53+
}
54+
}
55+
}

Runtime/Nodes/Lamps/SetLightSequenceUnit.cs.meta renamed to Editor/Descriptors/LampSequenceUnitDescriptor.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/Descriptors/SetLightSequenceUnitDescriptor.cs

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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(LampSequenceUnit))]
27+
public sealed class LampSequenceUnitWidget : GleUnitWidget<LampSequenceUnit>
28+
{
29+
private readonly List<Func<Metadata, VariableNameInspector>> _lampIdInspectorConstructorList;
30+
31+
public LampSequenceUnitWidget(FlowCanvas canvas, LampSequenceUnit 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+
for (var index = 0; index < unit.inputCount - _lampIdInspectorConstructorList.Count(); index++) {
40+
_lampIdInspectorConstructorList.Add(meta => new VariableNameInspector(meta, GetNameSuggestions));
41+
}
42+
}
43+
44+
for (var index = 0; index < unit.inputCount; index++) {
45+
if (unit.multiInputs[index] == port) {
46+
VariableNameInspector lampIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
47+
InspectorProvider.instance.Renew(ref lampIdInspector, meta, _lampIdInspectorConstructorList[index]);
48+
49+
return lampIdInspector;
50+
}
51+
}
52+
53+
return base.GetPortInspector(port, meta);
54+
}
55+
56+
private IEnumerable<string> GetNameSuggestions()
57+
{
58+
return !GleAvailable
59+
? new List<string>()
60+
: Gle.RequestedLamps.Select(lamp => lamp.Id).ToList();
61+
}
62+
}
63+
}

Editor/Descriptors/SetLightSequenceUnitDescriptor.cs.meta renamed to Editor/Widgets/LampSequenceUnitWidget.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/SetLightSequenceUnitWidget.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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 System.Collections.Generic;
18+
using System.Collections.ObjectModel;
19+
using Unity.VisualScripting;
20+
using UnityEngine;
21+
using VisualPinball.Engine.Math;
22+
23+
namespace VisualPinball.Unity.VisualScripting
24+
{
25+
[UnitTitle("Lamp Sequence")]
26+
[UnitSurtitle("Scene")]
27+
[UnitCategory("Visual Pinball")]
28+
public class LampSequenceUnit : GleUnit, IMultiInputUnit
29+
{
30+
[DoNotSerialize]
31+
[PortLabelHidden]
32+
public ControlInput InputTrigger;
33+
34+
[DoNotSerialize]
35+
[PortLabelHidden]
36+
public ControlOutput OutputTrigger;
37+
38+
[SerializeAs(nameof(inputCount))]
39+
private int _inputCount = 1;
40+
41+
[DoNotSerialize]
42+
[Inspectable, UnitHeaderInspectable("Lamp IDs")]
43+
public int inputCount
44+
{
45+
get => _inputCount;
46+
set => _inputCount = Mathf.Clamp(value, 1, 10);
47+
}
48+
49+
[DoNotSerialize]
50+
public ReadOnlyCollection<ValueInput> multiInputs { get; private set; }
51+
52+
[DoNotSerialize]
53+
[PortLabel("Value")]
54+
public ValueInput Value { get; private set; }
55+
56+
[DoNotSerialize]
57+
[PortLabel("Step")]
58+
public ValueInput Step;
59+
60+
private int _currentIndex;
61+
private List<LightComponent> _lightComponentCache = null;
62+
63+
protected override void Definition()
64+
{
65+
InputTrigger = ControlInput(nameof(InputTrigger), Process);
66+
OutputTrigger = ControlOutput(nameof(OutputTrigger));
67+
68+
var _multiInputs = new List<ValueInput>();
69+
70+
multiInputs = _multiInputs.AsReadOnly();
71+
72+
for (var i = 0; i < inputCount; i++) {
73+
var input = ValueInput(i.ToString(), string.Empty);
74+
_multiInputs.Add(input);
75+
76+
Requirement(input, InputTrigger);
77+
}
78+
79+
Value = ValueInput(nameof(Value), 0f);
80+
Step = ValueInput(nameof(Step), 1);
81+
82+
Succession(InputTrigger, OutputTrigger);
83+
84+
_lightComponentCache = null;
85+
}
86+
87+
private ControlOutput Process(Flow flow)
88+
{
89+
if (!AssertGle(flow))
90+
{
91+
Debug.LogError("Cannot find GLE.");
92+
return OutputTrigger;
93+
}
94+
95+
if (!AssertPlayer(flow)) {
96+
Debug.LogError("Cannot find player.");
97+
return OutputTrigger;
98+
}
99+
100+
var value = flow.GetValue<float>(Value);
101+
var stepRaw = flow.GetValue<int>(Step);
102+
103+
if (_lightComponentCache != null) {
104+
foreach (var component in _lightComponentCache) {
105+
if (component.IsUnityNull()) {
106+
_lightComponentCache = null;
107+
break;
108+
}
109+
}
110+
}
111+
112+
if (_lightComponentCache == null) {
113+
_lightComponentCache = new List<LightComponent>();
114+
115+
foreach (var input in multiInputs) {
116+
var lampId = flow.GetValue<string>(input);
117+
_lightComponentCache.AddRange(Flatten(Player.LampDevice(lampId)));
118+
}
119+
}
120+
121+
for (var index = 0; index < _lightComponentCache.Count; index++) {
122+
Player.Lamp(_lightComponentCache[index]).OnLamp(
123+
index >= _currentIndex * stepRaw && index < (_currentIndex + 1) * stepRaw ? value : 0, ColorChannel.Alpha); ;
124+
}
125+
126+
if (++_currentIndex >= _lightComponentCache.Count / stepRaw) {
127+
_currentIndex = 0;
128+
}
129+
130+
return OutputTrigger;
131+
}
132+
133+
private List<LightComponent> Flatten(List<ILampDeviceComponent> lampDeviceList)
134+
{
135+
List<LightComponent> lights = new List<LightComponent>();
136+
137+
foreach (ILampDeviceComponent device in lampDeviceList) {
138+
if (device is LightComponent) {
139+
lights.Add(device as LightComponent);
140+
}
141+
else if (device is LightGroupComponent) {
142+
lights.AddRange(Flatten(((LightGroupComponent)device).Lights));
143+
}
144+
}
145+
146+
return lights;
147+
}
148+
}
149+
}

Editor/Widgets/SetLightSequenceUnitWidget.cs.meta renamed to Runtime/Nodes/Lamps/LampSequenceUnit.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)