Skip to content

Commit 79fa574

Browse files
committed
Add set player state unit that allows selecting the variable.
1 parent bf4168d commit 79fa574

13 files changed

+351
-1
lines changed

Editor/Inspectors/GleInspector.cs

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+
using Unity.VisualScripting;
18+
using VisualPinball.Unity;
19+
using VisualPinball.Unity.VisualScripting;
20+
21+
public abstract class GleInspector : Inspector
22+
{
23+
protected GleInspector(Metadata metadata) : base(metadata) { }
24+
25+
private const string NoGleError = "Unable to find Gamelogic Engine in scene.";
26+
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;
38+
39+
} else if (_gle != null && ErrorMessage == NoGleError) {
40+
ErrorMessage = null;
41+
}
42+
return _gle;
43+
}
44+
}
45+
protected string ErrorMessage;
46+
47+
private VisualScriptingGamelogicEngine _gle;
48+
}

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

Editor/Inspectors/VisualScriptingPlayerStatePropertyDefinitionInspector.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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
//
19+
// namespace VisualPinball.Unity.VisualScripting.Editor
20+
// {
21+
// [CustomPropertyDrawer(typeof(VisualScriptingCoil))]
22+
// public class VisualScriptingPlayerStateDefinitionPropertyDrawer : PropertyDrawer
23+
// {
24+
// }
25+
// }
26+

Editor/PropertyDrawers/VisualScriptingPlayerStateDefinitionPropertyDrawer.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/GleUnitWidget.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public abstract class GleUnitWidget<TUnit> : UnitWidget<TUnit> where TUnit : Uni
2323
protected override NodeColorMix baseColor => GleAvailable ? NodeColorMix.TealReadable : new NodeColorMix { red = 1f, green = 0f, blue = 0f };
2424
protected bool GameObjectAvailable => reference != null && reference.gameObject != null;
2525
protected IGamelogicEngine Gle => reference.gameObject.GetComponentInParent<IGamelogicEngine>();
26+
protected VisualScriptingGamelogicEngine VsGle => reference.gameObject.GetComponentInParent<VisualScriptingGamelogicEngine>();
2627
private bool GleAvailable => GameObjectAvailable && Gle != null;
28+
private bool VsGleAvailable => GameObjectAvailable && VsGle != null;
2729

2830
protected GleUnitWidget(FlowCanvas canvas, TUnit unit) : base(canvas, unit)
2931
{
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(PlayerStateSetUnit))]
27+
public sealed class PlayerStateSetUnitWidget : GleUnitWidget<PlayerStateSetUnit>
28+
{
29+
public PlayerStateSetUnitWidget(FlowCanvas canvas, PlayerStateSetUnit unit) : base(canvas, unit)
30+
{
31+
_propertyNameInspectorConstructor = meta => new VariableNameInspector(meta, GetNameSuggestions);
32+
}
33+
34+
protected override NodeColorMix baseColor => NodeColorMix.TealReadable;
35+
36+
private VariableNameInspector _propertyNameInspector;
37+
private readonly Func<Metadata, VariableNameInspector> _propertyNameInspectorConstructor;
38+
39+
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
40+
{
41+
// if (port == unit.PropertyName) {
42+
// InspectorProvider.instance.Renew(ref _propertyNameInspector, meta, _propertyNameInspectorConstructor);
43+
//
44+
// return _propertyNameInspector;
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 = VsGle;
56+
return gle == null ? new List<string>() : gle.PlayerStateDefinition.Select(prop => prop.Name).ToList();
57+
}
58+
}
59+
}

Editor/Widgets/PlayerStateSetUnitWidget.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: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace VisualPinball.Unity.VisualScripting
2727
{
2828
[DisallowMultipleComponent]
2929
[AddComponentMenu("Visual Pinball/Gamelogic Engine/Visual Scripting Game Logic")]
30-
public class VisualScriptingGamelogicEngine : MonoBehaviour, IGamelogicEngine
30+
public class VisualScriptingGamelogicEngine : MonoBehaviour, IGamelogicEngine, ISerializationCallbackReceiver
3131
{
3232
public string Name => "Visual Scripting Gamelogic Engine";
3333

@@ -151,5 +151,23 @@ public bool GetCoil(string id)
151151
{
152152
return _player.CoilStatuses.ContainsKey(id) && _player.CoilStatuses[id];
153153
}
154+
155+
156+
public void OnBeforeSerialize()
157+
{
158+
#if UNITY_EDITOR
159+
160+
var ids = new HashSet<string>();
161+
foreach (var def in PlayerStateDefinition) {
162+
if (!def.HasId || ids.Contains(def.Id)) {
163+
def.GenerateId();
164+
}
165+
ids.Add(def.Id);
166+
}
167+
#endif
168+
}
169+
public void OnAfterDeserialize()
170+
{
171+
}
154172
}
155173
}

0 commit comments

Comments
 (0)