Skip to content

Commit f580566

Browse files
committed
misc: implement clear display callbacks
1 parent 5f9294c commit f580566

8 files changed

Lines changed: 249 additions & 11 deletions
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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(ClearDisplayUnit))]
26+
public class ClearDisplayUnitDescriptor : UnitDescriptor<ClearDisplayUnit>
27+
{
28+
public ClearDisplayUnitDescriptor(ClearDisplayUnit target) : base(target)
29+
{
30+
}
31+
32+
protected override string DefinedSummary()
33+
{
34+
return "This node clears a display connected through the given ID.";
35+
}
36+
37+
protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.UpdateDisplay);
38+
39+
protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
40+
{
41+
base.DefinedPort(port, desc);
42+
}
43+
}
44+
}

Editor/Descriptors/ClearDisplayUnitDescriptor.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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
[Widget(typeof(ClearDisplayUnit))]
24+
public sealed class ClearDisplayUnitWidget : GleUnitWidget<ClearDisplayUnit>
25+
{
26+
public ClearDisplayUnitWidget(FlowCanvas canvas, ClearDisplayUnit unit) : base(canvas, unit)
27+
{
28+
}
29+
}
30+
}

Editor/Widgets/ClearDisplayUnitWidget.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/Gamelogic/VisualScriptingGamelogicEngine.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public class VisualScriptingGamelogicEngine : MonoBehaviour, IGamelogicEngine, I
6464
public GamelogicEngineWire[] AvailableWires => Wires;
6565

6666
public event EventHandler<RequestedDisplays> OnDisplaysRequested;
67-
public event EventHandler<DisplayFrameData> OnDisplayFrame;
67+
public event EventHandler<DisplayFrameData> OnUpdateFrame;
68+
public event EventHandler<ClearDisplayData> OnClearFrame;
6869
public event EventHandler<LampEventArgs> OnLampChanged;
6970
public event EventHandler<LampsEventArgs> OnLampsChanged;
7071
public event EventHandler<CoilEventArgs> OnCoilChanged;
@@ -77,7 +78,7 @@ public class VisualScriptingGamelogicEngine : MonoBehaviour, IGamelogicEngine, I
7778
[NonSerialized] private int _currentPlayer;
7879
[NonSerialized] public readonly TableState TableState = new ();
7980
[NonSerialized] public readonly Dictionary<int, PlayerState> PlayerStates = new ();
80-
81+
8182
public PlayerState CurrentPlayerState {
8283
get {
8384
if (!PlayerStates.ContainsKey(_currentPlayer)) {
@@ -87,9 +88,14 @@ public PlayerState CurrentPlayerState {
8788
}
8889
}
8990

90-
public void DisplayFrame(DisplayFrameData data)
91+
public void UpdateFrame(DisplayFrameData data)
92+
{
93+
OnUpdateFrame?.Invoke(this, data);
94+
}
95+
96+
public void ClearFrame(ClearDisplayData data)
9197
{
92-
OnDisplayFrame?.Invoke(this, data);
98+
OnClearFrame?.Invoke(this, data);
9399
}
94100

95101
public void SetCurrentPlayer(int value, bool forceNotify = false)
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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;
18+
using System.Collections;
19+
using System.Collections.Generic;
20+
using Unity.VisualScripting;
21+
using UnityEngine;
22+
23+
namespace VisualPinball.Unity.VisualScripting
24+
{
25+
[UnitTitle("Clear Display")]
26+
[UnitSurtitle("Gamelogic Engine")]
27+
[UnitCategory("Visual Pinball")]
28+
public class ClearDisplayUnit : GleUnit
29+
{
30+
[Serialize, Inspectable, UnitHeaderInspectable("ID")]
31+
public DisplayDefinition Display { get; private set; }
32+
33+
[DoNotSerialize]
34+
[PortLabelHidden]
35+
public ControlInput InputTrigger;
36+
37+
[DoNotSerialize]
38+
[PortLabelHidden]
39+
public ControlOutput OutputTrigger;
40+
41+
[DoNotSerialize]
42+
public ControlOutput started;
43+
44+
[DoNotSerialize]
45+
public ControlOutput stopped;
46+
47+
[DoNotSerialize]
48+
public ControlOutput pulse;
49+
50+
private Queue<int> queue = new Queue<int>();
51+
52+
protected override void Definition()
53+
{
54+
InputTrigger = ControlInputCoroutine(nameof(InputTrigger), Process);
55+
OutputTrigger = ControlOutput(nameof(OutputTrigger));
56+
57+
started = ControlOutput(nameof(started));
58+
stopped = ControlOutput(nameof(stopped));
59+
pulse = ControlOutput(nameof(pulse));
60+
61+
Succession(InputTrigger, OutputTrigger);
62+
}
63+
64+
private IEnumerator Process(Flow flow)
65+
{
66+
if (!AssertVsGle(flow)) {
67+
throw new InvalidOperationException("Cannot retrieve GLE from unit.");
68+
}
69+
70+
yield return OutputTrigger;
71+
72+
if (Display != null) {
73+
bool _running = true;
74+
75+
queue.Clear();
76+
77+
VsGle.ClearFrame(new ClearDisplayData(Display.Id, HandleStarted, HandleStopped, HandlePulse));
78+
79+
while (_running)
80+
{
81+
yield return new WaitUntil(() => queue.Count != 0);
82+
83+
int status = queue.Dequeue();
84+
85+
if (status == 1)
86+
{
87+
yield return started;
88+
}
89+
else if (status == 2)
90+
{
91+
_running = false;
92+
93+
yield return stopped;
94+
}
95+
else if (status == 3)
96+
{
97+
yield return pulse;
98+
}
99+
}
100+
}
101+
}
102+
103+
private void HandleStarted(object sender, EventArgs args)
104+
{
105+
queue.Enqueue(1);
106+
107+
Debug.Log("HandleStarted!");
108+
}
109+
110+
111+
private void HandleStopped(object sender, EventArgs args)
112+
{
113+
queue.Enqueue(2);
114+
115+
Debug.Log("HandleStopped!");
116+
}
117+
118+
private void HandlePulse(object sender, EventArgs args)
119+
{
120+
queue.Enqueue(3);
121+
122+
Debug.Log("HandlePulse!");
123+
}
124+
}
125+
}

Runtime/Nodes/Display/ClearDisplayUnit.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/Display/UpdateDisplayUnit.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,42 +105,42 @@ private ControlOutput Process(Flow flow)
105105
if (Display != null) {
106106
if (Display.Supports(DisplayFrameFormat.Numeric) && NumericInput.hasValidConnection) {
107107
var numValue = flow.GetValue<float>(NumericInput);
108-
VsGle.DisplayFrame(new DisplayFrameData(Display.Id, DisplayFrameFormat.Numeric, BitConverter.GetBytes(numValue)));
108+
VsGle.UpdateFrame(new DisplayFrameData(Display.Id, DisplayFrameFormat.Numeric, BitConverter.GetBytes(numValue)));
109109
}
110110
if (Display.Supports(DisplayFrameFormat.AlphaNumeric) && flow.IsLocal(TextInput)) {
111111
var strValue = flow.GetValue<string>(TextInput);
112112
if (!string.IsNullOrEmpty(strValue)) {
113-
VsGle.DisplayFrame(new DisplayFrameData(Display.Id, DisplayFrameFormat.AlphaNumeric, Encoding.UTF8.GetBytes(strValue)));
113+
VsGle.UpdateFrame(new DisplayFrameData(Display.Id, DisplayFrameFormat.AlphaNumeric, Encoding.UTF8.GetBytes(strValue)));
114114
}
115115
}
116116
if (Display.Supports(DisplayFrameFormat.Segment) && SegmentInput.hasValidConnection) {
117117
var byteValue = flow.GetValue<byte[]>(SegmentInput);
118118
if (byteValue is { Length: > 0 }) {
119-
VsGle.DisplayFrame(new DisplayFrameData(Display.Id, DisplayFrameFormat.Segment, byteValue));
119+
VsGle.UpdateFrame(new DisplayFrameData(Display.Id, DisplayFrameFormat.Segment, byteValue));
120120
}
121121
}
122122
if (Display.Supports(DisplayFrameFormat.Dmd2) && Dmd2Input.hasValidConnection) {
123123
var byteValue = flow.GetValue<byte[]>(Dmd2Input);
124124
if (byteValue is { Length: > 0 }) {
125-
VsGle.DisplayFrame(new DisplayFrameData(Display.Id, DisplayFrameFormat.Dmd2, byteValue));
125+
VsGle.UpdateFrame(new DisplayFrameData(Display.Id, DisplayFrameFormat.Dmd2, byteValue));
126126
}
127127
}
128128
if (Display.Supports(DisplayFrameFormat.Dmd4) && Dmd4Input.hasValidConnection) {
129129
var byteValue = flow.GetValue<byte[]>(Dmd4Input);
130130
if (byteValue is { Length: > 0 }) {
131-
VsGle.DisplayFrame(new DisplayFrameData(Display.Id, DisplayFrameFormat.Dmd4, byteValue));
131+
VsGle.UpdateFrame(new DisplayFrameData(Display.Id, DisplayFrameFormat.Dmd4, byteValue));
132132
}
133133
}
134134
if (Display.Supports(DisplayFrameFormat.Dmd8) && Dmd8Input.hasValidConnection) {
135135
var byteValue = flow.GetValue<byte[]>(Dmd8Input);
136136
if (byteValue is { Length: > 0 }) {
137-
VsGle.DisplayFrame(new DisplayFrameData(Display.Id, DisplayFrameFormat.Dmd8, byteValue));
137+
VsGle.UpdateFrame(new DisplayFrameData(Display.Id, DisplayFrameFormat.Dmd8, byteValue));
138138
}
139139
}
140140
if (Display.Supports(DisplayFrameFormat.Dmd24) && Dmd24Input.hasValidConnection) {
141141
var byteValue = flow.GetValue<byte[]>(Dmd24Input);
142142
if (byteValue is { Length: > 0 }) {
143-
VsGle.DisplayFrame(new DisplayFrameData(Display.Id, DisplayFrameFormat.Dmd24, byteValue));
143+
VsGle.UpdateFrame(new DisplayFrameData(Display.Id, DisplayFrameFormat.Dmd24, byteValue));
144144
}
145145
}
146146
}

0 commit comments

Comments
 (0)