Skip to content

Commit c55d46d

Browse files
committed
state: Make player variables types work (somewhat).
1 parent 79fa574 commit c55d46d

20 files changed

+233
-246
lines changed

Editor/Inspectors/VisualScriptingPlayerStatePropertyDefinitionInspector.cs renamed to Editor/Inspectors/PlayerVariableDefinitionInspector.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,45 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
using System.Collections.Generic;
1718
using System.Linq;
1819
using Unity.VisualScripting;
1920
using UnityEditor;
2021
using UnityEngine;
2122
using VisualPinball.Unity.VisualScripting;
2223

23-
[Inspector(typeof(VisualScriptingPlayerStatePropertyDefinition))]
24-
public class VisualScriptingPlayerStatePropertyDefinitionInspector : GleInspector
24+
[Inspector(typeof(PlayerVariableDefinition))]
25+
public class PlayerVariableDefinitionInspector : GleInspector
2526
{
26-
public VisualScriptingPlayerStatePropertyDefinitionInspector(Metadata metadata) : base(metadata) { }
27+
public PlayerVariableDefinitionInspector(Metadata metadata) : base(metadata) { }
2728

2829
protected override void OnGUI(Rect position, GUIContent label)
2930
{
3031
// can't get this from the flow
3132
var gle = Gle;
3233
if (gle != null) {
33-
var propDefinitions = gle.PlayerStateDefinition;
34-
35-
if (propDefinitions.Count == 0) {
36-
ErrorMessage = "No properties defined.";
34+
var varDefinitions = gle.PlayerVariableDefinitions;
35+
if (varDefinitions == null || varDefinitions.Count(p => !string.IsNullOrEmpty(p.Name)) == 0) {
36+
ErrorMessage = "No variables defined.";
3737

3838
} 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];
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;
4947
}
48+
49+
var newIndex = EditorGUI.Popup(position, currentIndex, varNames);
50+
metadata.RecordUndo();
51+
metadata.value = newIndex == 0 ? null : varDefinitions[newIndex - 1];
52+
ErrorMessage = null;
5053
}
5154
}
5255

53-
5456
if (ErrorMessage != null) {
5557
position.height -= EditorGUIUtility.standardVerticalSpacing;
5658
EditorGUI.HelpBox(position, ErrorMessage, MessageType.Error);

Runtime/Gamelogic/VisualScriptingPlayerState.cs.meta renamed to Editor/Inspectors/PlayerVariableDefinitionInspector.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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 UnityEditor;
19+
using UnityEngine;
20+
21+
namespace VisualPinball.Unity.VisualScripting.Editor
22+
{
23+
[CustomPropertyDrawer(typeof(PlayerVariableDefinition))]
24+
public class PlayerVariableDefinitionPropertyDrawer : PropertyDrawer
25+
{
26+
private const float Padding = 2f;
27+
28+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
29+
{
30+
return 3 * (EditorGUIUtility.singleLineHeight + Padding);
31+
}
32+
33+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
34+
{
35+
EditorGUI.BeginProperty(position, label, property);
36+
37+
var nameProperty = property.FindPropertyRelative(nameof(PlayerVariableDefinition.Name));
38+
var typeProperty = property.FindPropertyRelative(nameof(PlayerVariableDefinition.Type));
39+
var typeIndex = typeProperty.enumValueIndex;
40+
41+
SerializedProperty valueProp;
42+
switch (typeIndex) {
43+
case (int)VariableType.String:
44+
valueProp = property.FindPropertyRelative(nameof(PlayerVariableDefinition.StringDefaultValue));
45+
break;
46+
case (int)VariableType.Integer:
47+
valueProp = property.FindPropertyRelative(nameof(PlayerVariableDefinition.IntegerDefaultValue));
48+
break;
49+
case (int)VariableType.Float:
50+
valueProp = property.FindPropertyRelative(nameof(PlayerVariableDefinition.FloatDefaultValue));
51+
break;
52+
case (int)VariableType.Boolean:
53+
valueProp = property.FindPropertyRelative(nameof(PlayerVariableDefinition.BooleanDefaultValue));
54+
break;
55+
default:
56+
throw new ArgumentException($"Undefined type index {typeIndex}.");
57+
}
58+
59+
position.height = EditorGUIUtility.singleLineHeight;
60+
61+
EditorGUI.PropertyField(position, nameProperty, new GUIContent("Name:"));
62+
position.y += EditorGUIUtility.singleLineHeight + Padding;
63+
EditorGUI.PropertyField(position, typeProperty, new GUIContent("Type:"));
64+
position.y += EditorGUIUtility.singleLineHeight + Padding;
65+
EditorGUI.PropertyField(position, valueProp, new GUIContent("Initial Value:"));
66+
67+
EditorGUI.EndProperty();
68+
}
69+
}
70+
}
71+

Editor/Inspectors/VisualScriptingPlayerStatePropertyDefinitionInspector.cs.meta renamed to Editor/PropertyDrawers/PlayerVariableDefinitionPropertyDrawer.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.

Editor/PropertyDrawers/VisualScriptingPlayerStateDefinitionPropertyDrawer.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

Editor/Widgets/GleUnitWidget.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace VisualPinball.Unity.VisualScripting.Editor
2020
{
2121
public abstract class GleUnitWidget<TUnit> : UnitWidget<TUnit> where TUnit : Unit, IGleUnit
2222
{
23-
protected override NodeColorMix baseColor => GleAvailable ? NodeColorMix.TealReadable : new NodeColorMix { red = 1f, green = 0f, blue = 0f };
23+
protected override NodeColorMix baseColor => GleAvailable ? new NodeColorMix(NodeColor.Orange) : 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>();
2626
protected VisualScriptingGamelogicEngine VsGle => reference.gameObject.GetComponentInParent<VisualScriptingGamelogicEngine>();

Editor/Widgets/PlayerStateSetUnitWidget.cs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,44 @@
1616

1717
// ReSharper disable UnusedType.Global
1818

19-
using System;
20-
using System.Collections.Generic;
21-
using System.Linq;
2219
using Unity.VisualScripting;
2320

2421
namespace VisualPinball.Unity.VisualScripting.Editor
2522
{
26-
[Widget(typeof(PlayerStateSetUnit))]
27-
public sealed class PlayerStateSetUnitWidget : GleUnitWidget<PlayerStateSetUnit>
23+
[Widget(typeof(PlayerVariableSetUnit))]
24+
public sealed class PlayerStateSetUnitWidget : GleUnitWidget<PlayerVariableSetUnit>
2825
{
29-
public PlayerStateSetUnitWidget(FlowCanvas canvas, PlayerStateSetUnit unit) : base(canvas, unit)
26+
public PlayerStateSetUnitWidget(FlowCanvas canvas, PlayerVariableSetUnit unit) : base(canvas, unit)
3027
{
31-
_propertyNameInspectorConstructor = meta => new VariableNameInspector(meta, GetNameSuggestions);
28+
//_intInspectorConstructor = meta => new IntInspector(meta);
3229
}
3330

34-
protected override NodeColorMix baseColor => NodeColorMix.TealReadable;
31+
// private IntInspector _intInspector;
32+
//
33+
// private readonly Func<Metadata, IntInspector> _intInspectorConstructor;
34+
35+
// public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
36+
// {
37+
// if (port != unit.Value) {
38+
// return base.GetPortInspector(port, meta);
39+
// }
40+
//
41+
// switch (unit.Property.Type) {
42+
// case VisualScriptingPropertyType.String:
43+
// break;
44+
// case VisualScriptingPropertyType.Integer:
45+
// InspectorProvider.instance.Renew(ref _intInspector, meta, _intInspectorConstructor);
46+
// return _intInspector;
47+
// case VisualScriptingPropertyType.Float:
48+
// break;
49+
// case VisualScriptingPropertyType.Boolean:
50+
// break;
51+
// default:
52+
// throw new ArgumentOutOfRangeException();
53+
// }
54+
//
55+
// return base.GetPortInspector(port, meta);
56+
// }
3557

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-
}
5858
}
5959
}

Runtime/Gamelogic/VisualScriptingPlayerState.cs renamed to Runtime/Gamelogic/PlayerState.cs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,52 +20,50 @@
2020

2121
namespace VisualPinball.Unity.VisualScripting
2222
{
23-
public class VisualScriptingPlayerState
23+
public class PlayerState
2424
{
2525
public readonly int Id;
2626

27-
private readonly Dictionary<string, VisualScriptingPlayerStateProperty> _properties = new();
27+
private readonly Dictionary<string, PlayerVariable> _variables = new();
2828

29-
public VisualScriptingPlayerState(int id)
29+
public PlayerState(int id)
3030
{
3131
Id = id;
3232
}
3333

34-
public void AddProperty(VisualScriptingPlayerStateProperty property)
34+
public void AddProperty(PlayerVariable variable)
3535
{
36-
_properties[property.Name] = property;
36+
_variables[variable.Id] = variable;
3737
}
3838

39-
public T Get<T>(string propertyName) where T : class
39+
public T Get<T>(string variableId) where T : class
4040
{
41-
if (!_properties.ContainsKey(propertyName)) {
42-
throw new ArgumentException($"No such property name ({propertyName}).", nameof(propertyName));
41+
if (!_variables.ContainsKey(variableId)) {
42+
throw new ArgumentException($"No such variable ID ({variableId}).", nameof(variableId));
4343
}
44-
if (_properties[propertyName].Type != typeof(T)) {
45-
throw new InvalidOperationException($"Property \"{propertyName}\" is of type {_properties[propertyName].Type}, but you asked for a {typeof(T)}.");
44+
if (_variables[variableId].Type != typeof(T)) {
45+
throw new InvalidOperationException($"Variable \"{variableId}\" is of type {_variables[variableId].Type}, but you asked for a {typeof(T)}.");
4646
}
4747

48-
return _properties[propertyName].Get<T>();
48+
return _variables[variableId].Get<T>();
4949
}
5050

51-
public void Set<T>(string propertyName, T value) where T : class
51+
public void Set<T>(string variableId, T value) where T : class
5252
{
53-
if (!_properties.ContainsKey(propertyName)) {
54-
throw new ArgumentException($"No such property name ({propertyName}).", nameof(propertyName));
53+
if (!_variables.ContainsKey(variableId)) {
54+
throw new ArgumentException($"No such variable ID ({variableId}).", nameof(variableId));
5555
}
56-
if (_properties[propertyName].Type != value.GetType()) {
57-
throw new ArgumentException($"Property \"{propertyName}\" is of type {_properties[propertyName].Type}, but you provided a {value.GetType()}.", nameof(value));
56+
if (_variables[variableId].Type != value.GetType()) {
57+
throw new ArgumentException($"Variable \"{variableId}\" is of type {_variables[variableId].Type}, but you provided a {value.GetType()}.", nameof(value));
5858
}
5959

60-
var currentValue = _properties[propertyName].Get<T>();
61-
_properties[propertyName].Set(value);
60+
var currentValue = _variables[variableId].Get<T>();
61+
_variables[variableId].Set(value);
6262

6363
if (currentValue != value) {
64-
EventBus.Trigger(VisualScriptingEventNames.CurrentPlayerStateChanged, new PlayerStateChangedArgs(propertyName));
64+
EventBus.Trigger(VisualScriptingEventNames.CurrentPlayerStateChanged, new PlayerVariableChangedArgs(variableId));
6565
}
6666
}
67-
68-
internal bool Has<T>(string propertyName) => _properties.ContainsKey(propertyName) && typeof(T) == _properties[propertyName].Type;
6967
}
7068

7169
public class Integer
@@ -109,13 +107,13 @@ public Bool(bool value)
109107
public static implicit operator Bool(bool num) => new(num);
110108
}
111109

112-
public readonly struct PlayerStateChangedArgs
110+
public readonly struct PlayerVariableChangedArgs
113111
{
114-
public readonly string PropertyName;
112+
public readonly string VariableId;
115113

116-
public PlayerStateChangedArgs(string propertyName)
114+
public PlayerVariableChangedArgs(string variableId)
117115
{
118-
PropertyName = propertyName;
116+
VariableId = variableId;
119117
}
120118
}
121119
}

Runtime/Gamelogic/VisualScriptingPlayerStateDefinition.cs.meta renamed to Runtime/Gamelogic/PlayerState.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.

Runtime/Gamelogic/VisualScriptingPlayerStateProperty.cs renamed to Runtime/Gamelogic/PlayerVariable.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,29 @@
1818

1919
namespace VisualPinball.Unity.VisualScripting
2020
{
21-
public class VisualScriptingPlayerStateProperty
21+
public class PlayerVariable
2222
{
23+
public string Id;
2324
public string Name;
2425
public Type Type;
2526
private object _value;
2627

27-
public VisualScriptingPlayerStateProperty(string name, string initialValue) : this(name, typeof(string), initialValue)
28+
public PlayerVariable(string id, string name, string initialValue) : this(id, name, typeof(string), initialValue)
2829
{
2930
}
30-
public VisualScriptingPlayerStateProperty(string name, int initialValue) : this(name, typeof(Integer),new Integer(initialValue))
31+
public PlayerVariable(string id, string name, int initialValue) : this(id, name, typeof(Integer),new Integer(initialValue))
3132
{
3233
}
33-
public VisualScriptingPlayerStateProperty(string name, float initialValue) : this(name, typeof(Float), new Float(initialValue))
34+
public PlayerVariable(string id, string name, float initialValue) : this(id, name, typeof(Float), new Float(initialValue))
3435
{
3536
}
36-
public VisualScriptingPlayerStateProperty(string name, bool initialValue) : this(name, typeof(Bool), new Bool(initialValue))
37+
public PlayerVariable(string id, string name, bool initialValue) : this(id, name, typeof(Bool), new Bool(initialValue))
3738
{
3839
}
3940

40-
private VisualScriptingPlayerStateProperty(string name, Type type, object initialValue)
41+
private PlayerVariable(string id, string name, Type type, object initialValue)
4142
{
43+
Id = id;
4244
Name = name;
4345
Type = type;
4446
_value = initialValue;

0 commit comments

Comments
 (0)