Skip to content

Commit 1eb88cd

Browse files
committed
Add pinball events.
1 parent f03cc57 commit 1eb88cd

18 files changed

+467
-3
lines changed

Editor/Descriptors/CreateBallUnitDescriptor.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ protected override string DefinedSummary()
3434
return "This node spawns a new ball at a given position.";
3535
}
3636

37-
38-
3937
protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.BallRoller(IconSize.Large, IconColor.Orange));
4038

4139
protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.Linq;
19+
using Unity.VisualScripting;
20+
using UnityEditor;
21+
using UnityEngine;
22+
23+
namespace VisualPinball.Unity.VisualScripting.Editor
24+
{
25+
[Inspector(typeof(EventDefinition))]
26+
public class EventDefinitionInspector : GleInspector
27+
{
28+
public EventDefinitionInspector(Metadata metadata) : base(metadata) { }
29+
30+
protected override void OnGUI(Rect position, GUIContent label)
31+
{
32+
// can't get this from the flow
33+
var gle = Gle;
34+
if (gle != null) {
35+
var eventDefinitions = gle.EventDefinitions;
36+
if (eventDefinitions == null || eventDefinitions.Count(p => !string.IsNullOrEmpty(p.Name)) == 0) {
37+
ErrorMessage = "No events defined.";
38+
39+
} else {
40+
var eventNames = new List<string> { "None" }
41+
.Concat(eventDefinitions.Select(d => d.Name))
42+
.ToArray();
43+
var currentEventDef = metadata.value as EventDefinition;
44+
var currentIndex = 0;
45+
if (currentEventDef != null) {
46+
var eventDef = eventDefinitions.FirstOrDefault(p => p.Id == currentEventDef!.Id);
47+
currentIndex = eventDef != null ? eventDefinitions.IndexOf(eventDef) + 1 : 0;
48+
}
49+
50+
var newIndex = EditorGUI.Popup(position, currentIndex, eventNames);
51+
metadata.RecordUndo();
52+
metadata.value = newIndex == 0 ? null : eventDefinitions[newIndex - 1];
53+
ErrorMessage = null;
54+
}
55+
}
56+
57+
if (ErrorMessage != null) {
58+
position.height -= EditorGUIUtility.standardVerticalSpacing;
59+
EditorGUI.HelpBox(position, ErrorMessage, MessageType.Error);
60+
}
61+
}
62+
63+
public override float GetAdaptiveWidth() => LudiqGUIUtility.currentInspectorWidth;
64+
65+
protected override float GetHeight(float width, GUIContent label)
66+
{
67+
if (ErrorMessage != null) {
68+
var height = LudiqGUIUtility.GetHelpBoxHeight(ErrorMessage, MessageType.Error, width);
69+
height += EditorGUIUtility.standardVerticalSpacing;
70+
return height;
71+
}
72+
73+
return EditorGUIUtility.singleLineHeight;
74+
}
75+
}
76+
}

Editor/Inspectors/EventDefinitionInspector.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/Inspectors/VisualScriptingGamelogicEngineInspector.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class VisualScriptingGamelogicEngineInspector : BaseEditor<VisualScriptin
3333
private SerializedProperty _lampsProperty;
3434
private SerializedProperty _tableVariableDefinitionsProperty;
3535
private SerializedProperty _playerVariableDefinitionsProperty;
36+
private SerializedProperty _eventDefinitionsProperty;
3637

3738
private readonly Dictionary<int, bool> _playerVarFoldout = new();
3839

@@ -47,6 +48,8 @@ private void OnEnable()
4748

4849
_tableVariableDefinitionsProperty = serializedObject.FindProperty(nameof(VisualScriptingGamelogicEngine.TableVariableDefinitions));
4950
_playerVariableDefinitionsProperty = serializedObject.FindProperty(nameof(VisualScriptingGamelogicEngine.PlayerVariableDefinitions));
51+
52+
_eventDefinitionsProperty = serializedObject.FindProperty(nameof(VisualScriptingGamelogicEngine.EventDefinitions));
5053
}
5154

5255
public override void OnInspectorGUI()
@@ -60,6 +63,7 @@ public override void OnInspectorGUI()
6063

6164
EditorGUILayout.PropertyField(_tableVariableDefinitionsProperty);
6265
EditorGUILayout.PropertyField(_playerVariableDefinitionsProperty);
66+
EditorGUILayout.PropertyField(_eventDefinitionsProperty);
6367

6468
serializedObject.ApplyModifiedProperties();
6569

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+
using UnityEditor;
18+
using UnityEngine;
19+
20+
namespace VisualPinball.Unity.VisualScripting.Editor
21+
{
22+
[CustomPropertyDrawer(typeof(EventDefinition))]
23+
public class EventDefinitionPropertyDrawer : PropertyDrawer
24+
{
25+
private const float Padding = 2f;
26+
27+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
28+
{
29+
return EditorGUIUtility.singleLineHeight + Padding;
30+
}
31+
32+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
33+
{
34+
EditorGUI.BeginProperty(position, label, property);
35+
36+
var nameProperty = property.FindPropertyRelative(nameof(EventDefinition.Name));
37+
position.height = EditorGUIUtility.singleLineHeight;
38+
EditorGUI.PropertyField(position, nameProperty);
39+
40+
EditorGUI.EndProperty();
41+
}
42+
}
43+
}
44+

Editor/PropertyDrawers/EventDefinitionPropertyDrawer.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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
using UnityEngine;
24+
25+
namespace VisualPinball.Unity.VisualScripting.Editor
26+
{
27+
[Widget(typeof(PinballEventUnit))]
28+
public sealed class CustomEventUnitWidget : UnitWidget<PinballEventUnit>
29+
{
30+
public CustomEventUnitWidget(FlowCanvas canvas, PinballEventUnit unit) : base(canvas, unit)
31+
{
32+
}
33+
}
34+
}

Editor/Widgets/CustomEventUnitWidget.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/GleMultiUnitWidget.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected GleMultiUnitWidget(FlowCanvas canvas, TUnit unit) : base(canvas, unit)
3434

3535
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
3636
{
37-
if (_idInspectorConstructorList.Count() < unit.inputCount) {
37+
if (_idInspectorConstructorList.Count < unit.inputCount) {
3838
for (var index = 0; index < unit.inputCount - _idInspectorConstructorList.Count(); index++) {
3939
_idInspectorConstructorList.Add(m => new VariableNameInspector(m, GetNameSuggestions));
4040
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 InconsistentNaming
18+
19+
using System;
20+
21+
namespace VisualPinball.Unity.VisualScripting
22+
{
23+
[Serializable]
24+
public class EventDefinition
25+
{
26+
public string Name;
27+
public string Id;
28+
29+
public bool HasId => !string.IsNullOrEmpty(Id);
30+
public void GenerateId() => Id = Guid.NewGuid().ToString()[..13];
31+
}
32+
}

0 commit comments

Comments
 (0)