Skip to content

Commit 51567f8

Browse files
jsm174freezy
authored andcommitted
units: add get switch unit
1 parent 235886d commit 51567f8

File tree

7 files changed

+198
-1
lines changed

7 files changed

+198
-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(GetSwitchUnit))]
26+
public class GetSwitchUnitDescriptor : UnitDescriptor<GetSwitchUnit>
27+
{
28+
public GetSwitchUnitDescriptor(GetSwitchUnit target) : base(target)
29+
{
30+
}
31+
32+
protected override string DefinedSummary()
33+
{
34+
return "This node retrieves the current status of the switch.";
35+
}
36+
37+
protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.Switch(false, 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(GetSwitchUnit.Id):
45+
desc.summary = "The ID of the switch for which to get current status.";
46+
break;
47+
case nameof(GetSwitchUnit.IsEnabled):
48+
desc.summary = "Whether the switch is enabled.";
49+
break;
50+
}
51+
}
52+
}
53+
}

Editor/Descriptors/GetSwitchUnitDescriptor.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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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(GetSwitchUnit))]
27+
public sealed class GetSwitchUnitWidget : GleUnitWidget<GetSwitchUnit>
28+
{
29+
public GetSwitchUnitWidget(FlowCanvas canvas, GetSwitchUnit unit) : base(canvas, unit)
30+
{
31+
_switchIdInspectorConstructor = meta => new VariableNameInspector(meta, GetNameSuggestions);
32+
}
33+
34+
protected override NodeColorMix baseColor => NodeColorMix.TealReadable;
35+
36+
private VariableNameInspector _switchIdInspector;
37+
private readonly Func<Metadata, VariableNameInspector> _switchIdInspectorConstructor;
38+
39+
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
40+
{
41+
if (port == unit.Id) {
42+
InspectorProvider.instance.Renew(ref _switchIdInspector, meta, _switchIdInspectorConstructor);
43+
44+
return _switchIdInspector;
45+
}
46+
47+
return base.GetPortInspector(port, meta);
48+
}
49+
50+
private IEnumerable<string> GetNameSuggestions()
51+
{
52+
if (!GameObjectAvailable) {
53+
return new List<string>();
54+
}
55+
var gle = Gle;
56+
return gle == null ? new List<string>() : gle.AvailableSwitches.Select(sw => sw.Id).ToList();
57+
}
58+
}
59+
}

Editor/Widgets/GetSwitchUnitWidget.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/SwitchEventUnitWidget.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private IEnumerable<string> GetNameSuggestions()
5151
return new List<string>();
5252
}
5353
var gle = Gle;
54-
return gle == null ? new List<string>() : gle.AvailableSwitches.Select(lamp => lamp.Id).ToList();
54+
return gle == null ? new List<string>() : gle.AvailableSwitches.Select(sw => sw.Id).ToList();
5555
}
5656
}
5757
}
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) 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+
[UnitTitle("Get Switch Value")]
23+
[UnitSurtitle("Gamelogic Engine")]
24+
[UnitCategory("Visual Pinball")]
25+
public class GetSwitchUnit : GleUnit
26+
{
27+
[DoNotSerialize]
28+
[PortLabel("Switch ID")]
29+
public ValueInput Id { get; private set; }
30+
31+
[DoNotSerialize]
32+
[PortLabel("Is Enabled")]
33+
public ValueOutput IsEnabled { get; private set; }
34+
35+
protected override void Definition()
36+
{
37+
Id = ValueInput(nameof(Id), string.Empty);
38+
39+
IsEnabled = ValueOutput(nameof(IsEnabled), GetEnabled);
40+
}
41+
42+
private bool GetEnabled(Flow flow)
43+
{
44+
if (!AssertGle(flow)) {
45+
Debug.LogError("Cannot find GLE.");
46+
return false;
47+
}
48+
49+
return Gle.GetSwitch(flow.GetValue<string>(Id));
50+
}
51+
}
52+
}

Runtime/Nodes/Switches/GetSwitchUnit.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.

0 commit comments

Comments
 (0)