Skip to content

Commit 0e3f103

Browse files
committed
More player state units.
1 parent c55d46d commit 0e3f103

40 files changed

+960
-161
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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(PlayerStateChangeUnit))]
24+
public class PlayerStateChangeUnitDescriptor : UnitDescriptor<PlayerStateChangeUnit>
25+
{
26+
public PlayerStateChangeUnitDescriptor(PlayerStateChangeUnit target) : base(target)
27+
{
28+
}
29+
30+
protected override string DefinedSummary()
31+
{
32+
return "This node changes the current player state with another one.";
33+
}
34+
35+
//protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.BallRoller(IconSize.Large, IconColor.Orange));
36+
37+
protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
38+
{
39+
base.DefinedPort(port, desc);
40+
41+
switch (port.key) {
42+
case nameof(PlayerStateChangeUnit.PlayerId):
43+
desc.summary = "The player ID of the desired state";
44+
break;
45+
}
46+
}
47+
}
48+
}

Editor/Widgets/PlayerStateSetUnitWidget.cs.meta renamed to Editor/Descriptors/PlayerStateChangeUnitDescriptor.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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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(PlayerStateCreateUnit))]
24+
public class PlayerStateCreateUnitDescriptor : UnitDescriptor<PlayerStateCreateUnit>
25+
{
26+
public PlayerStateCreateUnitDescriptor(PlayerStateCreateUnit target) : base(target)
27+
{
28+
}
29+
30+
protected override string DefinedSummary()
31+
{
32+
return "This node creates a new player state.";
33+
}
34+
35+
//protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.BallRoller(IconSize.Large, IconColor.Orange));
36+
37+
protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
38+
{
39+
base.DefinedPort(port, desc);
40+
41+
switch (port.key) {
42+
case nameof(PlayerStateCreateUnit.PlayerId):
43+
desc.summary = "The player ID of the new state";
44+
break;
45+
// case nameof(PlayerStateCreateUnit.AutoIncrement):
46+
// desc.summary = "If set, the new player ID will be the currently largest ID, plus one.";
47+
// break;
48+
case nameof(PlayerStateCreateUnit.SetAsActive):
49+
desc.summary = "If set, the new state will be the current state. Otherwise, it will only be the current state if there is no state set.";
50+
break;
51+
}
52+
}
53+
}
54+
}

Editor/Descriptors/PlayerStateCreateUnitDescriptor.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/GleInspector.cs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,34 @@
1818
using VisualPinball.Unity;
1919
using VisualPinball.Unity.VisualScripting;
2020

21-
public abstract class GleInspector : Inspector
21+
namespace VisualPinball.Unity.VisualScripting.Editor
2222
{
23-
protected GleInspector(Metadata metadata) : base(metadata) { }
23+
public abstract class GleInspector : Inspector
24+
{
25+
protected GleInspector(Metadata metadata) : base(metadata) { }
2426

25-
private const string NoGleError = "Unable to find Gamelogic Engine in scene.";
27+
private const string NoGleError = "Unable to find Gamelogic Engine in scene.";
2628

27-
protected VisualScriptingGamelogicEngine Gle {
28-
get {
29-
if (_gle != null) {
30-
return _gle;
31-
}
32-
var tableComponent = TableSelector.Instance.SelectedOrFirstTable;
33-
if (tableComponent != null) {
34-
_gle = tableComponent.GetComponentInChildren<VisualScriptingGamelogicEngine>();
35-
}
36-
if (_gle == null && ErrorMessage == null) {
37-
ErrorMessage = NoGleError;
29+
protected VisualScriptingGamelogicEngine Gle {
30+
get {
31+
if (_gle != null) {
32+
return _gle;
33+
}
34+
var tableComponent = TableSelector.Instance.SelectedOrFirstTable;
35+
if (tableComponent != null) {
36+
_gle = tableComponent.GetComponentInChildren<VisualScriptingGamelogicEngine>();
37+
}
38+
if (_gle == null && ErrorMessage == null) {
39+
ErrorMessage = NoGleError;
3840

39-
} else if (_gle != null && ErrorMessage == NoGleError) {
40-
ErrorMessage = null;
41+
} else if (_gle != null && ErrorMessage == NoGleError) {
42+
ErrorMessage = null;
43+
}
44+
return _gle;
4145
}
42-
return _gle;
4346
}
44-
}
45-
protected string ErrorMessage;
47+
protected string ErrorMessage;
4648

47-
private VisualScriptingGamelogicEngine _gle;
49+
private VisualScriptingGamelogicEngine _gle;
50+
}
4851
}

Editor/Inspectors/PlayerVariableDefinitionInspector.cs

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,56 +19,59 @@
1919
using Unity.VisualScripting;
2020
using UnityEditor;
2121
using UnityEngine;
22-
using VisualPinball.Unity.VisualScripting;
2322

24-
[Inspector(typeof(PlayerVariableDefinition))]
25-
public class PlayerVariableDefinitionInspector : GleInspector
23+
namespace VisualPinball.Unity.VisualScripting.Editor
2624
{
27-
public PlayerVariableDefinitionInspector(Metadata metadata) : base(metadata) { }
2825

29-
protected override void OnGUI(Rect position, GUIContent label)
26+
[Inspector(typeof(PlayerVariableDefinition))]
27+
public class PlayerVariableDefinitionInspector : GleInspector
3028
{
31-
// can't get this from the flow
32-
var gle = Gle;
33-
if (gle != null) {
34-
var varDefinitions = gle.PlayerVariableDefinitions;
35-
if (varDefinitions == null || varDefinitions.Count(p => !string.IsNullOrEmpty(p.Name)) == 0) {
36-
ErrorMessage = "No variables defined.";
29+
public PlayerVariableDefinitionInspector(Metadata metadata) : base(metadata) { }
3730

38-
} else {
39-
var varNames = new List<string> { "None" }
40-
.Concat(varDefinitions.Select(d => d.Name))
41-
.ToArray();
42-
var currentVarDef = metadata.value as PlayerVariableDefinition;
43-
var currentIndex = 0;
44-
if (currentVarDef != null) {
45-
var playerVarDef = varDefinitions.FirstOrDefault(p => p.Id == currentVarDef!.Id);
46-
currentIndex = playerVarDef != null ? varDefinitions.IndexOf(playerVarDef) + 1 : 0;
31+
protected override void OnGUI(Rect position, GUIContent label)
32+
{
33+
// can't get this from the flow
34+
var gle = Gle;
35+
if (gle != null) {
36+
var varDefinitions = gle.PlayerVariableDefinitions;
37+
if (varDefinitions == null || varDefinitions.Count(p => !string.IsNullOrEmpty(p.Name)) == 0) {
38+
ErrorMessage = "No variables defined.";
39+
40+
} else {
41+
var varNames = new List<string> { "None" }
42+
.Concat(varDefinitions.Select(d => d.Name))
43+
.ToArray();
44+
var currentVarDef = metadata.value as PlayerVariableDefinition;
45+
var currentIndex = 0;
46+
if (currentVarDef != null) {
47+
var playerVarDef = varDefinitions.FirstOrDefault(p => p.Id == currentVarDef!.Id);
48+
currentIndex = playerVarDef != null ? varDefinitions.IndexOf(playerVarDef) + 1 : 0;
49+
}
50+
51+
var newIndex = EditorGUI.Popup(position, currentIndex, varNames);
52+
metadata.RecordUndo();
53+
metadata.value = newIndex == 0 ? null : varDefinitions[newIndex - 1];
54+
ErrorMessage = null;
4755
}
56+
}
4857

49-
var newIndex = EditorGUI.Popup(position, currentIndex, varNames);
50-
metadata.RecordUndo();
51-
metadata.value = newIndex == 0 ? null : varDefinitions[newIndex - 1];
52-
ErrorMessage = null;
58+
if (ErrorMessage != null) {
59+
position.height -= EditorGUIUtility.standardVerticalSpacing;
60+
EditorGUI.HelpBox(position, ErrorMessage, MessageType.Error);
5361
}
5462
}
5563

56-
if (ErrorMessage != null) {
57-
position.height -= EditorGUIUtility.standardVerticalSpacing;
58-
EditorGUI.HelpBox(position, ErrorMessage, MessageType.Error);
59-
}
60-
}
64+
public override float GetAdaptiveWidth() => LudiqGUIUtility.currentInspectorWidth;
6165

62-
public override float GetAdaptiveWidth() => LudiqGUIUtility.currentInspectorWidth;
66+
protected override float GetHeight(float width, GUIContent label)
67+
{
68+
if (ErrorMessage != null) {
69+
var height = LudiqGUIUtility.GetHelpBoxHeight(ErrorMessage, MessageType.Error, width);
70+
height += EditorGUIUtility.standardVerticalSpacing;
71+
return height;
72+
}
6373

64-
protected override float GetHeight(float width, GUIContent label)
65-
{
66-
if (ErrorMessage != null) {
67-
var height = LudiqGUIUtility.GetHelpBoxHeight(ErrorMessage, MessageType.Error, width);
68-
height += EditorGUIUtility.standardVerticalSpacing;
69-
return height;
74+
return EditorGUIUtility.singleLineHeight;
7075
}
71-
72-
return EditorGUIUtility.singleLineHeight;
7376
}
7477
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 AssignmentInConditionalExpression
18+
19+
using System.Collections.Generic;
20+
using UnityEditor;
21+
using UnityEngine;
22+
using VisualPinball.Unity.Editor;
23+
24+
namespace VisualPinball.Unity.VisualScripting.Editor
25+
{
26+
[CustomEditor(typeof(VisualScriptingGamelogicEngine))]
27+
public class VisualScriptingGamelogicEngineInspector : BaseEditor<VisualScriptingGamelogicEngine>
28+
{
29+
private VisualScriptingGamelogicEngine _gle;
30+
private SerializedProperty _switchesProperty;
31+
private SerializedProperty _soilsProperty;
32+
private SerializedProperty _lampsProperty;
33+
private SerializedProperty _playerVariableDefinitionsProperty;
34+
35+
private readonly Dictionary<int, bool> _playerVarFoldout = new();
36+
37+
private void OnEnable()
38+
{
39+
_gle = target as VisualScriptingGamelogicEngine;
40+
41+
_switchesProperty = serializedObject.FindProperty(nameof(VisualScriptingGamelogicEngine.Switches));
42+
_soilsProperty = serializedObject.FindProperty(nameof(VisualScriptingGamelogicEngine.Coils));
43+
_lampsProperty = serializedObject.FindProperty(nameof(VisualScriptingGamelogicEngine.Lamps));
44+
45+
_playerVariableDefinitionsProperty = serializedObject.FindProperty(nameof(VisualScriptingGamelogicEngine.PlayerVariableDefinitions));
46+
}
47+
48+
public override void OnInspectorGUI()
49+
{
50+
serializedObject.Update();
51+
52+
EditorGUILayout.PropertyField(_switchesProperty);
53+
EditorGUILayout.PropertyField(_soilsProperty);
54+
EditorGUILayout.PropertyField(_lampsProperty);
55+
56+
EditorGUILayout.PropertyField(_playerVariableDefinitionsProperty);
57+
58+
serializedObject.ApplyModifiedProperties();
59+
60+
// what follows is runtime data
61+
if (!Application.isPlaying) {
62+
return;
63+
}
64+
if (_gle.PlayerStates.Count == 0) {
65+
EditorGUILayout.HelpBox("No player states created.", MessageType.Info);
66+
return;
67+
}
68+
69+
EditorGUILayout.TextField("Player States", new GUIStyle(EditorStyles.boldLabel));
70+
foreach (var playerId in _gle.PlayerStates.Keys) {
71+
if (!_playerVarFoldout.ContainsKey(playerId)) {
72+
_playerVarFoldout[playerId] = true;
73+
}
74+
if (_playerVarFoldout[playerId] = EditorGUILayout.BeginFoldoutHeaderGroup(_playerVarFoldout[playerId], $"Player {playerId}")) {
75+
EditorGUI.indentLevel++;
76+
77+
var playerState = _gle.PlayerStates[playerId];
78+
if (_gle.CurrentPlayerState == playerState) {
79+
EditorGUILayout.HelpBox("Current Player", MessageType.Info);
80+
}
81+
82+
foreach (var varDef in _gle.PlayerVariableDefinitions) {
83+
EditorGUILayout.LabelField(varDef.Name, playerState.Get(varDef.Id).ToString());
84+
}
85+
86+
EditorGUI.indentLevel--;
87+
}
88+
EditorGUILayout.EndFoldoutHeaderGroup();
89+
}
90+
}
91+
}
92+
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ public GetLampUnitWidget(FlowCanvas canvas, GetLampUnit unit) : base(canvas, uni
3131
_lampIdInspectorConstructor = meta => new VariableNameInspector(meta, GetNameSuggestions);
3232
}
3333

34-
protected override NodeColorMix baseColor => NodeColorMix.TealReadable;
35-
3634
private VariableNameInspector _lampIdInspector;
3735
private readonly Func<Metadata, VariableNameInspector> _lampIdInspectorConstructor;
3836

Editor/Widgets/GetSwitchUnitWidget.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ public GetSwitchUnitWidget(FlowCanvas canvas, GetSwitchUnit unit) : base(canvas,
3333
_switchIdInspectorConstructorList = new List<Func<Metadata, VariableNameInspector>>();
3434
}
3535

36-
protected override NodeColorMix baseColor => NodeColorMix.TealReadable;
37-
3836
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
3937
{
4038
if (_switchIdInspectorConstructorList.Count() < unit.idCount) {

0 commit comments

Comments
 (0)